00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OPAL_VEC3R_H
00029 #define OPAL_VEC3R_H
00030
00031
00032 #include "OpalMath.h"
00033 #include "Portability.h"
00034
00035
00036 #include <iostream>
00037
00038 namespace opal
00039 {
00040 class Vec3r;
00041 inline Vec3r operator+( const Vec3r &u, const Vec3r &v );
00042 inline Vec3r operator-( const Vec3r &u, const Vec3r &v );
00043 inline Vec3r operator*( const Vec3r &v, real scalar );
00044 inline Vec3r operator*( real scalar, const Vec3r &v );
00045 inline Vec3r operator/( const Vec3r &v, real scalar );
00046 inline Vec3r operator%( const Vec3r & a, const Vec3r & b );
00047 inline Vec3r operator-( const Vec3r &v );
00048 inline real dot( const Vec3r & u, const Vec3r & v );
00049 inline Vec3r cross( const Vec3r & u, const Vec3r& v );
00050
00052 inline Vec3r project( const Vec3r& u, const Vec3r& v );
00053
00056 inline Vec3r projectPreNorm( const Vec3r& u, const Vec3r& v );
00057
00059 inline real angleBetween( const Vec3r& u, const Vec3r& v );
00060
00063 inline real angleBetweenPreNorm( const Vec3r& u, const Vec3r& v );
00064
00066 inline bool areCollinear( const Vec3r& u, const Vec3r& v );
00067
00069 inline std::ostream& operator<<( std::ostream& o, const Vec3r& v );
00070
00071 class Vec3r
00072 {
00073 public:
00075 real x;
00076
00078 real y;
00079
00081 real z;
00082
00084 OPAL_DECL Vec3r();
00085
00086 OPAL_DECL Vec3r( const Vec3r & src );
00087
00088 OPAL_DECL Vec3r( real xx, real yy, real zz );
00089
00090 OPAL_DECL Vec3r( const real * data );
00091
00092 OPAL_DECL void OPAL_CALL set( real xx, real yy, real zz );
00093
00094 OPAL_DECL void OPAL_CALL set( real * data );
00095
00096 OPAL_DECL real & OPAL_CALL operator[] ( unsigned int i );
00097
00098 OPAL_DECL const real & OPAL_CALL operator[] ( unsigned int i ) const;
00099
00100 OPAL_DECL real OPAL_CALL lengthSquared() const;
00101
00102 OPAL_DECL real OPAL_CALL length() const;
00103
00104 OPAL_DECL void OPAL_CALL normalize();
00105
00106 OPAL_DECL Vec3r OPAL_CALL unit() const;
00107
00108 OPAL_DECL void OPAL_CALL operator+=( const Vec3r & v );
00109
00110 OPAL_DECL void OPAL_CALL operator-=( const Vec3r & v );
00111
00112 OPAL_DECL void OPAL_CALL operator*=( const Vec3r & v );
00113
00114 OPAL_DECL void OPAL_CALL operator*=( real scalar );
00115
00116 OPAL_DECL void OPAL_CALL operator/=( real scalar );
00117
00118 OPAL_DECL void OPAL_CALL operator/=( const Vec3r & v );
00119
00120 OPAL_DECL bool OPAL_CALL operator==( const Vec3r & v ) const;
00121
00122 OPAL_DECL bool OPAL_CALL operator!=( const Vec3r & v ) const;
00123
00124 OPAL_DECL void OPAL_CALL operator=( const Vec3r & v );
00125 };
00126
00127 inline Vec3r operator+( const Vec3r &u, const Vec3r &v )
00128 {
00129 return Vec3r( u.x + v.x, u.y + v.y, u.z + v.z );
00130 }
00131
00132 inline Vec3r operator-( const Vec3r &u, const Vec3r &v )
00133 {
00134 return Vec3r( u.x - v.x, u.y - v.y, u.z - v.z );
00135 }
00136
00137 inline Vec3r operator*( const Vec3r &v, real scalar )
00138 {
00139 return Vec3r( scalar * v.x, scalar * v.y, scalar * v.z );
00140 }
00141
00142 inline Vec3r operator*( real scalar, const Vec3r &v )
00143 {
00144 return Vec3r( scalar * v.x, scalar * v.y, scalar * v.z );
00145 }
00146
00147 inline Vec3r operator/( const Vec3r &v, real scalar )
00148 {
00149 return Vec3r( v.x / scalar, v.y / scalar, v.z / scalar );
00150 }
00151
00152 inline Vec3r operator%( const Vec3r & a, const Vec3r & b )
00153 {
00154 return Vec3r( ( a.y * b.z ) - ( a.z * b.y ), ( a.z * b.x ) - ( a.x * b.z ),
00155 ( a.x * b.y ) - ( a.y * b.x ) );
00156 }
00157
00158 inline real dot( const Vec3r & u, const Vec3r & v )
00159 {
00160 return u.x * v.x + u.y * v.y + u.z * v.z;
00161 }
00162
00163 inline Vec3r cross( const Vec3r & u, const Vec3r & v )
00164 {
00165 return u % v;
00166 }
00167
00168 inline Vec3r operator-( const Vec3r &v )
00169 {
00170 return v * -1;
00171 }
00172
00173 inline Vec3r project( const Vec3r& u, const Vec3r& v )
00174 {
00175 Vec3r u2 = u;
00176 u2.normalize();
00177 return projectPreNorm( u2, v );
00178 }
00179
00180 inline Vec3r projectPreNorm( const Vec3r& u, const Vec3r& v )
00181 {
00182 return dot( u, v ) * u;
00183 }
00184
00185 inline real angleBetween( const Vec3r& u, const Vec3r& v )
00186 {
00187 Vec3r u2 = u;
00188 u2.normalize();
00189 Vec3r v2 = v;
00190 v2.normalize();
00191 return angleBetweenPreNorm( u2, v2 );
00192 }
00193
00194 inline real angleBetweenPreNorm( const Vec3r& u, const Vec3r& v )
00195 {
00196 real val = dot( u, v );
00197
00198
00199
00200 if ( val < -1 )
00201 {
00202 val = -1;
00203 }
00204 else if ( val > 1 )
00205 {
00206 val = 1;
00207 }
00208
00209 return radToDeg( acos( val ) );
00210 }
00211
00212 inline std::ostream& operator<<( std::ostream& o, const Vec3r& v )
00213 {
00214 return o << "[" << v.x << " " << v.y << " " << v.z << "]";
00215 }
00216
00217 bool areCollinear( const Vec3r & u, const Vec3r & v )
00218 {
00219 Vec3r a = u, b = v;
00220 a.normalize();
00221 b.normalize();
00222
00223 if ( areEqual( a.x, b.x ) && areEqual( a.y, b.y ) && areEqual( a.z, b.z ) )
00224 return true;
00225 if ( areEqual( a.x, -b.x ) && areEqual( a.y, -b.y ) && areEqual( a.z, -b.z ) )
00226 return true;
00227
00228 return false;
00229 }
00230 }
00231
00232 #endif