/* * 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 . * * Copyright 2011 Danny Robson */ #include "vector.hpp" #include "debug.hpp" #include "maths.hpp" #include "random.hpp" #include #include #include #include //----------------------------------------------------------------------------- using namespace util; //----------------------------------------------------------------------------- template T util::vector::magnitude (void) const { // TODO: this should not truncate for integral types return static_cast (std::sqrt (magnitude2 ())); } //----------------------------------------------------------------------------- template T util::vector::magnitude2 (void) const { T total { 0 }; for (size_t i = 0; i < S; ++i) total += pow2 (this->data[i]); return total; } //----------------------------------------------------------------------------- template T util::vector::difference (const util::vector &rhs) const { // TODO: change the signature to ensure it does not truncate return static_cast (std::sqrt (difference2 (rhs))); } //----------------------------------------------------------------------------- template T util::vector::difference2 (const util::vector &rhs) const { T sum {0}; for (size_t i = 0; i < S; ++i) sum += pow2 (this->data[i] - rhs.data[i]); return sum; } //----------------------------------------------------------------------------- template util::vector& util::vector::normalise (void) { T mag = magnitude (); for (size_t i = 0; i < S; ++i) this->data[i] /= mag; return *this; } //----------------------------------------------------------------------------- template util::vector util::vector::normalised (void) const { T mag = magnitude (); util::vector out; for (size_t i = 0; i < S; ++i) out.data[i] = this->data[i] / mag; return out; } //----------------------------------------------------------------------------- template util::vector<2,T> util::polar_to_cartesian (const util::vector<2,T> &v) { return util::vector<2,T> { v.r * std::cos (v.t), v.r * std::sin (v.t) }; } //----------------------------------------------------------------------------- template util::vector<3,T> util::cross (const util::vector<3,T> &a, const util::vector<3,T> &b) { return util::vector<3,T> { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x }; } template util::vector3f util::cross(const util::vector3f&, const util::vector3f&); template util::vector3d util::cross(const util::vector3d&, const util::vector3d&); //----------------------------------------------------------------------------- template util::vector<3,T> util::spherical_to_cartesian (const util::vector<3,T> &s) { return util::vector<3,T> { s.x * sin (s.y) * cos (s.z), s.x * sin (s.y) * sin (s.z), s.x * cos (s.y), }; } //----------------------------------------------------------------------------- template util::vector<3,T> util::cartesian_to_spherical (const util::vector<3,T> &c) { T mag = c.magnitude (); return util::vector<3,T> { mag, acos (c.z / mag), atan2 (c.y, c.x) }; } //----------------------------------------------------------------------------- template bool util::vector::is_zero (void) const { return std::all_of (std::begin (this->data), std::end (this->data), [] (T i) { return almost_zero (i); }); } //----------------------------------------------------------------------------- template const util::vector util::vector::ZERO (T{0}); //----------------------------------------------------------------------------- template void util::vector::sanity (void) const { CHECK (std::all_of (std::begin (this->data), std::end (this->data), [] (T i) { return !std::isnan (i); })); } /////////////////////////////////////////////////////////////////////////////// // ostream template std::ostream& util::operator<< (std::ostream &os, const util::vector &v) { os << "vec" << S << "(" << v.data[0]; for (size_t i = 1; i < S; ++i) os << ", " << v.data[i]; os << ")"; return os; } //----------------------------------------------------------------------------- template const json::tree::node& util::operator>> (const json::tree::node &node, util::vector &v) { const json::tree::array &array = node.as_array (); if (array.size () != S) throw std::runtime_error ("Invalid dimensionality for json-to-vector"); // XXX: This used to be a std::transform but gcc 4.9.0 hit an internal // compiler error at this point in release mode, so we dumb it down a // little. for (size_t i = 0; i < array.size (); ++i) v.data[i] = static_cast (array[i].as_number ().native ()); return node; } //----------------------------------------------------------------------------- #define INSTANTIATE_S_T(S,T) \ template struct util::vector; \ template std::ostream& util::operator<< (std::ostream&, const util::vector &v);\ template const json::tree::node& util::operator>> (const json::tree::node&, util::vector&); #define INSTANTIATE(T) \ INSTANTIATE_S_T(1,T) \ INSTANTIATE_S_T(2,T) \ INSTANTIATE_S_T(3,T) \ INSTANTIATE_S_T(4,T) INSTANTIATE(uint32_t) INSTANTIATE(int32_t) INSTANTIATE(uint64_t) INSTANTIATE(int64_t) INSTANTIATE(float) INSTANTIATE(double) //----------------------------------------------------------------------------- namespace util { template <> vector<1,float> random (void) { util::vector<1,float> out; randomise (out.data); return out; } template <> vector<2,float> random (void) { util::vector<2,float> out; randomise (out.data); return out; } template <> vector<3,float> random (void) { util::vector<3,float> out; randomise (out.data); return out; } template <> vector<4,float> random (void) { util::vector<4,float> out; randomise (out.data); return out; } template <> vector<1,double> random (void) { util::vector<1,double> out; randomise (out.data); return out; } template <> vector<2,double> random (void) { util::vector<2,double> out; randomise (out.data); return out; } template <> vector<3,double> random (void) { util::vector<3,double> out; randomise (out.data); return out; } template <> vector<4,double> random (void) { util::vector<4,double> out; randomise (out.data); return out; } }