00001
00002
00003
00004
00005 #ifndef VEC3_H
00006 #define VEC3_H
00007
00008 #include <stdio.h>
00009 #include <math.h>
00010
00026 template<class Type>
00027 class Vec3
00028 {
00029 public:
00030 Type x, y, z;
00031
00034 Vec3 (void)
00035 {};
00037 Vec3 (const Type X, const Type Y, const Type Z)
00038 { x=X; y=Y; z=Z; };
00040 Vec3 (const Vec3& v)
00041 { x=v.x; y=v.y; z=v.z; };
00043 Vec3 (const Type v[3])
00044 { x=v[0]; y=v[1]; z=v[2]; };
00046 void Set (const Type X, const Type Y, const Type Z)
00047 { x=X; y=Y; z=Z; }
00049 void Set (const Type v[3])
00050 { x=v[0]; y=v[1]; z=v[2]; };
00051
00052 operator Type*()
00053 { return (Type *)&x; }
00054 operator const Type*() const
00055 { return &x; }
00056
00057 int operator == (const Vec3& A) const
00058 { return (x==A.x && y==A.y && z==A.z); }
00059
00060 Vec3& operator = (const Vec3& A)
00061 { x=A.x; y=A.y; z=A.z;
00062 return(*this); };
00063 Vec3 operator + (const Vec3& A) const
00064 { Vec3 Sum(x+A.x, y+A.y, z+A.z);
00065 return(Sum); };
00066 Vec3 operator - (const Vec3& A) const
00067 { Vec3 Diff(x-A.x, y-A.y, z-A.z);
00068 return(Diff); };
00069 Type operator * (const Vec3& A) const
00070 { Type DotProd = x*A.x+y*A.y+z*A.z;
00071 return(DotProd); };
00072 Vec3 operator / (const Vec3& A) const
00073 { Vec3 CrossProd(y*A.z-z*A.y, z*A.x-x*A.z, x*A.y-y*A.x);
00074 return(CrossProd); };
00075 Vec3 operator ^ (const Vec3& A) const
00076 { Vec3 CrossProd(y*A.z-z*A.y, z*A.x-x*A.z, x*A.y-y*A.x);
00077 return(CrossProd); };
00078 Vec3 operator * (const Type s) const
00079 { Vec3 Scaled(x*s, y*s, z*s);
00080 return(Scaled); };
00081 Vec3 operator / (const Type s) const
00082 { Vec3 Scaled(x/s, y/s, z/s);
00083 return(Scaled); };
00084 Vec3 operator & (const Vec3& A) const
00085 { Vec3 CompMult(x*A.x, y*A.y, z*A.z);
00086 return(CompMult); }
00087
00088 friend inline Vec3 operator *(Type s, const Vec3& v)
00089 { return Vec3(v.x*s, v.y*s, v.z*s); }
00090
00091 Vec3& operator += (const Vec3& A)
00092 { x+=A.x; y+=A.y; z+=A.z;
00093 return *this;}
00094 Vec3& operator -= (const Vec3& A)
00095 { x-=A.x; y-=A.y; z-=A.z;
00096 return *this; }
00097 Vec3& operator *= (const Type s)
00098 { x*=s; y*=s; z*=s;
00099 return *this; }
00100 Vec3& operator /= (const Type s)
00101 { x/=s; y/=s; z/=s;
00102 return *this; }
00103 Vec3& operator &= (const Vec3& A)
00104 { x*=A.x; y*=A.y; z*=A.z; return *this; }
00105 Vec3 operator - (void) const
00106 { Vec3 Negated(-x, -y, -z);
00107 return(Negated); };
00108
00109
00110
00111
00112
00113
00114
00115
00116 Type Length (void) const
00117 { return ((Type)sqrt(x*x+y*y+z*z)); };
00118 Type LengthSqr (void) const
00119 { return (x*x+y*y+z*z); };
00120 Vec3& Normalize (void)
00121 { Type L = Length();
00122 if (L>0) { x/=L; y/=L; z/=L; }
00123 return *this;
00124 };
00125
00127
00140 Type* Star() const {
00141 Type s[] = ( 0, -z, y,
00142 z, 0, -x,
00143 -y, x, 0);
00144 return s;
00145 }
00146
00148
00151 void UpdateMinMax(Vec3 &Min, Vec3 &Max) const
00152 {
00153 if (x<Min.x) Min.x=x; else if (x>Max.x) Max.x=x;
00154 if (y<Min.y) Min.y=y; else if (y>Max.y) Max.y=y;
00155 if (z<Min.z) Min.z=z; else if (z>Max.z) Max.z=z;
00156 }
00157
00159
00174 void CompleteOrthonormalBasis(Vec3 &U, Vec3 &V) const
00175 {
00176 U = *this;
00177 unsigned char s[3] = {0, 1, 2};
00178 unsigned char tmpa;
00179 U.x = U.x < 0 ? -U.x : -U.x;
00180 U.y = U.y < 0 ? -U.y : -U.y;
00181 U.z = U.z < 0 ? -U.z : -U.z;
00182 if ( U[0] > U[1] )
00183 {
00184 tmpa = s[0];
00185 s[0] = s[1];
00186 s[1] = tmpa;
00187 }
00188
00189 if ( U[s[0]] > U[2] ) {
00190 tmpa = s[2];
00191 s[2] = s[0];
00192 s[0] = tmpa;
00193 }
00194
00195 U = *this;
00196 U[s[0]] = 0;
00197
00198
00199 U.Normalize();
00200
00201
00202 V = *this ^ U;
00203
00204
00205
00206
00207 V.Normalize();
00208 }
00209
00211 void Print() const
00212 { printf("(%.3f, %.3f, %.3f)\n",x, y, z); }
00213
00215 static Vec3 ZERO;
00216 };
00217
00218 typedef Vec3<float> Vec3f;
00219 typedef Vec3<double> Vec3d;
00220
00221 template<class Type> Vec3<Type> Vec3<Type>::ZERO = Vec3<Type>(0,0,0);
00222
00223 #endif
00224
00225