2005-09-28 12:16:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
2016-06-13 07:29:25 +02:00
|
|
|
* Copyright(C) 2004-2016 \/)\/ *
|
2005-09-28 12:16:19 +02:00
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2014-05-05 23:50:18 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2005-09-28 12:16:19 +02:00
|
|
|
* 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. *
|
|
|
|
* *
|
2006-12-07 01:37:58 +01:00
|
|
|
****************************************************************************/
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
/**
|
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>
|
2014-11-05 16:47:57 +01:00
|
|
|
#include<wrap/io_trimesh/precision.h>
|
2011-04-03 22:55:22 +02:00
|
|
|
#include <vcg/complex/algorithms/clean.h>
|
|
|
|
#include <vcg/complex/algorithms/polygon_support.h>
|
2006-01-10 14:20:42 +01:00
|
|
|
|
2005-09-28 12:16:19 +02:00
|
|
|
|
|
|
|
namespace vcg {
|
2014-10-29 23:24:45 +01:00
|
|
|
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 int Save(SaveMeshType &m, const char * filename, int mask=0 )
|
|
|
|
{
|
|
|
|
vcg::face::Pos<FaceType> he;
|
|
|
|
vcg::face::Pos<FaceType> hei;
|
|
|
|
FILE * fpout = fopen(filename,"w");
|
|
|
|
if(fpout==NULL) return 1; // 1 is the error code for cant'open, see the ErrorMsg function
|
|
|
|
|
|
|
|
|
|
|
|
if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTNORMAL))
|
|
|
|
fprintf(fpout,"N");
|
|
|
|
if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR))
|
|
|
|
fprintf(fpout,"C");
|
|
|
|
if( tri::HasPerVertexTexCoord(m) && (mask & io::Mask::IOM_VERTTEXCOORD))
|
|
|
|
fprintf(fpout,"ST");
|
|
|
|
fprintf(fpout,"OFF\n");
|
|
|
|
|
|
|
|
int polynumber;
|
|
|
|
if (mask &io::Mask::IOM_BITPOLYGONAL)
|
|
|
|
polynumber = tri::Clean<SaveMeshType>::CountBitLargePolygons(m);
|
|
|
|
else
|
|
|
|
polynumber = m.fn;
|
|
|
|
|
|
|
|
fprintf(fpout,"%d %d 0\n", m.vn, polynumber); // note that as edge number we simply write zero
|
|
|
|
|
|
|
|
//vertices
|
2014-11-05 00:13:16 +01:00
|
|
|
const int DGT = vcg::tri::io::Precision<ScalarType>::digits();
|
2018-11-29 00:32:58 +01:00
|
|
|
for(auto vp=m.vert.begin();vp!=m.vert.end();++vp)
|
2014-10-29 23:24:45 +01:00
|
|
|
{
|
|
|
|
if( ! vp->IsD() )
|
|
|
|
{ // ***** ASCII *****
|
|
|
|
|
2014-11-05 00:13:16 +01:00
|
|
|
fprintf(fpout,"%.*g %.*g %.*g " ,DGT,vp->P()[0],DGT,vp->P()[1],DGT,vp->P()[2]);
|
2014-10-29 23:24:45 +01:00
|
|
|
if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR) )
|
|
|
|
fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
|
|
|
|
|
|
|
|
if( tri::HasPerVertexNormal(m) && (mask & io::Mask::IOM_VERTNORMAL) )
|
2014-11-15 00:05:40 +01:00
|
|
|
fprintf(fpout,"%g %g %g ", double(vp->N()[0]),double(vp->N()[1]),double(vp->N()[2]));
|
2014-10-29 23:24:45 +01:00
|
|
|
|
|
|
|
if( tri::HasPerVertexTexCoord(m) && (mask & io::Mask::IOM_VERTTEXCOORD) )
|
|
|
|
fprintf(fpout,"%g %g ",vp->T().u(),vp->T().v());
|
|
|
|
|
|
|
|
fprintf(fpout,"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mask &io::Mask::IOM_BITPOLYGONAL) {
|
|
|
|
tri::RequireFFAdjacency(m);
|
|
|
|
std::vector<VertexPointer> polygon;
|
|
|
|
tri::UpdateFlags<SaveMeshType>::FaceClearV(m);
|
|
|
|
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi) if (!fi->IsD()) if (!fi->IsV()) {
|
|
|
|
vcg::tri::PolygonSupport<SaveMeshType,SaveMeshType>::ExtractPolygon(&*fi,polygon);
|
2021-07-06 16:43:22 +02:00
|
|
|
//not sure why this std::reverse is needed. ExtractPolygon is used in
|
|
|
|
//many other functions, and nobody complained. however, this list is
|
|
|
|
//clockwise wrt fi normal.
|
|
|
|
std::reverse(polygon.begin(), polygon.end());
|
2014-10-29 23:24:45 +01:00
|
|
|
if(!polygon.empty())
|
2006-01-13 16:47:43 +01:00
|
|
|
{
|
2014-10-29 23:24:45 +01:00
|
|
|
fprintf(fpout,"%d ", int(polygon.size()) );
|
2021-07-06 16:43:22 +02:00
|
|
|
for (size_t i=0; i<polygon.size(); i++)
|
|
|
|
fprintf(fpout,"%lu ", tri::Index(m,polygon[i]));
|
|
|
|
if( tri::HasPerFaceColor(m) && (mask & io::Mask::IOM_FACECOLOR) )
|
|
|
|
fprintf(fpout,"%i %i %i", fi->C()[0],fi->C()[1],fi->C()[2] );
|
2014-10-29 23:24:45 +01:00
|
|
|
fprintf(fpout,"\n");
|
2006-01-13 16:47:43 +01:00
|
|
|
}
|
2014-10-29 23:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
|
|
|
|
{
|
|
|
|
if( ! fi->IsD() )
|
2006-01-30 14:43:59 +01:00
|
|
|
{
|
2018-11-29 00:32:58 +01:00
|
|
|
fprintf(fpout,"%i ",fi->VN());
|
|
|
|
for(int i=0;i<fi->VN();++i)
|
|
|
|
fprintf(fpout,"%lu ",tri::Index(m,fi->V(i)));
|
2014-10-29 23:24:45 +01:00
|
|
|
if( tri::HasPerFaceColor(m) && (mask & io::Mask::IOM_FACECOLOR) )
|
2018-11-29 00:32:58 +01:00
|
|
|
fprintf(fpout,"%i %i %i", fi->C()[0],fi->C()[1],fi->C()[2] );
|
|
|
|
fprintf(fpout,"\n");
|
2006-01-30 14:43:59 +01:00
|
|
|
}
|
2014-10-29 23:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 14:06:32 +02:00
|
|
|
int result = 0;
|
|
|
|
if (ferror(fpout)) result = 2;
|
|
|
|
fclose(fpout);
|
|
|
|
return result;
|
2014-10-29 23:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ErrorMsg(int error)
|
|
|
|
{
|
2018-05-09 14:06:32 +02:00
|
|
|
static std::vector<std::string> off_error_msg;
|
|
|
|
if (off_error_msg.empty())
|
|
|
|
{
|
|
|
|
off_error_msg.resize(3);
|
|
|
|
off_error_msg[0] = "No errors";
|
|
|
|
off_error_msg[1] = "Can't open file";
|
2020-04-17 16:44:23 +02:00
|
|
|
off_error_msg[2] = "Output Stream error";
|
2018-05-09 14:06:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error>2 || error<0) return "Unknown error";
|
|
|
|
else return off_error_msg[error].c_str();
|
2014-10-29 23:24:45 +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_VERTTEXCOORD;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
|
|
|
|
capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL;
|
|
|
|
return capability;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // end class
|
|
|
|
} // end namespace tri
|
|
|
|
} // end namespace io
|
2005-09-28 12:16:19 +02:00
|
|
|
} // end namespace vcg
|
|
|
|
//@}
|
2005-12-01 01:58:56 +01:00
|
|
|
#endif
|