From ba3e4370bb904228a02e5622bdc3689294065559 Mon Sep 17 00:00:00 2001 From: Luigi Malomo Date: Fri, 12 Nov 2021 19:27:30 +0100 Subject: [PATCH] added scalar * point operators + added correct normalize/normalized functions to points + some code cleaning --- vcg/space/deprecated_point2.h | 55 +++++++++--- vcg/space/deprecated_point3.h | 152 ++++++++++++++++++---------------- vcg/space/deprecated_point4.h | 45 ++++++++-- 3 files changed, 160 insertions(+), 92 deletions(-) diff --git a/vcg/space/deprecated_point2.h b/vcg/space/deprecated_point2.h index 6241e78d..5db300a3 100644 --- a/vcg/space/deprecated_point2.h +++ b/vcg/space/deprecated_point2.h @@ -193,12 +193,19 @@ public: _v[1] *= s; return *this; } + inline Point2 & operator /= ( const ScalarType s ) { _v[0] /= s; _v[1] /= s; return *this; } + + inline Point2 operator - (void) const + { + return Point2(-_v[0], -_v[1]); + } + //@} /// returns the norm (Euclidian) inline ScalarType Norm( void ) const @@ -216,7 +223,8 @@ public: _v[1] *= sy; return * this; } - /// normalizes, and returns itself as result + + /// normalizes, and returns itself as result (nonsense) inline Point2 & Normalize( void ) { ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]); @@ -225,6 +233,19 @@ public: } return *this; } + + inline void normalize(void) + { + this->Normalize(); + } + + inline Point2 normalized(void) const + { + Point2 p = *this; + p.normalize(); + return p; + } + /// points equality inline bool operator == ( const Point2 & p ) const { @@ -374,42 +395,50 @@ inline T Angle( Point2 const & p0, Point2 const & p1 ) } template -inline Point2 operator - ( Point2 const & p ){ - return Point2( -p[0], -p[1] ); -} - -template -inline Point2 operator * ( const T s, Point2 const & p ){ +inline Point2 operator * ( const T s, Point2 const & p ) +{ return Point2( p[0] * s, p[1] * s ); } template -inline T Norm( Point2 const & p ){ +inline T Norm( Point2 const & p ) +{ return p.Norm(); } template -inline T SquaredNorm( Point2 const & p ){ +inline T SquaredNorm( Point2 const & p ) +{ return p.SquaredNorm(); } template -inline Point2 & Normalize( Point2 & p ){ +inline Point2 & Normalize( Point2 & p ) +{ return p.Normalize(); } +template +inline Point2 Normalized(const Point2 & p ) +{ + return p.normalized(); +} + template -inline T Distance( Point2 const & p1,Point2 const & p2 ){ +inline T Distance( Point2 const & p1,Point2 const & p2 ) +{ return Norm(p1-p2); } template -inline T SquaredDistance( Point2 const & p1,Point2 const & p2 ){ +inline T SquaredDistance( Point2 const & p1,Point2 const & p2 ) +{ return SquaredNorm(p1-p2); } template -inline Point2 Abs(const Point2 & p) { +inline Point2 Abs(const Point2 & p) +{ return (Point2(math::Abs(p[0]), math::Abs(p[1]))); } diff --git a/vcg/space/deprecated_point3.h b/vcg/space/deprecated_point3.h index 3362a633..81e67862 100644 --- a/vcg/space/deprecated_point3.h +++ b/vcg/space/deprecated_point3.h @@ -277,12 +277,9 @@ public: assert(i>=0 && i<3); return _v[i]; } -//@} -//@{ - /** @name Classical overloading of operators - Note - **/ + /** @name Classical overloading of operators + **/ inline Point3 operator + ( Point3 const & p) const { @@ -306,7 +303,7 @@ public: return ( _v[0]*p._v[0] + _v[1]*p._v[1] + _v[2]*p._v[2] ); } inline P3ScalarType dot( const Point3 & p ) const { return (*this) * p; } - /// Cross product + /// Cross product inline Point3 operator ^ ( Point3 const & p ) const { return Point3 @@ -345,7 +342,8 @@ public: _v[2] /= s; return *this; } - // Norme + + // Norms inline P3ScalarType Norm() const { return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] ); @@ -370,67 +368,69 @@ public: return *this; } - // Normalizzazione - inline Point3 & Normalize() - { - P3ScalarType n = P3ScalarType(math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2])); - if (n > P3ScalarType(0)) { _v[0] /= n; _v[1] /= n; _v[2] /= n; } - return *this; - } + // Normalization + inline Point3 & Normalize() + { + P3ScalarType n = P3ScalarType(math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2])); + if (n > P3ScalarType(0)) { _v[0] /= n; _v[1] /= n; _v[2] /= n; } + return *this; + } - // for compatibility with eigen port - inline Point3 normalized() const - { - Point3 p = *this; - p.Normalize(); - return p; - } + inline void normalize(void) + { + this->Normalize(); + } - /** - * Convert to polar coordinates from cartesian coordinates. - * - * Theta is the azimuth angle and ranges between [0, 2PI) degrees. - * Phi is the elevation angle (not the polar angle) and ranges between [-PI/2, PI/2] degrees. - * - * /note Note that instead of the classical polar angle, which ranges between - * 0 and PI degrees we opt for the elevation angle to obtain a more - * intuitive spherical coordinate system. - */ - void ToPolarRad(P3ScalarType &ro, P3ScalarType &theta, P3ScalarType &phi) const - { - ro = Norm(); - theta = (P3ScalarType)atan2(_v[2], _v[0]); - phi = (P3ScalarType)asin(_v[1]/ro); - } + inline Point3 normalized(void) const + { + Point3 p = *this; + p.normalize(); + return p; + } - /** - * Convert from polar coordinates to cartesian coordinates. - * - * Theta is the azimuth angle and ranges between [0, 2PI) radians. - * Phi is the elevation angle (not the polar angle) and ranges between [-PI/2, PI/2] radians. - * - * \note Note that instead of the classical polar angle, which ranges between - * 0 and PI degrees, we opt for the elevation angle to obtain a more - * intuitive spherical coordinate system. - */ - void FromPolarRad(const P3ScalarType &ro, const P3ScalarType &theta, const P3ScalarType &phi) - { - _v[0]= ro*cos(theta)*cos(phi); - _v[1]= ro*sin(phi); - _v[2]= ro*sin(theta)*cos(phi); - } + /** + * Convert to polar coordinates from cartesian coordinates. + * + * Theta is the azimuth angle and ranges between [0, 2PI) degrees. + * Phi is the elevation angle (not the polar angle) and ranges between [-PI/2, PI/2] degrees. + * + * /note Note that instead of the classical polar angle, which ranges between + * 0 and PI degrees we opt for the elevation angle to obtain a more + * intuitive spherical coordinate system. + */ + void ToPolarRad(P3ScalarType &ro, P3ScalarType &theta, P3ScalarType &phi) const + { + ro = Norm(); + theta = (P3ScalarType)atan2(_v[2], _v[0]); + phi = (P3ScalarType)asin(_v[1]/ro); + } - Box3 GetBBox(Box3 &bb) const; -//@} -//@{ + /** + * Convert from polar coordinates to cartesian coordinates. + * + * Theta is the azimuth angle and ranges between [0, 2PI) radians. + * Phi is the elevation angle (not the polar angle) and ranges between [-PI/2, PI/2] radians. + * + * \note Note that instead of the classical polar angle, which ranges between + * 0 and PI degrees, we opt for the elevation angle to obtain a more + * intuitive spherical coordinate system. + */ + void FromPolarRad(const P3ScalarType &ro, const P3ScalarType &theta, const P3ScalarType &phi) + { + _v[0]= ro*cos(theta)*cos(phi); + _v[1]= ro*sin(phi); + _v[2]= ro*sin(theta)*cos(phi); + } - size_t MaxCoeffId() const - { - if (_v[0]>_v[1]) - return _v[0]>_v[2] ? 0 : 2; - else - return _v[1]>_v[2] ? 1 : 2; - } + Box3 GetBBox(Box3 &bb) const; + + size_t MaxCoeffId() const + { + if (_v[0]>_v[1]) + return _v[0]>_v[2] ? 0 : 2; + else + return _v[1]>_v[2] ? 1 : 2; + } /** @name Comparison Operators. Note that the reverse z prioritized ordering, useful in many situations. **/ @@ -468,8 +468,7 @@ inline bool operator == ( Point3 const & p ) const (_v[0]>=p._v[0]); } - - inline Point3 operator - () const + inline Point3 operator - (void) const { return Point3 ( -_v[0], -_v[1], -_v[2] ); } @@ -505,32 +504,37 @@ inline P3ScalarType AngleN( Point3 const & p1, Point3 inline P3ScalarType Norm( Point3 const & p ) { - return p.Norm(); + return p.Norm(); } template inline P3ScalarType SquaredNorm( Point3 const & p ) { - return p.SquaredNorm(); + return p.SquaredNorm(); } template inline Point3 & Normalize( Point3 & p ) { - p.Normalize(); - return p; + return p.Normalize(); +} + +template +inline Point3 Normalized(const Point3 & p) +{ + return p.normalized(); } template inline P3ScalarType Distance( Point3 const & p1,Point3 const & p2 ) { - return (p1-p2).Norm(); + return (p1-p2).Norm(); } template inline P3ScalarType SquaredDistance( Point3 const & p1,Point3 const & p2 ) { - return (p1-p2).SquaredNorm(); + return (p1-p2).SquaredNorm(); } template @@ -632,6 +636,12 @@ inline Point3 LowClampToZero(const Point3 & p) { return (Point3(std::max(p[0], (SCALARTYPE)0), std::max(p[1], (SCALARTYPE)0), std::max(p[2], (SCALARTYPE)0))); } +template +inline Point3 operator*(const Scalar s, const Point3 & p) +{ + return (p * s); +} + typedef Point3 Point3s; typedef Point3 Point3i; typedef Point3 Point3f; diff --git a/vcg/space/deprecated_point4.h b/vcg/space/deprecated_point4.h index 39b5d135..2b1b0410 100644 --- a/vcg/space/deprecated_point4.h +++ b/vcg/space/deprecated_point4.h @@ -274,21 +274,44 @@ public: return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]; } /// Euclidian normalization - inline Point4 & Normalize() + inline Point4 & Normalize() { T n = sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3] ); if(n>0.0) { _v[0] /= n; _v[1] /= n; _v[2] /= n; _v[3] /= n; } return *this; } - /// Homogeneous normalization (division by W) - inline Point4 & HomoNormalize(){ - if (_v[3]!=0.0) { _v[0] /= _v[3]; _v[1] /= _v[3]; _v[2] /= _v[3]; _v[3]=1.0; } - return *this; - }; -//@} + inline void normalize(void) + { + this->Normalize(); + } + + inline Point4 normalized(void) const + { + Point4 p = *this; + p.normalize(); + return p; + } + + /// Homogeneous normalization (division by W) + inline Point4 & HomoNormalize() + { + if (_v[3]!=0.0) { _v[0] /= _v[3]; _v[1] /= _v[3]; _v[2] /= _v[3]; _v[3]=1.0; } + return *this; + }; + + inline void homoNormalize(void) + { + this->HomoNormalize(); + }; + + inline Point4 homoNormalized(void) const + { + Point4 p = *this; + p.homoNormalize(); + return p; + } -//@{ /** @name Comparison operators (lexicographical order) **/ inline bool operator == ( const Point4& p ) const @@ -409,6 +432,12 @@ double StableDot ( Point4 const & p0, Point4 const & p1 ) return p0.StableDot(p1); } +template +inline Point4 operator*(const Scalar s, const Point4 & p) +{ + return (p * s); +} + typedef Point4 Point4s; typedef Point4 Point4i; typedef Point4 Point4f;