1 module doml.tests.frustum_intersect_test;
2 
3 import doml.tests.dunit_tests;
4 import Math = doml.math;
5 import doml.matrix_4d;
6 import doml.axis_angle_4d;
7 import doml.frustum_intersection;
8 import std.stdio;
9 
10 
11 /*
12  * The MIT License
13  *
14  * Copyright (c) 2015-2021 JOML.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a copy
17  * of this software and associated documentation files (the "Software"), to deal
18  * in the Software without restriction, including without limitation the rights
19  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20  * copies of the Software, and to permit persons to whom the Software is
21  * furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included in
24  * all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32  * THE SOFTWARE.
33  */
34 
35 /**
36  * Tests for the {@link FrustumIntersection} class.
37  * 
38  * @author Kai Burjack
39  */
40 unittest {
41 
42     writeln("\nBEGINNING FRUSTUM INTERSECT TEST\n");
43 
44     // testIsSphereInFrustumOrtho
45     {
46         Matrix4d m = Matrix4d().ortho(-1, 1, -1, 1, -1, 1);
47         FrustumIntersection c = FrustumIntersection(m);
48         assertTrue(c.testSphere(1, 0, 0, 0.1f));
49         assertFalse(c.testSphere(1.2f, 0, 0, 0.1f));
50     }
51 
52     // testIsSphereInFrustumPerspective
53     {
54         Matrix4d m = Matrix4d().perspective(cast(double) Math.PI / 2.0f, 1.0f, 0.1f, 100.0f);
55         FrustumIntersection c = FrustumIntersection(m);
56         assertTrue(c.testSphere(1, 0, -2, 0.1f));
57         assertFalse(c.testSphere(4f, 0, -2, 1.0f));
58     }
59 
60     // testIsAabInFrustumOrtho
61     {
62         Matrix4d m = Matrix4d().ortho(-1, 1, -1, 1, -1, 1);
63         FrustumIntersection c = FrustumIntersection(m);
64         assertEquals(FrustumIntersection.INTERSECT, c.intersectAab(-20, -2, 0, 20, 2, 0));
65         assertEquals(FrustumIntersection.INSIDE, c.intersectAab(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f));
66         assertEquals(Matrix4d.PLANE_PX, c.intersectAab(1.1f, 0, 0, 2, 2, 2));
67         c.set(Matrix4d().ortho(-1, 1, -1, 1, -1, 1));
68         assertEquals(FrustumIntersection.INTERSECT, c.intersectAab(0, 0, 0, 2, 2, 2));
69         assertEquals(Matrix4d.PLANE_PX, c.intersectAab(1.1f, 0, 0, 2, 2, 2));
70         c.set(Matrix4d());
71         assertEquals(FrustumIntersection.INTERSECT, c.intersectAab(0.5f, 0.5f, 0.5f, 2, 2, 2));
72         assertEquals(Matrix4d.PLANE_PX, c.intersectAab(1.5f, 0.5f, 0.5f, 2, 2, 2));
73         assertEquals(Matrix4d.PLANE_NX, c.intersectAab(-2.5f, 0.5f, 0.5f, -1.5f, 2, 2));
74         assertEquals(Matrix4d.PLANE_NY, c.intersectAab(-0.5f, -2.5f, 0.5f, 1.5f, -2, 2));
75     }
76 
77     // testIsAabInPerspective
78     {
79         Matrix4d m = Matrix4d().perspective(cast(double) Math.PI / 2.0f, 1.0f, 0.1f, 100.0f);
80         FrustumIntersection c = FrustumIntersection(m);
81         assertEquals(FrustumIntersection.INSIDE, c.intersectAab(0, 0, -7, 1, 1, -5));
82         assertEquals(FrustumIntersection.PLANE_PX, c.intersectAab(1.1f, 0, 0, 2, 2, 2));
83         assertEquals(FrustumIntersection.PLANE_PX, c.intersectAab(4, 4, -3, 5, 5, -5));
84         assertEquals(FrustumIntersection.PLANE_NY, c.intersectAab(-6, -6, -2, -1, -4, -4));
85     }
86 
87     // testIsPointInPerspective
88     {
89         Matrix4d m = Matrix4d().perspective(cast(double) Math.PI / 2.0f, 1.0f, 0.1f, 100.0f);
90         FrustumIntersection c = FrustumIntersection(m);
91         assertTrue(c.testPoint(0, 0, -5));
92         assertFalse(c.testPoint(0, 6, -5));
93     }
94 
95     // testIsAabInPerspectiveMask
96     {
97         Matrix4d m = Matrix4d().perspective(cast(double) Math.PI / 2.0f, 1.0f, 0.1f, 100.0f);
98         FrustumIntersection c = FrustumIntersection(m);
99         assertEquals(FrustumIntersection.INTERSECT, c.intersectAab(5.1f, 0, -3, 8, 2, -2, ~0 ^ FrustumIntersection.PLANE_MASK_PX));
100         assertEquals(FrustumIntersection.INTERSECT, c.intersectAab(-6.1f, 0, -3, -5, 2, -2, ~0 ^ FrustumIntersection.PLANE_MASK_NX));
101         assertEquals(Matrix4d.PLANE_NX, c.intersectAab(-6.1f, 0, -3, -5, 2, -2, FrustumIntersection.PLANE_MASK_NX));
102         assertEquals(Matrix4d.PLANE_NX, c.intersectAab(-6.1f, 0, -3, -5, 2, -2, ~0, Matrix4d.PLANE_NX));
103     }
104 
105     writeln("PASSED");
106 
107 }