1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2006 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 \*---------------------------------------------------------------------------*/
41 /*-------------------------------------------------------------------------*/
42 /* OSBGeometryHelper */
43 /*-------------------------------------------------------------------------*/
45 /*-------------------------------------------------------------------------*/
46 /* Reading Helper Functions */
48 template <class GeoPropPtrTypeT>
50 OSBGeometryHelper::readPackedIntegralProperty(
51 BinaryReadHandler *rh,
52 const GeoPropPtrTypeT prop,
53 const UInt32 maxValue,
54 const UInt32 propSize,
55 const UInt32 byteSize )
57 OSG_OSB_LOG(("OSBGeometryHelper::readPackedIntegralProperty<>:\n"));
59 typedef GeoPropPtrTypeT GeoPropPtrType;
60 typedef typename GeoPropPtrType::Object GeoPropType;
61 typedef typename GeoPropType::StoredFieldType GeoPropFieldType;
62 typedef typename GeoPropFieldType::StoredType IntegralType;
64 // read packed values into a buffer
65 BitUnpacker::BufferType buffer;
66 buffer.resize(byteSize);
67 rh->getValues(static_cast<UInt8*>(&buffer.front()), byteSize);
69 GeoPropFieldType *propField = prop->editFieldPtr();
71 propField->reserve(propSize);
72 BitUnpacker unpacker(buffer, maxValue);
74 for(UInt32 i = 0; i < propSize; ++i)
76 propField->push_back(static_cast<IntegralType>(unpacker.unpack()));
79 OSG_OSB_LOG(("OSBGeometryHelper::readPackedIntegralProperty<>: "
80 "Inflated [%u] bytes to [%u] values. Size ratio [%f] %%\n",
82 100.0 * (propSize * sizeof(IntegralType)) / byteSize));
85 template <class GeoPropPtrTypeT>
87 OSBGeometryHelper::readQuantizedVectorProperty(
88 BinaryReadHandler *rh,
89 const GeoPropPtrTypeT prop,
90 const UInt32 fieldSize,
91 const UInt8 resolution,
92 const Real32 minValue,
93 const Real32 maxValue,
94 const UInt32 propSize )
96 OSG_OSB_LOG(("OSBGeometryHelper::readQuantizedVectorProperty<>:\n"));
98 typedef GeoPropPtrTypeT GeoPropPtrType;
99 typedef typename GeoPropPtrType::Object GeoPropType;
100 typedef typename GeoPropType::StoredFieldType GeoPropFieldType;
101 typedef typename GeoPropFieldType::StoredType VectorType;
102 typedef typename VectorType::ValueType ElementType;
104 UInt32 propDim = prop->getDimension();
105 GeoPropFieldType *propField = prop->editFieldPtr();
108 propField->reserve(propSize);
110 if(resolution == Quantizer::QRES_8BIT)
112 Quantizer quan(minValue, maxValue, resolution);
116 for(UInt32 i = 0; i < propSize; ++i)
118 for(UInt32 j = 0; j < propDim; ++j)
121 v[j] = static_cast<ElementType>(quan.decode(qv));
124 propField->push_back(v);
127 else if(resolution == Quantizer::QRES_16BIT)
129 Quantizer quan(minValue, maxValue, resolution);
133 for(UInt32 i = 0; i < propSize; ++i)
135 for(UInt32 j = 0; j < propDim; ++j)
138 v[j] = static_cast<ElementType>(quan.decode(qv));
141 propField->push_back(v);
144 else if(resolution == Quantizer::QRES_24BIT)
146 Quantizer quan(minValue, maxValue, resolution);
152 for(UInt32 i = 0; i < propSize; ++i)
154 for(UInt32 j = 0; j < propDim; ++j)
158 qv = (((static_cast<UInt32>(qvh) << 16) |
159 static_cast<UInt32>(qvl) ) & 0x00FFFFFF);
160 v[j] = static_cast<ElementType>(quan.decode(qv));
163 propField->push_back(v);
168 FFATAL(("OSBGeometryHelper::readQuantizedVectorProperty<>: "
169 "Unknown quantization resolution, can not decode data.\n"));
171 // skip remaining part of field
172 rh->skip(fieldSize - sizeof(UInt8) - sizeof(Real32)
173 - sizeof(Real32) - sizeof(UInt32));
176 OSG_OSB_LOG(("OSBGeometryHelper::readQuantizedVectorProperty<>: "
177 "Inflated [%u] bytes to [%u] values. Size ratio [%f] %%\n",
179 100.0 * (propSize * sizeof(VectorType)) / fieldSize));
182 /*-------------------------------------------------------------------------*/
183 /* Conversion Helper Functions */
185 template <class OutIndexPtrTypeT, class InIndexPtrTypeT>
187 OSBGeometryHelper::splitMultiIndex(
188 const std::vector<UInt16> &indexMapping,
189 const InIndexPtrTypeT inIndex,
190 Geometry * const geo )
192 typedef typename PtrStripper<OutIndexPtrTypeT>::Object OutIndexType;
193 typedef typename OutIndexType::ObjUnrecPtr OutIndexUnrecPtrType;
194 typedef typename OutIndexType::StoredFieldType OutIndexFieldType;
196 UInt32 indexMappingSize = UInt32(indexMapping.size());
197 UInt32 inIndexSize = inIndex->size32();
199 std::vector<OutIndexUnrecPtrType > outIndices;
200 std::vector<OutIndexFieldType *> outFields;
202 // create the index properties
203 for(UInt32 i = 0; i < indexMappingSize; ++i)
205 OutIndexUnrecPtrType index = OutIndexType::create();
206 OutIndexFieldType *field = index->editFieldPtr();
208 outIndices.push_back(index);
209 outFields .push_back(field);
212 // split the multi-index into single indices
213 for(UInt32 i = 0; i < inIndexSize; i += indexMappingSize)
215 for(UInt32 j = 0; j < indexMappingSize; ++j)
217 outFields[j]->push_back(inIndex->getValue(i + j));
221 // remove the old indices.
222 geo->setIndices(NULL);
223 geo->resizePropIndices(0);
225 for(UInt32 i = 0; i < indexMappingSize; ++i)
227 if(indexMapping[i] & OSBGeometryElement::MapPosition)
228 geo->setIndex(outIndices[i], Geometry::PositionsIndex);
229 if(indexMapping[i] & OSBGeometryElement::MapNormal)
230 geo->setIndex(outIndices[i], Geometry::NormalsIndex);
231 if(indexMapping[i] & OSBGeometryElement::MapColor)
232 geo->setIndex(outIndices[i], Geometry::ColorsIndex);
233 if(indexMapping[i] & OSBGeometryElement::MapSecondaryColor)
234 geo->setIndex(outIndices[i], Geometry::SecondaryColorsIndex);
235 if(indexMapping[i] & OSBGeometryElement::MapTexCoords)
236 geo->setIndex(outIndices[i], Geometry::TexCoordsIndex);
237 if(indexMapping[i] & OSBGeometryElement::MapTexCoords1)
238 geo->setIndex(outIndices[i], Geometry::TexCoords1Index);
239 if(indexMapping[i] & OSBGeometryElement::MapTexCoords2)
240 geo->setIndex(outIndices[i], Geometry::TexCoords2Index);
241 if(indexMapping[i] & OSBGeometryElement::MapTexCoords3)
242 geo->setIndex(outIndices[i], Geometry::TexCoords3Index);
243 if(indexMapping[i] & OSBGeometryElement::MapTexCoords4)
244 geo->setIndex(outIndices[i], Geometry::TexCoords4Index);
245 if(indexMapping[i] & OSBGeometryElement::MapTexCoords5)
246 geo->setIndex(outIndices[i], Geometry::TexCoords5Index);
247 if(indexMapping[i] & OSBGeometryElement::MapTexCoords6)
248 geo->setIndex(outIndices[i], Geometry::TexCoords6Index);
249 if(indexMapping[i] & OSBGeometryElement::MapTexCoords7)
250 geo->setIndex(outIndices[i], Geometry::TexCoords7Index);