Skip to content

Commit 0173467

Browse files
authored
findCollidingEdgeTrisPrecise prepares int boxes in advance (#4699)
1 parent f485d73 commit 0173467

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

source/MRMesh/MRMeshCollidePrecise.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,32 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
2222
if ( aTree.nodes().empty() || bTree.nodes().empty() )
2323
return res;
2424

25-
Timer t( "1 init" );
25+
// parallel prepare of int boxes that will be used for consistency with precise intersections
26+
Timer t( "1 precise boxes" );
27+
Vector<Box3i, NodeId> aPreciseBoxes;
28+
aPreciseBoxes.resizeNoInit( aTree.nodes().size() );
29+
ParallelFor( aPreciseBoxes, [&]( NodeId i )
30+
{
31+
const auto & node = aTree.nodes()[i];
32+
aPreciseBoxes[i] = Box3i{ conv( node.box.min ), conv( node.box.max ) };
33+
} );
34+
35+
Vector<Box3i, NodeId> bPreciseBoxes;
36+
bPreciseBoxes.resizeNoInit( bTree.nodes().size() );
37+
ParallelFor( bPreciseBoxes, [&]( NodeId i )
38+
{
39+
const auto & node = bTree.nodes()[i];
40+
auto transformedBoxb = transformed( node.box, rigidB2A );
41+
bPreciseBoxes[i] = Box3i{ conv( transformedBoxb.min ), conv( transformedBoxb.max ) };
42+
} );
2643

2744
// sequentially subdivide full task on smaller subtasks;
2845
// they shall be not too many for this subdivision not to take too long;
2946
// and they shall be not too few for enough parallelism later
47+
t.restart( "2 top subtasks" );
48+
3049
std::vector<NodeNode> subtasks{ { NodeId{ 0 }, NodeId{ 0 } } }, nextSubtasks, leafTasks;
31-
// tested on two Spheres each with 3366 vertices:
50+
// tested on two Spheres each with 3366 vertices (these numbers are outdated after preparation of precise boxes):
3251
// 16 -> init=0.886 (13%), main=5.948, total=6.834
3352
// 14 -> init=0.429 ( 7%), main=5.990, total=6.419
3453
// 12 -> init=0.226 ( 3%), main=6.445, total=6.671
@@ -39,16 +58,14 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
3958
{
4059
const auto s = subtasks.back();
4160
subtasks.pop_back();
42-
const auto & aNode = aTree[s.aNode];
43-
const auto & bNode = bTree[s.bNode];
4461

45-
// check intersection in int boxes for consistency with precise intersections
46-
auto transformedBoxb = transformed( bNode.box, rigidB2A );
47-
Box3i aBox{ conv( aNode.box.min ),conv( aNode.box.max ) };
48-
Box3i bBox{ conv( transformedBoxb.min ),conv( transformedBoxb.max ) };
62+
const Box3i& aBox = aPreciseBoxes[s.aNode];
63+
const Box3i& bBox = bPreciseBoxes[s.bNode];
4964
if ( !aBox.intersects( bBox ) )
5065
continue;
5166

67+
const auto & aNode = aTree[s.aNode];
68+
const auto & bNode = bTree[s.bNode];
5269
if ( aNode.leaf() && bNode.leaf() )
5370
{
5471
leafTasks.push_back( s );
@@ -162,7 +179,8 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
162179
}
163180
};
164181

165-
t.restart( "2 process" );
182+
// checks subtasks in parallel
183+
t.restart( "3 process" );
166184

167185
struct ThreadData
168186
{
@@ -182,7 +200,6 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
182200
std::vector<SubtaskRes> subtaskRes( subtasks.size() );
183201

184202
std::atomic<bool> anyIntersectionAtm{ false };
185-
// checks subtasks in parallel
186203
ParallelFor( subtasks, threadData, [&]( size_t is, ThreadData & tls )
187204
{
188205
std::vector<NodeNode>& mySubtasks = tls.subtasks;
@@ -196,16 +213,14 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
196213
break;
197214
const auto s = mySubtasks.back();
198215
mySubtasks.pop_back();
199-
const auto & aNode = aTree[s.aNode];
200-
const auto & bNode = bTree[s.bNode];
201216

202-
// check intersection in int boxes for consistency with precise intersections
203-
auto transformedBoxb = transformed( bNode.box, rigidB2A );
204-
Box3i aBox{ conv( aNode.box.min ),conv( aNode.box.max ) };
205-
Box3i bBox{ conv( transformedBoxb.min ),conv( transformedBoxb.max ) };
217+
const Box3i& aBox = aPreciseBoxes[s.aNode];
218+
const Box3i& bBox = bPreciseBoxes[s.bNode];
206219
if ( !aBox.intersects( bBox ) )
207220
continue;
208221

222+
const auto & aNode = aTree[s.aNode];
223+
const auto & bNode = bTree[s.bNode];
209224
if ( aNode.leaf() && bNode.leaf() )
210225
{
211226
const auto aFace = aNode.leafId();
@@ -242,8 +257,8 @@ PreciseCollisionResult findCollidingEdgeTrisPrecise( const MeshPart & a, const M
242257
subtaskRes[is] = std::move( myRes );
243258
} );
244259

245-
// unite results from sub-trees into final vectors
246-
t.restart( "3 unite" );
260+
// unite results from sub-trees into final vector
261+
t.restart( "4 unite" );
247262
size_t cols = 0;
248263
for ( const auto & s : subtaskRes )
249264
cols += s.last - s.first;

0 commit comments

Comments
 (0)