24 #ifndef __VCGLIB_POINT3
25 #define __VCGLIB_POINT3
29 #include <vcg/math/base.h>
40 template <
class T>
class Box3;
42 template <
class P3ScalarType>
class Point3
49 typedef P3ScalarType ScalarType;
60 inline Point3 (
const P3ScalarType nx,
const P3ScalarType ny,
const P3ScalarType nz )
79 inline Point3 (
const P3ScalarType nv[3] )
93 _v[0] = p[0];
_v[1] = p[1];
_v[2] = p[2];
105 inline P3ScalarType
Ext(
const int i )
const
107 if(i>=0 && i<=2)
return _v[i];
112 inline void Import(
const Point3<Q> & b )
114 _v[0] = P3ScalarType(b[0]);
115 _v[1] = P3ScalarType(b[1]);
116 _v[2] = P3ScalarType(b[2]);
118 template <
class EigenVector>
119 inline void FromEigenVector(
const EigenVector & b )
121 _v[0] = P3ScalarType(b[0]);
122 _v[1] = P3ScalarType(b[1]);
123 _v[2] = P3ScalarType(b[2]);
125 template <
class EigenVector>
126 inline void ToEigenVector( EigenVector & b )
const
132 template <
class EigenVector>
133 inline EigenVector ToEigenVector(
void)
const
135 static_assert(EigenVector::RowsAtCompileTime == 3 || EigenVector::RowsAtCompileTime == 4,
136 "EigenVector type has not 3 or 4 components");
137 EigenVector b = EigenVector::Zero();
144 static inline Point3 Construct(
const Point3<Q> & b )
146 return Point3(P3ScalarType(b[0]),P3ScalarType(b[1]),P3ScalarType(b[2]));
150 static inline Point3 Construct(
const Q & P0,
const Q & P1,
const Q & P2)
152 return Point3(P3ScalarType(P0),P3ScalarType(P1),P3ScalarType(P2));
155 static inline Point3 Construct(
const Point3<ScalarType> & b )
160 static inline Point3 Zero(
void)
162 return Point3(0,0,0);
165 static inline Point3 One(
void)
167 return Point3(1,1,1);
177 inline P3ScalarType & operator [] (
const int i )
182 inline const P3ScalarType & operator [] (
const int i )
const
187 inline const P3ScalarType &X()
const {
return _v[0]; }
188 inline const P3ScalarType &Y()
const {
return _v[1]; }
189 inline const P3ScalarType &Z()
const {
return _v[2]; }
190 inline P3ScalarType &X() {
return _v[0]; }
191 inline P3ScalarType &Y() {
return _v[1]; }
192 inline P3ScalarType &Z() {
return _v[2]; }
193 inline const P3ScalarType * V()
const
197 inline P3ScalarType * V()
201 inline P3ScalarType & V(
const int i )
206 inline const P3ScalarType & V(
const int i )
const
215 inline Point3 operator + ( Point3
const & p)
const
217 return Point3<P3ScalarType>(
_v[0]+p._v[0],
_v[1]+p._v[1],
_v[2]+p._v[2] );
219 inline Point3 operator - ( Point3
const & p)
const
221 return Point3<P3ScalarType>(
_v[0]-p._v[0],
_v[1]-p._v[1],
_v[2]-p._v[2] );
223 inline Point3 operator * (
const P3ScalarType s )
const
225 return Point3<P3ScalarType>(
_v[0]*s,
_v[1]*s,
_v[2]*s );
227 inline Point3 operator / (
const P3ScalarType s )
const
229 return Point3<P3ScalarType>(
_v[0]/s,
_v[1]/s,
_v[2]/s );
232 inline P3ScalarType operator * (
Point3 const & p )
const
234 return (
_v[0]*p.
_v[0] +
_v[1]*p.
_v[1] +
_v[2]*p.
_v[2] );
236 inline P3ScalarType dot(
const Point3 & p )
const {
return (*
this) * p; }
255 inline Point3 & operator -= ( Point3
const & p)
262 inline Point3 & operator *= (
const P3ScalarType s )
269 inline Point3 & operator /= (
const P3ScalarType s )
278 inline P3ScalarType Norm()
const
280 return math::Sqrt(
_v[0]*
_v[0] +
_v[1]*
_v[1] +
_v[2]*
_v[2] );
282 inline P3ScalarType SquaredNorm()
const
287 inline Point3 & Scale(
const P3ScalarType sx,
const P3ScalarType sy,
const P3ScalarType sz )
294 inline Point3 & Scale(
const Point3 & p )
303 inline Point3 & Normalize()
305 P3ScalarType n = P3ScalarType(math::Sqrt(
_v[0]*
_v[0] +
_v[1]*
_v[1] +
_v[2]*
_v[2]));
306 if (n > P3ScalarType(0)) {
_v[0] /= n;
_v[1] /= n;
_v[2] /= n; }
310 inline void normalize(
void)
315 inline Point3 normalized(
void)
const
317 Point3<P3ScalarType> p = *
this;
332 void ToPolarRad(P3ScalarType &ro, P3ScalarType &theta, P3ScalarType &phi)
const
335 theta = (P3ScalarType)atan2(
_v[2],
_v[0]);
336 phi = (P3ScalarType)asin(
_v[1]/ro);
349 void FromPolarRad(
const P3ScalarType &ro,
const P3ScalarType &theta,
const P3ScalarType &phi)
351 _v[0]= ro*cos(theta)*cos(phi);
353 _v[2]= ro*sin(theta)*cos(phi);
358 size_t MaxCoeffId()
const
361 return _v[0]>
_v[2] ? 0 : 2;
363 return _v[1]>
_v[2] ? 1 : 2;
369 inline bool operator == ( Point3
const & p )
const
371 return _v[0]==p._v[0] &&
_v[1]==p._v[1] &&
_v[2]==p._v[2];
373 inline bool operator != ( Point3
const & p )
const
375 return _v[0]!=p._v[0] ||
_v[1]!=p._v[1] ||
_v[2]!=p._v[2];
377 inline bool operator < ( Point3
const & p )
const
379 return (
_v[2]!=p._v[2])?(
_v[2]<p._v[2]):
380 (
_v[1]!=p._v[1])?(
_v[1]<p._v[1]):
383 inline bool operator > ( Point3
const & p )
const
385 return (
_v[2]!=p._v[2])?(
_v[2]>p._v[2]):
386 (
_v[1]!=p._v[1])?(
_v[1]>p._v[1]):
389 inline bool operator <= ( Point3
const & p )
const
391 return (
_v[2]!=p._v[2])?(
_v[2]< p._v[2]):
392 (
_v[1]!=p._v[1])?(
_v[1]< p._v[1]):
395 inline bool operator >= ( Point3
const & p )
const
397 return (
_v[2]!=p._v[2])?(
_v[2]> p._v[2]):
398 (
_v[1]!=p._v[1])?(
_v[1]> p._v[1]):
402 inline Point3 operator - (
void)
const
404 return Point3<P3ScalarType> ( -
_v[0], -
_v[1], -
_v[2] );
411 template <
class P3ScalarType>
412 inline P3ScalarType Angle( Point3<P3ScalarType>
const & p1, Point3<P3ScalarType>
const & p2 )
414 P3ScalarType w = p1.Norm()*p2.Norm();
416 P3ScalarType t = (p1*p2)/w;
418 else if(t<-1) t = -1;
419 return (P3ScalarType) acos(t);
423 template <
class P3ScalarType>
424 inline P3ScalarType AngleN( Point3<P3ScalarType>
const & p1, Point3<P3ScalarType>
const & p2 )
426 P3ScalarType w = p1*p2;
431 return (P3ScalarType) acos(w);
435 template <
class P3ScalarType>
436 inline P3ScalarType Norm( Point3<P3ScalarType>
const & p )
441 template <
class P3ScalarType>
442 inline P3ScalarType SquaredNorm( Point3<P3ScalarType>
const & p )
444 return p.SquaredNorm();
447 template <
class P3ScalarType>
448 inline Point3<P3ScalarType> & Normalize( Point3<P3ScalarType> & p )
450 return p.Normalize();
453 template <
typename Scalar>
454 inline Point3<Scalar> Normalized(
const Point3<Scalar> & p)
456 return p.normalized();
459 template <
class P3ScalarType>
460 inline P3ScalarType Distance( Point3<P3ScalarType>
const & p1,Point3<P3ScalarType>
const & p2 )
462 return (p1-p2).Norm();
465 template <
class P3ScalarType>
466 inline P3ScalarType SquaredDistance( Point3<P3ScalarType>
const & p1,Point3<P3ScalarType>
const & p2 )
468 return (p1-p2).SquaredNorm();
471 template <
class P3ScalarType>
472 P3ScalarType ApproximateGeodesicDistance(
const Point3<P3ScalarType>& p0,
const Point3<P3ScalarType>& n0,
473 const Point3<P3ScalarType>& p1,
const Point3<P3ScalarType>& n1)
475 Point3<P3ScalarType> V(p0-p1);
477 const P3ScalarType C0 = V*n0;
478 const P3ScalarType C1 = V*n1;
479 const P3ScalarType De = Distance(p0,p1);
480 if(fabs(C0-C1)<0.0001)
return De/sqrt(1-C0*C1);
481 const P3ScalarType Dg = ((asin(C0) - asin(C1))/(C0-C1));
489 template<
class P3ScalarType>
490 double stable_dot ( Point3<P3ScalarType>
const & p0, Point3<P3ScalarType>
const & p1 )
492 P3ScalarType k0 = p0._v[0]*p1._v[0];
493 P3ScalarType k1 = p0._v[1]*p1._v[1];
494 P3ScalarType k2 = p0._v[2]*p1._v[2];
498 frexp(
double(k0), &exp0 );
499 frexp(
double(k1), &exp1 );
500 frexp(
double(k2), &exp2 );
521 template<
class P3ScalarType>
528 P3ScalarType t = ((p-v1)*e)/e.SquaredNorm();
532 return Distance(p,q);
536 template <
class P3ScalarType>
537 void GetUV( Point3<P3ScalarType> &n,Point3<P3ScalarType> &u, Point3<P3ScalarType> &v, Point3<P3ScalarType> up=(Point3<P3ScalarType>(0,1,0)) )
540 const double LocEps=double(1e-7);
542 double len = u.Norm();
545 if(fabs(n[0])<fabs(n[1])){
546 if(fabs(n[0])<fabs(n[2])) up=Point3<P3ScalarType>(1,0,0);
547 else up=Point3<P3ScalarType>(0,0,1);
549 if(fabs(n[1])<fabs(n[2])) up=Point3<P3ScalarType>(0,1,0);
550 else up=Point3<P3ScalarType>(0,0,1);
560 template <
class SCALARTYPE>
561 inline Point3<SCALARTYPE> Abs(
const Point3<SCALARTYPE> & p) {
562 return (Point3<SCALARTYPE>(math::Abs(p[0]), math::Abs(p[1]), math::Abs(p[2])));
565 template <
class SCALARTYPE>
566 inline Point3<SCALARTYPE> LowClampToZero(
const Point3<SCALARTYPE> & p) {
567 return (Point3<SCALARTYPE>(std::max(p[0], (SCALARTYPE)0), std::max(p[1], (SCALARTYPE)0), std::max(p[2], (SCALARTYPE)0)));
570 template <
typename Scalar>
571 inline Point3<Scalar> operator*(
const Scalar s,
const Point3<Scalar> & p)
576 typedef Point3<short> Point3s;
577 typedef Point3<int> Point3i;
578 typedef Point3<float> Point3f;
579 typedef Point3<double> Point3d;
Point3 & operator=(Point3 const &p)=default
Point3(Point3 const &p)=default
P3ScalarType _v[3]
The only data member. Hidden to user.
Definition: point3.h:46
void ToPolarRad(P3ScalarType &ro, P3ScalarType &theta, P3ScalarType &phi) const
Definition: point3.h:332
P3ScalarType Ext(const int i) const
Definition: point3.h:105
void FromPolarRad(const P3ScalarType &ro, const P3ScalarType &theta, const P3ScalarType &phi)
Definition: point3.h:349
Point3 operator^(Point3 const &p) const
Cross product.
Definition: point3.h:238
Point3(Point3< Q > const &p)
Definition: point3.h:72
Definition: namespaces.dox:6
P3ScalarType PSDist(const Point3< P3ScalarType > &p, const Point3< P3ScalarType > &v1, const Point3< P3ScalarType > &v2, Point3< P3ScalarType > &q)
Point(p) Edge(v1-v2) dist, q is the point in v1-v2 with min dist.
Definition: point3.h:522