diff --git a/util/Color.cc b/util/Color.cc index ca358f1..92904ac 100644 --- a/util/Color.cc +++ b/util/Color.cc @@ -10,109 +10,6 @@ const Color Color::yellow = Color(1, 1, 0); const Color Color::cyan = Color(0, 1, 1); const Color Color::magenta = Color(1, 0, 1); -Color::Color() -{ - r = g = b = 0.0; -} - -Color::Color(double r, double g, double b) -{ - this->r = r; - this->g = g; - this->b = b; -} - -Color::Color(const Vector & v) -{ - r = v[0]; - g = v[1]; - b = v[2]; -} - -Color::Color(refptr v) -{ - r = (*v)[0]; - g = (*v)[1]; - b = (*v)[2]; -} - -Color Color::operator*(const Color & other) const -{ - Color result; - result.r = r * other.r; - result.g = g * other.g; - result.b = b * other.b; - return result; -} - -Color Color::operator*(double scale) const -{ - return Color(r * scale, g * scale, b * scale); -} - -Color Color::operator/(double scale) const -{ - return Color(r / scale, g / scale, b / scale); -} - -Color & Color::operator+=(const Color & other) -{ - r += other.r; - g += other.g; - b += other.b; - return *this; -} - -Color & Color::operator-=(const Color & other) -{ - r += other.r; - g += other.g; - b += other.b; - return *this; -} - -Color & Color::operator*=(double scale) -{ - r *= scale; - g *= scale; - b *= scale; - return *this; -} - -Color & Color::operator*=(const Color & other) -{ - r *= other.r; - g *= other.g; - b *= other.b; - return *this; -} - -Color & Color::operator/=(double scale) -{ - r /= scale; - g /= scale; - b /= scale; - return *this; -} - -Color & Color::operator/=(const Color & other) -{ - r /= other.r; - g /= other.g; - b /= other.b; - return *this; -} - -Color operator+(const Color & c1, const Color & c2) -{ - return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b); -} - -Color operator-(const Color & c1, const Color & c2) -{ - return Color(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b); -} - std::ostream & operator<<(std::ostream & out, const Color & color) { out << "[" << color.r << ", " << color.g << ", " << color.b << "]"; diff --git a/util/Color.h b/util/Color.h index 8524f07..fccdb53 100644 --- a/util/Color.h +++ b/util/Color.h @@ -11,20 +11,108 @@ class Color public: double r, g, b; - Color(); - Color(double r, double g, double b); - Color(const Vector & v); - Color(refptr v); + Color() + { + r = g = b = 0.0; + } - Color operator*(const Color & other) const; - Color operator*(double scale) const; - Color operator/(double scale) const; - Color & operator+=(const Color & other); - Color & operator-=(const Color & other); - Color & operator*=(double scale); - Color & operator*=(const Color & other); - Color & operator/=(double scale); - Color & operator/=(const Color & other); + Color(double r, double g, double b) + { + this->r = r; + this->g = g; + this->b = b; + } + + Color(const Vector & v) + { + r = v[0]; + g = v[1]; + b = v[2]; + } + + Color(refptr v) + { + r = (*v)[0]; + g = (*v)[1]; + b = (*v)[2]; + } + + Color operator*(const Color & other) const + { + Color result; + result.r = r * other.r; + result.g = g * other.g; + result.b = b * other.b; + return result; + } + + Color operator*(double scale) const + { + return Color(r * scale, g * scale, b * scale); + } + + Color operator/(double scale) const + { + return Color(r / scale, g / scale, b / scale); + } + + Color & operator+=(const Color & other) + { + r += other.r; + g += other.g; + b += other.b; + return *this; + } + + Color & operator-=(const Color & other) + { + r += other.r; + g += other.g; + b += other.b; + return *this; + } + + Color & operator*=(double scale) + { + r *= scale; + g *= scale; + b *= scale; + return *this; + } + + Color & operator*=(const Color & other) + { + r *= other.r; + g *= other.g; + b *= other.b; + return *this; + } + + Color & operator/=(double scale) + { + r /= scale; + g /= scale; + b /= scale; + return *this; + } + + Color & operator/=(const Color & other) + { + r /= other.r; + g /= other.g; + b /= other.b; + return *this; + } + + Color operator+(const Color & c2) + { + return Color(r + c2.r, g + c2.g, b + c2.b); + } + + Color operator-(const Color & c2) + { + return Color(r - c2.r, g - c2.g, b - c2.b); + } static const Color black; static const Color white; @@ -36,8 +124,6 @@ class Color static const Color magenta; }; -Color operator+(const Color & c1, const Color & c2); -Color operator-(const Color & c1, const Color & c2); static inline Color operator*(double d, const Color & c) { return c * d; } static inline Color operator/(double d, const Color & c) { return c / d; } std::ostream & operator<<(std::ostream & out, const Color & color); diff --git a/util/Ray.cc b/util/Ray.cc index ff4e0b9..fe1a509 100644 --- a/util/Ray.cc +++ b/util/Ray.cc @@ -5,42 +5,13 @@ #include "Ray.h" -Ray::Ray() -{ -} - -Ray::Ray(const Vector & origin, const Vector & direction) -{ - m_origin = origin; - m_direction = direction; - m_direction.normalize(); -} - Ray Ray::randomRay() { return Ray(Vector(0, 0, 0), Vector::randomVector()); } -/* - * return a vector for the point at distance dist - * from the ray's origin point, along its direction. - */ -Vector Ray::getPositionAt(double dist) const -{ - Vector v; - v[0] = m_origin[0] + dist * m_direction[0]; - v[1] = m_origin[1] + dist * m_direction[1]; - v[2] = m_origin[2] + dist * m_direction[2]; - return v; -} - std::ostream & operator<<(std::ostream & out, const Ray & r) { out << "(" << r.getOrigin() << " -> " << r.getDirection() << ")"; return out; } - -Ray Ray::shift(double amt) -{ - return Ray(getPositionAt(amt), m_direction); -} diff --git a/util/Ray.h b/util/Ray.h index 146d7a9..b0a175d 100644 --- a/util/Ray.h +++ b/util/Ray.h @@ -8,16 +8,47 @@ class Ray { public: - Ray(); - Ray(const Vector & origin, const Vector & direction); - static Ray randomRay(); + Ray() + { + } + + Ray(const Vector & origin, const Vector & direction) + : m_origin(origin), m_direction(direction) + { + m_direction.normalize(); + } + + /* + * return a vector for the point at distance dist + * from the ray's origin point, along its direction. + */ + Vector getPositionAt(double dist) const + { + Vector v; + v[0] = m_origin[0] + dist * m_direction[0]; + v[1] = m_origin[1] + dist * m_direction[1]; + v[2] = m_origin[2] + dist * m_direction[2]; + return v; + } + + Ray shift(double amt) + { + return Ray(getPositionAt(amt), m_direction, false); + } + const Vector & getOrigin() const { return m_origin; } const Vector & getDirection() const { return m_direction; } - Vector getPositionAt(double dist) const; + + static Ray randomRay(); Vector operator[](double dist) const { return getPositionAt(dist); } - Ray shift(double amt); protected: + Ray(const Vector & origin, const Vector & direction, bool no_norm) + : m_origin(origin), m_direction(direction) + { + /* no normalize version */ + } + Vector m_origin; Vector m_direction; };