vsg 1.1.13
VulkanSceneGraph library
Loading...
Searching...
No Matches
transform.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/common.h>
16
17namespace vsg
18{
20 template<typename T>
21 constexpr t_mat4<T> rotate(const t_quat<T>& q)
22 {
23 T qxx(q.x * q.x);
24 T qyy(q.y * q.y);
25 T qzz(q.z * q.z);
26 T qxy(q.x * q.y);
27 T qxz(q.x * q.z);
28 T qyz(q.y * q.z);
29 T qwx(q.w * q.x);
30 T qwy(q.w * q.y);
31 T qwz(q.w * q.z);
32
33 const T zero(numbers<T>::zero());
34 const T one(numbers<T>::one());
35 const T two(numbers<T>::two());
36
37 return t_mat4<T>(one - two * (qyy + qzz), two * (qxy + qwz), two * (qxz - qwy), zero,
38 two * (qxy - qwz), one - two * (qxx + qzz), two * (qyz + qwx), zero,
39 two * (qxz + qwy), two * (qyz - qwx), one - two * (qxx + qyy), zero,
40 zero, zero, zero, one);
41 }
42
44 template<typename T>
45 t_mat4<T> rotate(T angle_radians, T x, T y, T z)
46 {
47 const T zero(numbers<T>::zero());
48 const T one(numbers<T>::one());
49 const T c = std::cos(angle_radians);
50 const T s = std::sin(angle_radians);
51 const T one_minus_c = one - c;
52 return t_mat4<T>(x * x * one_minus_c + c, y * x * one_minus_c + z * s, x * z * one_minus_c - y * s, zero,
53 x * y * one_minus_c - z * s, y * y * one_minus_c + c, y * z * one_minus_c + x * s, zero,
54 x * z * one_minus_c + y * s, y * z * one_minus_c - x * s, z * z * one_minus_c + c, zero,
55 zero, zero, zero, one);
56 }
57
59 template<typename T>
60 t_mat4<T> rotate(T angle_radians, const t_vec3<T>& v)
61 {
62 return rotate(angle_radians, v.value[0], v.value[1], v.value[2]);
63 }
64
66 template<typename T>
67 constexpr t_mat4<T> translate(T x, T y, T z)
68 {
69 const T zero(numbers<T>::zero());
70 const T one(numbers<T>::one());
71 return t_mat4<T>(one, zero, zero, zero,
72 zero, one, zero, zero,
73 zero, zero, one, zero,
74 x, y, z, one);
75 }
76
78 template<typename T>
79 constexpr t_mat4<T> translate(const t_vec3<T>& v)
80 {
81 return translate(v.value[0], v.value[1], v.value[2]);
82 }
83
85 template<typename T>
86 constexpr t_mat4<T> scale(T s)
87 {
88 const T zero(numbers<T>::zero());
89 const T one(numbers<T>::one());
90 return t_mat4<T>(s, zero, zero, zero,
91 zero, s, zero, zero,
92 zero, zero, s, zero,
93 zero, zero, zero, one);
94 }
95
97 template<typename T>
98 constexpr t_mat4<T> scale(T sx, T sy, T sz)
99 {
100 const T zero(numbers<T>::zero());
101 const T one(numbers<T>::one());
102 return t_mat4<T>(sx, zero, zero, zero,
103 zero, sy, zero, zero,
104 zero, zero, sz, zero,
105 zero, zero, zero, one);
106 }
107
109 template<typename T>
110 constexpr t_mat4<T> scale(const t_vec3<T>& v)
111 {
112 return scale(v.value[0], v.value[1], v.value[2]);
113 }
114
116 template<typename T>
117 constexpr t_mat3<T> transpose(const t_mat3<T>& m)
118 {
119 return t_mat3<T>(m[0][0], m[1][0], m[2][0],
120 m[0][1], m[1][1], m[2][1],
121 m[0][2], m[1][2], m[2][2]);
122 }
123
125 template<typename T>
126 constexpr t_mat4<T> transpose(const t_mat4<T>& m)
127 {
128 return t_mat4<T>(m[0][0], m[1][0], m[2][0], m[3][0],
129 m[0][1], m[1][1], m[2][1], m[3][1],
130 m[0][2], m[1][2], m[2][2], m[3][2],
131 m[0][3], m[1][3], m[2][3], m[3][3]);
132 }
133
138 //. https://vincent-p.github.io/posts/vulkan_perspective_matrix/
139 template<typename T>
140 constexpr t_mat4<T> perspective(T fovy_radians, T aspectRatio, T zNear, T zFar)
141 {
142 const T zero(numbers<T>::zero());
143 const T one(numbers<T>::one());
144 T f = static_cast<T>(one / std::tan(fovy_radians * numbers<T>::half()));
145 T r = static_cast<T>(one / (zFar - zNear));
146 return t_mat4<T>(f / aspectRatio, zero, zero, zero,
147 zero, -f, zero, zero,
148 zero, zero, zNear * r, -one,
149 zero, zero, (zFar * zNear) * r, zero);
150 }
151
153 template<typename T>
154 constexpr t_mat4<T> perspective(T left, T right, T bottom, T top, T zNear, T zFar)
155 {
156 const T zero(numbers<T>::zero());
157 const T one(numbers<T>::one());
158 const T two(numbers<T>::two());
159 return t_mat4<T>(two * zNear / (right - left), zero, zero, zero,
160 zero, two * zNear / (bottom - top), zero, zero,
161 (right + left) / (right - left), (bottom + top) / (bottom - top), zNear / (zFar - zNear), -one,
162 zero, zero, zNear * zFar / (zFar - zNear), zero);
163 }
164
166 template<typename T>
167 constexpr t_mat4<T> orthographic(T left, T right, T bottom, T top, T zNear, T zFar)
168 {
169 const T zero(numbers<T>::zero());
170 const T one(numbers<T>::one());
171 const T two(numbers<T>::two());
172 return t_mat4<T>(two / (right - left), zero, zero, zero,
173 zero, two / (bottom - top), zero, zero,
174 zero, zero, one / (zFar - zNear), zero,
175 -(right + left) / (right - left), -(bottom + top) / (bottom - top), zFar / (zFar - zNear), one);
176 }
177
178 template<typename T>
179 constexpr t_mat4<T> lookAt(const t_vec3<T>& eye, const t_vec3<T>& center, const t_vec3<T>& up)
180 {
181 using vec_type = t_vec3<T>;
182
183 const T zero(numbers<T>::zero());
184 const T one(numbers<T>::one());
185
186 vec_type forward = normalize(center - eye);
187 vec_type up_normal = normalize(up);
188 vec_type side = normalize(cross(forward, up_normal));
189 vec_type u = normalize(cross(side, forward));
190
191 return t_mat4<T>(side[0], u[0], -forward[0], zero,
192 side[1], u[1], -forward[1], zero,
193 side[2], u[2], -forward[2], zero,
194 zero, zero, zero, one) *
195 vsg::translate(-eye.x, -eye.y, -eye.z);
196 }
197
198 template<typename T>
199 constexpr t_mat4<T> computeBillboardMatrix(const t_vec3<T>& centerEye, T autoscaleDistance)
200 {
201 const T zero(numbers<T>::zero());
202 const T one(numbers<T>::one());
203
204 auto distance = -centerEye.z;
205 auto scale = (distance < autoscaleDistance) ? distance / autoscaleDistance : one;
206 t_mat4<T> mS(scale, zero, zero, zero,
207 zero, scale, zero, zero,
208 zero, zero, scale, zero,
209 zero, zero, zero, one);
210
211 t_mat4<T> mT(one, zero, zero, zero,
212 zero, one, zero, zero,
213 zero, zero, one, zero,
214 centerEye.x, centerEye.y, centerEye.z, one);
215
216 return mT * mS;
217 }
218
220 enum class CoordinateConvention
221 {
222 NO_PREFERENCE,
223 X_UP, // x up, y left/west, z out/south
224 Y_UP, // x right/east, y up, z out/south
225 Z_UP // x right/east, y forward/north, z up
226 };
227
230 extern VSG_DECLSPEC bool transform(CoordinateConvention source, CoordinateConvention destination, dmat4& matrix);
231
233 extern VSG_DECLSPEC mat3 inverse_3x3(const mat4& m);
234
236 extern VSG_DECLSPEC dmat3 inverse_3x3(const dmat4& m);
237
239 extern VSG_DECLSPEC mat4 inverse_4x3(const mat4& m);
240
242 extern VSG_DECLSPEC dmat4 inverse_4x3(const dmat4& m);
243
245 extern VSG_DECLSPEC mat4 inverse_4x4(const mat4& m);
246
248 extern VSG_DECLSPEC dmat4 inverse_4x4(const dmat4& m);
249
251 extern VSG_DECLSPEC mat4 inverse(const mat4& m);
252
254 extern VSG_DECLSPEC dmat4 inverse(const dmat4& m);
255
257 extern VSG_DECLSPEC float determinant(const mat4& m);
258
260 extern VSG_DECLSPEC double determinant(const dmat4& m);
261
265 extern VSG_DECLSPEC bool decompose(const mat4& m, vec3& translation, quat& rotation, vec3& scale);
266
270 extern VSG_DECLSPEC bool decompose(const dmat4& m, dvec3& translation, dquat& rotation, dvec3& scale);
271
275 extern VSG_DECLSPEC bool decompose(const ldmat4& m, ldvec3& translation, ldquat& rotation, ldvec3& scale);
276
278 extern VSG_DECLSPEC sphere computeFrustumBound(const mat4& m);
279
281 extern VSG_DECLSPEC dsphere computeFrustumBound(const dmat4& m);
282
285 struct VSG_DECLSPEC ComputeTransform : public ConstVisitor
286 {
287 dvec3 origin;
288 dmat4 matrix;
289
290 void apply(const Transform& transform) override;
291 void apply(const MatrixTransform& mt) override;
292 void apply(const CoordinateFrame& cf) override;
293 void apply(const Camera& camera) override;
294 };
295
297 template<typename T>
298 dmat4 computeTransform(const T& nodePath)
299 {
300 return visit<ComputeTransform>(nodePath).matrix;
301 }
302
303} // namespace vsg
Definition Camera.h:27
CoordinateFrame provides support for astronomically large coordinates.
Definition CoordinateFrame.h:22
Definition MatrixTransform.h:24
Transform node is a pure virtual base class for positioning/scaling/rotating subgraphs.
Definition Transform.h:22
Definition transform.h:286
t_mat3 template class that represents a 3x3 matrix.
Definition mat3.h:23
t_mat4 template class that represents a 4x4 matrix.
Definition mat4.h:25
t_quat template class that represents a quaternion
Definition quat.h:35
t_vec3 template class that represents a 3D vector
Definition vec3.h:34