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;
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<ScalarType> p = *this;
p.normalize();
return p;
}
/// points equality
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>
inline Point2<T> operator - ( 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 ){
inline Point2<T> operator * ( const T s, Point2<T> const & p )
{
return Point2<T>( p[0] * s, p[1] * s );
}
template <class T>
inline T Norm( Point2<T> const & p ){
inline T Norm( Point2<T> const & p )
{
return p.Norm();
}
template <class T>
inline T SquaredNorm( Point2<T> const & p ){
inline T SquaredNorm( Point2<T> const & p )
{
return p.SquaredNorm();
}
template <class T>
inline Point2<T> & Normalize( Point2<T> & p ){
inline Point2<T> & Normalize( Point2<T> & p )
{
return p.Normalize();
}
template <typename Scalar>
inline Point2<Scalar> Normalized(const Point2<Scalar> & p )
{
return p.normalized();
}
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);
}
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);
}
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])));
}

View File

@ -277,11 +277,8 @@ public:
assert(i>=0 && i<3);
return _v[i];
}
//@}
//@{
/** @name Classical overloading of operators
Note
**/
inline Point3 operator + ( Point3 const & p) const
@ -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,7 +368,7 @@ public:
return *this;
}
// Normalizzazione
// Normalization
inline Point3 & Normalize()
{
P3ScalarType n = P3ScalarType(math::Sqrt(_v[0]*_v[0] + _v[1]*_v[1] + _v[2]*_v[2]));
@ -378,11 +376,15 @@ public:
return *this;
}
// for compatibility with eigen port
inline Point3 normalized() const
inline void normalize(void)
{
this->Normalize();
}
inline Point3 normalized(void) const
{
Point3<P3ScalarType> p = *this;
p.Normalize();
p.normalize();
return p;
}
@ -421,8 +423,6 @@ public:
}
Box3<P3ScalarType> GetBBox(Box3<P3ScalarType> &bb) const;
//@}
//@{
size_t MaxCoeffId() const
{
@ -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<P3ScalarType> ( -_v[0], -_v[1], -_v[2] );
}
@ -517,8 +516,13 @@ inline P3ScalarType SquaredNorm( Point3<P3ScalarType> const & p )
template <class P3ScalarType>
inline Point3<P3ScalarType> & Normalize( Point3<P3ScalarType> & p )
{
p.Normalize();
return p;
return p.Normalize();
}
template <typename Scalar>
inline Point3<Scalar> Normalized(const Point3<Scalar> & p)
{
return p.normalized();
}
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)));
}
template <typename Scalar>
inline Point3<Scalar> operator*(const Scalar s, const Point3<Scalar> & p)
{
return (p * s);
}
typedef Point3<short> Point3s;
typedef Point3<int> Point3i;
typedef Point3<float> Point3f;

View File

@ -280,15 +280,38 @@ public:
if(n>0.0) { _v[0] /= n; _v[1] /= n; _v[2] /= n; _v[3] /= n; }
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(){
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)
**/
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);
}
template <typename Scalar>
inline Point4<Scalar> operator*(const Scalar s, const Point4<Scalar> & p)
{
return (p * s);
}
typedef Point4<short> Point4s;
typedef Point4<int> Point4i;
typedef Point4<float> Point4f;