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
34 changes: 34 additions & 0 deletions source/MRMesh/MRMeshDistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,40 @@ MeshSignedDistanceResult findSignedDistance( const MeshPart & a, const MeshPart
return (signedRes.signedDist > 0.0f) ? MeshSignedDistanceResult{res.a, res.b, 0.0f} : signedRes;
}

MRMESH_API float findMaxDistanceSqOneWay( const MeshPart& a, const MeshPart& b, const AffineXf3f* rigidB2A, float maxDistanceSq )
{
MR_TIMER;

const auto& bMeshVerts = b.mesh.points;

return tbb::parallel_reduce
(
tbb::blocked_range( MR::begin(bMeshVerts), MR::end( bMeshVerts ) ),
0.0f,
[&] ( const auto& range, float init )
{
for ( auto& vert : range )
{
auto distSq = findProjection( vert, a, maxDistanceSq, rigidB2A ).distSq;
if ( distSq > init )
init = distSq;
}

return init;
},
[] ( float a, float b ) -> float
{
return a > b ? a : b;
}
);
}

MRMESH_API float findMaxDistanceSq( const MeshPart& a, const MeshPart& b, const AffineXf3f* rigidB2A, float maxDistanceSq )
{
std::unique_ptr<AffineXf3f> rigidA2B = rigidB2A ? std::make_unique<AffineXf3f>( rigidB2A->inverse() ) : nullptr;
return std::max( findMaxDistanceSqOneWay( a, b, rigidB2A, maxDistanceSq ), findMaxDistanceSqOneWay( b, a, rigidA2B.get(), maxDistanceSq ) );
}

TEST(MRMesh, MeshDistance)
{
Mesh sphere1 = makeUVSphere( 1, 8, 8 );
Expand Down
14 changes: 14 additions & 0 deletions source/MRMesh/MRMeshDistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ MRMESH_API MeshDistanceResult findDistance( const MeshPart & a, const MeshPart &
MRMESH_API MeshSignedDistanceResult findSignedDistance( const MeshPart & a, const MeshPart & b,
const AffineXf3f* rigidB2A = nullptr, float upDistLimitSq = FLT_MAX );

/**
* \brief returns the maximum of the distances from each B-mesh point to A-mesh
* \param rigidB2A rigid transformation from B-mesh space to A mesh space, nullptr considered as identity transformation
* \param maxDistanceSq upper limit on the positive distance in question, if the real distance is larger than the function exists returning maxDistanceSq
*/
MRMESH_API float findMaxDistanceSqOneWay( const MeshPart& a, const MeshPart& b, const AffineXf3f* rigidB2A = nullptr, float maxDistanceSq = FLT_MAX );

/**
* \brief returns the maximum of the distances from each mesh point to another mesh in both directions
* \param rigidB2A rigid transformation from B-mesh space to A mesh space, nullptr considered as identity transformation
* \param maxDistanceSq upper limit on the positive distance in question, if the real distance is larger than the function exists returning maxDistanceSq
*/
MRMESH_API float findMaxDistanceSq( const MeshPart& a, const MeshPart& b, const AffineXf3f* rigidB2A = nullptr, float maxDistanceSq = FLT_MAX );

/// \}

} // namespace MR
3 changes: 3 additions & 0 deletions source/mrmeshpy/MRPythonMeshPlugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,6 @@ MR_ADD_PYTHON_CUSTOM_DEF( mrmeshpy, LaplacianEdgeWeightsParam, [] ( pybind11::mo
} )

MR_ADD_PYTHON_FUNCTION( mrmeshpy, position_verts_smoothly, &positionVertsSmoothly, "shifts vertices to make smooth surface by Unit Laplacian" )

MR_ADD_PYTHON_FUNCTION( mrmeshpy, findMaxMeshDistanceSqOneWay, &findMaxDistanceSqOneWay, "returns the maximum of the distances from each B-mesh point to A-mesh" )
MR_ADD_PYTHON_FUNCTION( mrmeshpy, findMaxMeshDistanceSq, &findMaxDistanceSq, "returns the maximum of the distances from each mesh point to another mesh in both directions" )