From 518c8ad3a520cfaec6b24f3f224e661e5ce39005 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Wed, 30 Nov 2005 00:44:07 +0000 Subject: [PATCH] added: 1. save TCoord2 with struct map 2. callback 3. define static member to the access class Exporter --- src/test/io/export_obj.h | 273 ++++++++++++++++++++++++++++++--------- src/test/io/io.cpp | 43 ++++-- src/test/io/io.pro | 6 +- 3 files changed, 245 insertions(+), 77 deletions(-) diff --git a/src/test/io/export_obj.h b/src/test/io/export_obj.h index ed18cd5f4..f8013331f 100644 --- a/src/test/io/export_obj.h +++ b/src/test/io/export_obj.h @@ -25,6 +25,12 @@ History $Log$ + Revision 1.5 2005/11/30 00:44:07 fmazzant + added: + 1. save TCoord2 with struct map + 2. callback + 3. define static member to the access class Exporter + Revision 1.4 2005/11/10 00:06:43 fmazzant Added comment code @@ -42,82 +48,225 @@ Added implementation of OBJ file exporter. #ifndef __VCGLIB_EXPORT_OBJ #define __VCGLIB_EXPORT_OBJ +#include +#include +#include +#include "io_obj.h" #include #include +#include -namespace vcg -{ - namespace tri +namespace vcg { +namespace tri { +namespace io { + + template + class ExporterOBJ { - namespace io + public: + typedef typename SaveMeshType::FaceIterator FaceIterator; + typedef typename SaveMeshType::VertexIterator VertexIterator; + + /* + salva la Mesh in formato Obj File. + */ + static bool SaveASCII(SaveMeshType &m, const char * filename, ObjInfo &oi) { - template - class ExporterOBJ + CallBackPos *cb = oi.cb; + + //TODO: aggiungere la maschera per salvare il file(o meglio che cosa dobbiamo salvare sul file) + //TODO: avere la possibilita' di salvare il colore per vertice + //TODO: rendere effettivo il salvataggio delle coordinate di texture. + + std::ofstream stream(filename); + + if (stream.fail()) + return false; + + //vertici + VertexIterator vi; + if(true)//controllare maschera(mask) + { + int numvert = 0; + for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) + { + stream << "v " << (*vi).P()[0] << " " << (*vi).P()[1] << " " << (*vi).P()[2] << std::endl; + if (cb !=NULL) + (*cb)(100.0 * (float)++numvert/(float)m.vert.size(), "writing vertices "); + } + stream << "# " << m.vert.size() << " vertices" << std::endl;//stampa numero di vertici come commento + stream << std::endl; + } + + //texture coord + FaceIterator fi; + std::map,int> CoordIndexTexture; + int value = 1; + if(false)//controllare maschera(mask) { - public: - typedef typename SaveMeshType::FaceIterator FaceIterator; - typedef typename SaveMeshType::VertexIterator VertexIterator; - - static bool Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0) + int numface = 0; + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) { - //controlla se il numero dei vertici e quello delle facce sono diversi da zero. - if(m.vert.size() == 0|m.face.size() == 0)return false; - - if(binary) + for(unsigned int k=0;k<3;k++) { - return false; - } - else - { - std::ofstream stream(filename); - - if (stream.fail()) - return false; - - stream << "#GENERATED BY EXPORT_OBJ" << std::endl; - - //vertici - stream << std::endl << "#VERTEXS" << std::endl; - VertexIterator vi; - for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) - stream << "v " << (*vi).P()[0] << " " << (*vi).P()[1] << " " << (*vi).P()[2] << std::endl; - - //facce - stream << std::endl << "#FACES" << std::endl; - FaceIterator fi; - for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) + if(AddNewTextureCoord(CoordIndexTexture,(*fi).WT(k),value)) { - stream << "f "; - int MAX = 3; - for(unsigned int k=0;kP()); - if(k!=MAX-1)stream << " "; - } - stream << std::endl; + stream << "vt " << (*fi).WT(k).u() << " " << (*fi).WT(k).v() << std::endl; + value++;//incrementa il numero di valore da associare alle texture } - - stream.close(); - return true; } - } - /* - restituisce l'indice del vertice, aggiunto di una unita'. - */ - static int GetIndexVertex(SaveMeshType &m,Point3f p) + if (cb !=NULL) + (*cb)(100.0 * (float)++numface/(float)m.face.size(), "writing TCoord2"); + }//for + stream << "# " << CoordIndexTexture.size() << " vertices texture" << std::endl;//stampa numero di vert di coord di text + stream << std::endl; + } + + //facce + if(true)//controllare maschera(mask) + { + int numface = 0; + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) { - static const unsigned int ADDTORETURN = 1; - for(unsigned int i=0;iP());//considera i vertici per faccia + + int vt = -1; + //vt = GetIndexVertexTexture(CoordIndexTexture,(*fi).WT(k));//considera le texture per faccia - }; // end class - } // end Namespace tri - } // end Namespace io + int vn = -1; + vn = GetIndexVertexNormal();//considera le normali per faccia per ora non va considerato. + + //scrive elementi sul file obj + WriteFacesElement(stream,v,vt,vn); + + if(k!=MAX-1) + stream << " "; + else + stream << std::endl; + } + if (cb !=NULL) + (*cb)(100.0 * (float)++numface/(float)m.face.size(), "writing faces "); + }//for + stream << "# " << m.face.size() << " faces" << std::endl;//stampa numero di facce come commento + stream << std::endl; + } + + stream.close(); + return true; + } + + static bool SaveBinary(SaveMeshType &m, const char * filename, ObjInfo &oi) + { + return Save(m,filename,oi); + } + + static bool Save(SaveMeshType &m, const char * filename, ObjInfo &oi) + { + return Save(m,filename,false,oi); + } + static bool Save(SaveMeshType &m, const char * filename, bool binary, ObjInfo &oi) + { + if(binary) + return SaveBinary(m,filename,oi); + else + return SaveASCII(m,filename,oi); + } + + static bool Save(SaveMeshType &m, const char * filename, bool binary, CallBackPos *cb=0) + { + ObjInfo oi; + oi.cb=cb; + + //TODO: la maschera prende qualsiasi cosa....cioe' deve salvare tutto + + if(binary) + return SaveBinary(m,filename,oi); + else + return SaveASCII(m,filename,oi); + } + + static bool Save(SaveMeshType &m, const char * filename, bool binary, int &mask, CallBackPos *cb=0) + { + ObjInfo oi; + oi.cb=cb; + oi.mask=mask; + if(binary) + return SaveBinary(m,filename,oi); + else + return SaveASCII(m,filename,oi); + } + + + + /* + restituisce l'indice del vertice, aggiunto di una unita'. + */ + inline static int GetIndexVertex(SaveMeshType &m,Point3f p) + { + for(unsigned int i=0;i,int> &m, const vcg::TCoord2 &wt) + { + int index = m[wt]; + if(index!=0){return index;} + return -1; + } + + + /* + restituisce l'indice della normale. + */ + inline static int GetIndexVertexNormal() + { + return -1; + } + + + /* + scrive gli elementi su file. + */ + inline static void WriteFacesElement(std::ofstream &stream,int v,int vt, int vn) + { + stream << v; + if(vt!=-1) + { + stream << "/" << vt; + if(vn!=-1) stream << "/" << vn; + } + + if(vn!=-1) + stream << "//" << vn; + } + + /* + aggiunge un nuovo indice alla coordinata di texture se essa e' la prima + volta che viene incontrata altrimenti non esegue niente. + */ + inline static bool AddNewTextureCoord(std::map,int> &m, const vcg::TCoord2 &wt,int value) + { + int index = m[wt]; + if(index == 0){m[wt]=value;return true;} + return false; + } + + + + }; // end class + +} // end Namespace tri +} // end Namespace io } // end Namespace vcg #endif diff --git a/src/test/io/io.cpp b/src/test/io/io.cpp index b1069ccaf..e24edfd44 100644 --- a/src/test/io/io.cpp +++ b/src/test/io/io.cpp @@ -25,6 +25,12 @@ History $Log$ + Revision 1.4 2005/11/30 00:44:07 fmazzant + added: + 1. save TCoord2 with struct map + 2. callback + 3. define static member to the access class Exporter + Revision 1.3 2005/11/10 00:15:15 fmazzant bug-fix History @@ -54,23 +60,36 @@ struct MyVertex: public VertexVN{}; struct MyFace: public FaceAF{}; struct MyMesh: public vcg::tri::TriMesh< vector, vector >{}; +bool callback(const int pos, const char * str) +{ + if(pos<10) + printf("\r%s [ %d%%] ", str, pos); + else + if(pos!=100) + printf("\r%s [ %d%%] ", str, pos); + else + printf("\r%s [ OK ] \n", str, pos); + return true; +} + int main(int argc, char **argv) { - if(argc < 3){return 0;} + if(argc < 3) + { + std::cout << "Using: " << std::endl << "[prompt]-> io_debug infile.obj outfile.obj" << std::endl; + return-1; + } MyMesh m; - //open file OBJ - vcg::tri::io::ImporterOBJ::OpenAscii(m,argv[1],0); + //open file OBJ + vcg::tri::io::ObjInfo oi; + oi.cb=&callback; + vcg::tri::io::ImporterOBJ::Open(m,argv[1],oi); //write copy file OBJ - bool result = vcg::tri::io::ExporterOBJ::Save(m,argv[2],false,0); - - //print result - if(result) - std::cout << "file is copied!" << std::endl; - else - std::cout << " file is not copied!" << std::endl; - - return 0; + bool result = false; + result = vcg::tri::io::ExporterOBJ::Save(m,argv[2],false,oi); + + return 0; } diff --git a/src/test/io/io.pro b/src/test/io/io.pro index 226e8ef7c..63d57edbd 100644 --- a/src/test/io/io.pro +++ b/src/test/io/io.pro @@ -3,13 +3,13 @@ ###################################################################### -TEMPLATE = lib +TEMPLATE = app CONFIG += plugin INCLUDEPATH += . ../../../../sf HEADERS += export_obj.h import_obj.h SOURCES += io.cpp -TARGET = io -DESTDIR = ../../meshlab/plugins +TARGET = io_debug +DESTDIR = . contains(TEMPLATE,lib) { CONFIG(debug, debug|release) {