Core/Math/VectorT_inc.hh

00001 //=============================================================================
00002 //
00003 //                               OpenMesh
00004 //      Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen
00005 //                           www.openmesh.org
00006 //
00007 //-----------------------------------------------------------------------------
00008 //
00009 //                                License
00010 //
00011 //   This library is free software; you can redistribute it and/or modify it
00012 //   under the terms of the GNU Library General Public License as published
00013 //   by the Free Software Foundation, version 2.
00014 //
00015 //   This library is distributed in the hope that it will be useful, but
00016 //   WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 //   Library General Public License for more details.
00019 //
00020 //   You should have received a copy of the GNU Library General Public
00021 //   License along with this library; if not, write to the Free Software
00022 //   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00023 //
00024 //-----------------------------------------------------------------------------
00025 //
00026 //   $Revision: 1.5.2.1 $
00027 //   $Date: 2007-02-26 12:51:17 $
00028 //
00029 //=============================================================================
00030 
00031 #if defined( OPENMESH_VECTOR_HH )
00032 
00033 // ----------------------------------------------------------------------------
00034 
00035 TEMPLATE_HEADER
00036 class CLASSNAME : public DERIVED
00037 {
00038 public:
00039 
00040   //---------------------------------------------------------------- class info
00041 
00043   typedef Scalar value_type;
00044 
00046   typedef VectorT<Scalar,DIM>  vector_type;
00047 
00049   static inline int dim() { return DIM; }
00050 
00052   static inline size_t size() { return DIM; }
00053 
00054   static const size_t size_ = DIM;
00055 
00056 
00057   //-------------------------------------------------------------- constructors
00058 
00060   inline VectorT() {}
00061 
00063   explicit inline VectorT(const Scalar& v) {
00064 //     assert(DIM==1);
00065 //     values_[0] = v0;
00066     vectorize(v);
00067   }
00068 
00070   inline VectorT(const Scalar& v0, const Scalar& v1) {
00071     assert(DIM==2);
00072     this->values_[0] = v0; this->values_[1] = v1;
00073   }
00074 
00076   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2) {
00077     assert(DIM==3);
00078     this->values_[0]=v0; this->values_[1]=v1; this->values_[2]=v2;
00079   }
00080 
00082   inline VectorT(const Scalar& v0, const Scalar& v1,
00083      const Scalar& v2, const Scalar& v3) {
00084     assert(DIM==4);
00085     this->values_[0]=v0; this->values_[1]=v1; this->values_[2]=v2; this->values_[3]=v3;
00086   }
00087 
00089   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2,
00090      const Scalar& v3, const Scalar& v4) {
00091     assert(DIM==5);
00092     this->values_[0]=v0; this->values_[1]=v1;this->values_[2]=v2; this->values_[3]=v3; this->values_[4]=v4;
00093   }
00094 
00096   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2,
00097      const Scalar& v3, const Scalar& v4, const Scalar& v5) {
00098     assert(DIM==6);
00099     this->values_[0]=v0; this->values_[1]=v1; this->values_[2]=v2;
00100     this->values_[3]=v3; this->values_[4]=v4; this->values_[5]=v5;
00101   }
00102 
00104   explicit inline VectorT(const Scalar _values[DIM]) {
00105     memcpy(this->values_, _values, DIM*sizeof(Scalar));
00106   }
00107 
00108 
00109 #ifdef OM_CC_MIPS
00111   // mipspro need this method
00112   inline vector_type& operator=(const vector_type& _rhs) {
00113     memcpy(this->values_, _rhs.values_, DIM*sizeof(Scalar));
00114     return *this;
00115   }
00116 #endif
00117 
00118 
00120   template<typename otherScalarType>
00121   explicit inline VectorT(const VectorT<otherScalarType,DIM>& _rhs) {
00122     operator=(_rhs);
00123   }
00124 
00125 
00126 
00127 
00128   //--------------------------------------------------------------------- casts
00129 
00131   template<typename otherScalarType>
00132   inline vector_type& operator=(const VectorT<otherScalarType,DIM>& _rhs) {
00133 #define expr(i)  this->values_[i] = (Scalar)_rhs[i];
00134     unroll(expr);
00135 #undef expr
00136     return *this;
00137   }
00138 
00139 
00141   inline Scalar* data() { return this->values_; }
00142 
00144   inline const Scalar*data() const { return this->values_; }
00145 
00146 
00147 
00148 
00149    //----------------------------------------------------------- element access
00150 
00152   inline Scalar& operator[](size_t _i) {
00153     assert(_i>=0 && _i<DIM); return this->values_[_i];
00154   }
00155 
00157   inline const Scalar& operator[](size_t _i) const {
00158     assert(_i>=0 && _i<DIM); return this->values_[_i];
00159   }
00160 
00161 
00162 
00163   //---------------------------------------------------------------- comparsion
00164 
00166   inline bool operator==(const vector_type& _rhs) const {
00167 #define expr(i) if(this->values_[i]!=_rhs.values_[i]) return false;
00168     unroll(expr);
00169 #undef expr
00170     return true;
00171   }
00172 
00174   inline bool operator!=(const vector_type& _rhs) const {
00175     return !(*this == _rhs);
00176   }
00177 
00178 
00179 
00180 
00181   //---------------------------------------------------------- scalar operators
00182 
00184   inline vector_type& operator*=(const Scalar& _s) {
00185 #define expr(i) this->values_[i] *= _s;
00186     unroll(expr);
00187 #undef expr
00188     return *this;
00189   }
00190 
00193   inline vector_type& operator/=(const Scalar& _s) {
00194 #define expr(i) this->values_[i] /= _s;
00195     unroll(expr);
00196 #undef expr
00197     return *this;
00198   }
00199 
00200 
00202   inline vector_type operator*(const Scalar& _s) const {
00203 #if DIM==N
00204     return vector_type(*this) *= _s;
00205 #else
00206 #define expr(i) this->values_[i] * _s
00207     return vector_type(unroll_csv(expr));
00208 #undef expr
00209 #endif
00210   }
00211 
00212 
00214   inline vector_type operator/(const Scalar& _s) const {
00215 #if DIM==N
00216     return vector_type(*this) /= _s;
00217 #else
00218 #define expr(i) this->values_[i] / _s
00219     return vector_type(unroll_csv(expr));
00220 #undef expr
00221 #endif
00222   }
00223 
00224 
00225 
00226 
00227 
00228 
00229   //---------------------------------------------------------- vector operators
00230 
00232   inline vector_type& operator*=(const vector_type& _rhs) {
00233 #define expr(i) this->values_[i] *= _rhs[i];
00234     unroll(expr);
00235 #undef expr
00236     return *this;
00237   }
00238 
00240   inline vector_type& operator/=(const vector_type& _rhs) {
00241 #define expr(i) this->values_[i] /= _rhs[i];
00242     unroll(expr);
00243 #undef expr
00244     return *this;
00245   }
00246 
00248   inline vector_type& operator-=(const vector_type& _rhs) {
00249 #define expr(i) this->values_[i] -= _rhs[i];
00250     unroll(expr);
00251 #undef expr
00252     return *this;
00253   }
00254 
00256   inline vector_type& operator+=(const vector_type& _rhs) {
00257 #define expr(i) this->values_[i] += _rhs[i];
00258     unroll(expr);
00259 #undef expr
00260     return *this;
00261   }
00262 
00263 
00265   inline vector_type operator*(const vector_type& _v) const {
00266 #if DIM==N
00267     return vector_type(*this) *= _v;
00268 #else
00269 #define expr(i) this->values_[i] * _v.values_[i]
00270     return vector_type(unroll_csv(expr));
00271 #undef expr
00272 #endif
00273   }
00274 
00275 
00277   inline vector_type operator/(const vector_type& _v) const {
00278 #if DIM==N
00279     return vector_type(*this) /= _v;
00280 #else
00281 #define expr(i) this->values_[i] / _v.values_[i]
00282     return vector_type(unroll_csv(expr));
00283 #undef expr
00284 #endif
00285   }
00286 
00287 
00289   inline vector_type operator+(const vector_type& _v) const {
00290 #if DIM==N
00291     return vector_type(*this) += _v;
00292 #else
00293 #define expr(i) this->values_[i] + _v.values_[i]
00294     return vector_type(unroll_csv(expr));
00295 #undef expr
00296 #endif
00297   }
00298 
00299 
00301   inline vector_type operator-(const vector_type& _v) const {
00302 #if DIM==N
00303     return vector_type(*this) -= _v;
00304 #else
00305 #define expr(i) this->values_[i] - _v.values_[i]
00306     return vector_type(unroll_csv(expr));
00307 #undef expr
00308 #endif
00309   }
00310 
00311 
00313   inline vector_type operator-(void) const {
00314     vector_type v;
00315 #define expr(i) v.values_[i] = -this->values_[i];
00316     unroll(expr);
00317 #undef expr
00318     return v;
00319   }
00320 
00321 
00324   inline VectorT<Scalar,3> operator%(const VectorT<Scalar,3>& _rhs) const
00325 #if DIM==3
00326   {
00327     return
00328       VectorT<Scalar,3>(this->values_[1]*_rhs.values_[2]-this->values_[2]*_rhs.values_[1],
00329                         this->values_[2]*_rhs.values_[0]-this->values_[0]*_rhs.values_[2],
00330                         this->values_[0]*_rhs.values_[1]-this->values_[1]*_rhs.values_[0]);
00331   }
00332 #else
00333   ;
00334 #endif
00335 
00336 
00339   inline Scalar operator|(const vector_type& _rhs) const {
00340   Scalar p(0);
00341 #define expr(i) p += this->values_[i] * _rhs.values_[i];
00342   unroll(expr);
00343 #undef expr
00344     return p;
00345   }
00346 
00347 
00348 
00349 
00350 
00351   //------------------------------------------------------------ euclidean norm
00352 
00354 
00355 
00356   inline Scalar norm() const { return (Scalar)sqrt(sqrnorm()); }
00357   inline Scalar length() const { return norm(); } // OpenSG interface
00358 
00360   inline Scalar sqrnorm() const {
00361 #if DIM==N
00362     Scalar s(0);
00363 #define expr(i) s += this->values_[i] * this->values_[i];
00364     unroll(expr);
00365 #undef expr
00366     return s;
00367 #else
00368 #define expr(i) this->values_[i]*this->values_[i]
00369     return (unroll_comb(expr, +));
00370 #undef expr
00371 #endif
00372   }
00374 
00377   inline vector_type& normalize() {
00378 #ifdef NDEBUG
00379     operator*=(((Scalar)1.0)/norm());
00380 #else
00381     Scalar n = norm();
00382     if (n != (Scalar)0.0)
00383       *this *= Scalar(1.0/n);
00384 #endif
00385     return *this;
00386   }
00387 
00388 
00389 
00390   //------------------------------------------------------------ max, min, mean
00391 
00393   inline Scalar max() const {
00394     Scalar m(this->values_[0]);
00395     for(int i=1; i<DIM; ++i) if(this->values_[i]>m) m=this->values_[i];
00396     return m;
00397   }
00398 
00400   inline Scalar min() const {
00401     Scalar m(this->values_[0]);
00402     for(int i=1; i<DIM; ++i) if(this->values_[i]<m) m=this->values_[i];
00403     return m;
00404   }
00405 
00407   inline Scalar mean() const {
00408     Scalar m(this->values_[0]);
00409     for(int i=1; i<DIM; ++i) m+=this->values_[i];
00410     return m/Scalar(DIM);
00411   }
00412 
00414   inline vector_type& minimize(const vector_type& _rhs) {
00415 #define expr(i) if (_rhs[i] < this->values_[i]) this->values_[i] = _rhs[i];
00416     unroll(expr);
00417 #undef expr
00418     return *this;
00419   }
00420 
00422   inline vector_type& maximize(const vector_type& _rhs) {
00423 #define expr(i) if (_rhs[i] > this->values_[i]) this->values_[i] = _rhs[i];
00424     unroll(expr);
00425 #undef expr
00426     return *this;
00427   }
00428 
00430   inline vector_type min(const vector_type& _rhs) {
00431     return vector_type(*this).minimize(_rhs);
00432   }
00433 
00435   inline vector_type max(const vector_type& _rhs) {
00436     return vector_type(*this).maximize(_rhs);
00437   }
00438 
00439 
00440 
00441 
00442   //------------------------------------------------------------ misc functions
00443 
00445   template<typename Functor>
00446   inline vector_type apply(const Functor& _func) const {
00447     vector_type result;
00448 #define expr(i) result[i] = _func(this->values_[i]);
00449     unroll(expr);
00450 #undef expr
00451     return result;
00452   }
00453 
00455   vector_type& vectorize(const Scalar& _s) {
00456 #define expr(i) this->values_[i] = _s;
00457     unroll(expr);
00458 #undef expr
00459     return *this;
00460   }
00461 
00462 
00464   static vector_type vectorized(const Scalar& _s) {
00465     return vector_type().vectorize(_s);
00466   }
00467 
00468 
00470   bool operator<(const vector_type& _rhs) const {
00471 #define expr(i) if (this->values_[i] != _rhs.values_[i]) \
00472                    return (this->values_[i] < _rhs.values_[i]);
00473     unroll(expr);
00474 #undef expr
00475     return false;
00476    }
00477 };
00478 
00479 
00480 
00482 TEMPLATE_HEADER
00483 inline std::istream&
00484 operator>>(std::istream& is, VectorT<Scalar,DIM>& vec)
00485 {
00486 #define expr(i) is >> vec[i];
00487   unroll(expr);
00488 #undef expr
00489   return is;
00490 }
00491 
00492 
00494 TEMPLATE_HEADER
00495 inline std::ostream&
00496 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
00497 {
00498 #if DIM==N
00499   for(int i=0; i<N-1; ++i) os << vec[i] << " ";
00500   os << vec[N-1];
00501 #else
00502 #define expr(i) vec[i]
00503   os << unroll_comb(expr, << " " <<);
00504 #undef expr
00505 #endif
00506 
00507   return os;
00508 }
00509 
00510 
00511 // ----------------------------------------------------------------------------
00512 #endif // included by VectorT.hh
00513 //=============================================================================

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .