From 0556b33b656057ab26d1de3babd6b0f26aa9ebbb Mon Sep 17 00:00:00 2001 From: cignoni Date: Wed, 3 Mar 2004 15:35:53 +0000 Subject: [PATCH] Yet another cr lf mismatch --- vcg/complex/trimesh/allocate.h | 384 ++++++++++++++++----------------- wrap/io_trimesh/export_stl.h | 201 ++++++++++------- 2 files changed, 309 insertions(+), 276 deletions(-) diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index 3079a857..55945c4e 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -1,196 +1,194 @@ -/**************************************************************************** -* 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 - +/**************************************************************************** +* 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 2004/02/24 21:36:42 cignoni +grouped documentation, changed typenames and reflection mechanism + Revision 1.1 2004/02/19 13:11:06 cignoni Initial commit - - -****************************************************************************/ - -namespace vcg { -namespace tri { - -template -class Allocator -{ - -public: -typedef AllocateMeshType MeshType; -typedef typename MeshType::VertexPointer VertexPointer; -typedef typename MeshType::VertexIterator VertexIterator; -typedef typename MeshType::FacePointer FacePointer; -typedef typename MeshType::FaceIterator FaceIterator; - -/* This class is used to */ -template -class PointerUpdater -{ -public: - void Clear(); - void Update(SimplexPointerType &vp); - bool NeedUpdate(); - - SimplexPointerType oldBase; - SimplexPointerType newBase; -}; - - -/** Function to add n vertices to the mesh. The second parameter hold a vector of - pointers to pointer to elements of the mesh that should be updated after a - possible vector realloc. - @param n Il numero di vertici che si vuole aggiungere alla mesh. - @param local_var Vettore di variabili locali che rappresentano puntatori a vertici. - restituisce l'iteratore al primo elemento aggiunto. -*/ -static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater &pu) -{ - VertexIterator last=m.vert.end(); - pu.Clear(); - if(m.vert.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element - else pu.oldBase=&*m.vert.begin(); - - for(int i=0; i pu; - return AddVertices(m, n,pu); -} - -/** Function to add n faces to the mesh. - @param n Il numero di facce che si vuole aggiungere alla mesh -*/ -static FaceIterator AddFaces(MeshType &m, int n) -{ - PointerUpdater pu; - return AddFaces(m,n,pu); -} -/** Function to add n faces to the mesh. - NOTA: Aggiorna fn; - The second parameter hold a vector of - pointers to pointer to elements of the mesh that should be updated after a - possible vector realloc. - @param n Facce da aggiungere - @param local_var Vettore di variabili locali che rappresentano puntatori a facce, occorre, - perche' questi valori siano consistenti, aggiornarli ogni qual volta venga eseguito un resize - del contenitore delle facce. -*/ -static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater &pu) -{ - FaceIterator last=m.vert.end(); - pu.Clear(); - if(m.face.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element - else pu.oldBase=&*m.face.begin(); - - unsigned int siz=0; for(int i=0; i::iterator jit; - for(jit=local_var.begin(); jit!=local_var.end(); ++jit) - if((**jit) !=0 ) **jit = **jit-&*oldbegin+&*newbegin; - - // deve restituire l'iteratore alla prima faccia aggiunta; - if(last!=0) - { - last = face.begin(); - advance(last,siz+1); - } - else last=face.begin(); - } - else // - { assert(newbegin == oldbegin); - // se non e'cambiato lo spazio (vector abbastanza grande o lista) - if(last==0) last = face.begin(); // se il vettore era vuoto si restituisce begin - else advance(last,1); // altrimenti il primo dopo quello che era in precedenza l'ultimo valido. - } - - return last; - -} - -}; // end class -} // End Namespace TriMesh -} // End Namespace vcg + + +****************************************************************************/ + +namespace vcg { +namespace tri { + +template +class Allocator +{ + +public: +typedef AllocateMeshType MeshType; +typedef typename MeshType::VertexType VertexType; +typedef typename MeshType::VertexPointer VertexPointer; +typedef typename MeshType::VertexIterator VertexIterator; +typedef typename MeshType::FaceType FaceType; +typedef typename MeshType::FacePointer FacePointer; +typedef typename MeshType::FaceIterator FaceIterator; + +/* This class is used when allocating new vertex and faces to update + the pointer that can be changed when resizing the vector of vertex or faces. + It can also be used to prevent any update of the various mesh fields + (e.g. in case you are building all the connections by hand as in a importer); +*/ +template +class PointerUpdater +{ +public: + void Clear(){newBase=oldBase=newEnd=oldEnd=0;preventUpdateFlag=false;}; + void Update(SimplexPointerType &vp) + { + vp=newBase+(vp-oldBase); + } + bool NeedUpdate() {if(newBase!=oldBase && !preventUpdateFlag) return true; else return false;} + + SimplexPointerType oldBase; + SimplexPointerType newBase; + SimplexPointerType newEnd; + SimplexPointerType oldEnd; + bool preventUpdateFlag; // when true no update is considered necessary. +}; + + +/** Function to add n vertices to the mesh. The second parameter hold a vector of + pointers to pointer to elements of the mesh that should be updated after a + possible vector realloc. + @param n Il numero di vertici che si vuole aggiungere alla mesh. + @param local_var Vettore di variabili locali che rappresentano puntatori a vertici. + restituisce l'iteratore al primo elemento aggiunto. +*/ +static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater &pu) +{ + VertexIterator last=m.vert.end(); + pu.Clear(); + if(m.vert.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element + else pu.oldBase=&*m.vert.begin(); + + for(int i=0; i pu; + return AddVertices(m, n,pu); +} + +/** Function to add n faces to the mesh. + @param n Il numero di facce che si vuole aggiungere alla mesh +*/ +static FaceIterator AddFaces(MeshType &m, int n) +{ + PointerUpdater pu; + return AddFaces(m,n,pu); +} +/** Function to add n faces to the mesh. + NOTA: Aggiorna fn; +*/ +static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater &pu) +{ + FaceIterator last=m.face.end(); + pu.Clear(); + if(m.face.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element + else pu.oldBase=&*m.face.begin(); + + unsigned int siz=0; for(int i=0; iP(), (*fi).V(1)->P(), (*fi).V(2)->P())); - fwrite(p.V(),3,sizeof(float),fp); - - for(int k=0;k<3;++k){ - p.Import((*fi).V(k)->P()); - fwrite(p.V(),3,sizeof(float),fp); - } - fwrite(&attributes,1,sizeof(short),fp); - } - } - else - { - if(objectname) fprintf(fp,"solid %s\n",objectname); - else fprintf(fp,"solid vcg\n"); - - Point3f p; - face_iterator fi; - for(fi=face.begin(); fi!=face.end(); ++fi) if( !(*fi).IsD() ) - { - // For each triangle write the normal, the three coords and a short set to zero - p.Import(vcg::NormalizedNormal((*fi).V(0)->P(), (*fi).V(1)->P(), (*fi).V(2)->P())); - fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]); - fprintf(fp," outer loop\n"); - for(int k=0;k<3;++k){ - p.Import((*fi).V(k)->P()); - fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]); - } - fprintf(fp," endloop\n"); - fprintf(fp," endfacet\n"); - } - fprintf(fp,"endsolid vcg\n"); - } - fclose(fp); - return true; -} - -//@} +/**************************************************************************** +* 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.3 2004/02/19 15:28:01 ponchio +*** empty log message *** + +Revision 1.2 2004/02/13 02:18:57 cignoni +Edited Comments and GPL license + + +****************************************************************************/ + +#ifndef __VCGLIB_EXPORT_STL +#define __VCGLIB_EXPORT_STL + +#include + +namespace vcg { +namespace tri { +namespace io { + +template +class ExporterSTL +{ +public: +static bool Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0) +{ + typedef typename SaveMeshType::FaceIterator FaceIterator; + FILE *fp; + + fp = fopen(filename,"wb"); + if(fp==0) + return false; + + if(binary) + { + // Write Header + char *header="VCG "; + if(objectname) strncpy(header,objectname,80); + fwrite(header,80,1,fp); + // write number of facets + fwrite(&m.fn,1,sizeof(int),fp); + Point3f p; + unsigned short attributes=0; + + FaceIterator fi; + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) + { + // For each triangle write the normal, the three coords and a short set to zero + p.Import(vcg::NormalizedNormal((*fi).V(0)->P(), (*fi).V(1)->P(), (*fi).V(2)->P())); + fwrite(p.V(),3,sizeof(float),fp); + + for(int k=0;k<3;++k){ + p.Import((*fi).V(k)->P()); + fwrite(p.V(),3,sizeof(float),fp); + } + fwrite(&attributes,1,sizeof(short),fp); + } + } + else + { + if(objectname) fprintf(fp,"solid %s\n",objectname); + else fprintf(fp,"solid vcg\n"); + + Point3f p; + FaceIterator fi; + for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) + { + // For each triangle write the normal, the three coords and a short set to zero + p.Import(vcg::NormalizedNormal((*fi).V(0)->P(), (*fi).V(1)->P(), (*fi).V(2)->P())); + fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]); + fprintf(fp," outer loop\n"); + for(int k=0;k<3;++k){ + p.Import((*fi).V(k)->P()); + fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]); + } + fprintf(fp," endloop\n"); + fprintf(fp," endfacet\n"); + } + fprintf(fp,"endsolid vcg\n"); + } + fclose(fp); + return true; +} +}; // end class + +} // end Namespace tri +} // end Namespace io +} // end Namespace vcg + + +//@} + +#endif \ No newline at end of file