point3.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003    FILE: point3.h
00004 
00005    DESCRIPTION: Class definitions for Point3
00006 
00007    CREATED BY: Dan Silva
00008 
00009    HISTORY:
00010 
00011  *>   Copyright (c) 1994, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 
00016 #include "GeomExport.h"
00017 #include "maxheap.h"
00018 #include "gfloat.h"
00019 #include "assert1.h"
00020 #include <math.h>
00021 
00050 class GEOMEXPORT Point3: public MaxHeapOperators {
00051 public:
00052    float x,y,z;
00053 
00054    // Constructors
00056    Point3() { /* NO INIT */ }
00058    Point3(float X, float Y, float Z)  { 
00059          x = X; y = Y; z = Z; 
00060      }
00062    Point3(double X, double Y, double Z) { 
00063          x = (float)X; y = (float)Y; z = (float)Z; 
00064      }
00066    Point3(int X, int Y, int Z) { 
00067          x = (float)X; y = (float)Y; z = (float)Z; 
00068      }
00070    Point3(const Point3& a) { 
00071          x = a.x; y = a.y; z = a.z; 
00072      } 
00075    Point3(float af[3]) { 
00076          x = af[0]; y = af[1]; z = af[2]; 
00077      }
00078 
00079     // Data members
00080     static const Point3 Origin;
00081     static const Point3 XAxis;
00082     static const Point3 YAxis;
00083     static const Point3 ZAxis;
00084 
00088    float& operator[](int i) { 
00089          return (&x)[i];
00090      }     
00094    const float& operator[](int i) const { 
00095          return (&x)[i]; 
00096      }  
00097 
00098    // Conversion function
00100    operator float*() { 
00101          return(&x); 
00102      }
00103 
00104    // Unary operators
00106    Point3 operator-() const { 
00107          return(Point3(-x,-y,-z)); 
00108      } 
00110    Point3 operator+() const { 
00111          return *this; 
00112      }
00113     
00114     // Property functions
00117     float Length() const;
00121     float FLength() const;
00124     float LengthSquared() const;
00125     int MaxComponent() const;
00126     int MinComponent() const;
00127     Point3 Normalize() const;     // more accurate than FNormalize()
00128     Point3 FNormalize() const;    // faster than Normalize()
00129 
00130    // Assignment operators
00132    inline Point3& operator-=(const Point3&);
00134    inline Point3& operator+=(const Point3&);
00136    inline Point3& operator*=(float); 
00138    inline Point3& operator/=(float);
00141    inline Point3& operator*=(const Point3&); // element-by-element multiply.
00142 
00143     inline Point3& Set(float X, float Y, float Z);
00144 
00145    // Test for equality
00148    int operator==(const Point3& p) const { 
00149          return ((p.x==x)&&(p.y==y)&&(p.z==z)); 
00150      }
00151    int operator!=(const Point3& p) const { 
00152          return ((p.x!=x)||(p.y!=y)||(p.z!=z)); 
00153      }
00154     int Equals(const Point3& p, float epsilon = 1E-6f) const;
00155 
00156     // In-place normalize
00157     Point3& Unify();
00158     float LengthUnify();              // returns old Length
00159 
00160    // Binary operators
00162    inline  Point3 operator-(const Point3&) const;
00164    inline  Point3 operator+(const Point3&) const;
00166    inline  Point3 operator/(const Point3&) const;
00169    inline  Point3 operator*(const Point3&) const;   
00170 
00173    Point3 operator^(const Point3&) const; // CROSS PRODUCT
00174    inline float operator%(const Point3&) const;     // DOT PRODUCT
00175    };
00176 
00179 GEOMEXPORT float Length(const Point3&); 
00183 GEOMEXPORT float FLength(const Point3&); 
00186 GEOMEXPORT float LengthSquared(const Point3&); 
00189 GEOMEXPORT int MaxComponent(const Point3&);  // the component with the maximum abs value
00192 GEOMEXPORT int MinComponent(const Point3&);  // the component with the minimum abs value
00195 GEOMEXPORT Point3 Normalize(const Point3&);  // Accurate normalize
00199 GEOMEXPORT Point3 FNormalize(const Point3&); // Fast normalize 
00203 GEOMEXPORT Point3 CrossProd(const Point3& a, const Point3& b);  // CROSS PRODUCT
00204 
00205 // RB: moved this here from object.h
00215 class Ray: public MaxHeapOperators {
00216    public:
00217       Point3 p;   // point of origin
00218       Point3 dir; // unit vector
00219    };
00220 
00221  
00222 // Inlines:
00223 
00224 inline float Point3::Length() const {  
00225    return (float)sqrt(x*x+y*y+z*z);
00226    }
00227 
00228 inline float Point3::FLength() const { 
00229    return Sqrt(x*x+y*y+z*z);
00230    }
00231 
00232 inline float Point3::LengthSquared() const { 
00233    return (x*x+y*y+z*z);
00234    }
00235 
00236 inline float Length(const Point3& v) { 
00237    return v.Length();
00238    }
00239 
00240 inline float FLength(const Point3& v) {   
00241    return v.FLength();
00242    }
00243 
00244 inline float LengthSquared(const Point3& v) {   
00245    return v.LengthSquared();
00246    }
00247 
00248 inline Point3& Point3::operator-=(const Point3& a) {  
00249    x -= a.x;   y -= a.y;   z -= a.z;
00250    return *this;
00251    }
00252 
00253 inline Point3& Point3::operator+=(const Point3& a) {
00254    x += a.x;   y += a.y;   z += a.z;
00255    return *this;
00256    }
00257 
00258 inline Point3& Point3::operator*=(float f) {
00259    x *= f;   y *= f; z *= f;
00260    return *this;
00261    }
00262 
00263 inline Point3& Point3::operator/=(float f) { 
00264    x /= f;  y /= f;  z /= f;  
00265    return *this; 
00266    }
00267 
00268 inline Point3& Point3::operator*=(const Point3& a) { 
00269    x *= a.x;   y *= a.y;   z *= a.z;   
00270    return *this; 
00271    }
00272 
00273 inline Point3& Point3::Set(float X, float Y, float Z) {
00274     x = X;
00275     y = Y;
00276     z = Z;
00277     return *this;
00278     }
00279 
00280 inline Point3 Point3::operator-(const Point3& b) const {
00281    return(Point3(x-b.x,y-b.y,z-b.z));
00282    }
00283 
00284 inline Point3 Point3::operator+(const Point3& b) const {
00285    return(Point3(x+b.x,y+b.y,z+b.z));
00286    }
00287 
00288 inline Point3 Point3::operator/(const Point3& b) const {
00289    assert(b.x != 0.0f && b.y != 0.0f && b.z != 0.0f);
00290    return Point3(x/b.x,y/b.y,z/b.z);
00291    }
00292 
00293 inline Point3 Point3::operator*(const Point3& b) const {  
00294    return Point3(x*b.x, y*b.y, z*b.z); 
00295    }
00296 
00297 inline float Point3::operator%(const Point3& b) const {
00298    return (x*b.x + y*b.y + z*b.z);
00299    }
00300 
00303 inline Point3 operator*(float f, const Point3& a) {
00304    return(Point3(a.x*f, a.y*f, a.z*f));
00305    }
00306 
00309 inline Point3 operator*(const Point3& a, float f) {
00310    return(Point3(a.x*f, a.y*f, a.z*f));
00311    }
00312 
00315 inline Point3 operator/(const Point3& a, float f) {
00316    DbgAssert(f != 0.0f);
00317    return(Point3(a.x/f, a.y/f, a.z/f));
00318    }
00319 
00322 inline Point3 operator+(const Point3& a, float f) {
00323    return(Point3(a.x+f, a.y+f, a.z+f));
00324    }
00325 
00331 inline float DotProd(const Point3& a, const Point3& b) { 
00332    return(a.x*b.x+a.y*b.y+a.z*b.z); 
00333    }
00334 
00335 
00336 // These typedefs must be the same as each other, since
00337 // vertex colors are contained in a MeshMap.
00338 typedef Point3 UVVert;
00339 typedef Point3 VertColor;