fixed: auto_ptr -> unique_ptr
[opensg.git] / Tools / osgBench / Nodes.cpp
blob91a656e13127b207c7d96607d8685d604e9932c9
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000,2001 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 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <boost/python/errors.hpp>
44 #include <Python.h>
45 #include <Nodes.h>
46 #include <OpenSG/OSGSimpleGeometry.h>
47 #include <OpenSG/OSGSceneFileHandler.h>
49 // Basic Node
51 NodeBase::NodeBase()
53 _node = OSG::Node::create();
56 NodeBase::NodeBase(const NodeBase &copy) : _node(copy._node)
60 NodeBase::NodeBase(OSG::NodePtr node) : _node(node)
64 NodeBase::NodeBase(OSG::NodeCorePtr core)
66 _node = OSG::Node::create();
68 // beginEditCP(_node);
69 _node->setCore(core);
70 // endEditCP(_node);
73 NodeBase::~NodeBase()
77 const char *NodeBase::getType(void)
79 if(_node->getCore() == OSG::NullFC)
80 return "<uncored>";
82 return _node->getCore()->getType().getCName();
85 void NodeBase::dump(void)
87 _node->dump();
90 void NodeBase::save(const char * name)
92 OSG::SceneFileHandler::the()->write(_node, name);
95 NodeBase NodeBase::clone(void)
97 return NodeBase(OSG::deepCloneTree(_node));
101 NodeIterator NodeBase::iter(const char * name)
103 return NodeIterator(*this, name);
106 TypedNodeIterator<Geometry> NodeBase::geometries(void)
108 return TypedNodeIterator<Geometry>(*this);
111 TypedNodeIterator<Transform> NodeBase::transforms(void)
113 return TypedNodeIterator<Transform>(*this);
116 // Group Node
118 Group::Group() : NodeBase()
120 _group = OSG::Group::create();
122 // OSG::beginEditCP(_node);
123 _node->setCore(_group);
124 // OSG::endEditCP(_node);
127 Group::Group(const Group &group) : NodeBase(group)
129 _group = group._group;
132 Group::Group(OSG::NodePtr node) : NodeBase(node)
134 _group = OSG::cast_dynamic<OSG::GroupPtr>(node->getCore());
136 // OSG::beginEditCP(_node);
137 _node->setCore(_group);
138 // OSG::endEditCP(_node);
141 Group::Group(OSG::GroupPtr group) : NodeBase()
143 // OSG::beginEditCP(_node);
144 _node->setCore(_group);
145 // OSG::endEditCP(_node);
148 Group::~Group()
153 void Group::addChild(NodeBase &child)
155 // OSG::beginEditCP(_node);
156 _node->addChild(child.getNode());
157 // OSG::endEditCP(_node);
161 void Group::subChild(NodeBase &child)
163 // OSG::beginEditCP(_node);
164 _node->subChild(child.getNode());
165 // OSG::endEditCP(_node);
168 // Transform Node
170 Transform::Transform()
172 _transform = OSG::Transform::create();
174 // OSG::beginEditCP(_node);
175 _node->setCore(_transform);
176 // OSG::endEditCP(_node);
179 Transform::Transform(const Transform &trans) : Group(trans)
181 _transform = trans._transform;
184 Transform::Transform(OSG::NodePtr node) : Group(node)
186 _transform = OSG::cast_dynamic<OSG::TransformPtr>(node->getCore());
188 // OSG::beginEditCP(_node);
189 _node->setCore(_transform);
190 // OSG::endEditCP(_node);
193 Transform::Transform(OSG::TransformPtr transform) : Group()
195 // OSG::beginEditCP(_node);
196 _node->setCore(_transform);
197 // OSG::endEditCP(_node);
200 Transform::~Transform()
205 void Transform::setTranslation(OSG::Real32 x, OSG::Real32 y, OSG::Real32 z)
207 OSG::Matrix m;
209 m.setTransform(OSG::Vec3f(x,y,z));
211 // OSG::beginEditCP(_transform);
212 _transform->setMatrix(m);
213 // OSG::endEditCP(_transform);
217 // Geometry Node
219 Geometry::Geometry()
221 _geometry = OSG::Geometry::create();
223 // OSG::beginEditCP(_node);
224 _node->setCore(_geometry);
225 // OSG::endEditCP(_node);
228 Geometry::Geometry(const Geometry &geo) : NodeBase(geo)
230 _geometry = geo._geometry;
233 Geometry::Geometry(OSG::NodePtr node) : NodeBase(node)
235 _geometry = OSG::cast_dynamic<OSG::GeometryPtr>(node->getCore());
237 // OSG::beginEditCP(_node);
238 _node->setCore(_geometry);
239 // OSG::endEditCP(_node);
242 Geometry::Geometry(OSG::GeometryPtr geometry) : NodeBase()
244 // OSG::beginEditCP(_node);
245 _node->setCore(_geometry);
246 // OSG::endEditCP(_node);
249 Geometry::~Geometry()
254 void Geometry::translate(OSG::Real32 x, OSG::Real32 y, OSG::Real32 z)
256 OSG::Vec3f d(x,y,z);
257 OSG::Pnt3f p;
258 OSG::GeoPositionsPtr pos = _geometry->getPositions();
260 // OSG::beginEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
262 for(OSG::UInt32 i = 0; i < pos->size(); ++i)
264 pos->getValue(p, i);
265 p += d;
266 pos->setValue(p,i);
269 // OSG::endEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
272 void Geometry::scale(OSG::Real32 x, OSG::Real32 y, OSG::Real32 z)
274 OSG::Pnt3f p;
275 OSG::GeoPositionsPtr pos = _geometry->getPositions();
277 // OSG::beginEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
279 for(OSG::UInt32 i = 0; i < pos->size(); ++i)
281 pos->getValue(p, i);
282 p[0] *= x;
283 p[1] *= y;
284 p[2] *= z;
285 pos->setValue(p,i);
288 // OSG::endEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
292 void Geometry::setDlistCache(bool cache)
294 _geometry->setDlistCache(cache);
298 // Node Iterator
300 NodeIterator::NodeIterator(void)
304 NodeIterator::NodeIterator(NodeBase &start, const char *name)
306 _stack.push_back(start.getNode());
308 _type = OSG::FieldContainerFactory::the()->findType(name);
312 NodeIterator::NodeIterator(const NodeIterator &copy) :
313 _type(copy._type), _stack(copy._stack)
317 NodeIterator::~NodeIterator()
321 NodeIterator NodeIterator::__iter__(void)
323 return *this;
326 NodeBase NodeIterator::next(void)
328 while(!_stack.empty())
330 OSG::NodePtr act = _stack.back();
332 _stack.pop_back();
334 for(OSG::UInt32 i = 0; i < act->getNChildren(); ++i)
335 _stack.push_back(act->getChild(i));
337 if(act->getCore()->getType().isDerivedFrom(*_type))
339 return NodeBase(act);
343 PyErr_SetString(PyExc_StopIteration, "Out of Nodes");
344 boost::python::throw_error_already_set();
348 template <class type>
349 TypedNodeIterator<type>::TypedNodeIterator(void) : NodeIterator()
351 _type = OSG::FieldContainerFactory::the()->findType(type::getCoreName());
354 template <class type>
355 TypedNodeIterator<type>::TypedNodeIterator(NodeBase &start) :
356 NodeIterator(start, type::getCoreName())
360 template <class type>
361 TypedNodeIterator<type>::TypedNodeIterator(const TypedNodeIterator<type> &copy)
362 : NodeIterator(copy)
366 template <class type>
367 TypedNodeIterator<type>::~TypedNodeIterator()
371 template <class type>
372 TypedNodeIterator<type> TypedNodeIterator<type>::__iter__(void)
374 return *this;
377 template <class type>
378 type TypedNodeIterator<type>::next(void)
380 return type(NodeIterator::next().getNode());
384 template class TypedNodeIterator<Geometry>;
385 template class TypedNodeIterator<Transform>;
387 // Functions
390 Geometry makeBox(OSG::Real32 x, OSG::Real32 y, OSG::Real32 z,
391 OSG::UInt16 hor, OSG::UInt16 vert, OSG::UInt16 depth)
393 Geometry g(OSG::makeBox(x,y,z,hor,vert,depth));
395 return g;
398 Geometry makeTorus(OSG::Real32 inner, OSG::Real32 outer,
399 OSG::UInt16 sides, OSG::UInt16 rings)
401 Geometry g(OSG::makeTorus(inner,outer,sides,rings));
403 return g;
406 Geometry makeSphere(OSG::UInt16 latres, OSG::UInt16 longres,
407 OSG::Real32 radius)
409 Geometry g(OSG::makeLatLongSphere(latres,longres,radius));
411 return g;
415 Geometry makePlane(OSG::Real32 x, OSG::Real32 y,
416 OSG::UInt16 hor, OSG::UInt16 vert)
418 Geometry g(OSG::makePlane(x, y, hor, vert));
420 return g;
423 NodeBase makeShared(NodeBase &node)
425 return NodeBase(node.getNode()->getCore());
428 void addRef(NodeBase &node)
430 OSG::addRef(node.getNode());
433 void subRef(NodeBase &node)
435 OSG::subRef(node.getNode());
438 NodeBase loadScene(char *filename)
440 OSG::NodePtr node = OSG::SceneFileHandler::the()->read(filename);
442 return NodeBase(node);