15 #include <vsg/maths/plane.h>
16 #include <vsg/maths/vec3.h>
17 #include <vsg/maths/vec4.h>
38 constexpr
explicit t_mat4(value_type v) :
44 constexpr
t_mat4(value_type v0, value_type v1, value_type v2, value_type v3,
45 value_type v4, value_type v5, value_type v6, value_type v7,
46 value_type v8, value_type v9, value_type v10, value_type v11,
47 value_type v12, value_type v13, value_type v14, value_type v15) :
48 value{{v0, v1, v2, v3},
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]}} {}
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; }
82 column_type& operator[](std::size_t c) {
return value[c]; }
83 const column_type& operator[](std::size_t c)
const {
return value[c]; }
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]; }
98 void set(value_type v0, value_type v1, value_type v2, value_type v3,
99 value_type v4, value_type v5, value_type v6, value_type v7,
100 value_type v8, value_type v9, value_type v10, value_type v11,
101 value_type v12, value_type v13, value_type v14, value_type v15)
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);
109 T* data() {
return value[0].data(); }
110 const T* data()
const {
return value[0].data(); }
122 return lhs.value[0] == rhs.value[0] &&
123 lhs.value[1] == rhs.value[1] &&
124 lhs.value[2] == rhs.value[2] &&
125 lhs.value[3] == rhs.value[3];
129 bool operator!=(
const t_mat4<T>& lhs,
const t_mat4<T>& rhs)
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];
138 bool operator<(
const t_mat4<T>& lhs,
const t_mat4<T>& rhs)
140 if (lhs.value[0] < rhs.value[0])
return true;
141 if (rhs.value[0] < lhs.value[0])
return false;
142 if (lhs.value[1] < rhs.value[1])
return true;
143 if (rhs.value[1] < lhs.value[1])
return false;
144 if (lhs.value[2] < rhs.value[2])
return true;
145 if (rhs.value[2] < lhs.value[2])
return false;
146 return lhs.value[3] < rhs.value[3];
150 T dot(
const t_mat4<T>& lhs,
const t_mat4<T>& rhs,
int c,
int r)
152 return lhs[0][r] * rhs[c][0] +
153 lhs[1][r] * rhs[c][1] +
154 lhs[2][r] * rhs[c][2] +
155 lhs[3][r] * rhs[c][3];
159 t_mat4<T> operator*(
const t_mat4<T>& lhs,
const t_mat4<T>& rhs)
161 return t_mat4<T>(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1), dot(lhs, rhs, 0, 2), dot(lhs, rhs, 0, 3),
162 dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1), dot(lhs, rhs, 1, 2), dot(lhs, rhs, 1, 3),
163 dot(lhs, rhs, 2, 0), dot(lhs, rhs, 2, 1), dot(lhs, rhs, 2, 2), dot(lhs, rhs, 2, 3),
164 dot(lhs, rhs, 3, 0), dot(lhs, rhs, 3, 1), dot(lhs, rhs, 3, 2), dot(lhs, rhs, 3, 3));
168 t_vec4<T> operator*(
const t_mat4<T>& lhs,
const t_vec4<T>& rhs)
170 return t_vec4<T>(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
171 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
172 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
173 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
179 template<
typename T,
typename R>
180 t_plane<R> operator*(
const t_mat4<T>& lhs,
const t_plane<R>& rhs)
182 t_plane<R> transformed(lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0] * rhs[3],
183 lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1] * rhs[3],
184 lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2] * rhs[3],
185 lhs[0][3] * rhs[0] + lhs[1][3] * rhs[1] + lhs[2][3] * rhs[2] + lhs[3][3] * rhs[3]);
186 T inv =
static_cast<R
>(1.0) / length(transformed.n);
187 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
193 t_vec4<T> operator*(
const t_vec4<T>& lhs,
const t_mat4<T>& rhs)
195 return t_vec4<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
196 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
197 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
198 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
204 template<
typename T,
typename R>
205 t_plane<T> operator*(
const t_plane<T>& lhs,
const t_mat4<R>& rhs)
207 t_plane<T> transformed(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + lhs[3] * rhs[0][3],
208 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + lhs[3] * rhs[1][3],
209 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + lhs[3] * rhs[2][3],
210 lhs[0] * rhs[3][0] + lhs[1] * rhs[3][1] + lhs[2] * rhs[3][2] + lhs[3] * rhs[3][3]);
211 T inv =
static_cast<T
>(1.0) / length(transformed.n);
212 return t_plane<T>(transformed[0] * inv, transformed[1] * inv, transformed[2] * inv, transformed[3] * inv);
216 t_vec3<T> operator*(
const t_mat4<T>& lhs,
const t_vec3<T>& rhs)
218 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]);
219 return t_vec3<T>((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2] + lhs[3][0]) * inv,
220 (lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2] + lhs[3][1]) * inv,
221 (lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2] + lhs[3][2]) * inv);
225 t_vec3<T> operator*(
const t_vec3<T>& lhs,
const t_mat4<T>& rhs)
227 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]);
228 return t_vec3<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2] + rhs[0][3] * inv,
229 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2] + rhs[1][3] * inv,
230 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2] + rhs[2][3] * inv);
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