Skip to content

Commit ba33ce2

Browse files
authored
Merge pull request #903 from Geode-solutions/fix/big-partial-triangle
fix(AABB): add compute triangle vs element bboxes intersection
2 parents 6a518d7 + 3c7115c commit ba33ce2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

include/geode/geometry/aabb.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <geode/basic/pimpl.h>
3535

3636
#include <geode/geometry/basic_objects/infinite_line.h>
37+
#include <geode/geometry/basic_objects/triangle.h>
3738
#include <geode/geometry/bounding_box.h>
3839
#include <geode/geometry/common.h>
3940

@@ -216,6 +217,25 @@ namespace geode
216217
const Segment< dimension >& segment,
217218
EvalIntersection& action ) const;
218219

220+
/*!
221+
* @brief Computes the intersections between a given Triangle and
222+
* all element boxes.
223+
* @param[in] triangle The triangle to test.
224+
* @param[in] action The functor to run when a box is intersected by the
225+
* segment.
226+
* @tparam EvalIntersection this functor should have an operator()
227+
* defined like this:
228+
* bool operator()( index_t cur_element_box ) ;
229+
* @note the operator define what to do with the box \p cur_element_box
230+
* if it is intersected by the \p triangle.
231+
* @note The returned boolean indicates if the search should stop or
232+
* continue. Return true to stop the search, false to continue.
233+
*/
234+
template < class EvalIntersection >
235+
void compute_triangle_element_bbox_intersections(
236+
const Triangle< dimension >& triangle,
237+
EvalIntersection& action ) const;
238+
219239
private:
220240
IMPLEMENTATION_MEMBER( impl_ );
221241
};

include/geode/geometry/detail/aabb_impl.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ namespace geode
101101
index_t element_end,
102102
ACTION& action ) const;
103103

104+
template < typename ACTION >
105+
bool triangle_intersect_recursive(
106+
const Triangle< dimension >& triangle,
107+
index_t node_index,
108+
index_t element_begin,
109+
index_t element_end,
110+
ACTION& action ) const;
111+
104112
template < typename ACTION >
105113
bool self_intersect_recursive( index_t node_index1,
106114
index_t element_begin1,
@@ -228,6 +236,19 @@ namespace geode
228236
line, Impl::ROOT_INDEX, 0, nb_bboxes(), action );
229237
}
230238

239+
template < index_t dimension >
240+
template < class EvalIntersection >
241+
void AABBTree< dimension >::compute_triangle_element_bbox_intersections(
242+
const Triangle< dimension >& triangle, EvalIntersection& action ) const
243+
{
244+
if( nb_bboxes() == 0 )
245+
{
246+
return;
247+
}
248+
impl_->triangle_intersect_recursive(
249+
triangle, Impl::ROOT_INDEX, 0, nb_bboxes(), action );
250+
}
251+
231252
template < index_t dimension >
232253
template < class EvalIntersection >
233254
void AABBTree< dimension >::compute_segment_element_bbox_intersections(
@@ -350,6 +371,41 @@ namespace geode
350371
box, it.child_right, it.middle_box, element_end, action );
351372
}
352373

374+
template < index_t dimension >
375+
template < typename ACTION >
376+
bool AABBTree< dimension >::Impl::triangle_intersect_recursive(
377+
const Triangle< dimension >& triangle,
378+
index_t node_index,
379+
index_t element_begin,
380+
index_t element_end,
381+
ACTION& action ) const
382+
{
383+
OPENGEODE_ASSERT( node_index < tree_.size(), "Node out of tree range" );
384+
OPENGEODE_ASSERT(
385+
element_begin != element_end, "No iteration allowed start == end" );
386+
387+
// Prune sub-tree that does not have intersection
388+
if( !node( node_index ).intersects( triangle ) )
389+
{
390+
return false;
391+
}
392+
393+
if( is_leaf( element_begin, element_end ) )
394+
{
395+
return action( mapping_morton( element_begin ) );
396+
}
397+
398+
const auto it =
399+
get_recursive_iterators( node_index, element_begin, element_end );
400+
if( triangle_intersect_recursive( triangle, it.child_left,
401+
element_begin, it.middle_box, action ) )
402+
{
403+
return true;
404+
}
405+
return triangle_intersect_recursive(
406+
triangle, it.child_right, it.middle_box, element_end, action );
407+
}
408+
353409
template < index_t dimension >
354410
template < typename ACTION >
355411
bool AABBTree< dimension >::Impl::self_intersect_recursive(

0 commit comments

Comments
 (0)