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 /*---------------------------------------------------------------------------*\
38 \*---------------------------------------------------------------------------*/
43 #include "OSGConfig.h"
55 #include "OSGGeometry.h"
56 #include "OSGGeoFunctions.h"
57 #include "OSGTypedGeoVectorProperty.h"
58 #include "OSGTypedGeoIntegralProperty.h"
60 #include "OSGPLYSceneFileType.h"
66 /*! \class OSG::PLYSceneFileType
67 \ingroup GrpSystemFileIO
70 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
71 #pragma warning (disable : 383)SG
74 const Char8
* PLYSceneFileType::_suffixA
[] = { "ply" };
76 PLYSceneFileType
PLYSceneFileType::_the(_suffixA
,
80 SceneFileType::OSG_READ_SUPPORTED
);
83 PLYSceneFileType
&PLYSceneFileType::the(void)
88 const Char8
*PLYSceneFileType::getName(void) const
103 unsigned char nverts
;
107 static PlyProperty vert_props
[] = { /* list of property information for a vertex */
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}
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)
125 static PlyProperty face_props
[] = { /* list of property information for a vertex */
127 {"vertex_indices", PLY_INT
, PLY_INT
, offsetof(Face
,verts
),
128 1, PLY_UCHAR
, PLY_UCHAR
, offsetof(Face
,nverts
)}
130 PlyProperty("vertex_indices", PLY_INT
, PLY_INT
, offsetof(Face
,verts
),
131 1, PLY_UCHAR
, PLY_UCHAR
, offsetof(Face
,nverts
))
136 NodeTransitPtr
PLYSceneFileType::read(
138 const Char8
*, /*fileNameOrExtension*/
139 Resolver resolver
) const
141 std::vector
<std::string
> elems
;
142 PlyFile
* ply
= ply_read(&is
, elems
);
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
];
158 std::vector
<PlyProperty
> props
;
159 if (!ply_get_element_description(ply
, elem_name
, &num_elems
, props
)) {
163 fprintf(stderr
, "process %s\n", elem_name
.c_str());
165 if ("vertex" == elem_name
)
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
184 else if ("green" == props
[j
].name
) {
185 ply_get_property (ply
, elem_name
, &vert_props
[4]); // g
188 else if ("blue" == props
[j
].name
) {
189 ply_get_property (ply
, elem_name
, &vert_props
[5]); // b
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();
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
);
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
)
239 ply_get_element(ply
, &f
);
242 FINFO(("PLYSceneFileType::read: face with less than 3 vertices?"));
246 // Treat faces as triangle strips.
249 for (int k
= 2; k
< f
.nverts
; ++k
)
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
);
290 // Clean up if necessary.
291 return NodeTransitPtr(NULL
);
297 PLYSceneFileType::PLYSceneFileType(const Char8
* suffixArray
[],
298 UInt16 suffixByteCount
,
300 UInt32 overridePriority
,
302 SceneFileType(suffixArray
,
310 PLYSceneFileType::~PLYSceneFileType(void)