owlps/owlps-positioning/src/point3d.hh

184 lines
3.8 KiB
C++
Raw Normal View History

#ifndef _OWLPS_POSITIONING_POINT3D_HH_
#define _OWLPS_POSITIONING_POINT3D_HH_
#include <ostream>
#include <cmath>
/// Represents a point of the 3-D space
class Point3D
{
protected:
float x ; ///< X horizontal coordinate (abscissa)
float y ; ///< Y horizontal coordinate (ordinate)
float z ; ///< Vertical coordinate
public:
/// 3 float constructor or default constructor
Point3D(const float &_x = 0, const float &_y = 0, const float &_z = 0) ;
/// Copy constructor
Point3D(const Point3D &source) ;
/// 3-float array constructor
Point3D(const float source[3]) ;
virtual ~Point3D(void) {} ///< Destructor (do nothing)
/** @name Read accessors */
//@{
float get_x(void) const ; ///< #x read accessor
float get_y(void) const ; ///< #y read accessor
float get_z(void) const ; ///< #z read accessor
//@}
/** @name Write accessors */
//@{
void set_x(const float &_x) ; ///< #x write accessor
void set_y(const float &_y) ; ///< #y write accessor
void set_z(const float &_z) ; ///< #z write accessor
void set_coordinates(const Point3D &source) ; ///< Sets all coordinates
//@}
/** @name Distance operations */
//@{
/// Square euclidean distance to a Point3D
float square_distance(const Point3D &p) const ;
/// Euclidean distance to a Point3D
float distance(const Point3D &p) const ;
/// Square euclidean distance to a 3-float defined point
float square_distance(const float &mx,
const float &my,
const float &mz) const ;
/// Euclidean distance to a 3-float defined point
float distance(const float &mx,
const float &my,
const float &mz) const ;
//@}
/** @name Operators */
//@{
const Point3D& operator=(const Point3D &source) ;
bool operator==(const Point3D &source) const ;
bool operator!=(const Point3D &source) const ;
bool operator<(const Point3D &source) const ;
bool operator>(const Point3D &source) const ;
bool operator<=(const Point3D &source) const ;
bool operator>=(const Point3D &source) const ;
operator std::string(void) const ;
//@}
/// Displays a Point3D
friend std::ostream& operator<<(std::ostream &os, const Point3D &p) ;
} ;
/* *** Read accessors *** */
inline float Point3D::get_x() const
{
return x ;
}
inline float Point3D::get_y() const
{
return y ;
}
inline float Point3D::get_z() const
{
return z ;
}
/* *** Write accessors *** */
inline void Point3D::set_x(const float &_x)
{
x = _x ;
}
inline void Point3D::set_y(const float &_y)
{
y = _y ;
}
inline void Point3D::set_z(const float &_z)
{
z = _z ;
}
/**
* Updates x, y, z by passing a Point3D.
* This is useful for derivated classes, and different than a direct
* call to operator=().
*/
inline void Point3D::set_coordinates(const Point3D &source)
{
this->operator=(source) ;
}
/* *** Distance operations *** */
/**
* Note: to compare distances, use preferably square_distance(),
* which is more efficient.
*/
inline float Point3D::distance(const Point3D &source) const
{
return sqrt(square_distance(source)) ;
}
/**
* Note: to compare distances, use preferably square_distance(),
* which is more efficient.
*/
inline float Point3D::distance(const float &_x,
const float &_y,
const float &_z) const
{
return sqrt(square_distance(_x, _y, _z)) ;
}
/* *** Operators *** */
inline bool Point3D::operator!=(const Point3D &source) const
{
return !(*this == source) ;
}
inline bool Point3D::operator>(const Point3D &source) const
{
return source < *this ;
}
inline bool Point3D::operator<=(const Point3D &source) const
{
return *this == source || *this < source ;
}
inline bool Point3D::operator>=(const Point3D &source) const
{
return source <= *this ;
}
#endif // _OWLPS_POSITIONING_POINT3D_HH_