MeshLib Documentation
Loading...
Searching...
No Matches
Mesh Offset

Example of mesh offset

▶ See it live in the browser

  • C++
    #include <MRMesh/MRBox.h>
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRCube.h>
    #include <iostream>
    int main()
    {
    // Create mesh
    // Setup parameters
    // calculate voxel size depending on desired accuracy and/or memory consumption
    params.voxelSize = suggestVoxelSize( mesh, 10000000.f );
    if ( !MR::findRightBoundary( mesh.topology ).empty() )
    params.signDetectionMode = MR::SignDetectionMode::HoleWindingRule; // use if you have holes in mesh
    // Make offset mesh
    float offset = mesh.computeBoundingBox().diagonal() * 0.1f;
    auto meshRes = MR::generalOffsetMesh( mesh, offset, params );
    if ( !meshRes.has_value() )
    {
    std::cerr << meshRes.error() << std::endl;
    return 1;
    }
    // Save result
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( *meshRes, "mesh_offset.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Expected< Mesh > generalOffsetMesh(const MeshPart &mp, float offset, const GeneralOffsetParameters &params)
    std::vector< EdgeLoop > findRightBoundary(const MeshTopology &topology, const FaceBitSet *region=nullptr, Turn turn=Turn::Rightmost)
    Mesh makeCube(const Vector3f &size=Vector3f::diagonal(1.0f), const Vector3f &base=Vector3f::diagonal(-0.5f))
    const std::shared_ptr< const Mesh > & mesh() const
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    Source mesh
    After offset
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    import sys
    # Load mesh
    mesh = mrmeshpy.loadMesh("mesh.stl")
    # Setup parameters
    params = mrmeshpy.OffsetParameters()
    params.voxelSize = mesh.computeBoundingBox().diagonal() * 5e-3 # offset grid precision (algorithm is voxel based)
    if mrmeshpy.findRightBoundary(mesh.topology).empty():
    params.signDetectionMode = mrmeshpy.SignDetectionMode.HoleWindingRule # use if you have holes in mesh
    # Make offset mesh
    offset = mesh.computeBoundingBox().diagonal() * 0.05
    try:
    result_mesh = mrmeshpy.offsetMesh(mesh, offset, params)
    except RuntimeError as e:
    print(e)
    sys.exit(1)
    # Save result
    mrmeshpy.saveMesh(result_mesh, "offsetMesh.stl")
    static constexpr SymMatrix2 diagonal(T diagVal) noexcept
    bool empty() const
    Source mesh
    After offset
  • C
    #include <MRCMesh/MRBox.h>
    #include <MRCMesh/MRCube.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshPart.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRMeshTopology.h>
    #include <MRCMesh/MRRegionBoundary.h>
    #include <MRCMesh/MRSignDetectionMode.h>
    #include <MRCMesh/MRString.h>
    #include <MRCMesh/MRVector3.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <MRCMisc/std_vector_std_vector_MR_EdgeId.h>
    #include <MRCVoxels/MROffset.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Create mesh
    MR_Vector3f size = MR_Vector3f_diagonal( 1.f );
    MR_Vector3f base = MR_Vector3f_diagonal( -0.5f );
    MR_Mesh* mesh = MR_makeCube( &size, &base );
    // offset functions can also be applied to separate mesh components rather than to the whole mesh
    // this is not our case, so the region is set to NULL
    MR_MeshPart* inputMeshPart = MR_MeshPart_Construct( mesh, NULL );
    // Setup parameters
    MR_GeneralOffsetParameters* params = MR_GeneralOffsetParameters_DefaultConstruct();
    // calculate voxel size depending on desired accuracy and/or memory consumption
    MR_GeneralOffsetParameters_Set_voxelSize( params, MR_suggestVoxelSize( inputMeshPart, 10000000.f ) );
    MR_std_vector_std_vector_MR_EdgeId* holes = MR_findRightBoundary( MR_Mesh_Get_topology( mesh ), NULL, NULL );
    if ( !MR_std_vector_std_vector_MR_EdgeId_empty( holes ) )
    MR_GeneralOffsetParameters_Set_signDetectionMode( params, MR_SignDetectionMode_HoleWindingRule ); // use if you have holes in mesh
    MR_std_vector_std_vector_MR_EdgeId_Destroy( holes );
    // Make offset mesh
    MR_Box3f bbox = MR_Mesh_computeBoundingBox( mesh, NULL );
    float offset = MR_Box3f_diagonal( &bbox ) * 0.1f;
    MR_expected_MR_Mesh_std_string* outputMeshEx = MR_generalOffsetMesh( inputMeshPart, offset, params );
    MR_MeshPart_Destroy( inputMeshPart );
    MR_GeneralOffsetParameters_Destroy( params );
    MR_Mesh* outputMesh = MR_expected_MR_Mesh_std_string_value_mut( outputMeshEx );
    if ( !outputMesh )
    {
    fprintf( stderr, "Failed to perform offset: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( outputMeshEx ) ) );
    goto fail_offset;
    }
    // Save result
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( outputMesh, "mesh_offset.stl", NULL, NULL);
    if ( MR_expected_void_std_string_error( saveEx ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx ) ) );
    goto fail_save;
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_offset:
    MR_expected_MR_Mesh_std_string_Destroy( outputMeshEx );
    MR_Mesh_Destroy( mesh );
    return rc;
    }
    size_t size() const
    Source mesh
    After offset
  • C#
    public static class MeshOffsetExample
    {
    public static void Main(string[] args)
    {
    // Create mesh
    var mesh = MR.makeCube();
    // Setup parameters
    MR.GeneralOffsetParameters parameters = new();
    // calculate voxel size depending on desired accuracy and/or memory consumption
    parameters.voxelSize = MR.suggestVoxelSize(mesh, 10000000.0f);
    if (!MR.findRightBoundary(mesh.topology).empty())
    parameters.signDetectionMode = MR.SignDetectionMode.HoleWindingRule; // use if you have holes in mesh
    // Make offset mesh
    float offset = mesh.computeBoundingBox().diagonal() * 0.1f;
    var result = MR.generalOffsetMesh(mesh, offset, parameters);
    // Save result
    MR.MeshSave.toAnySupportedFormat(result, "mesh_offset.stl");
    }
    }
    Source mesh
    After offset
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // Create mesh
    using mesh = ml.makeCube();
    // Setup parameters
    using params = new ml.GeneralOffsetParameters();
    // calculate voxel size depending on desired accuracy and/or memory consumption
    params.voxelSize = ml.suggestVoxelSize(mesh, 10000000.0);
    using topology = mesh.topology;
    if (ml.findRightBoundary(topology).length > 0)
    params.signDetectionMode = ml.SignDetectionMode.HoleWindingRule; // use if you have holes in mesh
    // Make offset mesh
    using box = mesh.computeBoundingBox();
    const offset = box.diagonal() * 0.1;
    using result = ml.generalOffsetMesh(mesh, offset, params);
    // Save result
    ml.MeshSave.toAnySupportedFormat(result, 'mesh_offset.stl');
    Note
    The using declaration requires Node.js 24+ or a modern browser. On older runtimes, call .delete() on each object instead (see Memory Management in the JavaScript Setup Guide).
    Source mesh
    After offset
  • Rust
    use meshlib::{GeneralOffsetParameters, MeshPart, SignDetectionMode};
    fn main() -> Result<(), String>
    {
    // Create mesh
    let mesh = meshlib::make_cube(None, None);
    let mesh_part = MeshPart::construct(&mesh, None);
    // Setup parameters
    let mut params = GeneralOffsetParameters::new();
    // calculate voxel size depending on desired accuracy and/or memory consumption
    params.set_voxel_size(meshlib::suggest_voxel_size(&mesh_part, 10000000.0));
    if !meshlib::find_right_boundary(mesh.topology(), None, None).empty()
    {
    params.set_sign_detection_mode(SignDetectionMode::HoleWindingRule); // use if you have holes in mesh
    }
    // Make offset mesh
    let offset = mesh.compute_bounding_box(None).diagonal() * 0.1;
    let result = meshlib::general_offset_mesh(&mesh_part, offset, &params).into_result()?;
    // Save result
    meshlib::mesh_save::to_any_supported_format(&result, "mesh_offset.stl", None).into_result()?;
    Ok(())
    }
    Source mesh
    After offset
  • Java
    import meshlib.Functions;
    import meshlib.GeneralOffsetParameters;
    import meshlib.Mesh;
    import meshlib.MeshPart;
    import meshlib.SignDetectionMode;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshOffset
    {
    public static void main(String[] args)
    {
    // Create mesh
    Mesh mesh = Functions.makeCube(null, null);
    MeshPart meshPart = new MeshPart(mesh, null);
    // Setup parameters
    GeneralOffsetParameters params = new GeneralOffsetParameters();
    // calculate voxel size depending on desired accuracy and/or memory consumption
    params.setVoxelSize(Functions.suggestVoxelSize(meshPart, 10000000.0f));
    if (!Functions.findRightBoundary(mesh.topology(), null, null).empty())
    params.setSignDetectionMode(SignDetectionMode.HoleWindingRule); // use if you have holes in mesh
    // Make offset mesh
    float offset = mesh.computeBoundingBox(null).diagonal() * 0.1f;
    // An error is thrown as `UnexpectedResultException`.
    Mesh result = Functions.generalOffsetMesh(meshPart, offset, params);
    // Save result
    meshlib.meshsave.Functions.toAnySupportedFormat(result, "mesh_offset.stl", null);
    }
    }
    MeshPart meshPart() const
    Source mesh
    After offset