libcruft-util/matrix.hpp

191 lines
6.2 KiB
C++
Raw Normal View History

/*
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
*
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.
*
2015-10-30 23:40:13 +11:00
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
*/
2011-05-23 17:18:52 +10:00
#ifndef __UTIL_MATRIX_HPP
#define __UTIL_MATRIX_HPP
#include "point.hpp"
#include "range.hpp"
2011-05-23 17:18:52 +10:00
2016-03-17 18:10:27 +11:00
#include <ostream>
2011-05-23 17:18:52 +10:00
namespace util {
2015-10-30 23:40:13 +11:00
template <size_t S, typename T>
struct matrix {
2015-10-30 23:40:13 +11:00
T values[S][S];
2011-05-23 17:18:52 +10:00
2016-08-15 17:34:24 +10:00
static constexpr size_t rows = S;
static constexpr size_t cols = S;
2014-08-19 20:45:28 +10:00
// index operators return a pointer into the data array so that
// multidimensional array syntax can be used transparently on this
// type.
T* operator[] (size_t);
const T* operator[] (size_t) const;
const T* begin (void) const;
const T* end (void) const;
T* begin (void);
T* end (void);
const T* cbegin (void) const;
const T* cend (void) const;
2014-12-15 13:42:44 +11:00
matrix& transpose (void);
matrix transposed (void) const;
T determinant (void) const;
2015-10-30 23:40:13 +11:00
matrix inverse (void) const;
matrix& invert (void);
matrix inverse_affine (void) const;
matrix& invert_affine (void);
2014-08-19 20:45:28 +10:00
2015-10-30 23:40:13 +11:00
matrix operator* (const matrix&) const;
matrix& operator*=(const matrix&);
2015-10-30 23:40:13 +11:00
vector<S,T> operator* (const vector<S,T>&) const;
point<S,T> operator* (const point<S,T> &) const;
2014-08-19 20:46:00 +10:00
bool is_affine (void) const;
2011-05-23 17:18:52 +10:00
2015-02-19 13:24:37 +11:00
template <typename U>
2015-10-30 23:40:13 +11:00
matrix<S,U> cast (void) const;
2015-02-19 13:24:37 +11:00
2014-12-15 13:44:33 +11:00
// Perspective matrices
2015-10-30 23:40:13 +11:00
static matrix<4,T> ortho (T left, T right, T bottom, T top, T near, T far);
static matrix<4,T> ortho2D (T left, T right, T bottom, T top);
static matrix<4,T> perspective (T fov, T aspect, range<T> Z);
2016-09-14 17:45:33 +10:00
static matrix<4,T> look_at (point<3,T> eye, point<3,T> target, vector<3,T> up);
2014-12-15 13:44:33 +11:00
// Affine matrices
static matrix<4,T> translation (util::vector<2,T>);
static matrix<4,T> translation (util::vector<3,T>);
static matrix<4,T> scale (util::vector<3,T>);
static matrix<4,T> scale (T);
static matrix<4,T> rotation (T angle, util::vector<3,T> about);
2014-12-15 13:44:33 +11:00
// Constant matrices
static constexpr matrix identity ();
static constexpr matrix zeroes ();
2011-05-23 17:18:52 +10:00
};
2016-09-14 17:53:34 +10:00
///////////////////////////////////////////////////////////////////////////
// Convert an affine rotation matrix to euler angles.
//
// Results are undefined if the matrix is not purely a rotation matrix,
// or if the dimension is not 3x3 or 4x4.
template <size_t S, typename T>
vector<3,T>
to_euler (const matrix<S,T>&);
2016-08-15 20:32:24 +10:00
///////////////////////////////////////////////////////////////////////////
// logical operations
template <size_t S, typename T>
constexpr
bool
operator== (const matrix<S,T>&, const matrix<S,T>&);
template <size_t S, typename T>
constexpr
bool
operator!= (const matrix<S,T>&, const matrix<S,T>&);
///////////////////////////////////////////////////////////////////////////
// element operations
template <size_t S, typename T>
2016-08-15 18:53:52 +10:00
constexpr
matrix<S,T>
operator+ (const matrix<S,T>&, const matrix<S,T>&);
template <size_t S, typename T>
constexpr
matrix<S,T>
operator- (const matrix<S,T>&, const matrix<S,T>&);
2016-08-15 20:32:24 +10:00
///////////////////////////////////////////////////////////////////////////
// scalar operations
template <size_t S, typename T> constexpr matrix<S,T> operator* (const matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T> operator/ (const matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T> operator+ (const matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T> operator- (const matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T> operator* (T, const matrix<S,T>&);
template <size_t S, typename T> constexpr matrix<S,T> operator/ (T, const matrix<S,T>&);
template <size_t S, typename T> constexpr matrix<S,T> operator+ (T, const matrix<S,T>&);
template <size_t S, typename T> constexpr matrix<S,T> operator- (T, const matrix<S,T>&);
template <size_t S, typename T> constexpr matrix<S,T>& operator*= (matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T>& operator/= (matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T>& operator+= (matrix<S,T>&, T);
template <size_t S, typename T> constexpr matrix<S,T>& operator-= (matrix<S,T>&, T);
///////////////////////////////////////////////////////////////////////////
2016-08-15 18:53:52 +10:00
template <size_t S, typename T>
T
determinant (const matrix<S,T>&);
template <size_t S, typename T>
matrix<S,T>
inverse (const matrix<S,T>&);
2016-09-14 17:46:03 +10:00
template <size_t S, typename T>
matrix<S,T>
transposed (const matrix<S,T>&);
///////////////////////////////////////////////////////////////////////////
2016-08-15 18:44:27 +10:00
template <size_t S, typename T>
matrix<S,T>
abs (const matrix<S,T>&);
template <size_t S, typename T>
constexpr
T
sum (const matrix<S,T>&);
2016-09-14 17:53:34 +10:00
///////////////////////////////////////////////////////////////////////////
2015-10-30 23:40:13 +11:00
template <typename T> using matrix3 = matrix<3,T>;
template <typename T> using matrix4 = matrix<4,T>;
2015-07-13 16:27:54 +10:00
2015-10-30 23:40:13 +11:00
template <size_t S> using matrixf = matrix<S,float>;
template <size_t S> using matrixd = matrix<S,double>;
typedef matrix<2,float> matrix2f;
typedef matrix<2,double> matrix2d;
typedef matrix<3,float> matrix3f;
typedef matrix<3,double> matrix3d;
2015-10-30 23:40:13 +11:00
typedef matrix<4,float> matrix4f;
typedef matrix<4,double> matrix4d;
2016-09-14 17:53:34 +10:00
///////////////////////////////////////////////////////////////////////////
2015-10-30 23:40:13 +11:00
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, const matrix<S,T>&);
2011-05-23 17:18:52 +10:00
}
2015-02-19 13:24:37 +11:00
#include "matrix.ipp"
2011-05-23 17:18:52 +10:00
#endif