00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef SeVec3d_h
00012 #define SeVec3d_h
00013
00014 #ifndef MAKEDEPEND
00015 #include <iostream>
00016 #include <math.h>
00017 #endif
00018
00027 class SeVec3d
00028 {
00029 public:
00030
00032 SeVec3d() {}
00033
00035 SeVec3d( double v )
00036 { setValue( v, v, v ); }
00037
00039 SeVec3d( double x, double y, double z )
00040 { setValue( x, y, z ); }
00041
00043 SeVec3d( const double* v )
00044 { setValue( v[0], v[1], v[2] ); }
00045
00047 SeVec3d( const float* v )
00048 { setValue( v[0], v[1], v[2] ); }
00049
00051 typedef double array[3];
00052 operator array&() { return _vec; }
00053
00055 double &operator []( int i ) { return (_vec[i]); }
00056
00058 double operator []( int i ) const { return (_vec[i]); }
00059
00061 SeVec3d &operator =( const SeVec3d &v )
00062 { _vec[0]=v[0]; _vec[1]=v[1]; _vec[2]=v[2]; return *this; }
00063
00065 SeVec3d &operator *=( double d )
00066 { _vec[0]*=d; _vec[1]*=d; _vec[2]*=d; return *this; }
00067
00069 SeVec3d &operator /=( double d )
00070 { *this *= 1/d; return *this; }
00071
00073 SeVec3d &operator +=( const SeVec3d &v )
00074 { _vec[0]+=v[0]; _vec[1]+=v[1]; _vec[2]+=v[2]; return *this; }
00075
00077 SeVec3d &operator -=( const SeVec3d &v )
00078 { _vec[0]-=v[0]; _vec[1]-=v[1]; _vec[2]-=v[2]; return *this; }
00079
00081 SeVec3d operator -() const
00082 { return SeVec3d( -_vec[0], -_vec[1], -_vec[2] ); }
00083
00085 bool operator ==( const SeVec3d &v ) const
00086 { return (_vec[0] == v[0] && _vec[1] == v[1] && _vec[2] == v[2]); }
00087
00089 bool operator !=( const SeVec3d &v ) const
00090 { return !(*this == v); }
00091
00093 SeVec3d operator *( double d ) const
00094 { return SeVec3d( _vec[0]*d, _vec[1]*d, _vec[2]*d ); }
00095
00097 SeVec3d operator /( double d ) const
00098 { return *this * (1/d); }
00099
00101 friend SeVec3d operator *( double d, const SeVec3d &v )
00102 { return v * d; }
00103
00105 SeVec3d operator *( const SeVec3d &v ) const
00106 { return SeVec3d(_vec[0]*v[0], _vec[1]*v[1], _vec[2]*v[2]); }
00107
00109 SeVec3d operator /( const SeVec3d &v ) const
00110 { return SeVec3d(_vec[0]/v[0], _vec[1]/v[1], _vec[2]/v[2]); }
00111
00113 SeVec3d operator +( const SeVec3d &v ) const
00114 { return SeVec3d( _vec[0]+v[0], _vec[1]+v[1], _vec[2]+v[2] ); }
00115
00117 SeVec3d operator -( const SeVec3d &v ) const
00118 { return SeVec3d( _vec[0]-v[0], _vec[1]-v[1], _vec[2]-v[2]); }
00119
00121 friend std::ostream & operator <<( std::ostream &os, const SeVec3d &v )
00122 { os << "(" << v[0] << "," << v[1] << "," << v[2] << ")"; return os; }
00123
00125 void getValue( double &x, double &y, double &z ) const
00126 { x = _vec[0]; y = _vec[1]; z = _vec[2]; }
00127
00129 const double *getValue() const
00130 { return _vec; }
00131
00133 void setValue( double x, double y, double z )
00134 { _vec[0] = x; _vec[1] = y; _vec[2] = z; }
00135
00137 void setValue( const double* v )
00138 { _vec[0] = v[0]; _vec[1] = v[1]; _vec[2] = v[2]; }
00139
00141 double dot( const SeVec3d &v ) const
00142 { return _vec[0]*v[0] + _vec[1]*v[1] + _vec[2]*v[2]; }
00143
00145 SeVec3d cross( const SeVec3d &v ) const
00146 { return SeVec3d(_vec[1]*v[2] - _vec[2]*v[1],
00147 _vec[2]*v[0] - _vec[0]*v[2],
00148 _vec[0]*v[1] - _vec[1]*v[0]); }
00149
00151 void negate()
00152 { _vec[0]*=-1; _vec[1]*=-1; _vec[2]*=-1; }
00153
00155 double length() const
00156 { return sqrt( _vec[0]*_vec[0]+_vec[1]*_vec[1]+_vec[2]*_vec[2] ); }
00157
00159 SeVec3d normalized() const
00160 {
00161 double len = length();
00162 if ( len ) return *this / len;
00163 else return 0.0;
00164 }
00165
00167 void normalize()
00168 {
00169 double len = length();
00170 if ( len ) *this /= len;
00171 }
00172
00174 SeVec3d orthogonal() const {
00175 return SeVec3d( _vec[1]+_vec[2], _vec[2]-_vec[0], -_vec[0]-_vec[1] );
00176 }
00177
00182 double angle( const SeVec3d &v ) const
00183 { double len = this->length()*v.length();
00184 if (len == 0) return 0;
00185 return acos(this->dot(v) / len);
00186 }
00187
00192 SeVec3d rotateBy( const SeVec3d &axis, double angle_ ) const
00193 {
00194 double c = cos(angle_), s = sin(angle_);
00195 const SeVec3d& v = *this;
00196 return c*v + (1-c)*v.dot(axis)*axis - s*v.cross(axis);
00197 }
00198
00199 private:
00200
00202 double _vec[3];
00203 };
00204
00205
00206 #endif