Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

VectorT_inc.hh

00001 //=============================================================================
00002 //                                                                            
00003 //                               OpenMesh                                     
00004 //        Copyright (C) 2003 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.10 $
00027 //   $Date: 2004/01/08 17:59:26 $
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& v0) {
00064     assert(DIM==1);
00065     values_[0] = v0; 
00066   }
00067 
00069   inline VectorT(const Scalar& v0, const Scalar& v1) {
00070     assert(DIM==2);
00071     values_[0] = v0; values_[1] = v1; 
00072   }
00073 
00075   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2) {
00076     assert(DIM==3);
00077     values_[0]=v0; values_[1]=v1; values_[2]=v2; 
00078   }
00079 
00081   inline VectorT(const Scalar& v0, const Scalar& v1, 
00082                  const Scalar& v2, const Scalar& v3) {
00083     assert(DIM==4); 
00084     values_[0]=v0; values_[1]=v1; values_[2]=v2; values_[3]=v3; 
00085   }
00086 
00088   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2, 
00089                  const Scalar& v3, const Scalar& v4) { 
00090     assert(DIM==5); 
00091     values_[0]=v0; values_[1]=v1;values_[2]=v2; values_[3]=v3; values_[4]=v4; 
00092   }
00093 
00095   inline VectorT(const Scalar& v0, const Scalar& v1, const Scalar& v2,
00096                  const Scalar& v3, const Scalar& v4, const Scalar& v5) {
00097     assert(DIM==6);
00098     values_[0]=v0; values_[1]=v1; values_[2]=v2;
00099     values_[3]=v3; values_[4]=v4; values_[5]=v5; 
00100   }
00101 
00103   explicit inline VectorT(const Scalar _values[DIM]) {
00104     memcpy(values_, _values, DIM*sizeof(Scalar)); 
00105   }
00106 
00107 
00108 #ifdef OM_CC_MIPS
00109 
00110   // mipspro need this method
00111   inline vector_type& operator=(const vector_type& _rhs) {
00112     memcpy(values_, _rhs.values_, DIM*sizeof(Scalar)); 
00113     return *this;
00114   }
00115 #endif
00116 
00117 
00119   template<typename otherScalarType>
00120   explicit inline VectorT(const VectorT<otherScalarType,DIM>& _rhs) { 
00121     operator=(_rhs); 
00122   }
00123 
00124 
00125 
00126 
00127   //--------------------------------------------------------------------- casts
00128 
00130   template<typename otherScalarType>
00131   inline vector_type& operator=(const VectorT<otherScalarType,DIM>& _rhs) {
00132 #define expr(i)  values_[i] = (Scalar)_rhs[i];
00133     unroll(expr);
00134 #undef expr
00135     return *this; 
00136   }
00137 
00139   inline operator Scalar*() { return values_; }
00140 
00142   inline operator const Scalar*() const { return values_; }
00143 
00144 
00145 
00146 
00147    //----------------------------------------------------------- element access
00148 
00150   inline Scalar& operator[](int _i) {
00151     assert(_i>=0 && _i<DIM); return values_[_i]; 
00152   }
00153 
00155   inline const Scalar& operator[](int _i) const {
00156     assert(_i>=0 && _i<DIM); return values_[_i]; 
00157   }
00158 
00160   inline Scalar& operator[](size_t _i) {
00161     assert(_i<DIM); return values_[_i]; 
00162   }
00163 
00165   inline const Scalar& operator[](size_t _i) const {
00166     assert(_i<DIM); return values_[_i]; 
00167   }
00168 
00169 
00170 
00171 
00172   //---------------------------------------------------------------- comparsion
00173 
00175   inline bool operator==(const vector_type& _rhs) const {
00176 #define expr(i) if(values_[i]!=_rhs.values_[i]) return false;
00177     unroll(expr);
00178 #undef expr
00179     return true; 
00180   }
00181 
00183   inline bool operator!=(const vector_type& _rhs) const {
00184     return !(*this == _rhs);
00185   }
00186 
00187 
00188 
00189 
00190   //---------------------------------------------------------- scalar operators
00191 
00193   inline vector_type& operator*=(const Scalar& _s) {
00194 #define expr(i) values_[i] *= _s; 
00195     unroll(expr);
00196 #undef expr
00197     return *this; 
00198   }
00199 
00202   inline vector_type& operator/=(const Scalar& _s) {
00203 #define expr(i) values_[i] /= _s; 
00204     unroll(expr);
00205 #undef expr
00206     return *this; 
00207   }
00208 
00209 
00211   inline vector_type operator*(const Scalar& _s) const {
00212 #if DIM==N
00213     return vector_type(*this) *= _s;
00214 #else
00215 #define expr(i) values_[i] * _s
00216     return vector_type(unroll_csv(expr));
00217 #undef expr
00218 #endif
00219   }
00220 
00221 
00223   inline vector_type operator/(const Scalar& _s) const {
00224 #if DIM==N
00225     return vector_type(*this) /= _s;
00226 #else
00227 #define expr(i) values_[i] / _s
00228     return vector_type(unroll_csv(expr));
00229 #undef expr
00230 #endif
00231   }
00232 
00233 
00234 
00235 
00236 
00237 
00238   //---------------------------------------------------------- vector operators
00239 
00241   inline vector_type& operator*=(const vector_type& _rhs) {
00242 #define expr(i) values_[i] *= _rhs[i]; 
00243     unroll(expr);
00244 #undef expr
00245     return *this; 
00246   }
00247 
00249   inline vector_type& operator/=(const vector_type& _rhs) {
00250 #define expr(i) values_[i] /= _rhs[i]; 
00251     unroll(expr);
00252 #undef expr
00253     return *this; 
00254   }
00255 
00257   inline vector_type& operator-=(const vector_type& _rhs) {
00258 #define expr(i) values_[i] -= _rhs[i]; 
00259     unroll(expr);
00260 #undef expr
00261     return *this; 
00262   }
00263 
00265   inline vector_type& operator+=(const vector_type& _rhs) {
00266 #define expr(i) values_[i] += _rhs[i]; 
00267     unroll(expr);
00268 #undef expr
00269     return *this; 
00270   }
00271   
00272 
00274   inline vector_type operator*(const vector_type& _v) const {
00275 #if DIM==N
00276     return vector_type(*this) *= _v;
00277 #else
00278 #define expr(i) values_[i] * _v.values_[i]
00279     return vector_type(unroll_csv(expr));
00280 #undef expr
00281 #endif
00282   }
00283 
00284 
00286   inline vector_type operator/(const vector_type& _v) const {
00287 #if DIM==N
00288     return vector_type(*this) /= _v;
00289 #else
00290 #define expr(i) values_[i] / _v.values_[i]
00291     return vector_type(unroll_csv(expr));
00292 #undef expr
00293 #endif
00294   }
00295 
00296 
00298   inline vector_type operator+(const vector_type& _v) const {
00299 #if DIM==N
00300     return vector_type(*this) += _v;
00301 #else
00302 #define expr(i) values_[i] + _v.values_[i]
00303     return vector_type(unroll_csv(expr));
00304 #undef expr
00305 #endif
00306   }
00307 
00308 
00310   inline vector_type operator-(const vector_type& _v) const {
00311 #if DIM==N
00312     return vector_type(*this) -= _v;
00313 #else
00314 #define expr(i) values_[i] - _v.values_[i]
00315     return vector_type(unroll_csv(expr));
00316 #undef expr
00317 #endif
00318   }
00319 
00320 
00322   inline vector_type operator-(void) const {
00323     vector_type v;
00324 #define expr(i) v.values_[i] = -values_[i];
00325     unroll(expr);
00326 #undef expr
00327     return v; 
00328   }
00329 
00330 
00333   inline VectorT<Scalar,3> operator%(const VectorT<Scalar,3>& _rhs) const 
00334 #if DIM==3
00335   {
00336     return 
00337       VectorT<Scalar,3>(values_[1]*_rhs.values_[2]-values_[2]*_rhs.values_[1],
00338                         values_[2]*_rhs.values_[0]-values_[0]*_rhs.values_[2],
00339                         values_[0]*_rhs.values_[1]-values_[1]*_rhs.values_[0]);
00340   }
00341 #else
00342   ;
00343 #endif
00344 
00345 
00348   inline Scalar operator|(const vector_type& _rhs) const {
00349   Scalar p(0);
00350 #define expr(i) p += values_[i] * _rhs.values_[i]; 
00351   unroll(expr);
00352 #undef expr
00353     return p; 
00354   }
00355 
00356 
00357 
00358 
00359 
00360   //------------------------------------------------------------ euclidean norm
00361 
00363 
00364 
00365   inline Scalar norm() const { return (Scalar)sqrt(sqrnorm()); }
00366   inline Scalar length() const { return norm(); } // OpenSG interface
00367 
00369   inline Scalar sqrnorm() const {
00370 #if DIM==N
00371     Scalar s(0);
00372 #define expr(i) s += values_[i] * values_[i]; 
00373     unroll(expr);
00374 #undef expr
00375     return s;
00376 #else
00377 #define expr(i) values_[i]*values_[i]
00378     return (unroll_comb(expr, +));
00379 #undef expr
00380 #endif
00381   }
00383 
00386   inline vector_type& normalize() {
00387 #ifdef NDEBUG
00388     operator*=(((Scalar)1.0)/norm());
00389 #else
00390     Scalar n = norm();
00391     if (n != (Scalar)0.0)
00392       *this *= Scalar(1.0/n);
00393 #endif
00394     return *this;
00395   }
00396   
00397 
00398 
00399   //------------------------------------------------------------ max, min, mean
00400 
00402   inline Scalar max() const {
00403     Scalar m(values_[0]);
00404     for(int i=1; i<DIM; ++i) if(values_[i]>m) m=values_[i];
00405     return m; 
00406   }
00407     
00409   inline Scalar min() const {
00410     Scalar m(values_[0]); 
00411     for(int i=1; i<DIM; ++i) if(values_[i]<m) m=values_[i];
00412     return m; 
00413   }
00414     
00416   inline Scalar mean() const {
00417     Scalar m(values_[0]); 
00418     for(int i=1; i<DIM; ++i) m+=values_[i];
00419     return m/Scalar(DIM); 
00420   }
00421 
00423   inline vector_type minimize(const vector_type& _rhs) {
00424 #define expr(i) if (_rhs[i] < values_[i]) values_[i] = _rhs[i];
00425     unroll(expr);
00426 #undef expr
00427     return *this;
00428   }
00429 
00431   inline vector_type maximize(const vector_type& _rhs) {
00432 #define expr(i) if (_rhs[i] > values_[i]) values_[i] = _rhs[i];
00433     unroll(expr);
00434 #undef expr
00435     return *this;
00436   }
00437 
00439   inline vector_type min(const vector_type& _rhs) {
00440     return vector_type(*this).minimize(_rhs);
00441   }
00442   
00444   inline vector_type max(const vector_type& _rhs) {
00445     return vector_type(*this).maximize(_rhs);
00446   }
00447   
00448   
00449 
00450 
00451   //------------------------------------------------------------ misc functions
00452 
00454   template<typename Functor>
00455   inline vector_type apply(const Functor& _func) const {
00456     vector_type result;
00457 #define expr(i) result[i] = _func(values_[i]);
00458     unroll(expr);
00459 #undef expr
00460     return result; 
00461   }
00462 
00464   vector_type& vectorize(const Scalar& _s) {
00465 #define expr(i) values_[i] = _s;
00466     unroll(expr);
00467 #undef expr
00468     return *this;
00469   }
00470 
00471 
00473   static vector_type vectorized(const Scalar& _s) {
00474     return vector_type().vectorize(_s);
00475   }
00476 
00477 
00479   bool operator<(const vector_type& _rhs) const {
00480 #define expr(i) if (values_[i] != _rhs.values_[i]) \
00481                    return (values_[i] < _rhs.values_[i]);
00482     unroll(expr);
00483 #undef expr
00484     return false;
00485    }
00486 };
00487 
00488 
00489 
00491 TEMPLATE_HEADER
00492 inline std::istream& 
00493 operator>>(std::istream& is, VectorT<Scalar,DIM>& vec)
00494 {
00495 #define expr(i) is >> vec[i];
00496   unroll(expr);
00497 #undef expr
00498   return is;
00499 }
00500 
00501 
00503 TEMPLATE_HEADER
00504 inline std::ostream& 
00505 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec) 
00506 {
00507 #if DIM==N
00508   for(int i=0; i<N-1; ++i) os << vec[i] << " ";
00509   os << vec[N-1];
00510 #else
00511 #define expr(i) vec[i]
00512   os << unroll_comb(expr, << " " <<);
00513 #undef expr 
00514 #endif
00515 
00516   return os;
00517 }
00518 
00519 
00520 // ----------------------------------------------------------------------------
00521 #endif // included by VectorT.hh
00522 //=============================================================================

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