/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2011-2017 Danny Robson */ #ifndef CRUFT_UTIL_VECTOR_HPP #define CRUFT_UTIL_VECTOR_HPP #include "coord/fwd.hpp" #include "coord/ops.hpp" #include "coord.hpp" #include "debug.hpp" #include "maths.hpp" #include #include /////////////////////////////////////////////////////////////////////////////// namespace util { template struct vector : public coord::base> { using coord::base>::base; // use a forwarding assignment operator so that we can let the base // take care of the many different types of parameters. otherwise we // have to deal with scalar, vector, initializer_list, ad nauseum. template vector& operator= (Arg&&arg) { coord::base>::operator=(std::forward (arg)); return *this; } // representations vector homog (void) const { return (*this).template redim (0.f); } // constants static constexpr vector ones (void) { return vector {1}; } static constexpr vector zeros (void) { return vector {0}; } }; template constexpr vector<3,T> cross (vector<3,T> a, vector<3,T> b) { return { 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 constexpr T cross (vector<2,T> a, vector<2,T> b) { return a[0] * b[1] - a[1] * b[0]; } //------------------------------------------------------------------------- // given a vector find two vectors which produce an orthonormal basis. // template std::pair< util::vector<3,T>, util::vector<3,T> > make_basis (const util::vector<3,T> n) { #if 0 // frisvad's method avoids explicit normalisation. a good alternative // is hughes-moeller, but the paper is hard to find. CHECK (is_normalised (n)); // avoid a singularity if (n.z < -T(0.9999999)) { return { { 0, -1, 0 }, { -1, -1, 0 } }; } const T a = 1 / (1 + n.z); const T b = -n.x * n.y * a; const util::vector<3,T> v0 { 1 - n.x * n.x * a, b, -n.x }; const util::vector<3,T> v1 { b, 1 - n.y * n.y * a, -n.y }; CHECK (is_normalised (v0)); CHECK (is_normalised (v1)); return { v0, v1 }; #else // huges-moeller isn't as fast, but is more accurate if(util::abs (n.x) > util::abs (n.z)) { // Normalization factor for b2 auto const a = rsqrt (n.x * n.x + n.y * n.y); util::vector<3,T> b1 { -n.y * a, n.x * a, 0 }; // Cross product using that b2 has a zero component util::vector<3,T> b0 { b1.y * n.z, -b1.x * n.z, b1.x * n.y - b1.y * n.x }; return { b0, b1 }; } else { // Normalization factor for b2 auto const a = rsqrt (n.y * n.y + n.z * n.z); util::vector<3,T> b1 { 0.0f, -n.z * a, n.y * a }; // Cross product using that b2 has a zero component util::vector<3,T> b0 { b1.y * n.z - b1.z * n.y, b1.z * n.x, -b1.y * n.x }; return { b0, b1 }; } #endif } // polar/cartesian conversions; assumes (mag, angle) form. template vector<2,T> polar_to_cartesian (vector<2,T>); template vector<2,T> cartesian_to_polar (vector<2,T>); // convert vector in spherical coordinates (r,theta,phi) with theta // inclination and phi azimuth to cartesian coordinates (x,y,z) template constexpr vector<3,T> spherical_to_cartesian (const vector<3,T> s) { return { s.x * std::sin (s.y) * std::cos (s.z), s.x * std::sin (s.y) * std::sin (s.z), s.x * std::cos (s.y) }; } // convert vector in cartesian coordinates (x,y,z) to spherical // coordinates (using ISO convention: r,inclination,azimuth) with theta // inclination and phi azimuth. template constexpr vector<3,T> cartesian_to_spherical (vector<3,T> c) { auto r = norm (c); return { r, std::acos (c.z / r), std::atan2 (c.y, c.x) }; } template constexpr vector<3,T> canonical_spherical (vector<3,T> s) { if (s.x < 0) { s.x = -s.x; s.y += util::pi; } if (s.y < 0) { s.y = -s.y; s.z += util::pi; } s.y = std::fmod (s.y, util::pi); s.z = std::fmod (s.z, util::pi); return s; } template vector<2,T> to_euler (vector<3,T>); template vector<3,T> from_euler (vector<2,T>); template using vector1 = vector<1,T>; template using vector2 = vector<2,T>; template using vector3 = vector<3,T>; template using vector4 = vector<4,T>; template using vectoru = vector; template using vectori = vector; template using vectorf = vector; template using vectorb = vector; using vector2u = vector2; using vector3u = vector3; using vector4u = vector4; using vector2i = vector2; using vector3i = vector3; using vector4i = vector4; using vector1f = vector1; using vector2f = vector2; using vector3f = vector3; using vector4f = vector4; using vector2d = vector2; using vector3d = vector3; using vector4d = vector4; using vector2b = vector2; using vector3b = vector3; using vector4b = vector4; } #endif