Enzo
Loading...
Searching...
No Matches
Transform.h
1#pragma once
2
3#include "Engine/Core/Types.h"
4#include <Eigen/Geometry>
5#include <numbers>
6#include <string_view>
7
8namespace enzo::attr {
9class Attribute;
10}
11
12namespace enzo {
13
20enum class TransformOrder : uint8_t
21{
22 SRT,
23 STR,
24 RST,
25 RTS,
26 TSR,
27 TRS
28};
29
34TransformOrder getTransformOrder(std::string_view name);
35
49class Transform
50{
51 public:
52 using Affine = Eigen::Transform<floatT, 3, Eigen::Affine>;
53
55 Transform() : xform_(Affine::Identity()) {}
56
58 explicit Transform(const Matrix4& matrix) : xform_(matrix) {}
59
60 // Factories
61
69 static Transform fromAttribute(const attr::Attribute& attribute, Offset offset);
70
79 static Transform lookAt(const Vector3& translation, const Vector3& forward, const Vector3& up);
80
88 const Vector3& translation,
89 const Vector3& rotationDegrees,
90 const Vector3& scale,
91 TransformOrder order = TransformOrder::SRT
92 );
93
94 // Builders compose in the current local frame, so the first call is applied
95 // to a point first and later calls wrap it.
96
98 Transform& translate(const Vector3& offset)
99 {
100 xform_.translate(offset);
101 return *this;
102 }
103
105 Transform& rotateEuler(const Vector3& degrees)
106 {
107 const Vector3 radians = degrees * (std::numbers::pi / 180.0);
108 xform_.rotate(
109 Eigen::AngleAxis<floatT>(radians.z(), Vector3::UnitZ()) *
110 Eigen::AngleAxis<floatT>(radians.y(), Vector3::UnitY()) *
111 Eigen::AngleAxis<floatT>(radians.x(), Vector3::UnitX())
112 );
113 return *this;
114 }
115
117 Transform& scale(const Vector3& factors)
118 {
119 xform_.scale(factors);
120 return *this;
121 }
122
124 Transform& scale(floatT uniform)
125 {
126 xform_.scale(uniform);
127 return *this;
128 }
129
132 {
133 xform_ = xform_ * other.xform_;
134 return *this;
135 }
136
141 Vector3 operator*(const Vector3& point) const { return xform_ * point; }
142
143 const Matrix4& getMatrix() const { return xform_.matrix(); }
144
145 private:
146 Affine xform_;
147};
148
149} // namespace enzo
Basic attribute, parameter, and node types for Enzo.
size_t Offset
enzo::Offset is the internal discontinuous index of an element in a given AttributeOwner.
Definition Types.h:181
An affine transform that composes translation, rotation, and scale.
Transform & rotateEuler(const Vector3 &degrees)
Rotates in X, then Y, then Z, with angles given in degrees.
Definition Transform.h:105
Transform & scale(const Vector3 &factors)
Scales each local axis independently.
Definition Transform.h:117
Transform & compose(const Transform &other)
Composes another transform into this one in the local frame.
Definition Transform.h:131
Transform & translate(const Vector3 &offset)
Moves the local origin by an offset.
Definition Transform.h:98
static Transform fromComponents(const Vector3 &translation, const Vector3 &rotationDegrees, const Vector3 &scale, TransformOrder order=TransformOrder::SRT)
Builds a transform from a translation, a rotation, and a scale.
Definition Transform.cpp:33
Vector3 operator*(const Vector3 &point) const
Applies the transform to a point.
Definition Transform.h:141
Transform()
Constructs an identity transform.
Definition Transform.h:55
static Transform lookAt(const Vector3 &translation, const Vector3 &forward, const Vector3 &up)
Builds an orientation frame that points down a forward axis.
Definition Transform.cpp:68
Transform(const Matrix4 &matrix)
Wraps an existing matrix.
Definition Transform.h:58
static Transform fromAttribute(const attr::Attribute &attribute, Offset offset)
Reads a transform from a single attribute value.
Definition Transform.cpp:16
Transform & scale(floatT uniform)
Scales all local axes by the same factor.
Definition Transform.h:124
Containers for geometry data.