owlps/owlps-positioner/src/point3d.hh

266 lines
6.3 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* Copyright © Université de Franche-Comté 2007-2012.
*
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
*
***********************************************************************
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL:
* http://www.cecill.info
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided
* only with a limited warranty and the software's authors, the holder
* of the economic rights, and the successive licensors have only
* limited liability.
*
* In this respect, the user's attention is drawn to the risks
* associated with loading, using, modifying and/or developing or
* reproducing the software by the user in light of its specific status
* of free software, that may mean that it is complicated to manipulate,
* and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users
* are therefore encouraged to load and test the software's suitability
* as regards their requirements in conditions enabling the security of
* their systems and/or data to be ensured and, more generally, to use
* and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
***********************************************************************
*/
#ifndef _OWLPS_POSITIONING_POINT3D_HH_
#define _OWLPS_POSITIONING_POINT3D_HH_
#include <string>
#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:
Point3D(const float _x = 0, const float _y = 0, const float _z = 0):
x(_x), y(_y), z(_z) {}
Point3D(const Point3D &source):
x(source.x), y(source.y), z(source.z) {}
Point3D(const float source[3]):
x(source[0]), y(source[1]), z(source[2]) {}
Point3D(const std::string &source) ;
virtual ~Point3D(void) {}
/** @name Read accessors */
//@{
float get_x(void) const ;
float get_y(void) const ;
float get_z(void) const ;
//@}
/** @name Write accessors */
//@{
void set_x(const float _x) ;
void set_y(const float _y) ;
void set_z(const float _z) ;
void set_coordinates(const float _x, const float _y, const float _z) ;
void set_coordinates(const float source[3]) ;
void set_coordinates(const Point3D &source) ;
//@}
/** @name Distance & angles operations */
//@{
/// Square euclidean distance to a Point3D, in 2D
float square_distance_2d(const Point3D &p) const ;
/// Square euclidean distance to a Point3D
float square_distance(const Point3D &p) const ;
/// Euclidean distance to a Point3D, in 2D
float distance_2d(const Point3D &p) const ;
/// Euclidean distance to a Point3D
float distance(const Point3D &p) const ;
/// Euclidean distance to the radius of a sphere
float distance_to_sphere(const Point3D &centre,
const float radius) const ;
/// Angle BÂC (A being *this) in 2D
double angle_2d(const Point3D &b, const Point3D &c) const ;
/// Rotate #x and #y with a given angle around a center c
void rotate_2d(const Point3D &center, float angle) ;
//@}
/** @name Operators */
//@{
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 bool(void) const ;
operator std::string(void) const ;
//@}
/// Displays a Point3D
friend std::ostream& operator<<(std::ostream &os, const Point3D &p) ;
/// Hashes a Point3D
friend std::size_t hash_value(const Point3D &source) ;
} ;
/* *** 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 ;
}
inline void Point3D::
set_coordinates(const float _x, const float _y, const float _z)
{
x = _x ;
y = _y ;
z = _z ;
}
inline void Point3D::
set_coordinates(const float source[3])
{
x = source[0] ;
y = source[1] ;
z = source[2] ;
}
/**
* 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_2d(),
* which is more efficient.
*/
inline float Point3D::distance_2d(const Point3D &source) const
{
return sqrt(square_distance_2d(source)) ;
}
/**
* 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)) ;
}
/**
* @param centre The centre of the sphere.
* @param radius The radius of the sphere.
*/
inline float Point3D::distance_to_sphere(
const Point3D &centre, const float radius) const
{
return distance(centre) - radius ;
}
/* *** 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_