20#ifndef SHAPE_POLY_TRI_INDEX_H
21#define SHAPE_POLY_TRI_INDEX_H
46 explicit TRIANGULATION_INDEX(
const std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>>& aPolys )
50 for(
const std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>& poly : aPolys )
51 count += poly->GetTriangleCount();
61 for(
const std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>& poly : aPolys )
63 for(
const TRI& tri : poly->Triangles() )
65 const BOX2I bbox = tri.BBox();
66 const int lo[2] = { bbox.
GetX(), bbox.
GetY() };
69 builder.
Add( lo, hi,
static_cast<uint32_t
>(
m_tris.size() ) );
81 template <
typename VISITOR>
87 const int lo[2] = { aBox.
GetX(), aBox.
GetY() };
91 auto forward = [&]( uint32_t aIndex ) ->
bool
93 return aVisitor(
m_tris[aIndex] );
96 m_tree.Search( lo, hi, forward );
116template <
typename VISITOR>
118 const std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>>& aPolys,
119 const BOX2I& aBox, VISITOR aVisitor )
123 bool stopped =
false;
128 if( aVisitor( aTri ) )
138 for(
const std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>& poly : aPolys )
142 if( !aVisitor( &tri ) )
158 const std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>>& aPolys,
159 const SHAPE* aShape,
int aClearance = 0,
int* aActual =
nullptr,
170 if( !aActual && !aLocation )
175 return !aShape->
Collide( aTri, aClearance );
179 int actual = std::numeric_limits<int>::max();
188 if( aShape->
Collide( aTri, aClearance, &triActual, &triLocation )
198 if(
actual == std::numeric_limits<int>::max() )
213 const std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>>& aPolys,
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
constexpr coord_type GetY() const
constexpr coord_type GetX() const
constexpr coord_type GetRight() const
constexpr coord_type GetBottom() const
Builder for constructing a PACKED_RTREE from a set of items.
void Add(const ELEMTYPE aMin[NUMDIMS], const ELEMTYPE aMax[NUMDIMS], const DATATYPE &aData)
void Reserve(size_t aCount)
Static (immutable) packed R-tree built via Hilbert-curve bulk loading.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
An abstract shape on 2D plane.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
A spatial index over the triangles of a cached triangulation, used to prune collision tests.
KIRTREE::PACKED_RTREE< uint32_t, int, 2 > m_tree
std::vector< const TRI * > m_tris
Not owning: the triangulation outlives us.
SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI TRI
TRIANGULATION_INDEX(const std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > &aPolys)
Index every triangle of every polygon. Immutable once constructed.
void Query(const BOX2I &aBox, VISITOR aVisitor) const
Visit every triangle whose bounding box overlaps aBox.
bool AnyTriangleHit(const TRIANGULATION_INDEX *aIndex, const std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > &aPolys, const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr)
Return true if any candidate triangle collides with aShape within aClearance.
bool VisitTriangleCandidates(const TRIANGULATION_INDEX *aIndex, const std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > &aPolys, const BOX2I &aBox, VISITOR aVisitor)
Visit the triangles that could collide with something inside aBox, through aIndex when one exists,...
VECTOR2< int32_t > VECTOR2I