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 #include "OSGBaseTypes.h"
40 #include "OSGGeoTypeGraphOp.h"
42 #include "OSGTypedGeoIntegralProperty.h"
43 #include "OSGTypedGeoVectorProperty.h"
44 #include "OSGGraphOpFactory.h"
51 //! Register the GraphOp with the factory
52 static bool registerOp(void)
54 GraphOpRefPtr newOp
= GeoTypeGraphOp::create();
56 GraphOpFactory::the()->registerOp(newOp
);
60 static OSG::StaticInitFuncWrapper
registerOpWrapper(registerOp
);
65 GeoTypeGraphOpTransitPtr
GeoTypeGraphOp::create(void)
67 return GeoTypeGraphOpTransitPtr(new GeoTypeGraphOp
);
70 GraphOpTransitPtr
GeoTypeGraphOp::clone(void)
72 return GraphOpTransitPtr(new GeoTypeGraphOp
);
75 GeoTypeGraphOp::GeoTypeGraphOp(const char* name
)
77 _filter (TypeTraits
<OSG::BitVector
>::getMax())
81 GeoTypeGraphOp::~GeoTypeGraphOp(void)
85 bool GeoTypeGraphOp::travNodeEnter(Node
*node
)
90 bool GeoTypeGraphOp::travNodeLeave(Node
*node
)
92 Geometry
*geo
= dynamic_cast<Geometry
*>(node
->getCore());
97 if(_filter
& FilterNormals
)
100 if(_filter
& FilterIndices
)
103 if(_filter
& FilterLengths
)
109 void GeoTypeGraphOp::processNormals(Geometry
*geo
)
111 GeoVectorProperty
*norm
= geo
->getProperty(Geometry::NormalsIndex
);
112 GeoVec3fProperty
*norm3f
= dynamic_cast<GeoVec3fProperty
*>(norm
);
116 GeoVec3NbPropertyUnrecPtr norm3b
= GeoVec3NbProperty::create();
118 const GeoVec3fProperty::StoredFieldType
*srcF
= norm3f
->getFieldPtr();
120 norm3b
->resize(srcF
->size());
122 for(UInt32 i
= 0; i
< srcF
->size(); ++i
)
124 Vec3f n
= (*srcF
)[i
];
127 norm3b
->setValue(n
, i
);
130 geo
->setProperty(norm3b
, Geometry::NormalsIndex
);
134 void GeoTypeGraphOp::processIndices(Geometry
*geo
)
136 Geometry::IndexBag ibag
= geo
->getUniqueIndexBag();
138 for(UInt32 i
= 0; i
< ibag
.size(); ++i
)
140 GeoIntegralProperty
*ind
= ibag
[i
].first
;
141 GeoUInt32Property
*ind32
= dynamic_cast<GeoUInt32Property
*>(ind
);
145 const GeoUInt32Property::StoredFieldType
*srcF
= ind32
->getFieldPtr();
147 UInt32 maxIndex
= TypeTraits
<UInt16
>::getMax();
148 bool allowConvert
= true;
150 for(UInt32 j
= 0; j
< srcF
->size(); ++j
)
152 if((*srcF
)[j
] > maxIndex
)
154 allowConvert
= false;
161 GeoUInt16PropertyUnrecPtr ind16
=
162 GeoUInt16Property::create();
163 GeoUInt16Property::StoredFieldType
*dstF
=
164 ind16
->editFieldPtr();
166 dstF
->reserve(srcF
->size());
168 for(UInt32 j
= 0; j
< srcF
->size(); ++j
)
170 UInt32 i
= (*srcF
)[j
];
174 // set index for all properties that use it
175 for(UInt32 j
= 0; j
< ibag
[i
].second
.size(); ++j
)
176 geo
->setIndex(ind16
, ibag
[i
].second
[j
]);
182 void GeoTypeGraphOp::processLengths(Geometry
*geo
)
184 GeoIntegralProperty
*len
= geo
->getLengths();
185 GeoUInt32Property
*len32
= dynamic_cast<GeoUInt32Property
*>(len
);
189 const GeoUInt32Property::StoredFieldType
*srcF
= len32
->getFieldPtr();
191 UInt32 maxLength
= TypeTraits
<UInt16
>::getMax();
192 bool allowConvert
= true;
194 for(UInt32 i
= 0; i
< srcF
->size(); ++i
)
196 if((*srcF
)[i
] > maxLength
)
198 allowConvert
= false;
205 GeoUInt16PropertyUnrecPtr len16
=
206 GeoUInt16Property::create();
207 GeoUInt16Property::StoredFieldType
*dstF
=
208 len16
->editFieldPtr();
210 dstF
->reserve(srcF
->size());
212 for(UInt32 j
= 0; j
< srcF
->size(); ++j
)
214 UInt32 i
= (*srcF
)[j
];
218 geo
->setLengths(len16
);
223 void GeoTypeGraphOp::setParams(const std::string params
)
228 if(ps("filter", filter
))
230 _filter
= TypeTraits
<BitVector
>::BitsClear
;
232 if(filter
.find("Nor") != std::string::npos
||
233 filter
.find("nor") != std::string::npos
)
235 _filter
|= FilterNormals
;
238 if(filter
.find("Ind") != std::string::npos
||
239 filter
.find("ind") != std::string::npos
)
241 _filter
|= FilterIndices
;
244 if(filter
.find("Len") != std::string::npos
||
245 filter
.find("len") != std::string::npos
)
247 _filter
|= FilterLengths
;
251 std::string out
= ps
.getUnusedParams();
255 FWARNING(("GeoTypeGraphOp doesn't have parameters '%s'.\n",
260 std::string
GeoTypeGraphOp::usage(void)
263 "GeoType: convert the types of a Geometry's attributes\n"
264 " Tries to convert the attributes of a Geometry to smaller/faster\n"
265 " types. By default only the lengths are changed to 16 bit.\n"
266 "Params: name (type, default)\n"
267 " filter (string, \"\"): fields to convert, can be a combination of\n"
268 " Normals, Indices and Lengths, connected by +.\n"
272 void GeoTypeGraphOp::setFilter(const OSG::BitVector
&filter
)