diff --git a/source/MRMesh/MRFillContour.cpp b/source/MRMesh/MRFillContour.cpp index a72f7b1d1362..4df43a1b712a 100644 --- a/source/MRMesh/MRFillContour.cpp +++ b/source/MRMesh/MRFillContour.cpp @@ -1,114 +1,126 @@ #include "MRFillContour.h" -#include "MRMeshTopology.h" +#include "MRMesh.h" #include "MRBitSet.h" +#include "MRVector.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 ) -{ - filledFaces_.resize( topology.lastValidFace() + 1 ); -} - -void ContourLeftFiller::addContour( const EdgePath & contour ) +/// 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 ) { - for ( auto e : contour ) - currentFacesBd_.push_back( e ); -} - -void ContourLeftFiller::firstStep_() -{ - // first step is more complicated to ensure that we do not fill to the other side from the contour - - ParallelHashSet initialEdges; - for ( EdgeId e : currentFacesBd_ ) - initialEdges.insert( e ); - - nextFacesBd_.clear(); - auto addNextStepEdge = [&]( EdgeId e ) + 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 ); + + filled.resize( topology.lastValidFace() + 1 ); + + // each edge here has a filled face on the left, and its two other edges are still to be crossed + Vector front, prevFront; + + // 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 ) ) + // 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 ) { - addNextStepEdge( topology_.next( e ).sym() ); - addNextStepEdge( topology_.prev( e.sym() ) ); + if ( contourEdges.contains( e.sym() ) ) + continue; + const auto frontSize = front.size(); + fillLeft( e ); + if ( front.size() > frontSize && !onLeftFace( e ) ) + return false; } - } - currentFacesBd_.swap( nextFacesBd_ ); -} + 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 ); -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, + // and the faces are passed to onLeftFace when their records are already loaded + 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 ) ) + { + // unfill the faces not passed to onLeftFace yet + for ( size_t j = i + 1; j < prevFront.size(); ++j ) + filled.reset( topology.left( prevFront[j] ) ); + for ( EdgeId u : front ) + filled.reset( topology.left( u ) ); + 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..87620b5d8baa 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 a part of them (possibly all) + FaceBitSet region; + + /// total area of the faces in region + 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..e1b239f2fdd3 --- /dev/null +++ b/source/MRTest/MRFillContourTests.cpp @@ -0,0 +1,68 @@ +#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() ), scattered( 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 ); + // isolated faces with 3 contour edges, pairs of faces with 2, and faces touching only by a vertex + if ( int( f ) % 5 == 0 || int( f ) % 7 == 0 ) + scattered.set( f ); + } + + for ( const auto & [region, numLoops] : { std::pair{ cap, size_t( 1 ) }, std::pair{ band, size_t( 2 ) }, std::pair{ scattered, size_t( 0 ) } } ) + { + const auto bds = findLeftBoundary( mesh.topology, region ); + if ( numLoops > 0 ) + { + 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_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 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