Skip to content

Commit fc1cd0f

Browse files
authored
added DistanceMap in mrmeshpy library (#516)
* added DistanceMap in mrmeshpy library
1 parent af8a922 commit fc1cd0f

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

source/MRMesh/MRDistanceMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ tl::expected<MR::DistanceMap, std::string> convertImageToDistanceMap( const Imag
255255
return tl::make_unexpected( "Error convert Image to DistanceMap: image isn't monochrome" );
256256
if ( pixels[i].r < threshold )
257257
continue;
258-
dm.set( i, 1.f - ( pixels[i].r - threshold ) / (1.f - threshold ) );
258+
dm.set( i, ( pixels[i].r - threshold ) / (1.f - threshold ) );
259259
}
260260
return dm;
261261
}

source/mrmeshpy/MRPythonMeshPlugins.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,18 @@ MR_ADD_PYTHON_CUSTOM_DEF( mrmeshpy, SubdivideSettings, [] ( pybind11::module_& m
251251
} )
252252

253253

254-
void saveDistanceMapToImageSimple( const DistanceMap& dm, const std::string& filename, float trashold )
254+
// Distance Map
255+
void saveDistanceMapToImageSimple( const DistanceMap& dm, const std::string& filename, float threshold )
255256
{
256-
saveDistanceMapToImage( dm, filename, trashold );
257+
saveDistanceMapToImage( dm, filename, threshold );
258+
}
259+
260+
DistanceMap loadDistanceMapFromImageSimple( const std::string& path, float threshold )
261+
{
262+
auto res = loadDistanceMapFromImage( path, threshold );
263+
if ( res.has_value() )
264+
return std::move( *res );
265+
return DistanceMap();
257266
}
258267

259268
// Distance Map
@@ -275,9 +284,8 @@ MR_ADD_PYTHON_CUSTOM_DEF( mrmeshpy, DistanceMap, [] ( pybind11::module_& m )
275284
def( "unset", static_cast< void( MR::DistanceMap::* )( size_t, size_t ) >( &MR::DistanceMap::unset), "invalidate X,Y pixel" ).
276285
def( "unset", static_cast< void( MR::DistanceMap::* )( size_t ) >( &MR::DistanceMap::unset), "invalidate by index" );
277286

278-
279287
pybind11::class_<MR::MeshToDistanceMapParams>( m, "MeshToDistanceMapParams" ).
280-
def( pybind11::init<>(), "default constructor. Manual params initialization is required" ).
288+
def( pybind11::init<>(), "Default constructor. Manual params initialization is required" ).
281289
def( "setDistanceLimits", &MR::MeshToDistanceMapParams::setDistanceLimits, pybind11::arg( "min" ), pybind11::arg( "max" ),
282290
"if distance is not in set range, pixel became invalid\n"
283291
"default value: false. Any distance will be applied (include negative)" ).
@@ -291,19 +299,56 @@ MR_ADD_PYTHON_CUSTOM_DEF( mrmeshpy, DistanceMap, [] ( pybind11::module_& m )
291299
def_readwrite( "maxValue", &MR::MeshToDistanceMapParams::maxValue, "Using of this parameter depends on useDistanceLimits" ).
292300
def_readwrite( "resolution", &MR::MeshToDistanceMapParams::resolution, "resolution of distance map" );
293301

302+
pybind11::class_<MR::DistanceMapToWorld>( m, "DistanceMapToWorld", "This structure store data to transform distance map to world coordinates" ).
303+
def( pybind11::init<>(), "Default ctor init all fields with zeros, make sure to fill them manually" ).
304+
def( pybind11::init<const MR::MeshToDistanceMapParams&>(), "Init fields by `MeshToDistanceMapParams` struct" ).
305+
def( pybind11::init<const MR::ContourToDistanceMapParams&>(), "Init fields by `ContourToDistanceMapParams` struct" ).
306+
def( "toWorld", &MR::DistanceMapToWorld::toWorld, pybind11::arg( "x" ), pybind11::arg( "y" ), pybind11::arg( "depth" ),
307+
"Get world coordinate by depth map info.\n"
308+
"x - float X coordinate of depth map: (0.0f - left corner of pixel 0, 1.0 - right corner of pixel 0 and left corner of pixel 1)\n"
309+
"y - float Y coordinate of depth map: (0.0f - left corner of pixel 0, 1.0 - right corner of pixel 0 and left corner of pixel 1)\n"
310+
"depth - value in distance map, represent depth in world" ).
311+
def_readwrite( "orgPoint", &MR::DistanceMapToWorld::orgPoint, "World coordinates of distance map origin corner" ).
312+
def_readwrite( "pixelXVec", &MR::DistanceMapToWorld::pixelXVec, "Vector in world space of pixel x positive direction.\n"
313+
"Note! Length is equal to pixel size. Typically it should be orthogonal to `pixelYVec`." ).
314+
def_readwrite( "pixelYVec", &MR::DistanceMapToWorld::pixelYVec, "Vector in world space of pixel y positive direction.\n"
315+
"Note! Length is equal to pixel size. Typically it should be orthogonal to `pixelXVec`." ).
316+
def_readwrite( "direction", &MR::DistanceMapToWorld::direction, "Vector of depth direction."
317+
"Note! Typically it should be normalized and orthogonal to `pixelXVec` `pixelYVec` plane." );
318+
294319
m.def( "computeDistanceMapD", &MR::computeDistanceMapD, pybind11::arg( "mp" ), pybind11::arg( "params" ),
295320
"computes distance map for presented projection parameters\n"
296321
"use MeshToDistanceMapParams constructor instead of overloads of this function\n"
297322
"MeshPart - input 3d model\n"
298323
"general call. You could customize params manually" );
299324

300-
m.def( "saveDistanceMapToImage", &saveDistanceMapToImageSimple,
325+
m.def( "distanceMapToMesh", &MR::distanceMapToMesh, pybind11::arg( "mp" ), pybind11::arg( "params" ),
326+
"converts distance map back to the mesh fragment with presented params" );
327+
328+
m.def( "saveDistanceMapToImage", &saveDistanceMapToImageSimple,
301329
pybind11::arg( "distMap" ), pybind11::arg( "filename" ), pybind11::arg( "threshold" ) = 1.0f / 255.0f,
302330
"saves distance map to monochrome image in scales of gray:\n"
303331
"\tthreshold - threshold of maximum values [0.; 1.]. invalid pixel set as 0. (black)\n"
304332
"minimum (close): 1.0 (white)\n"
305333
"maximum (far): threshold\n"
306334
"invalid (infinity): 0.0 (black)" );
335+
336+
m.def( "loadDistanceMapFromImage", &loadDistanceMapFromImageSimple,
337+
pybind11::arg( "filename" ), pybind11::arg( "threshold" ) = 1.0f / 255.0f,
338+
"load distance map from monochrome image file\n"
339+
"\tthreshold - threshold of valid values [0.; 1.]. pixel with color less then threshold set invalid" );
340+
341+
m.def( "distanceMapTo2DIsoPolyline", ( Polyline2( * )(const DistanceMap&, float) ) &MR::distanceMapTo2DIsoPolyline,
342+
pybind11::arg( "dm" ), pybind11::arg( "isoValue" ),
343+
"Converts distance map to 2d iso-lines:\n"
344+
"Iso-lines are created in space DistanceMap ( plane OXY with pixelSize = (1, 1) )" );
345+
346+
m.def( "distanceMapTo2DIsoPolyline", ( std::pair<Polyline2, AffineXf3f>( * )( const DistanceMap&, const DistanceMapToWorld&, float, bool ) )& MR::distanceMapTo2DIsoPolyline,
347+
pybind11::arg( "dm" ), pybind11::arg( "params" ), pybind11::arg( "isoValue" ), pybind11::arg( "useDepth" ),
348+
"Iso-lines are created in real space.\n"
349+
"( contours plane with parameters according DistanceMapToWorld )\n"
350+
"Return: pair contours in OXY & transformation from plane OXY to real contours plane" );
351+
307352
} )
308353

309354
// Position Verts Smooth

0 commit comments

Comments
 (0)