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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #ifndef __VCGLIB_POINT4
00061 #define __VCGLIB_POINT4
00062 #include <assert.h>
00063
00064 #include <vcg/math/base.h>
00065
00066 namespace vcg {
00075 template <class T> class Point4
00076 {
00077 public:
00079 T _v[4];
00080
00081 public:
00082 typedef T ScalarType;
00083 enum {Dimension = 4};
00084
00086
00091 inline Point4 () { }
00092 inline Point4 ( const T nx, const T ny, const T nz , const T nw )
00093 {
00094 _v[0] = nx; _v[1] = ny; _v[2] = nz; _v[3] = nw;
00095 }
00096 inline Point4 ( const T p[4] )
00097 {
00098 _v[0] = p[0]; _v[1]= p[1]; _v[2] = p[2]; _v[3]= p[3];
00099 }
00100 inline Point4 ( const Point4 & p )
00101 {
00102 _v[0]= p._v[0]; _v[1]= p._v[1]; _v[2]= p._v[2]; _v[3]= p._v[3];
00103 }
00104 inline void SetZero()
00105 {
00106 _v[0] = _v[1] = _v[2] = _v[3]= 0;
00107 }
00108 template <class Q>
00110 inline void Import( const Point4<Q> & b )
00111 {
00112 _v[0] = T(b[0]); _v[1] = T(b[1]);
00113 _v[2] = T(b[2]);
00114 _v[3] = T(b[3]);
00115 }
00116 template <class EigenVector>
00117 inline void FromEigenVector( const EigenVector & b )
00118 {
00119 _v[0] = T(b[0]);
00120 _v[1] = T(b[1]);
00121 _v[2] = T(b[2]);
00122 _v[3] = T(b[3]);
00123 }
00125 template <class Q>
00126 static inline Point4 Construct( const Point4<Q> & b )
00127 {
00128 return Point4(T(b[0]),T(b[1]),T(b[2]),T(b[3]));
00129 }
00130
00132
00134
00138 inline const T & operator [] ( const int i ) const
00139 {
00140 assert(i>=0 && i<4);
00141 return _v[i];
00142 }
00143 inline T & operator [] ( const int i )
00144 {
00145 assert(i>=0 && i<4);
00146 return _v[i];
00147 }
00148 inline T &X() {return _v[0];}
00149 inline T &Y() {return _v[1];}
00150 inline T &Z() {return _v[2];}
00151 inline T &W() {return _v[3];}
00152 inline T const * V() const
00153 {
00154 return _v;
00155 }
00156 inline T * V()
00157 {
00158 return _v;
00159 }
00160 inline const T & V ( const int i ) const
00161 {
00162 assert(i>=0 && i<4);
00163 return _v[i];
00164 }
00165 inline T & V ( const int i )
00166 {
00167 assert(i>=0 && i<4);
00168 return _v[i];
00169 }
00172 inline T Ext( const int i ) const
00173 {
00174 if(i>=0 && i<=3) return _v[i];
00175 else return 0;
00176 }
00178
00180
00182 inline Point4 operator + ( const Point4 & p) const
00183 {
00184 return Point4( _v[0]+p._v[0], _v[1]+p._v[1], _v[2]+p._v[2], _v[3]+p._v[3] );
00185 }
00186 inline Point4 operator - ( const Point4 & p) const
00187 {
00188 return Point4( _v[0]-p._v[0], _v[1]-p._v[1], _v[2]-p._v[2], _v[3]-p._v[3] );
00189 }
00190 inline Point4 operator * ( const T s ) const
00191 {
00192 return Point4( _v[0]*s, _v[1]*s, _v[2]*s, _v[3]*s );
00193 }
00194 inline Point4 operator / ( const T s ) const
00195 {
00196 return Point4( _v[0]/s, _v[1]/s, _v[2]/s, _v[3]/s );
00197 }
00198 inline Point4 & operator += ( const Point4 & p)
00199 {
00200 _v[0] += p._v[0]; _v[1] += p._v[1]; _v[2] += p._v[2]; _v[3] += p._v[3];
00201 return *this;
00202 }
00203 inline Point4 & operator -= ( const Point4 & p )
00204 {
00205 _v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[2] -= p._v[2]; _v[3] -= p._v[3];
00206 return *this;
00207 }
00208 inline Point4 & operator *= ( const T s )
00209 {
00210 _v[0] *= s; _v[1] *= s; _v[2] *= s; _v[3] *= s;
00211 return *this;
00212 }
00213 inline Point4 & operator /= ( const T s )
00214 {
00215 _v[0] /= s; _v[1] /= s; _v[2] /= s; _v[3] /= s;
00216 return *this;
00217 }
00218 inline Point4 operator - () const
00219 {
00220 return Point4( -_v[0], -_v[1], -_v[2], -_v[3] );
00221 }
00222 inline Point4 VectProd ( const Point4 &x, const Point4 &z ) const
00223 {
00224 Point4 res;
00225 const Point4 &y = *this;
00226
00227 res[0] = y[1]*x[2]*z[3]-y[1]*x[3]*z[2]-x[1]*y[2]*z[3]+
00228 x[1]*y[3]*z[2]+z[1]*y[2]*x[3]-z[1]*y[3]*x[2];
00229 res[1] = y[0]*x[3]*z[2]-z[0]*y[2]*x[3]-y[0]*x[2]*
00230 z[3]+z[0]*y[3]*x[2]+x[0]*y[2]*z[3]-x[0]*y[3]*z[2];
00231 res[2] = -y[0]*z[1]*x[3]+x[0]*z[1]*y[3]+y[0]*x[1]*
00232 z[3]-x[0]*y[1]*z[3]-z[0]*x[1]*y[3]+z[0]*y[1]*x[3];
00233 res[3] = -z[0]*y[1]*x[2]-y[0]*x[1]*z[2]+x[0]*y[1]*
00234 z[2]+y[0]*z[1]*x[2]-x[0]*z[1]*y[2]+z[0]*x[1]*y[2];
00235 return res;
00236 }
00238
00240
00242
00243 inline T Norm() const
00244 {
00245 return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );
00246 }
00248 inline T SquaredNorm() const
00249 {
00250 return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
00251 }
00253 inline Point4 & Normalize()
00254 {
00255 T n = sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );
00256 if(n>0.0) { _v[0] /= n; _v[1] /= n; _v[2] /= n; _v[3] /= n; }
00257 return *this;
00258 }
00260 inline Point4 & HomoNormalize(){
00261 if (_v[3]!=0.0) { _v[0] /= _v[3]; _v[1] /= _v[3]; _v[2] /= _v[3]; _v[3]=1.0; }
00262 return *this;
00263 };
00264
00266
00268
00270 inline bool operator == ( const Point4& p ) const
00271 {
00272 return _v[0]==p._v[0] && _v[1]==p._v[1] && _v[2]==p._v[2] && _v[3]==p._v[3];
00273 }
00274 inline bool operator != ( const Point4 & p ) const
00275 {
00276 return _v[0]!=p._v[0] || _v[1]!=p._v[1] || _v[2]!=p._v[2] || _v[3]!=p._v[3];
00277 }
00278 inline bool operator < ( Point4 const & p ) const
00279 {
00280 return (_v[3]!=p._v[3])?(_v[3]<p._v[3]):
00281 (_v[2]!=p._v[2])?(_v[2]<p._v[2]):
00282 (_v[1]!=p._v[1])?(_v[1]<p._v[1]):
00283 (_v[0]<p._v[0]);
00284 }
00285 inline bool operator > ( const Point4 & p ) const
00286 {
00287 return (_v[3]!=p._v[3])?(_v[3]>p._v[3]):
00288 (_v[2]!=p._v[2])?(_v[2]>p._v[2]):
00289 (_v[1]!=p._v[1])?(_v[1]>p._v[1]):
00290 (_v[0]>p._v[0]);
00291 }
00292 inline bool operator <= ( const Point4 & p ) const
00293 {
00294 return (_v[3]!=p._v[3])?(_v[3]< p._v[3]):
00295 (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
00296 (_v[1]!=p._v[1])?(_v[1]< p._v[1]):
00297 (_v[0]<=p._v[0]);
00298 }
00299 inline bool operator >= ( const Point4 & p ) const
00300 {
00301 return (_v[3]!=p._v[3])?(_v[3]> p._v[3]):
00302 (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
00303 (_v[1]!=p._v[1])?(_v[1]> p._v[1]):
00304 (_v[0]>=p._v[0]);
00305 }
00307
00309
00312
00313 inline T operator * ( const Point4 & p ) const
00314 {
00315 return _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2] + _v[3]*p._v[3];
00316 }
00317 inline T dot( const Point4 & p ) const { return (*this) * p; }
00318 inline Point4 operator ^ ( const Point4& ) const
00319 {
00320 assert(0);
00321 return Point4();
00322 }
00323
00325 T StableDot ( const Point4<T> & p ) const
00326 {
00327
00328 T k0=_v[0]*p._v[0], k1=_v[1]*p._v[1], k2=_v[2]*p._v[2], k3=_v[3]*p._v[3];
00329 int exp0,exp1,exp2,exp3;
00330
00331 frexp( double(k0), &exp0 );frexp( double(k1), &exp1 );
00332 frexp( double(k2), &exp2 );frexp( double(k3), &exp3 );
00333
00334 if (exp0>exp1) { std::swap(k0,k1); std::swap(exp0,exp1); }
00335 if (exp2>exp3) { std::swap(k2,k3); std::swap(exp2,exp3); }
00336 if (exp0>exp2) { std::swap(k0,k2); std::swap(exp0,exp2); }
00337 if (exp1>exp3) { std::swap(k1,k3); std::swap(exp1,exp3); }
00338 if (exp2>exp3) { std::swap(k2,k3); std::swap(exp2,exp3); }
00339
00340 return ( (k0 + k1) + k2 ) +k3;
00341 }
00343
00344
00345 };
00346
00347 template <class T>
00348 T Angle( const Point4<T>& p1, const Point4<T> & p2 )
00349 {
00350 T w = p1.Norm()*p2.Norm();
00351 if(w==0) return -1;
00352 T t = (p1*p2)/w;
00353 if(t>1) t=1;
00354 return T( math::Acos(t) );
00355 }
00356
00357 template <class T>
00358 inline T Norm( const Point4<T> & p )
00359 {
00360 return p.Norm();
00361 }
00362
00363 template <class T>
00364 inline T SquaredNorm( const Point4<T> & p )
00365 {
00366 return p.SquaredNorm();
00367 }
00368
00369 template <class T>
00370 inline T Distance( const Point4<T> & p1, const Point4<T> & p2 )
00371 {
00372 return Norm(p1-p2);
00373 }
00374
00375 template <class T>
00376 inline T SquaredDistance( const Point4<T> & p1, const Point4<T> & p2 )
00377 {
00378 return SquaredNorm(p1-p2);
00379 }
00380
00382 template<class T>
00383 double StableDot ( Point4<T> const & p0, Point4<T> const & p1 )
00384 {
00385 return p0.StableDot(p1);
00386 }
00387
00388 typedef Point4<short> Point4s;
00389 typedef Point4<int> Point4i;
00390 typedef Point4<float> Point4f;
00391 typedef Point4<double> Point4d;
00392
00394 }
00395 #endif