KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_cases.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
31#include "raytracing/frustum.h"
37#include <wx/debug.h>
38
39//#ifdef DEBUG
40#if 0
41static bool s_Run_Test_Cases = true;
42
44{
45 if( s_Run_Test_Cases == false )
46 return;
47
48 s_Run_Test_Cases = true;
49
50 // Test BBOX_2D
51 BBOX_2D bbox2d_A;
52 BBOX_2D bbox2d_B;
53
54 // Test a not initialized box conditions
55 bbox2d_A.Set( SFVEC2F( 1.0f, -1.0f ), SFVEC2F( -1.0f, 1.0f ) );
56
57 wxASSERT( bbox2d_A.IsInitialized() == true );
58 wxASSERT( bbox2d_A.Area() == 4.0f );
59 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.0f, 0.0f ) );
60 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 2.0f, 2.0f ) );
61 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
62 wxASSERT( bbox2d_A.Max() == SFVEC2F( 1.0f, 1.0f ) );
63 wxASSERT( bbox2d_A.Min() == SFVEC2F(-1.0f,-1.0f ) );
64 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
65 wxASSERT( bbox2d_A.MaxDimension() == 0 );
66 wxASSERT( bbox2d_A.Perimeter() == 8.0f );
67
68 bbox2d_A.Scale( 2.0f );
69
70 wxASSERT( bbox2d_A.IsInitialized() == true );
71 wxASSERT( bbox2d_A.Area() == 16.0f );
72 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.0f, 0.0f ) );
73 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 4.0f, 4.0f ) );
74 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
75 wxASSERT( bbox2d_A.Max() == SFVEC2F( 2.0f, 2.0f ) );
76 wxASSERT( bbox2d_A.Min() == SFVEC2F(-2.0f,-2.0f ) );
77 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
78 wxASSERT( bbox2d_A.MaxDimension() == 0 );
79 wxASSERT( bbox2d_A.Perimeter() == 16.0f );
80
81 bbox2d_B.Set( SFVEC2F(2.1f, 2.0f), SFVEC2F( 3.0f, 3.0f) );
82 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
83 bbox2d_B.Set( SFVEC2F(2.0f, 2.1f), SFVEC2F( 3.0f, 3.0f) );
84 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
85 bbox2d_B.Set( SFVEC2F(2.1f, 2.1f), SFVEC2F( 3.0f, 3.0f) );
86 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
87
88 bbox2d_B.Set( SFVEC2F(2.0f, 2.0f), SFVEC2F( 3.0f, 3.0f) );
89 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
90
91 bbox2d_A.Union( bbox2d_B );
92
93 wxASSERT( bbox2d_A.IsInitialized() == true );
94 wxASSERT( bbox2d_A.Area() == 25.0f );
95 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.5f, 0.5f ) );
96 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 5.0f, 5.0f ) );
97 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
98 wxASSERT( bbox2d_A.Inside( SFVEC2F( 3.0f, 3.0f ) ) == true );
99 wxASSERT( bbox2d_A.Inside( SFVEC2F(-2.0f,-2.0f ) ) == true );
100 wxASSERT( bbox2d_A.Max() == SFVEC2F( 3.0f, 3.0f ) );
101 wxASSERT( bbox2d_A.Min() == SFVEC2F(-2.0f,-2.0f ) );
102 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
103 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
104 wxASSERT( bbox2d_A.MaxDimension() == 0 );
105 wxASSERT( bbox2d_A.Perimeter() == 20.0f );
106
107 bbox2d_A.Set( SFVEC2F( -1.0f, -1.0f ), SFVEC2F( 1.0f, 1.0f ) );
108 bbox2d_B.Set( SFVEC2F( -2.0f, -2.0f ), SFVEC2F( 2.0f, 2.0f ) );
109 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
110 wxASSERT( bbox2d_B.Intersects( bbox2d_A ) == true );
111
112 bbox2d_B.Set( SFVEC2F( 1.0f, 1.0f ), SFVEC2F( 1.0f, 1.0f ) );
113 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
114
115 bbox2d_B.Set( SFVEC2F( 1.1f, 1.1f ), SFVEC2F( 2.0f, 2.0f ) );
116 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
117
118 bbox2d_B.Set( SFVEC2F(-0.5f, -0.5f ), SFVEC2F( 0.5f, 0.5f ) );
119 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
120
121 // Test FILLED_CIRCLE_2D
122 FILLED_CIRCLE_2D filledCircle2d( SFVEC2F( 2.0f, 2.0f ), 1.0f );
123
124 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 2.0f ) ) == true );
125
126 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 3.0f ) ) == true );
127 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 3.0f, 2.0f ) ) == true );
128 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 1.0f, 2.0f ) ) == true );
129 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 1.0f ) ) == true );
130
131 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.8f, 2.8f ) ) == false );
132 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.6f, 2.6f ) ) == true );
133 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 1.2f, 1.2f ) ) == false );
134
135 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 4.0f, 4.0f ) );
136 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
137
138 bbox2d_B.Set( SFVEC2F( 1.5f, 1.5f ), SFVEC2F( 2.5f, 2.5f ) );
139 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
140
141 // A box that does not intersect the sphere but still intersect the bbox of the sphere
142 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 1.2f, 1.2f ) );
143 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == false );
144
145 bbox2d_B.Set( SFVEC2F(-1.0f, -1.0f ), SFVEC2F( 0.5f, 0.5f ) );
146 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == false );
147
148 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 2.0f, 2.0f ) );
149 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
150
151 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 2.0f, 4.0f ) );
152 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
153
154 bbox2d_B.Set( SFVEC2F( 2.0f, 0.0f ), SFVEC2F( 4.0f, 4.0f ) );
155 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
156
157 bbox2d_B.Set( SFVEC2F( 0.0f, 2.0f ), SFVEC2F( 4.0f, 4.0f ) );
158 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
159
160 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 4.0f, 2.0f ) );
161 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
162
163
164 // Test ROUND_SEGMENT_2D
165 ROUND_SEGMENT_2D roundSegment2d( SFVEC2F( -1.0f, 0.0f ), SFVEC2F( 1.0f, 0.0f ), 2.0f );
166
167 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 0.0f ) ) == true );
168 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.0f, 0.0f ) ) == true );
169 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F(-1.0f, 0.0f ) ) == true );
170 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F(-2.0f, 0.0f ) ) == true );
171 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.0f, 0.0f ) ) == true );
172 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 1.0f ) ) == true );
173 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f,-1.0f ) ) == true );
174 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 1.1f ) ) == false );
175 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f,-1.1f ) ) == false );
176 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.1f, 0.0f ) ) == false );
177 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.1f, 0.0f ) ) == false );
178
179 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.8f, 0.8f ) ) == false );
180 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.8f,-0.8f ) ) == false );
181
182 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.8f, 0.8f ) ) == false );
183 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.8f,-0.8f ) ) == false );
184
185 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.6f, 0.6f ) ) == true );
186 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.6f,-0.6f ) ) == true );
187
188 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.6f, 0.6f ) ) == true );
189 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.6f,-0.6f ) ) == true );
190
191 bbox2d_A.Set( SFVEC2F(-2.0f,-1.0f), SFVEC2F( 2.0f, 1.0f) );
192 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
193
194 bbox2d_A.Set( SFVEC2F(-2.1f,-1.1f ), SFVEC2F( 2.1f, 1.1f ) );
195 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
196
197 bbox2d_A.Set( SFVEC2F( -1.9f,-0.9f ), SFVEC2F( 1.9f, 0.9f ) );
198 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
199
200 bbox2d_A.Set( SFVEC2F( -1.0f,-1.0f ), SFVEC2F( 1.0f, 1.0f ) );
201 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
202
203 bbox2d_A.Set( SFVEC2F( -1.0f,-0.5f ), SFVEC2F( 1.0f, 0.5f ) );
204 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
205
206 bbox2d_A.Set( SFVEC2F( -4.0f,-0.5f ), SFVEC2F( -3.0f, 0.5f ) );
207 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
208
209 bbox2d_A.Set( SFVEC2F( 1.8f, 0.8f ), SFVEC2F( 2.0f, 1.0f ) );
210 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
211
212 bbox2d_A.Set( SFVEC2F( -2.0f, 0.8f ), SFVEC2F( -1.8f, 1.0f ) );
213 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
214
215 bbox2d_A.Set( SFVEC2F( -2.0f, -1.0f ), SFVEC2F( -1.8f, -0.8f ) );
216 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
217
218 // Test CPOLYGON2D
220#if 0
221 // Test Frustum
222 {
223 FRUSTUM frustum;
224
225 SFVEC3F ori = SFVEC3F( 0.0, 0.0, 0.0 );
226
227 float z = 10.0;
228
229 const RAY topLeft( ori, glm::normalize( ori - SFVEC3F(+1.0,+1.0, z) ) );
230 const RAY topRight( ori, glm::normalize( ori - SFVEC3F(-1.0,+1.0, z) ) );
231 const RAY bottomLeft( ori, glm::normalize( ori - SFVEC3F(+1.0,-1.0, z) ) );
232 const RAY bottomRight( ori, glm::normalize( ori - SFVEC3F(-1.0,-1.0, z) ) );
233
234 frustum.GenerateFrustum( topLeft, topRight, bottomLeft, bottomRight );
235
236 BBOX_3D bbox3d;
237
238 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, z ), SFVEC3F( +1.0f, +1.0f, z + 1.0f ) );
239 wxASSERT( frustum.Intersect( bbox3d ) == true );
240
241 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, z + z ), SFVEC3F( +1.0f, +1.0f, z + z + 1.0f ) );
242 wxASSERT( frustum.Intersect( bbox3d ) == true );
243
244 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, 1.0f ), SFVEC3F( 0.0f, 0.0f, 2.0f ) );
245 wxASSERT( frustum.Intersect( bbox3d ) == true );
246
247 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -z - 1.0f ), SFVEC3F( +1.0f, +1.0f, -z ) );
248 wxASSERT( frustum.Intersect( bbox3d ) == false );
249
250 bbox3d.Set( SFVEC3F( z - 1.0f, z - 1.0f, 0.0 ), SFVEC3F( z + 1.0f, z + 1.0f, 1.0 ) );
251 wxASSERT( frustum.Intersect( bbox3d ) == false );
252
253 bbox3d.Set( SFVEC3F( -z, -z, 0.0), SFVEC3F( -1.0f, -1.0f, 1.0f ) );
254 wxASSERT( frustum.Intersect( bbox3d ) == false );
255
256 bbox3d.Set( SFVEC3F( -z, -z, -1.0f ), SFVEC3F( +z, +z, 1.0f ) );
257 wxASSERT( frustum.Intersect( bbox3d ) == true );
258
259 bbox3d.Set( SFVEC3F( -z, -z, z-1 ), SFVEC3F( +z, +z, z + 1.0f ) );
260 wxASSERT( frustum.Intersect( bbox3d ) == true );
261
262 bbox3d.Set( SFVEC3F( 0.5, 0.5, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
263 wxASSERT( frustum.Intersect( bbox3d ) == true );
264
265 bbox3d.Set( SFVEC3F( 1.1, 1.0, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
266 wxASSERT( frustum.Intersect( bbox3d ) == false );
267 }
268 {
269 FRUSTUM frustum;
270
271 float z = 10.0;
272
273 SFVEC3F ori = SFVEC3F( 0.0, 0.0, z );
274
275 const RAY topLeft( ori, glm::normalize( SFVEC3F( -1.0, 1.0, 0.0 ) - ori ) );
276 const RAY topRight( ori, glm::normalize( SFVEC3F( +1.0, 1.0, 0.0 ) - ori ) );
277 const RAY bottomLeft( ori, glm::normalize( SFVEC3F( -1.0, -1.0, 0.0 ) - ori ) );
278 const RAY bottomRight( ori, glm::normalize( SFVEC3F( +1.0, -1.0, 0.0 ) - ori ) );
279
280 frustum.GenerateFrustum( topLeft, topRight, bottomLeft, bottomRight );
281
282 BBOX_3D bbox3d;
283
284 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -z), SFVEC3F( +1.0f, +1.0f, -z + 1.0f ) );
285 wxASSERT( frustum.Intersect( bbox3d ) == true );
286
287 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -( z + z ) ), SFVEC3F( +1.0f, +1.0f, -( z + z + 1.0f ) ) );
288 wxASSERT( frustum.Intersect( bbox3d ) == true );
289
290 bbox3d.Set( SFVEC3F(- 1.0f, -1.0f, 1.0f ), SFVEC3F( 0.0f, 0.0f, 2.0f ) );
291 wxASSERT( frustum.Intersect( bbox3d ) == true );
292
293 // !TODO: The frustum alg does not exclude all the situations
294 //bbox3d.Set( SFVEC3F(-1.0f, -1.0f, z+1.0f), SFVEC3F(+1.0f,+1.0f, +z+2.0f) );
295 //wxASSERT( frustum.Intersect( bbox3d ) == false );
296
297 bbox3d.Set( SFVEC3F( z - 1.0f, z - 1.0f, 0.0), SFVEC3F( z + 1.0f, z + 1.0f, 1.0 ) );
298 wxASSERT( frustum.Intersect( bbox3d ) == false );
299
300 bbox3d.Set( SFVEC3F( -z, -z, 0.0 ), SFVEC3F( -1.0f,-1.0f, 1.0f ) );
301 wxASSERT( frustum.Intersect( bbox3d ) == false );
302
303 bbox3d.Set( SFVEC3F( -z, -z, -1.0f ), SFVEC3F( +z, +z, 1.0f ) );
304 wxASSERT( frustum.Intersect( bbox3d ) == true );
305
306 bbox3d.Set( SFVEC3F( -z, -z, -( z - 1 ) ), SFVEC3F( +z, +z, -( z + 1.0f ) ) );
307 wxASSERT( frustum.Intersect( bbox3d ) == true );
308
309 bbox3d.Set( SFVEC3F( 0.5, 0.5, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
310 wxASSERT( frustum.Intersect( bbox3d ) == true );
311
312 bbox3d.Set( SFVEC3F( 1.1, 1.0, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
313 wxASSERT( frustum.Intersect( bbox3d ) == false );
314 }
315#endif
316}
317#endif
2D bounding box class definition.
Bounding Box class definition.
Implement a frustum that is used for ray packet tests.
void Polygon2d_TestModule()
Manage a bounding box defined by two SFVEC2F min max points.
Definition: bbox_2d.h:42
float Perimeter() const
Definition: bbox_2d.cpp:142
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211
unsigned int MaxDimension() const
Definition: bbox_2d.cpp:131
SFVEC2F GetExtent() const
Definition: bbox_2d.cpp:125
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition: bbox_2d.cpp:93
void Scale(float aScale)
Scale a bounding box by its center.
Definition: bbox_2d.cpp:150
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
const SFVEC2F & Min() const
Definition: bbox_2d.h:175
float Area() const
Calculate the area of a bounding box.
Definition: bbox_2d.cpp:242
bool Inside(const SFVEC2F &aPoint) const
Check is a point is inside this bounding box.
Definition: bbox_2d.cpp:223
const SFVEC2F & Max() const
Definition: bbox_2d.h:180
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Set bounding box with new parameters.
Definition: bbox_2d.cpp:61
Manage a bounding box defined by two SFVEC3F min max points.
Definition: bbox_3d.h:42
void Set(const SFVEC3F &aPbMin, const SFVEC3F &aPbMax)
Set bounding box with new parameters.
Definition: bbox_3d.cpp:68
bool Intersect(const BBOX_3D &aBBox) const
Intersect aBBox with this frustum.
Definition: frustum.cpp:58
void GenerateFrustum(const RAY &topLeft, const RAY &topRight, const RAY &bottomLeft, const RAY &bottomRight)
Definition: frustum.cpp:37
Definition: ray.h:63
void Run_3d_viewer_test_cases()
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44