Command line program to view 3d meshes from files and piped input

Alec Jacobson

December 15, 2016

weblog/

Here's a little C++ program to directly render meshes in 3D viewer from the command line.

This let's you write little test programs without worrying about linking to a 3D viewer. You just need to output a mesh in a standard format. For example, here's a tiny program that outputs a cube in an .off format:

#include <igl/read_triangle_mesh.h>
#include <Eigen/Core>
#include <iostream>

int main(int argc, char * argv[])
{
  using namespace Eigen;
  MatrixXd V(8,3);
  MatrixXi Q(6,4);
  V<<
    0,0,1,
    0,1,1,
    1,1,1,
    1,0,1,
    0,0,0,
    0,1,0,
    1,1,0,
    1,0,0;
  Q<<
    3,2,1,0,
    0,1,5,4,
    6,5,1,2,
    3,7,6,2,
    4,7,3,0,
    4,5,6,7;
  std::cout<<
    "OFF\n"<<V.rows()<<" "<<Q.rows()<<" 0\n"<<
    V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","","","","\n"))<<
    (Q.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","4 ","","","\n"));
  return EXIT_SUCCESS;
}

Compile this into cube_off then issue:

./cube_off | view mesh

**Update: ** And here's a funny, little one-liner you can call from matlab to display a mesh via the .obj format:

system(sprintf('echo \"%s%s\" | /usr/local/bin/viewmesh',sprintf('v %0.17f %0.17f %0.17f\n',V'),sprintf('f %d %d %d\n',F')))