libcruft-util/point.cpp

103 lines
2.0 KiB
C++

/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
#include "point.hpp"
#include "debug.hpp"
#include <cmath>
using namespace std;
using namespace util;
double
point::distance (const point &other) const {
return sqrt (distance2 (other));
}
double
point::distance2 (const point &other) const {
return (x - other.x) * (x - other.x) +
(y - other.y) * (y - other.y) +
(z - other.z) * (z - other.z);
}
double
point::manhattan (const point &other) const {
return fabs (x - other.x) +
fabs (y - other.y) +
fabs (z - other.z);
}
point&
point::operator*= (double f) {
x *= f;
y *= f;
z *= f;
return *this;
}
point
point::operator* (double f) const {
return { x * f, y * f, z * f };
}
point
point::operator+ (const vector &rhs) const {
return { x + rhs.x, y + rhs.y, z + rhs.z };
}
point&
point::operator+= (const vector &rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
util::vector
point::operator- (const point &rhs) const {
return { x - rhs.x, y - rhs.y, z - rhs.z };
}
void
point::sanity (void) const {
CHECK_SOFT (!std::isnan (x));
CHECK_SOFT (!std::isnan (y));
CHECK_SOFT (!std::isnan (z));
}
std::ostream&
operator<< (std::ostream &os, const point &p) {
os << "point(" << p.x << ", " << p.y << ", " << p.z << ")";
return os;
}