vsg  1.1.0
VulkanSceneGraph library
mat4.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 
19 namespace 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  T* data() { return value[0].data(); }
110  const T* data() const { return value[0].data(); }
111  };
112 
113  using mat4 = t_mat4<float>;
114  using dmat4 = t_mat4<double>;
115 
116  VSG_type_name(vsg::mat4);
117  VSG_type_name(vsg::dmat4);
118 
119  template<typename T>
120  bool operator==(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
121  {
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];
126  }
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  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];
147  }
148 
149  template<typename T>
150  T dot(const t_mat4<T>& lhs, const t_mat4<T>& rhs, int c, int r)
151  {
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];
156  }
157 
158  template<typename T>
159  t_mat4<T> operator*(const t_mat4<T>& lhs, const t_mat4<T>& rhs)
160  {
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));
165  }
166 
167  template<typename T>
168  t_vec4<T> operator*(const t_mat4<T>& lhs, const t_vec4<T>& rhs)
169  {
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]);
174  }
175 
176  /* Right multiplication of a matrix and a plane. This can't be used directly to transform a
177  plane from one coordinate system to another using the coordinate system's matrix; the inverse
178  transpose of the matrix should be used. */
179  template<typename T, typename R>
180  t_plane<R> operator*(const t_mat4<T>& lhs, const t_plane<R>& rhs)
181  {
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);
188  }
189 
190  /* Left multiplication of a row vector. Equivalent to multiplying the column vector by the
191  matrix transpose. */
192  template<typename T>
193  t_vec4<T> operator*(const t_vec4<T>& lhs, const t_mat4<T>& rhs)
194  {
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]);
199  }
200 
201  /* Left multiplication of a plane and a matrix. If the matrix is the inverse of the
202  local-to-world transform i.e., the world-to-local transform, then this can be used directly
203  to transform a plane from a coordinate system's local coordinates to world coordinates. */
204  template<typename T, typename R>
205  t_plane<T> operator*(const t_plane<T>& lhs, const t_mat4<R>& rhs)
206  {
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);
213  }
214 
215  template<typename T>
216  t_vec3<T> operator*(const t_mat4<T>& lhs, const t_vec3<T>& rhs)
217  {
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);
222  }
223 
224  template<typename T>
225  t_vec3<T> operator*(const t_vec3<T>& lhs, const t_mat4<T>& rhs)
226  {
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);
231  }
232 
233 } // 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