changed: gcc8 base update
[opensg.git] / Source / System / FileIO / OSB / OSGOSBGeometryHelper.inl
blob8effcc9e7ebc03e0c064089caa402ffcf28de897
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *                   Copyright (C) 2006 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 OSG_BEGIN_NAMESPACE
41 /*-------------------------------------------------------------------------*/
42 /* OSBGeometryHelper                                                      */
43 /*-------------------------------------------------------------------------*/
45 /*-------------------------------------------------------------------------*/
46 /* Reading Helper Functions                                                */
48 template <class GeoPropPtrTypeT>
49 void
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)
75     {
76         propField->push_back(static_cast<IntegralType>(unpacker.unpack()));
77     }
79     OSG_OSB_LOG(("OSBGeometryHelper::readPackedIntegralProperty<>: "
80             "Inflated [%u] bytes to [%u] values. Size ratio [%f] %%\n",
81             byteSize, propSize,
82             100.0 * (propSize * sizeof(IntegralType)) / byteSize));
85 template <class GeoPropPtrTypeT>
86 void
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();
107     // allocate space
108     propField->reserve(propSize);
110     if(resolution == Quantizer::QRES_8BIT)
111     {
112         Quantizer quan(minValue, maxValue, resolution);
114         UInt8      qv;
115         VectorType v;
116         for(UInt32 i = 0; i < propSize; ++i)
117         {
118             for(UInt32 j = 0; j < propDim; ++j)
119             {
120                 rh->getValue(qv);
121                 v[j] = static_cast<ElementType>(quan.decode(qv));
122             }
124             propField->push_back(v);
125         }
126     }
127     else if(resolution == Quantizer::QRES_16BIT)
128     {
129         Quantizer quan(minValue, maxValue, resolution);
131         UInt16     qv;
132         VectorType v;
133         for(UInt32 i = 0; i < propSize; ++i)
134         {
135             for(UInt32 j = 0; j < propDim; ++j)
136             {
137                 rh->getValue(qv);
138                 v[j] = static_cast<ElementType>(quan.decode(qv));
139             }
141             propField->push_back(v);
142         }
143     }
144     else if(resolution == Quantizer::QRES_24BIT)
145     {
146         Quantizer quan(minValue, maxValue, resolution);
148         UInt32     qv;
149         UInt16     qvl;
150         UInt8      qvh;
151         VectorType v;
152         for(UInt32 i = 0; i < propSize; ++i)
153         {
154             for(UInt32 j = 0; j < propDim; ++j)
155             {
156                 rh->getValue(qvl);
157                 rh->getValue(qvh);
158                 qv = (((static_cast<UInt32>(qvh) << 16) |
159                         static_cast<UInt32>(qvl)         ) & 0x00FFFFFF);
160                 v[j] = static_cast<ElementType>(quan.decode(qv));
161             }
163             propField->push_back(v);
164         }
165     }
166     else
167     {
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));
174     }
176     OSG_OSB_LOG(("OSBGeometryHelper::readQuantizedVectorProperty<>: "
177             "Inflated [%u] bytes to [%u] values. Size ratio [%f] %%\n",
178             fieldSize, propSize,
179             100.0 * (propSize * sizeof(VectorType)) / fieldSize));
182 /*-------------------------------------------------------------------------*/
183 /* Conversion Helper Functions                                             */
185 template <class OutIndexPtrTypeT, class InIndexPtrTypeT>
186 void
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)
204     {
205         OutIndexUnrecPtrType  index = OutIndexType::create();
206         OutIndexFieldType    *field = index->editFieldPtr();
208         outIndices.push_back(index);
209         outFields .push_back(field);
210     }
212     // split the multi-index into single indices
213     for(UInt32 i = 0; i < inIndexSize; i += indexMappingSize)
214     {
215         for(UInt32 j = 0; j < indexMappingSize; ++j)
216         {
217             outFields[j]->push_back(inIndex->getValue(i + j));
218         }
219     }
221     // remove the old indices.
222     geo->setIndices(NULL);
223     geo->resizePropIndices(0);
225     for(UInt32 i = 0; i < indexMappingSize; ++i)
226     {
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);
251     }
255 OSG_END_NAMESPACE