1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000,2001 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 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
43 #include <boost/python/errors.hpp>
46 #include <OpenSG/OSGSimpleGeometry.h>
47 #include <OpenSG/OSGSceneFileHandler.h>
53 _node
= OSG::Node::create();
56 NodeBase::NodeBase(const NodeBase
©
) : _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);
77 const char *NodeBase::getType(void)
79 if(_node
->getCore() == OSG::NullFC
)
82 return _node
->getCore()->getType().getCName();
85 void NodeBase::dump(void)
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);
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);
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);
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
)
209 m
.setTransform(OSG::Vec3f(x
,y
,z
));
211 // OSG::beginEditCP(_transform);
212 _transform
->setMatrix(m
);
213 // OSG::endEditCP(_transform);
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
)
258 OSG::GeoPositionsPtr pos
= _geometry
->getPositions();
260 // OSG::beginEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
262 for(OSG::UInt32 i
= 0; i
< pos
->size(); ++i
)
269 // OSG::endEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
272 void Geometry::scale(OSG::Real32 x
, OSG::Real32 y
, OSG::Real32 z
)
275 OSG::GeoPositionsPtr pos
= _geometry
->getPositions();
277 // OSG::beginEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
279 for(OSG::UInt32 i
= 0; i
< pos
->size(); ++i
)
288 // OSG::endEditCP(_geometry, OSG::Geometry::PositionsFieldMask);
292 void Geometry::setDlistCache(bool cache
)
294 _geometry
->setDlistCache(cache
);
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
©
) :
313 _type(copy
._type
), _stack(copy
._stack
)
317 NodeIterator::~NodeIterator()
321 NodeIterator
NodeIterator::__iter__(void)
326 NodeBase
NodeIterator::next(void)
328 while(!_stack
.empty())
330 OSG::NodePtr act
= _stack
.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
> ©
)
366 template <class type
>
367 TypedNodeIterator
<type
>::~TypedNodeIterator()
371 template <class type
>
372 TypedNodeIterator
<type
> TypedNodeIterator
<type
>::__iter__(void)
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
>;
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
));
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
));
406 Geometry
makeSphere(OSG::UInt16 latres
, OSG::UInt16 longres
,
409 Geometry
g(OSG::makeLatLongSphere(latres
,longres
,radius
));
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
));
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
);