2005-09-28 12:16:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2005-09-28 12:17:49 +02:00
|
|
|
@name Save in OFF format
|
2005-09-28 12:16:19 +02:00
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
#ifndef __VCGLIB_EXPORT_OFF
|
|
|
|
#define __VCGLIB_EXPORT_OFF
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-01-10 14:20:42 +01:00
|
|
|
#include <wrap/io_trimesh/io_mask.h>
|
|
|
|
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-01-30 14:43:59 +01:00
|
|
|
static int Save(SaveMeshType &m, const char * filename, int mask=0 )
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
2005-12-01 01:58:56 +01:00
|
|
|
vcg::face::Pos<FaceType> he;
|
|
|
|
vcg::face::Pos<FaceType> hei;
|
2005-09-28 12:16:19 +02:00
|
|
|
FILE * fpout = fopen(filename,"w");
|
|
|
|
if(fpout==NULL) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-01-30 14:43:59 +01:00
|
|
|
if( m.HasPerVertexNormal() && (mask & io::Mask::IOM_VERTNORMAL)) fprintf(fpout,"N");
|
|
|
|
if( m.HasPerVertexColor() && (mask & io::Mask::IOM_VERTCOLOR)) fprintf(fpout,"C");
|
|
|
|
if( m.HasPerVertexTexture() && (mask & io::Mask::IOM_VERTTEXCOORD)) fprintf(fpout,"ST");
|
2005-09-28 12:16:19 +02:00
|
|
|
fprintf(fpout,"OFF\n");
|
|
|
|
fprintf(fpout,"%d %d ", m.vn, m.fn);
|
|
|
|
|
2005-12-01 01:58:56 +01:00
|
|
|
typename SaveMeshType::FaceIterator fi;
|
2005-09-28 12:16:19 +02:00
|
|
|
int count_e = 0;
|
2005-09-28 14:35:36 +02:00
|
|
|
int boundary_e = 0;
|
2005-09-28 12:16:19 +02:00
|
|
|
bool counted=false;
|
|
|
|
for(fi=m.face.begin();fi!=m.face.end();++fi)
|
|
|
|
(*fi).ClearS();
|
|
|
|
|
2005-09-28 14:35:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
for(fi=m.face.begin();fi!=m.face.end();fi++)
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
|
|
|
(*fi).SetS();
|
2005-09-28 14:35:36 +02:00
|
|
|
count_e +=3; //assume that we have to increase the number of edges with three
|
|
|
|
for(int j=0; j<3; j++)
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
2005-11-12 19:24:56 +01:00
|
|
|
if (face::IsBorder(*fi,j)) //If this edge is a border edge
|
2005-09-28 14:35:36 +02:00
|
|
|
boundary_e++; // then increase the number of boundary edges
|
2005-12-01 01:58:56 +01:00
|
|
|
else if (face::IsManifold(*fi,j)) //If this edge is manifold
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
2005-09-28 14:35:36 +02:00
|
|
|
if((*fi).FFp(j)->IsS()) //If the face on the other side of the edge is already selected
|
|
|
|
count_e--; // we counted one edge twice
|
2005-09-28 12:16:19 +02:00
|
|
|
}
|
2005-09-28 14:35:36 +02:00
|
|
|
else //We have a non-manifold edge
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
2005-09-28 14:35:36 +02:00
|
|
|
hei.Set(&(*fi), j , fi->V(j));
|
2005-09-28 12:16:19 +02:00
|
|
|
he=hei;
|
|
|
|
he.NextF();
|
2005-09-28 14:35:36 +02:00
|
|
|
while (he.f!=hei.f) // so we have to iterated all faces that are connected to this edge
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
2005-09-28 14:35:36 +02:00
|
|
|
if (he.f->IsS()) // if one of the other faces was already visited than this edge was counted already.
|
2005-09-28 12:16:19 +02:00
|
|
|
{
|
|
|
|
counted=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
he.NextF();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (counted)
|
|
|
|
{
|
|
|
|
count_e--;
|
|
|
|
counted=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-28 14:35:36 +02:00
|
|
|
}
|
2005-09-28 12:16:19 +02:00
|
|
|
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() )
|
2005-09-28 14:35:36 +02:00
|
|
|
fprintf(fpout,"%d %d %d %d\n",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
if( m.HasPerVertexNormal())
|
2005-09-28 14:35:36 +02:00
|
|
|
fprintf(fpout,"%g %g %g\n", vp->N()[0],vp->N()[1],vp->N()[2]);
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
if( m.HasPerVertexTexture())
|
2005-09-28 14:35:36 +02:00
|
|
|
fprintf(fpout,"%g %g\n",vp->T().u(),vp->T().v());
|
2005-09-28 12:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vp->UberFlags()=j; // Trucco! Nascondi nei flags l'indice del vertice non deletato!
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
FacePointer fp;
|
2005-11-12 19:24:56 +01:00
|
|
|
// int vv[3];
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2006-01-13 16:47:43 +01:00
|
|
|
|
|
|
|
static const char *ErrorMsg(int error)
|
|
|
|
{
|
|
|
|
static std::vector<std::string> off_error_msg;
|
|
|
|
if(off_error_msg.empty())
|
|
|
|
{
|
|
|
|
off_error_msg.resize(2 );
|
|
|
|
off_error_msg[0]="No errors";
|
|
|
|
off_error_msg[1]="Can't open file";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(error>1 || error<0) return "Unknown error";
|
|
|
|
else return off_error_msg[error].c_str();
|
|
|
|
}
|
2006-01-30 14:43:59 +01:00
|
|
|
/*
|
|
|
|
returns mask of capability one define with what are the saveable information of the format.
|
|
|
|
*/
|
|
|
|
static int GetExportMaskCapability()
|
|
|
|
{
|
|
|
|
int capability = 0;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
|
|
|
|
return capability;
|
|
|
|
}
|
2006-01-13 16:47:43 +01:00
|
|
|
|
2005-09-28 12:16:19 +02:00
|
|
|
}; // end class
|
|
|
|
} // end namespace tri
|
|
|
|
} // end namespace io
|
|
|
|
} // end namespace vcg
|
|
|
|
//@}
|
2005-12-01 01:58:56 +01:00
|
|
|
#endif
|