fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OFF / OSGOFFSceneFileType.cpp
blob5ed9e99fba530b2fce0043e72ee327d6fefa3436
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 #include <stdlib.h>
40 #include <stdio.h>
42 #include "OSGConfig.h"
44 #include "OSGGL.h"
46 #include <iostream>
47 #include <fstream>
49 #include <vector>
51 #include "OSGLog.h"
53 #include "OSGNode.h"
54 #include "OSGGeometry.h"
55 #include "OSGTypedGeoVectorProperty.h"
56 #include "OSGTypedGeoIntegralProperty.h"
57 #include "OSGGeoFunctions.h"
58 #include "OSGSimpleMaterial.h"
59 #include "OSGSceneFileHandler.h"
61 #include "OSGOFFSceneFileType.h"
63 OSG_USING_NAMESPACE
66 /*! \class OSG::OFFSceneFileType
67 \ingroup GrpSystemFileIO
71 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
72 #pragma warning (disable : 383)
73 #endif
75 /*****************************
76 * Types
77 *****************************/
78 // Static Class Varible implementations:
79 const Char8 *OFFSceneFileType:: _suffixA[] = { "off" };
81 OFFSceneFileType OFFSceneFileType:: _the(_suffixA,
82 sizeof(_suffixA),
83 false,
84 10,
85 SceneFileType::OSG_READ_SUPPORTED);
87 /*****************************
88 * Classvariables
89 *****************************/
91 /********************************
92 * Class methodes
93 *******************************/
95 /*******************************
96 *public
97 *******************************/
99 //----------------------------
100 // Function name: read
101 //----------------------------
103 //Parameters:
104 //p: Scene &image, const char *fileName
105 //GlobalVars:
106 //g:
107 //Returns:
108 //r:bool
109 // Caution
110 //c:
111 //Assumations:
112 //a:
113 //Describtions:
114 //d: read the image from the given file
115 //SeeAlso:
116 //s:
118 //------------------------------
119 NodeTransitPtr OFFSceneFileType::read( std::istream &is,
120 const Char8 *,
121 Resolver ) const
123 typedef std::vector<int> Face;
125 std::vector<Face> faceVec;
127 char head[256];
129 NodeTransitPtr root(NULL);
130 GeometryUnrecPtr geo = NULL;
132 Pnt3f point;
133 Vec3f norm;
134 Color4f color;
135 Vec2f texcoord;
137 GeoPnt3fPropertyUnrecPtr points = NULL;
138 GeoUInt32PropertyUnrecPtr index = NULL;
139 GeoUInt32PropertyUnrecPtr lens = NULL;
140 GeoUInt8PropertyUnrecPtr type = NULL;
141 GeoVec3fPropertyUnrecPtr norms = NULL;
142 GeoColor4fPropertyUnrecPtr colors = NULL;
143 GeoVec2fPropertyUnrecPtr texcoords = NULL;
144 SimpleMaterialUnrecPtr mat = NULL;
147 Int32 i, j, k, n, vN, fN, pType;
148 Int32 triCount = 0, vertexCount, faceCount, edgeCount;
149 Real32 x, y, z, a;
150 bool hasNormals = false;
151 bool hasColors = false;
152 bool hasTexCoords = false;
153 bool has4DimPoints = false;
155 if(!is)
157 return root;
160 is >> head >> vertexCount >> faceCount >> edgeCount;
162 FDEBUG(("OFF Head/vertexCount/faceCount: %s/%d/%d\n", head,
163 vertexCount, faceCount));
165 if(!vertexCount || !faceCount)
167 return root;
170 if(strstr(head, "ST"))
172 hasTexCoords = true;
175 if(strstr(head, "C"))
177 hasColors = true;
180 if(strstr(head, "N"))
182 hasNormals = true;
185 if(strstr(head, "4"))
187 has4DimPoints = true;
190 if(strstr(head, "nOFF"))
192 int ndim;
194 is >> ndim;
196 if(ndim != 3)
198 FWARNING(("OFFSceneFileType::read: nOFF with ndim != 3 "
199 "not supported.\n"));
201 return root;
205 if(has4DimPoints)
207 FWARNING(("OFFSceneFileType::read: 4D points not supported.\n"));
208 return root;
212 //-------------------------------------------------------------------
213 // create the OSG objects
214 root = Node ::create();
215 geo = Geometry ::create();
216 points = GeoPnt3fProperty ::create();
217 index = GeoUInt32Property::create();
218 lens = GeoUInt32Property::create();
219 type = GeoUInt8Property ::create();
220 mat = SimpleMaterial ::create();
222 if (hasNormals)
223 norms = GeoVec3fProperty::create();
224 if (hasColors)
225 colors = GeoColor4fProperty::create();
226 if (hasTexCoords)
227 texcoords = GeoVec2fProperty::create();
229 mat->setDiffuse(Color3f(0.42f, 0.42f, 0.52f));
230 mat->setSpecular(Color3f(1, 1, 1));
231 mat->setShininess(20);
233 root->setCore(geo);
235 geo->setProperty(points, Geometry::PositionsIndex);
236 geo->setIndex (index, Geometry::PositionsIndex);
238 geo->setLengths(lens);
239 geo->setTypes (type);
241 geo->setMaterial(mat);
243 if(hasNormals)
245 geo->setProperty(norms, Geometry::NormalsIndex);
246 geo->setIndex (index, Geometry::NormalsIndex);
248 if(hasColors)
250 geo->setProperty(colors, Geometry::ColorsIndex);
251 geo->setIndex (index, Geometry::ColorsIndex);
253 if(hasTexCoords)
255 geo->setProperty(texcoords, Geometry::TexCoordsIndex);
256 geo->setIndex (index, Geometry::TexCoordsIndex);
259 //-------------------------------------------------------------------
260 // read/set the points
262 for(i = 0; (!is.eof()) && (i < vertexCount); i++)
264 SceneFileHandler::the()->updateReadProgress();
266 is >> x >> y >> z;
268 point.setValues(Real32(x), Real32(y), Real32(z));
269 points->push_back(point);
271 if(hasNormals)
273 is >> x >> y >> z;
274 norm.setValues(Real32(x), Real32(y), Real32(z));
275 norms->push_back(norm);
278 if(hasColors)
280 is >> x >> y >> z >> a;
281 color.setValuesRGBA(Real32(x),
282 Real32(y),
283 Real32(z),
284 Real32(a));
285 colors->editField().push_back(color);
288 if(hasTexCoords)
290 is >> x >> y;
291 texcoord.setValues(Real32(x), Real32(y));
292 texcoords->push_back(texcoord);
296 //-------------------------------------------------------------------
297 // read the faces
298 // TODO; should we 'reserve' some index mem (3,4,..) ?
299 faceVec.resize(faceCount);
300 triCount = 0;
302 for(i = 0; (!is.eof()) && (i < faceCount); i++)
304 is >> n;
305 if(n >= 0)
307 triCount += n - 2;
308 for(j = 0; (!is.eof()) && (j < n); j++)
310 is >> k;
311 if((k >= 0) && (k < vertexCount))
312 faceVec[i].push_back(k);
313 else
315 FFATAL(("Invalid vertex index %d in face %d\n", k, i));
318 is.ignore(1000, '\n');
320 else
322 FFATAL(("Invalid face vec num %d\n", n));
326 //-------------------------------------------------------------------
327 // set the faces
328 for(i = 3; i <= 5; i++)
330 n = 0;
332 for(j = 0; j < faceCount; j++)
334 fN = UInt32(faceVec[j].size());
336 if(fN >= 5)
337 fN = 5;
339 if(fN == i)
341 vN = UInt32(faceVec[j].size());
342 n += vN;
344 for(k = 0; k < vN; ++k)
346 index->editFieldPtr()->push_back(faceVec[j][k]);
349 if(i == 5)
351 lens->push_back(n);
352 type->push_back(GL_POLYGON);
357 if(n)
359 switch(i)
361 case 3:
362 pType = GL_TRIANGLES;
363 break;
364 case 4:
365 pType = GL_QUADS;
366 break;
367 default:
368 pType = 0;
369 break;
372 if(pType)
374 lens->push_back(n);
375 type->push_back(pType);
380 FNOTICE(("Number of triangle read: %d\n", triCount));
382 createSharedIndex(geo);
384 if(!hasNormals)
385 calcVertexNormals(geo);
387 SceneFileHandler::the()->updateReadProgress(100);
389 commitChanges();
391 return root;
394 /******************************
395 *protected
396 ******************************/
398 /******************************
399 *private
400 ******************************/
402 /***************************
403 *instance methodes
404 ***************************/
406 /***************************
407 *public
408 ***************************/
410 /**constructors & destructors**/
412 //----------------------------
413 // Function name: OFFSceneFileType
414 //----------------------------
416 //Parameters:
417 //p: const char *suffixArray[], UInit16 suffixByteCount
418 //GlobalVars:
419 //g:
420 //Returns:
421 //r:
422 // Caution
423 //c:
424 //Assumations:
425 //a:
426 //Describtions:
427 //d: Default Constructor
428 //SeeAlso:
429 //s:
431 //------------------------------
432 OFFSceneFileType::OFFSceneFileType(const Char8 *suffixArray[],
433 UInt16 suffixByteCount,
434 bool override,
435 UInt32 overridePriority,
436 UInt32 flags ) :
437 SceneFileType(suffixArray,
438 suffixByteCount,
439 override,
440 overridePriority,
441 flags)
445 //----------------------------
446 // Function name: OFFSceneFileType
447 //----------------------------
449 //Parameters:
450 //p: const OFFSceneFileType &obj
451 //GlobalVars:
452 //g:
453 //Returns:
454 //r:
455 // Caution
456 //c:
457 //Assumations:
458 //a:
459 //Describtions:
460 //d: Copy Constructor
461 //SeeAlso:
462 //s:
464 //------------------------------
466 OFFSceneFileType::OFFSceneFileType(const OFFSceneFileType &obj) :
467 SceneFileType(obj)
471 //----------------------------
472 // Function name: ~OFFSceneFileType
473 //----------------------------
475 //Parameters:
476 //p: void
477 //GlobalVars:
478 //g:
479 //Returns:
480 //r:
481 // Caution
482 //c:
483 //Assumations:
484 //a:
485 //Describtions:
486 //d: Destructor
487 //SeeAlso:
488 //s:
490 //------------------------------
492 OFFSceneFileType &OFFSceneFileType::the(void)
494 return _the;
497 OFFSceneFileType::~OFFSceneFileType(void)
499 return;
502 const Char8 *OFFSceneFileType::getName(void) const
504 return "Geomview Geometry";