fixed: let the material fill the override chunk block
[opensg.git] / Source / System / GraphOp / OSGGeoTypeGraphOp.cpp
bloba0cc1c609cd885b7bac7091901db3b3129a2b49b
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 "OSGBaseTypes.h"
40 #include "OSGGeoTypeGraphOp.h"
41 #include "OSGLog.h"
42 #include "OSGTypedGeoIntegralProperty.h"
43 #include "OSGTypedGeoVectorProperty.h"
44 #include "OSGGraphOpFactory.h"
46 OSG_USING_NAMESPACE
48 namespace
51 //! Register the GraphOp with the factory
52 static bool registerOp(void)
54 GraphOpRefPtr newOp = GeoTypeGraphOp::create();
56 GraphOpFactory::the()->registerOp(newOp);
57 return true;
60 static OSG::StaticInitFuncWrapper registerOpWrapper(registerOp);
62 } // namespace
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)
76 : Inherited(name),
77 _filter (TypeTraits<OSG::BitVector>::getMax())
81 GeoTypeGraphOp::~GeoTypeGraphOp(void)
85 bool GeoTypeGraphOp::travNodeEnter(Node *node)
87 return true;
90 bool GeoTypeGraphOp::travNodeLeave(Node *node)
92 Geometry *geo = dynamic_cast<Geometry *>(node->getCore());
94 if(geo == NULL)
95 return true;
97 if(_filter & FilterNormals)
98 processNormals(geo);
100 if(_filter & FilterIndices)
101 processIndices(geo);
103 if(_filter & FilterLengths)
104 processLengths(geo);
106 return true;
109 void GeoTypeGraphOp::processNormals(Geometry *geo)
111 GeoVectorProperty *norm = geo->getProperty(Geometry::NormalsIndex);
112 GeoVec3fProperty *norm3f = dynamic_cast<GeoVec3fProperty *>(norm);
114 if(norm3f != NULL)
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];
125 n.normalize();
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);
143 if(ind32 != NULL)
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;
155 break;
159 if(allowConvert)
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];
171 dstF->push_back(i);
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);
187 if(len32 != NULL)
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;
199 break;
203 if(allowConvert)
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];
215 dstF->push_back(i);
218 geo->setLengths(len16);
223 void GeoTypeGraphOp::setParams(const std::string params)
225 ParamSet ps(params);
226 std::string filter;
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();
253 if(out.length())
255 FWARNING(("GeoTypeGraphOp doesn't have parameters '%s'.\n",
256 out.c_str()));
260 std::string GeoTypeGraphOp::usage(void)
262 return
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)
274 _filter = filter;