Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created May 4, 2012 03:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ashwin/2591901 to your computer and use it in GitHub Desktop.
Output 3D mesh to Ply file
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
float _p[ 3 ];
};
typedef vector< Point > PointVec;
struct Triangle
{
float _v[ 3 ];
};
typedef vector< Triangle > TriangleVec;
void writeMeshToPLYFile
(
const PointVec& pointVec,
const TriangleVec& triangleVec,
const string& outFilename
)
{
ofstream outFile( outFilename.c_str() );
if ( !outFile )
{
cerr << "Error opening output file: " << outFilename << "!" << endl;
exit( 1 );
}
////
// Header
////
const int pointNum = ( int ) pointVec.size();
const int triangleNum = ( int ) triangleVec.size();
outFile << "ply" << endl;
outFile << "format ascii 1.0" << endl;
outFile << "element vertex " << pointNum << endl;
outFile << "property float x" << endl;
outFile << "property float y" << endl;
outFile << "property float z" << endl;
outFile << "element face " << triangleNum << endl;
outFile << "property list uchar int vertex_index" << endl;
outFile << "end_header" << endl;
////
// Points
////
for ( int pi = 0; pi < pointNum; ++pi )
{
const Point& point = pointVec[ pi ];
for ( int vi = 0; vi < 3; ++vi )
outFile << point._p[ vi ] << " ";
outFile << endl;
}
////
// Triangles
////
for ( int ti = 0; ti < triangleNum; ++ti )
{
const Triangle& triangle = triangleVec[ ti ];
outFile << "3 ";
for ( int vi = 0; vi < 3; ++vi )
outFile << triangle._v[ vi ] << " ";
outFile << endl;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment