mirror of
https://github.com/lucaspalomodevelop/meshlab.git
synced 2026-03-19 19:14:42 +00:00
added:
1. save TCoord2 with struct map 2. callback 3. define static member to the access class Exporter
This commit is contained in:
parent
519311423e
commit
518c8ad3a5
@ -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 <wrap/callback.h>
|
||||
#include <vcg/complex/trimesh/allocate.h>
|
||||
#include <wrap/ply/io_mask.h>
|
||||
#include "io_obj.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
|
||||
namespace vcg
|
||||
{
|
||||
namespace tri
|
||||
namespace vcg {
|
||||
namespace tri {
|
||||
namespace io {
|
||||
|
||||
template <class SaveMeshType>
|
||||
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 SaveMeshType>
|
||||
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<vcg::TCoord2<float>,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;k<MAX;k++)
|
||||
{
|
||||
stream << GetIndexVertex(m,(*fi).V(k)->P());
|
||||
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;i<m.vert.size();i++)
|
||||
stream << "f ";
|
||||
unsigned int MAX = 3;
|
||||
for(unsigned int k=0;k<MAX;k++)
|
||||
{
|
||||
if(m.vert[i].P() == p){return i+ADDTORETURN;}
|
||||
}
|
||||
std::cout << "" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
int v = -1;
|
||||
v = GetIndexVertex(m, (*fi).V(k)->P());//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<m.vert.size();i++)
|
||||
if(m.vert[i].P() == p)
|
||||
return ++i;
|
||||
return-1;
|
||||
}
|
||||
|
||||
/*
|
||||
restituisce l'indice della coordinata di texture.
|
||||
*/
|
||||
inline static int GetIndexVertexTexture(std::map<vcg::TCoord2<float>,int> &m, const vcg::TCoord2<float> &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<vcg::TCoord2<float>,int> &m, const vcg::TCoord2<float> &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
|
||||
|
||||
@ -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<float,MyEdge,MyFace>{};
|
||||
struct MyFace: public FaceAF<MyVertex,MyEdge,MyFace>{};
|
||||
struct MyMesh: public vcg::tri::TriMesh< vector<MyVertex>, vector<MyFace> >{};
|
||||
|
||||
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<MyMesh>::OpenAscii(m,argv[1],0);
|
||||
//open file OBJ
|
||||
vcg::tri::io::ObjInfo oi;
|
||||
oi.cb=&callback;
|
||||
vcg::tri::io::ImporterOBJ<MyMesh>::Open(m,argv[1],oi);
|
||||
|
||||
//write copy file OBJ
|
||||
bool result = vcg::tri::io::ExporterOBJ<MyMesh>::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<MyMesh>::Save(m,argv[2],false,oi);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user