Nebula
Loading...
Searching...
No Matches
transform.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10#include "core/types.h"
11#include "math/vec3.h"
12#include "math/quat.h"
13
14//------------------------------------------------------------------------------
15namespace Math
16{
18{
19public:
21 transform();
23 transform(const Math::vec3& pos, const Math::quat& rot = Math::quat(), const Math::vec3& scale = Math::_plus1);
25 void Set(const Math::vec3& pos, const Math::quat& rot, const Math::vec3& scale);
27 transform GetRelative(const transform& child);
28
29 static transform FromMat4(const Math::mat4& mat);
30
34};
35
36//------------------------------------------------------------------------------
39inline transform::transform() : position(0.0f), scale(1.0f)
40{
41 // empty
42}
43
44//------------------------------------------------------------------------------
47inline transform::transform(const Math::vec3& pos, const Math::quat& rot, const Math::vec3& inScale) :
48 rotation(rot),
49 position(pos),
50 scale(inScale)
51{
52 // empty
53}
54
55//------------------------------------------------------------------------------
58inline void
59transform::Set(const Math::vec3& pos, const Math::quat& rot, const Math::vec3& inScale)
60{
61 this->rotation = rot;
62 this->position = pos;
63 this->position = inScale;
64}
65
66
67//------------------------------------------------------------------------------
70inline transform
72{
73 // this has many weird edge cases with scale, esp. negative
74 n_assert2(greater_all(child.scale, _zero) && greater_all(scale, _zero), "transform currently does not support negative scale");
75 transform result;
76 const vec3 invChildScale = reciprocal(child.scale);
77 result.scale = multiply(scale, invChildScale);
78 quat invChildRot = inverse(child.rotation);
79 result.rotation = rotation * invChildRot;
80 result.position = multiply(rotate(result.rotation, (position - child.position)), invChildScale);
81 return result;
82}
83
84//------------------------------------------------------------------------------
87inline transform
88operator*(const transform& a, const transform& b)
89{
90 // this has many weird edge cases with scale, esp. negative
91 n_assert2(greater_all(a.scale, _zero) && greater_all(b.scale, _zero), "transform currently does not support negative scale");
92 transform result;
93 result.rotation = a.rotation * b.rotation;
94 result.scale = multiply(a.scale, b.scale);
95 result.position = rotate(b.rotation, multiply(b.scale, a.position)) + b.position;
96 return result;
97}
98
99//------------------------------------------------------------------------------
102inline transform
103lerp(const transform& a, const transform& b, float t)
104{
105 return transform(lerp(a.position, b.position, t), normalize(slerp(a.rotation, b.rotation, t)), lerp(a.scale, b.scale, t));
106}
107
108//------------------------------------------------------------------------------
111inline point
112operator*(const transform& a, const point& b)
113{
114 return rotate(a.rotation, multiply(a.scale, b.vec)) + a.position;
115}
116
117//------------------------------------------------------------------------------
120inline vector
121operator*(const transform& a, const vector& b)
122{
123 return rotate(a.rotation, multiply(a.scale, b.vec));
124}
125
126
127//------------------------------------------------------------------------------
130inline transform
132{
133 transform trans;
134 Math::decompose(mat, trans.scale, trans.rotation, trans.position);
135 return trans;
136}
137
138}
Simple transform using position, quaternion, and scale.
Definition transform.h:18
Math::vec3 position
Definition transform.h:31
transform GetRelative(const transform &child)
get the relative transform from this to child, expressed as matrix result = this * inv(child)
Definition transform.h:71
Math::vec3 scale
Definition transform.h:33
Math::quat rotation
Definition transform.h:32
transform()
default constructor
Definition transform.h:39
void Set(const Math::vec3 &pos, const Math::quat &rot, const Math::vec3 &scale)
set content
Definition transform.h:59
static transform FromMat4(const Math::mat4 &mat)
transforms a vector (ignores position)
Definition transform.h:131
#define n_assert2(exp, msg)
Definition debug.h:51
Different curves.
Definition angularpfeedbackloop.h:17
static const __m128 _zero
Definition vec3.h:35
__forceinline vec3 multiply(const vec3 &v0, const vec3 &v1)
Definition vec3.h:375
quat slerp(const quat &q1, const quat &q2, scalar t)
quat slerp TODO: rewrite using sse/avx
Definition quat.h:463
__forceinline vec3 reciprocal(const vec3 &v)
Definition vec3.h:357
void decompose(const mat4 &mat, vec3 &outScale, quat &outRotation, vec3 &outTranslation)
Definition mat4.cc:50
__forceinline plane normalize(const plane &p)
Definition plane.h:255
__forceinline float lerp(float x, float y, float l)
Linearly interpolate between 2 values: ret = x + l * (y - x)
Definition scalar.h:597
half operator*(half one, half two)
Definition half.h:123
__forceinline bool greater_all(const point &v0, const point &v1)
Definition point.h:441
vec3 rotate(quat const &q, vec3 const &v)
Rotate a vector by a quaternion.
Definition quat.h:547
static const __m128 _plus1
Definition vec3.h:34
__forceinline mat4 inverse(const mat4 &m)
Definition mat4.h:429
A 4x4 single point precision float matrix.
Definition mat4.h:49
Represents a 3D point in space.
Definition point.h:22
__m128 vec
Definition point.h:75
A quaternion is usually used to represent an orientation in 3D space.
Definition quat.h:30
A 3D vector.
Definition vec3.h:40
A vector is a 3D direction in space.
Definition vector.h:22
__m128 vec
Definition vector.h:82