Skip to main content

model.ixx File

Model transform helper functions. More...

Included Headers

#include <cmath> #include <helios.math.types>

Namespaces Index

namespacehelios
namespacemath

Mathematical operations and types. More...

namespacetransform

Transformation utilities for 3D graphics and scene graph inheritance. More...

Description

Model transform helper functions.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file model.ixx
3 * @brief Model transform helper functions.
4 */
5module;
6
7#include <cmath>
8
9export module helios.math.transform:model;
10
11import helios.math.types;
12
14
16 const float cos_theta,
17 const float sin_theta,
18 const vec3f& normalized_axis
19 ) noexcept {
20
21 const float t = 1.0f - cos_theta;
22
23 const float x = normalized_axis[0],
24 y = normalized_axis[1],
25 z = normalized_axis[2];
26
27
28 return mat4f{
29 cos_theta + x * x * t,
30 x * y * t + z * sin_theta,
31 x * z * t - y * sin_theta,
32 0,
33
34 x * y * t - z * sin_theta,
35 cos_theta + y * y * t,
36 y * z * t + x * sin_theta,
37 0,
38
39 x * z * t + y * sin_theta,
40 y * z * t - x * sin_theta,
41 cos_theta + z * z * t,
42 0,
43
44 0, 0, 0, 1
45 };
46 };
47
48};
49
50export namespace helios::math {
51
52 /**
53 * @brief Creates an affine rotation matrix R' = M * R by "baking" the rotation part into the model matrix.
54 * Any operation R' * v will make sure that v is rotated around the local origin, then transformed into
55 * world space.
56 *
57 * @param model
58 * @param radians
59 * @param axis
60 * @return
61 */
62 inline mat4f rotate(const mat4f& model, float radians, const vec3f& axis) noexcept {
63
64
66 std::cos(radians),
67 std::sin(radians),
68 axis.normalize()
69 );
70 }
71
72 /**
73 * @brief Creates an affine transformation matrix M' = M * T by "baking" the translation part into the model
74 * matrix.
75 * Any operation M' * v will make sure that v is translated in local space, then transformed into
76 * world space.
77 *
78 * @param model
79 * @param v
80 * @return
81 */
82 constexpr mat4f translate(const mat4f& model, const vec3f& v) noexcept {
83 mat4 t = model;
84 t(0, 3) += t(0,0) * v[0] + t(0, 1) * v[1] + t(0, 2) * v[2];
85 t(1, 3) += t(1,0) * v[0] + t(1, 1) * v[1] + t(1, 2) * v[2];
86 t(2, 3) += t(2,0) * v[0] + t(2, 1) * v[1] + t(2, 2) * v[2];
87 return t;
88 };
89
90 /**
91 * @brief Creates an affine transformation matrix S' = M * S by ""baking" the scaling part into
92 * the model matrix.
93 * Any operation S' * v will make sure that v is scaled in local space, then transformed into world
94 * space.
95 *
96 * @param model
97 * @param v
98 * @return
99 */
100 constexpr mat4f scale(const mat4f& model, const vec3f& v) noexcept {
101 mat4f t = model;
102
103 t(0, 0) *= v[0]; t(0, 1) *= v[1]; t(0, 2) *= v[2];
104 t(1, 0) *= v[0]; t(1, 1) *= v[1]; t(1, 2) *= v[2];
105 t(2, 0) *= v[0]; t(2, 1) *= v[1]; t(2, 2) *= v[2];
106
107 return t;
108 }
109
110 /**
111 * @brief Creates an affine transformation matrix S' = M * S by "baking" the scaling part into
112 * the model matrix.
113 * Any operation S' * v will make sure that v is scaled in local space, then transformed into world
114 * space.
115 *
116 * @param model
117 * @param scale_by
118 * @return
119 */
120 constexpr mat4f scale(const mat4f& model, const float scale_by) noexcept {
121 mat4f t = model;
122
123 t(0, 0) *= scale_by; t(0, 1) *= scale_by; t(0, 2) *= scale_by;
124 t(1, 0) *= scale_by; t(1, 1) *= scale_by; t(1, 2) *= scale_by;
125 t(2, 0) *= scale_by; t(2, 1) *= scale_by; t(2, 2) *= scale_by;
126
127 return t;
128 }
129
130};

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.