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
|
|
|
*
|
2015-07-21 01:40:53 +10:00
|
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_VECTOR_HPP
|
|
|
|
#define __UTIL_VECTOR_HPP
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
#include "json/tree.hpp"
|
2015-03-06 01:15:52 +11:00
|
|
|
#include "coord.hpp"
|
2011-10-29 21:17:48 +11:00
|
|
|
|
2012-05-18 17:56:24 +10:00
|
|
|
#include <array>
|
|
|
|
#include <initializer_list>
|
2011-05-23 17:18:52 +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>
|
2015-04-09 17:47:35 +10:00
|
|
|
struct vector : public coord::base<S,T,vector,coord::xyzw,coord::stpq>
|
|
|
|
{
|
|
|
|
using coord::base<S,T,util::vector,coord::xyzw,coord::stpq>::base;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-04-15 13:48:51 +10:00
|
|
|
// vector size
|
2014-12-15 20:10:56 +11:00
|
|
|
bool is_zero (void) const;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
T magnitude (void) const;
|
|
|
|
T magnitude2 (void) const;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-04-15 14:06:49 +10:00
|
|
|
T difference (vector<S,T>) const;
|
|
|
|
T difference2 (vector<S,T>) const;
|
2015-01-28 15:00:20 +11:00
|
|
|
|
2015-04-15 13:48:51 +10:00
|
|
|
// normalisation
|
|
|
|
bool is_normalised (void) const;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
vector<S,T>& normalise (void);
|
|
|
|
vector<S,T> normalised [[gnu::warn_unused_result]] (void) const;
|
2012-05-03 15:56:56 +10:00
|
|
|
|
2015-07-21 01:40:53 +10:00
|
|
|
// representations
|
|
|
|
template <size_t D> vector<D,T> homog (void) const;
|
|
|
|
|
2015-01-28 14:59:33 +11:00
|
|
|
// constants
|
2015-04-15 13:51:52 +10:00
|
|
|
static const vector<S,T> UNIT;
|
2015-01-15 14:01:51 +11:00
|
|
|
static const vector<S,T> ZERO;
|
2013-08-06 14:32:08 +10:00
|
|
|
|
2011-10-18 21:45:55 +11:00
|
|
|
void sanity (void) const;
|
2011-05-23 17:18:52 +10:00
|
|
|
};
|
2012-04-24 18:12:07 +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
|
|
|
|
2015-04-15 14:06:49 +10:00
|
|
|
template <typename T> vector<3,T> cross (vector<3,T>, vector<3,T>);
|
|
|
|
template <typename T> vector<3,T> spherical_to_cartesian (vector<3,T>);
|
|
|
|
template <typename T> vector<3,T> cartesian_to_spherical (vector<3,T>);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
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>
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::node& operator>> (const json::tree::node&, vector<S,T>&);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// convenience typedefs
|
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>;
|
|
|
|
|
2015-10-06 15:19:29 +11:00
|
|
|
template <size_t S> using vectori = vector<S,intmax_t>;
|
|
|
|
template <size_t S> using vectorf = vector<S,float>;
|
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef vector2<size_t> vector2u;
|
|
|
|
typedef vector3<size_t> vector3u;
|
2015-07-22 02:54:52 +10:00
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef vector2<intmax_t> vector2i;
|
|
|
|
typedef vector3<intmax_t> vector3i;
|
2015-01-16 14:44:26 +11:00
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef vector2<float> vector2f;
|
|
|
|
typedef vector3<float> vector3f;
|
|
|
|
typedef vector4<float> vector4f;
|
2012-04-24 18:12:07 +10:00
|
|
|
|
2015-07-24 01:34:44 +10:00
|
|
|
typedef vector2<double> vector2d;
|
|
|
|
typedef vector3<double> vector3d;
|
|
|
|
typedef vector3<double> vector4d;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:40:53 +10:00
|
|
|
#include "vector.ipp"
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif
|
2011-10-18 21:45:55 +11:00
|
|
|
|