2011-10-18 21:45:55 +11: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-10-18 21:45:55 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-30 20:20:19 +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-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"
|
|
|
|
|
2016-10-11 23:47:57 +11:00
|
|
|
#include "json/tree.hpp"
|
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
#include <cmath>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-04-15 13:48:07 +10:00
|
|
|
using util::vector;
|
2015-04-15 14:06:49 +10:00
|
|
|
using util::vector3f;
|
|
|
|
using util::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>
|
|
|
|
util::polar_to_cartesian (vector<2,T> v)
|
2015-04-02 14:57:30 +11:00
|
|
|
{
|
2014-12-15 20:10:56 +11:00
|
|
|
return util::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>
|
|
|
|
util::cartesian_to_polar (vector<2,T> v)
|
2015-04-02 14:57:30 +11:00
|
|
|
{
|
|
|
|
return util::vector<2,T> {
|
|
|
|
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>
|
|
|
|
util::from_euler (vector<2,T> euler)
|
|
|
|
{
|
2016-10-11 20:57:17 +11:00
|
|
|
return normalised (util::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 util::vector3f util::from_euler (util::vector2f);
|
|
|
|
template util::vector3d util::from_euler (util::vector2d);
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
vector<2,T>
|
|
|
|
util::to_euler (vector<3,T> vec)
|
|
|
|
{
|
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),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template util::vector2f util::to_euler (util::vector3f);
|
|
|
|
template util::vector2d util::to_euler (util::vector3d);
|
|
|
|
|
|
|
|
|
2016-08-11 16:33:42 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
2018-04-16 15:55:11 +10:00
|
|
|
struct util::debug::validator<vector<S,T>> {
|
|
|
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t S, typename T>
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::node&
|
2015-04-15 13:48:07 +10:00
|
|
|
util::operator>> (const json::tree::node &node, util::vector<S,T> &v)
|
|
|
|
{
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::array &array = node.as_array ();
|
2014-08-18 22:14:31 +10:00
|
|
|
if (array.size () != S)
|
2012-05-18 17:56:24 +10:00
|
|
|
throw std::runtime_error ("Invalid dimensionality for json-to-vector");
|
|
|
|
|
2014-08-01 21:39:13 +10:00
|
|
|
// 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)
|
2016-05-12 17:39:33 +10:00
|
|
|
v.data[i] = array[i].as<T> ();
|
2014-08-01 21:39:13 +10:00
|
|
|
|
2012-05-18 17:56:24 +10:00
|
|
|
return node;
|
2011-10-29 21:17:48 +11:00
|
|
|
}
|
2012-05-18 17:56:24 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:37:45 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-04-15 14:06:49 +10:00
|
|
|
#define INSTANTIATE_S_T(S,T) \
|
|
|
|
template struct util::vector<S,T>; \
|
2018-04-16 15:55:11 +10:00
|
|
|
template struct util::debug::validator<vector<S,T>>; \
|
2015-02-02 21:22:38 +11:00
|
|
|
template const json::tree::node& util::operator>> (const json::tree::node&, util::vector<S,T>&);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
|
|
|
|
2015-01-21 23:37:45 +11:00
|
|
|
#define INSTANTIATE(T) \
|
|
|
|
INSTANTIATE_S_T(1,T) \
|
|
|
|
INSTANTIATE_S_T(2,T) \
|
|
|
|
INSTANTIATE_S_T(3,T) \
|
|
|
|
INSTANTIATE_S_T(4,T)
|
2012-05-22 14:13:34 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
|
|
|
|
INSTANTIATE(uint32_t)
|
|
|
|
INSTANTIATE(int32_t)
|
|
|
|
INSTANTIATE(uint64_t)
|
|
|
|
INSTANTIATE(int64_t)
|
|
|
|
INSTANTIATE(float)
|
|
|
|
INSTANTIATE(double)
|
2012-05-22 14:13:34 +10:00
|
|
|
|
2015-04-01 17:05:24 +11:00
|
|
|
|
2012-05-26 18:02:11 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-11-17 18:25:46 +11:00
|
|
|
template vector<2,float> util::polar_to_cartesian (util::vector<2,float>);
|
|
|
|
template vector<2,float> util::cartesian_to_polar (util::vector<2,float>);
|