Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Command line program to view 3D meshes from files, standard input and pipes
#include <igl/guess_extension.h>
#include <igl/read_triangle_mesh.h>
#include <igl/viewer/Viewer.h>
#include <Eigen/Core>
#include <iostream>
#include <string>
#include <cstdio>
#include <unistd.h>
int main(int argc, char * argv[])
{
Eigen::MatrixXd V;
Eigen::MatrixXi F;
if(argc >= 2)
{
igl::read_triangle_mesh(argv[1],V,F);
}else if(isatty(fileno(stdin)))
{
std::cerr<<R"(Usage:
./viewmesh input.obj
./viewmesh < input.obj
cat input.obj | ./viewmesh
)";
return EXIT_FAILURE;
}else{
FILE * fp = tmpfile();
while(std::cin.good())
{
std::vector<char> buf(1024);
std::cin.read(&buf[0],buf.size());
fwrite(&buf[0],sizeof(char),std::cin.gcount(),fp);
}
fp = freopen(NULL,"r",fp);
std::string ext = igl::guess_extension(fp);
std::cout<<"[treating stdin as ."<<ext<<" file]"<<std::endl;
igl::read_triangle_mesh(ext,fp,V,F);
}
igl::viewer::Viewer v;
v.data.set_mesh(V,F);
v.launch();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment