diff --git a/wrap/io_trimesh/export_obj.h b/wrap/io_trimesh/export_obj.h index 488bbac5..e366d40e 100644 --- a/wrap/io_trimesh/export_obj.h +++ b/wrap/io_trimesh/export_obj.h @@ -25,6 +25,9 @@ History $Log: not supported by cvs2svn $ + Revision 1.10 2007/10/17 09:49:50 cignoni + correct management of point only files + Revision 1.9 2007/03/12 16:40:16 tarini Texture coord name change! "TCoord" and "Texture" are BAD. "TexCoord" is GOOD. @@ -176,16 +179,16 @@ namespace io { /* function which saves in OBJ file format */ - static int SaveASCII(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0) + static int Save(SaveMeshType &m, const char * filename, int mask, CallBackPos *cb=0) { if(m.vn == 0) return E_NOTVEXTEXVALID; // Commented out this control. You should be allowed to save a point cloud. // if(m.fn == 0) return E_NOTFACESVALID; int current = 0; - int max = m.vn+ m.fn; + int totalPrimitives = m.vn+m.fn; - std::vector materials; + std::vector materialVec; std::string fn(filename); int LastSlash=fn.size()-1; @@ -194,13 +197,13 @@ namespace io { FILE *fp; fp = fopen(filename,"w"); - if(fp == NULL)return E_CANTOPENFILE; + if(fp == NULL) return E_CANTOPENFILE; fprintf(fp,"####\n#\n# OBJ File Generated by Meshlab\n#\n####\n"); fprintf(fp,"# Object %s\n#\n# Vertices: %d\n# Faces: %d\n#\n####\n",fn.substr(LastSlash+1).c_str(),m.vn,m.fn); - //library materials - if(mask & vcg::tri::io::Mask::IOM_FACECOLOR) + //library materialVec + if( (mask & vcg::tri::io::Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) ) fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(LastSlash+1).c_str()); //vertexs + normal @@ -208,17 +211,17 @@ namespace io { std::map NormalVertex; std::vector VertexId(m.vert.size()); int numvert = 0; - int value = 1; + int curNormalIndex = 1; for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() ) { VertexId[vi-m.vert.begin()]=numvert; //saves normal per vertex - if(mask & vcg::tri::io::Mask::IOM_VERTNORMAL | mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) + if((mask & Mask::IOM_VERTNORMAL) || (mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD) ) { - if(AddNewNormalVertex(NormalVertex,(*vi).N(),value)) + if(AddNewNormalVertex(NormalVertex,(*vi).N(),curNormalIndex)) { fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]); - value++; + curNormalIndex++; } } @@ -226,7 +229,7 @@ namespace io { fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); if (cb !=NULL) { - if(!(*cb)((100*++current)/max, "writing vertices ")) + if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) { fclose(fp); return E_ABORTED;} } numvert++; } @@ -239,68 +242,64 @@ namespace io { std::map,int> CoordIndexTexture; unsigned int material_num = 0; int mem_index = 0; //var temporany - /*int*/ value = 1;//tmp + int curTexCoordIndex = 1; for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) { - if(mask & vcg::tri::io::Mask::IOM_FACECOLOR) + if((mask & Mask::IOM_FACECOLOR) || (mask & Mask::IOM_WEDGTEXCOORD) ) { - int index = vcg::tri::io::Materials::CreateNewMaterial(m,materials,material_num,fi); + int index = Materials::CreateNewMaterial(m,materialVec,material_num,fi); - if(index == materials.size())//inserts a new element material + if(index == materialVec.size())//inserts a new element material { material_num++; - fprintf(fp,"\nusemtl material_%d\n",materials[index-1].index); + fprintf(fp,"\nusemtl material_%d\n",materialVec[index-1].index); mem_index = index-1; } else { if(index != mem_index)//inserts old name elemente material { - fprintf(fp,"\nusemtl material_%d\n",materials[index].index); + fprintf(fp,"\nusemtl material_%d\n",materialVec[index].index); mem_index=index; } } } //saves texture coord - unsigned int MAX = 3; - for(unsigned int k=0;kcN());//index of vertex normal per face. //writes elements on file obj - WriteFacesElement(fp,v,vt,vn); - - if(k!=MAX-1) - fprintf(fp," "); - else - fprintf(fp,"\n"); + WriteFacesElement(fp,vInd,vt,vn); } + fprintf(fp,"\n"); if (cb !=NULL) { - if(!(*cb)((100*++current)/max, "writing vertices ")) + if(!(*cb)((100*++current)/totalPrimitives, "writing vertices ")) { fclose(fp); return E_ABORTED;} } @@ -311,30 +310,14 @@ namespace io { fclose(fp); int r = 0; - if(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD | mask & vcg::tri::io::Mask::IOM_FACECOLOR) - r = WriteMaterials(materials, filename,cb);//write material + if((mask & Mask::IOM_WEDGTEXCOORD) || (mask & Mask::IOM_FACECOLOR) ) + r = WriteMaterials(materialVec, filename,cb);//write material if(r!= E_NOERROR) return r; return E_NOERROR; } - /* - function which saves in OBJ file format - */ - static int SaveBinary(SaveMeshType &m, const char * filename) - { - return E_NOTDEFINITION; - } - - /* - function which saves in OBJ file format - */ - static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0) - { - return SaveASCII(m,filename,mask,cb); - } - /* returns index of the vertex */ @@ -346,25 +329,37 @@ namespace io { /* returns index of the texture coord */ - inline static int GetIndexVertexTexture(std::map,int> &m, const vcg::TexCoord2 &wt) + inline static int GetIndexVertexTexture(std::map,int> &mapTexToInt, const vcg::TexCoord2 &wt) { - int index = m[wt]; - if(index!=0){return index;} - return -1; + std::map,int>::iterator iter= mapTexToInt.find(wt); + if(iter != mapTexToInt.end()) return (*iter).second; + else return -1; + // Old wrong version. + // int index = mapTexToInt[wt]; + // if(index!=0){return index;} } /* returns index of the vertex normal */ - inline static int GetIndexVertexNormal(SaveMeshType &m, std::map &ma, unsigned int iv ) + inline static int GetIndexVertexNormal(SaveMeshType &m, std::map &mapNormToInt, const Point3f &norm ) { - int index = ma[m.vert[iv].N()]; - if(index!=0){return index;} - return -1; + std::map::iterator iter= mapNormToInt.find(norm); + if(iter != mapNormToInt.end()) return (*iter).second; + else return -1; + // Old wrong version. + // int index = mapNormToInt[m.vert[iv].N()]; + // if(index!=0){return index;} } /* write elements on file + + f v/tc/vn v/tc/vn v/tc/vn ... + f v/tc v/tc v/tc ... + f v//vn v//vn v//vn ... + f v v v ... + */ inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn) { @@ -404,12 +399,12 @@ namespace io { /* writes material into file */ - inline static int WriteMaterials(std::vector &materials, const char * filename, CallBackPos *cb=0) + inline static int WriteMaterials(std::vector &materialVec, const char * filename, CallBackPos *cb=0) { std::string fileName = std::string(filename); fileName+=".mtl"; - if(materials.size() > 0) + if(materialVec.size() > 0) { FILE *fp; fp = fopen(fileName.c_str(),"w"); @@ -419,23 +414,23 @@ namespace io { int current = 0; - for(unsigned int i=0;i0) - fprintf(fp,"map_Kd %s\n",materials[i].map_Kd.c_str()); + if(materialVec[i].map_Kd.size()>0) + fprintf(fp,"map_Kd %s\n",materialVec[i].map_Kd.c_str()); fprintf(fp,"\n"); } fclose(fp); diff --git a/wrap/io_trimesh/io_material.h b/wrap/io_trimesh/io_material.h index 5c3b15ab..f7dcb66f 100644 --- a/wrap/io_trimesh/io_material.h +++ b/wrap/io_trimesh/io_material.h @@ -25,6 +25,9 @@ History $Log: not supported by cvs2svn $ + Revision 1.4 2007/06/20 10:28:04 tarini + "newline at end of file" and "endif" warnings fixed + Revision 1.3 2006/11/09 07:51:44 cignoni bug due to wrong access to eventually unexistent FaceColor @@ -126,14 +129,14 @@ namespace io { { for(unsigned int i=0;i