2009-12-07 16:03:53 +01:00
|
|
|
#include "point3d.hh"
|
2011-04-27 18:47:56 +02:00
|
|
|
#include "posexcept.hh"
|
2009-12-07 16:03:53 +01:00
|
|
|
|
2010-02-04 18:46:23 +01:00
|
|
|
#include <sstream>
|
2010-06-07 15:09:22 +02:00
|
|
|
#include <boost/functional/hash.hpp>
|
2009-12-07 16:03:53 +01:00
|
|
|
|
2011-04-27 18:47:56 +02:00
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
|
|
|
Point3D::Point3D(const string &source)
|
|
|
|
{
|
|
|
|
float pos[3] ;
|
|
|
|
istringstream iss(source) ;
|
|
|
|
|
|
|
|
for (int i = 0 ; i < 2 ; ++i)
|
|
|
|
{
|
|
|
|
iss >> pos[i] ;
|
|
|
|
if (iss.get() != ';')
|
|
|
|
throw malformed_input_data(
|
|
|
|
"Point3D(string): cannot extract coordinates!") ;
|
|
|
|
}
|
|
|
|
iss >> pos[2] ;
|
|
|
|
|
|
|
|
set_coordinates(pos) ;
|
|
|
|
}
|
|
|
|
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Distance operations *** */
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* The distance is not square rooted after the computation, in order
|
|
|
|
* to optimise comparisons.
|
2009-12-07 16:03:53 +01:00
|
|
|
*/
|
2010-02-03 13:40:27 +01:00
|
|
|
float Point3D::square_distance(const Point3D &source) const
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
|
|
|
return
|
2010-02-03 13:40:27 +01:00
|
|
|
(x - source.x) * (x - source.x) +
|
|
|
|
(y - source.y) * (y - source.y) +
|
|
|
|
(z - source.z) * (z - source.z) ;
|
2009-12-07 16:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
|
|
|
* The distance is not square rooted after the computation, in order
|
|
|
|
* to optimise comparisons.
|
2009-12-07 16:03:53 +01:00
|
|
|
*/
|
2010-03-11 10:58:19 +01:00
|
|
|
float Point3D::square_distance(const float _x,
|
|
|
|
const float _y,
|
|
|
|
const float _z) const
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
|
|
|
return
|
|
|
|
(x - _x) * (x - _x) +
|
|
|
|
(y - _y) * (y - _y) +
|
|
|
|
(z - _z) * (z - _z) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/* *** Operators *** */
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
const Point3D& Point3D::operator=(const Point3D &source)
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-02-03 13:40:27 +01:00
|
|
|
if (this == &source)
|
2009-12-07 16:03:53 +01:00
|
|
|
return *this ;
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
x = source.x ;
|
|
|
|
y = source.y ;
|
|
|
|
z = source.z ;
|
2009-12-07 16:03:53 +01:00
|
|
|
|
|
|
|
return *this ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
bool Point3D::operator==(const Point3D &source) const
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-02-03 13:40:27 +01:00
|
|
|
if (this == &source)
|
2009-12-11 17:56:31 +01:00
|
|
|
return true ;
|
|
|
|
|
2009-12-07 16:03:53 +01:00
|
|
|
return
|
2010-02-03 13:40:27 +01:00
|
|
|
x == source.x &&
|
|
|
|
y == source.y &&
|
|
|
|
z == source.z ;
|
2009-12-07 16:03:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
bool Point3D::operator<(const Point3D &source) const
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
2010-02-03 13:40:27 +01:00
|
|
|
if (x < source.x)
|
2009-12-07 16:03:53 +01:00
|
|
|
return true ;
|
2010-02-03 13:40:27 +01:00
|
|
|
if (x > source.x)
|
2009-12-07 16:03:53 +01:00
|
|
|
return false ;
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
if (y < source.y)
|
2009-12-07 16:03:53 +01:00
|
|
|
return true ;
|
2010-02-03 13:40:27 +01:00
|
|
|
if (y > source.y)
|
2009-12-07 16:03:53 +01:00
|
|
|
return false ;
|
|
|
|
|
2010-02-03 13:40:27 +01:00
|
|
|
if (z < source.z)
|
2009-12-07 16:03:53 +01:00
|
|
|
return true ;
|
|
|
|
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-27 18:47:56 +02:00
|
|
|
Point3D::operator string(void) const
|
2010-02-04 18:46:23 +01:00
|
|
|
{
|
2011-04-27 18:47:56 +02:00
|
|
|
ostringstream oss ;
|
2010-02-04 18:46:23 +01:00
|
|
|
oss << *this ;
|
|
|
|
return oss.str() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-07 16:03:53 +01:00
|
|
|
|
2011-04-27 18:47:56 +02:00
|
|
|
ostream& operator<<(ostream &os, const Point3D &p)
|
2009-12-07 16:03:53 +01:00
|
|
|
{
|
|
|
|
os << "(" << p.x << ";" << p.y << ";" << p.z << ")" ;
|
|
|
|
return os ;
|
|
|
|
}
|
2010-06-07 15:09:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t hash_value(const Point3D &source)
|
|
|
|
{
|
|
|
|
size_t seed = 0 ;
|
|
|
|
|
|
|
|
boost::hash_combine(seed, source.x) ;
|
|
|
|
boost::hash_combine(seed, source.y) ;
|
|
|
|
boost::hash_combine(seed, source.z) ;
|
|
|
|
|
|
|
|
return seed ;
|
|
|
|
}
|