2011-10-18 21:45:55 +11: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/>.
|
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
2011-10-18 21:45:55 +11:00
|
|
|
*/
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include "vector.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
2011-10-18 21:45:55 +11:00
|
|
|
#include "maths.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
using namespace util;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
util::vector
|
|
|
|
util::vector::operator* (double rhs) const {
|
|
|
|
return { rhs * x, rhs * y, rhs * z };
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector&
|
|
|
|
vector::operator*= (double rhs) {
|
|
|
|
x *= rhs;
|
|
|
|
y *= rhs;
|
|
|
|
z *= rhs;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
return *this;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-24 15:17:15 +11:00
|
|
|
util::vector
|
|
|
|
util::vector::operator* (const vector &rhs) const {
|
|
|
|
return { x * rhs.x, y * rhs.y, z * rhs.z };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
util::vector&
|
|
|
|
util::vector::operator*= (const vector &rhs) {
|
|
|
|
x *= rhs.x;
|
|
|
|
y *= rhs.y;
|
|
|
|
z *= rhs.z;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector
|
|
|
|
vector::operator+ (const vector &rhs) const {
|
|
|
|
return { x + rhs.x, y + rhs.y, z + rhs.z };
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-24 15:17:15 +11:00
|
|
|
vector
|
|
|
|
vector::operator- (void) const {
|
|
|
|
return { -x, -y, -z };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector
|
|
|
|
vector::operator- (const vector &rhs) const
|
|
|
|
{ return { x - rhs.x, y - rhs.y, z - rhs.z }; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector&
|
|
|
|
vector::operator-= (const vector &rhs) {
|
|
|
|
x -= rhs.x;
|
|
|
|
y -= rhs.y;
|
|
|
|
z -= rhs.z;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
return *this;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector&
|
|
|
|
vector::operator+= (const vector &rhs) {
|
|
|
|
x += rhs.x;
|
|
|
|
y += rhs.y;
|
|
|
|
z += rhs.z;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
return *this;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector&
|
|
|
|
vector::operator= (const vector &rhs) {
|
|
|
|
x = rhs.x;
|
|
|
|
y = rhs.y;
|
|
|
|
z = rhs.z;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
return *this;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-29 15:07:34 +11:00
|
|
|
bool
|
|
|
|
vector::operator== (const vector &rhs) const {
|
|
|
|
return almost_equal (x, rhs.x) &&
|
|
|
|
almost_equal (y, rhs.y) &&
|
|
|
|
almost_equal (z, rhs.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
double
|
|
|
|
vector::magnitude (void) const {
|
|
|
|
return sqrt (x * x + y * y + z * z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
vector::magnitude2 (void) const {
|
|
|
|
return x * x + y * y + z * z;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
double
|
|
|
|
vector::dot (const vector &rhs) const {
|
|
|
|
return x * rhs.x + y * rhs.y + z * rhs.z;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector
|
|
|
|
vector::cross (const vector &rhs) const {
|
|
|
|
return { y * rhs.z - z * rhs.y,
|
|
|
|
z * rhs.x - x * rhs.z,
|
|
|
|
x * rhs.y - y * rhs.x };
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
vector&
|
|
|
|
vector::normalise (void) {
|
|
|
|
double mag = magnitude ();
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
x /= mag;
|
|
|
|
y /= mag;
|
|
|
|
z /= mag;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
return *this;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
2011-10-18 21:45:55 +11:00
|
|
|
|
|
|
|
|
|
|
|
vector
|
|
|
|
vector::normalised (void) const {
|
|
|
|
double mag = magnitude ();
|
|
|
|
return { x / mag, y / mag, z / mag };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-29 23:14:07 +11:00
|
|
|
vector
|
|
|
|
vector::spherical_to_cartesian (const vector &s) {
|
|
|
|
return {
|
|
|
|
s.x * sin (s.y) * cos (s.z),
|
|
|
|
s.x * sin (s.y) * sin (s.z),
|
|
|
|
s.x * cos (s.y),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector
|
|
|
|
vector::cartesian_to_spherical (const vector &c) {
|
|
|
|
double mag = c.magnitude ();
|
|
|
|
|
|
|
|
return {
|
|
|
|
mag,
|
|
|
|
acos (c.z / mag),
|
|
|
|
atan2 (c.y, c.x)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
void
|
|
|
|
vector::sanity (void) const {
|
|
|
|
check_soft (!std::isnan (x));
|
|
|
|
check_soft (!std::isnan (y));
|
|
|
|
check_soft (!std::isnan (z));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
util::vector
|
2012-04-24 18:12:07 +10:00
|
|
|
util::operator* (double a, const util::vector &b)
|
2011-10-18 21:45:55 +11:00
|
|
|
{ return b * a; }
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream&
|
2012-04-24 18:12:07 +10:00
|
|
|
util::operator<< (std::ostream &os, const util::vector &v) {
|
2011-10-18 21:45:55 +11:00
|
|
|
os << "vec(" << v.x << ", " << v.y << ", " << v.z << ")";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2011-10-29 21:17:48 +11:00
|
|
|
|
|
|
|
const json::node&
|
2012-04-24 18:12:07 +10:00
|
|
|
util::operator>> (const json::node &node, util::vector &v) {
|
2012-04-12 16:08:06 +10:00
|
|
|
v.x = node[0].as_number ();
|
|
|
|
v.y = node[1].as_number ();
|
|
|
|
v.z = node[2].as_number ();
|
2011-10-29 21:17:48 +11:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|