fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / STL / OSGSTLSceneFileType.cpp
blob55e3c2fa02c8d4d1761cb02c450fc9f121640276
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //-------------------------------
40 // Includes
41 //-------------------------------
43 #include <stdlib.h>
44 #include <stdio.h>
46 #include "OSGConfig.h"
48 #include <iostream>
49 #include <fstream>
51 #include "OSGLog.h"
53 #include "OSGNode.h"
54 #include "OSGGeometry.h"
55 #include "OSGSimpleMaterial.h"
56 #include "OSGTypedGeoIntegralProperty.h"
57 #include "OSGTypedGeoVectorProperty.h"
59 #include "OSGSTLSceneFileType.h"
61 OSG_USING_NAMESPACE
63 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
64 #pragma warning (disable : 383)
65 #endif
68 /*! \class OSG::STLSceneFileType
69 \ingroup GrpSystemFileIO
73 /*****************************
74 * Types
75 *****************************/
76 // Static Class Varible implementations:
78 const Char8 *STLSceneFileType::_suffixA[] = {"stl", "stla", "stlb"};
81 STLSceneFileType STLSceneFileType::_the(_suffixA,
82 sizeof(_suffixA),
83 false,
84 10,
85 SceneFileType::OSG_READ_SUPPORTED);
87 /*****************************
88 * Classvariables
89 *****************************/
92 /********************************
93 * Class methodes
94 *******************************/
97 /*******************************
98 *public
99 *******************************/
101 //----------------------------
102 // Function name: read
103 //----------------------------
105 //Parameters:
106 //p: Scene &image, const char *fileName
107 //GlobalVars:
108 //g:
109 //Returns:
110 //r:bool
111 // Caution
112 //c:
113 //Assumations:
114 //a:
115 //Describtions:
116 //d: read the image from the given file
117 //SeeAlso:
118 //s:
120 //------------------------------
122 #ifdef __sgi
123 #pragma set woff 1209
124 #endif
126 NodeTransitPtr STLSceneFileType::read( std::istream &is,
127 const Char8 *fileNameOrExtension,
128 Resolver ) const
131 NodeTransitPtr root;
132 GeometryUnrecPtr geo;
133 GeoPnt3fPropertyUnrecPtr points;
134 GeoVec3fPropertyUnrecPtr normals;
135 GeoIntegralPropertyUnrecPtr index;
136 GeoIntegralPropertyUnrecPtr lens;
137 GeoIntegralPropertyUnrecPtr type;
139 STLFaceList theFaces;
140 std::string theName;
141 bool isAscii = isASCII(is, fileNameOrExtension);
143 if(isAscii)
145 if(!readASCII(is, theFaces, theName))
146 return root;
148 else
150 if(!readBinary(is, theFaces, theName))
151 return root;
153 if(!theFaces.empty())
155 geo = Geometry::create();
157 points = GeoPnt3fProperty::create();
158 geo->setPositions(points);
159 normals = GeoVec3fProperty::create();
160 geo->setNormals(normals);
161 lens = GeoUInt32Property::create();
162 geo->setLengths(lens);
163 lens->push_back(theFaces.size()*3);
164 type = GeoUInt8Property::create();
165 geo->setTypes(type);
166 type->push_back(GL_TRIANGLES);
168 root = Node::create();
169 //setName(root, theName.c_str());
171 GeoPnt3fProperty::StoredFieldType *fPos = points ->editFieldPtr();
172 GeoVec3fProperty::StoredFieldType *fNorm = normals->editFieldPtr();
174 STLFaceListIterator actIt, endIt;
175 actIt = theFaces.begin();
176 endIt = theFaces.end();
178 while(actIt != endIt)
180 fPos ->push_back(Pnt3f(actIt->v1x, actIt->v1y, actIt->v1z));
181 fPos ->push_back(Pnt3f(actIt->v2x, actIt->v2y, actIt->v2z));
182 fPos ->push_back(Pnt3f(actIt->v3x, actIt->v3y, actIt->v3z));
184 fNorm->push_back(Vec3f(actIt->nx, actIt->ny, actIt->nz));
185 fNorm->push_back(Vec3f(actIt->nx, actIt->ny, actIt->nz));
186 fNorm->push_back(Vec3f(actIt->nx, actIt->ny, actIt->nz));
188 ++actIt;
191 geo->setTypes (type);
192 geo->setLengths (lens);
193 geo->setPositions(points);
194 geo->setNormals (normals);
196 geo->setMaterial(getDefaultMaterial());
198 root->setCore(geo);
201 commitChanges();
203 return root;
206 bool STLSceneFileType::isASCII(std::istream &is, const Char8* fileNameOrExtension) const
208 std::string theAbsName = fileNameOrExtension;
209 std::string theExtension;
210 int nameLength = int(theAbsName.length());
211 SizeT separator = theAbsName.rfind(".", nameLength);
212 theExtension = theAbsName.substr(separator+1, nameLength-separator-1);
214 for (UInt32 i = 0; i < theExtension.length(); i++)
215 theExtension[i] = tolower(theExtension[i]);
217 if (theExtension.compare("stla") == 0)
218 return true;
219 else if (theExtension.compare("stlb") == 0)
220 return false;
221 else
223 // we have to check if our file start with "solid"
224 std::string theCheck;
225 is >> theCheck;
226 for (UInt32 i = 0; i< theCheck.length(); i++)
227 theCheck[i] = tolower(theCheck[i]);
229 is.seekg( 0, std::ios_base::beg );
231 return (theCheck.compare("solid") == 0);
235 bool STLSceneFileType::readASCII(std::istream &is, STLFaceList& theFaces, std::string& theName) const
237 FDEBUG(("STLSceneFileType::readASCII:\n"));
239 char token[9];
240 int finished = 0;
241 UInt32 i;
243 if(!is)
244 return false;
246 while ((finished == 0) && (! (is.eof())))
248 //SceneFileHandler::the().updateReadProgress();
249 memset(token, ' ', 9);
250 is >> token;
251 for (i = 0; i < strlen(token); i++)
253 token[i] = tolower(token[i]);
255 if (strcmp(token, "solid") == 0)
257 char tmp = is.get();
258 if (tmp == ' ')
259 is >> theName;
261 else if (strcmp(token, "facet") == 0)
263 STLFace tmpFace;
264 is >> token >> tmpFace.nx >> tmpFace.ny >> tmpFace.nz;
265 is >> token >> token;// skip "outer loop"
266 is >> token >> tmpFace.v1x >> tmpFace.v1y >> tmpFace.v1z;
267 is >> token >> tmpFace.v2x >> tmpFace.v2y >> tmpFace.v2z;
268 is >> token >> tmpFace.v3x >> tmpFace.v3y >> tmpFace.v3z;
269 is >> token;// skip "endloop"
270 is >> token;// skip "endfacet"
271 theFaces.push_back(tmpFace);
273 else if (strcmp(token, "endsolid") == 0)
275 finished = 1;
276 return true;
279 return false;
282 bool STLSceneFileType::readBinary(std::istream &is, STLFaceList& theFaces, std::string& theName) const
284 FDEBUG(("STLSceneFileType::readBinary:\n"));
286 if(!is)
287 return false;
289 bool bigEndian = osgIsBigEndian();
290 char name[80] = "";
291 UInt32 facetCount = 0;
292 UInt32 facet = 0;
294 is.read(&name[0], 80); // read the header
295 theName = name;
296 facetCount = readUInt32(is, bigEndian);
298 for(; facet < facetCount && !is.eof(); ++facet)
300 //SceneFileHandler::the().updateReadProgress();
301 // a binary facet block has 50 bytes
302 STLFace tmpFace;
304 tmpFace.nx = readFloat(is, bigEndian);
305 tmpFace.ny = readFloat(is, bigEndian);
306 tmpFace.nz = readFloat(is, bigEndian);
308 tmpFace.v1x = readFloat(is, bigEndian);
309 tmpFace.v1y = readFloat(is, bigEndian);
310 tmpFace.v1z = readFloat(is, bigEndian);
312 tmpFace.v2x = readFloat(is, bigEndian);
313 tmpFace.v2y = readFloat(is, bigEndian);
314 tmpFace.v2z = readFloat(is, bigEndian);
316 tmpFace.v3x = readFloat(is, bigEndian);
317 tmpFace.v3y = readFloat(is, bigEndian);
318 tmpFace.v3z = readFloat(is, bigEndian);
320 theFaces.push_back(tmpFace);
321 char buffer[2]; // offset filler
322 is.read (&buffer[0], 2);
325 if(facet < facetCount)
327 SWARNING << "STLSceneFileType::readBinary: Encountered EOF after "
328 << "reading [" << facet << "] facets, was expecting ["
329 << facetCount << "]."
330 << std::endl;
333 return true;
336 Real32 STLSceneFileType::readFloat(std::istream& is, bool bigEndian) const
338 Real32 result = 0.f;
340 if(bigEndian)
342 UInt32 buffer;
344 is.read(reinterpret_cast<char *>(&buffer), sizeof(Real32));
345 result = osgNetToHostFP(buffer);
347 else
349 is.read(reinterpret_cast<char *>(&result), sizeof(Real32));
352 return result;
355 UInt32 STLSceneFileType::readUInt32(std::istream &is, bool bigEndian) const
357 UInt32 result = 0;
358 char buffer[4];
360 is.read(&buffer[0], 4);
361 std::memcpy(reinterpret_cast<char *>(&result), buffer, 4);
363 if(bigEndian)
364 result = osgNetToHost(result);
366 return result;
369 #ifdef __sgi
370 #pragma reset woff 1209
371 #endif
373 /******************************
374 *protected
375 ******************************/
378 /******************************
379 *private
380 ******************************/
383 /***************************
384 *instance methodes
385 ***************************/
388 /***************************
389 *public
390 ***************************/
393 /**constructors & destructors**/
396 STLSceneFileType::STLSceneFileType(const Char8 *suffixArray[],
397 UInt16 suffixByteCount,
398 bool override,
399 UInt32 overridePriority,
400 UInt32 flags) :
401 SceneFileType(suffixArray,
402 suffixByteCount,
403 override,
404 overridePriority,
405 flags)
409 STLSceneFileType &STLSceneFileType::the(void)
411 return _the;
414 const Char8 *STLSceneFileType::getName(void) const
416 return "Stereolithography Geometry";
420 STLSceneFileType::~STLSceneFileType (void )
422 return;