libcruft-util/vector.hpp

105 lines
3.1 KiB
C++
Raw Normal View History

2011-05-23 17:18:52 +10:00
/*
* This file is part of libgim.
2011-05-23 17:18:52 +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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* 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
#include "json.hpp"
#include "detail/coord.hpp"
#include <array>
#include <iostream>
#include <initializer_list>
2011-05-23 17:18:52 +10:00
namespace util {
template <size_t S>
struct vector : public detail::coord_data<S> {
static_assert (S > 0, "vector dimensions must be strictly positive");
2011-05-23 17:18:52 +10:00
vector ();
2011-05-23 17:18:52 +10:00
template <typename... T>
vector (T ...t): detail::coord_data<S> {std::forward<T> (t)...} { ; }
2011-05-23 17:18:52 +10:00
util::vector<S> operator* (double) const;
util::vector<S>& operator*=(double);
util::vector<S> operator/ (double) const;
util::vector<S>& operator/=(double);
util::vector<S> operator+ (double) const;
util::vector<S>& operator+=(double);
util::vector<S> operator- (double) const;
util::vector<S>& operator-=(double);
util::vector<S> operator* (const util::vector<S>&) const;
util::vector<S>& operator*=(const util::vector<S>&);
2011-05-23 17:18:52 +10:00
util::vector<S> operator+ (const util::vector<S>&) const;
util::vector<S>& operator+=(const util::vector<S>&);
2011-05-23 17:18:52 +10:00
util::vector<S> operator- (void) const;
util::vector<S> operator- (const util::vector<S>&) const;
util::vector<S>& operator-=(const util::vector<S>&);
util::vector<S>& operator =(const util::vector <S>&);
bool operator== (const util::vector <S>&) const;
2011-10-29 15:07:34 +11:00
double magnitude (void) const;
double magnitude2 (void) const;
2011-05-23 17:18:52 +10:00
double dot (const util::vector<S>&) const;
2011-05-23 17:18:52 +10:00
util::vector<S>& normalise (void);
util::vector<S> normalised (void) const;
2011-05-23 17:18:52 +10:00
2011-10-29 23:14:07 +11:00
2012-05-03 15:56:56 +10:00
bool is_zero (void) const;
void sanity (void) const;
2011-05-23 17:18:52 +10:00
};
vector<3> cross (const vector<3>&, const vector<3>&);
vector<3> spherical_to_cartesian (const util::vector <3>&);
vector<3> cartesian_to_spherical (const util::vector <3>&);
typedef vector<2> vector2;
typedef vector<3> vector3;
2012-05-26 18:02:11 +10:00
template <size_t S> util::vector<S> operator* (double, const util::vector<S>&);
template <size_t S> util::vector<S> operator+ (double, const util::vector<S>&);
template <size_t S> util::vector<S> operator- (double, const util::vector<S>&);
2012-05-26 18:02:11 +10:00
template <size_t S> std::ostream& operator<< (std::ostream&, const util::vector<S>&);
template <size_t S>
const json::node& operator>> (const json::node&, util::vector<S>&);
2011-05-23 17:18:52 +10:00
}
#endif