From b692bdb4209f07831723da1e870749995258b4d7 Mon Sep 17 00:00:00 2001 From: cignoni Date: Wed, 18 Feb 2004 12:28:52 +0000 Subject: [PATCH] Initial commit --- wrap/io_trimesh/export_smf.h | 57 +++++++++++++++++++++++++ wrap/io_trimesh/export_stl.h | 83 ++++++++++++++++++++++++++++++++++++ wrap/io_trimesh/import_smf.h | 78 +++++++++++++++++++++++++++++++++ wrap/io_trimesh/import_stl.h | 50 ++++++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 wrap/io_trimesh/export_smf.h create mode 100644 wrap/io_trimesh/export_stl.h create mode 100644 wrap/io_trimesh/import_smf.h create mode 100644 wrap/io_trimesh/import_stl.h diff --git a/wrap/io_trimesh/export_smf.h b/wrap/io_trimesh/export_smf.h new file mode 100644 index 00000000..4dc47279 --- /dev/null +++ b/wrap/io_trimesh/export_smf.h @@ -0,0 +1,57 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + + +/** Function to save to Smf format file. + @param filename Name of the new Smf file +*/ +void Save_Smf(const char * filename) +{ + FILE *fp; + fp = fopen(filename,"wb"); + fprintf(fp,"#SMF \n" ); + + face_iterator fi; + vertex_iterator vi; + map index; + int ind; + + for(ind=1,vi=vert.begin(); vi!=vert.end(); ++vi,++ind) + { + fprintf(fp,"v " ); + fprintf(fp,"%f%s",(*vi).P()[0]," " ); + fprintf(fp,"%f%s",(*vi).P()[1]," " ); + fprintf(fp,"%f%s",(*vi).P()[2],"\n"); + index[&*vi] = ind; + } + + for (fi=face.begin(); fi!=face.end(); ++fi) + { + fprintf(fp,"%s","f "); + for (int j = 0; j < 3; j++) + fprintf(fp,"%i%s",index[(*fi).V(j)]," "); + fprintf(fp,"%s","\n"); + } + + fclose(fp); +} diff --git a/wrap/io_trimesh/export_stl.h b/wrap/io_trimesh/export_stl.h new file mode 100644 index 00000000..a04e9d7e --- /dev/null +++ b/wrap/io_trimesh/export_stl.h @@ -0,0 +1,83 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + +bool Save_STL(const char * filename , bool binary =true, const char *objectname=0) +{ + 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(&fn,1,sizeof(int),fp); + face_iterator fi; + Point3f p; + unsigned short attributes=0; + + 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())); + 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; +} + +//@} diff --git a/wrap/io_trimesh/import_smf.h b/wrap/io_trimesh/import_smf.h new file mode 100644 index 00000000..969ef864 --- /dev/null +++ b/wrap/io_trimesh/import_smf.h @@ -0,0 +1,78 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ +/** Function to load a Smf file + @param filename Name of Smf file +*/ +int Load_Smf( const char * filename ) +{ + char buf[1024]; + FILE *fp; + float x,y,z; + bool one = true; + map mv; + + fp = fopen(filename,"r"); + + if(!fp) return -1; + + vertex_type v; + v.Supervisor_Flags() = 0; + face_type f; + f.Supervisor_Flags() = 0; + + while( fgets(buf,1024,fp) ) + { + char *vf, *comm_pt; + + if((comm_pt = strstr(buf,"#")) != NULL) + *comm_pt = '\0'; + if( (vf = strstr(buf,"v")) != NULL ) + { + sscanf(vf+1,"%f %f %f", &x, &y, &z); + v.P()[0] = x; + v.P()[1] = y; + v.P()[2] = z; + vert.push_back(v); + } + else if( (vf = strstr(buf,"f")) != NULL) + { + if(one) + { + vertex_iterator vi; + int ind; + for(ind=1,vi=vert.begin(); vi!=vert.end(); ++vi,++ind) + mv[ind]=&*vi; + one = false; + } + int v1,v2,v3; + sscanf(vf+1,"%d %d %d", &v1, &v2, &v3); + f.V(0) = mv[v1]; + f.V(1) = mv[v2]; + f.V(2) = mv[v3]; + face.push_back(f); + } + } + vn = vert.size(); + fn = face.size(); + return 0; +} \ No newline at end of file diff --git a/wrap/io_trimesh/import_stl.h b/wrap/io_trimesh/import_stl.h new file mode 100644 index 00000000..ac073ab6 --- /dev/null +++ b/wrap/io_trimesh/import_stl.h @@ -0,0 +1,50 @@ +/**************************************************************************** +* 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. * +* * +****************************************************************************/ + +This folders contains most common FACE configuration files +The name of the file specify the members that are added to the vertex class +The name is a sequence of letters, in strict alphabetical order. +The possible admitted letters pairs are + +FA - face-face adjacency +FC - Per-Face Color +FN - Per-Face Normal +FQ - Per-Face Quality +VA - Vertex-face adjacency +WC - Per-Wedge Color +WN - Per-Wedge Normal +WQ - Per-Wedge Quality +WT - Per-Wedge Texture Coords +SA - Shared Vertex-Face and Face-Face Adjacency +RT - Data for Optimized Point-Face Distance and Ray-Tracing Stuff + +E.g. + +#include + +generate a type +VertexFAFNWC + +that can store F-F adjacency, Per face normal color and per wedge color. + +