forked from grow-graphics/xy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform3d.go
212 lines (193 loc) · 7.73 KB
/
transform3d.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package xy
/*
Transform3D is a 3×4 matrix (3 rows, 4 columns) used for 3D linear transformations. It can represent transformations such as
translation, rotation, and scaling. It consists of a basis (first 3 columns) and a Vector3 for the origin (last column).
*/
type Transform3D struct {
Basis Basis
Origin Vector3
}
// "Constants"
func (Transform3D) IDENTITY() Transform3D {
return Transform3D{
Basis: Basis{Vector3{1, 0, 0}, Vector3{0, 1, 0}, Vector3{0, 0, 1}},
Origin: Vector3{0, 0, 0},
}
}
func (Transform3D) FLIP_X() Transform3D {
return Transform3D{
Basis: Basis{Vector3{-1, 0, 0}, Vector3{0, 1, 0}, Vector3{0, 0, 1}},
Origin: Vector3{0, 0, 0},
}
}
func (Transform3D) FLIP_Y() Transform3D {
return Transform3D{
Basis: Basis{Vector3{1, 0, 0}, Vector3{0, -1, 0}, Vector3{0, 0, 1}},
Origin: Vector3{0, 0, 0},
}
}
func (Transform3D) FLIP_Z() Transform3D {
return Transform3D{
Basis: Basis{Vector3{1, 0, 0}, Vector3{0, 1, 0}, Vector3{0, 0, -1}},
Origin: Vector3{0, 0, 0},
}
}
// "Methods"
// AffineInverse returns the inverse of the transform, under the assumption that the basis is
// invertible (must have non-zero determinant).
func (t Transform3D) AffineInverse() Transform3D { //Transform3D.affine_inverse
var inv = t.Basis.Inverse()
return Transform3D{
Basis: inv,
Origin: inv.Transform(t.Origin.Neg()),
}
}
// InterpolateWith returns a transform interpolated between this transform and another by a given
// weight (on the range of 0.0 to 1.0).
func (t Transform3D) InterpolateWith(other Transform3D, weight float64) Transform3D { //Transform3D.interpolate_with
var (
src_scale = t.Basis.Scale()
src_rot = t.Basis.Quaternion()
src_loc = t.Origin
)
var (
dst_scale = other.Basis.Scale()
dst_rot = other.Basis.Quaternion()
dst_loc = other.Origin
)
return Transform3D{
Basis: newBasisWithQuaternionScale(src_rot.Slerp(dst_rot, weight).Normalized(), src_scale.lerp(dst_scale, weight)),
Origin: src_loc.Lerp(dst_loc, weight),
}
}
// Inverse returns the inverse of the transform, under the assumption that the transformation basis is
// orthonormal (i.e. rotation/reflection is fine, scaling/skew is not). Use affine_inverse for
// non-orthonormal transforms (e.g. with scaling).
func (t Transform3D) Inverse() Transform3D { //Transform3D.inverse
basis := t.Basis.Transposed()
return Transform3D{
Basis: basis,
Origin: basis.Transform(t.Origin.Neg()),
}
}
// IsApproximatelyEqual returns true if this transform and xform are approximately equal, by running
// [IsApproximatelyEqual] on each component.
func (t Transform3D) IsApproximatelyEqual(other Transform3D) bool { //Transform3D.is_equal_approx
return t.Basis.IsApproximatelyEqual(other.Basis) && t.Origin.IsApproximatelyEqual(other.Origin)
}
// IsFinite returns true if this transform is finite, by calling [IsFinite] on each component.
func (t Transform3D) IsFinite() bool { //Transform3D.is_finite
return t.Basis.IsFinite() && t.Origin.IsFinite()
}
// LookingAt returns a copy of the transform rotated such that the forward axis (-Z) points towards the target position.
//
// The up axis (+Y) points as close to the up vector as possible while staying perpendicular to the forward axis. The
// resulting transform is orthonormalized. The existing rotation, scale, and skew information from the original transform
// is discarded. The target and up vectors cannot be zero, cannot be parallel to each other, and are defined in global/parent space.
//
// If use_model_front is true, the +Z axis (asset front) is treated as forward (implies +X is left) and points toward the target
// position. By default, the -Z axis (camera forward) is treated as forward (implies +X is right).
func (t Transform3D) LookingAt(target, up Vector3, use_model_front bool) Transform3D { //Transform3D.looking_at
t.Basis = NewBasisLookingAt(target, up, use_model_front)
return t
}
// Orthonormalized returns the transform with the basis orthogonal (90 degrees), and normalized axis vectors (scale of 1 or -1).
func (t Transform3D) Orthonormalized() Transform3D { //Transform3D.orthonormalized
t.Basis = t.Basis.Orthonormalized()
return t
}
// Rotated returns a copy of the transform rotated around the given axis by the given angle (in radians).
//
// The axis must be a normalized vector.
//
// This method is an optimized version of multiplying the given transform X with a corresponding rotation
// transform R from the left, i.e., R * X.
//
// This can be seen as transforming with respect to the global/parent frame.
func (t Transform3D) Rotated(axis Vector3, phi Radians) Transform3D { //Transform3D.rotated
var basis = NewBasisRotatedAround(axis, phi)
return Transform3D{
Basis: basis.Mul(t.Basis),
Origin: basis.Transform(t.Origin),
}
}
// RotatedLocal returns a copy of the transform rotated around the given axis by the given angle
// (in radians).
//
// The axis must be a normalized vector.
//
// This method is an optimized version of multiplying the given transform X with a corresponding
// rotation transform R from the right, i.e., X * R.
//
// This can be seen as transforming with respect to the local frame.
func (t Transform3D) RotatedLocal(axis Vector3, phi Radians) Transform3D { //Transform3D.rotated_local
var basis = NewBasisRotatedAround(axis, phi)
return Transform3D{
Basis: t.Basis.Mul(basis),
Origin: t.Origin,
}
}
// Scaled returns a copy of the transform scaled by the given scale factor.
//
// This method is an optimized version of multiplying the given transform X with a corresponding
// scaling transform S from the left, i.e., S * X.
//
// This can be seen as transforming with respect to the global/parent frame.
func (t Transform3D) Scaled(scale Vector3) Transform3D { //Transform3D.scaled
var basis = NewBasisScaledBy(scale)
return Transform3D{
Basis: basis.Mul(t.Basis),
Origin: t.Origin.Mul(scale),
}
}
// ScaledLocal returns a copy of the transform scaled by the given scale factor.
//
// This method is an optimized version of multiplying the given transform X with a
// corresponding scaling transform S from the right, i.e., X * S.
//
// This can be seen as transforming with respect to the local frame.
func (t Transform3D) ScaledLocal(scale Vector3) Transform3D { //Transform3D.scaled_local
var basis = NewBasisScaledBy(scale)
return Transform3D{
Basis: t.Basis.Mul(basis),
Origin: t.Origin,
}
}
// Translated returns a copy of the transform translated by the given offset.
//
// This method is an optimized version of multiplying the given transform X with a corresponding
// translation transform T from the left, i.e., T * X.
//
// This can be seen as transforming with respect to the global/parent frame.
func (t Transform3D) Translated(offset Vector3) Transform3D { //Transform3D.translated
return Transform3D{
Basis: t.Basis,
Origin: t.Origin.Add(offset),
}
}
// TranslatedLocal returns a copy of the transform translated by the given offset.
//
// This method is an optimized version of multiplying the given transform X with a
// corresponding translation transform T from the right, i.e., X * T.
//
// This can be seen as transforming with respect to the local frame.
func (t Transform3D) TranslatedLocal(offset Vector3) Transform3D { //Transform3D.translated_local
return Transform3D{
Basis: t.Basis,
Origin: t.Origin.Add(t.Basis.Transform(offset)),
}
}
func (t Transform3D) Projection() Projection { // Projection(Transform3D)
return Projection{
Vector4{t.Basis[0][X], t.Basis[1][X], t.Basis[2][X], 0},
Vector4{t.Basis[0][Y], t.Basis[1][Y], t.Basis[2][Y], 0},
Vector4{t.Basis[0][Z], t.Basis[1][Z], t.Basis[2][Z], 0},
Vector4{t.Origin[X], t.Origin[Y], t.Origin[Z], 1},
}
}
func (t Transform3D) Mul(other Transform3D) Transform3D { //Transform3D * Transform3D
return Transform3D{
Basis: t.Basis.Mul(other.Basis),
Origin: t.Origin.Transform(other),
}
}