added scalar * point operators + added correct normalize/normalized functions to points + some code cleaning

This commit is contained in:
Luigi Malomo 2021-11-12 19:27:30 +01:00
parent aee9055ffe
commit ba3e4370bb
3 changed files with 160 additions and 92 deletions

View File

@ -193,12 +193,19 @@ public:
_v[1] *= s; _v[1] *= s;
return *this; return *this;
} }
inline Point2 & operator /= ( const ScalarType s ) inline Point2 & operator /= ( const ScalarType s )
{ {
_v[0] /= s; _v[0] /= s;
_v[1] /= s; _v[1] /= s;
return *this; return *this;
} }
inline Point2 operator - (void) const
{
return Point2(-_v[0], -_v[1]);
}
//@} //@}
/// returns the norm (Euclidian) /// returns the norm (Euclidian)
inline ScalarType Norm( void ) const inline ScalarType Norm( void ) const
@ -216,7 +223,8 @@ public:
_v[1] *= sy; _v[1] *= sy;
return * this; return * this;
} }
/// normalizes, and returns itself as result
/// normalizes, and returns itself as result (nonsense)
inline Point2 & Normalize( void ) inline Point2 & Normalize( void )
{ {
ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]); ScalarType n = math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1]);
@ -225,6 +233,19 @@ public:
} }
return *this; return *this;
} }
inline void normalize(void)
{
this->Normalize();
}
inline Point2 normalized(void) const
{
Point2<ScalarType> p = *this;
p.normalize();
return p;
}
/// points equality /// points equality
inline bool operator == ( const Point2 & p ) const inline bool operator == ( const Point2 & p ) const
{ {
@ -374,42 +395,50 @@ inline T Angle( Point2<T> const & p0, Point2<T> const & p1 )
} }
template <class T> template <class T>
inline Point2<T> operator - ( Point2<T> const & p ){ inline Point2<T> operator * ( const T s, Point2<T> const & p )
return Point2<T>( -p[0], -p[1] ); {
}
template <class T>
inline Point2<T> operator * ( const T s, Point2<T> const & p ){
return Point2<T>( p[0] * s, p[1] * s ); return Point2<T>( p[0] * s, p[1] * s );
} }
template <class T> template <class T>
inline T Norm( Point2<T> const & p ){ inline T Norm( Point2<T> const & p )
{
return p.Norm(); return p.Norm();
} }
template <class T> template <class T>
inline T SquaredNorm( Point2<T> const & p ){ inline T SquaredNorm( Point2<T> const & p )
{
return p.SquaredNorm(); return p.SquaredNorm();
} }
template <class T> template <class T>
inline Point2<T> & Normalize( Point2<T> & p ){ inline Point2<T> & Normalize( Point2<T> & p )
{
return p.Normalize(); return p.Normalize();
} }
template <typename Scalar>
inline Point2<Scalar> Normalized(const Point2<Scalar> & p )
{
return p.normalized();
}
template <class T> template <class T>
inline T Distance( Point2<T> const & p1,Point2<T> const & p2 ){ inline T Distance( Point2<T> const & p1,Point2<T> const & p2 )
{
return Norm(p1-p2); return Norm(p1-p2);
} }
template <class T> template <class T>
inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 ){ inline T SquaredDistance( Point2<T> const & p1,Point2<T> const & p2 )
{
return SquaredNorm(p1-p2); return SquaredNorm(p1-p2);
} }
template <class T> template <class T>
inline Point2<T> Abs(const Point2<T> & p) { inline Point2<T> Abs(const Point2<T> & p)
{
return (Point2<T>(math::Abs(p[0]), math::Abs(p[1]))); return (Point2<T>(math::Abs(p[0]), math::Abs(p[1])));
} }

View File

@ -277,12 +277,9 @@ public:
assert(i>=0 && i<3); assert(i>=0 && i<3);
return _v[i]; return _v[i];
} }
//@}
//@{
/** @name Classical overloading of operators /** @name Classical overloading of operators
Note **/
**/
inline Point3 operator + ( Point3 const & p) const 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] ); 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; } inline P3ScalarType dot( const Point3 & p ) const { return (*this) * p; }
/// Cross product /// Cross product
inline Point3 operator ^ ( Point3 const & p ) const inline Point3 operator ^ ( Point3 const & p ) const
{ {
return Point3 <P3ScalarType> return Point3 <P3ScalarType>
@ -345,7 +342,8 @@ public:
_v[2] /= s; _v[2] /= s;
return *this; return *this;
} }
// Norme
// Norms
inline P3ScalarType Norm() const inline P3ScalarType Norm() const
{ {
return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] ); return math::Sqrt( _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] );
@ -370,67 +368,69 @@ public:
return *this; return *this;
} }
// Normalizzazione // Normalization
inline Point3 & Normalize() inline Point3 & Normalize()
{ {
P3ScalarType n = P3ScalarType(math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2])); 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; } if (n > P3ScalarType(0)) { _v[0] /= n; _v[1] /= n; _v[2] /= n; }
return *this; return *this;
} }
// for compatibility with eigen port inline void normalize(void)
inline Point3 normalized() const {
{ this->Normalize();
Point3<P3ScalarType> p = *this; }
p.Normalize();
return p;
}
/** inline Point3 normalized(void) const
* Convert to polar coordinates from cartesian coordinates. {
* Point3<P3ScalarType> p = *this;
* Theta is the azimuth angle and ranges between [0, 2PI) degrees. p.normalize();
* Phi is the elevation angle (not the polar angle) and ranges between [-PI/2, PI/2] degrees. return p;
* }
* /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);
}
/** /**
* Convert from polar coordinates to cartesian coordinates. * Convert to polar coordinates from cartesian coordinates.
* *
* Theta is the azimuth angle and ranges between [0, 2PI) radians. * 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] radians. * 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 * /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 * 0 and PI degrees we opt for the elevation angle to obtain a more
* intuitive spherical coordinate system. * intuitive spherical coordinate system.
*/ */
void FromPolarRad(const P3ScalarType &ro, const P3ScalarType &theta, const P3ScalarType &phi) void ToPolarRad(P3ScalarType &ro, P3ScalarType &theta, P3ScalarType &phi) const
{ {
_v[0]= ro*cos(theta)*cos(phi); ro = Norm();
_v[1]= ro*sin(phi); theta = (P3ScalarType)atan2(_v[2], _v[0]);
_v[2]= ro*sin(theta)*cos(phi); phi = (P3ScalarType)asin(_v[1]/ro);
} }
Box3<P3ScalarType> GetBBox(Box3<P3ScalarType> &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 Box3<P3ScalarType> GetBBox(Box3<P3ScalarType> &bb) const;
{
if (_v[0]>_v[1]) size_t MaxCoeffId() const
return _v[0]>_v[2] ? 0 : 2; {
else if (_v[0]>_v[1])
return _v[1]>_v[2] ? 1 : 2; return _v[0]>_v[2] ? 0 : 2;
} else
return _v[1]>_v[2] ? 1 : 2;
}
/** @name Comparison Operators. /** @name Comparison Operators.
Note that the reverse z prioritized ordering, useful in many situations. 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]); (_v[0]>=p._v[0]);
} }
inline Point3 operator - (void) const
inline Point3 operator - () const
{ {
return Point3<P3ScalarType> ( -_v[0], -_v[1], -_v[2] ); return Point3<P3ScalarType> ( -_v[0], -_v[1], -_v[2] );
} }
@ -505,32 +504,37 @@ inline P3ScalarType AngleN( Point3<P3ScalarType> const & p1, Point3<P3ScalarType
template <class P3ScalarType> template <class P3ScalarType>
inline P3ScalarType Norm( Point3<P3ScalarType> const & p ) inline P3ScalarType Norm( Point3<P3ScalarType> const & p )
{ {
return p.Norm(); return p.Norm();
} }
template <class P3ScalarType> template <class P3ScalarType>
inline P3ScalarType SquaredNorm( Point3<P3ScalarType> const & p ) inline P3ScalarType SquaredNorm( Point3<P3ScalarType> const & p )
{ {
return p.SquaredNorm(); return p.SquaredNorm();
} }
template <class P3ScalarType> template <class P3ScalarType>
inline Point3<P3ScalarType> & Normalize( Point3<P3ScalarType> & p ) inline Point3<P3ScalarType> & Normalize( Point3<P3ScalarType> & p )
{ {
p.Normalize(); return p.Normalize();
return p; }
template <typename Scalar>
inline Point3<Scalar> Normalized(const Point3<Scalar> & p)
{
return p.normalized();
} }
template <class P3ScalarType> template <class P3ScalarType>
inline P3ScalarType Distance( Point3<P3ScalarType> const & p1,Point3<P3ScalarType> const & p2 ) inline P3ScalarType Distance( Point3<P3ScalarType> const & p1,Point3<P3ScalarType> const & p2 )
{ {
return (p1-p2).Norm(); return (p1-p2).Norm();
} }
template <class P3ScalarType> template <class P3ScalarType>
inline P3ScalarType SquaredDistance( Point3<P3ScalarType> const & p1,Point3<P3ScalarType> const & p2 ) inline P3ScalarType SquaredDistance( Point3<P3ScalarType> const & p1,Point3<P3ScalarType> const & p2 )
{ {
return (p1-p2).SquaredNorm(); return (p1-p2).SquaredNorm();
} }
template <class P3ScalarType> template <class P3ScalarType>
@ -632,6 +636,12 @@ inline Point3<SCALARTYPE> LowClampToZero(const Point3<SCALARTYPE> & p) {
return (Point3<SCALARTYPE>(std::max(p[0], (SCALARTYPE)0), std::max(p[1], (SCALARTYPE)0), std::max(p[2], (SCALARTYPE)0))); return (Point3<SCALARTYPE>(std::max(p[0], (SCALARTYPE)0), std::max(p[1], (SCALARTYPE)0), std::max(p[2], (SCALARTYPE)0)));
} }
template <typename Scalar>
inline Point3<Scalar> operator*(const Scalar s, const Point3<Scalar> & p)
{
return (p * s);
}
typedef Point3<short> Point3s; typedef Point3<short> Point3s;
typedef Point3<int> Point3i; typedef Point3<int> Point3i;
typedef Point3<float> Point3f; typedef Point3<float> Point3f;

View File

@ -274,21 +274,44 @@ public:
return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3]; return _v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2] + _v[3]*_v[3];
} }
/// Euclidian normalization /// 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] ); 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; } if(n>0.0) { _v[0] /= n; _v[1] /= n; _v[2] /= n; _v[3] /= n; }
return *this; 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<ScalarType> 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<ScalarType> p = *this;
p.homoNormalize();
return p;
}
//@{
/** @name Comparison operators (lexicographical order) /** @name Comparison operators (lexicographical order)
**/ **/
inline bool operator == ( const Point4& p ) const inline bool operator == ( const Point4& p ) const
@ -409,6 +432,12 @@ double StableDot ( Point4<T> const & p0, Point4<T> const & p1 )
return p0.StableDot(p1); return p0.StableDot(p1);
} }
template <typename Scalar>
inline Point4<Scalar> operator*(const Scalar s, const Point4<Scalar> & p)
{
return (p * s);
}
typedef Point4<short> Point4s; typedef Point4<short> Point4s;
typedef Point4<int> Point4i; typedef Point4<int> Point4i;
typedef Point4<float> Point4f; typedef Point4<float> Point4f;