Created the ExporterOOFF Class

created the method Save
still to work on the faces since we only write he vertices indexes for each face and no other information.
This commit is contained in:
Paolo Cignoni 2005-09-28 10:16:19 +00:00
parent 0fa8212a49
commit ca8ec26f06
1 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,170 @@
/****************************************************************************
* 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. *
* *
****************************************************************************/
/**
@name Load and Save in Ply format
*/
//@{
#ifndef __VCGLIB_EXPORT_OFF
#define __VCGLIB_EXPORT_OFF
#include<wrap/ply/io_mask.h>
#include <stdio.h>
namespace vcg {
namespace tri {
namespace io {
template <class SaveMeshType>
class ExporterOFF
{
public:
typedef typename SaveMeshType::VertexPointer VertexPointer;
typedef typename SaveMeshType::ScalarType ScalarType;
typedef typename SaveMeshType::VertexType VertexType;
typedef typename SaveMeshType::FaceType FaceType;
typedef typename SaveMeshType::FacePointer FacePointer;
typedef typename SaveMeshType::VertexIterator VertexIterator;
typedef typename SaveMeshType::FaceIterator FaceIterator;
static bool Save(SaveMeshType &m, const char * filename )
{
vcg::face::Pos<MyMesh::FaceType> he;
vcg::face::Pos<MyMesh::FaceType> hei;
FILE * fpout = fopen(filename,"w");
if(fpout==NULL) return false;
if( m.HasPerVertexNormal())
fprintf(fpout,"N");
if( m.HasPerVertexColor())
fprintf(fpout,"C");
if( m.HasPerVertexTexture())
fprintf(fpout,"ST");
fprintf(fpout,"OFF\n");
fprintf(fpout,"%d %d ", m.vn, m.fn);
MyMesh::FaceIterator fi;
int count_e = 0;
bool counted=false;
for(fi=m.face.begin();fi!=m.face.end();++fi)
(*fi).ClearS();
for(fi=m.face.begin();fi!=m.face.end();++fi)
{
(*fi).SetS();
count_e +=3;
for(int i=0; i<3; ++i)
{
if (IsManifold(*fi,i))
{
if((*fi).FFp(i)->IsS())
count_e--;
}
else
{
hei.Set(&(*fi), i , fi->V(i));
he=hei;
he.NextF();
while (he.f!=hei.f)
{
if (he.f->IsS())
{
counted=true;
break;
}
else
{
he.NextF();
}
}
if (counted)
{
count_e--;
counted=false;
}
}
}
}
fprintf(fpout,"%d\n", count_e);
//vertices
int j;
std::vector<int> FlagV;
VertexPointer vp;
VertexIterator vi;
for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
{
vp=&(*vi);
FlagV.push_back(vp->UberFlags()); // Salva in ogni caso flag del vertice
if( ! vp->IsD() )
{ // ***** ASCII *****
fprintf(fpout,"Vertex: %g %g %g\n" ,vp->P()[0],vp->P()[1],vp->P()[2]);
if( m.HasPerVertexColor() )
fprintf(fpout,"Color: %d %d %d %d\n",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
if( m.HasPerVertexNormal())
fprintf(fpout,"Normal: %g %g %g\n", vp->N()[0],vp->N()[1],vp->N()[2]);
if( m.HasPerVertexTexture())
fprintf(fpout,"Texture: %g %g\n",vp->T().u(),vp->T().v());
}
vp->UberFlags()=j; // Trucco! Nascondi nei flags l'indice del vertice non deletato!
j++;
}
FacePointer fp;
int vv[3];
int fcnt=0;
for(j=0,fi=m.face.begin();fi!=m.face.end();++fi)
{
fp=&(*fi);
if( ! fp->IsD() )
{ fcnt++;
fprintf(fpout,"3 %d %d %d\n",
fp->cV(0)->UberFlags(), fp->cV(1)->UberFlags(), fp->cV(2)->UberFlags() );
}
}
fclose(fpout);
// Recupera i flag originali
for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
(*vi).UberFlags()=FlagV[j++];
return 0;
}
}; // end class
} // end namespace tri
} // end namespace io
} // end namespace vcg
//@}
#endif