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 #if defined( OPENMESH_VECTOR_HH )
00032
00033
00034
00035 TEMPLATE_HEADER
00036 class CLASSNAME : public DERIVED
00037 {
00038 public:
00039
00040
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
00058
00060 inline VectorT() {}
00061
00063 explicit inline VectorT(const Scalar& v) {
00064
00065
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
00110
00111
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
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
00140 inline operator Scalar*() { return this->values_; }
00141
00143 inline operator const Scalar*() const { return this->values_; }
00144
00145
00146
00147
00148
00149
00151 inline Scalar& operator[](int _i) {
00152 assert(_i>=0 && _i<DIM); return this->values_[_i];
00153 }
00154
00156 inline const Scalar& operator[](int _i) const {
00157 assert(_i>=0 && _i<DIM); return this->values_[_i];
00158 }
00159
00161 inline Scalar& operator[](size_t _i) {
00162 assert(_i<DIM); return this->values_[_i];
00163 }
00164
00166 inline const Scalar& operator[](size_t _i) const {
00167 assert(_i<DIM); return this->values_[_i];
00168 }
00169
00170
00171
00172
00173
00174
00176 inline bool operator==(const vector_type& _rhs) const {
00177 #define expr(i) if(this->values_[i]!=_rhs.values_[i]) return false;
00178 unroll(expr);
00179 #undef expr
00180 return true;
00181 }
00182
00184 inline bool operator!=(const vector_type& _rhs) const {
00185 return !(*this == _rhs);
00186 }
00187
00188
00189
00190
00191
00192
00194 inline vector_type& operator*=(const Scalar& _s) {
00195 #define expr(i) this->values_[i] *= _s;
00196 unroll(expr);
00197 #undef expr
00198 return *this;
00199 }
00200
00203 inline vector_type& operator/=(const Scalar& _s) {
00204 #define expr(i) this->values_[i] /= _s;
00205 unroll(expr);
00206 #undef expr
00207 return *this;
00208 }
00209
00210
00212 inline vector_type operator*(const Scalar& _s) const {
00213 #if DIM==N
00214 return vector_type(*this) *= _s;
00215 #else
00216 #define expr(i) this->values_[i] * _s
00217 return vector_type(unroll_csv(expr));
00218 #undef expr
00219 #endif
00220 }
00221
00222
00224 inline vector_type operator/(const Scalar& _s) const {
00225 #if DIM==N
00226 return vector_type(*this) /= _s;
00227 #else
00228 #define expr(i) this->values_[i] / _s
00229 return vector_type(unroll_csv(expr));
00230 #undef expr
00231 #endif
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00242 inline vector_type& operator*=(const vector_type& _rhs) {
00243 #define expr(i) this->values_[i] *= _rhs[i];
00244 unroll(expr);
00245 #undef expr
00246 return *this;
00247 }
00248
00250 inline vector_type& operator/=(const vector_type& _rhs) {
00251 #define expr(i) this->values_[i] /= _rhs[i];
00252 unroll(expr);
00253 #undef expr
00254 return *this;
00255 }
00256
00258 inline vector_type& operator-=(const vector_type& _rhs) {
00259 #define expr(i) this->values_[i] -= _rhs[i];
00260 unroll(expr);
00261 #undef expr
00262 return *this;
00263 }
00264
00266 inline vector_type& operator+=(const vector_type& _rhs) {
00267 #define expr(i) this->values_[i] += _rhs[i];
00268 unroll(expr);
00269 #undef expr
00270 return *this;
00271 }
00272
00273
00275 inline vector_type operator*(const vector_type& _v) const {
00276 #if DIM==N
00277 return vector_type(*this) *= _v;
00278 #else
00279 #define expr(i) this->values_[i] * _v.values_[i]
00280 return vector_type(unroll_csv(expr));
00281 #undef expr
00282 #endif
00283 }
00284
00285
00287 inline vector_type operator/(const vector_type& _v) const {
00288 #if DIM==N
00289 return vector_type(*this) /= _v;
00290 #else
00291 #define expr(i) this->values_[i] / _v.values_[i]
00292 return vector_type(unroll_csv(expr));
00293 #undef expr
00294 #endif
00295 }
00296
00297
00299 inline vector_type operator+(const vector_type& _v) const {
00300 #if DIM==N
00301 return vector_type(*this) += _v;
00302 #else
00303 #define expr(i) this->values_[i] + _v.values_[i]
00304 return vector_type(unroll_csv(expr));
00305 #undef expr
00306 #endif
00307 }
00308
00309
00311 inline vector_type operator-(const vector_type& _v) const {
00312 #if DIM==N
00313 return vector_type(*this) -= _v;
00314 #else
00315 #define expr(i) this->values_[i] - _v.values_[i]
00316 return vector_type(unroll_csv(expr));
00317 #undef expr
00318 #endif
00319 }
00320
00321
00323 inline vector_type operator-(void) const {
00324 vector_type v;
00325 #define expr(i) v.values_[i] = -this->values_[i];
00326 unroll(expr);
00327 #undef expr
00328 return v;
00329 }
00330
00331
00334 inline VectorT<Scalar,3> operator%(const VectorT<Scalar,3>& _rhs) const
00335 #if DIM==3
00336 {
00337 return
00338 VectorT<Scalar,3>(this->values_[1]*_rhs.values_[2]-this->values_[2]*_rhs.values_[1],
00339 this->values_[2]*_rhs.values_[0]-this->values_[0]*_rhs.values_[2],
00340 this->values_[0]*_rhs.values_[1]-this->values_[1]*_rhs.values_[0]);
00341 }
00342 #else
00343 ;
00344 #endif
00345
00346
00349 inline Scalar operator|(const vector_type& _rhs) const {
00350 Scalar p(0);
00351 #define expr(i) p += this->values_[i] * _rhs.values_[i];
00352 unroll(expr);
00353 #undef expr
00354 return p;
00355 }
00356
00357
00358
00359
00360
00361
00362
00364
00365
00366 inline Scalar norm() const { return (Scalar)sqrt(sqrnorm()); }
00367 inline Scalar length() const { return norm(); }
00368
00370 inline Scalar sqrnorm() const {
00371 #if DIM==N
00372 Scalar s(0);
00373 #define expr(i) s += this->values_[i] * this->values_[i];
00374 unroll(expr);
00375 #undef expr
00376 return s;
00377 #else
00378 #define expr(i) this->values_[i]*this->values_[i]
00379 return (unroll_comb(expr, +));
00380 #undef expr
00381 #endif
00382 }
00384
00387 inline vector_type& normalize() {
00388 #ifdef NDEBUG
00389 operator*=(((Scalar)1.0)/norm());
00390 #else
00391 Scalar n = norm();
00392 if (n != (Scalar)0.0)
00393 *this *= Scalar(1.0/n);
00394 #endif
00395 return *this;
00396 }
00397
00398
00399
00400
00401
00403 inline Scalar max() const {
00404 Scalar m(this->values_[0]);
00405 for(int i=1; i<DIM; ++i) if(this->values_[i]>m) m=this->values_[i];
00406 return m;
00407 }
00408
00410 inline Scalar min() const {
00411 Scalar m(this->values_[0]);
00412 for(int i=1; i<DIM; ++i) if(this->values_[i]<m) m=this->values_[i];
00413 return m;
00414 }
00415
00417 inline Scalar mean() const {
00418 Scalar m(this->values_[0]);
00419 for(int i=1; i<DIM; ++i) m+=this->values_[i];
00420 return m/Scalar(DIM);
00421 }
00422
00424 inline vector_type& minimize(const vector_type& _rhs) {
00425 #define expr(i) if (_rhs[i] < this->values_[i]) this->values_[i] = _rhs[i];
00426 unroll(expr);
00427 #undef expr
00428 return *this;
00429 }
00430
00432 inline vector_type& maximize(const vector_type& _rhs) {
00433 #define expr(i) if (_rhs[i] > this->values_[i]) this->values_[i] = _rhs[i];
00434 unroll(expr);
00435 #undef expr
00436 return *this;
00437 }
00438
00440 inline vector_type min(const vector_type& _rhs) {
00441 return vector_type(*this).minimize(_rhs);
00442 }
00443
00445 inline vector_type max(const vector_type& _rhs) {
00446 return vector_type(*this).maximize(_rhs);
00447 }
00448
00449
00450
00451
00452
00453
00455 template<typename Functor>
00456 inline vector_type apply(const Functor& _func) const {
00457 vector_type result;
00458 #define expr(i) result[i] = _func(this->values_[i]);
00459 unroll(expr);
00460 #undef expr
00461 return result;
00462 }
00463
00465 vector_type& vectorize(const Scalar& _s) {
00466 #define expr(i) this->values_[i] = _s;
00467 unroll(expr);
00468 #undef expr
00469 return *this;
00470 }
00471
00472
00474 static vector_type vectorized(const Scalar& _s) {
00475 return vector_type().vectorize(_s);
00476 }
00477
00478
00480 bool operator<(const vector_type& _rhs) const {
00481 #define expr(i) if (this->values_[i] != _rhs.values_[i]) \
00482 return (this->values_[i] < _rhs.values_[i]);
00483 unroll(expr);
00484 #undef expr
00485 return false;
00486 }
00487 };
00488
00489
00490
00492 TEMPLATE_HEADER
00493 inline std::istream&
00494 operator>>(std::istream& is, VectorT<Scalar,DIM>& vec)
00495 {
00496 #define expr(i) is >> vec[i];
00497 unroll(expr);
00498 #undef expr
00499 return is;
00500 }
00501
00502
00504 TEMPLATE_HEADER
00505 inline std::ostream&
00506 operator<<(std::ostream& os, const VectorT<Scalar,DIM>& vec)
00507 {
00508 #if DIM==N
00509 for(int i=0; i<N-1; ++i) os << vec[i] << " ";
00510 os << vec[N-1];
00511 #else
00512 #define expr(i) vec[i]
00513 os << unroll_comb(expr, << " " <<);
00514 #undef expr
00515 #endif
00516
00517 return os;
00518 }
00519
00520
00521
00522 #endif // included by VectorT.hh
00523