forked from juj/MathGeoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrustum.cpp
1175 lines (1046 loc) · 32.2 KB
/
Frustum.cpp
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
/* Copyright Jukka Jylänki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/** @file Frustum.cpp
@author Jukka Jylänki
@brief Implementation for the Frustum geometry object. */
#include "Frustum.h"
#include "AABB.h"
#include "Circle.h"
#include "../Math/MathFunc.h"
#include "Plane.h"
#include "Line.h"
#include "OBB.h"
#include "Polyhedron.h"
#include "Polygon.h"
#include "Ray.h"
#include "Sphere.h"
#include "Capsule.h"
#include "Triangle.h"
#include "LineSegment.h"
#include "PBVolume.h"
#include "../Math/float2.h"
#include "../Math/float3x3.h"
#include "../Math/float3x4.h"
#include "../Math/float4x4.h"
#include "../Math/float4.h"
#include "../Math/Quat.h"
#include "../Algorithm/Random/LCG.h"
#include "../Algorithm/GJK.h"
#ifdef MATH_ENABLE_STL_SUPPORT
#include <iostream>
#endif
#if defined(MATH_TINYXML_INTEROP) && defined(MATH_CONTAINERLIB_SUPPORT)
#include "Container/UString.h"
#endif
#if defined(MATH_SIMD) && defined(MATH_AUTOMATIC_SSE)
#include "../Math/float4_sse.h"
#include "../Math/float4_neon.h"
#endif
MATH_BEGIN_NAMESPACE
Frustum::Frustum()
:type(InvalidFrustum),
pos(vec::nan),
front(vec::nan),
up(vec::nan),
nearPlaneDistance(FLOAT_NAN),
farPlaneDistance(FLOAT_NAN),
worldMatrix(float3x4::nan),
viewProjMatrix(float4x4::nan)
{
// For conveniency, allow automatic initialization of the graphics API and handedness in use.
// If neither of the #defines are set, user must specify per-instance.
#ifdef MATH_USE_DIRECT3D
projectiveSpace = FrustumSpaceD3D;
#elif defined(MATH_USE_OPENGL)
projectiveSpace = FrustumSpaceGL;
#else
projectiveSpace = FrustumSpaceInvalid;
#endif
#ifdef MATH_LEFTHANDED_CAMERA
handedness = FrustumLeftHanded;
#elif defined(MATH_RIGHTHANDED_CAMERA)
handedness = FrustumRightHanded;
#else
handedness = FrustumHandednessInvalid;
#endif
}
void Frustum::SetKind(FrustumProjectiveSpace p, FrustumHandedness h)
{
projectiveSpace = p;
handedness = h;
if (up.IsFinite())
WorldMatrixChanged(); // Setting handedness affects world matrix.
ProjectionMatrixChanged();
}
void Frustum::SetViewPlaneDistances(float n, float f)
{
nearPlaneDistance = n;
farPlaneDistance = f;
ProjectionMatrixChanged();
}
void Frustum::SetFrame(const vec &p, const vec &f, const vec &u)
{
pos = p;
front = f;
up = u;
WorldMatrixChanged();
}
void Frustum::SetPos(const vec &p)
{
pos = p;
WorldMatrixChanged();
}
void Frustum::SetFront(const vec &f)
{
front = f;
WorldMatrixChanged();
}
void Frustum::SetUp(const vec &u)
{
up = u;
WorldMatrixChanged();
}
void Frustum::SetPerspective(float h, float v)
{
type = PerspectiveFrustum;
horizontalFov = h;
verticalFov = v;
ProjectionMatrixChanged();
}
void Frustum::SetOrthographic(float w, float h)
{
type = OrthographicFrustum;
orthographicWidth = w;
orthographicHeight = h;
ProjectionMatrixChanged();
}
void Frustum::WorldMatrixChanged()
{
worldMatrix = ComputeWorldMatrix();
float3x4 viewMatrix = worldMatrix;
viewMatrix.InverseOrthonormal();
viewProjMatrix = projectionMatrix * viewMatrix;
}
void Frustum::ProjectionMatrixChanged()
{
projectionMatrix = ComputeProjectionMatrix();
if (!IsNan(worldMatrix[0][0]))
{
float3x4 viewMatrix = worldMatrix;
viewMatrix.InverseOrthonormal();
viewProjMatrix = projectionMatrix * viewMatrix;
}
}
float Frustum::AspectRatio() const
{
return Tan(horizontalFov*0.5f) / Tan(verticalFov*0.5f);
}
void Frustum::SetHorizontalFovAndAspectRatio(float hFov, float aspectRatio)
{
type = PerspectiveFrustum;
horizontalFov = hFov;
verticalFov = 2.f * Atan(Tan(hFov*0.5f)/aspectRatio);
ProjectionMatrixChanged();
}
void Frustum::SetVerticalFovAndAspectRatio(float vFov, float aspectRatio)
{
type = PerspectiveFrustum;
verticalFov = vFov;
horizontalFov = 2.f * Atan(Tan(vFov*0.5f)*aspectRatio);
ProjectionMatrixChanged();
}
vec Frustum::WorldRight() const
{
if (handedness == FrustumRightHanded)
return Cross(front, up);
else
return Cross(up, front);
}
float Frustum::NearPlaneWidth() const
{
if (type == PerspectiveFrustum)
return Tan(horizontalFov*0.5f)*2.f * nearPlaneDistance;
else
return orthographicWidth;
}
float Frustum::NearPlaneHeight() const
{
if (type == PerspectiveFrustum)
return Tan(verticalFov*0.5f)*2.f * nearPlaneDistance;
else
return orthographicHeight;
}
Plane Frustum::NearPlane() const
{
return Plane(pos + front * nearPlaneDistance, -front);
}
Plane Frustum::FarPlane() const
{
return Plane(pos + front * farPlaneDistance, front);
}
Plane Frustum::LeftPlane() const
{
if (type == PerspectiveFrustum)
{
vec left = -WorldRight();
left.ScaleToLength(Tan(horizontalFov*0.5f));
vec leftSide = front + left;
vec leftSideNormal = ((handedness == FrustumRightHanded) ? Cross(up, leftSide) : Cross(leftSide, up)).Normalized();
return Plane(pos, leftSideNormal);
}
else
{
vec left = -WorldRight();
return Plane(NearPlanePos(-1.f, 0.f), left.Normalized());
}
}
Plane Frustum::RightPlane() const
{
if (type == PerspectiveFrustum)
{
vec right = WorldRight();
right.ScaleToLength(Tan(horizontalFov*0.5f));
vec rightSide = front + right;
vec rightSideNormal = ((handedness == FrustumRightHanded) ? Cross(rightSide, up) : Cross(up, rightSide)).Normalized();
return Plane(pos, rightSideNormal);
}
else
{
vec right = WorldRight();
return Plane(NearPlanePos(1.f, 0.f), right.Normalized());
}
}
Plane Frustum::TopPlane() const
{
if (type == PerspectiveFrustum)
{
vec topSide = front + Tan(verticalFov * 0.5f) * up;
vec right = WorldRight();
vec topSideNormal = ((handedness == FrustumRightHanded) ? Cross(right, topSide) : Cross(topSide, right)).Normalized();
return Plane(pos, topSideNormal);
}
else
{
return Plane(NearPlanePos(0.f, 1.f), up);
}
}
Plane Frustum::BottomPlane() const
{
if (type == PerspectiveFrustum)
{
vec bottomSide = front - Tan(verticalFov * 0.5f) * up;
vec left = -WorldRight();
vec bottomSideNormal = ((handedness == FrustumRightHanded) ? Cross(left, bottomSide) : Cross(bottomSide, left)).Normalized();
return Plane(pos, bottomSideNormal);
}
else
{
return Plane(NearPlanePos(0.f, -1.f), -up);
}
}
void Frustum::SetWorldMatrix(const float3x4 &worldTransform)
{
pos = POINT_VEC(worldTransform.TranslatePart());
if (handedness == FrustumRightHanded)
front = -DIR_VEC(worldTransform.Col(2)); // The camera looks towards -Z axis of the given transform.
else
front = DIR_VEC(worldTransform.Col(2)); // The camera looks towards +Z axis of the given transform.
up = DIR_VEC(worldTransform.Col(1)); // The camera up points towards +Y of the given transform.
assume1(pos.IsFinite(), pos);
assume2(up.IsNormalized(1e-3f), up, up.Length());
assume2(front.IsNormalized(1e-3f), front, front.Length());
assume3(up.IsPerpendicular(front), up, front, up.Dot(front));
assume(worldTransform.IsColOrthogonal3()); // Front and up must be orthogonal to each other.
assume1(EqualAbs(worldTransform.Determinant(), 1.f), worldTransform.Determinant()); // The matrix cannot contain mirroring.
WorldMatrixChanged();
}
float3x4 Frustum::ComputeWorldMatrix() const
{
assume1(pos.IsFinite(), pos);
assume2(up.IsNormalized(1e-3f), up, up.Length());
assume2(front.IsNormalized(1e-3f), front, front.Length());
assume3(up.IsPerpendicular(front), up, front, up.Dot(front));
float3x4 m;
m.SetCol(0, DIR_TO_FLOAT3(WorldRight().Normalized()));
m.SetCol(1, DIR_TO_FLOAT3(up));
if (handedness == FrustumRightHanded)
m.SetCol(2, DIR_TO_FLOAT3(-front)); // In right-handed convention, the -Z axis must map towards the front vector. (so +Z maps to -front)
else
m.SetCol(2, DIR_TO_FLOAT3(front)); // In left-handed convention, the +Z axis must map towards the front vector.
m.SetCol(3, POINT_TO_FLOAT3(pos));
assume(!m.HasNegativeScale());
return m;
}
float3x4 Frustum::ComputeViewMatrix() const
{
float3x4 world = ComputeWorldMatrix();
world.InverseOrthonormal();
return world;
}
float4x4 Frustum::ComputeViewProjMatrix() const
{
return ComputeProjectionMatrix() * ComputeViewMatrix();
}
float4x4 Frustum::ComputeProjectionMatrix() const
{
if (type == InvalidFrustum || projectiveSpace == FrustumSpaceInvalid)
return float4x4::nan;
assume(type == PerspectiveFrustum || type == OrthographicFrustum);
assume(projectiveSpace == FrustumSpaceGL || projectiveSpace == FrustumSpaceD3D);
assume(handedness == FrustumLeftHanded || handedness == FrustumRightHanded);
if (type == PerspectiveFrustum)
{
if (projectiveSpace == FrustumSpaceGL)
{
if (handedness == FrustumRightHanded)
return float4x4::OpenGLPerspProjRH(nearPlaneDistance, farPlaneDistance, NearPlaneWidth(), NearPlaneHeight());
else if (handedness == FrustumLeftHanded)
return float4x4::OpenGLPerspProjLH(nearPlaneDistance, farPlaneDistance, NearPlaneWidth(), NearPlaneHeight());
}
else if (projectiveSpace == FrustumSpaceD3D)
{
if (handedness == FrustumRightHanded)
return float4x4::D3DPerspProjRH(nearPlaneDistance, farPlaneDistance, NearPlaneWidth(), NearPlaneHeight());
else if (handedness == FrustumLeftHanded)
return float4x4::D3DPerspProjLH(nearPlaneDistance, farPlaneDistance, NearPlaneWidth(), NearPlaneHeight());
}
}
else if (type == OrthographicFrustum)
{
if (projectiveSpace == FrustumSpaceGL)
{
if (handedness == FrustumRightHanded)
return float4x4::OpenGLOrthoProjRH(nearPlaneDistance, farPlaneDistance, orthographicWidth, orthographicHeight);
else if (handedness == FrustumLeftHanded)
return float4x4::OpenGLOrthoProjLH(nearPlaneDistance, farPlaneDistance, orthographicWidth, orthographicHeight);
}
else if (projectiveSpace == FrustumSpaceD3D)
{
if (handedness == FrustumRightHanded)
return float4x4::D3DOrthoProjRH(nearPlaneDistance, farPlaneDistance, orthographicWidth, orthographicHeight);
else if (handedness == FrustumLeftHanded)
return float4x4::D3DOrthoProjLH(nearPlaneDistance, farPlaneDistance, orthographicWidth, orthographicHeight);
}
}
#ifndef OPTIMIZED_RELEASE
LOGE("Not all values of Frustum were initialized properly! Please initialize correctly before calling Frustum::ProjectionMatrix()!");
#endif
return float4x4::nan;
}
vec Frustum::NearPlanePos(float x, float y) const
{
assume(type == PerspectiveFrustum || type == OrthographicFrustum);
if (type == PerspectiveFrustum)
{
float frontPlaneHalfWidth = Tan(horizontalFov*0.5f)*nearPlaneDistance;
float frontPlaneHalfHeight = Tan(verticalFov*0.5f)*nearPlaneDistance;
x = x * frontPlaneHalfWidth; // Map [-1,1] to [-width/2, width/2].
y = y * frontPlaneHalfHeight; // Map [-1,1] to [-height/2, height/2].
vec right = WorldRight();
return pos + front * nearPlaneDistance + x * right + y * up;
}
else
{
vec right = WorldRight();
return pos + front * nearPlaneDistance
+ x * orthographicWidth * 0.5f * right
+ y * orthographicHeight * 0.5f * up;
}
}
vec Frustum::NearPlanePos(const float2 &point) const
{
return NearPlanePos(point.x, point.y);
}
vec Frustum::FarPlanePos(float x, float y) const
{
assume(type == PerspectiveFrustum || type == OrthographicFrustum);
if (type == PerspectiveFrustum)
{
float farPlaneHalfWidth = Tan(horizontalFov*0.5f)*farPlaneDistance;
float farPlaneHalfHeight = Tan(verticalFov*0.5f)*farPlaneDistance;
x = x * farPlaneHalfWidth;
y = y * farPlaneHalfHeight;
vec right = WorldRight();
return pos + front * farPlaneDistance + x * right + y * up;
}
else
{
vec right = WorldRight();
return pos + front * farPlaneDistance
+ x * orthographicWidth * 0.5f * right
+ y * orthographicHeight * 0.5f * up;
}
}
vec Frustum::FarPlanePos(const float2 &point) const
{
return FarPlanePos(point.x, point.y);
}
float2 Frustum::ViewportToScreenSpace(float x, float y, int screenWidth, int screenHeight)
{
return float2((x + 1.f) * 0.5f * (screenWidth-1.f), (1.f - y) * 0.5f * (screenHeight-1.f));
}
float2 Frustum::ViewportToScreenSpace(const float2 &point, int screenWidth, int screenHeight)
{
return ViewportToScreenSpace(point.x, point.y, screenWidth, screenHeight);
}
float2 Frustum::ScreenToViewportSpace(float x, float y, int screenWidth, int screenHeight)
{
return float2(x * 2.f / (screenWidth-1.f) - 1.f, 1.f - y * 2.f / (screenHeight - 1.f));
}
float2 Frustum::ScreenToViewportSpace(const float2 &point, int screenWidth, int screenHeight)
{
return ScreenToViewportSpace(point.x, point.y, screenWidth, screenHeight);
}
Ray Frustum::UnProject(float x, float y) const
{
assume1(x >= -1.f, x);
assume1(x <= 1.f, x);
assume1(y >= -1.f, y);
assume1(y <= 1.f, y);
if (type == PerspectiveFrustum)
{
vec nearPlanePos = NearPlanePos(x, y);
return Ray(pos, (nearPlanePos - pos).Normalized());
}
else
return UnProjectFromNearPlane(x, y);
}
LineSegment Frustum::UnProjectLineSegment(float x, float y) const
{
vec nearPlanePos = NearPlanePos(x, y);
vec farPlanePos = FarPlanePos(x, y);
return LineSegment(nearPlanePos, farPlanePos);
}
Ray Frustum::UnProjectFromNearPlane(float x, float y) const
{
return UnProjectLineSegment(x, y).ToRay();
}
vec Frustum::PointInside(float x, float y, float z) const
{
assume(z >= 0.f);
assume(z <= 1.f);
return UnProjectLineSegment(x, y).GetPoint(z);
}
vec Frustum::Project(const vec &point) const
{
float4 projectedPoint = ViewProjMatrix().Mul(POINT_TO_FLOAT4(point));
projectedPoint.NormalizeW(); // Post-projective perspective divide.
return FLOAT4_TO_POINT(projectedPoint);
}
bool Frustum::Contains(const vec &point) const
{
#if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE)
// SSE 4.1 32-bit: Best: 6.913 nsecs / 18.52 ticks, Avg: 6.978 nsecs, Worst: 7.297 nsecs
vec projected = Project(point);
simd4f a = abs_ps(projected);
simd4f y = yyyy_ps(a);
a = max_ps(a, y);
y = zzzz_ps(a);
a = max_ps(a, y);
const float eps = 1e-3f;
return _mm_cvtss_f32(a) <= 1.f + eps &&
(projectiveSpace == FrustumSpaceGL || s4f_z(projected) >= -eps);
#else
// SSE 4.1 32-bit: Best: 7.681 nsecs / 21.096 ticks, Avg : 7.954 nsecs, Worst : 9.217 nsecs
const float eps = 1e-3f;
const float position = 1.f + eps;
const float neg = -position;
vec projected = Project(point);
if (projectiveSpace == FrustumSpaceD3D)
{
return neg <= projected.x && projected.x <= position &&
neg <= projected.y && projected.y <= position &&
-eps <= projected.z && projected.z <= position;
}
else if (projectiveSpace == FrustumSpaceGL)
{
return neg <= projected.x && projected.x <= position &&
neg <= projected.y && projected.y <= position &&
neg <= projected.z && projected.z <= position;
}
else
{
#ifndef OPTIMIZED_RELEASE
///\todo Make Frustum::Contains agnostic of the projection settings.
LOGE("Not all values of Frustum were initialized properly! Please initialize correctly before calling Frustum::Contains()!");
#endif
return false;
}
#endif
}
bool Frustum::Contains(const LineSegment &lineSegment) const
{
#if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE)
// SSE 4.1: Best: 15.746 nsecs / 42.152 ticks, Avg: 15.842 nsecs, Worst: 16.130 nsecs
vec pa = Project(lineSegment.a);
simd4f a = abs_ps(pa);
vec pb = Project(lineSegment.b);
simd4f b = abs_ps(pb);
a = max_ps(a, b);
simd4f y = yyyy_ps(a);
a = max_ps(a, y);
y = zzzz_ps(a);
a = max_ps(a, y);
const float eps = 1e-3f;
return _mm_cvtss_f32(a) <= 1.f + eps &&
(projectiveSpace == FrustumSpaceGL || s4f_z(min_ps(pa, pb)) >= -eps);
#else
// SSE 4.1: 16.514 nsecs / 44.784 ticks, Avg: 16.825 nsecs, Worst: 16.898 nsecs
return Contains(lineSegment.a) && Contains(lineSegment.b);
#endif
}
bool Frustum::Contains(const Triangle &triangle) const
{
#if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE)
// Best: 21.206 nsecs / 55.496 ticks, Avg: 21.702 nsecs, Worst: 21.891 nsecs
vec pa = Project(triangle.a);
simd4f a = abs_ps(pa);
vec pb = Project(triangle.b);
simd4f b = abs_ps(pb);
a = max_ps(a, b);
vec pc = Project(triangle.c);
simd4f c = abs_ps(pc);
a = max_ps(a, c);
simd4f y = yyyy_ps(a);
a = max_ps(a, y);
y = zzzz_ps(a);
a = max_ps(a, y);
const float eps = 1e-3f;
return _mm_cvtss_f32(a) <= 1.f + eps &&
(projectiveSpace == FrustumSpaceGL || s4f_z(min_ps(min_ps(pa, pb), pc)) >= -eps);
#else
// Best: 21.122 nsecs / 56.512 ticks, Avg: 21.226 nsecs, Worst: 21.506 nsecs
return Contains(triangle.a) && Contains(triangle.b) && Contains(triangle.c);
#endif
}
bool Frustum::Contains(const Polygon &polygon) const
{
for(int i = 0; i < polygon.NumVertices(); ++i)
if (!Contains(polygon.Vertex(i)))
return false;
return true;
}
bool Frustum::Contains(const AABB &aabb) const
{
for(int i = 0; i < 8; ++i)
if (!Contains(aabb.CornerPoint(i)))
return false;
return true;
}
bool Frustum::Contains(const OBB &obb) const
{
for(int i = 0; i < 8; ++i)
if (!Contains(obb.CornerPoint(i)))
return false;
return true;
}
bool Frustum::Contains(const Frustum &frustum) const
{
for(int i = 0; i < 8; ++i)
if (!Contains(frustum.CornerPoint(i)))
return false;
return true;
}
bool Frustum::Contains(const Polyhedron &polyhedron) const
{
assume(polyhedron.IsClosed());
for(int i = 0; i < polyhedron.NumVertices(); ++i)
if (!Contains(polyhedron.Vertex(i)))
return false;
return true;
}
vec Frustum::ClosestPoint(const vec &point) const
{
if (type == OrthographicFrustum)
{
float frontHalfSize = (farPlaneDistance - nearPlaneDistance) * 0.5f;
float halfWidth = orthographicWidth * 0.5f;
float halfHeight = orthographicHeight * 0.5f;
vec frustumCenter = pos + (frontHalfSize + nearPlaneDistance) * front;
vec right = Cross(front, up);
assert(right.IsNormalized());
vec d = point - frustumCenter;
vec closestPoint = frustumCenter;
#if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SIMD)
// Best: 21.506 nsecs / 57.224 ticks, Avg: 21.683 nsecs, Worst: 22.659 nsecs
simd4f z = set1_ps(frontHalfSize);
closestPoint = madd_ps(max_ps(min_ps(dot4_ps(d.v, front.v), z), neg_ps(z)), front.v, closestPoint);
simd4f y = set1_ps(halfHeight);
closestPoint = madd_ps(max_ps(min_ps(dot4_ps(d.v, up.v), y), neg_ps(y)), up.v, closestPoint);
simd4f x = set1_ps(halfWidth);
closestPoint = madd_ps(max_ps(min_ps(dot4_ps(d.v, right.v), x), neg_ps(x)), right.v, closestPoint);
#else
// Best: 30.724 nsecs / 82.192 ticks, Avg: 31.069 nsecs, Worst: 39.172 nsecs
closestPoint += Clamp(Dot(d, front), -frontHalfSize, frontHalfSize) * front;
closestPoint += Clamp(Dot(d, right), -halfWidth, halfWidth) * right;
closestPoint += Clamp(Dot(d, up), -halfHeight, halfHeight) * up;
#endif
return closestPoint;
}
else
{
// Best: 12.339 usecs / 32900.7 ticks, Avg: 12.521 usecs, Worst: 13.308 usecs
return ToPolyhedron().ClosestPoint(point);
}
///\todo Improve numerical stability enough to do effectively this - but do so without temporary memory allocations.
// return ToPolyhedron().ClosestPointConvex(point);
}
float Frustum::Distance(const vec &point) const
{
vec pt = ClosestPoint(point);
return pt.Distance(point);
}
bool Frustum::IsFinite() const
{
return pos.IsFinite() && front.IsFinite() && up.IsFinite() && MATH_NS::IsFinite(nearPlaneDistance)
&& MATH_NS::IsFinite(farPlaneDistance) && MATH_NS::IsFinite(horizontalFov) && MATH_NS::IsFinite(verticalFov);
}
Plane Frustum::GetPlane(int faceIndex) const
{
assume(0 <= faceIndex && faceIndex <= 5);
switch(faceIndex)
{
default: // For release builds where assume() is disabled, always return the first option if out-of-bounds.
case 0: return NearPlane();
case 1: return FarPlane();
case 2: return LeftPlane();
case 3: return RightPlane();
case 4: return TopPlane();
case 5: return BottomPlane();
}
}
float Frustum::Volume() const
{
return ToPolyhedron().Volume();
}
vec Frustum::FastRandomPointInside(LCG &rng) const
{
float f1 = rng.Float(-1.f, 1.f);
float f2 = rng.Float(-1.f, 1.f);
float f3 = rng.Float(0.f, 1.f);
return PointInside(f1, f2, f3);
}
vec Frustum::UniformRandomPointInside(LCG &rng) const
{
if (type == OrthographicFrustum)
return FastRandomPointInside(rng);
else
{
OBB o = MinimalEnclosingOBB();
for(int numTries = 0; numTries < 1000; ++numTries)
{
vec pt = o.RandomPointInside(rng);
if (Contains(pt))
return pt;
}
LOGW("Rejection sampling failed in Frustum::UniformRandomPointInside! Producing a non-uniformly distributed point inside the frustum!");
return FastRandomPointInside(rng);
}
}
void Frustum::Translate(const vec &offset)
{
pos += offset;
}
void Frustum::Transform(const float3x3 &transform)
{
assume(transform.HasUniformScale());
pos = transform * pos;
front = transform * front;
float scaleFactor = front.Normalize();
up = (transform * up).Normalized();
nearPlaneDistance *= scaleFactor;
farPlaneDistance *= scaleFactor;
if (type == OrthographicFrustum)
{
orthographicWidth *= scaleFactor;
orthographicHeight *= scaleFactor;
}
}
void Frustum::Transform(const float3x4 &transform)
{
assume(transform.HasUniformScale());
pos = transform.MulPos(pos);
front = transform.MulDir(front);
float scaleFactor = front.Normalize();
up = transform.MulDir(up).Normalized();
nearPlaneDistance *= scaleFactor;
farPlaneDistance *= scaleFactor;
if (type == OrthographicFrustum)
{
orthographicWidth *= scaleFactor;
orthographicHeight *= scaleFactor;
}
}
void Frustum::Transform(const float4x4 &transform)
{
assume(transform.Row(3).Equals(0,0,0,1));
Transform(transform.Float3x4Part());
}
void Frustum::Transform(const Quat &transform)
{
Transform(transform.ToFloat3x3());
}
void Frustum::GetPlanes(Plane *outArray) const
{
assume(outArray);
#ifndef MATH_ENABLE_INSECURE_OPTIMIZATIONS
if (!outArray)
return;
#endif
for(int i = 0; i < 6; ++i)
outArray[i] = GetPlane(i);
}
vec Frustum::CenterPoint() const
{
return pos + (nearPlaneDistance + farPlaneDistance) * 0.5f * front;
}
void Frustum::GetCornerPoints(vec *outPointArray) const
{
assume(outPointArray);
#ifndef MATH_ENABLE_INSECURE_OPTIMIZATIONS
if (!outPointArray)
return;
#endif
if (type == PerspectiveFrustum)
{
float tanhfov = Tan(horizontalFov*0.5f);
float tanvfov = Tan(verticalFov*0.5f);
float frontPlaneHalfWidth = tanhfov*nearPlaneDistance;
float frontPlaneHalfHeight = tanvfov*nearPlaneDistance;
float farPlaneHalfWidth = tanhfov*farPlaneDistance;
float farPlaneHalfHeight = tanvfov*farPlaneDistance;
vec right = WorldRight();
vec nearCenter = pos + front * nearPlaneDistance;
vec nearHalfWidth = frontPlaneHalfWidth*right;
vec nearHalfHeight = frontPlaneHalfHeight*up;
outPointArray[0] = nearCenter - nearHalfWidth - nearHalfHeight;
outPointArray[1] = nearCenter + nearHalfWidth - nearHalfHeight;
outPointArray[2] = nearCenter - nearHalfWidth + nearHalfHeight;
outPointArray[3] = nearCenter + nearHalfWidth + nearHalfHeight;
vec farCenter = pos + front * farPlaneDistance;
vec farHalfWidth = farPlaneHalfWidth*right;
vec farHalfHeight = farPlaneHalfHeight*up;
outPointArray[4] = farCenter - farHalfWidth - farHalfHeight;
outPointArray[5] = farCenter + farHalfWidth - farHalfHeight;
outPointArray[6] = farCenter - farHalfWidth + farHalfHeight;
outPointArray[7] = farCenter + farHalfWidth + farHalfHeight;
}
else
{
vec right = WorldRight();
vec nearCenter = pos + front * nearPlaneDistance;
vec farCenter = pos + front * farPlaneDistance;
vec halfWidth = orthographicWidth * 0.5f * right;
vec halfHeight = orthographicHeight * 0.5f * up;
outPointArray[0] = nearCenter - halfWidth - halfHeight;
outPointArray[1] = nearCenter + halfWidth - halfHeight;
outPointArray[2] = nearCenter - halfWidth + halfHeight;
outPointArray[3] = nearCenter + halfWidth + halfHeight;
outPointArray[4] = farCenter - halfWidth - halfHeight;
outPointArray[5] = farCenter + halfWidth - halfHeight;
outPointArray[6] = farCenter - halfWidth + halfHeight;
outPointArray[7] = farCenter + halfWidth + halfHeight;
}
}
LineSegment Frustum::Edge(int edgeIndex) const
{
assume(0 <= edgeIndex && edgeIndex <= 11);
switch(edgeIndex)
{
default: // For release builds where assume() is disabled, return always the first option if out-of-bounds.
case 0: return LineSegment(CornerPoint(0), CornerPoint(1));
case 1: return LineSegment(CornerPoint(0), CornerPoint(2));
case 2: return LineSegment(CornerPoint(0), CornerPoint(4));
case 3: return LineSegment(CornerPoint(1), CornerPoint(3));
case 4: return LineSegment(CornerPoint(1), CornerPoint(5));
case 5: return LineSegment(CornerPoint(2), CornerPoint(3));
case 6: return LineSegment(CornerPoint(2), CornerPoint(6));
case 7: return LineSegment(CornerPoint(3), CornerPoint(7));
case 8: return LineSegment(CornerPoint(4), CornerPoint(5));
case 9: return LineSegment(CornerPoint(4), CornerPoint(6));
case 10: return LineSegment(CornerPoint(5), CornerPoint(7));
case 11: return LineSegment(CornerPoint(6), CornerPoint(7));
}
}
vec Frustum::CornerPoint(int cornerIndex) const
{
assume(0 <= cornerIndex && cornerIndex <= 7);
switch(cornerIndex)
{
default: // For release builds where assume() is disabled, return always the first option if out-of-bounds.
case 0: return NearPlanePos(-1, -1);
case 1: return FarPlanePos(-1, -1);
case 2: return NearPlanePos(-1, 1);
case 3: return FarPlanePos(-1, 1);
case 4: return NearPlanePos(1, -1);
case 5: return FarPlanePos(1, -1);
case 6: return NearPlanePos(1, 1);
case 7: return FarPlanePos(1, 1);
}
}
vec Frustum::ExtremePoint(const vec &direction, float &projectionDistance) const
{
vec corners[8];
GetCornerPoints(corners);
vec mostExtreme = vec::nan;
projectionDistance = -FLOAT_INF;
for(int i = 0; i < 8; ++i)
{
float d = Dot(direction, corners[i]);
if (d > projectionDistance)
{
projectionDistance = d;
mostExtreme = corners[i];
}
}
return mostExtreme;
}
void Frustum::ProjectToAxis(const vec &direction, float &outMin, float &outMax) const
{
vec corners[8];
GetCornerPoints(corners);
outMax = -FLOAT_INF;
outMin = FLOAT_INF;
for(int i = 0; i < 8; ++i)
{
float d = Dot(direction, corners[i]);
outMax = Max(outMax, d);
outMin = Min(outMin, d);
}
}
int Frustum::UniqueFaceNormals(vec *out) const
{
if (type == PerspectiveFrustum)
{
out[0] = front;
out[1] = LeftPlane().normal;
out[2] = RightPlane().normal;
out[3] = TopPlane().normal;
out[4] = BottomPlane().normal;
return 5;
}
else
{
out[0] = front;
out[1] = up;
out[2] = Cross(front, up);
return 3;
}
}
int Frustum::UniqueEdgeDirections(vec *out) const
{
if (type == PerspectiveFrustum)
{
out[0] = NearPlanePos(-1, -1) - NearPlanePos(1, -1);
out[1] = NearPlanePos(-1, -1) - NearPlanePos(-1, 1);
out[2] = FarPlanePos(-1, -1) - NearPlanePos(-1, -1);
out[3] = FarPlanePos( 1, -1) - NearPlanePos( 1, -1);
out[4] = FarPlanePos(-1, 1) - NearPlanePos(-1, 1);
out[5] = FarPlanePos( 1, 1) - NearPlanePos( 1, 1);
return 6;
}
else
{
out[0] = front;
out[1] = up;
out[2] = Cross(front, up);
return 3;
}
}
AABB Frustum::MinimalEnclosingAABB() const
{
AABB aabb;
aabb.SetNegativeInfinity();
for(int i = 0; i < 8; ++i)
aabb.Enclose(CornerPoint(i));
return aabb;
}
OBB Frustum::MinimalEnclosingOBB(float expandGuardband) const
{
assume(IsFinite());
assume(front.IsNormalized());
assume(up.IsNormalized());
OBB obb;
obb.pos = pos + (nearPlaneDistance + farPlaneDistance) * 0.5f * front;
obb.axis[1] = up;
obb.axis[2] = -front;
obb.axis[0] = Cross(obb.axis[1], obb.axis[2]);
obb.r = vec::zero;
for(int i = 0; i < 8; ++i)
obb.Enclose(CornerPoint(i));
// Expand the generated OBB very slightly to avoid numerical issues when
// testing whether this Frustum actually is contained inside the generated OBB.
obb.r.x += expandGuardband;
obb.r.y += expandGuardband;
obb.r.z += expandGuardband;
return obb;
}
Polyhedron Frustum::ToPolyhedron() const
{
// Note to maintainer: This function is an exact copy of AABB:ToPolyhedron() and OBB::ToPolyhedron().
Polyhedron p;
// Populate the corners of this Frustum.
// The will be in the order 0: ---, 1: --+, 2: -+-, 3: -++, 4: +--, 5: +-+, 6: ++-, 7: +++.
for(int i = 0; i < 8; ++i)
p.v.push_back(CornerPoint(i));
// Generate the 6 faces of this Frustum. The function Frustum::GetPlane() has a convention of returning
// the planes in order near,far,left,right,top,bottom, so follow the same convention here.
const int faces[6][4] =
{
{ 0, 4, 6, 2 }, // Z-: near plane
{ 1, 3, 7, 5 }, // Z+: far plane
{ 0, 2, 3, 1 }, // X-: left plane