00001
00002 #ifndef RAY_H
00003 #define RAY_H
00004
00005 #include "raytrace_defs.h"
00006
00007 #include <iostream>
00008 #include <sstream>
00009
00011
00012 class Ray {
00013 protected:
00014 bool _is_normalized;
00015
00016 Point3D _origin;
00017 Vector _direction;
00018
00019 public:
00020 Ray() : _origin(Point3D()), _direction(Vector()), _is_normalized(false) {}
00021
00022 Ray(Point3D origin, Point3D direction)
00023 : _origin(origin),
00024 _direction(direction),
00025 _is_normalized(false)
00026 {
00027 Ray::normalize();
00028 }
00029
00030 Ray(const Ray &other)
00031 : _origin(other._origin),
00032 _direction(other._direction),
00033 _is_normalized(other._is_normalized)
00034 {
00035 if (!_is_normalized) {
00036 Ray::normalize();
00037 }
00038 }
00039
00040 ~Ray() {}
00041
00043 inline void normalize();
00044
00045 bool is_normalized() { return _is_normalized; }
00046
00049 Point3D origin() const { return _origin; }
00050
00053 const Vector &direction() const { return _direction; }
00054
00057 std::string to_string() {
00058 std::ostringstream rv;
00059 rv << "origin = " << _origin.to_string() << ", direction = " << _direction.to_string();
00060 return rv.str();
00061 }
00062 };
00063
00064
00065
00067 void Ray::normalize() {
00068 double norm = sqrtf(_direction.x * _direction.x + _direction.y * _direction.y + _direction.z * _direction.z);
00069 _direction.x /= norm;
00070 _direction.y /= norm;
00071 _direction.z /= norm;
00072 }
00073
00074
00075 inline bool operator==(const Ray &r1, const Ray &r2) {
00076 return r1.origin() == r2.origin() && r1.direction() == r2.direction();
00077 }
00078
00079
00080
00081 inline bool operator!=(const Ray &r1, const Ray &r2) {
00082 return !(r1 == r2);
00083 }
00084
00085 #endif