2011-10-18 21:45:55 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-10-18 21:45:55 +11:00
|
|
|
*
|
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"
|
2019-01-03 15:48:34 +11:00
|
|
|
#include "std.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
#include <cmath>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::vector;
|
|
|
|
using cruft::vector3f;
|
|
|
|
using cruft::vector3d;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2015-04-02 14:57:30 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename T>
|
2015-04-15 14:06:49 +10:00
|
|
|
vector<2,T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::polar_to_cartesian (vector<2,T> v)
|
2015-04-02 14:57:30 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::vector<2,T> {
|
2015-04-01 17:05:24 +11:00
|
|
|
v[0] * std::cos (v[1]),
|
|
|
|
v[0] * std::sin (v[1])
|
2013-08-06 21:23:56 +10:00
|
|
|
};
|
2013-07-30 23:52:31 +10:00
|
|
|
}
|
2011-10-18 21:45:55 +11:00
|
|
|
|
2012-05-26 18:02:11 +10:00
|
|
|
|
2015-02-19 14:20:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename T>
|
2015-04-15 14:06:49 +10:00
|
|
|
vector<2,T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::cartesian_to_polar (vector<2,T> v)
|
2015-04-02 14:57:30 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::vector<2,T> {
|
2015-04-02 14:57:30 +11:00
|
|
|
std::hypot (v.x, v.y),
|
|
|
|
std::atan2 (v.y, v.x)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 02:56:37 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
vector<3,T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::from_euler (vector<2,T> euler)
|
2015-07-21 02:56:37 +10:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return normalised (cruft::vector<3,T> {
|
2015-07-21 02:56:37 +10:00
|
|
|
std::sin (euler.x) * std::cos (euler.y),
|
|
|
|
std::cos (euler.x),
|
|
|
|
-std::sin (euler.x) * std::sin (euler.y),
|
2016-10-11 20:57:17 +11:00
|
|
|
});
|
2015-07-21 02:56:37 +10:00
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template cruft::vector3f cruft::from_euler (cruft::vector2f);
|
|
|
|
template cruft::vector3d cruft::from_euler (cruft::vector2d);
|
2015-07-21 02:56:37 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
vector<2,T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::to_euler (vector<3,T> vec)
|
2015-07-21 02:56:37 +10:00
|
|
|
{
|
2016-08-11 14:58:46 +10:00
|
|
|
CHECK (is_normalised (vec));
|
2015-07-21 02:56:37 +10:00
|
|
|
|
|
|
|
return {
|
2016-08-11 14:58:46 +10:00
|
|
|
std::acos (vec.y),
|
2015-07-21 02:56:37 +10:00
|
|
|
-std::atan2 (vec.z, vec.x),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
template cruft::vector2f cruft::to_euler (cruft::vector3f);
|
|
|
|
template cruft::vector2d cruft::to_euler (cruft::vector3d);
|
2015-07-21 02:56:37 +10:00
|
|
|
|
|
|
|
|
2016-08-11 16:33:42 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
struct cruft::debug::validator<vector<S,T>> {
|
2018-04-16 15:55:11 +10:00
|
|
|
static bool
|
|
|
|
is_valid (vector<S,T> const& val)
|
|
|
|
{
|
|
|
|
return std::all_of (
|
|
|
|
std::begin (val.data),
|
|
|
|
std::end (val.data),
|
|
|
|
[] (auto v) { return !std::isnan (v); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2012-05-18 17:56:24 +10:00
|
|
|
|
|
|
|
|
2015-02-19 13:27:06 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-04 15:02:06 +10:00
|
|
|
#define INSTANTIATE_S_T(S,T) \
|
2019-01-03 15:48:34 +11:00
|
|
|
template struct cruft::vector<(S),T>; \
|
|
|
|
template struct cruft::debug::validator<vector<(S),T>>;
|
2012-05-22 14:13:34 +10:00
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
MAP_CRUFT_COORD_PARAMS(INSTANTIATE_S_T)
|
2015-04-01 17:05:24 +11:00
|
|
|
|
2012-05-26 18:02:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
template vector<2,float> cruft::polar_to_cartesian (cruft::vector<2,float>);
|
|
|
|
template vector<2,float> cruft::cartesian_to_polar (cruft::vector<2,float>);
|