vsg: include/vsg/maths/mat4.h Source File

vsg 1.1.10 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 using ldmat4 = t_mat4<long double>;
125
126 VSG_type_name(vsg::mat4);
127 VSG_type_name(vsg::dmat4);
128 VSG_type_name(vsg::ldmat4);
129
130 template<typename T>
131 bool operator==(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
132 {
133 return lhs.value[0] == rhs.value[0] &&
134 lhs.value[1] == rhs.value[1] &&
135 lhs.value[2] == rhs.value[2] &&
136 lhs.value[3] == rhs.value[3];
137 }
138
139 template<typename T>
140 bool operator!=(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
141 {
142 return lhs.value[0] != rhs.value[0] ||
143 lhs.value[1] != rhs.value[1] ||
144 lhs.value[2] != rhs.value[2] ||
145 lhs.value[3] != rhs.value[3];
146 }
147
148 template<typename T>
149 bool operator<(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
150 {
151 if (lhs.value[0] < rhs.value[0]) return true;
152 if (rhs.value[0] < lhs.value[0]) return false;
153 if (lhs.value[1] < rhs.value[1]) return true;
154 if (rhs.value[1] < lhs.value[1]) return false;
155 if (lhs.value[2] < rhs.value[2]) return true;
156 if (rhs.value[2] < lhs.value[2]) return false;
157 return lhs.value[3] < rhs.value[3];
158 }
159
160 template<typename T>
161 T dot(const t_mat4<T>& lhs, const t_mat4<T>& rhs, int c, int r)
162 {
163 return lhs[0][r] * rhs[c][0] +
164 lhs[1][r] * rhs[c][1] +
165 lhs[2][r] * rhs[c][2] +
166 lhs[3][r] * rhs[c][3];
167 }
168
169 template<typename T>
170 t_mat4<T> operator*(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
171 {
172 return t_mat4<T>(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1), dot(lhs, rhs, 0, 2), dot(lhs, rhs, 0, 3),
173 dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1), dot(lhs, rhs, 1, 2), dot(lhs, rhs, 1, 3),
174 dot(lhs, rhs, 2, 0), dot(lhs, rhs, 2, 1), dot(lhs, rhs, 2, 2), dot(lhs, rhs, 2, 3),
175 dot(lhs, rhs, 3, 0), dot(lhs, rhs, 3, 1), dot(lhs, rhs, 3, 2), dot(lhs, rhs, 3, 3));
176 }
177
178 template<typename T>
179 t_vec4<T> operator*(const t_mat4<T>& lhs, const t_vec4<T>& rhs)
180 {
181 return t_vec4<T>(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
182 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
183 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
184 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
185 }
186
187 /* Right multiplication of a matrix and a plane. This can't be used directly to transform a
188 plane from one coordinate system to another using the coordinate system's matrix; the inverse
189 transpose of the matrix should be used. */
190 template<typename T, typename R>
191 t_plane<R> operator*(const t_mat4<T>& lhs, const t_plane<R>& rhs)
192 {
193 t_plane<R> transformed(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
194 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
195 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
196 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
197 T inv = static_cast<R>(1.0) / length(transformed.n);
198 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
199 }
200
201 /* Left multiplication of a row vector. Equivalent to multiplying the column vector by the
202 matrix transpose. */
203 template<typename T>
204 t_vec4<T> operator*(const t_vec4<T>& lhs, const t_mat4<T>& rhs)
205 {
206 return t_vec4<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
207 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
208 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
209 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
210 }
211
212 /* Left multiplication of a plane and a matrix. If the matrix is the inverse of the
213 local-to-world transform i.e., the world-to-local transform, then this can be used directly
214 to transform a plane from a coordinate system's local coordinates to world coordinates. */
215 template<typename T, typename R>
216 t_plane<T> operator*(const t_plane<T>& lhs, const t_mat4<R>& rhs)
217 {
218 t_plane<T> transformed(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
219 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
220 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
221 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
222 T inv = static_cast<T>(1.0) / length(transformed.n);
223 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
224 }
225
226 template<typename T>
227 t_vec3<T> operator*(const t_mat4<T>& lhs, const t_vec3<T>& rhs)
228 {
229 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]);
230 return t_vec3<T>((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0]) * inv,
231 (lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1]) * inv,
232 (lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2]) * inv);
233 }
234
235 template<typename T>
236 t_vec3<T> operator*(const t_vec3<T>& lhs, const t_mat4<T>& rhs)
237 {
238 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]);
239 return t_vec3<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + rhs[0][3] * inv,
240 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + rhs[1][3] * inv,
241 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + rhs[2][3] * inv);
242 }
243
244} // namespace vsg
vsg::t_mat4
t_mat4 template class that represents a 4x4 matrix.
Definition mat4.h:25
vsg::t_plane
Definition plane.h:33
vsg::t_vec3
t_vec3 template class that represents a 3D vector
Definition vec3.h:34
vsg::t_vec4
t_vec4 template class that represents a 4D vector
Definition vec4.h:35