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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
26
27#include "raytracing/frustum.h"
33#include <wx/debug.h>
34
35//#ifdef DEBUG
36#if 0
37static bool s_Run_Test_Cases = true;
38
40{
41 if( s_Run_Test_Cases == false )
42 return;
43
44 s_Run_Test_Cases = true;
45
46 // Test BBOX_2D
47 BBOX_2D bbox2d_A;
48 BBOX_2D bbox2d_B;
49
50 // Test a not initialized box conditions
51 bbox2d_A.Set( SFVEC2F( 1.0f, -1.0f ), SFVEC2F( -1.0f, 1.0f ) );
52
53 wxASSERT( bbox2d_A.IsInitialized() == true );
54 wxASSERT( bbox2d_A.Area() == 4.0f );
55 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.0f, 0.0f ) );
56 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 2.0f, 2.0f ) );
57 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
58 wxASSERT( bbox2d_A.Max() == SFVEC2F( 1.0f, 1.0f ) );
59 wxASSERT( bbox2d_A.Min() == SFVEC2F(-1.0f,-1.0f ) );
60 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
61 wxASSERT( bbox2d_A.MaxDimension() == 0 );
62 wxASSERT( bbox2d_A.Perimeter() == 8.0f );
63
64 bbox2d_A.Scale( 2.0f );
65
66 wxASSERT( bbox2d_A.IsInitialized() == true );
67 wxASSERT( bbox2d_A.Area() == 16.0f );
68 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.0f, 0.0f ) );
69 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 4.0f, 4.0f ) );
70 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
71 wxASSERT( bbox2d_A.Max() == SFVEC2F( 2.0f, 2.0f ) );
72 wxASSERT( bbox2d_A.Min() == SFVEC2F(-2.0f,-2.0f ) );
73 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
74 wxASSERT( bbox2d_A.MaxDimension() == 0 );
75 wxASSERT( bbox2d_A.Perimeter() == 16.0f );
76
77 bbox2d_B.Set( SFVEC2F(2.1f, 2.0f), SFVEC2F( 3.0f, 3.0f) );
78 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
79 bbox2d_B.Set( SFVEC2F(2.0f, 2.1f), SFVEC2F( 3.0f, 3.0f) );
80 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
81 bbox2d_B.Set( SFVEC2F(2.1f, 2.1f), SFVEC2F( 3.0f, 3.0f) );
82 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
83
84 bbox2d_B.Set( SFVEC2F(2.0f, 2.0f), SFVEC2F( 3.0f, 3.0f) );
85 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
86
87 bbox2d_A.Union( bbox2d_B );
88
89 wxASSERT( bbox2d_A.IsInitialized() == true );
90 wxASSERT( bbox2d_A.Area() == 25.0f );
91 wxASSERT( bbox2d_A.GetCenter() == SFVEC2F( 0.5f, 0.5f ) );
92 wxASSERT( bbox2d_A.GetExtent() == SFVEC2F( 5.0f, 5.0f ) );
93 wxASSERT( bbox2d_A.Inside( SFVEC2F( 0.0f, 0.0f ) ) == true );
94 wxASSERT( bbox2d_A.Inside( SFVEC2F( 3.0f, 3.0f ) ) == true );
95 wxASSERT( bbox2d_A.Inside( SFVEC2F(-2.0f,-2.0f ) ) == true );
96 wxASSERT( bbox2d_A.Max() == SFVEC2F( 3.0f, 3.0f ) );
97 wxASSERT( bbox2d_A.Min() == SFVEC2F(-2.0f,-2.0f ) );
98 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
99 wxASSERT( bbox2d_A.Intersects( bbox2d_A ) == true );
100 wxASSERT( bbox2d_A.MaxDimension() == 0 );
101 wxASSERT( bbox2d_A.Perimeter() == 20.0f );
102
103 bbox2d_A.Set( SFVEC2F( -1.0f, -1.0f ), SFVEC2F( 1.0f, 1.0f ) );
104 bbox2d_B.Set( SFVEC2F( -2.0f, -2.0f ), SFVEC2F( 2.0f, 2.0f ) );
105 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
106 wxASSERT( bbox2d_B.Intersects( bbox2d_A ) == true );
107
108 bbox2d_B.Set( SFVEC2F( 1.0f, 1.0f ), SFVEC2F( 1.0f, 1.0f ) );
109 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
110
111 bbox2d_B.Set( SFVEC2F( 1.1f, 1.1f ), SFVEC2F( 2.0f, 2.0f ) );
112 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == false );
113
114 bbox2d_B.Set( SFVEC2F(-0.5f, -0.5f ), SFVEC2F( 0.5f, 0.5f ) );
115 wxASSERT( bbox2d_A.Intersects( bbox2d_B ) == true );
116
117 // Test FILLED_CIRCLE_2D
118 FILLED_CIRCLE_2D filledCircle2d( SFVEC2F( 2.0f, 2.0f ), 1.0f );
119
120 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 2.0f ) ) == true );
121
122 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 3.0f ) ) == true );
123 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 3.0f, 2.0f ) ) == true );
124 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 1.0f, 2.0f ) ) == true );
125 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.0f, 1.0f ) ) == true );
126
127 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.8f, 2.8f ) ) == false );
128 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 2.6f, 2.6f ) ) == true );
129 wxASSERT( filledCircle2d.IsPointInside( SFVEC2F( 1.2f, 1.2f ) ) == false );
130
131 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 4.0f, 4.0f ) );
132 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
133
134 bbox2d_B.Set( SFVEC2F( 1.5f, 1.5f ), SFVEC2F( 2.5f, 2.5f ) );
135 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
136
137 // A box that does not intersect the sphere but still intersect the bbox of the sphere
138 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 1.2f, 1.2f ) );
139 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == false );
140
141 bbox2d_B.Set( SFVEC2F(-1.0f, -1.0f ), SFVEC2F( 0.5f, 0.5f ) );
142 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == false );
143
144 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 2.0f, 2.0f ) );
145 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
146
147 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 2.0f, 4.0f ) );
148 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
149
150 bbox2d_B.Set( SFVEC2F( 2.0f, 0.0f ), SFVEC2F( 4.0f, 4.0f ) );
151 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
152
153 bbox2d_B.Set( SFVEC2F( 0.0f, 2.0f ), SFVEC2F( 4.0f, 4.0f ) );
154 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
155
156 bbox2d_B.Set( SFVEC2F( 0.0f, 0.0f ), SFVEC2F( 4.0f, 2.0f ) );
157 wxASSERT( filledCircle2d.Intersects( bbox2d_B ) == true );
158
159
160 // Test ROUND_SEGMENT_2D
161 ROUND_SEGMENT_2D roundSegment2d( SFVEC2F( -1.0f, 0.0f ), SFVEC2F( 1.0f, 0.0f ), 2.0f );
162
163 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 0.0f ) ) == true );
164 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.0f, 0.0f ) ) == true );
165 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F(-1.0f, 0.0f ) ) == true );
166 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F(-2.0f, 0.0f ) ) == true );
167 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.0f, 0.0f ) ) == true );
168 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 1.0f ) ) == true );
169 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f,-1.0f ) ) == true );
170 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f, 1.1f ) ) == false );
171 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 0.0f,-1.1f ) ) == false );
172 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.1f, 0.0f ) ) == false );
173 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 2.1f, 0.0f ) ) == false );
174
175 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.8f, 0.8f ) ) == false );
176 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.8f,-0.8f ) ) == false );
177
178 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.8f, 0.8f ) ) == false );
179 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.8f,-0.8f ) ) == false );
180
181 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.6f, 0.6f ) ) == true );
182 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( 1.6f,-0.6f ) ) == true );
183
184 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.6f, 0.6f ) ) == true );
185 wxASSERT( roundSegment2d.IsPointInside( SFVEC2F( -1.6f,-0.6f ) ) == true );
186
187 bbox2d_A.Set( SFVEC2F(-2.0f,-1.0f), SFVEC2F( 2.0f, 1.0f) );
188 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
189
190 bbox2d_A.Set( SFVEC2F(-2.1f,-1.1f ), SFVEC2F( 2.1f, 1.1f ) );
191 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
192
193 bbox2d_A.Set( SFVEC2F( -1.9f,-0.9f ), SFVEC2F( 1.9f, 0.9f ) );
194 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
195
196 bbox2d_A.Set( SFVEC2F( -1.0f,-1.0f ), SFVEC2F( 1.0f, 1.0f ) );
197 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
198
199 bbox2d_A.Set( SFVEC2F( -1.0f,-0.5f ), SFVEC2F( 1.0f, 0.5f ) );
200 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == true );
201
202 bbox2d_A.Set( SFVEC2F( -4.0f,-0.5f ), SFVEC2F( -3.0f, 0.5f ) );
203 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
204
205 bbox2d_A.Set( SFVEC2F( 1.8f, 0.8f ), SFVEC2F( 2.0f, 1.0f ) );
206 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
207
208 bbox2d_A.Set( SFVEC2F( -2.0f, 0.8f ), SFVEC2F( -1.8f, 1.0f ) );
209 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
210
211 bbox2d_A.Set( SFVEC2F( -2.0f, -1.0f ), SFVEC2F( -1.8f, -0.8f ) );
212 wxASSERT( roundSegment2d.Intersects( bbox2d_A ) == false );
213
214 // Test CPOLYGON2D
215 Polygon2d_TestModule();
216#if 0
217 // Test Frustum
218 {
219 FRUSTUM frustum;
220
221 SFVEC3F ori = SFVEC3F( 0.0, 0.0, 0.0 );
222
223 float z = 10.0;
224
225 const RAY topLeft( ori, glm::normalize( ori - SFVEC3F(+1.0,+1.0, z) ) );
226 const RAY topRight( ori, glm::normalize( ori - SFVEC3F(-1.0,+1.0, z) ) );
227 const RAY bottomLeft( ori, glm::normalize( ori - SFVEC3F(+1.0,-1.0, z) ) );
228 const RAY bottomRight( ori, glm::normalize( ori - SFVEC3F(-1.0,-1.0, z) ) );
229
230 frustum.GenerateFrustum( topLeft, topRight, bottomLeft, bottomRight );
231
232 BBOX_3D bbox3d;
233
234 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, z ), SFVEC3F( +1.0f, +1.0f, z + 1.0f ) );
235 wxASSERT( frustum.Intersect( bbox3d ) == true );
236
237 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, z + z ), SFVEC3F( +1.0f, +1.0f, z + z + 1.0f ) );
238 wxASSERT( frustum.Intersect( bbox3d ) == true );
239
240 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, 1.0f ), SFVEC3F( 0.0f, 0.0f, 2.0f ) );
241 wxASSERT( frustum.Intersect( bbox3d ) == true );
242
243 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -z - 1.0f ), SFVEC3F( +1.0f, +1.0f, -z ) );
244 wxASSERT( frustum.Intersect( bbox3d ) == false );
245
246 bbox3d.Set( SFVEC3F( z - 1.0f, z - 1.0f, 0.0 ), SFVEC3F( z + 1.0f, z + 1.0f, 1.0 ) );
247 wxASSERT( frustum.Intersect( bbox3d ) == false );
248
249 bbox3d.Set( SFVEC3F( -z, -z, 0.0), SFVEC3F( -1.0f, -1.0f, 1.0f ) );
250 wxASSERT( frustum.Intersect( bbox3d ) == false );
251
252 bbox3d.Set( SFVEC3F( -z, -z, -1.0f ), SFVEC3F( +z, +z, 1.0f ) );
253 wxASSERT( frustum.Intersect( bbox3d ) == true );
254
255 bbox3d.Set( SFVEC3F( -z, -z, z-1 ), SFVEC3F( +z, +z, z + 1.0f ) );
256 wxASSERT( frustum.Intersect( bbox3d ) == true );
257
258 bbox3d.Set( SFVEC3F( 0.5, 0.5, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
259 wxASSERT( frustum.Intersect( bbox3d ) == true );
260
261 bbox3d.Set( SFVEC3F( 1.1, 1.0, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
262 wxASSERT( frustum.Intersect( bbox3d ) == false );
263 }
264 {
265 FRUSTUM frustum;
266
267 float z = 10.0;
268
269 SFVEC3F ori = SFVEC3F( 0.0, 0.0, z );
270
271 const RAY topLeft( ori, glm::normalize( SFVEC3F( -1.0, 1.0, 0.0 ) - ori ) );
272 const RAY topRight( ori, glm::normalize( SFVEC3F( +1.0, 1.0, 0.0 ) - ori ) );
273 const RAY bottomLeft( ori, glm::normalize( SFVEC3F( -1.0, -1.0, 0.0 ) - ori ) );
274 const RAY bottomRight( ori, glm::normalize( SFVEC3F( +1.0, -1.0, 0.0 ) - ori ) );
275
276 frustum.GenerateFrustum( topLeft, topRight, bottomLeft, bottomRight );
277
278 BBOX_3D bbox3d;
279
280 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -z), SFVEC3F( +1.0f, +1.0f, -z + 1.0f ) );
281 wxASSERT( frustum.Intersect( bbox3d ) == true );
282
283 bbox3d.Set( SFVEC3F( -1.0f, -1.0f, -( z + z ) ), SFVEC3F( +1.0f, +1.0f, -( z + z + 1.0f ) ) );
284 wxASSERT( frustum.Intersect( bbox3d ) == true );
285
286 bbox3d.Set( SFVEC3F(- 1.0f, -1.0f, 1.0f ), SFVEC3F( 0.0f, 0.0f, 2.0f ) );
287 wxASSERT( frustum.Intersect( bbox3d ) == true );
288
289 // !TODO: The frustum alg does not exclude all the situations
290 //bbox3d.Set( SFVEC3F(-1.0f, -1.0f, z+1.0f), SFVEC3F(+1.0f,+1.0f, +z+2.0f) );
291 //wxASSERT( frustum.Intersect( bbox3d ) == false );
292
293 bbox3d.Set( SFVEC3F( z - 1.0f, z - 1.0f, 0.0), SFVEC3F( z + 1.0f, z + 1.0f, 1.0 ) );
294 wxASSERT( frustum.Intersect( bbox3d ) == false );
295
296 bbox3d.Set( SFVEC3F( -z, -z, 0.0 ), SFVEC3F( -1.0f,-1.0f, 1.0f ) );
297 wxASSERT( frustum.Intersect( bbox3d ) == false );
298
299 bbox3d.Set( SFVEC3F( -z, -z, -1.0f ), SFVEC3F( +z, +z, 1.0f ) );
300 wxASSERT( frustum.Intersect( bbox3d ) == true );
301
302 bbox3d.Set( SFVEC3F( -z, -z, -( z - 1 ) ), SFVEC3F( +z, +z, -( z + 1.0f ) ) );
303 wxASSERT( frustum.Intersect( bbox3d ) == true );
304
305 bbox3d.Set( SFVEC3F( 0.5, 0.5, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
306 wxASSERT( frustum.Intersect( bbox3d ) == true );
307
308 bbox3d.Set( SFVEC3F( 1.1, 1.0, 0.0 ), SFVEC3F( 2.0, 2.0, z ) );
309 wxASSERT( frustum.Intersect( bbox3d ) == false );
310 }
311#endif
312}
313#endif
2D bounding box class definition.
Bounding Box class definition.
Implement a frustum that is used for ray packet tests.
Manage a bounding box defined by two SFVEC2F min max points.
Definition bbox_2d.h:38
float Perimeter() const
Definition bbox_2d.cpp:138
SFVEC2F GetCenter() const
Definition bbox_2d.cpp:115
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition bbox_2d.cpp:207
unsigned int MaxDimension() const
Definition bbox_2d.cpp:127
SFVEC2F GetExtent() const
Definition bbox_2d.cpp:121
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition bbox_2d.cpp:89
void Scale(float aScale)
Scale a bounding box by its center.
Definition bbox_2d.cpp:146
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition bbox_2d.cpp:75
const SFVEC2F & Min() const
Definition bbox_2d.h:171
float Area() const
Calculate the area of a bounding box.
Definition bbox_2d.cpp:238
bool Inside(const SFVEC2F &aPoint) const
Check is a point is inside this bounding box.
Definition bbox_2d.cpp:219
const SFVEC2F & Max() const
Definition bbox_2d.h:176
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Set bounding box with new parameters.
Definition bbox_2d.cpp:57
Manage a bounding box defined by two SFVEC3F min max points.
Definition bbox_3d.h:39
void Set(const SFVEC3F &aPbMin, const SFVEC3F &aPbMax)
Set bounding box with new parameters.
Definition bbox_3d.cpp:64
bool Intersect(const BBOX_3D &aBBox) const
Intersect aBBox with this frustum.
Definition frustum.cpp:59
void GenerateFrustum(const RAY &topLeft, const RAY &topRight, const RAY &bottomLeft, const RAY &bottomRight)
Definition frustum.cpp:33
Definition ray.h:59
void Run_3d_viewer_test_cases()
glm::vec2 SFVEC2F
Definition xv3d_types.h:38
glm::vec3 SFVEC3F
Definition xv3d_types.h:40