1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //-------------------------------
41 //-------------------------------
46 #include "OSGConfig.h"
54 #include "OSGGeometry.h"
55 #include "OSGSimpleMaterial.h"
56 #include "OSGTypedGeoIntegralProperty.h"
57 #include "OSGTypedGeoVectorProperty.h"
59 #include "OSGSTLSceneFileType.h"
63 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
64 #pragma warning (disable : 383)
68 /*! \class OSG::STLSceneFileType
69 \ingroup GrpSystemFileIO
73 /*****************************
75 *****************************/
76 // Static Class Varible implementations:
78 const Char8
*STLSceneFileType::_suffixA
[] = {"stl", "stla", "stlb"};
81 STLSceneFileType
STLSceneFileType::_the(_suffixA
,
85 SceneFileType::OSG_READ_SUPPORTED
);
87 /*****************************
89 *****************************/
92 /********************************
94 *******************************/
97 /*******************************
99 *******************************/
101 //----------------------------
102 // Function name: read
103 //----------------------------
106 //p: Scene &image, const char *fileName
116 //d: read the image from the given file
120 //------------------------------
123 #pragma set woff 1209
126 NodeTransitPtr
STLSceneFileType::read( std::istream
&is
,
127 const Char8
*fileNameOrExtension
,
132 GeometryUnrecPtr geo
;
133 GeoPnt3fPropertyUnrecPtr points
;
134 GeoVec3fPropertyUnrecPtr normals
;
135 GeoIntegralPropertyUnrecPtr index
;
136 GeoIntegralPropertyUnrecPtr lens
;
137 GeoIntegralPropertyUnrecPtr type
;
139 STLFaceList theFaces
;
141 bool isAscii
= isASCII(is
, fileNameOrExtension
);
145 if(!readASCII(is
, theFaces
, theName
))
150 if(!readBinary(is
, theFaces
, theName
))
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();
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
));
191 geo
->setTypes (type
);
192 geo
->setLengths (lens
);
193 geo
->setPositions(points
);
194 geo
->setNormals (normals
);
196 geo
->setMaterial(getDefaultMaterial());
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)
219 else if (theExtension
.compare("stlb") == 0)
223 // we have to check if our file start with "solid"
224 std::string 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"));
246 while ((finished
== 0) && (! (is
.eof())))
248 //SceneFileHandler::the().updateReadProgress();
249 memset(token
, ' ', 9);
251 for (i
= 0; i
< strlen(token
); i
++)
253 token
[i
] = tolower(token
[i
]);
255 if (strcmp(token
, "solid") == 0)
261 else if (strcmp(token
, "facet") == 0)
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)
282 bool STLSceneFileType::readBinary(std::istream
&is
, STLFaceList
& theFaces
, std::string
& theName
) const
284 FDEBUG(("STLSceneFileType::readBinary:\n"));
289 bool bigEndian
= osgIsBigEndian();
291 UInt32 facetCount
= 0;
294 is
.read(&name
[0], 80); // read the header
296 facetCount
= readUInt32(is
, bigEndian
);
298 for(; facet
< facetCount
&& !is
.eof(); ++facet
)
300 //SceneFileHandler::the().updateReadProgress();
301 // a binary facet block has 50 bytes
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
<< "]."
336 Real32
STLSceneFileType::readFloat(std::istream
& is
, bool bigEndian
) const
344 is
.read(reinterpret_cast<char *>(&buffer
), sizeof(Real32
));
345 result
= osgNetToHostFP(buffer
);
349 is
.read(reinterpret_cast<char *>(&result
), sizeof(Real32
));
355 UInt32
STLSceneFileType::readUInt32(std::istream
&is
, bool bigEndian
) const
360 is
.read(&buffer
[0], 4);
361 std::memcpy(reinterpret_cast<char *>(&result
), buffer
, 4);
364 result
= osgNetToHost(result
);
370 #pragma reset woff 1209
373 /******************************
375 ******************************/
378 /******************************
380 ******************************/
383 /***************************
385 ***************************/
388 /***************************
390 ***************************/
393 /**constructors & destructors**/
396 STLSceneFileType::STLSceneFileType(const Char8
*suffixArray
[],
397 UInt16 suffixByteCount
,
399 UInt32 overridePriority
,
401 SceneFileType(suffixArray
,
409 STLSceneFileType
&STLSceneFileType::the(void)
414 const Char8
*STLSceneFileType::getName(void) const
416 return "Stereolithography Geometry";
420 STLSceneFileType::~STLSceneFileType (void )