gel.scripts.downsample_mesh

Downsamples .stl file with pymeshlab

 1"""Downsamples .stl file with `pymeshlab`"""
 2import argparse
 3import os
 4import pymeshlab
 5
 6
 7def downsample_mesh(num_faces,filename,outputname):
 8    """Reads fine-resolution file, writes coarse-resolution file.
 9
10    * `num_faces`: int number of target faces for downsampled mesh
11    * `filename`: str path to fine-resolution .stl mesh
12    * `outputname`: str path to coarse-resolution .stl mesh to create
13
14    Side-effects: writes the new file
15    """
16    ms = pymeshlab.MeshSet()
17    ms.load_new_mesh(filename)
18
19    # New version
20    ms.apply_filter(
21        "meshing_decimation_quadric_edge_collapse",
22        targetfacenum=num_faces,
23        qualitythr=1,
24        preserveboundary=True,
25        preservenormal=True,
26        preservetopology=True,
27        planarquadric=True
28    )
29
30    # New version
31    ms.apply_filter("meshing_re_orient_faces_coherently")
32
33    ms.save_current_mesh(outputname)
34
35
36def downsample_mesh_main():
37    """The function invoked by the command. Parses arguments and passes
38    to `downsample_mesh`.
39    """
40    parser = argparse.ArgumentParser(
41        description="Interface to pymeshlab quadratic edge collapse for"
42        " FEniCS environment usage"
43    )
44    parser.add_argument(
45        "-d",
46        type=str,
47        metavar="WORKING_DIR",
48        help="directory with input CytoD.stl and for output "
49        "CytoD_downsampled.stl"
50    )
51    parser.add_argument(
52        "-n",
53        "--num-reduced-faces",
54        type=int,
55        metavar="NUM_REDUCED_FACES",
56        default=1100,
57        help="target number of triangular faces"
58    )
59    args = parser.parse_args()
60
61    downsample_mesh(
62        args.num_reduced_faces,
63        os.path.join(args.d, "CytoD.stl"),
64        os.path.join(args.d, "CytoD_downsampled.stl")
65    )
66
67
68if __name__=="__main__":
69    downsample_mesh_main()
def downsample_mesh(num_faces, filename, outputname):
 8def downsample_mesh(num_faces,filename,outputname):
 9    """Reads fine-resolution file, writes coarse-resolution file.
10
11    * `num_faces`: int number of target faces for downsampled mesh
12    * `filename`: str path to fine-resolution .stl mesh
13    * `outputname`: str path to coarse-resolution .stl mesh to create
14
15    Side-effects: writes the new file
16    """
17    ms = pymeshlab.MeshSet()
18    ms.load_new_mesh(filename)
19
20    # New version
21    ms.apply_filter(
22        "meshing_decimation_quadric_edge_collapse",
23        targetfacenum=num_faces,
24        qualitythr=1,
25        preserveboundary=True,
26        preservenormal=True,
27        preservetopology=True,
28        planarquadric=True
29    )
30
31    # New version
32    ms.apply_filter("meshing_re_orient_faces_coherently")
33
34    ms.save_current_mesh(outputname)

Reads fine-resolution file, writes coarse-resolution file.

  • num_faces: int number of target faces for downsampled mesh
  • filename: str path to fine-resolution .stl mesh
  • outputname: str path to coarse-resolution .stl mesh to create

Side-effects: writes the new file

def downsample_mesh_main():
37def downsample_mesh_main():
38    """The function invoked by the command. Parses arguments and passes
39    to `downsample_mesh`.
40    """
41    parser = argparse.ArgumentParser(
42        description="Interface to pymeshlab quadratic edge collapse for"
43        " FEniCS environment usage"
44    )
45    parser.add_argument(
46        "-d",
47        type=str,
48        metavar="WORKING_DIR",
49        help="directory with input CytoD.stl and for output "
50        "CytoD_downsampled.stl"
51    )
52    parser.add_argument(
53        "-n",
54        "--num-reduced-faces",
55        type=int,
56        metavar="NUM_REDUCED_FACES",
57        default=1100,
58        help="target number of triangular faces"
59    )
60    args = parser.parse_args()
61
62    downsample_mesh(
63        args.num_reduced_faces,
64        os.path.join(args.d, "CytoD.stl"),
65        os.path.join(args.d, "CytoD_downsampled.stl")
66    )

The function invoked by the command. Parses arguments and passes to downsample_mesh.