-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform3.cs
527 lines (482 loc) · 15.7 KB
/
Transform3.cs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using System;
using System.Text;
/// <summary>
/// Facilitates 3D affine transformations for entities.
/// </summary>
[Serializable]
public class Transform3
{
/// <summary>
/// The transform's location.
/// </summary>
protected Vec3 location = Vec3.Zero;
/// <summary>
/// The transform's rotation in radians.
/// </summary>
protected Quat rotation = Quat.Identity;
/// <summary>
/// The transform's scale.
/// </summary>
protected Vec3 scale = Vec3.One;
/// <summary>
/// The transform's forward axis.
/// </summary>
/// <value>forward</value>
public Vec3 Forward { get { return this.rotation.Forward; } }
/// <summary>
/// The transform's location.
/// </summary>
/// <value>location</value>
public Vec3 Location
{
get
{
return this.location;
}
set
{
this.location = value;
}
}
/// <summary>
/// The transform's right axis.
/// </summary>
/// <value>right</value>
public Vec3 Right { get { return this.rotation.Right; } }
/// <summary>
/// The transform's rotation in radians.
/// </summary>
/// <value>rotation</value>
public Quat Rotation
{
get
{
return this.rotation;
}
set
{
if (Quat.Any(value)) { this.rotation = value; }
}
}
/// <summary>
/// The transform's scale.
/// </summary>
/// <value>scale</value>
public Vec3 Scale
{
get
{
return this.scale;
}
set
{
if (Vec3.All(value)) { this.scale = value; }
}
}
/// <summary>
/// The transform's up axis.
/// </summary>
/// <value>up</value>
public Vec3 Up { get { return this.rotation.Up; } }
/// <summary>
/// The default constructor.
/// </summary>
public Transform3() { }
/// <summary>
/// Creates a transform from a location, rotation and scale.
/// </summary>
/// <param name="location">location</param>
/// <param name="rotation">rotation</param>
/// <param name="scale">scale</param>
public Transform3(in Vec3 location, in Quat rotation, in Vec3 scale)
{
this.Location = location;
this.Rotation = rotation;
this.Scale = scale;
}
/// <summary>
/// Creates a transform from loose real numbers.
/// </summary>
/// <param name="x">x location</param>
/// <param name="y">y location</param>
/// <param name="z">z location</param>
/// <param name="real">quaternion real</param>
/// <param name="i">x imaginary</param>
/// <param name="j">y imaginary</param>
/// <param name="k">z imaginary</param>
/// <param name="width">x scale</param>
/// <param name="height">y scale</param>
/// <param name="depth">z scale</param>
public Transform3(
in float x, in float y, in float z,
in float real, in float i, in float j, in float k,
in float width, in float height, in float depth)
{
this.Location = new Vec3(x, y, z);
this.Rotation = new Quat(real, new Vec3(i, j, k));
this.Scale = new Vec3(width, height, depth);
}
/// <summary>
/// Returns a hash code representing this transform.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
int hash = Utils.HashBase;
hash = hash * Utils.HashMul ^ this.location.GetHashCode();
hash = hash * Utils.HashMul ^ this.rotation.GetHashCode();
hash = hash * Utils.HashMul ^ this.scale.GetHashCode();
return hash;
}
}
/// <summary>
/// Returns a string representation of this transform.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Transform3.ToString(this);
}
/// <summary>
/// Moves the transform by a direction to a new location in the global
/// coordinate system.
/// </summary>
/// <param name="v">direction</param>
/// <returns>this transform</returns>
public Transform3 MoveByGlobal(in Vec3 v)
{
this.Location += v;
return this;
}
/// <summary>
/// Moves the transform by a direction rotated according to the transform's
/// rotation.
/// </summary>
/// <param name="v">direction</param>
/// <returns>this transform</returns>
public Transform3 MoveByLocal(in Vec3 v)
{
this.Location += Transform3.MulDir(this, v);
return this;
}
/// <summary>
/// Eases the transform to a location by a step.
/// </summary>
/// <param name="v">direction</param>
/// <param name="step">step</param>
/// <returns>this transform</returns>
public Transform3 MoveTo(in Vec3 v, in Vec3 step)
{
this.Location = Vec3.Mix(this.location, v, step);
return this;
}
/// <summary>
/// Eases the transform to a location by a step according to a function.
/// </summary>
/// <param name="v">direction</param>
/// <param name="step">step</param>
/// <param name="easing">easing function</param>
/// <returns>this transform</returns>
public Transform3 MoveTo(
in Vec3 v,
in Vec3 step,
in Func<Vec3, Vec3, Vec3, Vec3> easing)
{
Vec3 t = easing(this.location, v, step);
this.Location = Vec3.Mix(this.location, v, t);
return this;
}
/// <summary>
/// Rotates this transform around an axis by an angle in radians.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this transform</returns>
public Transform3 RotateBy(in float radians, in Vec3 axis)
{
this.rotation *= Quat.FromAxisAngle(radians, axis);
return this;
}
/// <summary>
/// Eases the transform toward a new orientation by a step in [0.0, 1.0] .
/// </summary>
/// <param name="q">orientation</param>
/// <param name="step">step</param>
/// <returns>this transform</returns>
public Transform3 RotateTo(in Quat q, in float step)
{
this.Rotation = Quat.Mix(this.rotation, q, step);
return this;
}
/// <summary>
/// Rotates this transform around the x axis by an angle in radians.
///
/// Beware that using sequences of orthonormal rotations will result in
/// gimbal lock.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this transform</returns>
public Transform3 RotateX(in float radians)
{
this.rotation = Quat.RotateX(this.rotation, radians);
return this;
}
/// <summary>
/// Rotates this transform around the y axis by an angle in radians.
///
/// Beware that using sequences of orthonormal rotations will result in
/// gimbal lock.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this transform</returns>
public Transform3 RotateY(in float radians)
{
this.rotation = Quat.RotateY(this.rotation, radians);
return this;
}
/// <summary>
/// Rotates this transform around the z axis by an angle in radians.
///
/// Beware that using sequences of orthonormal rotations will result in
/// gimbal lock.
/// </summary>
/// <param name="radians">angle</param>
/// <returns>this transform</returns>
public Transform3 RotateZ(in float radians)
{
this.rotation = Quat.RotateZ(this.rotation, radians);
return this;
}
/// <summary>
/// Scales the transform by a uniform scalar, i.e., multiplies the scale by
/// the argument.
/// </summary>
/// <param name="v">uniform scalar</param>
/// <returns>this transform</returns>
public Transform3 ScaleBy(in float v)
{
this.Scale *= v;
return this;
}
/// <summary>
/// Scales the transform by a nonuniform scalar, i.e., multiplies the scale
/// by the argument.
/// </summary>
/// <param name="v">nonuniform scalar</param>
/// <returns>this transform</returns>
public Transform3 ScaleBy(in Vec3 v)
{
this.Scale *= v;
return this;
}
/// <summary>
/// Eases the transform to a scale by a step.
/// </summary>
/// <param name="v">nonuniform scale</param>
/// <param name="step">step</param>
/// <returns>this transform</returns>
public Transform3 ScaleTo(in Vec3 v, in Vec3 step)
{
this.Scale = Vec3.Mix(this.scale, Vec3.CopySign(v, this.scale), step);
return this;
}
/// <summary>
/// Eases the transform to a scale by a step according to a function.
/// </summary>
/// <param name="v">nonuniform scale</param>
/// <param name="step">step</param>
/// <param name="easing">easing function</param>
/// <returns>this transform</returns>
public Transform3 ScaleTo(
in Vec3 v,
in Vec3 step,
in Func<Vec3, Vec3, Vec3, Vec3> easing)
{
Vec3 s = Vec3.CopySign(v, this.scale);
Vec3 t = easing(this.scale, s, step);
this.Scale = Vec3.Mix(this.scale, s, t);
return this;
}
/// <summary>
/// Converts a 2D transform to a 3D transform.
/// </summary>
/// <param name="t">transform</param>
public static implicit operator Transform3(in Transform2 t)
{
return new Transform3(
t.Location,
Quat.FromAngle(t.Rotation),
Vec3.Promote(t.Scale, 1.0f));
}
/// <summary>
/// Multiplies a direction by a transform's inverse. This rotates the
/// direction by the transform's negative angle.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="dir">direction</param>
/// <returns>direction</returns>
public static Vec3 InvMulDir(in Transform3 transform, in Vec3 dir)
{
return Quat.InvMulVector(transform.rotation, dir);
}
/// <summary>
/// Multiplies a normal by a transform's inverse. This rotates the normal
/// by the inverse quaternion, multiplies the normal by the scale,
/// then normalizes.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="normal">normal</param>
/// <returns>normal</returns>
public static Vec3 InvMulNormal(in Transform3 transform, in Vec3 normal)
{
return Vec3.Normalize(transform.scale
* Quat.InvMulVector(transform.rotation, normal));
}
/// <summary>
/// Multiplies a point by a transform's inverse. This subtracts the
/// translation from the point, divides the point by the scale, then rotates
/// by the negative angle.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="point">point</param>
/// <returns>point</returns>
public static Vec3 InvMulPoint(in Transform3 transform, in Vec3 point)
{
return Quat.InvMulVector(transform.rotation,
(point - transform.location) / transform.scale);
}
/// <summary>
/// Multiplies a vector by a transform's inverse. This divides the vector by
/// the scale, then rotates by the negative angle.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="vec">vector</param>
/// <returns>vector</returns>
public static Vec3 InvMulVector(in Transform3 transform, in Vec3 vec)
{
return Quat.InvMulVector(transform.rotation, vec / transform.scale);
}
/// <summary>
/// Returns the maximum dimension occupied by a transform.
/// </summary>
/// <param name="t">transform</param>
/// <returns>maximum</returns>
public static float MaxDimension(in Transform3 t)
{
Vec3 scl = Vec3.Abs(t.scale);
return Utils.Max(scl.X, scl.Y, scl.Z);
}
/// <summary>
/// Returns the minimum dimension occupied by a transform.
/// </summary>
/// <param name="t">transform</param>
/// <returns>minimum</returns>
public static float MinDimension(in Transform3 t)
{
Vec3 scl = Vec3.Abs(t.scale);
return Utils.Min(scl.X, scl.Y, scl.Z);
}
/// <summary>
/// Multiplies a direction by a transform. This rotates the direction by
/// the transform's rotation.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="dir">direction</param>
/// <returns>direction</returns>
public static Vec3 MulDir(in Transform3 transform, in Vec3 dir)
{
return Quat.MulVector(transform.rotation, dir);
}
/// <summary>
/// Multiplies a normal by a transform. This divides the normal by the
/// transform's scale, rotates it by the transform's rotation, then
/// normalizes the result.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="normal">normal</param>
/// <returns>normal</returns>
public static Vec3 MulNormal(in Transform3 transform, in Vec3 normal)
{
return Vec3.Normalize(Quat.MulVector(
transform.rotation, normal / transform.scale));
}
/// <summary>
/// Multiplies a point by a transform. This rotates the point, multiplies
/// the point by the scale, then adds the translation.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="point">point</param>
/// <returns>point</returns>
public static Vec3 MulPoint(in Transform3 transform, in Vec3 point)
{
return transform.location + transform.scale
* Quat.MulVector(transform.rotation, point);
}
/// <summary>
/// Multiplies a vector by a transform. This rotates the vector by the
/// transform's rotation and then multiplies it by the transform's scale.
/// </summary>
/// <param name="transform">transform</param>
/// <param name="vec">vector</param>
/// <returns>vector</returns>
public static Vec3 MulVector(in Transform3 transform, in Vec3 vec)
{
return transform.scale * Quat.MulVector(transform.rotation, vec);
}
/// <summary>
/// Converts a transform to two axes, which in turn may constitute a
/// rotation matrix.
///
/// Returns a named value tuple containing the right, forward and up axes.
/// </summary>
/// <param name="tr">transform</param>
/// <returns>axes</returns>
public static (Vec3 right, Vec3 forward, Vec3 up) ToAxes(in Transform3 tr)
{
return Quat.ToAxes(tr.rotation);
}
/// <summary>
/// Returns a string representation of a transform.
/// </summary>
/// <param name="tr">transform</param>
/// <param name="places">number of decimal places</param>
/// <returns>string</returns>
public static string ToString(in Transform3 tr, in int places = 4)
{
return Transform3.ToString(
new StringBuilder(354), tr, places).ToString();
}
/// <summary>
/// Appends a representation of a transform to a string builder.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="tr">transform</param>
/// <param name="places">number of decimal places</param>
/// <returns>string builder</returns>
public static StringBuilder ToString(
in StringBuilder sb,
in Transform3 tr,
in int places = 4)
{
sb.Append("{\"location\":");
Vec3.ToString(sb, tr.location, places);
sb.Append(",\"rotation\":");
Quat.ToString(sb, tr.rotation, places);
sb.Append(",\"scale\":");
Vec3.ToString(sb, tr.scale, places);
sb.Append('}');
return sb;
}
/// <summary>
/// Returns an identity transform.
/// </summary>
/// <value>identity</value>
public static Transform3 Identity
{
get
{
return new(Vec3.Zero, Quat.Identity, Vec3.One);
}
}
}