Meshlab: invoke meshlab command line version

6.5k Views Asked by At

How to call meshlab command line version from inside C++ program or javascript I tried the following code but did not work. I need to create reduced polygon(.obj file) from a inputted(.obj file)

#include <windows.h>
#include <cstdlib>

int main()
{
    std::system("path of meshlab.exe");

}
2

There are 2 best solutions below

0
On

First, this is the C++ code I use to call meshlab (as a viewer) inside my programs. It is quite similar to yours, but you have to be extra careful with correctly scaping whitespace and backslashes in case you are in windows, so maybe this was your problem.

///////////////////////////////////////////////////////////////////////
//Save mesh to a file in .obj format
string outputFilename="output_meshColor.obj";

//Visualize the .obj file with an external viewer
#ifdef WIN32
    //Path to meshlab 2016.12    
    string viewcmd = "\"C:\\Program Files (x86)\\VCG\\MeshLab\\meshlab.exe\"";
    //Path to meshlab 1.32
    //string viewcmd = "C:/meshlab/meshlab_32.exe";
#else
    //Path to meshlab in linux
    string viewcmd = "/usr/bin/meshlab >/dev/null 2>&1 ";
#endif

string cmd = viewcmd+" "+outputFilename;
cout << "Executing external command: " << cmd << endl;
int result = std::system(cmd.c_str());

Next, as pointed by lelabo_m, you should think about using meshlabserver instead, to automatize the creation of reduced polygon mesh. The command you need to use is

meshlabserver -i inputMesh.obj -o reducedMesh.obj -s quadric.mlx

where quadric.mlx is a file like this, but replacing the 1400 in the target number of triangles in the first "

<!DOCTYPE FilterScript>
<FilterScript>
 <filter name="Simplification: Quadric Edge Collapse Decimation">
  <Param  value="1400" tooltip="The desired final number of faces." name="TargetFaceNum" type="RichInt" description="Target number of faces"/>
  <Param  value="0" tooltip="If non zero, this parameter specifies the desired final size of the mesh as a percentage of the initial size." name="TargetPerc" type="RichFloat" description="Percentage reduction (0..1)"/>
  <Param  value="0.3" tooltip="Quality threshold for penalizing bad shaped faces.&lt;br>The value is in the range [0..1]&#xa; 0 accept any kind of face (no penalties),&#xa; 0.5  penalize faces with quality &lt; 0.5, proportionally to their shape&#xa;" name="QualityThr" type="RichFloat" description="Quality threshold"/>
  <Param  value="false" tooltip="The simplification process tries to do not affect mesh boundaries during simplification" name="PreserveBoundary" type="RichBool" description="Preserve Boundary of the mesh"/>
  <Param  value="1" tooltip="The importance of the boundary during simplification. Default (1.0) means that the boundary has the same importance of the rest. Values greater than 1.0 raise boundary importance and has the effect of removing less vertices on the border. Admitted range of values (0,+inf). " name="BoundaryWeight" type="RichFloat" description="Boundary Preserving Weight"/>
  <Param  value="true" tooltip="Try to avoid face flipping effects and try to preserve the original orientation of the surface" name="PreserveNormal" type="RichBool" description="Preserve Normal"/>
  <Param  value="false" tooltip="Avoid all the collapses that should cause a topology change in the mesh (like closing holes, squeezing handles, etc). If checked the genus of the mesh should stay unchanged." name="PreserveTopology" type="RichBool" description="Preserve Topology"/>
  <Param  value="true" tooltip="Each collapsed vertex is placed in the position minimizing the quadric error.&#xa; It can fail (creating bad spikes) in case of very flat areas. &#xa;If disabled edges are collapsed onto one of the two original vertices and the final mesh is composed by a subset of the original vertices. " name="OptimalPlacement" type="RichBool" description="Optimal position of simplified vertices"/>
  <Param  value="false" tooltip="Add additional simplification constraints that improves the quality of the simplification of the planar portion of the mesh, as a side effect, more triangles will be preserved in flat areas (allowing better shaped triangles)." name="PlanarQuadric" type="RichBool" description="Planar Simplification"/>
  <Param  value="0.001" tooltip="How much we should try to preserve the triangles in the planar regions. If you lower this value planar areas will be simplified more." name="PlanarWeight" type="RichFloat" description="Planar Simp. Weight"/>
  <Param  value="false" tooltip="Use the Per-Vertex quality as a weighting factor for the simplification. The weight is used as a error amplification value, so a vertex with a high quality value will not be simplified and a portion of the mesh with low quality values will be aggressively simplified." name="QualityWeight" type="RichBool" description="Weighted Simplification"/>
  <Param  value="true" tooltip="After the simplification an additional set of steps is performed to clean the mesh (unreferenced vertices, bad faces, etc)" name="AutoClean" type="RichBool" description="Post-simplification cleaning"/>
  <Param  value="false" tooltip="The simplification is applied only to the selected set of faces.&#xa; Take care of the target number of faces!" name="Selected" type="RichBool" description="Simplify only selected faces"/>
 </filter>
</FilterScript>
0
On

It's weird, it should have worked. Check your path.

You could also take a look on meshlabserver.exe. It's a command line version of meshlab.