Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Vertex3.h

00001 /*
00002   File: Vertex3.h
00003 
00004   Copyright(C) C. Kotterink, Computed Graphics
00005 */
00006 #ifndef VERTEX3_H
00007 #define VERTEX3_H
00008 
00009 #include<real.h>
00010 
00011 #include<iostream>
00012 #include<algorithm>
00013 
00014 class Vertex4;
00015 class ObjectFile;
00016 
00020 class Vertex3
00021 {
00022 public:
00023     typedef enum Component { X, Y, Z };
00024 
00025     // Constructors
00026     Vertex3() {};
00027     Vertex3(real xx, real yy, real zz) : x(xx), y(yy), z(zz) {}
00028     Vertex3(const Vertex3 &v) : x(v.x), y(v.y), z(v.z) {}
00029 
00030     // Get
00031     real &operator [](unsigned int i) {
00032         return ((real*)(this))[i];
00033     }
00034 
00035     Component largestComponent() const;
00036     Vertex3 &lerp(Vertex3 &from, Vertex3 &to, float t);
00037 
00038     // Assignment operators
00039     Vertex3 &operator =(const Vertex3 &v) {
00040         x = v.x;
00041         y = v.y;
00042         z = v.z;
00043         return *this;
00044     }
00045     Vertex3 &operator =(const Vertex4 &v);
00046     void operator -=(const Vertex3 &v) {
00047         x -= v.x;
00048         y -= v.y;
00049         z -= v.z;
00050     }
00051     void operator += (const Vertex3 &v) {
00052         x += v.x;
00053         y += v.y;
00054         z += v.z;
00055     }
00056     void operator *=(real r) {
00057         x *= r;
00058         y *= r;
00059         z *= r;
00060     }
00061     void operator /=(real r) {
00062         x /= r;
00063         y /= r;
00064         z /= r;
00065     }
00066     void operator &=(const Vertex3 &v) {        // Cross product
00067         real xx = x;
00068         real yy = y;
00069         x = y*v.z - z*v.y;
00070         y = z*v.x - xx*v.z;
00071         z = xx*v.y - yy*v.x;
00072     }
00073 
00074     // Operators
00075     Vertex3 operator -(const Vertex3 &v) const {
00076         Vertex3 vv(x - v.x, y - v.y, z - v.z);
00077         return vv;
00078     }
00079     Vertex3 operator +(const Vertex3 &v) const {
00080         Vertex3 vv(x + v.x, y + v.y, z + v.z);
00081         return vv;
00082     }
00083     Vertex3 operator *(real r) const {
00084         Vertex3 v(x * r, y * r, z * r);
00085         return v;
00086     }
00087     Vertex3 operator /(real r) const {
00088         Vertex3 v(x / r, y / r, z / r);
00089         return v;
00090     }
00091     real operator *(const Vertex3 &v) const {
00092         return x*v.x + y*v.y + z*v.z;
00093     }
00097     Vertex3 operator &(const Vertex3 &v) const {
00098         Vertex3 vv;
00099         vv.x = y*v.z - z*v.y;
00100         vv.y = z*v.x - x*v.z;
00101         vv.z = x*v.y - y*v.x;
00102         return vv;
00103     }
00104 
00105     real sqrNorm() const {
00106         return x*x + y*y + z*z;
00107     }
00108     real norm() const {
00109         return (real)sqrt(x*x + y*y + z*z);
00110     }
00111     real maxNorm() {
00112         return max(x, max(y, z));
00113     }
00114     Vertex3 &normalize();
00115     Vertex3 &setLength(real s) {
00116         *this *= (s/norm());
00117         return *this;
00118     }
00119     Vertex3 &invert() {
00120         x = -x;
00121         y = -y;
00122         z = -z;
00123         return *this;
00124     }
00125 
00126     // 3D Transformation, righthanded
00127 
00128     Vertex3 &scale(Vertex3 &v);
00130     Vertex3 &rotateX(real alpha);
00131     Vertex3 &rotateY(real alpha);
00132     Vertex3 &rotateZ(real alpha);
00133 
00134     real x, y, z;
00135 };
00136 inline Vertex3 operator *(real r, const Vertex3 &vv)
00137 {
00138     Vertex3 v(vv.x * r, vv.y * r, vv.z * r);
00139     return v;
00140 }
00141 
00142 ostream &operator<<(ostream &stream, const Vertex3 &v);
00143 ObjectFile &operator>>(ObjectFile &f, Vertex3 &v);
00144 #endif

This documentation was generated using doxygen. If you have any comments or additions please mail me.