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

Example of mesh weighted offset

  • C++
    #include "MRMesh/MRTorus.h"
    #include "MRMesh/MRMesh.h"
    #include <iostream>
    int main()
    {
    // CTM, JPEG and other formats beyond the basic ones come with the MRIOExtras library
    // Create some mesh
    auto mesh = MR::makeTorus();
    // Create VertScalars obj with weights for every vertex
    auto vertSize = mesh.topology.vertSize();
    MR::VertScalars scalars( vertSize );
    MR::ParallelFor( scalars, [&] ( MR::VertId v )
    {
    scalars[v] = std::abs( mesh.points[v].x / 5.0f ); // Individual extra offset sizes for points
    } );
    // Algorithm is voxel based, voxel size affects performance and form of result mesh
    params.voxelSize = MR::suggestVoxelSize( mesh, 1000 );
    // common basic offset applied for all point
    // Vertex-specific weighted offsets applied after the basic one
    params.offset = 0.2f;
    params.dist.maxWeight = *std::max_element( begin( scalars ), end( scalars ) ); // should always have maximum between weights provided
    auto res = MR::WeightedShell::meshShell( mesh, scalars, params );
    if ( !res.has_value() )
    {
    std::cerr << res.error();
    return 1;
    }
    auto saveRes = MR::MeshSave::toAnySupportedFormat( *res, "offset_weighted.ctm" );
    if ( !saveRes.has_value() )
    {
    std::cerr << saveRes.error();
    return 1;
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    auto ParallelFor(I begin, I end, F &&f, Cb &&... cb)
    Iterator end() const
    Mesh makeTorus(float primaryRadius=1.0f, float secondaryRadius=0.1f, int primaryResolution=16, int secondaryResolution=16, std::vector< Vector3f > *points=nullptr)
    float suggestVoxelSize(const MeshPart &mp, float approxNumVoxels)
    void loadIOExtras()
    size_t vertSize() const
    Iterator begin() const
    const std::shared_ptr< const Mesh > & mesh() const
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    Expected< Mesh > meshShell(const Mesh &mesh, const ParametersMetric &params)
  • Python
    from meshlib import mrmeshpy
    # Create some mesh
    mesh = mrmeshpy.makeTorus(primaryRadius=1.0,
    secondaryRadius=0.1,
    primaryResolution=16,
    secondaryResolution=16)
    verts_num = mesh.points.size()
    # Create VertScalars obj with weights for every vertex
    scalars = mrmeshpy.VertScalars(verts_num)
    for i in range(verts_num):
    weight = abs(mesh.points[mrmeshpy.VertId(i)].x / 5) # Individual extra offset sizes for points
    scalars[mrmeshpy.VertId(i)] = weight
    # ===params
    params = mrmeshpy.WeightedShell.ParametersMetric()
    # Algorithm is voxel based, voxel size affects performance and form of result mesh
    params.voxelSize = mrmeshpy.suggestVoxelSize(mesh, 10000)
    # common basic offset applied for all point
    # Vertex-specific weighted offsets applied after the basic one
    params.offset = 0.2
    params.dist.maxWeight = max(scalars) # should always have maximum between weights provided
    # ===offset
    res = mrmeshpy.WeightedShell.meshShell(mesh, scalars, params)
    mrmeshpy.saveMesh(res, "offset_weighted.ctm")
  • C
    #include <MRCMesh/MRTorus.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshTopology.h>
    #include <MRCMesh/MRMeshPart.h>
    #include <MRCMesh/MRVector.h>
    #include <MRCVoxels/MRWeightedPointsShell.h>
    #include <MRCMesh/MRClosestWeightedPoint.h>
    #include <MRCVoxels/MROffset.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMisc/std_string.h>
    #include <MRCMisc/std_vector_MR_Vector3f.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Create some mesh
    MR_Mesh* mesh = MR_makeTorus( NULL, NULL, NULL, NULL, NULL );
    // Create VertScalars obj with weights for every vertex
    size_t vertSize = MR_MeshTopology_vertSize( MR_Mesh_Get_topology( mesh ) );
    MR_VertScalars* scalars = MR_VertScalars_Construct( vertSize );
    float maxWeight = 0.0f;
    for ( size_t i = 0; i < vertSize; ++i )
    {
    const MR_Vector3f* point = MR_VertCoords_index( MR_Mesh_Get_points( mesh ), (MR_VertId){ (int)i } );
    float weight = fabsf( point->x / 5.0f ); // Individual extra offset sizes for points
    MR_VertScalars_data_mut( scalars )[i] = weight;
    maxWeight = fmaxf( maxWeight, weight ); // should always have maximum between weights provided
    }
    MR_WeightedShell_ParametersMetric* params = MR_WeightedShell_ParametersMetric_DefaultConstruct();
    // Algorithm is voxel based, voxel size affects performance and form of result mesh
    MR_MeshPart* mp = MR_MeshPart_Construct( mesh, NULL );
    MR_WeightedShell_ParametersMetric_Set_voxelSize( params, MR_suggestVoxelSize( mp, 1000.0f ) );
    // common basic offset applied for all point
    // Vertex-specific weighted offsets applied after the basic one
    MR_WeightedShell_ParametersMetric_Set_offset( params, 0.2f );
    MR_DistanceFromWeightedPointsParams* dist = MR_WeightedShell_ParametersMetric_GetMutable_dist( params );
    MR_DistanceFromWeightedPointsParams_Set_maxWeight( dist, maxWeight );
    MR_expected_MR_Mesh_std_string* res = MR_WeightedShell_meshShell( mesh, scalars, params );
    MR_Mesh* resMesh = MR_expected_MR_Mesh_std_string_value_mut( res );
    if ( !resMesh )
    {
    fprintf( stderr, "Failed to create shell: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( res ) ) );
    goto fail_shell;
    }
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( resMesh, "offset_weighted.ctm", 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; // error while saving file
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_shell:
    MR_expected_MR_Mesh_std_string_Destroy( res );
    MR_MeshPart_Destroy( mp );
    MR_WeightedShell_ParametersMetric_Destroy( params );
    MR_VertScalars_Destroy( scalars );
    MR_Mesh_Destroy( mesh );
    return rc;
    }
    float dist() const
    const UiRenderParams * params
  • C#
    public static class MeshOffsetWeightedExample
    {
    public static void Main(string[] args)
    {
    // Create some mesh
    var mesh = MR.makeTorus();
    // Create VertScalars obj with weights for every vertex
    var vertSize = mesh.topology.vertSize();
    MR.VertScalars scalars = new(vertSize);
    float maxWeight = 0.0f;
    for (int i = 0; i < (int)vertSize; ++i)
    {
    var v = new MR.VertId(i);
    scalars[v] = MathF.Abs(mesh.points[v].x / 5.0f); // Individual extra offset sizes for points
    maxWeight = MathF.Max(maxWeight, scalars[v]);
    }
    // Algorithm is voxel based, voxel size affects performance and form of result mesh
    parameters.voxelSize = MR.suggestVoxelSize(mesh, 1000);
    // common basic offset applied for all point
    // Vertex-specific weighted offsets applied after the basic one
    parameters.offset = 0.2f;
    parameters.dist.maxWeight = maxWeight; // should always have maximum between weights provided
    var res = MR.WeightedShell.meshShell(mesh, scalars, parameters);
    MR.MeshSave.toAnySupportedFormat(res, "offset_weighted.ctm");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    using mesh = ml.makeTorus();
    // build a per-vertex weight (extra offset) from each vertex's x coordinate
    using coords = mesh.points;
    const positions = coords.toArray();
    const weights = new Float32Array(positions.length / 3);
    let maxWeight = 0;
    for (let v = 0; v < weights.length; ++v) {
    weights[v] = Math.abs(positions[3 * v] / 5);
    maxWeight = Math.max(maxWeight, weights[v]);
    }
    using scalars = ml.VertScalars.fromArray(weights);
    using params = new ml.WeightedShellParametersMetric();
    // the algorithm is voxel-based; the voxel size affects performance and the result shape
    params.voxelSize = ml.suggestVoxelSize(mesh, 1000);
    // a common basic offset applied to every vertex; the weighted offsets are added on top
    params.offset = 0.2;
    // the maximum weight must be an upper bound of the provided weights
    using dist = params.dist;
    dist.maxWeight = maxWeight;
    params.dist = dist;
    using result = ml.WeightedShell.meshShell(mesh, scalars, params);
    ml.MeshSave.toAnySupportedFormat(result, 'offset_weighted.ply');
    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).
  • Rust
    use meshlib::{MeshPart, VertId, VertScalars};
    fn main() -> Result<(), String>
    {
    // Create some mesh
    let mesh = meshlib::make_torus(None, None, None, None, None);
    // Create VertScalars obj with weights for every vertex
    let vert_size = mesh.topology().vert_size();
    let mut scalars = VertScalars::construct(vert_size);
    for i in 0..vert_size
    {
    let v = VertId {id_: i as i32};
    *scalars.index_mut(v) = (mesh.points().index(v).x / 5.0).abs(); // Individual extra offset sizes for points
    }
    let mut params = meshlib::weighted_shell::ParametersMetric::new();
    // Algorithm is voxel based, voxel size affects performance and form of result mesh
    params.set_voxel_size(meshlib::suggest_voxel_size(&MeshPart::construct(&mesh, None), 1000.0));
    // common basic offset applied for all point
    // Vertex-specific weighted offsets applied after the basic one
    params.set_offset(0.2);
    let max_weight = (0..vert_size).map(|i| *scalars.index(VertId {id_: i as i32})).fold(f32::MIN, f32::max);
    params.dist_mut().set_max_weight(max_weight); // should always have maximum between weights provided
    let result = meshlib::weighted_shell::mesh_shell(&mesh, &scalars, &params).into_result()?;
    meshlib::mesh_save::to_any_supported_format(&result, "offset_weighted.ctm", None).into_result()?;
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.MeshPart;
    import meshlib.VertId;
    import meshlib.VertScalars;
    import meshlib.weightedshell.ParametersMetric;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshOffsetWeighted
    {
    public static void main(String[] args)
    {
    // Create some mesh
    Mesh mesh = Functions.makeTorus(null, null, null, null, null);
    // Create VertScalars obj with weights for every vertex
    long vertSize = mesh.topology().vertSize();
    VertScalars scalars = new VertScalars(vertSize);
    float maxWeight = 0.0f;
    for (int i = 0; i < vertSize; i++)
    {
    VertId v = new VertId(i);
    float weight = Math.abs(mesh.points().index(v).x() / 5.0f); // Individual extra offset sizes for points
    scalars.autoResizeSet(v, weight);
    maxWeight = Math.max(maxWeight, weight);
    }
    ParametersMetric params = new ParametersMetric();
    // Algorithm is voxel based, voxel size affects performance and form of result mesh
    params.setVoxelSize(Functions.suggestVoxelSize(new MeshPart(mesh, null), 1000.0f));
    // common basic offset applied for all point
    // Vertex-specific weighted offsets applied after the basic one
    params.setOffset(0.2f);
    params.dist().setMaxWeight(maxWeight); // should always have maximum between weights provided
    // An error is thrown as `UnexpectedResultException`.
    Mesh result = meshlib.weightedshell.Functions.meshShell(mesh, scalars, params);
    meshlib.meshsave.Functions.toAnySupportedFormat(result, "offset_weighted.ctm", null);
    }
    }