From d327e529a9945f578de397fe25e17220b346e2ce Mon Sep 17 00:00:00 2001 From: cignoni Date: Fri, 12 Mar 2004 21:42:52 +0000 Subject: [PATCH] First working version! --- wrap/io_trimesh/import_stl.h | 238 +++++++++++++++++++++++++++-------- 1 file changed, 188 insertions(+), 50 deletions(-) diff --git a/wrap/io_trimesh/import_stl.h b/wrap/io_trimesh/import_stl.h index 0e9bdc96..a06ebb0f 100644 --- a/wrap/io_trimesh/import_stl.h +++ b/wrap/io_trimesh/import_stl.h @@ -1,50 +1,188 @@ -/**************************************************************************** -* 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. - - +/**************************************************************************** +* 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 $ +****************************************************************************/ + +#ifndef __VCGLIB_IMPORT_STL +#define __VCGLIB_IMPORT_STL + +#include + +namespace vcg { +namespace tri { +namespace io { + +/** +This class encapsulate a filter for importing stl (stereolitograpy) meshes. +The stl format is quite simple and rather un-flexible. It just stores, in ascii or binary the, unindexed, geometry of the faces. +warning this code assume little endian (PC) architecture!!! +*/ +template +class ImporterSTL +{ +public: + +typedef typename OpenMeshType::VertexPointer VertexPointer; +typedef typename OpenMeshType::ScalarType ScalarType; +typedef typename OpenMeshType::VertexType VertexType; +typedef typename OpenMeshType::FaceType FaceType; +typedef typename OpenMeshType::VertexIterator VertexIterator; +typedef typename OpenMeshType::FaceIterator FaceIterator; + +// if it is binary there are 80 char of comment, the number fn of faces and then exactly fn*4*3 bytes. + +enum {STL_LABEL_SIZE=80}; + +class STLFacet +{ +public: + Point3f n; + Point3f v[3]; +// short attr; +}; + +static int Open( OpenMeshType &m, const char * filename, CallBackPos *cb=0) +{ + FILE *fp; + bool binary=false; + fp = fopen(filename, "r"); + if(fp == NULL) + { + return 0; + } + + /* Find size of file */ + fseek(fp, 0, SEEK_END); + int file_size = ftell(fp); + int facenum; + /* Check for binary or ASCII file */ + fseek(fp, STL_LABEL_SIZE, SEEK_SET); + fread(&facenum, sizeof(int), 1, fp); + int expected_file_size=STL_LABEL_SIZE + 4 + (sizeof(short)+sizeof(STLFacet) )*facenum ; + if(file_size == expected_file_size) binary = true; + unsigned char tmpbuf[128]; + fread(tmpbuf,sizeof(tmpbuf),1,fp); + for(int i = 0; i < sizeof(tmpbuf); i++) + { + if(tmpbuf[i] > 127) + { + binary=true; + break; + } + } + // Now we know if the stl file is ascii or binary. + fclose(fp); + if(binary) return OpenBinary(m,filename,cb); + else return OpenAscii(m,filename,cb); +} + +static int OpenBinary( OpenMeshType &m, const char * filename, CallBackPos *cb=0) +{ + FILE *fp; + fp = fopen(filename, "rb"); + if(fp == NULL) + { + return 0; + } + + int facenum; + fseek(fp, STL_LABEL_SIZE, SEEK_SET); + fread(&facenum, sizeof(int), 1, fp); + + m.Clear(); + FaceIterator fi=Allocator::AddFaces(m,facenum); + VertexIterator vi=Allocator::AddVertices(m,facenum*3); + // For each triangle read the normal, the three coords and a short set to zero + for(int i=0;i::AddFaces(m,1); + VertexIterator vi=Allocator::AddVertices(m,3); + for(int k=0;k<3;++k) + { + (*vi).P().Import(f.v[k]); + (*fi).V(k)=&*vi; + ++vi; + } + } + fclose(fp); + return 1; + } +}; // end class +} // end Namespace tri +} // end Namespace io +} // end Namespace vcg + + +//@} + +#endif \ No newline at end of file