1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2007 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 /* OSBTypedGeoIntegralPropertyElement */
43 /*-------------------------------------------------------------------------*/
45 /*-------------------------------------------------------------------------*/
48 template <class GeoPropertyTypeT>
49 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::
50 OSBTypedGeoIntegralPropertyElement(OSBRootElement *root)
51 : Inherited(root, OSGOSBHeaderVersion200)
56 /*-------------------------------------------------------------------------*/
59 template <class GeoPropertyTypeT>
60 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::
61 ~OSBTypedGeoIntegralPropertyElement(void)
66 /*-------------------------------------------------------------------------*/
69 template <class GeoPropertyTypeT>
71 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::read(
72 const std::string &typeName)
74 OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::read\n"));
76 BinaryReadHandler *rh = editRoot()->getReadHandler();
82 rh->getValue(ptrTypeId);
83 rh->getValue(version );
85 if(version >= OSGOSBHeaderVersion200)
87 if(version > OSGOSBHeaderVersion200)
89 FINFO(("OSBTypedGeoIntegralPropertyElement<>::read: "
90 "Unknown version, trying to read as latest.\n"));
95 FFATAL(("OSBTypedGeoIntegralPropertyElement<>::read: "
96 "Can not read pre-OpenSG 2 GeoProperty data.\n"));
102 GeoPropertyUnrecPtrType prop = GeoPropertyType::create();
107 std::string fieldName;
108 std::string fieldTypeName;
110 PtrFieldListIt ptrFieldIt;
112 if(!readFieldHeader("", fieldName, fieldTypeName, fieldSize))
114 OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::read: "
115 "Reading stopped at field: [%s].\n", fieldName.c_str()));
119 if(fieldName == "values")
121 // "values" can be packed and require special handling
122 if((flags & FlagPackedMask) == FlagPacked)
124 OSBGeometryHelper gh;
129 gh.readPackedIntegralPropertyHeader(
130 rh, maxValue, propSize, byteSize);
132 gh.readPackedIntegralProperty(
133 rh, prop, maxValue, propSize, byteSize);
137 readFieldContent(fieldName, fieldTypeName,
138 fieldSize, "", ptrFieldIt);
143 // all other fields can be read normally
144 readFieldContent(fieldName, fieldTypeName,
145 fieldSize, "", ptrFieldIt);
150 template <class GeoPropertyTypeT>
152 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::postRead(void)
154 OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::postRead\n"));
157 /*-------------------------------------------------------------------------*/
160 template <class GeoPropertyTypeT>
162 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::preWrite(
163 FieldContainer * const fc)
165 OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::preWrite\n"));
167 preWriteFieldContainer(fc, "");
170 template <class GeoPropertyTypeT>
172 OSBTypedGeoIntegralPropertyElement<GeoPropertyTypeT>::write(void)
174 OSG_OSB_LOG(("OSBTypedGeoIntegralPropertyElement<>::write\n"));
176 BinaryWriteHandler *wh = editRoot()->getWriteHandler();
178 GeoPropertyPtrType prop =
179 dynamic_cast<GeoPropertyPtrType>(getContainer());
181 wh->putValue(getFCPtrType(getContainer()));
182 wh->putValue(getVersion() );
184 if(getPackData() == true)
190 flags &= ~FlagPackedMask;
195 std::string excludedFields("'values'");
197 // write all other fields
198 writeFields(excludedFields, false);
200 // get info to write "values" field
201 const FieldDescriptionBase *fieldDesc =
202 getContainer()->getFieldDescription("values");
203 UInt32 fieldId = fieldDesc->getFieldId ();
204 const FieldType &fieldType = fieldDesc->getFieldType();
205 const std::string &fieldName = fieldDesc->getName ();
206 const std::string &fieldTypeName = fieldType .getName ();
207 BitVector fieldMask = fieldDesc->getFieldMask();
209 if(getPackData() == true)
211 UInt32 propSize = prop->size32();
215 IntegralType maxValue;
216 const GeoPropertyFieldType &propField = *(prop->getFieldPtr());
218 // propField is not empty, so there is a max -> we do not deref end()
219 maxValue = *(std::max_element(propField.begin(), propField.end()));
222 BitPacker packer(propSize, maxValue);
223 for(UInt32 i = 0; i < propSize; ++i)
224 packer.pack(propField[i]);
226 const BitPacker::BufferType &buffer = packer.getBuffer();
227 UInt32 byteSize = UInt32(buffer.size());
229 // fieldSize: flags + maxValue + propSize + byteSize
230 UInt32 fieldSize = sizeof(UInt8) + sizeof(IntegralType)
231 + sizeof(UInt32) + sizeof(UInt8) * byteSize;
234 writeFieldHeader(fieldName, fieldTypeName, fieldSize);
235 wh->putValue(maxValue);
236 wh->putValue(propSize);
237 wh->putValue(byteSize);
238 wh->putValues(&buffer.front(), byteSize);
242 UInt32 fieldSize = sizeof(IntegralType) + 2 * sizeof(UInt32);
244 writeFieldHeader(fieldName, fieldTypeName, fieldSize);
245 wh->putValue(static_cast<IntegralType>(0)); // maxValue
246 wh->putValue(static_cast<UInt32> (0)); // propSize
247 wh->putValue(static_cast<UInt32> (0)); // byteSize
252 UInt32 fieldSize = UInt32(getContainer()->getBinSize(fieldMask));
254 writeFieldHeader (fieldName, fieldTypeName, fieldSize);
255 writeFieldContent(fieldId);