3
// Copyright (C) 2004 Navel Ltd.
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
Tools::Geometry::Point::Point()
27
: m_dimension( 0 ), m_pCoords( 0 )
31
Tools::Geometry::Point::Point( const double* pCoords, unsigned long dimension )
32
: m_dimension( dimension )
34
// no need to initialize m_pCoords to 0 since if a bad_alloc is raised the destructor will not be called.
36
m_pCoords = new double[m_dimension];
37
memcpy( m_pCoords, pCoords, m_dimension * sizeof( double ) );
40
Tools::Geometry::Point::Point( const Point& p )
41
: m_dimension( p.m_dimension )
43
// no need to initialize m_pCoords to 0 since if a bad_alloc is raised the destructor will not be called.
45
m_pCoords = new double[m_dimension];
46
memcpy( m_pCoords, p.m_pCoords, m_dimension * sizeof( double ) );
49
Tools::Geometry::Point::~Point()
54
Tools::Geometry::Point& Tools::Geometry::Point::operator=( const Point & p )
58
makeDimension( p.m_dimension );
59
memcpy( m_pCoords, p.m_pCoords, m_dimension * sizeof( double ) );
65
bool Tools::Geometry::Point::operator==( const Point& p ) const
67
if ( m_dimension != p.m_dimension )
68
throw IllegalArgumentException(
69
"Tools::Geometry::Point::operator==: Points have different number of dimensions."
72
for ( unsigned long i = 0; i < m_dimension; i++ )
75
m_pCoords[i] < p.m_pCoords[i] - std::numeric_limits<double>::epsilon() ||
76
m_pCoords[i] > p.m_pCoords[i] + std::numeric_limits<double>::epsilon() ) return false;
85
Tools::Geometry::Point* Tools::Geometry::Point::clone()
87
return new Point( *this );
91
// ISerializable interface
93
unsigned long Tools::Geometry::Point::getByteArraySize()
95
return ( sizeof( unsigned long ) + m_dimension * sizeof( double ) );
98
void Tools::Geometry::Point::loadFromByteArray( const byte* ptr )
100
unsigned long dimension;
101
memcpy( &dimension, ptr, sizeof( unsigned long ) );
102
ptr += sizeof( unsigned long );
104
makeDimension( dimension );
105
memcpy( m_pCoords, ptr, m_dimension * sizeof( double ) );
106
//ptr += m_dimension * sizeof(double);
109
void Tools::Geometry::Point::storeToByteArray( byte** data, unsigned long& len )
111
len = getByteArraySize();
112
*data = new byte[len];
115
memcpy( ptr, &m_dimension, sizeof( unsigned long ) );
116
ptr += sizeof( unsigned long );
117
memcpy( ptr, m_pCoords, m_dimension * sizeof( double ) );
118
//ptr += m_dimension * sizeof(double);
124
bool Tools::Geometry::Point::intersectsShape( const IShape& s ) const
126
const Region* pr = dynamic_cast<const Region*>( &s );
129
return pr->containsPoint( *this );
132
throw IllegalStateException(
133
"Tools::Geometry::Point::intersectsShape: Not implemented yet!"
137
bool Tools::Geometry::Point::containsShape( const IShape& s ) const
142
bool Tools::Geometry::Point::touchesShape( const IShape& s ) const
144
const Point* ppt = dynamic_cast<const Point*>( &s );
147
if ( *this == *ppt ) return true;
151
const Region* pr = dynamic_cast<const Region*>( &s );
154
return pr->touchesPoint( *this );
157
throw IllegalStateException(
158
"Tools::Geometry::Point::touchesShape: Not implemented yet!"
162
void Tools::Geometry::Point::getCenter( Point& out ) const
167
unsigned long Tools::Geometry::Point::getDimension() const
172
void Tools::Geometry::Point::getMBR( Region& out ) const
174
out = Region( m_pCoords, m_pCoords, m_dimension );
177
double Tools::Geometry::Point::getArea() const
182
double Tools::Geometry::Point::getMinimumDistance( const IShape& s ) const
184
const Point* ppt = dynamic_cast<const Point*>( &s );
187
return getMinimumDistance( *ppt );
190
const Region* pr = dynamic_cast<const Region*>( &s );
193
return pr->getMinimumDistance( *this );
196
throw IllegalStateException(
197
"Tools::Geometry::Point::getMinimumDistance: Not implemented yet!"
201
double Tools::Geometry::Point::getMinimumDistance( const Point& p ) const
203
if ( m_dimension != p.m_dimension )
204
throw IllegalArgumentException(
205
"Tools::Geometry::Point::getMinimumDistance: Shapes have different number of dimensions."
210
for ( unsigned long cDim = 0; cDim < m_dimension; cDim++ )
212
ret += std::pow( m_pCoords[cDim] - p.m_pCoords[cDim], 2.0 );
215
return std::sqrt( ret );
218
double Tools::Geometry::Point::getCoordinate( unsigned long index ) const
220
if ( index < 0 || index >= m_dimension )
221
throw IndexOutOfBoundsException( index );
223
return m_pCoords[index];
226
void Tools::Geometry::Point::makeInfinite( unsigned long dimension )
228
makeDimension( dimension );
229
for ( unsigned long cIndex = 0; cIndex < m_dimension; cIndex++ )
231
m_pCoords[cIndex] = std::numeric_limits<double>::max();
235
void Tools::Geometry::Point::makeDimension( unsigned long dimension )
237
if ( m_dimension != dimension )
241
// remember that this is not a constructor. The object will be destructed normally if
242
// something goes wrong (bad_alloc), so we must take care not to leave the object at an intermediate state.
245
m_dimension = dimension;
246
m_pCoords = new double[m_dimension];
250
std::ostream& Tools::Geometry::operator<<( std::ostream& os, const Point& pt )
252
for ( unsigned long cDim = 0; cDim < pt.m_dimension; cDim++ )
254
os << pt.m_pCoords[cDim] << " ";