2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-18 22:14:31 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2017-08-27 12:32:00 +10:00
|
|
|
* Copyright 2011-2017 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
2017-08-27 12:32:00 +10:00
|
|
|
#ifndef CRUFT_UTIL_VECTOR_HPP
|
|
|
|
#define CRUFT_UTIL_VECTOR_HPP
|
|
|
|
|
2018-04-18 21:46:26 +10:00
|
|
|
#include "coord/fwd.hpp"
|
|
|
|
#include "coord/ops.hpp"
|
|
|
|
#include "coord.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2018-04-18 21:46:26 +10:00
|
|
|
#include "debug.hpp"
|
2016-10-11 23:47:57 +11:00
|
|
|
#include "json/fwd.hpp"
|
2018-04-18 21:46:26 +10:00
|
|
|
#include "maths.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2017-08-27 12:32:00 +10:00
|
|
|
#include <cstddef>
|
2017-11-02 18:11:16 +11:00
|
|
|
#include <cmath>
|
2017-08-27 12:32:00 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-10-18 21:45:55 +11:00
|
|
|
namespace util {
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
2017-11-22 17:03:00 +11:00
|
|
|
struct vector : public coord::base<S,T,vector<S,T>>
|
2015-04-09 17:47:35 +10:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
using coord::base<S,T,vector<S,T>>::base;
|
2018-04-11 19:28:03 +10:00
|
|
|
|
|
|
|
// 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 <typename Arg>
|
|
|
|
vector&
|
|
|
|
operator= (Arg&&arg)
|
|
|
|
{
|
|
|
|
coord::base<S,T,vector<S,T>>::operator=(std::forward<Arg> (arg));
|
|
|
|
return *this;
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-07-21 01:40:53 +10:00
|
|
|
// representations
|
2018-04-17 14:26:23 +10:00
|
|
|
vector<S+1,T>
|
|
|
|
homog (void) const
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2018-04-17 14:26:23 +10:00
|
|
|
return (*this).template redim<S+1> (0.f);
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-07-21 01:40:53 +10:00
|
|
|
|
2015-01-28 14:59:33 +11:00
|
|
|
// constants
|
2018-02-28 11:49:13 +11:00
|
|
|
static constexpr vector<S,T> ones (void) { return vector<S,T> {1}; }
|
|
|
|
static constexpr vector<S,T> zeros (void) { return vector<S,T> {0}; }
|
2011-05-23 17:18:52 +10:00
|
|
|
};
|
2012-04-24 18:12:07 +10:00
|
|
|
|
2016-10-17 16:49:26 +11:00
|
|
|
template <typename T>
|
2018-02-28 11:49:13 +11:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
2016-10-17 16:49:26 +11:00
|
|
|
|
2016-12-21 20:22:37 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
T
|
2018-02-28 11:49:13 +11:00
|
|
|
cross (vector<2,T> a, vector<2,T> b)
|
|
|
|
{
|
|
|
|
return a[0] * b[1] - a[1] * b[0];
|
|
|
|
}
|
2016-12-21 20:22:37 +11:00
|
|
|
|
2018-04-12 12:59:45 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// given a vector find two vectors which produce an orthonormal basis.
|
|
|
|
//
|
|
|
|
template <typename T>
|
|
|
|
std::pair<
|
|
|
|
util::vector<3,T>,
|
|
|
|
util::vector<3,T>
|
|
|
|
>
|
|
|
|
make_basis (const util::vector<3,T> n)
|
|
|
|
{
|
2018-04-18 21:46:26 +10:00
|
|
|
#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));
|
2018-04-12 12:59:45 +10:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2018-04-18 21:46:26 +10:00
|
|
|
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
|
2018-04-12 12:59:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-08 19:00:46 +10:00
|
|
|
// polar/cartesian conversions; assumes (mag, angle) form.
|
2015-04-01 17:05:24 +11:00
|
|
|
template <typename T> vector<2,T> polar_to_cartesian (vector<2,T>);
|
2015-04-02 14:57:30 +11:00
|
|
|
template <typename T> vector<2,T> cartesian_to_polar (vector<2,T>);
|
2014-12-15 20:10:56 +11:00
|
|
|
|
2017-11-02 18:11:16 +11:00
|
|
|
// convert vector in spherical coordinates (r,theta,phi) with theta
|
|
|
|
// inclination and phi azimuth to cartesian coordinates (x,y,z)
|
|
|
|
template <typename T>
|
|
|
|
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
|
2018-04-05 19:22:38 +10:00
|
|
|
// coordinates (using ISO convention: r,inclination,azimuth) with theta
|
|
|
|
// inclination and phi azimuth.
|
2017-11-02 18:11:16 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr vector<3,T>
|
|
|
|
cartesian_to_spherical (vector<3,T> c)
|
|
|
|
{
|
|
|
|
auto r = norm (c);
|
|
|
|
return {
|
|
|
|
r,
|
|
|
|
std::acos (c.z / r),
|
2018-01-17 17:44:45 +11:00
|
|
|
std::atan2 (c.y, c.x)
|
2017-11-02 18:11:16 +11:00
|
|
|
};
|
|
|
|
}
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2018-01-17 17:44:59 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr vector<3,T>
|
|
|
|
canonical_spherical (vector<3,T> s)
|
|
|
|
{
|
|
|
|
if (s.x < 0) {
|
|
|
|
s.x = -s.x;
|
2018-03-12 23:06:15 +11:00
|
|
|
s.y += util::pi<T>;
|
2018-01-17 17:44:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.y < 0) {
|
|
|
|
s.y = -s.y;
|
2018-03-12 23:06:15 +11:00
|
|
|
s.z += util::pi<T>;
|
2018-01-17 17:44:59 +11:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:06:15 +11:00
|
|
|
s.y = std::fmod (s.y, util::pi<T>);
|
|
|
|
s.z = std::fmod (s.z, util::pi<T>);
|
2018-01-17 17:44:59 +11:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:56:37 +10:00
|
|
|
template <typename T> vector<2,T> to_euler (vector<3,T>);
|
|
|
|
template <typename T> vector<3,T> from_euler (vector<2,T>);
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// output and serialisation operators
|
|
|
|
template <size_t S, typename T>
|
2016-08-11 16:33:42 +10:00
|
|
|
const json::tree::node&
|
|
|
|
operator>> (const json::tree::node&, vector<S,T>&);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
template <typename T> using vector1 = vector<1,T>;
|
2015-07-13 16:29:15 +10:00
|
|
|
template <typename T> using vector2 = vector<2,T>;
|
|
|
|
template <typename T> using vector3 = vector<3,T>;
|
|
|
|
template <typename T> using vector4 = vector<4,T>;
|
|
|
|
|
2016-10-25 17:46:36 +11:00
|
|
|
template <size_t S> using vectoru = vector<S,unsigned>;
|
|
|
|
template <size_t S> using vectori = vector<S,int>;
|
2015-10-06 15:19:29 +11:00
|
|
|
template <size_t S> using vectorf = vector<S,float>;
|
2017-08-27 12:28:00 +10:00
|
|
|
template <std::size_t S> using vectorb = vector<S,bool>;
|
2015-10-06 15:19:29 +11:00
|
|
|
|
2017-08-27 12:28:00 +10:00
|
|
|
using vector2u = vector2<unsigned>;
|
|
|
|
using vector3u = vector3<unsigned>;
|
|
|
|
using vector4u = vector4<unsigned>;
|
2015-07-22 02:54:52 +10:00
|
|
|
|
2017-08-27 12:28:00 +10:00
|
|
|
using vector2i = vector2<int>;
|
|
|
|
using vector3i = vector3<int>;
|
|
|
|
using vector4i = vector4<int>;
|
2015-01-16 14:44:26 +11:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
using vector1f = vector1<float>;
|
2017-08-27 12:28:00 +10:00
|
|
|
using vector2f = vector2<float>;
|
|
|
|
using vector3f = vector3<float>;
|
|
|
|
using vector4f = vector4<float>;
|
2012-04-24 18:12:07 +10:00
|
|
|
|
2017-08-27 12:28:00 +10:00
|
|
|
using vector2d = vector2<double>;
|
|
|
|
using vector3d = vector3<double>;
|
|
|
|
using vector4d = vector4<double>;
|
|
|
|
|
|
|
|
using vector2b = vector2<bool>;
|
|
|
|
using vector3b = vector3<bool>;
|
|
|
|
using vector4b = vector4<bool>;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-10-18 21:45:55 +11:00
|
|
|
|