From f125ebd34fda78cfe59d4f8e82d5d110591b8d27 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Sun, 29 Jan 2006 16:33:03 +0000 Subject: [PATCH] moved export_obj and export_3ds from test/io into meshio/ --- src/meshlabplugins/meshio/export_3ds.h | 414 +++++++++++++++++++++ src/meshlabplugins/meshio/export_obj.h | 489 +++++++++++++++++++++++++ src/meshlabplugins/meshio/meshio.cpp | 7 +- src/meshlabplugins/meshio/meshio.pro | 4 +- 4 files changed, 910 insertions(+), 4 deletions(-) create mode 100644 src/meshlabplugins/meshio/export_3ds.h create mode 100644 src/meshlabplugins/meshio/export_obj.h diff --git a/src/meshlabplugins/meshio/export_3ds.h b/src/meshlabplugins/meshio/export_3ds.h new file mode 100644 index 000000000..e87381c73 --- /dev/null +++ b/src/meshlabplugins/meshio/export_3ds.h @@ -0,0 +1,414 @@ +/**************************************************************************** +* 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$ + Revision 1.1 2006/01/29 16:33:03 fmazzant + moved export_obj and export_3ds from test/io into meshio/ + + Revision 1.30 2006/01/26 15:57:20 fmazzant + deleted a small bug + + Revision 1.29 2006/01/26 15:48:49 fmazzant + added control on the maximum number of vertices allowed by the 3DS file format + + Revision 1.28 2006/01/23 14:07:39 fmazzant + deleted bug when saving a face color. + + Revision 1.27 2006/01/22 23:59:01 fmazzant + changed default value of diffuse. 1.0 -> 0.8 + + Revision 1.26 2006/01/22 10:42:18 fmazzant + cleaned code & optimized code of material's name 3ds + + Revision 1.25 2006/01/22 01:26:52 fmazzant + deleted bug on saving name material 3ds + + Revision 1.24 2006/01/21 15:19:51 fmazzant + changed: + inserting coord texture in to map from key = index, value=coord + to key=coord, value=index_of_vertix. + + Revision 1.23 2006/01/20 14:15:52 fmazzant + added texture filename on material 3ds and coordtexture on face + + Revision 1.22 2006/01/19 09:36:29 fmazzant + cleaned up history log + + Revision 1.21 2006/01/18 16:14:52 fmazzant + update small error + + Revision 1.20 2006/01/18 16:12:58 fmazzant + updated material + + Revision 1.19 2006/01/18 15:09:45 fmazzant + added texture base + + Revision 1.18 2006/01/18 14:57:26 fmazzant + added Lib3dsNode in export_3ds + +****************************************************************************/ + +#ifndef __VCGLIB_EXPORT_3DS +#define __VCGLIB_EXPORT_3DS + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#define MAX_POLYGONS 65535 + +namespace vcg { +namespace tri { +namespace io { + + + template + class Exporter3DS + { + public: + typedef typename SaveMeshType::FaceIterator FaceIterator; + typedef typename SaveMeshType::VertexIterator VertexIterator; + typedef typename SaveMeshType::VertexType VertexType; + + enum SaveError + { + E_NOERROR, // 0 + E_CANTOPENFILE, // 1 + E_CANTCLOSEFILE, // 2 + E_UNESPECTEDEOF, // 3 + E_ABORTED, // 4 + E_NOTDEFINITION, // 5 + E_NOTVEXTEXVALID, // 6 + E_NOTFACESVALID, // 7 + E_NOTEXCOORDVALID, // 8 + E_NOTNUMBERVERTVALID // 9 + }; + + /* + stampa messaggio di errore dell'export obj + */ + static const char* ErrorMsg(int error) + { + static const char* obj_error_msg[] = + { + "No errors", // 0 + "Can't open file", // 1 + "can't close file", // 2 + "Premature End of file", // 3 + "File saving aborted", // 4 + "Function not defined", // 5 + "Vertices not valid", // 6 + "Faces not valid", // 7 + "Texture Coord not valid", // 8 + "You cannot save more than 65535 vertices for the 3DS format" // 9 + }; + + if(error>9 || error<0) return "Unknown error"; + else return obj_error_msg[error]; + }; + + /* + restituisce quali informazioni sono possibili salvare in base alla maschera + */ + static int GetExportMaskCapability() + { + int capability = 0; + + //camera + //capability |= MeshModel::IOM_CAMERA; + + //vert + capability |= MeshModel::IOM_VERTQUALITY; + //capability |= MeshModel::IOM_VERTTEXCOORD; + + //face + capability |= MeshModel::IOM_FACEFLAGS; + capability |= MeshModel::IOM_FACECOLOR; + capability |= MeshModel::IOM_FACEQUALITY; + capability |= MeshModel::IOM_FACENORMAL; + + //wedg + capability |= MeshModel::IOM_WEDGTEXCOORD; + + return capability; + } + + static int SaveASCII(SaveMeshType &m, const char * filename) + { + return E_NOTDEFINITION; + } + + static int SaveBinary(SaveMeshType &m, const char * filename, int &mask, CallBackPos *cb=0) + { + if(m.vert.size() > MAX_POLYGONS) + return E_NOTNUMBERVERTVALID; + + if(m.vert.size() == 0) + return E_NOTVEXTEXVALID; + if(m.face.size() == 0) + return E_NOTFACESVALID; + + Lib3dsFile *file = lib3ds_file_new();//crea un nuovo file + Lib3dsMesh *mesh = lib3ds_mesh_new("mesh");//crea una nuova mesh con nome mesh + + QString qnamematerial = "Material - %1"; + std::vector materials; + + int current = 0; + int max = m.vert.size()+m.face.size(); + + lib3ds_mesh_new_point_list(mesh, m.vert.size());//definisce il numero di vertici + + if(m.HasPerWedgeTexture() && mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD ) + lib3ds_mesh_new_texel_list(mesh,m.vert.size()); + + int v_index = 0; + VertexIterator vi; + if(mask & MeshModel::IOM_VERTQUALITY) + { + for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() ) + { + Lib3dsPoint point; + point.pos[0] = (*vi).P()[0]; + point.pos[1] = (*vi).P()[1]; + point.pos[2] = (*vi).P()[2]; + + mesh->pointL[v_index] = point; + + if (cb !=NULL) + (*cb)(100.0 * (float)++current/(float)max, "writing vertices "); + else + return E_ABORTED; + v_index++; + } + } + lib3ds_mesh_new_face_list (mesh, m.face.size()); + int f_index = 0;//indice facce + int t_index = 0;//indice texture + FaceIterator fi; + if(mask & MeshModel::IOM_FACEQUALITY) + { + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) + { + Lib3dsFace face; + face.points[0] = GetIndexVertex(m, (*fi).V(0)); + face.points[1] = GetIndexVertex(m, (*fi).V(1)); + face.points[2] = GetIndexVertex(m, (*fi).V(2)); + + if(m.HasPerWedgeTexture() && mask & MeshModel::IOM_WEDGTEXCOORD ) + { + unsigned int MAX = 3; + for(unsigned int k=0;ktexelL[face.points[k]][0] = (*fi).WT(k).u(); + mesh->texelL[face.points[k]][1] = (*fi).WT(k).v(); + } + } + + if(mask & MeshModel::IOM_FACEFLAGS) + face.flags = 0; + + face.smoothing = 10;//da modificare. + if(mask & MeshModel::IOM_FACENORMAL) + { + face.normal[0] = (*fi).N()[0]; + face.normal[1] = (*fi).N()[1]; + face.normal[2] = (*fi).N()[2]; + } + + int material_index = CreateNewMaterial(m, materials, 0, fi); + if(material_index == materials.size()) + { + Lib3dsMaterial *material = lib3ds_material_new();//cre un nuovo materiale + + std::string name = qnamematerial.arg(material_index-1).toStdString(); + strcpy(material->name,name.c_str()); + + if(mask & MeshModel::IOM_FACECOLOR) + { + //ambient + material->ambient[0] = materials[materials.size()-1].Ka[0]; + material->ambient[1] = materials[materials.size()-1].Ka[1]; + material->ambient[2] = materials[materials.size()-1].Ka[2]; + material->ambient[3] = materials[materials.size()-1].Tr; + + //diffuse + material->diffuse[0] = materials[materials.size()-1].Kd[0]; + material->diffuse[1] = materials[materials.size()-1].Kd[1]; + material->diffuse[2] = materials[materials.size()-1].Kd[2]; + material->diffuse[3] = materials[materials.size()-1].Tr; + + //specular + material->specular[0] = materials[materials.size()-1].Ks[0]; + material->specular[1] = materials[materials.size()-1].Ks[1]; + material->specular[2] = materials[materials.size()-1].Ks[2]; + material->specular[3] = materials[materials.size()-1].Tr; + + //shininess + material->shininess = materials[materials.size()-1].Ns; + } + + //texture + if(mask & MeshModel::IOM_WEDGTEXCOORD) + strcpy(material->texture1_map.name,materials[materials.size()-1].map_Kd.c_str()); + + lib3ds_file_insert_material(file,material);//inserisce il materiale nella mesh + strcpy(face.material,name.c_str()); + } + else + { + std::string name = qnamematerial.arg(material_index).toStdString(); + strcpy(face.material,name.c_str()); + } + + + mesh->faceL[f_index]=face; + + if (cb !=NULL) + (*cb)(100.0 * (float)++current/(float)max, "writing faces "); + else + return E_ABORTED; + f_index++; + + } + } + + lib3ds_file_insert_mesh(file, mesh);//inserisce la mesh al file + + Lib3dsNode *node = lib3ds_node_new_object();//crea un nuovo nodo + strcpy(node->name,mesh->name); + node->parent_id = LIB3DS_NO_PARENT; + lib3ds_file_insert_node(file,node); + + bool result = lib3ds_file_save(file, filename); //salva il file + if(result) + return E_NOERROR; + else + return E_ABORTED; + } + + static int Save(SaveMeshType &m, const char * filename, bool binary,int &mask, CallBackPos *cb=0) + { + return SaveBinary(m,filename,mask,cb); + } + + /* + restituisce l'indice del vertice. + */ + inline static int GetIndexVertex(SaveMeshType &m, VertexType *p) + { + return p-&*(m.vert.begin()); + } + + /* + crea un nuovo materiale + */ + inline static int CreateNewMaterial(SaveMeshType &m, std::vector &materials, unsigned int index, FaceIterator &fi) + { + unsigned char r = (*fi).C()[0]; + unsigned char g = (*fi).C()[1]; + unsigned char b = (*fi).C()[2]; + unsigned char alpha = (*fi).C()[3]; + + Point3f diffuse; + if(r > 1 || g > 1 || b > 1) + diffuse = Point3f((float)r/255.0,(float)g/255.0,(float)b/255.0); + else if (r != 0 || g != 0 || b != 0) + diffuse = Point3f((float)r,(float)g,(float)b); + else + diffuse = Point3f(0.8,0.8,0.80); + + float Tr; + if(alpha > 1) + Tr = (float)alpha/255.0; + else + Tr = (float)alpha; + + int illum = 2; //default not use Ks! + float ns = 0.0; //default + + Material mtl; + + mtl.index = index;//index of materials + mtl.Ka = Point3f(0.2,0.2,0.2);//ambient + mtl.Kd = diffuse;//diffuse + mtl.Ks = Point3f(1.0,1.0,1.0);//specular + mtl.Tr = Tr;//alpha + mtl.Ns = ns; + mtl.illum = illum;//illumination + + if(m.textures.size() && (*fi).WT(0).n() >=0 ) + mtl.map_Kd = m.textures[(*fi).WT(0).n()]; + else + mtl.map_Kd = ""; + + int i = -1; + if((i = MaterialsCompare(materials,mtl)) == -1) + { + materials.push_back(mtl); + index++; + return materials.size(); + } + return i; + } + /* + compara il materiale. + */ + inline static int MaterialsCompare(std::vector &materials, Material mtl) + { + for(int i=0;i 0.8 + + Revision 1.33 2006/01/19 09:36:29 fmazzant + cleaned up history log + + Revision 1.32 2006/01/18 00:45:56 fmazzant + added control on face's diffuse + + Revision 1.31 2006/01/17 13:48:54 fmazzant + added capability mask on export file format + + Revision 1.30 2006/01/15 00:45:40 fmazzant + extend mask exporter for all type file format + + + Revision 1.29 2006/01/14 00:03:26 fmazzant + added more controls + +****************************************************************************/ + +#ifndef __VCGLIB_EXPORT_OBJ +#define __VCGLIB_EXPORT_OBJ + +#include +#include +#include +#include +#include +#include +#include + +namespace vcg { +namespace tri { +namespace io { + + struct Material + { + unsigned int index; + + Point3f Ka;//ambient + Point3f Kd;//diffuse + Point3f Ks;//specular + + float d;//alpha + float Tr;//alpha + + int illum;//specular illumination + float Ns; + + std::string map_Kd; //filename texture + }; + + template + class ExporterOBJ + { + public: + typedef typename SaveMeshType::FaceIterator FaceIterator; + typedef typename SaveMeshType::VertexIterator VertexIterator; + typedef typename SaveMeshType::VertexType VertexType; + + enum SaveError + { + E_NOERROR, // 0 + E_CANTOPENFILE, // 1 + E_CANTCLOSEFILE, // 2 + E_UNESPECTEDEOF, // 3 + E_ABORTED, // 4 + E_NOTDEFINITION, // 5 + E_NOTVEXTEXVALID, // 6 + E_NOTFACESVALID // 7 + }; + + /* + stampa messaggio di errore dell'export obj + */ + static const char* ErrorMsg(int error) + { + static const char* obj_error_msg[] = + { + "No errors", // 0 + "Can't open file", // 1 + "can't close file", // 2 + "Premature End of file", // 3 + "File saving aborted", // 4 + "Function not defined", // 5 + "Vertices not valid", // 6 + "Faces not valid" // 7 + }; + + if(error>7 || error<0) return "Unknown error"; + else return obj_error_msg[error]; + }; + + /* + restituisce quali informazioni sono possibili salvare in base alla maschera + */ + static int GetExportMaskCapability() + { + int capability = 0; + + //vert + capability |= vcg::tri::io::Mask::IOM_VERTQUALITY; + capability |= vcg::tri::io::Mask::IOM_VERTNORMAL; + + //face + capability |= vcg::tri::io::Mask::IOM_FACECOLOR; + capability |= vcg::tri::io::Mask::IOM_FACEQUALITY; + capability |= vcg::tri::io::Mask::IOM_FACECOLOR; + + //wedg + capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD; + + return capability; + } + + /* + salva la Mesh in formato Obj File. + */ + static int SaveASCII(SaveMeshType &m, const char * filename, ObjInfo &oi) + { + CallBackPos *cb = oi.cb; + + if(m.vert.size() == 0) + return E_NOTVEXTEXVALID; + if(m.face.size() == 0) + return E_NOTFACESVALID; + + int current = 0; + int max = m.vert.size()+ m.face.size(); + + std::vector materials;//vettore dei materiali. + + std::string fn(filename); + int i=fn.size()-1; + while(fn[--i]!='/'); + + FILE *fp; + fp = fopen(filename,"w"); + 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(i+1).c_str(),m.vert.size(),m.face.size()); + + //library materials + if(oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) + fprintf(fp,"mtllib ./%s.mtl\n\n",fn.substr(i+1).c_str()); + + //vertexs + normal + VertexIterator vi; + std::map NormalVertex; + if(oi.mask & vcg::tri::io::Mask::IOM_VERTQUALITY) + { + int numvert = 0; + int value = 1; + for(vi=m.vert.begin(); vi!=m.vert.end(); ++vi) if( !(*vi).IsD() ) + { + //salva le normali per vertice + if(oi.mask & vcg::tri::io::Mask::IOM_VERTNORMAL) + { + if(AddNewNormalVertex(NormalVertex,(*vi).N(),value)) + { + fprintf(fp,"vn %f %f %f\n",(*vi).N()[0],(*vi).N()[1],(*vi).N()[2]); + value++; + } + } + + //salva il vertice + fprintf(fp,"v %f %f %f\n",(*vi).P()[0],(*vi).P()[1],(*vi).P()[2]); + + if (cb !=NULL) + (*cb)(100.0 * (float)++current/(float)max, "writing vertices "); + else + { fclose(fp); return E_ABORTED;} + } + fprintf(fp,"# %d vertices, %d vertices normals\n\n",m.vert.size(),NormalVertex.size()); + } + + //faces + texture coords + FaceIterator fi; + std::map,int> CoordIndexTexture; + if(oi.mask & vcg::tri::io::Mask::IOM_FACEQUALITY) + { + unsigned int material_num = 0; + int mem_index = 0; //var temporany + int value = 1;//tmp + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) + { + if(oi.mask & vcg::tri::io::Mask::IOM_FACECOLOR) + { + int index = CreateNewMaterial(m,materials,material_num,fi); + + if(index == materials.size())//inserted a new element material + { + material_num++; + fprintf(fp,"\nusemtl material_%d\n",materials[index-1].index); + mem_index = index-1; + } + else + { + if(index != mem_index) + { + fprintf(fp,"\nusemtl material_%d\n",materials[index].index); + mem_index=index; + } + } + } + + //salva le coordinate di texture + unsigned int MAX = 3; + for(unsigned int k=0;k,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(SaveMeshType &m, std::map &ma, unsigned int iv ) + { + int index = ma[m.vert[iv].N()]; + if(index!=0){return index;} + return -1; + } + + /* + scrive gli elementi su file. + */ + inline static void WriteFacesElement(FILE *fp,int v,int vt, int vn) + { + fprintf(fp,"%d",v); + if(vt!=-1) + { + fprintf(fp,"/%d",vt); + if(vn!=-1) + fprintf(fp,"/%d",vn); + } + else if(vn!=-1) + fprintf(fp,"//%d",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; + } + + /* + aggiunge una normal restituendo true nel caso dell'insirimento e false in caso contrario + */ + inline static bool AddNewNormalVertex(std::map &m, Point3f &n ,int value) + { + int index = m[n]; + if(index==0){m[n]=value;return true;} + return false; + } + + /* + gestione del file material. + */ + inline static int CreateNewMaterial(SaveMeshType &m, std::vector &materials, unsigned int index, FaceIterator &fi) + { + unsigned char r = (*fi).C()[0]; + unsigned char g = (*fi).C()[1]; + unsigned char b = (*fi).C()[2]; + unsigned char alpha = (*fi).C()[3]; + + Point3f diffuse; + if(r > 1 || g > 1 || b > 1) + diffuse = Point3f((float)r/255.0,(float)g/255.0,(float)b/255.0); + else if (r != 0 || g != 0 || b != 0) + diffuse = Point3f((float)r,(float)g,(float)b); + else + diffuse = Point3f(0.8,0.8,0.8); + + float Tr; + if(alpha > 1) + Tr = (float)alpha/255.0; + else + Tr = (float)alpha; + + int illum = 2; //default not use Ks! + float ns = 0.0; //default + + Material mtl; + + mtl.index = index;//index of materials + mtl.Ka = Point3f(0.2,0.2,0.2);//ambient + mtl.Kd = diffuse;//diffuse + mtl.Ks = Point3f(1.0,1.0,1.0);//specular + mtl.Tr = Tr;//alpha + mtl.Ns = ns; + mtl.illum = illum;//illumination + + if(m.textures.size() && (*fi).WT(0).n() >=0 ) { + + mtl.map_Kd = m.textures[(*fi).WT(0).n()]; + } + + else + mtl.map_Kd = ""; + + int i = -1; + if((i = MaterialsCompare(materials,mtl)) == -1) + { + materials.push_back(mtl); + return materials.size(); + } + return i; + } + + inline static int WriteMaterials(std::vector &materials, const char * filename, CallBackPos *cb=0) + { + std::string fileName = std::string(filename); + fileName+=".mtl"; + + if(materials.size() > 0) + { + FILE *fp; + fp = fopen(fileName.c_str(),"w"); + if(fp==NULL)return E_ABORTED; + + fprintf(fp,"#\n# Wavefront material file\n# Converted by Meshlab Group\n#\n\n"); + + int current = 0; + + for(unsigned int i=0;i0) + fprintf(fp,"map_Kd %s\n",materials[i].map_Kd.c_str()); + fprintf(fp,"\n"); + } + fclose(fp); + } + return E_NOERROR; + } + + inline static int MaterialsCompare(std::vector &materials, Material mtl) + { + for(int i=0;i #include "../../test/io/import_3ds.h" -#include "../../test/io/export_3ds.h" +#include "export_3ds.h" #include #include diff --git a/src/meshlabplugins/meshio/meshio.pro b/src/meshlabplugins/meshio/meshio.pro index f52b0c60f..20d2b5d94 100644 --- a/src/meshlabplugins/meshio/meshio.pro +++ b/src/meshlabplugins/meshio/meshio.pro @@ -1,8 +1,8 @@ TEMPLATE = lib CONFIG += plugin INCLUDEPATH += ../.. ../../../../sf ../../../../code/lib/glew/include ../../../../code/lib/lib3ds-1.2.0 -HEADERS = meshio.h ../../../../sf/wrap/ply/plylib.h -SOURCES = meshio.cpp ../../../../sf/wrap/ply/plylib.cpp +HEADERS = meshio.h ../../../../sf/wrap/ply/plylib.h export_obj.h export_3ds.h +SOURCES = meshio.cpp ../../../../sf/wrap/ply/plylib.cpp TARGET = meshio DESTDIR = ../../meshlab/plugins