Add File I/O

This commit is contained in:
Paolo Cignoni 2006-11-16 11:24:44 +00:00
parent 48669a758a
commit b9eebf7d98
5 changed files with 1268 additions and 1093 deletions

View File

@ -20,15 +20,9 @@
* for more details. *
* *
****************************************************************************/
#ifndef __VCGLIB_EXPORT_DXF
#define __VCGLIB_EXPORT_DXF
namespace vcg {
namespace tri {
namespace io {
@ -43,8 +37,9 @@ public:
*/
static int Save(SaveMeshType &m, const char * filename)
{
FILE * o = fopen(filename,"w");
if(o==NULL) return false;
if(o==NULL) return 1;
fprintf(o,"0\n");
fprintf(o,"SECTION\n");
fprintf(o,"2\n");
@ -83,7 +78,6 @@ public:
fprintf(o,"EOF\n");
fclose(o);
return 0;
}
static const char *ErrorMsg(int error)
@ -107,6 +101,4 @@ public:
} // end Namespace io
} // end Namespace vcg
#endif

View File

@ -20,38 +20,78 @@
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#ifndef __VCGLIB_EXPORT_SMF
#define __VCGLIB_EXPORT_SMF
/** Function to save to Smf format file.
@param filename Name of the new Smf file
*/
void Save_Smf(const char * filename)
{
#include <stdio.h>
namespace vcg {
namespace tri {
namespace io {
template <class SaveMeshType>
class ExporterSMF
{
public:
typedef typename SaveMeshType::VertexPointer VertexPointer;
typedef typename SaveMeshType::ScalarType ScalarType;
typedef typename SaveMeshType::VertexType VertexType;
typedef typename SaveMeshType::FaceType FaceType;
typedef typename SaveMeshType::VertexIterator VertexIterator;
typedef typename SaveMeshType::FaceIterator FaceIterator;
static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
{
VertexIterator vi;
FaceIterator fi;
FILE *fp;
fp = fopen(filename,"wb");
fprintf(fp,"#SMF \n" );
face_iterator fi;
vertex_iterator vi;
map<vertex_pointer,int> index;
std::map<VertexPointer,int> index;
int ind;
for(ind=1,vi=vert.begin(); vi!=vert.end(); ++vi,++ind)
for(ind=1,vi=m.vert.begin(); vi!=m.vert.end(); ++vi,++ind)
{
fprintf(fp,"v " );
fprintf(fp,"%f%s",(*vi).P()[0]," " );
fprintf(fp,"%f%s",(*vi).P()[1]," " );
fprintf(fp,"%f%s",(*vi).P()[2],"\n");
index[&*vi] = ind;
}
for (fi=face.begin(); fi!=face.end(); ++fi)
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
{
fprintf(fp,"%s","f ");
for (int j = 0; j < 3; j++)
fprintf(fp,"%i%s",index[(*fi).V(j)]," ");
fprintf(fp,"%s","\n");
}
fclose(fp);
}
return 0;
}
static const char *ErrorMsg(int error)
{
static std::vector<std::string> smf_error_msg;
if(smf_error_msg.empty())
{
smf_error_msg.resize(2 );
smf_error_msg[0]="No errors";
smf_error_msg[1]="Can't open file";
}
if(error>1 || error<0) return "Unknown error";
else return smf_error_msg[error].c_str();
}
}; // end class
} // end Namespace tri
} // end Namespace io
} // end Namespace vcg
#endif

View File

@ -1,38 +1,32 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef __VCGLIB_EXPORT_WRL
#define __VCGLIB_EXPORT_WRL
#include <stdio.h>
#include <wrap/io_trimesh/io_mask.h>
#include "io_material.h"
/** Function to save in Inventor format file.
@param filename Name of the new inventor file
*/
bool Save_VRML2(const char * filename, int savemask = SM_ALL )
{
namespace vcg {
namespace tri {
namespace io {
template <class SaveMeshType>
class ExporterWRL
{
public:
typedef typename SaveMeshType::VertexPointer VertexPointer;
typedef typename SaveMeshType::ScalarType ScalarType;
typedef typename SaveMeshType::VertexType VertexType;
typedef typename SaveMeshType::FaceType FaceType;
typedef typename SaveMeshType::VertexIterator VertexIterator;
typedef typename SaveMeshType::FaceIterator FaceIterator;
static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
{
FILE *fp;
fp = fopen(filename,"wb");
if(fp==0)
return false;
if(fp==NULL)
return 1;
// Header
fprintf(fp,
@ -45,8 +39,9 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
"}\n"
);
// Trans principale
double ss = 8.0/bbox.Diag();
// Tranche principale
double ss = 8.0/m.bbox.Diag();
m.bbox.Diag();
fprintf(fp,
"Transform {\n"
@ -57,9 +52,9 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
,ss
,ss
,ss
, -bbox.Center()[0]*ss
, -bbox.Center()[1]*ss
,-3-bbox.Center()[2]*ss
, -m.bbox.Center()[0]*ss
, -m.bbox.Center()[1]*ss
,-3-m.bbox.Center()[2]*ss
);
// Start Shape
@ -75,33 +70,34 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" point\n"
" ["
);
face_iterator fi;
vertex_iterator vi;
map<vertex_pointer,int> index;
FaceIterator fi;
VertexIterator vi;
std::map<VertexPointer,int> index;
int ind;
// Vertici
for(ind=0,vi=vert.begin(); vi!=vert.end(); ++vi,++ind)
for(ind=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi, ++ind)
{
if(ind%4==0)
fprintf(fp,"\n ");
fprintf(fp,"%g %g %g, "
fprintf(fp,
"\n "
);
fprintf(fp,
"%g %g %g, "
,(*vi).P()[0]
,(*vi).P()[1]
,(*vi).P()[2]
);
index[&*vi] = ind;
}
fprintf(fp,
"\n"
}
fprintf(fp,"\n"
" ]\n"
" }\n"
);
if( ( savemask & SM_VERTCOLOR ) && (vertex_type::OBJ_TYPE & vertex_type::OBJ_TYPE_C) )
if( m.HasPerVertexColor() && (mask & vcg::tri::io::Mask::IOM_VERTCOLOR))
{
fprintf(fp,
" color Color\n"
@ -109,23 +105,16 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" color\n"
" ["
);
for(ind=0,vi=vert.begin();vi!=vert.end();++vi,++ind)
for(ind=0,vi=m.vert.begin();vi!=m.vert.end();++vi,++ind)
{
float r = float(vi->C()[0])/255;
float g = float(vi->C()[1])/255;
float b = float(vi->C()[2])/255;
if(ind%4==0)
fprintf(fp,"\n ");
fprintf(fp,
"%g %g %g,"
,r,g,b
);
fprintf(fp,"%g %g %g,",r,g,b);
}
fprintf(fp,
"\n"
" ]\n"
@ -134,7 +123,7 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" ["
);
for(ind=0,fi=face.begin(); fi!=face.end(); ++fi,++ind)
for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
{
if(ind%4==0)
fprintf(fp,"\n ");
@ -142,49 +131,38 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
fprintf(fp,"%i,",index[(*fi).V(j)]);
fprintf(fp,"-1,");
}
fprintf(fp,
"\n"
" ]\n"
);
}
else if( ( savemask & SM_WEDGCOLOR ) && (face_type::OBJ_TYPE & face_type::OBJ_TYPE_WC) )
else if( m.HasPerWedgeColor() && (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR ))
{
fprintf(fp,
" color Color\n"
" {\n"
" color\n"
" ["
);
for(ind=0,fi=face.begin();fi!=face.end();++fi,++ind)
" [" );
for(ind=0,fi=m.face.begin();fi!=m.face.end();++fi,++ind)
{
for(int z=0;z<3;++z)
{
float r = float(fi->WC(z)[0])/255;
float g = float(fi->WC(z)[1])/255;
float b = float(fi->WC(z)[2])/255;
if(ind%4==0)
fprintf(fp,"\n ");
fprintf(fp,
"%g %g %g,"
,r,g,b
);
fprintf(fp,"%g %g %g,",r,g,b);
}
}
fprintf(fp,
"\n"
" ]\n"
" }\n"
" colorIndex\n"
" ["
);
" [" );
int nn = 0;
for(ind=0,fi=face.begin(); fi!=face.end(); ++fi,++ind)
for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
{
if(ind%4==0)
fprintf(fp,"\n ");
@ -195,13 +173,12 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
}
fprintf(fp,"-1,");
}
fprintf(fp,
"\n"
" ]\n"
);
}
else if( ( savemask & SM_WEDGTEXCOORD ) && (face_type::OBJ_TYPE & face_type::OBJ_TYPE_WT) )
else if (HasPerWedgeTexture(m) &&(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD))
{
fprintf(fp,
"\n"
@ -210,20 +187,15 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" point\n"
" [\n"
);
for(ind=0,fi=face.begin(); fi!=face.end(); ++fi,++ind)
for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
{
if(ind%4==0)
fprintf(fp,"\n ");
for (int j = 0; j < 3; j++)
{
fprintf(fp,"%g %g, "
,fi->WT(j).u()
,fi->WT(j).v()
);
fprintf(fp,"%g %g, ",fi->WT(j).u(),fi->WT(j).v());
}
}
fprintf(fp,
"\n"
" ]\n"
@ -231,9 +203,8 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" texCoordIndex\n"
" [\n"
);
int nn = 0;
for(ind=0,fi=face.begin(); fi!=face.end(); ++fi,++ind)
for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
{
if(ind%4==0)
fprintf(fp,"\n ");
@ -244,31 +215,26 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
}
fprintf(fp,"-1,");
}
fprintf(fp,
"\n"
" ]\n"
);
}
fprintf(fp,
" coordIndex\n"
" ["
);
// Facce
for(ind=0,fi=face.begin(); fi!=face.end(); ++fi,++ind)
for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
{
if(ind%6==0)
{
fprintf(fp,"\n ");
}
for (int j = 0; j < 3; j++)
fprintf(fp,"%i,",index[(*fi).V(j)]);
fprintf(fp,"-1,");
}
fprintf(fp,
"\n"
" ]\n"
@ -283,23 +249,52 @@ bool Save_VRML2(const char * filename, int savemask = SM_ALL )
" shininess .5\n"
" }\n"
);
if(textures.size())
if(m.textures.size())
{
fprintf(fp,
" texture ImageTexture { url \"%s\" }\n"
,textures[0]
" texture ImageTexture { url \" %s \" }\n"
,m.textures[0].c_str()
);
}
fprintf(fp,
" }\n"
" }\n"
" ]\n"
"}\n"
);
fclose(fp);
return 0;
}
static int GetExportMaskCapability()
{
int capability = 0;
return true;
}
//vert
capability |= MeshModel::IOM_VERTCOLOR;
//wedg
capability |= MeshModel::IOM_WEDGTEXCOORD;
capability |= MeshModel::IOM_WEDGCOLOR;
return capability;
}
static const char *ErrorMsg(int error)
{
static std::vector<std::string> wrl_error_msg;
if(wrl_error_msg.empty())
{
wrl_error_msg.resize(2 );
wrl_error_msg[0]="No errors";
wrl_error_msg[1]="Can't open file";
}
if(error>1 || error<0) return "Unknown error";
else return wrl_error_msg[error].c_str();
}
}; // end class
} // end Namespace io
} // end Namespace tri
} // end Namespace vcg
#endif

View File

@ -32,7 +32,6 @@
namespace vcg {
namespace tri {
namespace io {
/**
This class encapsulate a filter for importing ptx meshes.
*/
@ -40,7 +39,6 @@ namespace vcg {
class ImporterPTX
{
public:
enum PTX_OPEN_MASK_ENUM
{
PTX_ONLY_POINTS = 0x08000000, //BIT_27 no add faces (PTX_FLIPFACES and PTX_SWITCHSIDE are ignored!)
@ -49,7 +47,6 @@ namespace vcg {
PTX_FLIPFACES = 0x40000000, //BIT_30 flip all faces ( PTX_ONLY_POINTS must be false )
PTX_SWITCHSIDE = 0x80000000 //BIT_31 inverse triangulation order (swaping row->cols) ( PTX_ONLY_POINTS must be false )
};
typedef typename OpenMeshType::VertexPointer VertexPointer;
typedef typename OpenMeshType::ScalarType ScalarType;
typedef typename OpenMeshType::VertexType VertexType;
@ -73,8 +70,20 @@ namespace vcg {
RANGEMAP_INFO_TABLE rmapInfo;
};
// skip a mesh
static const char *ErrorMsg(int error)
{
static const char * ptx_error_msg[] =
{
"No errors",
"Can't open file",
"Header not found",
"Eof in header",
"Format not found",
"Syntax error on header",
};
if(error>6 || error<0) return "Unknown error";
else return ptx_error_msg[error];
};
static bool skipmesh(FILE* fp, CallBackPos *cb=NULL)
{
PTX_HEAD_INFO tab;
@ -131,8 +140,7 @@ namespace vcg {
while ( skipmesh( fp, info, cb ) ) {}
return true;
};
static bool Open( OpenMeshType &m, const char * filename, int meshNumber, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
static int Open( OpenMeshType &m, const char * filename, int meshNumber, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
{
FILE *fp;
fp = fopen(filename, "rb");
@ -142,14 +150,15 @@ namespace vcg {
m.fn=0;
PTX_HEAD_INFO ptxHead;
if ( meshNumber>0 ) for (int i=0; i!=meshNumber; ++i) skipmesh(fp, ptxHead, cb);
if (!readPTX( m, fp, mask, meshNumber, cb))
{
m.Clear();
return false;
return 1;
}
clearBadVertex(m, mask, cb);
return true;
return 0;
}
static void clearBadVertex(OpenMeshType &m, int mask, CallBackPos *cb=NULL)
@ -178,7 +187,7 @@ namespace vcg {
}
// eliminate high angle triangles
int angle = 90;
int angle = 88;
//printf(" culling by angle \n");
float limit = cos( angle*3.14159265358979323846/180.0 );
@ -213,11 +222,11 @@ namespace vcg {
}
//if numMesh == -1 load all mesh
static bool Open( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
static int Open_( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
{
FILE *fp;
fp = fopen(filename, "rb");
if(fp == NULL) return false;
if(fp == NULL) return 1;
m.Clear();
m.vn=0;
@ -258,7 +267,7 @@ namespace vcg {
}
fclose(fp);
*/
*/
// now i delete all points in (0,0,0) that are unsampled points
for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
{
@ -284,7 +293,7 @@ namespace vcg {
}
// eliminate high angle triangles
int angle = 90;
int angle = 88;
printf(" culling by angle \n");
float limit = cos( angle*3.14159265358979323846/180.0 );
@ -313,206 +322,8 @@ namespace vcg {
tri::Clean<OpenMeshType>::RemoveUnreferencedVertex(m);
}*/
if(cb) cb(100,"PTX Mesh Loading finish!");
return true;
return 0;
}
static bool readPTX( OpenMeshType &m, FILE *fp, int mask, int mn, CallBackPos *cb=NULL)
{
int colnum;
int rownum;
int numtokens;
char linebuf[256];
int ii;
float xx,yy,zz; // position
float rr,gg,bb; // color
float rf; // reflectance
Matrix44f currtrasf;
bool hascolor;
bool savecolor = ((mask & PTX_COLOR) != 0) && VertexType::HasColor();
bool computeBbox = ((mask & PTX_COMPUTE_AABBOX) != 0);
bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
bool switchside = ((mask & PTX_SWITCHSIDE) != 0);
bool flipfaces = ((mask & PTX_FLIPFACES) != 0);
int total = 50;
if ( onlypoints ) total = 100;
// getting mesh size;
fscanf(fp,"%i\n",&colnum);
fscanf(fp,"%i\n",&rownum);
if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false;
// initial 4 lines [still don't know what is this :) :)]
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
// now the transformation matrix
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(0,0)), &(currtrasf.ElementAt(0,1)), &(currtrasf.ElementAt(0,2)), &(currtrasf.ElementAt(0,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(1,0)), &(currtrasf.ElementAt(1,1)), &(currtrasf.ElementAt(1,2)), &(currtrasf.ElementAt(1,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(2,0)), &(currtrasf.ElementAt(2,1)), &(currtrasf.ElementAt(2,2)), &(currtrasf.ElementAt(2,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(3,0)), &(currtrasf.ElementAt(3,1)), &(currtrasf.ElementAt(3,2)), &(currtrasf.ElementAt(3,3))) )return false;
// now the real data begins
// first line, we should know if the format is
// XX YY ZZ RF
// or it is
// XX YY ZZ RF RR GG BB
// read the entire first line and then count the spaces. it's rude but it works :)
ii=0;
fread(&(linebuf[ii++]),1,1,fp);
while(linebuf[ii-1] != '\n') if ( fread(&(linebuf[ii++]),1,1,fp)==0 ) return false;
linebuf[ii-1] = '\0'; // terminate the string
numtokens=1;
for(ii=0; ii<(int)strlen(linebuf); ii++) if(linebuf[ii] == ' ') numtokens++;
if(numtokens == 4) hascolor = false;
else if(numtokens == 7) hascolor = true;
else return false;
Transpose(currtrasf);
int vn = rownum*colnum;
VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m,vn);
m.vn = vn;
// parse the first line....
if(hascolor)
{
printf("\n hascolor ");
sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
}
else
{
printf("\n no color ");
sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf);
}
if (computeBbox) m.bbox.SetNull();
//addthefirstpoint
(*vi).P()[0]=xx;
(*vi).P()[1]=yy;
(*vi).P()[2]=zz;
(*vi).P() = currtrasf * (*vi).P();
if (computeBbox) m.bbox.Add( (*vi).P() );
if(hascolor && savecolor)
{
(*vi).C()[0]=rr;
(*vi).C()[1]=gg;
(*vi).C()[2]=bb;
}
vi++;
// now for each line until end of mesh (row*col)-1
for(ii=0; ii<((rownum*colnum)-1); ii++)
{
char tmp[255];
sprintf(tmp, "PTX Mesh Loading... mesh %i", mn);
if(cb) cb((ii*total)/vn, tmp);
// read the stream
if(hascolor) fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
else fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf);
// add the point
(*vi).P()[0]=xx;
(*vi).P()[1]=yy;
(*vi).P()[2]=zz;
(*vi).P() = currtrasf * (*vi).P();
if (computeBbox) m.bbox.Add( (*vi).P() );
if(hascolor && savecolor)
{
(*vi).C()[0]=rr;
(*vi).C()[1]=gg;
(*vi).C()[2]=bb;
}
vi++;
}
if(! onlypoints)
{
// now i can triangulate
int trinum = (rownum-1) * (colnum-1) * 2;
OpenMeshType::FaceIterator fi= Allocator<OpenMeshType>::AddFaces(m,trinum);
m.fn = trinum;
int v0i,v1i,v2i, t;
t=0;
for(int rit=0; rit<rownum-1; rit++)
for(int cit=0; cit<colnum-1; cit++)
{
t++;
if(cb) cb(50 + (t*50)/(rownum*colnum),"PTX Mesh Loading");
if(!switchside)
{
v0i = (rit ) + ((cit ) * rownum);
v1i = (rit+1) + ((cit ) * rownum);
v2i = (rit ) + ((cit+1) * rownum);
}
else
{
v0i = (cit ) + ((rit ) * colnum);
v1i = (cit+1) + ((rit ) * colnum);
v2i = (cit ) + ((rit+1) * colnum);
}
// upper tri
(*fi).V(2) = &(m.vert[v0i]);
(*fi).V(1) = &(m.vert[v1i]);
(*fi).V(0) = &(m.vert[v2i]);
if(flipfaces)
{
(*fi).V(2) = &(m.vert[v1i]);
(*fi).V(1) = &(m.vert[v0i]);
}
//m.fn++;
fi++;
if(!switchside)
{
v0i = (rit+1) + ((cit ) * rownum);
v1i = (rit+1) + ((cit+1) * rownum);
v2i = (rit ) + ((cit+1) * rownum);
}
else
{
v0i = (cit+1) + ((rit ) * colnum);
v1i = (cit+1) + ((rit+1) * colnum);
v2i = (cit ) + ((rit+1) * colnum);
}
// lower tri
(*fi).V(2) = &(m.vert[v0i]);
(*fi).V(1) = &(m.vert[v1i]);
(*fi).V(0) = &(m.vert[v2i]);
if(flipfaces)
{
(*fi).V(2) = &(m.vert[v1i]);
(*fi).V(1) = &(m.vert[v0i]);
}
// m.fn++;
fi++;
}
}
return true;
}
static bool readPTX( OpenMeshType &m, FILE *fp, VertexIterator &vi, FaceIterator &fi, const RANGEMAP_INFO &ptxInfo, int mask, int mn, CallBackPos *cb=NULL)
{
int colnum;
@ -727,14 +538,246 @@ namespace vcg {
}
static bool readPTX( OpenMeshType &m, FILE *fp, int mask, CallBackPos *cb=NULL)
{
int numtokens;
int colnum;
int rownum;
float xx,yy,zz; // position
float rr,gg,bb; // color
float rf; // reflectance
Matrix44f currtrasf;
bool hascolor;
bool savecolor = ((mask & PTX_COLOR) != 0) && VertexType::HasColor();
bool computeBbox = ((mask & PTX_COMPUTE_AABBOX) != 0);
bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
bool switchside = ((mask & PTX_SWITCHSIDE) != 0);
bool flipfaces = ((mask & PTX_FLIPFACES) != 0);
int total = 50;
if ( onlypoints ) total = 100;
char linebuf[256];
fscanf(fp,"%i\n",&colnum);
fscanf(fp,"%i\n",&rownum);
if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false;
// initial 4 lines [still don't know what is this :) :)]
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
// now the transformation matrix
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(0,0)), &(currtrasf.ElementAt(0,1)), &(currtrasf.ElementAt(0,2)), &(currtrasf.ElementAt(0,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(1,0)), &(currtrasf.ElementAt(1,1)), &(currtrasf.ElementAt(1,2)), &(currtrasf.ElementAt(1,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(2,0)), &(currtrasf.ElementAt(2,1)), &(currtrasf.ElementAt(2,2)), &(currtrasf.ElementAt(2,3))) )return false;
if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(3,0)), &(currtrasf.ElementAt(3,1)), &(currtrasf.ElementAt(3,2)), &(currtrasf.ElementAt(3,3))) )return false;
//now the real data begins
// first line, we should know if the format is
// XX YY ZZ RF
// or it is
// XX YY ZZ RF RR GG BB
// read the entire first line and then count the spaces. it's rude but it works :)
int ii=0;
fread(&(linebuf[ii++]),1,1,fp);
while(linebuf[ii-1] != '\n') if ( fread(&(linebuf[ii++]),1,1,fp)==0 ) return false;
linebuf[ii-1] = '\0'; // terminate the string
numtokens=1;
for(ii=0; ii<(int)strlen(linebuf); ii++) if(linebuf[ii] == ' ') numtokens++;
if(numtokens == 4) hascolor = false;
else if(numtokens == 7) hascolor = true;
else return false;
Transpose(currtrasf);
int vn = rownum*colnum;
VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m,vn);
m.vn = vn;
// parse the first line....
if(hascolor)
{
printf("\n hascolor ");
sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
}
else
{
printf("\n no color ");
sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf);
}
if (computeBbox) m.bbox.SetNull();
//addthefirstpoint
(*vi).P()[0]=xx;
(*vi).P()[1]=yy;
(*vi).P()[2]=zz;
if (computeBbox) m.bbox.Add( (*vi).P() );
if(hascolor && savecolor)
{
(*vi).C()[0]=rr;
(*vi).C()[1]=gg;
(*vi).C()[2]=bb;
}
vi++;
// now for each line until end of mesh (row*col)-1
for(ii=0; ii<((rownum*colnum)-1); ii++)
{
char tmp[255];
sprintf(tmp, "PTX Mesh Loading...");
if(cb) cb((ii*total)/vn, tmp);
// read the stream
if(hascolor) fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
else fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf);
// add the point
(*vi).P()[0]=xx;
(*vi).P()[1]=yy;
(*vi).P()[2]=zz;
if (computeBbox) m.bbox.Add( (*vi).P() );
if(hascolor && savecolor)
{
(*vi).C()[0]=rr;
(*vi).C()[1]=gg;
(*vi).C()[2]=bb;
}
vi++;
}
if(! onlypoints)
{
// now i can triangulate
int trinum = (rownum-1) * (colnum-1) * 2;
OpenMeshType::FaceIterator fi= Allocator<OpenMeshType>::AddFaces(m,trinum);
m.fn = trinum;
int v0i,v1i,v2i, t;
t=0;
for(int rit=0; rit<rownum-1; rit++)
for(int cit=0; cit<colnum-1; cit++)
{
t++;
if(cb) cb(50 + (t*50)/(rownum*colnum),"PTX Mesh Loading");
if(!switchside)
{
v0i = (rit ) + ((cit ) * rownum);
v1i = (rit+1) + ((cit ) * rownum);
v2i = (rit ) + ((cit+1) * rownum);
}
else
{
v0i = (cit ) + ((rit ) * colnum);
v1i = (cit+1) + ((rit ) * colnum);
v2i = (cit ) + ((rit+1) * colnum);
}
// upper tri
(*fi).V(2) = &(m.vert[v0i]);
(*fi).V(1) = &(m.vert[v1i]);
(*fi).V(0) = &(m.vert[v2i]);
if(flipfaces)
{
(*fi).V(2) = &(m.vert[v1i]);
(*fi).V(1) = &(m.vert[v0i]);
}
fi++;
if(!switchside)
{
v0i = (rit+1) + ((cit ) * rownum);
v1i = (rit+1) + ((cit+1) * rownum);
v2i = (rit ) + ((cit+1) * rownum);
}
else
{
v0i = (cit+1) + ((rit ) * colnum);
v1i = (cit+1) + ((rit+1) * colnum);
v2i = (cit ) + ((rit+1) * colnum);
}
// lower tri
(*fi).V(2) = &(m.vert[v0i]);
(*fi).V(1) = &(m.vert[v1i]);
(*fi).V(0) = &(m.vert[v2i]);
if(flipfaces)
{
(*fi).V(2) = &(m.vert[v1i]);
(*fi).V(1) = &(m.vert[v0i]);
}
fi++;
}
}
if(cb) cb(40,"PTX Mesh Loading - remove bad vertex!");
for(OpenMeshType::VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
{
if((*vi).P() == Point3f(0.0, 0.0, 0.0))
{
(*vi).SetD();
m.vn--;
}
}
if(cb) cb(60,"PTX Mesh Loading - remove bad face!");
onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
if(! onlypoints)
{
for(OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
{
if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) )
{
(*fi).SetD();
m.fn--;
}
}
// eliminate high angle triangles
int angle = 88;
printf(" culling by angle \n");
float limit = cos( angle*3.14159265358979323846/180.0 );
Point3f raggio;
vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
for(OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
if(!(*fi).IsD())
{
raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0;
raggio.Normalize();
if((raggio * (*fi).N()) < limit)
{
(*fi).SetD();
m.fn--;
}
}
}
for(OpenMeshType::VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
{
if(!(*vi).IsD())
(*vi).P() = currtrasf * (*vi).P();
}
vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
vcg::tri::UpdateBounding<CMeshO>::Box(m);
if(cb) cb(100,"PTX Mesh Loading finish!");
return true;
}
//The Open function called from meshio.cpp
static int Open( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
{
FILE *fp;
fp = fopen(filename, "rb");
if(fp == NULL) return 1;
m.Clear();
m.vn=0;
m.fn=0;
if (!readPTX( m, fp, mask,cb))
{
m.Clear();
return 1;
}
int endfile,end = 0;
fscanf(fp,"%i%i",&endfile,&end);
if(end != 0)
return 2;
return 0;
}
}; // end class
} // end Namespace tri
} // end Namespace io
} // end Namespace vcg
#endif

View File

@ -1,24 +1,129 @@
/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/ #ifndef __VCGLIB_IMPORTERSMF
#define __VCGLIB_IMPORTERSMF #include <map>
****************************************************************************/
#ifndef __VCGLIB_IMPORTER_SMF
#define __VCGLIB_IMPORTER_SMF
#include <stdio.h>
#include <vcg/space/point3.h>
namespace vcg {
namespace tri {
namespace io {
template<class OpenMeshType>
class ImporterSMF
{
public:
typedef typename OpenMeshType::VertexPointer VertexPointer;
typedef typename OpenMeshType::ScalarType ScalarType;
typedef typename OpenMeshType::VertexType VertexType;
typedef typename OpenMeshType::FaceType FaceType;
typedef typename OpenMeshType::VertexIterator VertexIterator;
typedef typename OpenMeshType::FaceIterator FaceIterator;
template <class MESHTYPE>
int Load_Smf( MESHTYPE & m, const char * filename ) { typedef typename MESHTYPE::VertexPointer VertexPointer;
typedef typename MESHTYPE::VertexType VertexType;
typedef typename MESHTYPE::FaceType FaceType;
typedef typename MESHTYPE::VertexIterator VertexIterator;
typedef typename MESHTYPE::FaceIterator FaceIterator;
// if it is binary there are 80 char of comment, the number fn of faces and then exactly fn*4*3 bytes.
enum {SMF_LABEL_SIZE=80};
class SMFFacet
{
public:
vcg::Point3f n;
vcg::Point3f v[3];
};
enum SMFError {
E_NOERROR, // 0
// Errori di open
E_CANTOPEN, // 1
E_UNESPECTEDEOF // 2
};
static const char *ErrorMsg(int error)
{
static const char * smf_error_msg[] =
{
"No errors",
"Can't open file",
"Premature End of file",
};
if(error>2 || error<0) return "Unknown error";
else return smf_error_msg[error];
};
static int Open(OpenMeshType &m, const char * filename )
{
VertexType v;
FaceType f;
FILE *fp;
float x,y,z;
bool one = true;
std::map<int,VertexPointer> mv;
fp = fopen(filename,"r");
if(!fp) return -1;
char buf[1024];
FILE *fp; float x,y,z; bool one = true; std::map<int,VertexPointer> mv; fp = fopen(filename,"r"); if(!fp) return -1; VertexType v; v.Supervisor_Flags() = 0; FaceType f; f.Supervisor_Flags() = 0;
while( fgets(buf,1024,fp) ) { char *vf, *comm_pt; if((comm_pt = strstr(buf,"#")) != NULL) *comm_pt = '\0'; if( (vf = strstr(buf,"v")) != NULL ) { sscanf(vf+1,"%f %f %f", &x, &y, &z); v.P()[0] = x; v.P()[1] = y; v.P()[2] = z; m.vert.push_back(v); } else if( (vf = strstr(buf,"f")) != NULL) { if(one) { VertexIterator vi; int ind; for(ind=1,vi=m.vert.begin(); vi!=m.vert.end(); ++vi,++ind) mv[ind]=&*vi; one = false; } int v1,v2,v3; sscanf(vf+1,"%d %d %d", &v1, &v2, &v3); f.V(0) = mv[v1]; f.V(1) = mv[v2]; f.V(2) = mv[v3]; m.face.push_back(f); } } m.vn = m.vert.size(); m.fn = m.face.size(); return 0; } };// end of io };// end of tri };// end of vcg #endif
while( fgets(buf,1024,fp) )
{
char *vf, *comm_pt;
if((comm_pt = strstr(buf,"#")) != NULL)
*comm_pt = '\0';
if( (vf = strstr(buf,"v")) != NULL )
{
sscanf(vf+1,"%f %f %f", &x, &y, &z);
v.P()[0] = x;
v.P()[1] = y;
v.P()[2] = z;
m.vert.push_back(v);
}
else if( (vf = strstr(buf,"f")) != NULL)
{
if(one)
{
VertexIterator vi;
int ind;
for(ind=1,vi=m.vert.begin(); vi!=m.vert.end(); ++vi,++ind)
mv[ind]=&*vi;
one = false;
}
int v1,v2,v3;
sscanf(vf+1,"%d %d %d", &v1, &v2, &v3);
f.V(0) = mv[v1];
f.V(1) = mv[v2];
f.V(2) = mv[v3];
m.face.push_back(f);
}
}
m.vn = m.vert.size();
m.fn = m.face.size();
return 0;
}
};
}// end of io
}// end of tri
}// end of vcg
#endif