-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathwykobi_matrix.hpp
130 lines (101 loc) · 4.08 KB
/
wykobi_matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
(***********************************************************************)
(* *)
(* Wykobi Computational Geometry Library *)
(* Release Version 0.0.5 *)
(* http://www.wykobi.com *)
(* Copyright (c) 2005-2019 Arash Partow, All Rights Reserved. *)
(* *)
(* The Wykobi computational geometry library and its components are *)
(* supplied under the terms of the open source MIT License. *)
(* The contents of the Wykobi computational geometry library and its *)
(* components may not be copied or disclosed except in accordance with *)
(* the terms of the MIT License. *)
(* *)
(* URL: https://opensource.org/licenses/MIT *)
(* *)
(***********************************************************************)
*/
#ifndef INCLUDE_WYKOBI_MATRIX
#define INCLUDE_WYKOBI_MATRIX
#include <vector>
#include <limits>
#include <cstdlib>
#include <cassert>
#include "wykobi.hpp"
#include "wykobi_math.hpp"
namespace wykobi
{
template <typename T, std::size_t M, std::size_t N>
class matrix
{
public:
matrix()
: dptr(reinterpret_cast<T*>(&data))
{
zero();
}
~matrix()
{}
matrix(const matrix<T,M,N>& m);
// column major
const T& operator()(std::size_t x, std::size_t y) const
{
return data[y][x];
}
T& operator()(std::size_t x, std::size_t y)
{
return data[y][x];
}
const T& operator()(std::size_t i) const
{
return dptr[i];
}
T& operator()(std::size_t i)
{
return dptr[i];
}
const T& operator[](std::size_t i) const
{
return dptr[i];
}
T& operator[](std::size_t i)
{
return dptr[i];
}
matrix<T,M,N>& operator=(const matrix<T,M,N>& m);
matrix<T,M,N>& operator+=(const T& value);
matrix<T,M,N>& operator-=(const T& value);
matrix<T,M,N>& operator*=(const T& value);
matrix<T,M,N>& operator/=(const T& value);
matrix<T,M,N>& operator+=(const matrix<T,M,N>& _matrix);
matrix<T,M,N>& operator-=(const matrix<T,M,N>& _matrix);
void zero();
void identity();
void swap(const unsigned int& x1,const unsigned int& y1,
const unsigned int& x2,const unsigned int& y2);
std::size_t size() const
{
return M * N;
}
private:
T data[M][N];
T* dptr;
};
template <typename T> inline T det(const matrix<T,1,1>& matrix);
template <typename T> inline T det(const matrix<T,2,2>& matrix);
template <typename T> inline T det(const matrix<T,3,3>& matrix);
template <typename T> inline T det(const matrix<T,4,4>& matrix);
template <typename T> inline void transpose(matrix<T,1,1>& matrix);
template <typename T> inline void transpose(matrix<T,2,2>& matrix);
template <typename T> inline void transpose(matrix<T,3,3>& matrix);
template <typename T> inline void transpose(matrix<T,4,4>& matrix);
template <typename T> inline void inverse(matrix<T,2,2>& out_matrix, const matrix<T,2,2>& in_matrix);
template <typename T> inline void inverse(matrix<T,3,3>& out_matrix, const matrix<T,3,3>& in_matrix);
template <typename T> inline void inverse(matrix<T,4,4>& out_matrix, const matrix<T,4,4>& in_matrix);
template <typename T, std::size_t N> inline void inverse(matrix<T,N,N>& out_matrix, const matrix<T,N,N>& in_matrix);
template <typename T> inline void eigen_values(const matrix<T,2,2>& matrix, T& eigen_value1, T& eigen_value2);
template <typename T> inline void eigenvector(const matrix<T,2,2>& matrix, vector2d<T>& eigenvector1, vector2d<T>& eigenvector2);
} // namespace wykobi
#include "wykobi_matrix.inl"
#endif