point3d.h

Go to the documentation of this file.
00001 #ifndef POINT3D_H
00002 #define POINT3D_H
00003 
00004 #include <cmath>
00005 #include <sstream>
00006 #include <algorithm>
00007 #include "raytrace_defs.h"
00008 
00009 class Point3D;
00010 typedef Point3D Vector;
00011 
00012 inline bool iswhitespace(char c) {
00013     return c == ' '
00014         || c == '\n'
00015         || c == '\r'
00016         || c == '\t';
00017 }
00019 //{{{
00020 class Point3D {
00021     public:
00022         
00024         double x;
00026         double y;
00028         double z;
00029 
00031         Point3D() : x(0.0), y(0.0), z(0.0) {}
00032 
00034         template <class _T>
00035         Point3D(_T _x, _T _y) : x((double)_x), y((double)_y), z(0.0) {}
00036 
00038         template <class _T>
00039         Point3D(_T _x, _T _y, _T _z) : x((double)_x), y((double)_y), z((double)_z) {}
00040 
00043         Point3D(std::string str) {
00044             syslog(LOG_INFO, "%s", str.c_str());
00045             // remove all whitespace
00046             std::string::iterator end;
00047             std::remove_if(str.begin(), end, iswhitespace);
00048             int openparen   = str.find('(');
00049             int closeparen  = str.find(')');
00050             int firstcomma  = str.find(',') + 1;
00051             int secondcomma = str.find(',', firstcomma) + 1;
00052             
00053             x = (double)strtod(str.substr(openparen+1, firstcomma - openparen).c_str(),     NULL);
00054             y = (double)strtod(str.substr(firstcomma, secondcomma - firstcomma).c_str(),  NULL);
00055             z = (double)strtod(str.substr(secondcomma, closeparen - secondcomma).c_str(), NULL);
00056         }
00057 
00059         Point3D(const Point3D &other) : x(other.x), y(other.y), z(other.z) {}
00060 
00067         inline double distance_to(const Point3D &other);
00068 
00071         std::string to_string() {
00072             std::ostringstream rv;
00073             rv << "(" << x << ", " << y << ", " << z << ")";
00074             return rv.str();
00075         }
00076 
00078         template <class _T>
00079         void set(_T x, _T y, _T z) {
00080             this->x = (double)x;
00081             this->y = (double)y;
00082             this->z = (double)z;
00083         }
00084 
00086         void normalize() {
00087             double norm = sqrtf(x * x + y * y + z * z);
00088             x /= norm;
00089             y /= norm;
00090             z /= norm;
00091         }
00092 
00093 };
00094 //}}}
00095 
00096 // This is inline, it must be defined in the header.
00101 //{{{
00102 double Point3D::distance_to(const Point3D &other) {
00103     double dx = x - other.x;
00104     double dy = y - other.y;
00105     double dz = z - other.z;
00106     return sqrtf(dx*dx + dy*dy + dz*dz);
00107 }
00108 //}}}
00109 
00114 //{{{
00115 inline Vector operator-(const Vector &A, const Vector &B) {
00116     return Vector(A.x - B.x, A.y - B.y, A.z - B.z);
00117 }
00118 //}}}
00120 //{{{
00121 inline Vector operator-(const Vector &A) {
00122     return Vector( -A.x, -A.y, -A.z);
00123 }
00124 //}}}
00125 
00127 //{{{
00128 inline Vector operator+(const Vector &A, const Vector &B) {
00129     return Vector(A.x + B.x, A.y + B.y, A.z + B.z);
00130 }
00131 //}}}
00132 //{{{
00133 inline Vector operator*(const Vector &A, double factor) {
00134     return Vector(A.x * factor, A.y * factor, A.z * factor);
00135 }
00136 //}}}
00137 
00138 //{{{
00139 inline bool operator==(const Vector &A, const Vector &B) {
00140     return (A.x == B.x) && (A.y == B.y) && (A.z == B.z);
00141 }
00142 //}}}
00143 //{{{
00144 inline bool operator!=(const Vector &A, const Vector &B) {
00145     return !(A == B);
00146 }
00147 //}}}
00148 
00150 //{{{
00151 inline double dot_product(const Vector &A, const Vector &B) {
00152     return (A.x * B.x) + (A.y * B.y) + (A.z * B.z);
00153 }
00154 //}}}
00155 
00158 //{{{
00159 inline 
00160 Vector cross_product(const Vector& one, const Vector& two)
00161 {
00162     return Vector(one.y * two.z - one.z * two.y,
00163                   one.z * two.x - one.x * two.z,
00164                   one.x * two.y - one.y * two.x);
00165 
00166 }
00167 //}}}
00168 //{{{
00169 template <class _T>
00170 inline Vector operator*(Vector &v, const _T n) {
00171     return Vector(v.x * (double)n, v.y * (double)n, v.z * (double)n);
00172 }
00173 //}}}
00174 
00175 
00176 #endif

Generated on Tue Oct 30 22:12:15 2007 for mbrt by  doxygen 1.5.2