-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec3.cs
2152 lines (1971 loc) · 65.2 KB
/
Vec3.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A readonly struct influenced by GLSL and OSL. This is intended
/// for storing points and directions in three-dimensional graphics
/// programs.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit)]
public readonly struct Vec3 : IComparable<Vec3>, IEquatable<Vec3>, IEnumerable
{
/// <summary>
/// Component on the x axis in the Cartesian coordinate system.
/// </summary>
[FieldOffset(0)] private readonly float x;
/// <summary>
/// Component on the y axis in the Cartesian coordinate system.
/// </summary>
[FieldOffset(4)] private readonly float y;
/// <summary>
/// Component on the z axis in the Cartesian coordinate system.
/// </summary>
[FieldOffset(8)] private readonly float z;
/// <summary>
/// The number of values (dimensions) in this vector.
/// </summary>
/// <value>length</value>
public int Length { get { return 3; } }
/// <summary>
/// Component on the z axis in the Cartesian coordinate system.
/// </summary>
/// <value>x</value>
public float X { get { return this.x; } }
/// <summary>
/// Component on the y axis in the Cartesian coordinate system.
/// </summary>
/// <value>y</value>
public float Y { get { return this.y; } }
/// <summary>
/// Component on the z axis in the Cartesian coordinate system.
/// </summary>
/// <value>z</value>
public float Z { get { return this.z; } }
/// <summary>
/// Gets the x and y components as a 2D vector.
/// </summary>
/// <value>2D vector</value>
public Vec2 XY
{
get
{
return new Vec2(this.x, this.y);
}
}
/// <summary>
/// Gets the x and z components as a 2D vector.
/// </summary>
/// <value>2D vector</value>
public Vec2 XZ
{
get
{
return new Vec2(this.x, this.z);
}
}
/// <summary>
/// Retrieves a component by index. When the provided index is 2 or -1,
/// returns z; 1 or -2, y; 0 or -3, x.
/// </summary>
/// <value>the component</value>
public float this[int i]
{
get
{
return i switch
{
0 or -3 => this.x,
1 or -2 => this.y,
2 or -1 => this.z,
_ => 0.0f,
};
}
}
/// <summary>
/// Constructs a vector from single precision real numbers.
/// </summary>
/// <param name="x">x component</param>
/// <param name="y">y component</param>
/// <param name="z">z component</param>
public Vec3(in float x, in float y, in float z)
{
this.x = x;
this.y = y;
this.z = z;
}
/// <summary>
/// Constructs a vector from boolean values, where true is 1.0 and false is
/// 0.0 .
/// </summary>
/// <param name="x">x component</param>
/// <param name="y">y component</param>
/// <param name="z">z component</param>
public Vec3(in bool x, in bool y, in bool z)
{
this.x = x ? 1.0f : 0.0f;
this.y = y ? 1.0f : 0.0f;
this.z = z ? 1.0f : 0.0f;
}
/// <summary>
/// Tests this vector for equivalence with an object. For approximate
/// equality with another vector, use the static approx function instead.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Vec3 vec) { return this.Equals(vec); }
return false;
}
/// <summary>
/// Returns a hash code representing this vector.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
return ((Utils.MulBase ^ this.x.GetHashCode()) *
Utils.HashMul ^ this.y.GetHashCode()) *
Utils.HashMul ^ this.z.GetHashCode();
}
}
/// <summary>
/// Returns a string representation of this vector.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Vec3.ToString(this);
}
/// <summary>
/// Compares this vector to another.
/// Returns 1 when a component of this vector is greater than
/// another; -1 when lesser. Returns 0 as a last resort.
/// Prioritizes the highest dimension first: z, y, x.
/// </summary>
/// <param name="v">the comparisand</param>
/// <returns>evaluation</returns>
public int CompareTo(Vec3 v)
{
return (this.z < v.z) ? -1 :
(this.z > v.z) ? 1 :
(this.y < v.y) ? -1 :
(this.y > v.y) ? 1 :
(this.x < v.x) ? -1 :
(this.x > v.x) ? 1 :
0;
}
/// <summary>
/// Tests this vector for equivalence with another in compliance with the
/// IEquatable interface. For approximate equality with another vector, use
/// the static approx function instead.
/// </summary>
/// <param name="v">vector</param>
/// <returns>equivalence</returns>
public bool Equals(Vec3 v)
{
if (this.z.GetHashCode() != v.z.GetHashCode()) { return false; }
if (this.y.GetHashCode() != v.y.GetHashCode()) { return false; }
if (this.x.GetHashCode() != v.x.GetHashCode()) { return false; }
return true;
}
/// <summary>
/// Returns an enumerator (or iterator) for this vector, allowing its
/// components to be accessed in a foreach loop.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator GetEnumerator()
{
yield return this.x;
yield return this.y;
yield return this.z;
}
/// <summary>
/// Converts a boolean to a vector by supplying the boolean to all the
/// vector's components: 1.0 for true; 0.0 for false.
/// </summary>
/// <param name="b">boolean</param>
public static implicit operator Vec3(in bool b)
{
float eval = b ? 1.0f : 0.0f;
return new(eval, eval, eval);
}
/// <summary>
/// Converts a float to a vector by supplying the scalar to all the vector's
/// components.
/// </summary>
/// <param name="s">scalar</param>
public static implicit operator Vec3(in float s)
{
return new(s, s, s);
}
/// <summary>
/// Promotes a 2D vector to a 3D vector; the z component is assumed to be
/// 0.0 .
/// </summary>
/// <param name="v">2D vector</param>
public static implicit operator Vec3(in Vec2 v)
{
return Vec3.Promote(v, 0.0f);
}
/// <summary>
/// Converts a vector to a boolean by finding whether all of its components
/// are non-zero.
/// </summary>
/// <param name="v">vector</param>
public static explicit operator bool(in Vec3 v)
{
return Vec3.All(v);
}
/// <summary>
/// A vector evaluates to true when all of its components are not equal to
/// zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool operator true(in Vec3 v)
{
return Vec3.All(v);
}
/// <summary>
/// A vector evaluates to false when all of its components are equal to
/// zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool operator false(in Vec3 v)
{
return Vec3.None(v);
}
/// <summary>
/// Evaluates a vector as a boolean. Equivalent to using the ! operator.
/// </summary>
/// <param name="v">vector</param>
/// <returns>opposite</returns>
public static Vec3 operator !(in Vec3 v)
{
return Vec3.Not(v);
}
/// <summary>
/// Evaluates a vector as a boolean. Equivalent to using the ~ operator.
/// </summary>
/// <param name="v">vector</param>
/// <returns>complement</returns>
public static Vec3 operator ~(in Vec3 v)
{
return Vec3.Not(v);
}
/// <summary>
/// Evaluates two vectors like booleans, using the inclusive and (AND) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec3 operator &(in Vec3 a, in Vec3 b)
{
return new(
Utils.And(a.x, b.x),
Utils.And(a.y, b.y),
Utils.And(a.z, b.z));
}
/// <summary>
/// Evaluates two vectors like booleans, using the inclusive or (OR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec3 operator |(in Vec3 a, in Vec3 b)
{
return new(
Utils.Or(a.x, b.x),
Utils.Or(a.y, b.y),
Utils.Or(a.z, b.z));
}
/// <summary>
/// Evaluates two vectors like booleans, using the exclusive or (XOR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Vec3 operator ^(in Vec3 a, in Vec3 b)
{
return new(
Utils.Xor(a.x, b.x),
Utils.Xor(a.y, b.y),
Utils.Xor(a.z, b.z));
}
/// <summary>
/// Negates the vector.
/// </summary>
/// <param name="v">vector</param>
/// <returns>negation</returns>
public static Vec3 operator -(in Vec3 v)
{
return new(-v.x, -v.y, -v.z);
}
/// <summary>
/// Increments all components of a vector by 1.
/// </summary>
/// <param name="v">vector</param>
/// <returns>increment</returns>
public static Vec3 operator ++(in Vec3 v)
{
return new(v.x + 1.0f, v.y + 1.0f, v.z + 1.0f);
}
/// <summary>
/// Decrements all components of a vector by 1.
/// </summary>
/// <param name="v">vector</param>
/// <returns>decrement</returns>
public static Vec3 operator --(in Vec3 v)
{
return new(v.x - 1.0f, v.y - 1.0f, v.z - 1.0f);
}
/// <summary>
/// Multiplies two vectors, component-wise, i.e.,
/// returns the Hadamard product.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Vec3 operator *(in Vec3 a, in Vec3 b)
{
return new(
a.x * b.x,
a.y * b.y,
a.z * b.z);
}
/// <summary>
/// Multiplies a vector by a scalar.
/// </summary>
/// <param name="a">left operand, the vector</param>
/// <param name="b">right operand, the scalar</param>
/// <returns>product</returns>
public static Vec3 operator *(in Vec3 a, in float b)
{
return new(
a.x * b,
a.y * b,
a.z * b);
}
/// <summary>
/// Multiplies a vector by a scalar.
/// </summary>
/// <param name="a">left operand, the scalar</param>
/// <param name="b">right operand, the vector</param>
/// <returns>product</returns>
public static Vec3 operator *(in float a, in Vec3 b)
{
return new(
a * b.x,
a * b.y,
a * b.z);
}
/// <summary>
/// Divides the left operand by the right, component-wise.
/// </summary>
/// <param name="a">numerator</param>
/// <param name="b">denominator</param>
/// <returns>quotient</returns>
public static Vec3 operator /(in Vec3 a, in Vec3 b)
{
return new(
Utils.Div(a.x, b.x),
Utils.Div(a.y, b.y),
Utils.Div(a.z, b.z));
}
/// <summary>
/// Divides a vector by a scalar.
/// </summary>
/// <param name="a">vector, numerator</param>
/// <param name="b">scalar, denominator</param>
/// <returns>quotient</returns>
public static Vec3 operator /(in Vec3 a, in float b)
{
if (b != 0.0f)
{
float bInv = 1.0f / b;
return new(
a.x * bInv,
a.y * bInv,
a.z * bInv);
}
return Vec3.Zero;
}
/// <summary>
/// Divides a scalar by a vector.
/// </summary>
/// <param name="a">scalar, numerator</param>
/// <param name="b">vector, denominator</param>
/// <returns>quotient</returns>
public static Vec3 operator /(in float a, in Vec3 b)
{
return new(
Utils.Div(a, b.x),
Utils.Div(a, b.y),
Utils.Div(a, b.z));
}
/// <summary>
/// Applies truncation-based modulo to the left and right vectors.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec3 operator %(in Vec3 a, in Vec3 b)
{
return Vec3.RemTrunc(a, b);
}
/// <summary>
/// Applies truncation-based modulo to the left and right operands.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec3 operator %(in Vec3 a, in float b)
{
if (b != 0.0f)
{
return new(
a.x % b,
a.y % b,
a.z % b);
}
return a;
}
/// <summary>
/// Applies truncation-based modulo to the left and right operands.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Vec3 operator %(in float a, in Vec3 b)
{
return new(
Utils.RemTrunc(a, b.x),
Utils.RemTrunc(a, b.y),
Utils.RemTrunc(a, b.z));
}
/// <summary>
/// Adds two vectors together.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Vec3 operator +(in Vec3 a, in Vec3 b)
{
return new(
a.x + b.x,
a.y + b.y,
a.z + b.z);
}
/// <summary>
/// Subtracts the right vector from the left vector.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>difference</returns>
public static Vec3 operator -(in Vec3 a, in Vec3 b)
{
return new(
a.x - b.x,
a.y - b.y,
a.z - b.z);
}
/// <summary>
/// Evaluates whether the left comparisand is less than the right
/// comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator <(in Vec3 a, in Vec3 b)
{
return new(
a.x < b.x,
a.y < b.y,
a.z < b.z);
}
/// <summary>
/// Evaluates whether the left comparisand is greater than the right
/// comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator >(in Vec3 a, in Vec3 b)
{
return new(
a.x > b.x,
a.y > b.y,
a.z > b.z);
}
/// <summary>
/// Evaluates whether the left comparisand is less than or equal to the
/// right comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator <=(in Vec3 a, in Vec3 b)
{
return new(
a.x <= b.x,
a.y <= b.y,
a.z <= b.z);
}
/// <summary>
/// Evaluates whether the left comparisand is greater than or equal to the
/// right comparisand.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator >=(in Vec3 a, in Vec3 b)
{
return new(
a.x >= b.x,
a.y >= b.y,
a.z >= b.z);
}
/// <summary>
/// Evaluates whether two vectors are not equal to each other.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator !=(in Vec3 a, in Vec3 b)
{
return new(
a.x != b.x,
a.y != b.y,
a.z != b.z);
}
/// <summary>
/// Evaluates whether two vectors are equal to each other.
///
/// The return type is not a boolean, but a vector, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Vec3 operator ==(in Vec3 a, in Vec3 b)
{
return new(
a.x == b.x,
a.y == b.y,
a.z == b.z);
}
/// <summary>
/// Finds the absolute value of each vector component.
/// </summary>
/// <param name="v">vector</param>
/// <returns>absolute vector</returns>
public static Vec3 Abs(in Vec3 v)
{
return new(
MathF.Abs(v.x),
MathF.Abs(v.y),
MathF.Abs(v.z));
}
/// <summary>
/// Tests to see if all the vector's components are non-zero. Useful when
/// testing valid dimensions (width and depth) stored in vectors.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool All(in Vec3 v)
{
return v.x != 0.0f && v.y != 0.0f && v.z != 0.0f;
}
/// <summary>
/// Finds the angle between two vectors.
/// </summary>
/// <param name="a">the first vector</param>
/// <param name="b">the second vector</param>
/// <returns>angle</returns>
public static float AngleBetween(in Vec3 a, in Vec3 b)
{
// Double precision is required for accurate angle distance.
if (Vec3.Any(a) && Vec3.Any(b))
{
double ax = a.x;
double ay = a.y;
double az = a.z;
double bx = b.x;
double by = b.y;
double bz = b.z;
return (float)Math.Acos(
(ax * bx + ay * by + az * bz) /
(Math.Sqrt(ax * ax + ay * ay + az * az) *
Math.Sqrt(bx * bx + by * by + bz * bz)));
}
return 0.0f;
}
/// <summary>
/// Tests to see if any of the vector's components are non-zero.
/// </summary>
/// <param name="v">vector</param>
/// <returns>evaluation</returns>
public static bool Any(in Vec3 v)
{
return v.x != 0.0f || v.y != 0.0f || v.z != 0.0f;
}
/// <summary>
/// Appends a vector to a one-dimensional vector array. Returns a new array.
/// </summary>
/// <param name="a">array</param>
/// <param name="b">vector</param>
/// <returns>array</returns>
public static Vec3[] Append(in Vec3[] a, in Vec3 b)
{
bool aNull = a == null;
if (aNull) { return new Vec3[] { b }; }
int aLen = a.Length;
Vec3[] result = new Vec3[aLen + 1];
System.Array.Copy(a, 0, result, 0, aLen);
result[aLen] = b;
return result;
}
/// <summary>
/// Tests to see if two vectors approximate each other.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <param name="tol">tolerance</param>
/// <returns>evaluation</returns>
public static bool Approx(
in Vec3 a, in Vec3 b,
in float tol = Utils.Epsilon)
{
return Utils.Approx(a.x, b.x, tol) &&
Utils.Approx(a.y, b.y, tol) &&
Utils.Approx(a.z, b.z, tol);
}
/// <summary>
/// Finds the vector's azimuth in the range [-PI, PI] .
/// </summary>
/// <param name="v">vector</param>
/// <returns>angle in radians</returns>
public static float AzimuthSigned(in Vec3 v)
{
return MathF.Atan2(v.y, v.x);
}
/// <summary>
/// Finds the vector's azimuth in the range [0.0, TAU] .
/// </summary>
/// <param name="v">vector</param>
/// <returns>angle in radians</returns>
public static float AzimuthUnsigned(in Vec3 v)
{
float h = Vec3.AzimuthSigned(v);
return h < -0.0f ? h + Utils.Tau : h;
}
/// <summary>
/// Returns a point on a Bezier curve described by two anchor points and two
/// control points according to a step in [0.0, 1.0] .
///
/// When the step is less than zero, returns the first anchor point. When
/// the step is greater than one, returns the second anchor point.
/// </summary>
/// <param name="ap0">first anchor point</param>
/// <param name="cp0">first control point</param>
/// <param name="cp1">second control point</param>
/// <param name="ap1">second anchor point</param>
/// <param name="step">step</param>
/// <returns>point along the curve</returns>
public static Vec3 BezierPoint(
in Vec3 ap0,
in Vec3 cp0,
in Vec3 cp1,
in Vec3 ap1,
in float step)
{
if (step <= 0.0f) { return ap0; }
else if (step >= 1.0f) { return ap1; }
float u = 1.0f - step;
float tcb = step * step;
float ucb = u * u;
float usq3t = ucb * (step + step + step);
float tsq3u = tcb * (u + u + u);
ucb *= u;
tcb *= step;
return new(
ap0.x * ucb +
cp0.x * usq3t +
cp1.x * tsq3u +
ap1.x * tcb,
ap0.y * ucb +
cp0.y * usq3t +
cp1.y * tsq3u +
ap1.y * tcb,
ap0.z * ucb +
cp0.z * usq3t +
cp1.z * tsq3u +
ap1.z * tcb);
}
/// <summary>
/// Returns a tangent on a Bezier curve described by two anchor points and
/// two control points according to a step in [0.0, 1.0] .
///
/// When the step is less than zero, returns the first anchor point
/// subtracted from the first control point. When the step is greater than
/// one, returns the second anchor point subtracted from the second control
/// point.
/// </summary>
/// <param name="ap0">first anchor point</param>
/// <param name="cp0">first control point</param>
/// <param name="cp1">second control point</param>
/// <param name="ap1">second anchor point</param>
/// <param name="step">step</param>
/// <returns>tangent along the curve</returns>
public static Vec3 BezierTangent(
in Vec3 ap0,
in Vec3 cp0,
in Vec3 cp1,
in Vec3 ap1,
in float step)
{
if (step <= 0.0f) { return cp0 - ap0; }
else if (step >= 1.0f) { return ap1 - cp1; }
float u = 1.0f - step;
float t3 = step + step + step;
float usq3 = u * (u + u + u);
float tsq3 = step * t3;
float ut6 = u * (t3 + t3);
return new(
(cp0.x - ap0.x) * usq3 +
(cp1.x - cp0.x) * ut6 +
(ap1.x - cp1.x) * tsq3,
(cp0.y - ap0.y) * usq3 +
(cp1.y - cp0.y) * ut6 +
(ap1.y - cp1.y) * tsq3,
(cp0.z - ap0.z) * usq3 +
(cp1.z - cp0.z) * ut6 +
(ap1.z - cp1.z) * tsq3);
}
/// <summary>
/// Returns a normalized tangent on a Bezier curve.
/// </summary>
/// <param name="ap0">first anchor point</param>
/// <param name="cp0">first control point</param>
/// <param name="cp1">second control point</param>
/// <param name="ap1">second anchor point</param>
/// <param name="step">step</param>
/// <returns>tangent along the curve</returns>
public static Vec3 BezierTanUnit(
in Vec3 ap0,
in Vec3 cp0,
in Vec3 cp1,
in Vec3 ap1,
in float step)
{
return Vec3.Normalize(Vec3.BezierTangent(
ap0, cp0, cp1, ap1, step));
}
/// <summary>
/// Raises each component of the vector to the nearest greater integer.
/// </summary>
/// <param name="v">vector</param>
/// <returns>result</returns>
public static Vec3 Ceil(in Vec3 v)
{
return new(
MathF.Ceiling(v.x),
MathF.Ceiling(v.y),
MathF.Ceiling(v.z));
}
/// <summary>
/// Clamps a vector to a range within the lower and upper bound.
/// </summary>
/// <param name="v">vector</param>
/// <param name="lb">range lower bound</param>
/// <param name="ub">range upper bound</param>
/// <returns>clamped vector</returns>
public static Vec3 Clamp(in Vec3 v, in float lb = 0.0f, in float ub = 1.0f)
{
return new(
Utils.Clamp(v.x, lb, ub),
Utils.Clamp(v.y, lb, ub),
Utils.Clamp(v.z, lb, ub));
}
/// <summary>
/// Concatenates two one-dimensional Vec2 arrays.
/// </summary>
/// <param name="a">left array</param>
/// <param name="b">right array</param>
/// <returns>concatenation</returns>
public static Vec3[] Concat(in Vec3[] a, in Vec3[] b)
{
bool aNull = a == null;
bool bNull = b == null;
if (aNull && bNull) { return new Vec3[] { }; }
if (aNull)
{
Vec3[] result0 = new Vec3[b.Length];
System.Array.Copy(b, 0, result0, 0, b.Length);
return result0;
}
if (bNull)
{
Vec3[] result1 = new Vec3[a.Length];
System.Array.Copy(a, 0, result1, 0, a.Length);
return result1;
}
int aLen = a.Length;
int bLen = b.Length;
Vec3[] result2 = new Vec3[aLen + bLen];
System.Array.Copy(a, 0, result2, 0, aLen);
System.Array.Copy(b, 0, result2, aLen, bLen);
return result2;
}
/// <summary>
/// Tests to see if the vector contains a value
/// </summary>
/// <param name="a">vector</param>
/// <param name="b">value</param>
/// <returns>evaluation</returns>
public static bool Contains(in Vec3 a, in float b)
{
return Utils.Approx(a.x, b) ||
Utils.Approx(a.y, b) ||
Utils.Approx(a.z, b);
}
/// <summary>
/// Returns the first vector with the sign of the second.
/// Returns zero where the sign is zero.
/// </summary>
/// <param name="a">magnitude</param>
/// <param name="b">sign</param>
/// <returns>signed vector</returns>
public static Vec3 CopySign(in Vec3 a, in Vec3 b)
{
return new(
Utils.CopySign(a.x, b.x),
Utils.CopySign(a.y, b.y),
Utils.CopySign(a.z, b.z));
}
/// <summary>
/// The cross product returns a vector perpendicular to both a and b, and
/// therefore normal to the plane on which a and b rest. The cross product
/// is anti-commutative, meaning a x b = - ( b x a ) . A unit vector does
/// not necessarily result from the cross of two unit vectors.
///
/// Crossed orthonormal vectors are as follows:
///
/// right x forward = up,
///
/// ( 1.0, 0.0, 0.0 ) x ( 0.0, 1.0, 0.0 ) = ( 0.0, 0.0, 1.0 )
///
/// forward x up = right,
///
/// ( 0.0, 1.0, 0.0 ) x ( 0.0, 0.0, 1.0 ) = ( 1.0, 0.0, 0.0 )
///
/// up x right = forward,
///
/// ( 0.0, 0.0, 1.0 ) x ( 1.0, 0.0, 0.0 ) = ( 0.0, 1.0, 0.0 )
///
/// The 3D equivalent to the 2D vector's perpendicular.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>cross product</returns>
public static Vec3 Cross(in Vec3 a, in Vec3 b)
{
return new(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
/// <summary>
/// Finds the absolute value of the difference between two vectors.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>absolute difference</returns>
public static Vec3 Diff(in Vec3 a, in Vec3 b)
{
return new(
Utils.Diff(b.x, a.x),
Utils.Diff(b.y, a.y),
Utils.Diff(b.z, a.z));
}
/// <summary>
/// Finds the Chebyshev distance between two vectors. Forms a cube pattern
/// when plotted.