point.cc
来自「tools.03b.zip」· CC 代码 · 共 257 行
CC
257 行
// Tools Library//// Copyright (C) 2004 Navel Ltd.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//// Contact information:// Mailing address:// Marios Hadjieleftheriou// University of California, Riverside// Department of Computer Science// Surge Building, Room 310// Riverside, CA 92521//// Email:// marioh@cs.ucr.edu#include <Tools.h>Tools::Geometry::Point::Point() : m_dimension(0), m_pCoords(0){}Tools::Geometry::Point::Point(const double* pCoords, unsigned long dimension) : m_dimension(dimension){ // no need to initialize m_pCoords to 0 since if a bad_alloc is raised the destructor will not be called. m_pCoords = new double[m_dimension]; memcpy(m_pCoords, pCoords, m_dimension * sizeof(double));}Tools::Geometry::Point::Point(const Point& p) : m_dimension(p.m_dimension){ // no need to initialize m_pCoords to 0 since if a bad_alloc is raised the destructor will not be called. m_pCoords = new double[m_dimension]; memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double));}Tools::Geometry::Point::~Point(){ delete[] m_pCoords;}Tools::Geometry::Point& Tools::Geometry::Point::operator=(const Point& p){ if (this != &p) { makeDimension(p.m_dimension); memcpy(m_pCoords, p.m_pCoords, m_dimension * sizeof(double)); } return *this;}bool Tools::Geometry::Point::operator==(const Point& p) const{ if (m_dimension != p.m_dimension) throw IllegalArgumentException("Point::operator ==: Points have different number of dimensions."); for (unsigned long i = 0; i < m_dimension; i++) { if (
m_pCoords[i] < p.m_pCoords[i] - std::numeric_limits<double>::epsilon() || m_pCoords[i] > p.m_pCoords[i] + std::numeric_limits<double>::epsilon()) return false; } return true;}//// IObject interface//Tools::Geometry::Point* Tools::Geometry::Point::clone() throw (Tools::NotSupportedException){ return new Point(*this);}//// ISerializable interface//unsigned long Tools::Geometry::Point::getByteArraySize(){ return (sizeof(unsigned long) + m_dimension * sizeof(double));}unsigned long Tools::Geometry::Point::loadFromByteArray(byte* const data){ byte* ptr = data; unsigned long dimension;
memcpy(&dimension, ptr, sizeof(unsigned long));
ptr += sizeof(unsigned long); makeDimension(dimension); memcpy(m_pCoords, ptr, m_dimension * sizeof(double)); //ptr += m_dimension * sizeof(double); return getByteArraySize();}void Tools::Geometry::Point::storeToByteArray(unsigned long& len, byte** data){ len = getByteArraySize(); *data = new byte[len]; byte* ptr = *data; memcpy(ptr, &m_dimension, sizeof(unsigned long)); ptr += sizeof(unsigned long); memcpy(ptr, m_pCoords, m_dimension * sizeof(double)); //ptr += m_dimension * sizeof(double);}//// IShape interface//bool Tools::Geometry::Point::intersectsShape(const IShape& s) const{ const Region* pr = dynamic_cast<const Region*>(&s); if (pr != 0) { return pr->containsPoint(*this); } throw IllegalStateException("intersectsShape: Not implemented yet!");}bool Tools::Geometry::Point::containsShape(const IShape& s) const{ return false;}bool Tools::Geometry::Point::touchesShape(const IShape& s) const{ const Point* ppt = dynamic_cast<const Point*>(&s); if (ppt != 0) { if (*this == *ppt) return true; return false; } const Region* pr = dynamic_cast<const Region*>(&s); if (pr != 0) { return pr->touchesPoint(*this); } throw IllegalStateException("touchesShape: Not implemented yet!");}void Tools::Geometry::Point::getCenter(Point& out) const
{
out = *this;
}
unsigned long Tools::Geometry::Point::getDimension() const
{
return m_dimension;
}
void Tools::Geometry::Point::getMBR(Region& out) const{ out = Region(m_pCoords, m_pCoords, m_dimension);}double Tools::Geometry::Point::getArea() const{ return 0.0;}double Tools::Geometry::Point::getMinimumDistance(const IShape& s) const{ const Point* ppt = dynamic_cast<const Point*>(&s); if (ppt != 0) { return getMinimumDistance(*ppt); } const Region* pr = dynamic_cast<const Region*>(&s); if (pr != 0) { return pr->getMinimumDistance(*this); } throw IllegalStateException("getMinimumDistance: Not implemented yet!");}double Tools::Geometry::Point::getMinimumDistance(const Point& p) const{ if (m_dimension != p.m_dimension) throw IllegalArgumentException("Point::getMinimumDistance: Shapes have different number of dimensions."); double ret = 0.0; for (unsigned long i = 0; i < m_dimension; i++) { ret += std::pow(m_pCoords[i] - p.m_pCoords[i], 2.0); } return std::sqrt(ret);}double Tools::Geometry::Point::getCoordinate(unsigned long index) const throw (IndexOutOfBoundsException){
if (index < 0 || index >= m_dimension) throw IndexOutOfBoundsException(index);
return m_pCoords[index];}void Tools::Geometry::Point::makeInfinite(unsigned long dimension){ makeDimension(dimension); for (unsigned long cIndex = 0; cIndex < m_dimension; cIndex++) { m_pCoords[cIndex] = std::numeric_limits<double>::max(); }}void Tools::Geometry::Point::makeDimension(unsigned long dimension){ if (m_dimension != dimension) { delete[] m_pCoords; // remember that this is not a constructor. The object will be destructed normally if // something goes wrong (bad_alloc), so we must take care not to leave the object at an intermediate state. m_pCoords = 0; m_dimension = dimension; m_pCoords = new double[m_dimension]; }}std::ostream& Tools::Geometry::operator<<(std::ostream& os, const Point& pt){ unsigned long i; for (i = 0; i < pt.m_dimension; i++) { os << pt.m_pCoords[i] << " "; } return os;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?