-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbounding_box.cpp
More file actions
536 lines (495 loc) · 18.2 KB
/
bounding_box.cpp
File metadata and controls
536 lines (495 loc) · 18.2 KB
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
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <geode/geometry/bounding_box.hpp>
#include <limits>
#include <geode/geometry/basic_objects/infinite_line.hpp>
#include <geode/geometry/basic_objects/segment.hpp>
#include <geode/geometry/basic_objects/tetrahedron.hpp>
#include <geode/geometry/basic_objects/triangle.hpp>
#include <geode/geometry/information.hpp>
#include <geode/geometry/perpendicular.hpp>
#include <geode/geometry/position.hpp>
namespace
{
template < geode::index_t dimension >
bool line_intersects( const geode::BoundingBox< dimension >& box,
const geode::GenericLine< geode::RefPoint< dimension >, dimension >&
line );
template <>
bool line_intersects< 3 >( const geode::BoundingBox< 3 >& box,
const geode::GenericLine< geode::RefPoint< 3 >, 3 >& line )
{
const auto box_half_extent = box.diagonal() / 2.;
const geode::Vector3D line_translated_origin{ box.center(),
line.origin() };
const auto origin_cross_direction =
line.direction().cross( line_translated_origin );
const geode::Point3D abs_line_direction{
{ std::fabs( line.direction().value( 0 ) ),
std::fabs( line.direction().value( 1 ) ),
std::fabs( line.direction().value( 2 ) ) }
};
for( const auto i : geode::LRange{ 3 } )
{
const auto ii = i == 2 ? 0 : i + 1;
const auto iii = ii == 2 ? 0 : ii + 1;
if( std::fabs( origin_cross_direction.value( i ) )
- ( box_half_extent.value( ii )
* abs_line_direction.value( iii )
+ box_half_extent.value( iii )
* abs_line_direction.value( ii ) )
> geode::GLOBAL_EPSILON )
{
return false;
}
}
return true;
}
template <>
bool line_intersects< 2 >( const geode::BoundingBox< 2 >& box,
const geode::GenericLine< geode::RefPoint< 2 >, 2 >& line )
{
const auto box_center = box.center();
const auto box_half_extent = box.diagonal() / 2.;
const geode::Vector2D line_translated_origin{ box_center,
line.origin() };
const auto lhs = std::fabs(
dot_perpendicular( line.direction(), line_translated_origin ) );
const auto rhs = box_half_extent.value( 0 )
* std::fabs( line.direction().value( 1 ) )
+ box_half_extent.value( 1 )
* std::fabs( line.direction().value( 0 ) );
return lhs - rhs <= geode::GLOBAL_EPSILON;
}
template <>
bool line_intersects< 1 >( const geode::BoundingBox< 1 >& box,
const geode::GenericLine< geode::RefPoint< 1 >, 1 >& line )
{
if( box.diagonal().dot( line.direction() ) > 0 )
{
return line.origin().value( 0 ) < box.min().value( 0 );
}
return line.origin().value( 0 ) > box.max().value( 0 );
}
template < geode::index_t dimension, typename Condition >
std::tuple< geode::local_index_t, double > test_axes(
const geode::Vector< dimension >& diagonal,
double length,
Condition predicate )
{
geode::local_index_t axis{ 0 };
for( const auto i : geode::LRange{ dimension } )
{
if( predicate( diagonal.value( i ), length ) )
{
length = diagonal.value( i );
axis = i;
}
}
return std::make_tuple( axis, length );
}
} // namespace
namespace geode
{
template < index_t dimension >
BoundingBox< dimension >::BoundingBox()
{
for( const auto i : LRange{ dimension } )
{
min_.set_value( i, std::numeric_limits< double >::max() );
max_.set_value( i, std::numeric_limits< double >::lowest() );
}
}
template < index_t dimension >
BoundingBox< dimension >::BoundingBox(
Point< dimension > min, Point< dimension > max )
: min_{ std::move( min ) }, max_{ std::move( max ) }
{
}
template < index_t dimension >
BoundingBox< dimension >::~BoundingBox() = default;
template < index_t dimension >
BoundingBox< dimension >::BoundingBox( const BoundingBox& ) = default;
template < index_t dimension >
BoundingBox< dimension >& BoundingBox< dimension >::operator=(
const BoundingBox& ) = default;
template < index_t dimension >
BoundingBox< dimension >::BoundingBox( BoundingBox&& ) noexcept = default;
template < index_t dimension >
BoundingBox< dimension >& BoundingBox< dimension >::operator=(
BoundingBox&& ) noexcept = default;
template < index_t dimension >
const Point< dimension >& BoundingBox< dimension >::min() const
{
return min_;
}
template < index_t dimension >
const Point< dimension >& BoundingBox< dimension >::max() const
{
return max_;
}
template < index_t dimension >
void BoundingBox< dimension >::add_point( const Point< dimension >& point )
{
for( const auto i : LRange{ dimension } )
{
min_.set_value( i, std::min( min_.value( i ), point.value( i ) ) );
max_.set_value( i, std::max( max_.value( i ), point.value( i ) ) );
}
}
template < index_t dimension >
void BoundingBox< dimension >::extends( double length )
{
for( const auto i : LRange{ dimension } )
{
min_.set_value( i, min_.value( i ) - length );
max_.set_value( i, max_.value( i ) + length );
}
}
template < index_t dimension >
void BoundingBox< dimension >::add_box(
const BoundingBox< dimension >& box )
{
add_point( box.min_ );
add_point( box.max_ );
}
template < index_t dimension >
bool BoundingBox< dimension >::contains(
const Point< dimension >& point ) const
{
for( const auto i : LRange{ dimension } )
{
if( point.value( i ) < min_.value( i )
|| point.value( i ) > max_.value( i ) )
{
return false;
}
}
return true;
}
template < index_t dimension >
bool BoundingBox< dimension >::contains(
const BoundingBox< dimension >& bbox ) const
{
return contains( bbox.min_ ) && contains( bbox.max_ );
}
template < index_t dimension >
bool BoundingBox< dimension >::intersects(
const BoundingBox< dimension >& box ) const
{
for( const auto i : LRange{ dimension } )
{
if( max_.value( i ) < box.min_.value( i )
|| min_.value( i ) > box.max_.value( i ) )
{
return false;
}
}
return true;
}
template < index_t dimension >
bool BoundingBox< dimension >::intersects(
const Ray< dimension >& ray ) const
{
const auto box_half_extent = diagonal() / 2.;
const auto ray_translated_origin = ray.origin() - center();
for( const auto i : LRange{ dimension } )
{
if( std::fabs( ray_translated_origin.value( i ) )
- box_half_extent.value( i )
> GLOBAL_EPSILON
&& ray_translated_origin.value( i ) * ray.direction().value( i )
> GLOBAL_EPSILON )
{
return false;
}
}
return line_intersects( *this, ray );
}
template < index_t dimension >
bool BoundingBox< dimension >::intersects(
const InfiniteLine< dimension >& line ) const
{
return line_intersects( *this, line );
}
template < index_t dimension >
bool BoundingBox< dimension >::intersects(
const Segment< dimension >& segment ) const
{
const auto& vertices = segment.vertices();
for( const auto v : LRange{ 2 } )
{
if( contains( vertices[v].get() ) )
{
return true;
}
}
if( segment.length() < GLOBAL_EPSILON )
{
return false;
}
if( !intersects( segment.bounding_box() ) )
{
return false;
}
const auto box_center = center();
const auto box_extent = diagonal() / 2.;
const auto segment_origin = segment.barycenter() - box_center;
const auto segment_extent = segment.length() / 2.;
const auto segment_direction = segment.normalized_direction();
for( const auto i : LRange{ dimension } )
{
const auto lhs = std::fabs( segment_origin.value( i ) );
const auto rhs =
box_extent.value( i )
+ segment_extent * std::fabs( segment_direction.value( i ) );
if( lhs > rhs )
{
return false;
}
}
return this->intersects( InfiniteLine< dimension >{ segment } );
}
template <>
template <>
bool opengeode_geometry_api BoundingBox< 3 >::intersects< 3 >(
const Triangle< 3 >& triangle ) const
{
const auto& vertices = triangle.vertices();
for( const auto v : LRange{ 3 } )
{
if( contains( vertices[v].get() ) )
{
return true;
}
}
if( !intersects( triangle.bounding_box() ) )
{
return false;
}
const auto triangle_projection = [&vertices]( const Vector3D& normal ) {
BoundingBox1D interval;
interval.add_point(
Point1D{ { normal.dot( Vector3D{ vertices[0].get() } ) } } );
interval.add_point(
Point1D{ { normal.dot( Vector3D{ vertices[1].get() } ) } } );
interval.add_point(
Point1D{ { normal.dot( Vector3D{ vertices[2].get() } ) } } );
return interval;
};
const Vector3D box_center{ center() };
const auto box_diagonal = diagonal();
const auto bbox_projection = [&box_center, &box_diagonal](
const Vector3D& normal ) {
const auto origin = normal.dot( box_center );
const auto maximum_extent =
( std::fabs( normal.value( 0 ) * box_diagonal.value( 0 ) )
+ std::fabs( normal.value( 1 ) * box_diagonal.value( 1 ) )
+ std::fabs( normal.value( 2 ) * box_diagonal.value( 2 ) ) )
/ 2.;
return BoundingBox1D{ Point1D{ { origin - maximum_extent } },
Point1D{ { origin + maximum_extent } } };
};
const std::array< Vector3D, 3 > edges{ Vector3D{ vertices[0].get(),
vertices[1].get() },
Vector3D{ vertices[0].get(), vertices[2].get() },
Vector3D{ vertices[1].get(), vertices[2].get() } };
// Test direction of triangle normal.
const auto triangle_normal = edges[0].cross( edges[1] );
if( !bbox_projection( triangle_normal )
.contains( Point1D{ { triangle_normal.dot(
Vector3D{ vertices[0].get() } ) } } ) )
{
return false;
}
// Test direction of box faces.
for( const auto i : LRange{ 3 } )
{
Vector3D axis;
axis.set_value( i, 1 );
const auto triangle_interval = triangle_projection( axis );
const auto center_value = box_center.value( i );
const auto extent = box_diagonal.value( i ) / 2.;
const BoundingBox1D box_interval{ Point1D{
{ center_value - extent } },
Point1D{ { center_value + extent } } };
if( !triangle_interval.intersects( box_interval ) )
{
return false;
}
}
// Test direction of triangle-box edge cross products.
for( const auto i0 : LRange{ 3 } )
{
for( const auto i1 : LRange{ 3 } )
{
Vector3D axis;
axis.set_value( i1, 1 );
const auto normal = edges[i0].cross( axis );
const auto triangle_interval = triangle_projection( normal );
const auto box_interval = bbox_projection( normal );
if( !triangle_interval.intersects( box_interval ) )
{
return false;
}
}
}
return true;
}
template <>
template <>
bool opengeode_geometry_api BoundingBox< 2 >::intersects< 2 >(
const Triangle< 2 >& triangle ) const
{
if( point_triangle_position( center(), triangle ) == POSITION::inside )
{
return true;
}
const auto& vertices = triangle.vertices();
for( const auto v : LRange{ 3 } )
{
if( contains( vertices[v].get() ) )
{
return true;
}
}
return intersects( Segment2D{ vertices[0].get(), vertices[1].get() } )
|| intersects(
Segment2D{ vertices[0].get(), vertices[2].get() } )
|| intersects(
Segment2D{ vertices[1].get(), vertices[2].get() } );
}
template < index_t dimension >
template < index_t T >
typename std::enable_if< T == 3, bool >::type
BoundingBox< dimension >::intersects( const Tetrahedron& tetra ) const
{
if( point_tetrahedron_position( center(), tetra ) == POSITION::inside )
{
return true;
}
const auto& vertices = tetra.vertices();
for( const auto v : LRange{ 4 } )
{
if( contains( vertices[v].get() ) )
{
return true;
}
}
return intersects(
{ vertices[0].get(), vertices[1].get(), vertices[2].get() } )
|| intersects(
{ vertices[0].get(), vertices[1].get(), vertices[3].get() } )
|| intersects(
{ vertices[0].get(), vertices[2].get(), vertices[3].get() } )
|| intersects( { vertices[1].get(), vertices[2].get(),
vertices[3].get() } );
}
template < index_t dimension >
Point< dimension > BoundingBox< dimension >::center() const
{
return ( min_ + max_ ) / 2.;
}
template < index_t dimension >
Vector< dimension > BoundingBox< dimension >::diagonal() const
{
return { min_, max_ };
}
template < index_t dimension >
std::tuple< local_index_t, double >
BoundingBox< dimension >::smallest_length() const
{
auto length = std::numeric_limits< double >::max();
return test_axes(
diagonal(), length, []( double diagonal_value, double min_value ) {
return diagonal_value < min_value;
} );
}
template < index_t dimension >
std::tuple< local_index_t, double >
BoundingBox< dimension >::largest_length() const
{
double length{ 0 };
return test_axes(
diagonal(), length, []( double diagonal_value, double max_value ) {
return diagonal_value > max_value;
} );
}
template < index_t dimension >
double BoundingBox< dimension >::signed_distance(
const Point< dimension >& point ) const
{
bool inside{ true };
Vector< dimension > result;
for( const auto c : LRange{ dimension } )
{
const auto value = point.value( c );
if( value < min_.value( c ) )
{
inside = false;
result.set_value( c, value - min_.value( c ) );
}
else if( value > max_.value( c ) )
{
inside = false;
result.set_value( c, value - max_.value( c ) );
}
}
if( !inside )
{
return result.length();
}
const auto Pmin = point - min_;
const auto Pmax = point - max_;
auto inner_distance = std::numeric_limits< double >::max();
for( const auto c : geode::LRange{ dimension } )
{
const auto local_distance = std::min(
std::fabs( Pmin.value( c ) ), std::fabs( Pmax.value( c ) ) );
inner_distance = std::min( inner_distance, local_distance );
}
return -inner_distance;
}
template < index_t dimension >
double BoundingBox< dimension >::n_volume() const
{
double volume{ 1.0 };
const auto box_extent = diagonal();
for( const auto c : geode::LRange{ dimension } )
{
volume *= ( box_extent.value( c ) );
}
return volume;
}
template < index_t dimension >
std::string BoundingBox< dimension >::string() const
{
return absl::StrCat(
"(min: ", min_.string(), ", max: ", max_.string(), ")" );
}
template class opengeode_geometry_api BoundingBox< 1 >;
template class opengeode_geometry_api BoundingBox< 2 >;
template class opengeode_geometry_api BoundingBox< 3 >;
template opengeode_geometry_api bool BoundingBox< 3 >::intersects< 3 >(
const Tetrahedron& ) const;
} // namespace geode