Debian Codegraph @

vcg/vcg/space/point.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: point.h,v $
27 Revision 1.10 2007/01/18 01:27:44 cignoni
28 Corrected a syntax error in stabledot
29
30 Revision 1.9 2006/12/20 15:23:52 ganovelli
31 using of locally defined variable removed
32
33 Revision 1.8 2006/04/11 08:10:05 zifnab1974
34 changes necessary for gcc 3.4.5 on linux 64bit.
35
36 Revision 1.7 2005/12/12 11:22:32 ganovelli
37 compiled with gcc
38
39 Revision 1.6 2005/01/12 11:25:52 ganovelli
40 corrected Point<3
41
42 Revision 1.5 2004/10/20 16:45:21 ganovelli
43 first compiling version (MC,INtel,gcc)
44
45 Revision 1.4 2004/04/29 10:47:06 ganovelli
46 some siyntax error corrected
47
48 Revision 1.3 2004/04/05 12:36:43 tarini
49 unified version: PointBase version, with no guards "(N==3)"
50
51
52
53 Revision 1.1 2004/03/16 03:07:38 tarini
54 "dimensionally unified" version: first commit
55
56 ****************************************************************************/
57
58 #ifndef __VCGLIB_POINT
59 #define __VCGLIB_POINT
60
61 #include <assert.h>
62 #include <vcg/math/base.h>
63 #include <vcg/space/space.h>
64
65 namespace vcg {
66
67 namespace ndim{
68
69
70 template <int N, class S>
71 class Point;
72
73 /** \addtogroup space */
74 /*@{*/
75 /**
76 The templated class for representing a point in R^N space.
77 The class is templated over the ScalarType class that is used to represent coordinates.
78 PointBase provides the interface and the common operators for points
79 of any dimensionality.
80 */
81
82 template <int N, class S>
83 class PointBase
84 {
85 public:
86 typedef S ScalarType;
87 typedef VoidType ParamType;
88 typedef Point<N,S> PointType;
89 enum {Dimension=N};
90
91
92 protected:
93 /// The only data member. Hidden to user.
94 S _v[N];
95
96 public:
97
98 //@{
99
100 /** @name Standard Constructors and Initializers
101 No casting operators have been introduced to avoid automatic unattended (and costly) conversion between different PointType types
102 **/
103
104 inline PointBase () { };
105 // inline PointBase ( const S nv[N] );
106
107 /// Padding function: give a default 0 value to all the elements that are not in the [0..2] range.
108 /// Useful for managing in a consistent way object that could have point2 / point3 / point4
109 inline S Ext( const int i ) const
110 {
111 if(i>=0 && i<=N) return _v[i];
112 else return 0;
113 }
114
115 /// importer for points with different scalar type and-or dimensionality
116 template <int N2, class S2>
117 inline void Import( const Point<N2,S2> & b )
118 {
119 _v[0] = ScalarType(b[0]);
120 _v[1] = ScalarType(b[1]);
121 if (N>2) { if (N2>2) _v[2] = ScalarType(b[2]); else _v[2] = 0;};
122 if (N>3) { if (N2>3) _v[3] = ScalarType(b[3]); else _v[3] = 0;};
123 }
124
125 /// constructor for points with different scalar type and-or dimensionality
126 template <int N2, class S2>
127 static inline PointType Construct( const Point<N2,S2> & b )
128 {
129 PointType p; p.Import(b);
130 return p;
131 }
132
133 /// importer for homogeneous points
134 template <class S2>
135 inline void ImportHomo( const Point<N-1,S2> & b )
136 {
137 _v[0] = ScalarType(b[0]);
138 _v[1] = ScalarType(b[1]);
139 if (N>2) { _v[2] = ScalarType(_v[2]); };
140 _v[N-1] = 1.0;
141 }
142
143 /// constructor for homogeneus point.
144 template <int N2, class S2>
145 static inline PointType Construct( const Point<N-1,S2> & b )
146 {
147 PointType p; p.ImportHomo(b);
148 return p;
149 }
150
151 //@}
152
153 //@{
154
155 /** @name Data Access.
156 access to data is done by overloading of [] or explicit naming of coords (x,y,z)**/
157
158 inline S & operator [] ( const int i )
159 {
160 assert(i>=0 && i<N);
161 return _v[i];
162 }
163 inline const S & operator [] ( const int i ) const
164 {
165 assert(i>=0 && i<N);
166 return _v[i];
167 }
168 inline const S &X() const { return _v[0]; }
169 inline const S &Y() const { return _v[1]; }
170 inline const S &Z() const { static_assert(N>2); return _v[2]; }
171 /// W is in any case the last coordinate.
172 /// (in a 2D point, W() == Y(). In a 3D point, W()==Z()
173 /// in a 4D point, W() is a separate component)
174 inline const S &W() const { return _v[N-1]; }
175 inline S &X() { return _v[0]; }
176 inline S &Y() { return _v[1]; }
177 inline S &Z() { static_assert(N>2); return _v[2]; }
178 inline S &W() { return _v[N-1]; }
179 inline const S * V() const
180 {
181 return _v;
182 }
183 inline S & V( const int i )
184 {
185 assert(i>=0 && i<N);
186 return _v[i];
187 }
188 inline const S & V( const int i ) const
189 {
190 assert(i>=0 && i<N);
191 return _v[i];
192 }
193 //@}
194 //@{
195
196 /** @name Linearity for points
197 **/
198
199 /// sets a PointType to Zero
200 inline void Zero();
201 inline PointType operator + ( PointType const & p) const;
202 inline PointType operator - ( PointType const & p) const;
203 inline PointType operator * ( const S s ) const;
204 inline PointType operator / ( const S s ) const;
205 inline PointType & operator += ( PointType const & p);
206 inline PointType & operator -= ( PointType const & p);
207 inline PointType & operator *= ( const S s );
208 inline PointType & operator /= ( const S s );
209 inline PointType operator - () const;
210 //@}
211 //@{
212
213 /** @name Dot products (cross product "%" is defined olny for 3D points)
214 **/
215 /// Dot product
216 inline S operator * ( PointType const & p ) const;
217
218 /// slower version, more stable (double precision only)
219 inline S StableDot ( const PointType & p ) const;
220
221 //@}
222
223 //@{
224
225 /** @name Norms
226 **/
227
228 /// Euclidean norm
229 inline S Norm() const;
230 /// Euclidean norm, static version
231 template <class PT> static S Norm(const PT &p );
232 /// Squared Euclidean norm
233 inline S SquaredNorm() const;
234 /// Squared Euclidean norm, static version
235 template <class PT> static S SquaredNorm(const PT &p );
236 /// Normalization (division by norm)
237 inline PointType & Normalize();
238 /// Normalization (division by norm), static version
239 template <class PT> static PointType & Normalize(const PT &p);
240 /// Homogeneous normalization (division by W)
241 inline PointType & HomoNormalize();
242
243 /// norm infinity: largest absolute value of compoenet
244 inline S NormInfinity() const;
245 /// norm 1: sum of absolute values of components
246 inline S NormOne() const;
247
248 //@}
249 /// Signed area operator
250 /// a % b returns the signed area of the parallelogram inside a and b
251 inline S operator % ( PointType const & p ) const;
252
253 /// the sum of the components
254 inline S Sum() const;
255 /// returns the biggest component
256 inline S Max() const;
257 /// returns the smallest component
258 inline S Min() const;
259 /// returns the index of the biggest component
260 inline int MaxI() const;
261 /// returns the index of the smallest component
262 inline int MinI() const;
263
264
265 /// Per component scaling
266 inline PointType & Scale( const PointType & p );
267
268
269 /// Convert to polar coordinates
270 void ToPolar( S & ro, S & tetha, S & fi ) const
271 {
272 ro = Norm();
273 tetha = (S)atan2( _v[1], _v[0] );
274 fi = (S)acos( _v[2]/ro );
275 }
276
277 //@{
278
279 /** @name Comparison Operators.
280 Lexicographic order.
281 **/
282
283 inline bool operator == ( PointType const & p ) const;
284 inline bool operator != ( PointType const & p ) const;
285 inline bool operator < ( PointType const & p ) const;
286 inline bool operator > ( PointType const & p ) const;
287 inline bool operator <= ( PointType const & p ) const;
288 inline bool operator >= ( PointType const & p ) const;
289 //@}
290
291 //@{
292
293 /** @name
294 Glocal to Local and viceversa
295 (provided for uniformity with other spatial classes. trivial for points)
296 **/
297
298 inline PointType LocalToGlobal(ParamType p) const{
299 return *this; }
300
301 inline ParamType GlobalToLocal(PointType p) const{
302 ParamType p(); return p; }
303 //@}
304
305 }; // end class definition
306
307
308
309
310
311
312
313
314
315 template <class S>
316 class Point2 : public PointBase<2,S> {
317 public:
318 typedef S ScalarType;
319 typedef Point2 PointType;
320 using PointBase<2,S>::_v;
321 using PointBase<2,S>::V;
322 using PointBase<2,S>::W;
323
324 //@{
325 /** @name Special members for 2D points. **/
326
327 /// default
328 inline Point2 (){}
329
330 /// yx constructor
331 inline Point2 ( const S a, const S b){
332 _v[0]=a; _v[1]=b; };
333
334 /// unary orthogonal operator (2D equivalent of cross product)
335 /// returns orthogonal vector (90 deg left)
336 inline Point2 operator ~ () const {
337 return Point2 ( -_v[2], _v[1] );
338 }
339
340 /// returns the angle with X axis (radiants, in [-PI, +PI] )
341 inline ScalarType &Angle(){
342 return math::Atan2(_v[1],_v[0]);}
343
344 /// transform the point in cartesian coords into polar coords
345 inline Point2 & ToPolar(){
346 ScalarType t = Angle();
347 _v[0] = Norm();
348 _v[1] = t;
349 return *this;}
350
351 /// transform the point in polar coords into cartesian coords
352 inline Point2 & ToCartesian() {
353 ScalarType l = _v[0];
354 _v[0] = (ScalarType)(l*math::Cos(_v[1]));
355 _v[1] = (ScalarType)(l*math::Sin(_v[1]));
356 return *this;}
357
358 /// rotates the point of an angle (radiants, counterclockwise)
359 inline Point2 & Rotate( const ScalarType rad ){
360 ScalarType t = _v[0];
361 ScalarType s = math::Sin(rad);
362 ScalarType c = math::Cos(rad);
363 _v[0] = _v[0]*c - _v[1]*s;
364 _v[1] = t *s + _v[1]*c;
365 return *this;}
366
367 //@}
368
369 //@{
370 /** @name Implementation of standard functions for 3D points **/
371
372 inline void Zero(){
373 _v[0]=0; _v[1]=0; };
374
375 inline Point2 ( const S nv[2] ){
376 _v[0]=nv[0]; _v[1]=nv[1]; };
377
378 inline Point2 operator + ( Point2 const & p) const {
379 return Point2( _v[0]+p._v[0], _v[1]+p._v[1]); }
380
381 inline Point2 operator - ( Point2 const & p) const {
382 return Point2( _v[0]-p._v[0], _v[1]-p._v[1]); }
383
384 inline Point2 operator * ( const S s ) const {
385 return Point2( _v[0]*s, _v[1]*s ); }
386
387 inline Point2 operator / ( const S s ) const {
388 S t=1.0/s;
389 return Point2( _v[0]*t, _v[1]*t ); }
390
391 inline Point2 operator - () const {
392 return Point2 ( -_v[0], -_v[1] ); }
393
394 inline Point2 & operator += ( Point2 const & p ) {
395 _v[0] += p._v[0]; _v[1] += p._v[1]; return *this; }
396
397 inline Point2 & operator -= ( Point2 const & p ) {
398 _v[0] -= p._v[0]; _v[1] -= p._v[1]; return *this; }
399
400 inline Point2 & operator *= ( const S s ) {
401 _v[0] *= s; _v[1] *= s; return *this; }
402
403 inline Point2 & operator /= ( const S s ) {
404 S t=1.0/s; _v[0] *= t; _v[1] *= t; return *this; }
405
406 inline S Norm() const {
407 return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] );}
408
409 template <class PT> static S Norm(const PT &p ) {
410 return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) );}
411
412 inline S SquaredNorm() const {
413 return ( _v[0]*_v[0] + _v[1]*_v[1] );}
414
415 template <class PT> static S SquaredNorm(const PT &p ) {
416 return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) );}
417
418 inline S operator * ( Point2 const & p ) const {
419 return ( _v[0]*p._v[0] + _v[1]*p._v[1]) ; }
420
421 inline bool operator == ( Point2 const & p ) const {
422 return _v[0]==p._v[0] && _v[1]==p._v[1] ;}
423
424 inline bool operator != ( Point2 const & p ) const {
425 return _v[0]!=p._v[0] || _v[1]!=p._v[1] ;}
426
427 inline bool operator < ( Point2 const & p ) const{
428 return (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
429
430 inline bool operator > ( Point2 const & p ) const {
431 return (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
432
433 inline bool operator <= ( Point2 const & p ) {
434 return (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
435
436 inline bool operator >= ( Point2 const & p ) const {
437 return (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
438
439 inline Point2 & Normalize() {
440 PointType n = Norm(); if(n!=0.0) { n=1.0/n; _v[0]*=n; _v[1]*=n;} return *this;};
441
442 template <class PT> Point2 & Normalize(const PT &p){
443 PointType n = Norm(); if(n!=0.0) { n=1.0/n; V(0)*=n; V(1)*=n; }
444 return *this;};
445
446 inline Point2 & HomoNormalize(){
447 if (_v[2]!=0.0) { _v[0] /= W(); W()=1.0; } return *this;};
448
449 inline S NormInfinity() const {
450 return math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ); }
451
452 inline S NormOne() const {
453 return math::Abs(_v[0])+ math::Abs(_v[1]);}
454
455 inline S operator % ( Point2 const & p ) const {
456 return _v[0] * p._v[1] - _v[1] * p._v[0]; }
457
458 inline S Sum() const {
459 return _v[0]+_v[1];}
460
461 inline S Max() const {
462 return math::Max( _v[0], _v[1] ); }
463
464 inline S Min() const {
465 return math::Min( _v[0], _v[1] ); }
466
467 inline int MaxI() const {
468 return (_v[0] < _v[1]) ? 1:0; };
469
470 inline int MinI() const {
471 return (_v[0] > _v[1]) ? 1:0; };
472
473 inline PointType & Scale( const PointType & p ) {
474 _v[0] *= p._v[0]; _v[1] *= p._v[1]; return *this; }
475
476 inline S StableDot ( const PointType & p ) const {
477 return _v[0]*p._v[0] +_v[1]*p._v[1]; }
478 //@}
479
480 };
481
482 template <typename S>
483 class Point3 : public PointBase<3,S> {
484 public:
485 typedef S ScalarType;
486 typedef Point3<S> PointType;
487 using PointBase<3,S>::_v;
488 using PointBase<3,S>::V;
489 using PointBase<3,S>::W;
490
491
492 //@{
493 /** @name Special members for 3D points. **/
494
495 /// default
496 inline Point3 ():PointBase<3,S>(){}
497 /// yxz constructor
498 inline Point3 ( const S a, const S b, const S c){
499 _v[0]=a; _v[1]=b; _v[2]=c; };
500
501 /// Cross product for 3D points
502 inline PointType operator ^ ( PointType const & p ) const {
503 return Point3 (
504 _v[1]*p._v[2] - _v[2]*p._v[1],
505 _v[2]*p._v[0] - _v[0]*p._v[2],
506 _v[0]*p._v[1] - _v[1]*p._v[0] );
507 }
508 //@}
509
510 //@{
511 /** @name Implementation of standard functions for 3D points **/
512
513 inline void Zero(){
514 _v[0]=0; _v[1]=0; _v[2]=0; };
515
516 inline Point3 ( const S nv[3] ){
517 _v[0]=nv[0]; _v[1]=nv[1]; _v[2]=nv[2]; };
518
519 inline Point3 operator + ( Point3 const & p) const{
520 return Point3( _v[0]+p._v[0], _v[1]+p._v[1], _v[2]+p._v[2]); }
521
522 inline Point3 operator - ( Point3 const & p) const {
523 return Point3( _v[0]-p._v[0], _v[1]-p._v[1], _v[2]-p._v[2]); }
524
525 inline Point3 operator * ( const S s ) const {
526 return Point3( _v[0]*s, _v[1]*s , _v[2]*s ); }
527
528 inline Point3 operator / ( const S s ) const {
529 S t=1.0/s;
530 return Point3( _v[0]*t, _v[1]*t , _v[2]*t ); }
531
532 inline Point3 operator - () const {
533 return Point3 ( -_v[0], -_v[1] , -_v[2] ); }
534
535 inline Point3 & operator += ( Point3 const & p ) {
536 _v[0] += p._v[0]; _v[1] += p._v[1]; _v[2] += p._v[2]; return *this; }
537
538 inline Point3 & operator -= ( Point3 const & p ) {
539 _v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[2] -= p._v[2]; return *this; }
540
541 inline Point3 & operator *= ( const S s ) {
542 _v[0] *= s; _v[1] *= s; _v[2] *= s; return *this; }
543
544 inline Point3 & operator /= ( const S s ) {
545 S t=1.0/s; _v[0] *= t; _v[1] *= t; _v[2] *= t; return *this; }
546
547 inline S Norm() const {
548 return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );}
549
550 template <class PT> static S Norm(const PT &p ) {
551 return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) );}
552
553 inline S SquaredNorm() const {
554 return ( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );}
555
556 template <class PT> static S SquaredNorm(const PT &p ) {
557 return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) );}
558
559 inline S operator * ( PointType const & p ) const {
560 return ( _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2]) ; }
561
562 inline bool operator == ( PointType const & p ) const {
563 return _v[0]==p._v[0] && _v[1]==p._v[1] && _v[2]==p._v[2] ;}
564
565 inline bool operator != ( PointType const & p ) const {
566 return _v[0]!=p._v[0] || _v[1]!=p._v[1] || _v[2]!=p._v[2] ;}
567
568 inline bool operator < ( PointType const & p ) const{
569 return (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
570 (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
571
572 inline bool operator > ( PointType const & p ) const {
573 return (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
574 (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
575
576 inline bool operator <= ( PointType const & p ) {
577 return (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
578 (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
579
580 inline bool operator >= ( PointType const & p ) const {
581 return (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
582 (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
583
584 inline PointType & Normalize() {
585 S n = Norm();
586 if(n!=0.0) {
587 n=S(1.0)/n;
588 _v[0]*=n; _v[1]*=n; _v[2]*=n; }
589 return *this;};
590
591 template <class PT> PointType & Normalize(const PT &p){
592 S n = Norm(); if(n!=0.0) { n=1.0/n; V(0)*=n; V(1)*=n; V(2)*=n; }
593 return *this;};
594
595 inline PointType & HomoNormalize(){
596 if (_v[2]!=0.0) { _v[0] /= W(); _v[1] /= W(); W()=1.0; }
597 return *this;};
598
599 inline S NormInfinity() const {
600 return math::Max( math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ),
601 math::Abs(_v[3]) ); }
602
603 inline S NormOne() const {
604 return math::Abs(_v[0])+ math::Abs(_v[1])+math::Max(math::Abs(_v[2]));}
605
606 inline S operator % ( PointType const & p ) const {
607 S t = (*this)*p; /* Area, general formula */
608 return math::Sqrt( SquaredNorm() * p.SquaredNorm() - (t*t) );};
609
610 inline S Sum() const {
611 return _v[0]+_v[1]+_v[2];}
612
613 inline S Max() const {
614 return math::Max( math::Max( _v[0], _v[1] ), _v[2] ); }
615
616 inline S Min() const {
617 return math::Min( math::Min( _v[0], _v[1] ), _v[2] ); }
618
619 inline int MaxI() const {
620 int i= (_v[0] < _v[1]) ? 1:0; if (_v[i] < _v[2]) i=2; return i;};
621
622 inline int MinI() const {
623 int i= (_v[0] > _v[1]) ? 1:0; if (_v[i] > _v[2]) i=2; return i;};
624
625 inline PointType & Scale( const PointType & p ) {
626 _v[0] *= p._v[0]; _v[1] *= p._v[1]; _v[2] *= p._v[2]; return *this; }
627
628 inline S StableDot ( const PointType & p ) const {
629 S k0=_v[0]*p._v[0], k1=_v[1]*p._v[1], k2=_v[2]*p._v[2];
630 int exp0,exp1,exp2;
631 frexp( double(k0), &exp0 );
632 frexp( double(k1), &exp1 );
633 frexp( double(k2), &exp2 );
634 if( exp0<exp1 )
635 if(exp0<exp2) return (k1+k2)+k0; else return (k0+k1)+k2;
636 else
637 if(exp1<exp2) return (k0+k2)+k1; else return (k0+k1)+k2;
638 }
639 //@}
640
641 };
642
643 template <typename S>
644 class Point4 : public PointBase<4,S> {
645 public:
646 typedef S ScalarType;
647 typedef Point4<S> PointType;
648 using PointBase<3,S>::_v;
649 using PointBase<3,S>::V;
650 using PointBase<3,S>::W;
651
652 //@{
653 /** @name Special members for 4D points. **/
654 /// default
655 inline Point4 (){}
656
657 /// xyzw constructor
658 //@}
659 inline Point4 ( const S a, const S b, const S c, const S d){
660 _v[0]=a; _v[1]=b; _v[2]=c; _v[3]=d; };
661 //@{
662 /** @name Implementation of standard functions for 3D points **/
663
664 inline void Zero(){
665 _v[0]=0; _v[1]=0; _v[2]=0; _v[3]=0; };
666
667 inline Point4 ( const S nv[4] ){
668 _v[0]=nv[0]; _v[1]=nv[1]; _v[2]=nv[2]; _v[3]=nv[3]; };
669
670 inline Point4 operator + ( Point4 const & p) const {
671 return Point4( _v[0]+p._v[0], _v[1]+p._v[1], _v[2]+p._v[2], _v[3]+p._v[3] ); }
672
673 inline Point4 operator - ( Point4 const & p) const {
674 return Point4( _v[0]-p._v[0], _v[1]-p._v[1], _v[2]-p._v[2], _v[3]-p._v[3] ); }
675
676 inline Point4 operator * ( const S s ) const {
677 return Point4( _v[0]*s, _v[1]*s , _v[2]*s , _v[3]*s ); }
678
679 inline PointType operator ^ ( PointType const & p ) const {
680 assert(0);
681 return *this;
682 }
683
684 inline Point4 operator / ( const S s ) const {
685 S t=1.0/s;
686 return Point4( _v[0]*t, _v[1]*t , _v[2]*t , _v[3]*t ); }
687
688 inline Point4 operator - () const {
689 return Point4 ( -_v[0], -_v[1] , -_v[2] , -_v[3] ); }
690
691 inline Point4 & operator += ( Point4 const & p ) {
692 _v[0] += p._v[0]; _v[1] += p._v[1]; _v[2] += p._v[2]; _v[3] += p._v[3]; return *this; }
693
694 inline Point4 & operator -= ( Point4 const & p ) {
695 _v[0] -= p._v[0]; _v[1] -= p._v[1]; _v[2] -= p._v[2]; _v[3] -= p._v[3]; return *this; }
696
697 inline Point4 & operator *= ( const S s ) {
698 _v[0] *= s; _v[1] *= s; _v[2] *= s; _v[3] *= s; return *this; }
699
700 inline Point4 & operator /= ( const S s ) {
701 S t=1.0/s; _v[0] *= t; _v[1] *= t; _v[2] *= t; _v[3] *= t; return *this; }
702
703 inline S Norm() const {
704 return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );}
705
706 template <class PT> static S Norm(const PT &p ) {
707 return math::Sqrt( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) + p.V(3)*p.V(3) );}
708
709 inline S SquaredNorm() const {
710 return ( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] );}
711
712 template <class PT> static S SquaredNorm(const PT &p ) {
713 return ( p.V(0)*p.V(0) + p.V(1)*p.V(1) + p.V(2)*p.V(2) + p.V(3)*p.V(3) );}
714
715 inline S operator * ( PointType const & p ) const {
716 return ( _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2] + _v[3]*p._v[3] ); }
717
718 inline bool operator == ( PointType const & p ) const {
719 return _v[0]==p._v[0] && _v[1]==p._v[1] && _v[2]==p._v[2] && _v[3]==p._v[3];}
720
721 inline bool operator != ( PointType const & p ) const {
722 return _v[0]!=p._v[0] || _v[1]!=p._v[1] || _v[2]!=p._v[2] || _v[3]!=p._v[3];}
723
724 inline bool operator < ( PointType const & p ) const{
725 return (_v[3]!=p._v[3])?(_v[3]< p._v[3]) : (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
726 (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<p._v[0]); }
727
728 inline bool operator > ( PointType const & p ) const {
729 return (_v[3]!=p._v[3])?(_v[3]> p._v[3]) : (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
730 (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>p._v[0]); }
731
732 inline bool operator <= ( PointType const & p ) {
733 return (_v[3]!=p._v[3])?(_v[3]< p._v[3]) : (_v[2]!=p._v[2])?(_v[2]< p._v[2]):
734 (_v[1]!=p._v[1])?(_v[1]< p._v[1]) : (_v[0]<=p._v[0]); }
735
736 inline bool operator >= ( PointType const & p ) const {
737 return (_v[3]!=p._v[3])?(_v[3]> p._v[3]) : (_v[2]!=p._v[2])?(_v[2]> p._v[2]):
738 (_v[1]!=p._v[1])?(_v[1]> p._v[1]) : (_v[0]>=p._v[0]); }
739
740 inline PointType & Normalize() {
741 PointType n = Norm(); if(n!=0.0) { n=1.0/n; _v[0]*=n; _v[1]*=n; _v[2]*=n; _v[3]*=n; }
742 return *this;};
743
744 template <class PT> PointType & Normalize(const PT &p){
745 PointType n = Norm(); if(n!=0.0) { n=1.0/n; V(0)*=n; V(1)*=n; V(2)*=n; V(3)*=n; }
746 return *this;};
747
748 inline PointType & HomoNormalize(){
749 if (_v[3]!=0.0) { _v[0] /= W(); _v[1] /= W(); _v[2] /= W(); W()=1.0; }
750 return *this;};
751
752 inline S NormInfinity() const {
753 return math::Max( math::Max( math::Abs(_v[0]), math::Abs(_v[1]) ),
754 math::Max( math::Abs(_v[2]), math::Abs(_v[3]) ) ); }
755
756 inline S NormOne() const {
757 return math::Abs(_v[0])+ math::Abs(_v[1])+math::Max(math::Abs(_v[2]),math::Abs(_v[3]));}
758
759 inline S operator % ( PointType const & p ) const {
760 S t = (*this)*p; /* Area, general formula */
761 return math::Sqrt( SquaredNorm() * p.SquaredNorm() - (t*t) );};
762
763 inline S Sum() const {
764 return _v[0]+_v[1]+_v[2]+_v[3];}
765
766 inline S Max() const {
767 return math::Max( math::Max( _v[0], _v[1] ), math::Max( _v[2], _v[3] )); }
768
769 inline S Min() const {
770 return math::Min( math::Min( _v[0], _v[1] ), math::Min( _v[2], _v[3] )); }
771
772 inline int MaxI() const {
773 int i= (_v[0] < _v[1]) ? 1:0; if (_v[i] < _v[2]) i=2; if (_v[i] < _v[3]) i=3;
774 return i;};
775
776 inline int MinI() const {
777 int i= (_v[0] > _v[1]) ? 1:0; if (_v[i] > _v[2]) i=2; if (_v[i] > _v[3]) i=3;
778 return i;};
779
780 inline PointType & Scale( const PointType & p ) {
781 _v[0] *= p._v[0]; _v[1] *= p._v[1]; _v[2] *= p._v[2]; _v[3] *= p._v[3]; return *this; }
782
783 inline S StableDot ( const PointType & p ) const {
784 S k0=_v[0]*p._v[0], k1=_v[1]*p._v[1], k2=_v[2]*p._v[2], k3=_v[3]*p._v[3];
785 int exp0,exp1,exp2,exp3;
786 frexp( double(k0), &exp0 );frexp( double(k1), &exp1 );
787 frexp( double(k2), &exp2 );frexp( double(k3), &exp3 );
788 if (exp0>exp1) { math::Swap(k0,k1); math::Swap(exp0,exp1); }
789 if (exp2>exp3) { math::Swap(k2,k3); math::Swap(exp2,exp3); }
790 if (exp0>exp2) { math::Swap(k0,k2); math::Swap(exp0,exp2); }
791 if (exp1>exp3) { math::Swap(k1,k3); math::Swap(exp1,exp3); }
792 if (exp2>exp3) { math::Swap(k2,k3); math::Swap(exp2,exp3); }
793 return ( (k0 + k1) + k2 ) +k3; }
794
795 //@}
796 };
797
798
799 template <class S>
800 inline S Angle( Point3<S> const & p1, Point3<S> const & p2 )
801 {
802 S w = p1.Norm()*p2.Norm();
803 if(w==0) return -1;
804 S t = (p1*p2)/w;
805 if(t>1) t = 1;
806 else if(t<-1) t = -1;
807 return (S) acos(t);
808 }
809
810 // versione uguale alla precedente ma che assume che i due vettori siano unitari
811 template <class S>
812 inline S AngleN( Point3<S> const & p1, Point3<S> const & p2 )
813 {
814 S w = p1*p2;
815 if(w>1)
816 w = 1;
817 else if(w<-1)
818 w=-1;
819 return (S) acos(w);
820 }
821
822
823 template <int N,class S>
824 inline S Norm( Point<N,S> const & p )
825 {
826 return p.Norm();
827 }
828
829 template <int N,class S>
830 inline S SquaredNorm( Point<N,S> const & p )
831 {
832 return p.SquaredNorm();
833 }
834
835 template <int N,class S>
836 inline Point<N,S> & Normalize( Point<N,S> & p )
837 {
838 p.Normalize();
839 return p;
840 }
841
842 template <int N, class S>
843 inline S Distance( Point<N,S> const & p1,Point<N,S> const & p2 )
844 {
845 return (p1-p2).Norm();
846 }
847
848 template <int N, class S>
849 inline S SquaredDistance( Point<N,S> const & p1,Point<N,S> const & p2 )
850 {
851 return (p1-p2).SquaredNorm();
852 }
853
854
855 //template <typename S>
856 //struct Point2:public Point<2,S>{
857 // inline Point2(){};
858 // inline Point2(Point<2,S> const & p):Point<2,S>(p){} ;
859 // inline Point2( const S a, const S b):Point<2,S>(a,b){};
860 //};
861 //
862 //template <typename S>
863 //struct Point3:public Point3<S> {
864 // inline Point3(){};
865 // inline Point3(Point3<S> const & p):Point3<S> (p){}
866 // inline Point3( const S a, const S b, const S c):Point3<S> (a,b,c){};
867 //};
868 //
869 //
870 //template <typename S>
871 //struct Point4:public Point4<S>{
872 // inline Point4(){};
873 // inline Point4(Point4<S> const & p):Point4<S>(p){}
874 // inline Point4( const S a, const S b, const S c, const S d):Point4<S>(a,b,c,d){};
875 //};
876
877 typedef Point2<short> Point2s;
878 typedef Point2<int> Point2i;
879 typedef Point2<float> Point2f;
880 typedef Point2<double> Point2d;
881 typedef Point2<short> Vector2s;
882 typedef Point2<int> Vector2i;
883 typedef Point2<float> Vector2f;
884 typedef Point2<double> Vector2d;
885
886 typedef Point3<short> Point3s;
887 typedef Point3<int> Point3i;
888 typedef Point3<float> Point3f;
889 typedef Point3<double> Point3d;
890 typedef Point3<short> Vector3s;
891 typedef Point3<int> Vector3i;
892 typedef Point3<float> Vector3f;
893 typedef Point3<double> Vector3d;
894
895
896 typedef Point4<short> Point4s;
897 typedef Point4<int> Point4i;
898 typedef Point4<float> Point4f;
899 typedef Point4<double> Point4d;
900 typedef Point4<short> Vector4s;
901 typedef Point4<int> Vector4i;
902 typedef Point4<float> Vector4f;
903 typedef Point4<double> Vector4d;
904
905 /*@}*/
906
907
908 } // end namespace ndim
909 } // end namespace vcg
910 #endif
911