vsg 1.1.3
VulkanSceneGraph library
Loading...
Searching...
No Matches
mat4.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/maths/plane.h>
16#include <vsg/maths/vec3.h>
17#include <vsg/maths/vec4.h>
18
19namespace vsg
20{
21
23 template<typename T>
24 struct t_mat4
25 {
26 public:
27 using value_type = T;
28 using column_type = t_vec4<T>;
29
30 column_type value[4];
31
32 constexpr t_mat4() :
33 value{{1, 0, 0, 0},
34 {0, 1, 0, 0},
35 {0, 0, 1, 0},
36 {0, 0, 0, 1}} {}
37
38 constexpr explicit t_mat4(value_type v) :
39 value{{v, 0, 0, 0},
40 {0, v, 0, 0},
41 {0, 0, v, 0},
42 {0, 0, 0, v}} {}
43
44 constexpr t_mat4(value_type v0, value_type v1, value_type v2, value_type v3, /* column 0 */
45 value_type v4, value_type v5, value_type v6, value_type v7, /* column 1 */
46 value_type v8, value_type v9, value_type v10, value_type v11, /* column 2 */
47 value_type v12, value_type v13, value_type v14, value_type v15) /* column 3 */ :
48 value{{v0, v1, v2, v3},
49 {v4, v5, v6, v7},
50 {v8, v9, v10, v11},
51 {v12, v13, v14, v15}}
52 {
53 }
54
55 constexpr explicit t_mat4(value_type v[16]) :
56 value{{v[0], v[1], v[2], v[3]},
57 {v[4], v[5], v[6], v[7]},
58 {v[8], v[9], v[10], v[11]},
59 {v[12], v[13], v[14], v[15]}} {}
60
61 constexpr t_mat4(const column_type& c0,
62 const column_type& c1,
63 const column_type& c2,
64 const column_type& c3) :
65 value{c0, c1, c2, c3}
66 {
67 }
68
69 template<typename R>
70 explicit t_mat4(const t_mat4<R>& rhs)
71 {
72 value[0] = rhs[0];
73 value[1] = rhs[1];
74 value[2] = rhs[2];
75 value[3] = rhs[3];
76 }
77
78 constexpr std::size_t size() const { return 16; }
79 constexpr std::size_t columns() const { return 4; }
80 constexpr std::size_t rows() const { return 4; }
81
82 column_type& operator[](std::size_t c) { return value[c]; }
83 const column_type& operator[](std::size_t c) const { return value[c]; }
84
85 value_type& operator()(std::size_t c, std::size_t r) { return value[c][r]; }
86 value_type operator()(std::size_t c, std::size_t r) const { return value[c][r]; }
87
88 template<typename R>
89 t_mat4& operator=(const t_mat4<R>& rhs)
90 {
91 value[0] = rhs[0];
92 value[1] = rhs[1];
93 value[2] = rhs[2];
94 value[3] = rhs[3];
95 return *this;
96 }
97
98 void set(value_type v0, value_type v1, value_type v2, value_type v3, /* column 0 */
99 value_type v4, value_type v5, value_type v6, value_type v7, /* column 1 */
100 value_type v8, value_type v9, value_type v10, value_type v11, /* column 2 */
101 value_type v12, value_type v13, value_type v14, value_type v15) /* column 3 */
102 {
103 value[0].set(v0, v1, v2, v3);
104 value[1].set(v4, v5, v6, v7);
105 value[2].set(v8, v9, v10, v11);
106 value[3].set(v12, v13, v14, v15);
107 }
108
109 template<typename R>
110 void set(const t_mat4<R>& rhs)
111 {
112 value[0] = rhs[0];
113 value[1] = rhs[1];
114 value[2] = rhs[2];
115 value[3] = rhs[3];
116 }
117
118 T* data() { return value[0].data(); }
119 const T* data() const { return value[0].data(); }
120 };
121
122 using mat4 = t_mat4<float>;
123 using dmat4 = t_mat4<double>;
124
125 VSG_type_name(vsg::mat4);
126 VSG_type_name(vsg::dmat4);
127
128 template<typename T>
129 bool operator==(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
130 {
131 return lhs.value[0] == rhs.value[0] &&
132 lhs.value[1] == rhs.value[1] &&
133 lhs.value[2] == rhs.value[2] &&
134 lhs.value[3] == rhs.value[3];
135 }
136
137 template<typename T>
138 bool operator!=(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
139 {
140 return lhs.value[0] != rhs.value[0] ||
141 lhs.value[1] != rhs.value[1] ||
142 lhs.value[2] != rhs.value[2] ||
143 lhs.value[3] != rhs.value[3];
144 }
145
146 template<typename T>
147 bool operator<(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
148 {
149 if (lhs.value[0] < rhs.value[0]) return true;
150 if (rhs.value[0] < lhs.value[0]) return false;
151 if (lhs.value[1] < rhs.value[1]) return true;
152 if (rhs.value[1] < lhs.value[1]) return false;
153 if (lhs.value[2] < rhs.value[2]) return true;
154 if (rhs.value[2] < lhs.value[2]) return false;
155 return lhs.value[3] < rhs.value[3];
156 }
157
158 template<typename T>
159 T dot(const t_mat4<T>& lhs, const t_mat4<T>& rhs, int c, int r)
160 {
161 return lhs[0][r] * rhs[c][0] +
162 lhs[1][r] * rhs[c][1] +
163 lhs[2][r] * rhs[c][2] +
164 lhs[3][r] * rhs[c][3];
165 }
166
167 template<typename T>
168 t_mat4<T> operator*(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
169 {
170 return t_mat4<T>(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1), dot(lhs, rhs, 0, 2), dot(lhs, rhs, 0, 3),
171 dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1), dot(lhs, rhs, 1, 2), dot(lhs, rhs, 1, 3),
172 dot(lhs, rhs, 2, 0), dot(lhs, rhs, 2, 1), dot(lhs, rhs, 2, 2), dot(lhs, rhs, 2, 3),
173 dot(lhs, rhs, 3, 0), dot(lhs, rhs, 3, 1), dot(lhs, rhs, 3, 2), dot(lhs, rhs, 3, 3));
174 }
175
176 template<typename T>
177 t_vec4<T> operator*(const t_mat4<T>& lhs, const t_vec4<T>& rhs)
178 {
179 return t_vec4<T>(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
180 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
181 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
182 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
183 }
184
185 /* Right multiplication of a matrix and a plane. This can't be used directly to transform a
186 plane from one coordinate system to another using the coordinate system's matrix; the inverse
187 transpose of the matrix should be used. */
188 template<typename T, typename R>
189 t_plane<R> operator*(const t_mat4<T>& lhs, const t_plane<R>& rhs)
190 {
191 t_plane<R> transformed(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
192 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
193 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
194 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
195 T inv = static_cast<R>(1.0) / length(transformed.n);
196 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
197 }
198
199 /* Left multiplication of a row vector. Equivalent to multiplying the column vector by the
200 matrix transpose. */
201 template<typename T>
202 t_vec4<T> operator*(const t_vec4<T>& lhs, const t_mat4<T>& rhs)
203 {
204 return t_vec4<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
205 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
206 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
207 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
208 }
209
210 /* Left multiplication of a plane and a matrix. If the matrix is the inverse of the
211 local-to-world transform i.e., the world-to-local transform, then this can be used directly
212 to transform a plane from a coordinate system's local coordinates to world coordinates. */
213 template<typename T, typename R>
214 t_plane<T> operator*(const t_plane<T>& lhs, const t_mat4<R>& rhs)
215 {
216 t_plane<T> transformed(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
217 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
218 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
219 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
220 T inv = static_cast<T>(1.0) / length(transformed.n);
221 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
222 }
223
224 template<typename T>
225 t_vec3<T> operator*(const t_mat4<T>& lhs, const t_vec3<T>& rhs)
226 {
227 T inv = static_cast<T>(1.0) / (lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3]);
228 return t_vec3<T>((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0]) * inv,
229 (lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1]) * inv,
230 (lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2]) * inv);
231 }
232
233 template<typename T>
234 t_vec3<T> operator*(const t_vec3<T>& lhs, const t_mat4<T>& rhs)
235 {
236 T inv = static_cast<T>(1.0) / (lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + rhs[3][3]);
237 return t_vec3<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + rhs[0][3] * inv,
238 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + rhs[1][3] * inv,
239 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + rhs[2][3] * inv);
240 }
241
242} // namespace vsg
t_mat4 template class that represents a 4x4 matrix.
Definition mat4.h:25
t_vec4 template class that represents a 4D vector
Definition vec4.h:35