fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / PLY / OSGPLYSceneFileType.cpp
blob083c7414ed7ae3cb54a8f993b38c4187063093d8
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 * *
38 \*---------------------------------------------------------------------------*/
40 #include <stdlib.h>
41 #include <stdio.h>
43 #include "OSGConfig.h"
45 #include "OSGGL.h"
47 #include <iostream>
48 #include <fstream>
50 #include <vector>
52 #include "OSGLog.h"
54 #include "OSGNode.h"
55 #include "OSGGeometry.h"
56 #include "OSGGeoFunctions.h"
57 #include "OSGTypedGeoVectorProperty.h"
58 #include "OSGTypedGeoIntegralProperty.h"
60 #include "OSGPLYSceneFileType.h"
61 #include "OSGply.h"
63 OSG_USING_NAMESPACE
66 /*! \class OSG::PLYSceneFileType
67 \ingroup GrpSystemFileIO
70 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
71 #pragma warning (disable : 383)SG
72 #endif
74 const Char8* PLYSceneFileType::_suffixA[] = { "ply" };
76 PLYSceneFileType PLYSceneFileType::_the(_suffixA,
77 sizeof(_suffixA),
78 false,
79 10,
80 SceneFileType::OSG_READ_SUPPORTED);
83 PLYSceneFileType &PLYSceneFileType::the(void)
85 return _the;
88 const Char8 *PLYSceneFileType::getName(void) const
90 return "PLY"; // ?
93 struct Vertex {
94 float x;
95 float y;
96 float z;
97 unsigned char r;
98 unsigned char g;
99 unsigned char b;
102 struct Face {
103 unsigned char nverts;
104 int* verts;
107 static PlyProperty vert_props[] = { /* list of property information for a vertex */
108 #ifndef WIN32
109 {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
110 {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
111 {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0},
112 {"red", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,r), 0, 0, 0, 0},
113 {"green", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,g), 0, 0, 0, 0},
114 {"blue", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,b), 0, 0, 0, 0}
115 #else
116 PlyProperty("x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0),
117 PlyProperty("y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0),
118 PlyProperty("z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0),
119 PlyProperty("red", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,r), 0, 0, 0, 0),
120 PlyProperty("green", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,g), 0, 0, 0, 0),
121 PlyProperty("blue", PLY_UCHAR, PLY_UCHAR, offsetof(Vertex,b), 0, 0, 0, 0)
122 #endif
125 static PlyProperty face_props[] = { /* list of property information for a vertex */
126 #ifndef WIN32
127 {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
128 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)}
129 #else
130 PlyProperty("vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
131 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts))
132 #endif
136 NodeTransitPtr PLYSceneFileType::read(
137 std::istream &is,
138 const Char8 *, /*fileNameOrExtension*/
139 Resolver resolver) const
141 std::vector<std::string> elems;
142 PlyFile* ply = ply_read(&is, elems);
143 if (!ply)
145 return NodeTransitPtr(NULL);
148 GeoPnt3fPropertyUnrecPtr pos3f;
149 GeoUInt32PropertyUnrecPtr indices;
151 GeoColor3fPropertyUnrecPtr col3f;
152 bool has_colors = false;
154 for (size_t i = 0; i < elems.size(); ++i)
156 const std::string& elem_name = elems[i];
157 int num_elems;
158 std::vector<PlyProperty> props;
159 if (!ply_get_element_description(ply, elem_name, &num_elems, props)) {
160 continue;
163 fprintf(stderr, "process %s\n", elem_name.c_str());
165 if ("vertex" == elem_name)
168 // check for colors:
169 int color_components=0;
170 for (unsigned int j=0; j<props.size(); j++) {
171 if ("x" == props[j].name) {
172 ply_get_property (ply, elem_name, &vert_props[0]); // x
174 else if ("y" == props[j].name) {
175 ply_get_property (ply, elem_name, &vert_props[1]); // y
177 else if ("z" == props[j].name) {
178 ply_get_property (ply, elem_name, &vert_props[2]); // z
180 else if ("red" == props[j].name) {
181 ply_get_property (ply, elem_name, &vert_props[3]); // r
182 color_components++;
184 else if ("green" == props[j].name) {
185 ply_get_property (ply, elem_name, &vert_props[4]); // g
186 color_components++;
188 else if ("blue" == props[j].name) {
189 ply_get_property (ply, elem_name, &vert_props[5]); // b
190 color_components++;
194 has_colors = color_components == 3;
196 pos3f = GeoPnt3fProperty::create();
198 MFPnt3f &data = pos3f->editField();
200 data.resize(num_elems);
202 /*ply_get_property(ply, elem_name, &vert_props[0]);
203 ply_get_property(ply, elem_name, &vert_props[1]);
204 ply_get_property(ply, elem_name, &vert_props[2]);*/
206 col3f = GeoColor3fProperty::create();
207 MFColor3f &dataCol = col3f->editField();
208 if (has_colors) {
209 dataCol.resize(num_elems);
210 /*ply_get_property(ply, elem_name, &vert_props[3]);
211 ply_get_property(ply, elem_name, &vert_props[4]);
212 ply_get_property(ply, elem_name, &vert_props[5]);*/
215 for (int j = 0; j < num_elems; ++j)
217 Vertex vertex = { 0, 0, 0, 128 ,128 ,128 };
218 ply_get_element(ply, &vertex);
219 data[j] = Pnt3f(vertex.x, vertex.y, vertex.z);
220 if (has_colors) {
221 dataCol[j] = Color3f(vertex.r/256.f, vertex.g/256.f, vertex.b/256.f);
225 else if ("face" == elem_name)
227 indices = GeoUInt32Property::create();
229 MFUInt32 &data = indices->editField();
231 // At least three vertices per face...
232 data.reserve(num_elems * 3);
234 ply_get_property(ply, elem_name, &face_props[0]);
236 for (int j = 0; j < num_elems; ++j)
238 Face f;
239 ply_get_element(ply, &f);
240 if (f.nverts < 3)
242 FINFO(("PLYSceneFileType::read: face with less than 3 vertices?"));
243 continue;
246 // Treat faces as triangle strips.
247 int v1 = f.verts[0];
248 int v2 = f.verts[1];
249 for (int k = 2; k < f.nverts; ++k)
251 int v3 = f.verts[k];
252 data.push_back(v1);
253 data.push_back(v2);
254 data.push_back(v3);
255 v1 = v2;
256 v2 = v3;
259 free(f.verts);
264 ply_close(ply);
266 if (pos3f != NULL && indices != NULL)
268 GeometryUnrecPtr geo = Geometry::create();
270 GeoUInt8PropertyUnrecPtr types = GeoUInt8Property::create();
272 types->addValue(GL_TRIANGLES);
274 GeoUInt32PropertyUnrecPtr lengths = GeoUInt32Property::create();
276 lengths->addValue(indices->size32());
278 geo->setTypes (types );
279 geo->setLengths (lengths);
280 geo->setPositions(pos3f );
281 if (has_colors) { geo->setColors(col3f); }
282 geo->setIndices (indices);
284 calcVertexNormals(geo);
286 return makeNodeFor(geo);
288 else
290 // Clean up if necessary.
291 return NodeTransitPtr(NULL);
297 PLYSceneFileType::PLYSceneFileType(const Char8* suffixArray[],
298 UInt16 suffixByteCount,
299 bool override,
300 UInt32 overridePriority,
301 UInt32 flags) :
302 SceneFileType(suffixArray,
303 suffixByteCount,
304 override,
305 overridePriority,
306 flags)
310 PLYSceneFileType::~PLYSceneFileType(void)