libcruft-util/vector.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

/*
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/.
*
2012-04-23 13:06:41 +10:00
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
2011-05-23 17:18:52 +10:00
#include "vector.hpp"
#include "debug/assert.hpp"
2019-01-03 15:48:34 +11:00
#include "std.hpp"
2011-05-23 17:18:52 +10:00
#include <cmath>
2011-05-23 17:18:52 +10:00
using cruft::vector;
using cruft::vector3f;
using cruft::vector3d;
2011-05-23 17:18:52 +10:00
2015-04-02 14:57:30 +11:00
///////////////////////////////////////////////////////////////////////////////
template <typename T>
vector<2,T>
cruft::polar_to_cartesian (vector<2,T> v)
2015-04-02 14:57:30 +11: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])
};
}
2012-05-26 18:02:11 +10:00
2015-02-19 14:20:01 +11:00
//-----------------------------------------------------------------------------
template <typename T>
vector<2,T>
cruft::cartesian_to_polar (vector<2,T> v)
2015-04-02 14:57:30 +11: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>
cruft::from_euler (vector<2,T> euler)
2015-07-21 02:56:37 +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
}
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>
cruft::to_euler (vector<3,T> vec)
2015-07-21 02:56:37 +10:00
{
CHECK (is_normalised (vec));
2015-07-21 02:56:37 +10:00
return {
std::acos (vec.y),
2015-07-21 02:56:37 +10:00
-std::atan2 (vec.z, vec.x),
};
}
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
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
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); }
);
}
};
2015-02-19 13:27:06 +11: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>>;
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
//-----------------------------------------------------------------------------
template vector<2,float> cruft::polar_to_cartesian (cruft::vector<2,float>);
template vector<2,float> cruft::cartesian_to_polar (cruft::vector<2,float>);