From ed752ce1cd477abcedeb49ae8ca62029bb0400c9 Mon Sep 17 00:00:00 2001 From: Fedor Chelnokov Date: Fri, 4 Dec 2026 23:33:00 +0300 Subject: [PATCH 1/4] fillContourLeft: fill front by front with prefetch; fillContourLeftUpToArea Co-Authored-By: Claude Opus 5.5 --- source/MRMesh/MRFillContour.cpp | 157 ++++++++++++++------------- source/MRMesh/MRFillContour.h | 17 +++ source/MRTest/MRFillContourTests.cpp | 50 +++++++++ source/MRTest/MRTest.vcxproj | 1 + source/MRTest/MRTest.vcxproj.filters | 3 + 5 files changed, 153 insertions(+), 75 deletions(-) create mode 100644 source/MRTest/MRFillContourTests.cpp diff --git a/source/MRMesh/MRFillContour.cpp b/source/MRMesh/MRFillContour.cpp index a72f7b1d1362..aeba82c46e68 100644 --- a/source/MRMesh/MRFillContour.cpp +++ b/source/MRMesh/MRFillContour.cpp @@ -1,114 +1,121 @@ #include "MRFillContour.h" -#include "MRMeshTopology.h" +#include "MRMesh.h" #include "MRBitSet.h" #include "MRphmap.h" #include "MRTimer.h" +#include namespace MR { -class ContourLeftFiller +namespace { -public: - ContourLeftFiller( const MeshTopology & topology ); - void addContour( const EdgePath & contour ); - const FaceBitSet& fill(); -private: - const MeshTopology & topology_; - FaceBitSet filledFaces_; - std::vector currentFacesBd_, nextFacesBd_; - - void firstStep_(); - void nextStep_(); -}; - -ContourLeftFiller::ContourLeftFiller( const MeshTopology & topology ) : topology_( topology ) +/// fills the faces to the left from the contours; +/// onLeftFace( e ) is called once for each filled face with an edge having that face on the left, +/// and the filling stops as soon as it returns false; +/// returns false if the filling was stopped +template +bool fillLeftOfContours( const MeshTopology & topology, std::span contours, FaceBitSet & filled, OnLeftFace && onLeftFace ) { - filledFaces_.resize( topology.lastValidFace() + 1 ); -} + size_t numEdges = 0; + for ( const auto & contour : contours ) + numEdges += contour.size(); + EdgeHashSet contourEdges; + contourEdges.reserve( numEdges ); + for ( const auto & contour : contours ) + for ( EdgeId e : contour ) + contourEdges.insert( e ); + + // the contour can pass back and forth an edge, then neither side of it is filled from that edge + auto skipContourEdge = [&]( EdgeId e ) + { + return contourEdges.contains( e.sym() ) || !topology.left( e ); + }; -void ContourLeftFiller::addContour( const EdgePath & contour ) -{ - for ( auto e : contour ) - currentFacesBd_.push_back( e ); -} + filled.clear(); + filled.resize( topology.lastValidFace() + 1 ); -void ContourLeftFiller::firstStep_() -{ - // first step is more complicated to ensure that we do not fill to the other side from the contour + // all faces to the left from the contour are filled first, + // so the faces discovered from them later can be checked not to cross the contour + for ( const auto & contour : contours ) + for ( EdgeId e : contour ) + if ( !skipContourEdge( e ) && !filled.test_set( topology.left( e ) ) && !onLeftFace( e ) ) + return false; - ParallelHashSet initialEdges; - for ( EdgeId e : currentFacesBd_ ) - initialEdges.insert( e ); + // each edge here has a filled face on the left, and its two other edges are still to be crossed + std::vector front, prevFront; - nextFacesBd_.clear(); - auto addNextStepEdge = [&]( EdgeId e ) + // fills the face to the left from e + auto fillLeft = [&]( EdgeId e ) { - if ( initialEdges.find( e ) != initialEdges.end() ) - return; - nextFacesBd_.push_back( e.sym() ); + auto f = topology.left( e ); + if ( f && !filled.test_set( f ) ) + front.push_back( e ); }; - for ( EdgeId e : currentFacesBd_ ) - { - //allow the contour to pass back and forth an edge - if ( initialEdges.find( e.sym() ) != initialEdges.end() ) - continue; - auto l = topology_.left( e ); - if ( !l.valid() ) - continue; - if ( !filledFaces_.test_set( l ) ) + for ( const auto & contour : contours ) + for ( EdgeId e : contour ) { - addNextStepEdge( topology_.next( e ).sym() ); - addNextStepEdge( topology_.prev( e.sym() ) ); + if ( skipContourEdge( e ) ) + continue; + for ( EdgeId n : { topology.next( e ), topology.prev( e.sym() ).sym() } ) + if ( !contourEdges.contains( n.sym() ) ) + fillLeft( n ); } - } - currentFacesBd_.swap( nextFacesBd_ ); -} - -void ContourLeftFiller::nextStep_() -{ - nextFacesBd_.clear(); - for ( EdgeId e : currentFacesBd_ ) + // the filling goes front by front, so the records of the edges in the current front can be prefetched + constexpr size_t cAhead = 16; + while ( !front.empty() ) { - auto l = topology_.left( e ); - if ( !l.valid() ) - continue; - if ( !filledFaces_.test_set( l ) ) + front.swap( prevFront ); + front.clear(); + for ( size_t i = 0; i < prevFront.size(); ++i ) { - nextFacesBd_.push_back( topology_.next( e ) ); - nextFacesBd_.push_back( topology_.prev( e.sym() ).sym() ); + if ( i + cAhead < prevFront.size() ) + { + const EdgeId a = prevFront[i + cAhead]; + topology.prefetchEdge( topology.next( a ) ); + topology.prefetchEdge( topology.prev( a.sym() ) ); + } + const EdgeId e = prevFront[i]; + if ( !onLeftFace( e ) ) + return false; + fillLeft( topology.next( e ) ); + fillLeft( topology.prev( e.sym() ).sym() ); } } - - currentFacesBd_.swap( nextFacesBd_ ); + return true; } -const FaceBitSet& ContourLeftFiller::fill() +} // anonymous namespace + +FaceBitSet fillContourLeft( const MeshTopology & topology, const EdgePath & contour ) { - firstStep_(); - while ( !currentFacesBd_.empty() ) - nextStep_(); - return filledFaces_; + MR_TIMER; + FaceBitSet res; + fillLeftOfContours( topology, std::span( &contour, 1 ), res, []( EdgeId ) { return true; } ); + return res; } -FaceBitSet fillContourLeft(const MeshTopology& topology, const EdgePath& contour) +FaceBitSet fillContourLeft( const MeshTopology & topology, const std::vector & contours ) { MR_TIMER; - ContourLeftFiller filler( topology ); - filler.addContour( contour ); - return filler.fill(); + FaceBitSet res; + fillLeftOfContours( topology, contours, res, []( EdgeId ) { return true; } ); + return res; } -FaceBitSet fillContourLeft( const MeshTopology & topology, const std::vector & contours ) +ContourLeftFill fillContourLeftUpToArea( const Mesh & mesh, const std::vector & contours, double maxArea ) { MR_TIMER; - ContourLeftFiller filler( topology ); - for ( auto & contour : contours ) - filler.addContour( contour ); - return filler.fill(); + ContourLeftFill res; + res.exceeded = !fillLeftOfContours( mesh.topology, contours, res.region, [&]( EdgeId e ) + { + res.area += 0.5 * mesh.leftDirDblArea( e ).length(); + return res.area <= maxArea; + } ); + return res; } } // namespace MR diff --git a/source/MRMesh/MRFillContour.h b/source/MRMesh/MRFillContour.h index 885bb137a1e1..65d9b4b4e541 100644 --- a/source/MRMesh/MRFillContour.h +++ b/source/MRMesh/MRFillContour.h @@ -1,6 +1,7 @@ #pragma once #include "MRMeshFwd.h" +#include "MRBitSet.h" namespace MR { @@ -12,4 +13,20 @@ namespace MR MRMESH_API FaceBitSet fillContourLeft( const MeshTopology & topology, const EdgePath & contour ); MRMESH_API FaceBitSet fillContourLeft( const MeshTopology & topology, const std::vector & contours ); +/// the faces to the left from contour(s), filled until their area exceeded the limit +struct ContourLeftFill +{ + /// all faces to the left from the contour(s) if !exceeded, otherwise only a part of them + FaceBitSet region; + + /// total area of the faces in region if !exceeded, otherwise the area that exceeded the limit + double area = 0; + + /// the filling was stopped because the area of filled faces became larger than the limit + bool exceeded = false; +}; + +/// same as \ref fillContourLeft, but stops filling as soon as the area of filled faces exceeds \p maxArea +[[nodiscard]] MRMESH_API ContourLeftFill fillContourLeftUpToArea( const Mesh & mesh, const std::vector & contours, double maxArea ); + } //namespace MR diff --git a/source/MRTest/MRFillContourTests.cpp b/source/MRTest/MRFillContourTests.cpp new file mode 100644 index 000000000000..d7a2311cd500 --- /dev/null +++ b/source/MRTest/MRFillContourTests.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +namespace MR +{ + +TEST( MRMesh, FillContourLeft ) +{ + const auto mesh = makeUVSphere( 1.0f, 32, 32 ); + FaceBitSet cap( mesh.topology.faceSize() ), band( mesh.topology.faceSize() ); + for ( auto f : mesh.topology.getValidFaces() ) + { + const auto z = mesh.triCenter( f ).z; + if ( z > 0.5f ) + cap.set( f ); + if ( std::abs( z ) < 0.5f ) + band.set( f ); + } + + for ( const auto & [region, numLoops] : { std::pair{ cap, size_t( 1 ) }, std::pair{ band, size_t( 2 ) } } ) + { + const auto bds = findLeftBoundary( mesh.topology, region ); + EXPECT_EQ( bds.size(), numLoops ); + auto filled = fillContourLeft( mesh.topology, bds ); + filled.resize( region.size() ); + EXPECT_EQ( filled, region ); + + const double area = mesh.area( region ); + auto all = fillContourLeftUpToArea( mesh, bds, area * 1.01 ); + EXPECT_FALSE( all.exceeded ); + EXPECT_NEAR( all.area, area, area * 1e-6 ); + all.region.resize( region.size() ); + EXPECT_EQ( all.region, region ); + + auto part = fillContourLeftUpToArea( mesh, bds, area * 0.5 ); + EXPECT_TRUE( part.exceeded ); + EXPECT_GT( part.area, area * 0.5 ); + EXPECT_LT( part.region.count(), region.count() ); + part.region.resize( region.size() ); + EXPECT_TRUE( part.region.is_subset_of( region ) ); + } + + const auto cut = findLeftBoundary( mesh.topology, cap ); + EXPECT_EQ( fillContourLeft( mesh.topology, cut[0] ).count(), cap.count() ); +} + +} //namespace MR diff --git a/source/MRTest/MRTest.vcxproj b/source/MRTest/MRTest.vcxproj index 3d6954e90693..30030941e2bd 100644 --- a/source/MRTest/MRTest.vcxproj +++ b/source/MRTest/MRTest.vcxproj @@ -134,6 +134,7 @@ + diff --git a/source/MRTest/MRTest.vcxproj.filters b/source/MRTest/MRTest.vcxproj.filters index 1158e8dee549..96a75e3190ce 100644 --- a/source/MRTest/MRTest.vcxproj.filters +++ b/source/MRTest/MRTest.vcxproj.filters @@ -349,6 +349,9 @@ Source Files + + Source Files + Source Files From a2f45f5f9ec382b417a409f246f37e4c895bd01f Mon Sep 17 00:00:00 2001 From: Fedor Chelnokov Date: Sat, 5 Dec 2026 00:23:33 +0300 Subject: [PATCH 2/4] fillContourLeft: MR::Vector for the fronts Co-Authored-By: Claude Opus 5.5 --- source/MRMesh/MRFillContour.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/MRMesh/MRFillContour.cpp b/source/MRMesh/MRFillContour.cpp index aeba82c46e68..03bd47ce4770 100644 --- a/source/MRMesh/MRFillContour.cpp +++ b/source/MRMesh/MRFillContour.cpp @@ -1,6 +1,7 @@ #include "MRFillContour.h" #include "MRMesh.h" #include "MRBitSet.h" +#include "MRVector.h" #include "MRphmap.h" #include "MRTimer.h" #include @@ -44,7 +45,7 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span front, prevFront; + Vector front, prevFront; // fills the face to the left from e auto fillLeft = [&]( EdgeId e ) From e47d90d72b7df7fdee67d91985725e72b32f5bdc Mon Sep 17 00:00:00 2001 From: Fedor Chelnokov Date: Sat, 5 Dec 2026 14:10:11 +0300 Subject: [PATCH 3/4] fillContourLeftUpToArea: count faces on discovery; expand only newly filled contour faces; degenerate-case tests Co-Authored-By: Claude Opus 5.5 --- source/MRMesh/MRFillContour.cpp | 44 +++++++++++----------------- source/MRMesh/MRFillContour.h | 4 +-- source/MRTest/MRFillContourTests.cpp | 24 +++++++++++++-- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/source/MRMesh/MRFillContour.cpp b/source/MRMesh/MRFillContour.cpp index 03bd47ce4770..d3abd0eb404e 100644 --- a/source/MRMesh/MRFillContour.cpp +++ b/source/MRMesh/MRFillContour.cpp @@ -28,42 +28,34 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span front, prevFront; - // fills the face to the left from e + // fills the face to the left from e; returns false if the filling must stop auto fillLeft = [&]( EdgeId e ) { auto f = topology.left( e ); - if ( f && !filled.test_set( f ) ) - front.push_back( e ); + if ( !f || filled.test_set( f ) ) + return true; + front.push_back( e ); + return onLeftFace( e ); }; + // all faces to the left from the contour are filled first, + // so the faces discovered from them later can be checked not to cross the contour; + // the contour can pass back and forth an edge, then neither side of it is filled from that edge for ( const auto & contour : contours ) for ( EdgeId e : contour ) - { - if ( skipContourEdge( e ) ) - continue; - for ( EdgeId n : { topology.next( e ), topology.prev( e.sym() ).sym() } ) - if ( !contourEdges.contains( n.sym() ) ) - fillLeft( n ); - } + if ( !contourEdges.contains( e.sym() ) && !fillLeft( e ) ) + return false; + + front.swap( prevFront ); + for ( EdgeId e : prevFront ) + for ( EdgeId n : { topology.next( e ), topology.prev( e.sym() ).sym() } ) + if ( !contourEdges.contains( n.sym() ) && !fillLeft( n ) ) + return false; // the filling goes front by front, so the records of the edges in the current front can be prefetched constexpr size_t cAhead = 16; @@ -80,10 +72,8 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span 0 ) + { + EXPECT_EQ( bds.size(), numLoops ); + } auto filled = fillContourLeft( mesh.topology, bds ); filled.resize( region.size() ); EXPECT_EQ( filled, region ); @@ -38,13 +44,25 @@ TEST( MRMesh, FillContourLeft ) auto part = fillContourLeftUpToArea( mesh, bds, area * 0.5 ); EXPECT_TRUE( part.exceeded ); EXPECT_GT( part.area, area * 0.5 ); + EXPECT_NEAR( part.area, mesh.area( part.region ), area * 1e-6 ); EXPECT_LT( part.region.count(), region.count() ); part.region.resize( region.size() ); EXPECT_TRUE( part.region.is_subset_of( region ) ); + + const auto one = fillContourLeftUpToArea( mesh, bds, 0 ); + EXPECT_TRUE( one.exceeded ); + EXPECT_EQ( one.region.count(), size_t( 1 ) ); + EXPECT_NEAR( one.area, mesh.area( one.region ), area * 1e-6 ); } const auto cut = findLeftBoundary( mesh.topology, cap ); EXPECT_EQ( fillContourLeft( mesh.topology, cut[0] ).count(), cap.count() ); + + // a path followed by the same path in the opposite direction encloses nothing + EdgePath backAndForth( cut[0].begin(), cut[0].begin() + 5 ); + for ( int i = 4; i >= 0; --i ) + backAndForth.push_back( cut[0][i].sym() ); + EXPECT_EQ( fillContourLeft( mesh.topology, backAndForth ).count(), size_t( 0 ) ); } } //namespace MR From 7a1773497dabc9ac8874e02dc42aa1189688a409 Mon Sep 17 00:00:00 2001 From: Fedor Chelnokov Date: Sat, 5 Dec 2026 15:36:14 +0300 Subject: [PATCH 4/4] fillContourLeftUpToArea: count faces on pop when their records are prefetched, unfill uncounted faces on stop Co-Authored-By: Claude Opus 5.5 --- source/MRMesh/MRFillContour.cpp | 34 +++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/source/MRMesh/MRFillContour.cpp b/source/MRMesh/MRFillContour.cpp index d3abd0eb404e..4df43a1b712a 100644 --- a/source/MRMesh/MRFillContour.cpp +++ b/source/MRMesh/MRFillContour.cpp @@ -33,14 +33,12 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span front, prevFront; - // fills the face to the left from e; returns false if the filling must stop + // fills the face to the left from e auto fillLeft = [&]( EdgeId e ) { auto f = topology.left( e ); - if ( !f || filled.test_set( f ) ) - return true; - front.push_back( e ); - return onLeftFace( e ); + if ( f && !filled.test_set( f ) ) + front.push_back( e ); }; // all faces to the left from the contour are filled first, @@ -48,16 +46,23 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span frontSize && !onLeftFace( e ) ) return false; + } front.swap( prevFront ); for ( EdgeId e : prevFront ) for ( EdgeId n : { topology.next( e ), topology.prev( e.sym() ).sym() } ) - if ( !contourEdges.contains( n.sym() ) && !fillLeft( n ) ) - return false; + if ( !contourEdges.contains( n.sym() ) ) + fillLeft( n ); - // the filling goes front by front, so the records of the edges in the current front can be prefetched + // the filling goes front by front, so the records of the edges in the current front can be prefetched, + // and the faces are passed to onLeftFace when their records are already loaded constexpr size_t cAhead = 16; while ( !front.empty() ) { @@ -72,8 +77,17 @@ bool fillLeftOfContours( const MeshTopology & topology, std::span