fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBNodeElement.cpp
blob8461807320c8b4ceb6418a5085f9b856c29dbb32
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 #include "OSGOSBNodeElement.h"
41 #include "OSGOSBRootElement.h"
42 #include "OSGNode.h"
44 OSG_USING_NAMESPACE
46 /*-------------------------------------------------------------------------*/
47 /* OSBNodeElement */
48 /*-------------------------------------------------------------------------*/
50 /*-------------------------------------------------------------------------*/
51 /* Static members */
53 OSBElementRegistrationHelper<OSBNodeElement>
54 OSBNodeElement::_regHelper =
55 OSBElementRegistrationHelper<OSBNodeElement>("Node");
57 /*-------------------------------------------------------------------------*/
58 /* Constructor */
60 OSBNodeElement::OSBNodeElement(OSBRootElement *root)
61 : Inherited(root, OSGOSBHeaderVersion200)
65 /*-------------------------------------------------------------------------*/
66 /* Destructor */
68 OSBNodeElement::~OSBNodeElement(void)
72 /*-------------------------------------------------------------------------*/
73 /* Reading */
75 void
76 OSBNodeElement::read(const std::string &typeName)
78 OSG_OSB_LOG(("OSBNodeElement::read [%s]\n", typeName.c_str()));
80 BinaryReadHandler *rh = editRoot()->getReadHandler();
81 UInt8 fcPtrType;
82 UInt16 version;
84 rh->getValue(fcPtrType);
85 rh->getValue(version );
87 OSG_OSB_LOG(("OSBNodeElement::read: version: [%u]\n", version));
89 if(fcPtrType != OSBCommonElement::FCPtrNode)
91 FFATAL(("OSBNodeElement::read: fcPtrType has unexpected value.\n"));
93 skipFields();
94 return;
97 NodeUnrecPtr node = Node::create();
99 setContainer(node);
101 if(version == OSGOSBHeaderVersion100)
103 std::string fieldName;
104 std::string fieldTypeName;
105 UInt32 fieldSize;
106 PtrFieldListIt ptrFieldIt;
108 while(readFieldHeader("", fieldName, fieldTypeName, fieldSize))
110 // some fields need to be duplicated for the two replacement chunks
111 if(fieldName == "volume")
113 // parent fields are ignored
114 UInt32 fieldType = 0;
115 rh->getValue(fieldType);
117 switch(fieldSize)
119 case 30:
121 UInt16 sState;
122 Pnt3f vMin;
123 Pnt3f vMax;
125 rh->getValue(sState );
127 rh->getValue(vMin[0]);
128 rh->getValue(vMin[1]);
129 rh->getValue(vMin[2]);
130 rh->getValue(vMax[0]);
131 rh->getValue(vMax[1]);
132 rh->getValue(vMax[2]);
134 node->editVolume().setState (sState );
135 node->editVolume().setBounds(vMin, vMax);
137 break;
139 case 22:
141 UInt16 sState;
142 Pnt3f vCenter;
143 Real32 fRadius;
145 rh->getValue(sState );
147 rh->getValue(vCenter[0]);
148 rh->getValue(vCenter[1]);
149 rh->getValue(vCenter[2]);
150 rh->getValue(fRadius);
152 node->editVolume().setState (sState );
153 node->editVolume().setBounds(vCenter[0] - fRadius,
154 vCenter[1] - fRadius,
155 vCenter[2] - fRadius,
156 vCenter[0] + fRadius,
157 vCenter[1] + fRadius,
158 vCenter[2] + fRadius);
160 break;
162 default:
163 fprintf(stderr,
164 "unknown volume type with size %d, skipping\n",
165 fieldSize );
167 rh->skip(fieldSize - sizeof(UInt32));
169 break;
172 else
174 readFieldContent(fieldName,
175 fieldTypeName,
176 fieldSize, "",
177 ptrFieldIt );
181 else
183 readFields("", "");
187 void
188 OSBNodeElement::postRead(void)
192 /*-------------------------------------------------------------------------*/
193 /* Writing */
195 void
196 OSBNodeElement::preWrite(FieldContainer * const fc)
198 OSG_OSB_LOG(("OSBNodeElement::preWrite\n"));
200 preWriteFieldContainer(fc, "");
203 void
204 OSBNodeElement::write(void)
206 OSG_OSB_LOG(("OSBNodeElement::write\n"));
208 if(getContainer() == NULL)
210 FWARNING(("OSBNodeElement::write: Attempt to write NULL.\n"));
211 return;
214 BinaryWriteHandler *wh = editRoot()->getWriteHandler();
216 wh->putValue(OSBCommonElement::FCPtrNode);
217 wh->putValue(getVersion() );
219 const OSBRootElement *root = getRoot();
220 Node *node = dynamic_cast<Node*>(getContainer());
221 std::string doSkipFields = "";
223 if(node->getVolume ().isStatic () == false &&
224 node->getVolume ().isInfinite () == false &&
225 root->getOptions().forceVolumeExport() == false )
227 doSkipFields += "'volume'";
230 writeFields(doSkipFields, true);