Repository navigation
fillContourLeft: up to 2.8x faster on meshes without regular face order; fillContourLeftUpToArea stops at area limit #7683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+179
−78
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed752ce
fillContourLeft: fill front by front with prefetch; fillContourLeftUp…
Fedr a2f45f5
fillContourLeft: MR::Vector for the fronts
Fedr e47d90d
fillContourLeftUpToArea: count faces on discovery; expand only newly …
Fedr 7a17734
fillContourLeftUpToArea: count faces on pop when their records are pr…
Fedr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <span> | ||
|
|
||
| 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<EdgeId> 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 <typename OnLeftFace> | ||
| bool fillLeftOfContours( const MeshTopology & topology, std::span<const EdgePath> 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<EdgeId> 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<EdgeId> 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<EdgePath> & 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<EdgePath> & contours ) | ||
| ContourLeftFill fillContourLeftUpToArea( const Mesh & mesh, const std::vector<EdgePath> & 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include <MRMesh/MRFillContour.h> | ||
| #include <MRMesh/MRMakeSphereMesh.h> | ||
| #include <MRMesh/MRMesh.h> | ||
| #include <MRMesh/MRRegionBoundary.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace MR | ||
| { | ||
|
|
||
| TEST( MRMesh, FillContourLeft ) | ||
|
Fedr marked this conversation as resolved.
Resolved
|
||
| { | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.