DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Static Public Member Functions
DGtal::MeshReader< TPoint > Struct Template Reference

#include <MeshReader.h>

Static Public Member Functions

static bool importOFFFile (const std::string &filename, DGtal::MeshFromPoints< TPoint > &aMesh, bool invertVertexOrder=false) throw (DGtal::IOException)
static bool importOFSFile (const std::string &filename, DGtal::MeshFromPoints< TPoint > &aMesh, bool invertVertexOrder=false, double scale=1.0) throw (DGtal::IOException)

Detailed Description

template<typename TPoint>
struct DGtal::MeshReader< TPoint >

Aim: Defined to import OFF and OFS surface mesh. It allows to import a MeshFromPoints object and takes into accouts the optional color faces.

Description of class 'MeshReader'

The importation can be done automatically according the input file extension with the operator <<

Example of typical use: Add these include files:

#include "DGtal/shapes/fromPoints/MeshFromPoints.h"
#include "DGtal/io/readers/MeshReader.h"

And automatic import the MeshFromPoints through the filename extension:

std::string filenameOFF = testPath + "samples/box.off";
MeshFromPoints<Point> a3DMesh;
bool importOK = a3DMesh << filenameOFF;

Then you can also display the resulting with a Viewer3D:

viewer.setLineColor(DGtal::Color(150,0,0,254));
viewer << a3DMesh;
See also:
MeshFromPoints MeshWriter

Definition at line 89 of file MeshReader.h.


Member Function Documentation

template<typename TPoint >
bool DGtal::MeshReader< TPoint >::importOFFFile ( const std::string &  filename,
DGtal::MeshFromPoints< TPoint > &  aMesh,
bool  invertVertexOrder = false 
) throw (DGtal::IOException)
inlinestatic

Main method to import OFF meshes file (Geomview Object File Format)

Parameters:
filenamethe file name to import.
aMesh,:(return) the mesh object to be imported.
invertVertexOrder,:used to invert (default value=false) the order of imported points (important for normal orientation).
Returns:
an instance of the imported mesh: MeshFromPoint.

Definition at line 52 of file MeshReader.ih.

References DGtal::Trace::error(), and DGtal::trace.

Referenced by DGtal::operator<<().

{
ifstream infile;
try
{
infile.open (aFilename.c_str(), ifstream::in);
}
catch( ... )
{
trace.error() << "MeshReader : can't open " << aFilename << endl;
throw dgtalio;
}
string str;
getline( infile, str );
if ( ! infile.good() )
{
trace.error() << "MeshReader : can't read " << aFilename << endl;
throw dgtalio;
}
if ( str.substr(0,3) != "OFF")
{
cerr <<"*" <<str<<"*"<< endl;
trace.error() << "MeshReader : No OFF format in " << aFilename << endl;
throw dgtalio;
}
// Processing comments
do
{
getline( infile, str );
if ( ! infile.good() ){
trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
}
while ( str[ 0 ] == '#' || str=="");
istringstream str_in( str );
int nbPoints, nbFaces, nbEdges;
str_in >> nbPoints;
str_in >> nbFaces;
str_in >> nbEdges;
// Reading mesh vertex
for(int i=0; i<nbPoints; i++){
TPoint p;
infile >> p[0];
infile >> p[1];
infile >> p[2];
aMesh.addVertex(p);
// Needed since a line can also contain vertex colors
getline(infile, str);
}
// Reading mesh faces
for(int i=0; i<nbFaces; i++){
// Reading the number of face vertex
unsigned int aNbFaceVertex;
infile >> aNbFaceVertex;
vector<unsigned int> aFace;
for (unsigned int j=0; j< aNbFaceVertex; j++){
unsigned int anIndex;
infile >> anIndex;
aFace.push_back(anIndex);
}
if( invertVertexOrder ){
for(unsigned int j=0; j < aFace.size()/2; j++){
unsigned int tmp=aFace.at(j);
aFace.at(j)=aFace.at(aFace.size()-1-j);
aFace.at(aFace.size()-1-j)=tmp;
}
}
// Needed since a can also contain vertex colors
getline(infile, str);
// Contains colors:
bool findValidColor=true;
if(str!=""){
istringstream inColor(str);
double colorR, colorG, colorB, colorT;
findValidColor=inColor.good();
if(findValidColor && inColor.good()){
inColor >> colorR;
}
findValidColor &=!inColor.fail();
if(findValidColor && inColor.good()){
inColor >> colorG;
}
findValidColor &=!inColor.fail();
if(findValidColor && inColor.good()){
inColor >> colorB;
}
findValidColor &=!inColor.fail();
if(findValidColor && inColor.good()){
inColor >> colorT;
// Since alpha is optional:
if(inColor.fail()){
colorT=1.0;
}
}else{
colorT=1.0;
}
if(findValidColor){
DGtal::Color c((unsigned int)(colorR*255.0), (unsigned int)(colorG*255.0),
(unsigned int)(colorB*255.0), (unsigned int)(colorT*255.0));
aMesh.addFace(aFace, c);
}else{
aMesh.addFace(aFace);
}
}else{
aMesh.addFace(aFace);
}
}
return true;
}
template<typename TPoint >
bool DGtal::MeshReader< TPoint >::importOFSFile ( const std::string &  filename,
DGtal::MeshFromPoints< TPoint > &  aMesh,
bool  invertVertexOrder = false,
double  scale = 1.0 
) throw (DGtal::IOException)
inlinestatic

Main method to import OFS meshes file (an equivalent of OFF format)

Parameters:
filenamethe file name to import.
aMesh,:(return) the mesh object to be imported.
invertVertexOrder,:used to invert (default value=false) the order of imported points (important for normal orientation).
scale,:used to avoid to display tiny shapes (since OFS shapes are generally included in a 1x1x1 cube)
Returns:
an instance of the imported mesh: MeshFromPoint.

Definition at line 182 of file MeshReader.ih.

References DGtal::Trace::error(), and DGtal::trace.

Referenced by DGtal::operator<<().

{
ifstream infile;
try
{
infile.open (aFilename.c_str(), ifstream::in);
}
catch( ... )
{
trace.error() << "MeshReader : can't open " << aFilename << endl;
throw dgtalio;
}
string str;
getline( infile, str );
if ( ! infile.good() )
{
trace.error() << "MeshReader : can't read " << aFilename << endl;
throw dgtalio;
}
if ( str.substr(0,3) != "OFS")
{
trace.error() << "MeshReader : No OFS format in " << aFilename << endl;
throw dgtalio;
}
// Processing comments
do
{
getline( infile, str );
if ( ! infile.good() ){
trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
}
while ( str[ 0 ] == '#' || str=="");
istringstream str_in( str );
int nbPoints;
str_in >> nbPoints;
// Reading mesh vertex
for(int i=0; i<nbPoints; i++){
TPoint p;
infile >> p[0];
infile >> p[1];
infile >> p[2];
p[0]*=scale;
p[1]*=scale;
p[2]*=scale;
aMesh.addVertex(p);
// Needed since a line can also contain vertex colors
getline(infile, str);
}
do
{
getline( infile, str );
if ( ! infile.good() ){
trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
throw dgtalio;
}
}
while ( str[ 0 ] == '#' || str=="");
istringstream str_in2( str );
unsigned int nbFaces;
str_in2 >> nbFaces;
// Reading mesh faces
for(unsigned int i=0; i<nbFaces; i++){
// Reading the number of face vertex
vector<unsigned int> aFace;
for (unsigned int j=0; j< 3; j++){
unsigned int anIndex;
infile >> anIndex;
aFace.push_back(anIndex);
}
if( invertVertexOrder ){
unsigned int tmp=aFace.at(0);
aFace.at(0)=aFace.at(2);
aFace.at(2)=tmp;
}
aMesh.addFace(aFace);
getline(infile, str);
}
return true;
}

The documentation for this struct was generated from the following files: