Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 90 additions & 78 deletions source/MRMesh/MRFillContour.cpp
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 );
};
Comment thread
Fedr marked this conversation as resolved.
Resolved

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
17 changes: 17 additions & 0 deletions source/MRMesh/MRFillContour.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "MRMeshFwd.h"
#include "MRBitSet.h"

namespace MR
{
Expand All @@ -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<EdgePath> & 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<EdgePath> & contours, double maxArea );

} //namespace MR
68 changes: 68 additions & 0 deletions source/MRTest/MRFillContourTests.cpp
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 )
Comment thread
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
1 change: 1 addition & 0 deletions source/MRTest/MRTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<ClCompile Include="MRDistanceMapTests.cpp" />
<ClCompile Include="MREdgePathsTests.cpp" />
<ClCompile Include="MRFillContourByGraphCutTests.cpp" />
<ClCompile Include="MRFillContourTests.cpp" />
<ClCompile Include="MRFillContours2DTests.cpp" />
<ClCompile Include="MRFinallyTests.cpp" />
<ClCompile Include="MRStacktraceTests.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions source/MRTest/MRTest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@
<ClCompile Include="MRFillContourByGraphCutTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MRFillContourTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MRFillContours2DTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
Loading