1 /*
2 Copyright (c) 2014-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Copyright: Timur Gafarov 2014-2021.
31  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
32  * Authors: Timur Gafarov, Andrey Penechko, Roman Vlasov
33  */
34 module dlib.geometry.frustum;
35 
36 import std.math;
37 import dlib.math.vector;
38 import dlib.math.matrix;
39 import dlib.geometry.plane;
40 import dlib.geometry.aabb;
41 import dlib.geometry.sphere;
42 
43 /// Frustum object
44 struct Frustum
45 {
46     union
47     {
48         Plane[6] planes;
49         struct
50         {
51             Plane leftPlane;
52             Plane rightPlane;
53             Plane bottomPlane;
54             Plane topPlane;
55             Plane farPlane;
56             Plane nearPlane;
57         }
58     }
59 
60     this(Matrix4x4f mvp)
61     {
62         fromMVP(mvp);
63     }
64 
65     void fromMVP(Matrix4x4f mvp)
66     {
67         leftPlane.a = mvp[3]  + mvp[0];
68         leftPlane.b = mvp[7]  + mvp[4];
69         leftPlane.c = mvp[11] + mvp[8];
70         leftPlane.d = mvp[15] + mvp[12];
71         leftPlane.normalize();
72         leftPlane.vectorof = -leftPlane.vectorof;
73 
74         rightPlane.a = mvp[3]  - mvp[0];
75         rightPlane.b = mvp[7]  - mvp[4];
76         rightPlane.c = mvp[11] - mvp[8];
77         rightPlane.d = mvp[15] - mvp[12];
78         rightPlane.normalize();
79         rightPlane.vectorof = -rightPlane.vectorof;
80 
81         bottomPlane.a = mvp[3]  + mvp[1];
82         bottomPlane.b = mvp[7]  + mvp[5];
83         bottomPlane.c = mvp[11] + mvp[9];
84         bottomPlane.d = mvp[15] + mvp[13];
85         bottomPlane.normalize();
86         bottomPlane.vectorof = -bottomPlane.vectorof;
87 
88         topPlane.a = mvp[3]  - mvp[1];
89         topPlane.b = mvp[7]  - mvp[5];
90         topPlane.c = mvp[11] - mvp[9];
91         topPlane.d = mvp[15] - mvp[13];
92         topPlane.normalize();
93         topPlane.vectorof = -topPlane.vectorof;
94 
95         farPlane.a = mvp[3]  - mvp[2];
96         farPlane.b = mvp[7]  - mvp[6];
97         farPlane.c = mvp[11] - mvp[10];
98         farPlane.d = mvp[15] - mvp[14];
99         farPlane.normalize();
100         farPlane.vectorof = -farPlane.vectorof;
101 
102         nearPlane.a = mvp[3]  + mvp[2];
103         nearPlane.b = mvp[7]  + mvp[6];
104         nearPlane.c = mvp[11] + mvp[10];
105         nearPlane.d = mvp[15] + mvp[14];
106         nearPlane.normalize();
107         nearPlane.vectorof = -nearPlane.vectorof;
108     }
109 
110     bool containsPoint(Vector3f point, bool checkNearPlane = false)
111     {
112         int res = 0;
113 
114         foreach(i, ref p; planes)
115         {
116             if (i == 5 && !checkNearPlane)
117                 break;
118 
119             if (p.distance(point) <= 0.0f)
120                 res++;
121         }
122 
123         return (res == (checkNearPlane? 6 : 5));
124     }
125 
126     bool intersectsSphere(Sphere sphere)
127     {
128 	    float d;
129 
130 	    foreach(i, ref p; planes)
131         {
132 		    d = p.distance(sphere.center);
133 
134 		    if (d > sphere.radius)
135 			    return false;
136 	    }
137 
138 	    return true;
139     }
140 
141     bool intersectsAABB(
142         AABB aabb,
143         bool checkBoundariesOnly = false,
144         bool checkNearPlane = true)
145     {
146         bool result = !checkBoundariesOnly; // Inside
147 
148         foreach (i, ref plane; planes)
149         {
150             if (i == 5 && !checkNearPlane)
151                 break;
152 
153             float d = dot(aabb.center, -plane.normal);
154 
155             float r = aabb.size.x * abs(plane.normal.x) +
156                       aabb.size.y * abs(plane.normal.y) +
157                       aabb.size.z * abs(plane.normal.z);
158 
159             float d_p_r = d + r;
160             float d_m_r = d - r;
161 
162             if (d_p_r < plane.d)
163             {
164                 result = false; // Outside
165                 break;
166             }
167             else if(d_m_r < plane.d)
168                 result = true; // Intersect
169         }
170 
171         return result;
172     }
173 }
174