2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2011-06-21 20:26:32 +10:00
|
|
|
* This file is part of libgim.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2011-06-21 20:26:32 +10:00
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
2011-05-23 17:18:52 +10:00
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
2014-08-18 22:14:31 +10:00
|
|
|
*
|
2011-06-21 20:26:32 +10:00
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
2011-05-23 17:18:52 +10:00
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
2014-08-18 22:14:31 +10:00
|
|
|
*
|
2011-05-23 17:18:52 +10:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-06-21 20:26:32 +10:00
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2011 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>
|
2011-10-18 21:45:55 +11:00
|
|
|
#include <iostream>
|
2012-05-18 17:56:24 +10:00
|
|
|
#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-03-06 01:46:01 +11:00
|
|
|
struct vector : public coord::base<S,T,coord::xyzw,coord::stpq> {
|
|
|
|
using coord::base<S,T,coord::xyzw,coord::stpq>::base;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
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
|
|
|
// vector operators
|
|
|
|
T magnitude (void) const;
|
|
|
|
T magnitude2 (void) const;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-01-28 15:00:20 +11:00
|
|
|
T difference (const vector<S,T>&) const;
|
|
|
|
T difference2 (const vector<S,T>&) 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-01-28 14:59:33 +11:00
|
|
|
// size operations
|
2014-12-15 20:10:56 +11:00
|
|
|
template <size_t D> vector<D,T> redim (void) const;
|
2015-01-15 14:01:51 +11:00
|
|
|
template <size_t D> vector<D,T> redim (const util::vector<D,T> &fill) const;
|
2015-02-12 17:41:07 +11:00
|
|
|
template <size_t D> vector<D,T> redim (T fill) const;
|
2015-01-15 14:01:51 +11:00
|
|
|
|
2015-01-28 14:59:33 +11:00
|
|
|
// constants
|
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
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// free vector operators
|
|
|
|
template <typename T> vector<2,T> polar_to_cartesian (const vector<2,T>&);
|
|
|
|
|
|
|
|
template <typename T> vector<3,T> cross (const vector<3,T>&, const vector<3,T>&);
|
|
|
|
template <typename T> vector<3,T> spherical_to_cartesian (const vector<3,T>&);
|
|
|
|
template <typename T> vector<3,T> cartesian_to_spherical (const vector<3,T>&);
|
2012-05-18 17:56:24 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
// output and serialisation operators
|
|
|
|
template <size_t S, typename T> std::ostream& operator<< (std::ostream&, const vector<S,T>&);
|
2012-05-18 17:56:24 +10: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& 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-01-16 14:44:26 +11:00
|
|
|
typedef vector<2,size_t> vector2u;
|
|
|
|
typedef vector<2,long> vector2i;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
typedef vector<2,float> vector2f;
|
|
|
|
typedef vector<3,float> vector3f;
|
2015-01-05 18:08:34 +11:00
|
|
|
typedef vector<4,float> vector4f;
|
2012-04-24 18:12:07 +10:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
typedef vector<2,double> vector2d;
|
|
|
|
typedef vector<3,double> vector3d;
|
2014-12-16 15:30:25 +11:00
|
|
|
typedef vector<3,double> vector4d;
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
2015-01-15 14:01:51 +11:00
|
|
|
#include "vector.ipp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
#endif
|
2011-10-18 21:45:55 +11:00
|
|
|
|