libcruft-util/point.cpp

187 lines
4.5 KiB
C++
Raw Normal View History

2011-06-23 22:04:51 +10:00
/*
* 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"
2011-06-23 22:04:51 +10:00
#include <cmath>
using namespace std;
2012-05-26 18:02:11 +10:00
#pragma GCC optimize("-O3")
2011-06-23 22:04:51 +10:00
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
util::point<S>::point ()
{ ; }
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
2011-06-23 22:04:51 +10:00
double
util::point<S>::distance (const util::point<S> &other) const {
2012-05-03 18:12:12 +10:00
return sqrt (distance2 (other));
}
template <size_t S>
2012-05-03 18:12:12 +10:00
double
util::point<S>::distance2 (const util::point<S> &other) const {
double total = 0.0;
for (size_t i = 0; i < S; ++i)
total += pow2 (this->data[i] - other.data[i]);
return total;
2011-06-23 22:04:51 +10:00
}
template <size_t S>
2011-06-23 22:04:51 +10:00
double
util::point<S>::manhattan (const util::point<S> &other) const {
double total = 0.0;
for (size_t i = 0; i < S; ++i)
total += fabs (this->data[i] - other.data[i]);
return total;
2011-06-23 22:04:51 +10:00
}
2011-09-07 21:51:13 +10:00
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
util::point<S>&
util::point<S>::operator*= (double f) {
for (double &i: this->data)
i *= f;
return *this;
}
template <size_t S>
util::point<S>
util::point<S>::operator* (double f) const {
util::point<S> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = this->data[i] * f;
return out;
}
template <size_t S>
util::point<S>
util::point<S>::operator+ (const util::vector<S> &rhs) const {
util::point<S> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = this->data[i] + rhs.data[i];
return out;
}
2011-09-07 21:51:13 +10:00
template <size_t S>
util::point<S>&
util::point<S>::operator+= (const util::vector<S> &rhs) {
for (size_t i = 0; i < S; ++i)
this->data[i] += rhs.data[i];
2011-09-07 21:51:13 +10:00
return *this;
}
2011-09-13 15:12:30 +10:00
template <size_t S>
util::point<S>
util::point<S>::operator- (const util::point<S> &rhs) const {
util::point<S> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = this->data[i] - rhs.data[i];
return out;
}
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
util::vector<S>
util::point<S>::to (const util::point<S> &rhs) const {
util::vector<S> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = rhs.data[i] - this->data[i];
return out;
}
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
void
util::point<S>::sanity (void) const {
CHECK_SOFT (std::all_of (begin (this->data),
end (this->data),
[] (double i) { return !std::isnan (i); }));
}
2011-09-13 16:49:10 +10:00
2012-05-26 18:02:11 +10:00
//-----------------------------------------------------------------------------
template <size_t S>
util::point<S>
util::operator* (const vector<S> &v, const point<S> &p) {
point<S> out;
for (size_t i = 0; i < S; ++i)
out.data[i] = p.data[i] + v.data[i];
return out;
}
template util::point<1> util::operator* (const vector<1>&, const point<1>&);
template util::point<2> util::operator* (const vector<2>&, const point<2>&);
template util::point<3> util::operator* (const vector<3>&, const point<3>&);
template <size_t S>
util::point<S>
util::operator* (const point<S> &p, const vector<S> &v)
{ return v * p; }
template util::point<1> util::operator* (const point<1>&, const vector<1>&);
template util::point<2> util::operator* (const point<2>&, const vector<2>&);
template util::point<3> util::operator* (const point<3>&, const vector<3>&);
//-----------------------------------------------------------------------------
template <size_t S>
2011-09-13 15:12:30 +10:00
std::ostream&
util::operator<< (std::ostream &os, const util::point<S> &p) {
os << "point" << S << "(";
os << p.data[0];
for (size_t i = 1; i < S; ++i)
os << ", " << p.data[i];
os << ")";
2011-09-13 15:12:30 +10:00
return os;
}
template struct util::point<1>;
template struct util::point<2>;
template struct util::point<3>;