-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_extensions.hpp
1133 lines (1005 loc) · 40 KB
/
vector_extensions.hpp
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
/// @ref gtx_vector_extensions
/// @file vector_extensions.hpp
///
/// @defgroup gtx_vector_extensions GLM_GTX_vector_extensions
/// @ingroup gtx
///
/// GLM vector extensions
/// 1. API unifying functions, usually handling functions without genType or vec<1, genType> declarations
/// 2. Support for C99/C++11 math functions
/// 3. Functions emulated/ported from other popular vector-math libraries
///
/// @see core_func_common
/// @see ext_vector_common
#pragma once
#if !defined(GLM_ENABLE_EXPERIMENTAL)
#define GLM_ENABLE_EXPERIMENTAL
#endif
#include <limits>
#include <type_traits>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <glm/gtc/random.hpp>
#include <glm/gtx/compatibility.hpp>
#include <glm/gtx/extended_min_max.hpp>
#include <glm/gtx/orthonormalize.hpp>
#include <glm/gtx/projection.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <glm/gtx/vector_query.hpp>
#include <glm/ext/scalar_common.hpp>
#include <glm/ext/scalar_constants.hpp>
#include <glm/ext/quaternion_trigonometric.hpp>
#include "scalar_extensions.hpp"
#if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
#pragma message("GLM: GLM_GTX_vector_ext extension included")
#endif
namespace glm {
/// Controlled by compilation flags:
/// - GLM_FORCE_LEFT_HANDED
/// - GLM_FORCE_Z_UP
///
/// @addtogroup gtx_handed_coordinate_space
/// @{
/// Return the direction vector representing the "right" to an actor in world space.
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> right() {
return vec<3, T, Q>(T(1), T(0), T(0));
}
/// Return the direction vector representing "up" to an actor in world space.
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> up() {
return vec<3, T, Q>(
#if defined(GLM_FORCE_Z_UP)
T(0), T(0), T(1)
#else
T(0), T(1), T(0)
#endif
);
}
/// Return the direction vector representing "forward" to an actor in left-handed world space.
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> forwardLH() {
return vec<3, T, Q>(
#if defined(GLM_FORCE_Z_UP)
T(0), T(-1), T(0)
#else
T(0), T(0), T(1)
#endif
);
}
/// Return the direction vector representing "forward" to an actor in right-handed world space.
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> forwardRH() {
return vec<3, T, Q>(
#if defined(GLM_FORCE_Z_UP)
T(0), T(1), T(0)
#else
T(0), T(0), T(-1)
#endif
);
}
/// Return the direction vector representing "forward" to an actor in world space.
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> forward() {
#if defined(GLM_FORCE_LEFT_HANDED)
return forwardLH<T, Q>();
#else
return forwardRH<T, Q>();
#endif
}
/// @}
/// @addtogroup gtx_vector_extensions
/// @{
/// <summary>
/// all(equal(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_equal(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return all(equal(x, y));
}
/// <summary>
/// all(equal(x, y, eps)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_equal(vec<L, T, Q> const &x, vec<L, T, Q> const &y, T eps) {
return all(equal(x, y, eps));
}
/// <summary>
/// all(equal(x, y, MaxULPs)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_equal(vec<L, T, Q> const &x, vec<L, T, Q> const &y, int MaxULPs) {
return all(equal(x, y, MaxULPs));
}
/// <summary>
/// all(equal(x, y, eps)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_equal(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, T, Q> const &eps) {
return all(equal(x, y, eps));
}
/// <summary>
/// all(equal(x, y, MaxULPs)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_equal(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, int, Q> const &MaxULPs) {
return all(equal(x, y, MaxULPs));
}
/// <summary>
/// any(notEqual(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_notEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return any(notEqual(x, y));
}
/// <summary>
/// any(notEqual(x, y, eps)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_notEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y, T eps) {
return any(notEqual(x, y, eps));
}
/// <summary>
/// any(notEqual(x, y, MaxULPs)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_notEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y, int MaxULPs) {
return any(notEqual(x, y, MaxULPs));
}
/// <summary>
/// any(notEqual(x, y, eps)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_notEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, T, Q> const &eps) {
return any(notEqual(x, y, eps));
}
/// <summary>
/// any(notEqual(x, y, MaxULPs)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_notEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, int, Q> const &MaxULPs) {
return any(notEqual(x, y, MaxULPs));
}
/// <summary>
/// all(lessThan(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_lessThan(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return all(lessThan(x, y));
}
/// <summary>
/// all(lessThanEqual(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_lessThanEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return all(lessThanEqual(x, y));
}
/// <summary>
/// all(greaterThan(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_greaterThan(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return all(greaterThan(x, y));
}
/// <summary>
/// all(greaterThanEqual(x, y)) shorthand
/// @private @see core_func_vector_relational
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_greaterThanEqual(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return all(greaterThanEqual(x, y));
}
/// <summary>
/// any(isinf(x)) shorthand
/// @private @see core_func_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_isinf(vec<L, T, Q> const &x) {
return any(isinf(x));
}
/// <summary>
/// all(isfinite(x)) shorthand
/// @private @see core_func_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all_isfinite(vec<L, T, Q> const &x) {
return all(isfinite(x));
}
/// <summary>
/// glm::any(glm::isnan(...)) shorthand
/// @private @see core_func_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any_isnan(vec<L, T, Q> const &x) {
return any(isnan(x));
}
/// <summary>
/// Returns 1.0 if >= 0, or –1.0 if x < 0
/// @see core_func_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> signP(vec<L, T, Q> const &x) {
return vec<L, T, Q>(lessThanEqual(vec<L, T, Q>(0), x)) - vec<L, T, Q>(lessThan(x, vec<L, T, Q>(0)));
}
/// <summary>
/// Returns 1.0 if x > 0, or –1.0 if x <= 0
/// @see core_func_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> signN(vec<L, T, Q> const &x) {
return vec<L, T, Q>(lessThan(vec<L, T, Q>(0), x)) - vec<L, T, Q>(lessThanEqual(x, vec<L, T, Q>(0)));
}
/// <summary>
/// ceilMultiple that accepts a scalar multiple parameter.
///
/// @tparam T Floating-point or integer scalar types
/// @param Source Source values to which is applied the function
/// @param Multiple Must be a null or positive value
/// @see gtc_round
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> ceilMultiple(vec<L, T, Q> const &Source, T Multiple) {
return ceilMultiple(Source, vec<L, T, Q>(Multiple));
}
/// <summary>
/// floorMultiple that accepts a scalar multiple parameter.
///
/// @tparam T Floating-point or integer scalar types
/// @param Source Source values to which is applied the function
/// @param Multiple Must be a null or positive value
/// @see gtc_round
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> floorMultiple(vec<L, T, Q> const &Source, T Multiple) {
return floorMultiple(Source, vec<L, T, Q>(Multiple));
}
/// <summary>
/// roundMultiple that accepts a scalar multiple parameter.
///
/// @tparam T Floating-point or integer scalar types
/// @param Source Source values to which is applied the function
/// @param Multiple Must be a null or positive value
/// @see gtc_round
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> roundMultiple(vec<L, T, Q> const &Source, T Multiple) {
return roundMultiple(Source, vec<L, T, Q>(Multiple));
}
/* Numeric extensions */
/// <summary>
/// Return true if all vector elements are equal (or within eps of each other).
/// @see gtx_vector_query
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER bool isUniform(vec<L, T, Q> const &v, T eps = epsilon<T>()) {
bool result = true;
for (length_t i = 1; i < L; ++i)
result &= equal(v[i], v[0], eps);
return result;
}
/// <summary>
/// Reverse the elements of a vector
/// @see gtx_vector_query
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> reverse(vec<L, T, Q> const &v) {
vec<L, T, Q> result;
for (length_t i = 0; i < L; ++i)
result[i] = v[L - i - 1];
return result;
}
/// <summary>
/// Calculate sin and cos simultaneously.
/// @param[in] v vector representing angles in radians
/// @param[out] s the sine of value in the range [-1 ; +1]
/// @param[out] c the cosine of value in the range [-1 ; +1]
/// @see core_func_trigonometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER void sincos(vec<L, T, Q> const &v, vec<L, T, Q> &s, vec<L, T, Q> &c) {
s = sin(v);
c = cos(v);
}
/// <summary>
/// Cardinal sine
/// @see core_func_trigonometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> sinc(vec<L, T, Q> const &v) {
return detail::functor1<vec, L, T, T, Q>::call(sinc, v);
}
/// <summary>
/// Normalized cardinal sine
/// @see core_func_trigonometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> sincn(vec<L, T, Q> const &v) {
return detail::functor1<vec, L, T, T, Q>::call(sincn, v);
}
/// <summary>
/// Logistic function with basic overflow handling; underflow to-be-determined.
/// @see core_func_exponential
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> logistic(vec<L, T, Q> const &v) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'logistic' only accept floating-point inputs.");
return detail::functor1<vec, L, T, T, Q>::call(logistic, v);
}
/// <summary>
/// Create a normalized vector2 from an angle (in radians).
/// @see core_func_trigonometric
/// </summary>
template<typename T, qualifier Q = glm::defaultp>
GLM_FUNC_QUALIFIER vec<2, T, Q> fromAngle(T angle) {
T sin, cos;
sincos(angle, sin, cos);
return vec<2, T, Q>(sin, cos);
}
/// <summary>
/// Return the direction vector given spherical coordinates.
/// @see core_func_trigonometric
/// </summary>
template<typename T, qualifier Q = defaultp>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<3, T, Q> spherical(T phi, T theta) {
T sinphi, cosphi, sintheta, costheta; // see sphericalRand
sincos(phi, sinphi, cosphi);
sincos(theta, sintheta, costheta);
return vec<3, T, Q>(sinphi * costheta, sinphi * sintheta, cosphi);
}
/// <summary>
/// Return a copy of the vector 'v' with its length clamped to 'maxLength'
/// @see core_func_geometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> clampLength(vec<L, T, Q> const &v, T maxLength) {
return (length2(v) > (maxLength * maxLength)) ? (normalize(v) * maxLength) : v;
}
template<typename genType>
GLM_FUNC_QUALIFIER genType clampLength(genType x, genType maxLength) {
return clampLength(vec<1, genType>(x), maxLength).x;
}
/// <summary>
/// Scales the length of vector "v" to "newLength".
/// @see core_func_geometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> scaleLength(vec<L, T, Q> const &v, T newLength) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'scaleLength' only accept floating-point inputs");
const T sqlen = length2(v);
if (sqlen < epsilon<T>()) {
vec<L, T, Q> result(T(0));
result[0] = newLength;
return result;
}
return v * (newLength / sqrt(sqlen));
}
template<typename genType>
GLM_FUNC_QUALIFIER genType scaleLength(genType x, genType newLength) {
return scaleLength(vec<1, genType>(x), newLength).x;
}
/// <summary>
/// Returns the homogenized vector: divides all components by w
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> homogenize(vec<4, T, Q> const &v) {
return vec<3, T, Q>(v.x / v.w, v.y / v.w, v.z / v.w);
}
/// <summary>
/// Returns the cross product of v and {1,0,0}.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> crossXAxis(vec<3, T, Q> const &v) {
return vec<3, T, Q>(T(0), v.z, -v.y);
}
/// <summary>
/// Returns the cross product of v and {0,1,0}.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> crossYAxis(vec<3, T, Q> const &v) {
return vec<3, T, Q>(-v.z, T(0), v.x);
}
/// <summary>
/// Returns the cross product of v and {0,0,1}.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> crossZAxis(vec<3, T, Q> const &v) {
return vec<3, T, Q>(v.y, -v.x, T(0));
}
/// <summary>
/// Returns the cross product of {1,0,0} and v.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> xAxisCross(vec<3, T, Q> const &v) {
return vec<3, T, Q>(T(0), -v.z, v.y);
}
/// <summary>
/// Returns the cross product of {0,1,0} and v.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> yAxisCross(vec<3, T, Q> const &v) {
return vec<3, T, Q>(v.z, T(0), -v.x);
}
/// <summary>
/// Returns the cross product of {0,0,1} and v.
/// @tparam T Floating-point scalar types.
/// @see core_func_geometric
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> zAxisCross(vec<3, T, Q> const &v) {
return vec<3, T, Q>(-v.y, v.x, T(0));
}
/// <summary>
/// areOrthonormal that assumes the vectors are normalized
/// @see gtx_vector_query
/// @private
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER bool areOrthonormal2(vec<L, T, Q> const &v0, vec<L, T, Q> const &v1, T const &epsilon) {
// assert(isNormalized(v0, epsilon));
// assert(isNormalized(v1, epsilon));
return abs(dot(v0, v1)) <= epsilon;
}
template<typename genType>
GLM_FUNC_QUALIFIER bool areOrthonormal2(genType v0, genType v1, genType eps = epsilon<genType>()) {
return abs(dot(v0, v1)) <= eps;
}
/// <summary>
/// Create a 'hint' axis for perpendicular/basis calculations.
/// @see gtx_perpendicular
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> hint(vec<3, T, Q> const &v) {
return ((v.x * v.x) < T(0.5) * length2(v)) ? right<T, Q>() : forward<T, Q>();
}
/// <summary>
/// Return a direction vector perpendicular to 'v'
/// @param[in] v direction to compute the perpendicular of.
/// @param[in] hint reference axis/vector to computer perpendicular.
/// @param[in] hint2 Alternative reference axis if 'v' points towards 'hint'
/// @see gtx_perpendicular
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> perpendicular(vec<3, T, Q> const &v, vec<3, T, Q> const &hint = forward<T, Q>(), vec<3, T, Q> const &hint2 = up<T, Q>()) {
const vec<3, T, Q> v2 = cross(v, hint);
return detail::approx_zero(dot(v2, v2)) ? hint2 : normalize(v2);
}
/// <summary>
/// Return a vector that is perpendicular to 'v' and the vector returned by perpendicular.
/// @see gtx_perpendicular
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> perpendicular2(vec<3, T, Q> const &v, vec<3, T, Q> const &hint = forward<T, Q>(), vec<3, T, Q> const &hint2 = up<T, Q>()) {
return normalize(cross(v, perpendicular(v, hint, hint2)));
}
/// <summary>
/// Update vectors 'out' and 'out2' to be orthogonal to 'v' and each other.
/// @see gtx_perpendicular
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER void perpendicularBasis(vec<3, T, Q> const &v, vec<3, T, Q> &out, vec<3, T, Q> &out2) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'perpendicularBasis' only accept floating-point inputs");
const T s = v.z >= T(0) ? T(1) : T(-1);
const T a = T(-1) / (s + v.z);
const T b = v.x * v.y * a;
out = vec<3, T, Q>(T(1) + s * v.x * v.x * a, s * b, -s * v.x);
out2 = vec<3, T, Q>(b, s + v.y * v.y * a, -v.y);
}
/// <summary>
/// A mutable glm::orthonormalize implementation.
/// @see gtx_orthonormalize
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER void orthonormalize2(vec<3, T, Q> &x, vec<3, T, Q> &y) {
x = normalize(x);
y = orthonormalize(y, x);
}
/// <summary>
/// Normalize the provided vectors and make them orthogonal to each other.
/// @see gtx_orthonormalize
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER void orthonormalize3(vec<3, T, Q> &x, vec<3, T, Q> &y, vec<3, T, Q> &z) {
x = normalize(x);
y = orthonormalize(y, x);
const T dot0 = dot(x, z);
const T dot1 = dot(y, z);
z = normalize(z - (y * dot1 + x * dot0));
}
/// <summary>
/// glm::proj with the assumption 'Normal' is already normalized.
/// @see gtx_projection
/// </summary>
template<typename genType>
GLM_FUNC_QUALIFIER genType projNorm(genType const &x, genType const &Normal) {
return dot(x, Normal) * Normal;
}
/// <summary>
/// Project a vector onto this plane defined by its normal orthogonal
/// @see gtx_projection
/// </summary>
template<typename genType>
GLM_FUNC_QUALIFIER genType projPlane(genType const &x, genType const &Normal) {
return x - proj(x, Normal);
}
/// <summary>
/// Breaks this vector down into parallel and perpendicular components with respect to the given direction
/// @see gtx_projection
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER void projDecompose(vec<L, T, Q> const &v, vec<L, T, Q> const &direction, vec<L, T, Q> &outPara, vec<L, T, Q> &outPerp) {
outPara = proj(v, direction);
outPerp = v - outPara;
}
template<typename genType>
GLM_FUNC_QUALIFIER void projDecompose(genType v, genType direction, genType &outPara, genType &outPerp) {
vec<1, genType> vParallel, vPerpendicular;
projDecompose(vec<1, genType>(v), vec<1, genType>(direction), vParallel, vPerpendicular);
outPara = vParallel.x;
outPerp = vPerpendicular.x;
}
/// <summary>
/// Return true if the three given points are collinear, i.e., lie on the same line.
/// @see gtx_vector_query
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER bool areCollinear(vec<L, T, Q> const &p1, vec<L, T, Q> const &p2, vec<L, T, Q> const &p3, T eps = epsilon<T>()) {
return length2(cross(p2 - p1, p3 - p1)) <= eps;
}
/// <summary>
/// Encode a normal using a spherical coordinate system
/// @see gtc_packing
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> sphericalEncode(vec<3, T, Q> const &v) {
const vec<2, T, Q> Result(atan2(v.y, v.x) * one_over_pi<T>(), v.z);
return Result * T(0.5) + T(0.5);
}
/// <summary>
/// Decode a vector from a spherical coordinate system
/// @see gtc_packing
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> sphericalDecode(vec<2, T, Q> const &v) {
const vec<2, T, Q> ang = v * T(2) - T(1);
const vec<2, T, Q> sc(sin(ang.x * pi<T>()), cos(ang.x * pi<T>()));
const vec<2, T, Q> phi(sqrt(T(1) - ang.y * ang.y), ang.y);
return vec<3, T, Q>(sc.y * phi.x, sc.x * phi.x, phi.y);
}
/// <summary>
/// Encode a normal using a octahedron coordinate system
/// @see gtc_packing
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> octahedronEncode(vec<3, T, Q> const &v) {
const vec<3, T, Q> n = v / (abs(v.x) + abs(v.y) + abs(v.z));
vec<2, T, Q> Result(T(0));
if (n.z >= T(0)) {
Result.x = n.x;
Result.y = n.y;
}
else {
Result.x = (T(1) - abs(n.y)) * (n.x >= T(0) ? T(1) : -T(1));
Result.y = (T(1) - abs(n.x)) * (n.y >= T(0) ? T(1) : -T(1));
}
Result.x = Result.x * T(0.5) + T(0.5);
Result.y = Result.y * T(0.5) + T(0.5);
return Result;
}
/// <summary>
/// Decode a vector from a octahedron coordinate system
/// @see gtc_packing
/// </summary>
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<3, T, Q> octahedronDecode(vec<2, T, Q> const &v) {
const vec<2, T, Q> f(v.x * T(2) - T(1), v.y * T(2) - T(1));
const vec<3, T, Q> n(f.x, f.y, T(1) - abs(f.x) - abs(f.y));
const T t = saturate(-n.z);
return normalize(n + vec<3, T, Q>(n.x >= T(0) ? -t : t, n.y >= T(0) ? -t : t, T(0)));
}
/// <summary>
/// Return a reflection vector according to a incident and surface normal.
///
/// @param[in] I Incident vector
/// @param[in] N Surface normal
/// @param[in] negativeSideRefractionIndex Refraction index of material exiting
/// @param[in] positiveSideRefractionIndex Refraction index of material entering
/// @see core_func_geometric
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> refract(vec<L, T, Q> const &I, vec<L, T, Q> const &N, T negativeSideRefractionIndex, T positiveSideRefractionIndex) {
return refract(I, N, negativeSideRefractionIndex / positiveSideRefractionIndex);
}
/// <summary>
/// Return a vector containing the Cartesian coordinates of a point specified
/// in barycentric (relative to a N-Dimension triangle) coordinates.
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> barycentric(vec<L, T, Q> const &value1, vec<L, T, Q> const &value2, vec<L, T, Q> const &value3, T amount1, T amount2) {
return (value1 + (amount1 * (value2 - value1))) + (amount2 * (value3 - value1));
}
template<typename genType>
GLM_FUNC_QUALIFIER genType barycentric(genType value1, genType value2, genType value3, genType amount1, genType amount2) {
return barycentric(vec<1, genType>(value1), vec<1, genType>(value2), vec<1, genType>(value3), amount1, amount2).x;
}
/// <summary>
/// Wraps x between [0, maxValue]
/// @see gtx_wrap
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> wrap(vec<L, T, Q> const &x, vec<L, T, Q> const &maxValue) {
vec<L, T, Q> Result(T(0));
for (length_t i = 0; i < L; ++i)
Result[i] = wrap<T>(x[i], maxValue[i]);
return Result;
}
/// <summary>
/// Wraps x between [0, maxValue]
/// @see gtx_wrap
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> wrap(vec<L, T, Q> const &x, T maxValue) {
return wrap(x, vec<L, T, Q>(maxValue));
}
/// <summary>
/// Loops 't' so that it is never greater than 'length' and less than zero.
/// This function is an emulation of: Unity.Mathf.Repeat
/// @see gtx_wrap
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> loopRepeat(vec<L, T, Q> const &t, vec<L, T, Q> const &length) {
return detail::functor2<vec, L, T, Q>::call(loopRepeat, t, length);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> loopRepeat(vec<L, T, Q> const &t, T length) {
if (detail::exactly_zero(length))
return vec<L, T, Q>(T(0));
return loopRepeat(t, vec<L, T, Q>(length));
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> pingPong(vec<L, T, Q> const &v, vec<L, T, Q> const &length) {
const vec<L, T, Q> t = loopRepeat(v, length * vec<L, T, Q>(2));
return length - abs(t - length);
}
/// <summary>
/// @see gtx_fast_trigonometry
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> wrapAngleSigned(vec<L, T, Q> const &x) {
return detail::functor1<vec, L, T, T, Q>::call(wrapAngleSigned, x);
}
/// <summary>
/// A lerp implementation that ensures values interpolate correctly when wrapped around two-pi.
/// This function emulates Unity.Mathf.LerpAngle
/// @see gtx_fast_trigonometry
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> lerpAngle(vec<L, T, Q> const &x, vec<L, T, Q> const &y, T t) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'lerpAngle' only accept floating-point inputs");
vec<L, T, Q> Result(T(0));
for (length_t i = 0; i < L; ++i)
Result[i] = lerpAngle<T>(x[i], y[i], t);
return Result;
}
/// <summary>
/// A lerp implementation that ensures values interpolate correctly when wrapped around two-pi.
/// @see gtx_fast_trigonometry
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> lerpAngle(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, T, Q> const &t) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'lerpAngle' only accept floating-point inputs");
vec<L, T, Q> Result(T(0));
for (length_t i = 0; i < L; ++i)
Result[i] = lerpAngle<T>(x[i], y[i], t[i]);
return Result;
}
/// <summary>
/// Return a position between two points, moving no further than maxDist.
/// This function emulates Unity.Vector3.MoveTowards
/// @see gtx_functions
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> moveTowards(vec<L, T, Q> const ¤t, vec<L, T, Q> const &target, T maxDist) {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'moveTowards' only accept floating-point inputs");
const vec<L, T, Q> delta = target - current;
const T sqdist = dot(delta, delta);
if (detail::approx_zero(sqdist) || (maxDist >= T(0) && sqdist <= maxDist * maxDist))
return target;
return current + (delta / (sqrt(sqdist) * maxDist));
}
/// <summary>
/// Round a 'value' to a specified grid size.
/// @see gtc_round
/// @see gtx_scalar_extensions
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> snap(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return detail::functor2<vec, L, T, Q>::call(snap, x, y);
}
/// <summary>
/// Round a 'value' to a specified grid size.
/// @see gtc_round
/// @see gtx_scalar_extensions
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> snap(vec<L, T, Q> const &x, T y) {
return snap(x, vec<L, T, Q>(y));
}
/// <summary>
/// Returns the normalized vector pointing from 'x' to 'y'.
/// @see gtx_vector_common
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> direction(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return normalize(y - x);
}
/// <summary>
/// Returns a vector 't' such that lerp(x, y, t) == value (or 0 if x == y)
/// @see gtx_compatibility
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> lerpInverse(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, T, Q> const &value) {
vec<L, T, Q> result(T(0));
for (length_t i = 0; i < L; ++i)
result[i] = lerpInverse(x[i], y[i], value[i]);
return result;
}
/// <summary>
/// Returns a vector 't' such that lerp(x, y, t) == value (or 0 if x == y)
/// @see gtx_compatibility
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> lerpInverse(vec<L, T, Q> const &x, vec<L, T, Q> const &y, T value) {
vec<L, T, Q> result(T(0));
for (length_t i = 0; i < L; ++i)
result[i] = lerpInverse(x[i], y[i], value);
return result;
}
/// <summary>
/// Normalize Lerp
/// @see gtx_compatibility
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> nlerp(vec<L, T, Q> const &x, vec<L, T, Q> const &y, vec<L, T, Q> const &t) {
return normalize(lerp(x, y, t));
}
/// <summary>
/// Normalize Lerp
/// @see gtx_compatibility
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> nlerp(vec<L, T, Q> const &x, vec<L, T, Q> const &y, T t) {
return normalize(lerp(x, y, t));
}
/* Missing implicit genType support & API unification */
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> fclamp(vec<L, T, Q> const &x) {
return fclamp(x, T(0), T(1));
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<1, T, Q> lerp(vec<1, T, Q> const &x, vec<1, T, Q> const &y, T a) {
return mix(x, y, a);
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<1, T, Q> lerp(vec<1, T, Q> const &x, vec<1, T, Q> const &y, vec<1, T, Q> const &a) {
return mix(x, y, a);
}
/* Functions with additional integral type support. */
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<std::is_integral<T>::value, vec<L, T, Q>>::type iceil(vec<L, T, Q> const &x) {
return x;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<std::is_integral<T>::value, vec<L, T, Q>>::type ifloor(vec<L, T, Q> const &x) {
return x;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<!std::is_integral<T>::value, vec<L, T, Q>>::type iceil(vec<L, T, Q> const &x) {
return ceil(x);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<!std::is_integral<T>::value, vec<L, T, Q>>::type ifloor(vec<L, T, Q> const &x) {
return floor(x);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<!std::is_integral<T>::value, vec<L, T, Q>>::type imod(vec<L, T, Q> const &x, T y) {
return mod(x, y);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER typename std::enable_if<!std::is_integral<T>::value, vec<L, T, Q>>::type imod(vec<L, T, Q> const &x, vec<L, T, Q> const &y) {
return mod(x, y);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> pow(vec<L, T, Q> const &base, T exponent) {
return pow(base, vec<L, T, Q>(exponent));
}
/* glm/gtx/associated.hpp extensions */
/// @private
namespace detail {
template<typename T, bool Aligned>
struct compute_associated {};
template<length_t L, typename T, qualifier Q, bool Aligned>
struct compute_associated<vec<L, T, Q>, Aligned> {
static GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> eq(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
vec<L, T, Q> Result;
for (length_t i = 0; i < L; ++i)
Result[i] = equal_to(cA[i], cB[i]) ? vA[i] : vB[i];
return Result;
}
static GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> gt(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
vec<L, T, Q> Result;
for (length_t i = 0; i < L; ++i)
Result[i] = (cA[i] > cB[i]) ? vA[i] : vB[i];
return Result;
}
static GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, T, Q> gte(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
vec<L, T, Q> Result;
for (length_t i = 0; i < L; ++i)
Result[i] = (cA[i] >= cB[i]) ? vA[i] : vB[i];
return Result;
}
};
}
/// <summary>
/// Equal comparison between 2 variables and returns 2 associated variable values
/// @see gtx_associated_min_max
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> associatedEqual(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
return detail::compute_associated<vec<L, T, Q>, detail::is_aligned<Q>::value>::eq(cA, cB, vA, vB);
}
/// <summary>
/// Greater-than comparison between 2 variables and returns 2 associated variable values
/// @see gtx_associated_min_max
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> associatedGreater(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
return detail::compute_associated<vec<L, T, Q>, detail::is_aligned<Q>::value>::gt(cA, cB, vA, vB);
}
/// <summary>
/// Greater-than-or-equal-to comparison between 2 variables and returns 2 associated variable values
/// @see gtx_associated_min_max
/// </summary>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> associatedGreaterEqual(vec<L, T, Q> const &cA, vec<L, T, Q> const &cB, vec<L, T, Q> const &vA, vec<L, T, Q> const &vB) {
return detail::compute_associated<vec<L, T, Q>, detail::is_aligned<Q>::value>::gte(cA, cB, vA, vB);
}