361 lines
14 KiB
C++
361 lines
14 KiB
C++
/****************************************************************************
|
|
* 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: not supported by cvs2svn $
|
|
Revision 1.1 2007/02/14 01:20:37 ganovelli
|
|
working draft of VCG Mesh Image importer and exporter. Does not consider optional attributes. The mesh atributes are only vn and fn (no bbox, texture coordiantes)
|
|
|
|
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __VCGLIB_EXPORT_VMI
|
|
#define __VCGLIB_EXPORT_VMI
|
|
|
|
|
|
/*
|
|
VMI VCG Mesh Image.
|
|
The vmi image file consists of a header containing the description of the vertex and face type,
|
|
the length of vectors containing vertices of faces and the memory image of the object mesh as it is when
|
|
passed to the function Save(SaveMeshType m).
|
|
NOTE: THIS IS NOT A FILE FORMAT. IT IS ONLY USEFUL FOR DUMPING MESH IMAGES FOR DEBUG PURPOSE.
|
|
Example of use: say you are running a time consuming mesh processing and you want to save intermediate
|
|
state, but no file format support all the attributes you need in your vertex/face type.
|
|
NOTE2: At the present if you add members to your TriMesh these will NOT be saved. More precisely, this file and
|
|
import_vmi must be updated to reflect changes in vcg/complex/trimesh/base.h
|
|
*/
|
|
|
|
#include <vcg/simplex/face/component.h>
|
|
#include <vcg/simplex/face/component_ocf.h>
|
|
#include <vcg/simplex/vertex/component.h>
|
|
#include <vcg/simplex/vertex/component_ocf.h>
|
|
|
|
namespace vcg {
|
|
namespace tri {
|
|
namespace io {
|
|
|
|
template <int N> struct PlaceHolderType{ char A[N];};
|
|
|
|
|
|
void WriteString(FILE *f,const char * in) { unsigned int l = strlen(in); fwrite(&l,4,1,f); fwrite(in,1,l,f);}
|
|
void WriteInt(FILE *f,const unsigned int i) { fwrite(&i,1,4,f);}
|
|
|
|
/* save Ocf Vertex Components */
|
|
template <typename OpenMeshType,typename CONT>
|
|
struct SaveVertexOcf{
|
|
SaveVertexOcf(FILE*f, const CONT & vert){
|
|
// do nothing, it is a std::vector
|
|
}
|
|
};
|
|
|
|
/* partial specialization for vector_ocf */
|
|
template <typename MeshType>
|
|
struct SaveVertexOcf<MeshType, vertex::vector_ocf<typename MeshType::VertexType> >{
|
|
typedef typename MeshType::VertexType VertexType;
|
|
SaveVertexOcf(FILE * f,const vertex::vector_ocf<VertexType> & vert){
|
|
|
|
if( VertexType::HasVertexQualityOcf() && vert.IsQualityEnabled()){
|
|
WriteString(f,"HAS_VERTEX_QUALITY_OCF");
|
|
fwrite(&vert.QV[0],sizeof(VertexType::QualityType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_QUALITY_OCF");
|
|
|
|
if( VertexType::HasVertexColorOcf() && vert.IsColorEnabled()){
|
|
WriteString(f,"HAS_VERTEX_COLOR_OCF");
|
|
fwrite(&vert.CV[0],sizeof(VertexType::ColorType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_COLOR_OCF");
|
|
|
|
if( VertexType::HasVertexNormalOcf() && vert.IsNormalEnabled()){
|
|
WriteString(f,"HAS_VERTEX_NORMAL_OCF");
|
|
fwrite(&vert.NV[0],sizeof(VertexType::NormalType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_NORMAL_OCF");
|
|
|
|
if( VertexType::HasVertexMarkOcf() && vert.IsMarkEnabled()){
|
|
WriteString(f,"HAS_VERTEX_MARK_OCF");
|
|
fwrite(&vert.MV[0],sizeof(VertexType::MarkType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_MARK_OCF");
|
|
|
|
if( VertexType::HasTexCoordOcf() && vert.IsTexCoordEnabled()){
|
|
WriteString(f,"HAS_VERTEX_TEXCOORD_OCF");
|
|
fwrite(&vert.TV[0],sizeof(vertex::vector_ocf<VertexType>::TexCoordType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_TEXCOORD_OCF");
|
|
|
|
if( VertexType::HasVFAdjacencyOcf() && vert.IsVFAdjacencyEnabled()){
|
|
WriteString(f,"HAS_VERTEX_VFADJACENCY_OCF");
|
|
fwrite(&vert.AV[0],sizeof(vertex::vector_ocf<VertexType>::VFAdjType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_VFADJACENCY_OCF");
|
|
|
|
if( VertexType::HasCurvatureOcf() && vert.IsCurvatureEnabled()){
|
|
WriteString(f,"HAS_VERTEX_CURVATURE_OCF");
|
|
fwrite(&vert.CuV[0],sizeof(VertexType::CurvatureType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_CURVATURE_OCF");
|
|
|
|
if( VertexType::HasCurvatureDirOcf() && vert.IsCurvatureDirEnabled()){
|
|
WriteString(f,"HAS_VERTEX_CURVATUREDIR_OCF");
|
|
fwrite(&vert.CuDV[0],sizeof(VertexType::CurvatureDirType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_CURVATUREDIR_OCF");
|
|
|
|
if( VertexType::HasRadiusOcf() && vert.IsRadiusEnabled()){
|
|
WriteString(f,"HAS_VERTEX_RADIUS_OCF");
|
|
fwrite(&vert.RadiusV[0],sizeof(vertex::vector_ocf<VertexType>::RadiusType),vert.size(),f);
|
|
}else WriteString(f,"NOT_HAS_VERTEX_RADIUS_OCF");
|
|
|
|
}
|
|
};
|
|
|
|
|
|
/* save Ocf Face Components */
|
|
template <typename MeshType,typename CONT>
|
|
struct SaveFaceOcf{
|
|
SaveFaceOcf(FILE * f,const CONT & face){
|
|
// do nothing, it is a std::vector
|
|
}
|
|
};
|
|
|
|
/* partial specialization for vector_ocf */
|
|
template <typename MeshType>
|
|
struct SaveFaceOcf< MeshType, face::vector_ocf<typename MeshType::FaceType> >{
|
|
typedef typename MeshType::FaceType FaceType;
|
|
SaveFaceOcf(FILE * f,const face::vector_ocf<FaceType> & face){
|
|
|
|
if( FaceType::HasFaceQualityOcf() && face.IsQualityEnabled()){
|
|
WriteString(f,"HAS_FACE_QUALITY_OCF");
|
|
fwrite(&face.QV[0],sizeof(FaceType::QualityType),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_QUALITY_OCF");
|
|
|
|
if( FaceType::HasFaceColorOcf() && face.IsColorEnabled()){
|
|
WriteString(f,"HAS_FACE_COLOR_OCF");
|
|
fwrite(&face.CV[0],sizeof(FaceType::ColorType),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_COLOR_OCF");
|
|
|
|
if( FaceType::HasFaceNormalOcf() && face.IsNormalEnabled()){
|
|
WriteString(f,"HAS_FACE_NORMAL_OCF");
|
|
fwrite(&face.NV[0],sizeof(FaceType::NormalType),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_NORMAL_OCF");
|
|
|
|
if( FaceType::HasFaceMarkOcf() && face.IsMarkEnabled()){
|
|
WriteString(f,"HAS_FACE_MARK_OCF");
|
|
fwrite(&face.MV[0],sizeof(FaceType::MarkType),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_MARK_OCF");
|
|
|
|
if( FaceType::HasWedgeTexCoordOcf() && face.IsWedgeTexEnabled()){
|
|
WriteString(f,"HAS_FACE_WEDGETEXCOORD_OCF");
|
|
fwrite(&face.WTV[0],sizeof(FaceType::WedgeTexCoordType),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_WEDGETEXCOORD_OCF");
|
|
|
|
if( FaceType::HasFFAdjacencyOcf() && face.IsFFAdjacencyEnabled()){
|
|
WriteString(f,"HAS_FACE_FFADJACENCY_OCF");
|
|
fwrite(&face.AF[0],sizeof(face::vector_ocf<FaceType>::AdjTypePack),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_FFADJACENCY_OCF");
|
|
|
|
if( FaceType::HasVFAdjacencyOcf() && face.IsVFAdjacencyEnabled()){
|
|
WriteString(f,"HAS_FACE_VFADJACENCY_OCF");
|
|
fwrite(&face.AV[0],sizeof(face::vector_ocf<FaceType>::AdjTypePack),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_VFADJACENCY_OCF");
|
|
|
|
if( FaceType::HasWedgeColorOcf() && face.IsWedgeColorEnabled()){
|
|
WriteString(f,"HAS_FACE_WEDGECOLOR_OCF");
|
|
fwrite(&face.WCV[0],sizeof(face::vector_ocf<FaceType>::WedgeColorTypePack),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_WEDGECOLOR_OCF");
|
|
|
|
if( FaceType::HasWedgeNormalOcf() && face.IsWedgeNormalEnabled()){
|
|
WriteString(f,"HAS_FACE_WEDGENORMAL_OCF");
|
|
fwrite(&face.WNV[0],sizeof(face::vector_ocf<FaceType>::WedgeNormalTypePack),face.size(),f);
|
|
}else WriteString(f,"NOT_HAS_FACE_WEDGENORMAL_OCF");
|
|
}
|
|
};
|
|
|
|
|
|
template <class SaveMeshType>
|
|
class ExporterVMI
|
|
{
|
|
public:
|
|
static FILE *& F(){static FILE * f; return f;}
|
|
|
|
typedef typename SaveMeshType::FaceContainer FaceContainer;
|
|
typedef typename SaveMeshType::FaceIterator FaceIterator;
|
|
typedef typename SaveMeshType::VertContainer VertContainer;
|
|
typedef typename SaveMeshType::VertexIterator VertexIterator;
|
|
typedef typename SaveMeshType::VertexType VertexType;
|
|
typedef typename SaveMeshType::FaceType FaceType;
|
|
typedef SimpleTempDataBase<typename SaveMeshType::VertContainer> STDBv;
|
|
typedef SimpleTempDataBase<typename SaveMeshType::FaceContainer> STDBf;
|
|
// typedef typename SaveMeshType::Attribute <SaveMeshType::FaceContainer> STDBm;
|
|
|
|
/* save Ocf Components */
|
|
|
|
|
|
|
|
static void Save(const SaveMeshType &m,char * filename){
|
|
unsigned int i;
|
|
unsigned int vertSize,faceSize;
|
|
F() = fopen(filename,"wb");
|
|
std::vector<std::string> nameF,nameV;
|
|
SaveMeshType::FaceType::Name(nameF);
|
|
SaveMeshType::VertexType::Name(nameV);
|
|
vertSize = m.vert.size();
|
|
faceSize = m.face.size();
|
|
|
|
/* write header */
|
|
WriteString(F(),"FACE_TYPE");
|
|
WriteInt(F(),nameF.size());
|
|
|
|
for(i=0; i < nameF.size(); ++i) WriteString(F(),nameF[i].c_str());
|
|
WriteString(F(),"SIZE_VECTOR_FACES");
|
|
WriteInt(F(), faceSize );
|
|
|
|
WriteString(F(),"VERTEX_TYPE");
|
|
WriteInt(F(),nameV.size());
|
|
|
|
for(i=0; i < nameV.size(); ++i) WriteString(F(),nameV[i].c_str());
|
|
|
|
WriteString(F(),"SIZE_VECTOR_VERTS");
|
|
WriteInt(F(),vertSize);
|
|
|
|
WriteString(F(),"end_header");
|
|
|
|
if(vertSize!=0){
|
|
unsigned int offsetV = (unsigned int) &m.vert[0];
|
|
/* write the address of the first vertex */
|
|
fwrite(&offsetV,sizeof(unsigned int),1,F());
|
|
}
|
|
|
|
if(faceSize!=0){
|
|
int offsetF= ( int) &m.face[0];
|
|
/* write the address of the first face */
|
|
fwrite(&offsetF,sizeof( int),1,F());
|
|
}
|
|
/* save the object mesh */
|
|
fwrite(&m.shot,sizeof(Shot<typename SaveMeshType::ScalarType>),1,F());
|
|
fwrite(&m.vn,sizeof(int),1,F());
|
|
fwrite(&m.fn,sizeof(int),1,F());
|
|
fwrite(&m.imark,sizeof(int),1,F());
|
|
fwrite(&m.bbox,sizeof(Box3<typename SaveMeshType::ScalarType>),1,F());
|
|
fwrite(&m.C(),sizeof(Color4b),1,F());
|
|
|
|
unsigned int written;
|
|
|
|
|
|
if(vertSize!=0){
|
|
/* save the vertices */
|
|
written = fwrite((void*)&m.vert[0],sizeof(typename SaveMeshType::VertexType),m.vert.size(),F());
|
|
assert(written==m.vert.size());
|
|
SaveVertexOcf<SaveMeshType,VertContainer>(F(),m.vert);
|
|
}
|
|
|
|
if(faceSize!=0){
|
|
/* save the faces */
|
|
written = fwrite((void*)&m.face[0],sizeof(typename SaveMeshType::FaceType),faceSize,F());
|
|
assert(written==m.face.size());
|
|
|
|
SaveFaceOcf<SaveMeshType,FaceContainer>(F(),m.face);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the attributes */
|
|
typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
|
|
|
|
|
|
/* save the per vertex attributes */
|
|
{
|
|
typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
|
|
unsigned int n_named_attr = 0;
|
|
for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
|
|
|
|
WriteString(F(),"N_PER_VERTEX_ATTRIBUTES"); WriteInt (F(),n_named_attr);
|
|
for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai)
|
|
if(!(*ai)._name.empty())
|
|
{
|
|
STDBv * stdb = (STDBv *) (*ai)._handle;
|
|
|
|
WriteString(F(),"PER_VERTEX_ATTR_NAME");
|
|
WriteString(F(),(*ai)._name.c_str() );
|
|
|
|
WriteString(F(),"PER_VERTEX_ATTR_SIZE");
|
|
WriteInt(F(),stdb->SizeOf());
|
|
|
|
fwrite(stdb->DataBegin(),m.vert.size(),stdb->SizeOf(),F());
|
|
}
|
|
}
|
|
|
|
/* save the per face attributes */
|
|
{
|
|
typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
|
|
unsigned int n_named_attr = 0;
|
|
for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
|
|
|
|
WriteString(F(),"N_PER_FACE_ATTRIBUTES");
|
|
WriteInt (F(),n_named_attr);
|
|
|
|
for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai)
|
|
if(!(*ai)._name.empty())
|
|
{
|
|
STDBf * stdb = (STDBf *) (*ai)._handle;
|
|
|
|
WriteString(F(),"PER_FACE_ATTR_NAME");
|
|
WriteString(F(),(*ai)._name.c_str());
|
|
|
|
WriteString(F(),"PER_FACE_ATTR_SIZE");
|
|
WriteInt(F(),stdb->SizeOf());
|
|
|
|
fwrite(stdb->DataBegin(),m.face.size(),stdb->SizeOf(),F());
|
|
}
|
|
}
|
|
|
|
///* save the per mesh attributes */
|
|
{
|
|
typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
|
|
unsigned int n_named_attr = 0;
|
|
for(ai = m.mesh_attr.begin(); ai != m.mesh_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
|
|
WriteString(F(),"N_PER_MESH_ATTRIBUTES"); WriteInt(F(),n_named_attr);
|
|
for(ai = m.mesh_attr.begin(); ai != m.mesh_attr.end(); ++ai)
|
|
if(!(*ai)._name.empty())
|
|
{
|
|
AttributeBase * handle = (AttributeBase *) (*ai)._handle ;
|
|
|
|
WriteString(F(),"PER_MESH_ATTR_NAME");
|
|
WriteString(F(),(*ai)._name.c_str());
|
|
|
|
WriteString(F(),"PER_MESH_ATTR_SIZE");
|
|
WriteInt(F(),handle->SizeOf());
|
|
|
|
fwrite(handle->DataBegin(),1,handle->SizeOf(),F());
|
|
}
|
|
}
|
|
|
|
// fflush(F());
|
|
fclose(F());
|
|
}
|
|
|
|
}; // end class
|
|
|
|
} // end Namespace tri
|
|
} // end Namespace io
|
|
} // end Namespace vcg
|
|
|
|
#endif
|