fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / Misc / OSGStackedTransform.cpp
blob385386630a88bb0bdfa5dd41de9e7264c5edc2e0
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-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 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include <OSGConfig.h>
48 #include "OSGStackedTransform.h"
49 #include "OSGTransformationElement.h"
50 #include "OSGIntersectAction.h"
52 #include "OSGRenderAction.h"
53 #include "OSGNameAttachment.h"
55 #include "OSGVolume.h"
57 OSG_BEGIN_NAMESPACE
59 // Documentation for this class is emitted in the
60 // OSGStackedTransformBase.cpp file.
61 // To modify it, please change the .fcd file (OSGStackedTransform.fcd) and
62 // regenerate the base file.
64 /***************************************************************************\
65 * Class variables *
66 \***************************************************************************/
68 std::string StackedTransform::TranslateName = std::string("translate");
69 std::string StackedTransform::RotateXName = std::string("rotateX" );
70 std::string StackedTransform::RotateYName = std::string("rotateY" );
71 std::string StackedTransform::RotateZName = std::string("rotateZ" );
72 std::string StackedTransform::ScaleName = std::string("scale" );
74 /***************************************************************************\
75 * Class methods *
76 \***************************************************************************/
78 void StackedTransform::initMethod(InitPhase ePhase)
80 Inherited::initMethod(ePhase);
82 if(ePhase == TypeObject::SystemPost)
84 RenderAction::registerEnterDefault(
85 StackedTransform::getClassType(),
86 reinterpret_cast<Action::Callback>(
87 &StackedTransform::renderEnter));
89 RenderAction::registerLeaveDefault(
90 StackedTransform::getClassType(),
91 reinterpret_cast<Action::Callback>(
92 &StackedTransform::renderLeave));
95 IntersectAction::registerEnterDefault(
96 getClassType(),
97 reinterpret_cast<Action::Callback>(
98 &StackedTransform::intersectEnter));
100 IntersectAction::registerLeaveDefault(
101 getClassType(),
102 reinterpret_cast<Action::Callback>(
103 &StackedTransform::intersectLeave));
108 /***************************************************************************\
109 * Instance methods *
110 \***************************************************************************/
112 void StackedTransform::updateTransform(void)
114 _mTransformation.setIdentity();
116 for(UInt32 i =0; i < getMFTransformElements()->size(); ++i)
118 getTransformElements(i)->accumulateMatrix(_mTransformation);
121 invalidateVolume();
124 void StackedTransform::accumulateMatrix(Matrix &result)
126 result.mult(_mTransformation);
129 void StackedTransform::adjustVolume(Volume &volume)
131 volume.transform(_mTransformation);
134 TransformationElement *StackedTransform::getElement(
135 const std::string &szName) const
137 const Char8 *szElemName = NULL;
139 for(UInt32 i = 0; i < getMFTransformElements()->size(); ++i)
141 szElemName = getName(getTransformElements(i));
143 if(szElemName != NULL && szName.compare(szElemName) == 0)
145 return getTransformElements(i);
149 return NULL;
152 void StackedTransform::pushToNamedTransformElements(
153 TransformationElement * const pValue,
154 std::string const & szName)
156 setName(pValue, szName.c_str());
158 Inherited::pushToTransformElements(pValue);
161 void StackedTransform::insertIntoNamedTransformElements(
162 UInt32 uiIndex,
163 TransformationElement * const pValue,
164 std::string const & szName)
166 setName(pValue, szName.c_str());
168 Inherited::insertIntoTransformElements(uiIndex, pValue);
171 void StackedTransform::replaceInNamedTransformElements(
172 UInt32 uiIndex,
173 TransformationElement * const pValue,
174 std::string const & szName)
176 setName(pValue, szName.c_str());
178 Inherited::replaceInTransformElements(uiIndex, pValue);
181 void StackedTransform::replaceObjInNamedTransformElements(
182 TransformationElement * const pOldElem,
183 TransformationElement * const pNewElem,
184 std::string const & szName )
186 setName(pNewElem, szName.c_str());
188 Inherited::replaceObjInTransformElements(pOldElem, pNewElem);
191 void StackedTransform::removeFromNamedTransformElements(
192 const std::string &szName)
194 const Char8 *szElemName = NULL;
196 for(UInt32 i = 0; i < getMFTransformElements()->size(); ++i)
198 szElemName = getName(getTransformElements(i));
200 if(szElemName != NULL && szName.compare(szElemName) == 0)
202 Inherited::removeFromTransformElements(i);
204 return;
209 /*-------------------------------------------------------------------------*/
210 /* Render */
212 Action::ResultE StackedTransform::renderEnter(Action *action)
214 RenderAction *pAction = dynamic_cast<RenderAction *>(action);
216 pAction->pushVisibility();
218 pAction->pushMatrix(this->_mTransformation);
220 return Action::Continue;
223 Action::ResultE StackedTransform::renderLeave(Action *action)
225 RenderAction *pAction = dynamic_cast<RenderAction *>(action);
227 pAction->popVisibility();
229 pAction->popMatrix();
231 return Action::Continue;
234 /*-------------------------------------------------------------------------*/
235 /* Intersect */
237 Action::ResultE StackedTransform::intersectEnter(Action *action)
239 // Use parent class for trivial reject
240 if(Inherited::intersectEnter(action) == Action::Skip)
241 return Action::Skip;
243 // Need to check children
244 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
245 Matrix m = this->_mTransformation;
247 m.invert();
249 Pnt3f pos;
250 Vec3f dir;
252 m.multFull(ia->getLine().getPosition (), pos);
253 m.mult (ia->getLine().getDirection(), dir);
255 Real32 length = dir.length();
257 if(length < TypeTraits<Real32>::getDefaultEps())
258 SWARNING << "StackedTransform::intersectEnter: Near-zero scale!"
259 << std::endl;
261 ia->setLine(Line(pos, dir), ia->getMaxDist());
262 ia->scale (length );
264 return Action::Continue;
267 Action::ResultE StackedTransform::intersectLeave(Action *action)
269 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
270 Matrix m = this->_mTransformation;
272 Pnt3f pos;
273 Vec3f dir;
275 m.multFull(ia->getLine().getPosition (), pos);
276 m.mult (ia->getLine().getDirection(), dir);
278 ia->setLine(Line(pos, dir), ia->getMaxDist());
279 ia->scale(dir.length());
281 return Action::Continue;
284 /*-------------------------------------------------------------------------*\
285 - private -
286 \*-------------------------------------------------------------------------*/
288 /*----------------------- constructors & destructors ----------------------*/
290 StackedTransform::StackedTransform(void) :
291 Inherited (),
292 _mTransformation()
294 _mTransformation.setIdentity();
297 StackedTransform::StackedTransform(const StackedTransform &source) :
298 Inherited (source ),
299 _mTransformation(source._mTransformation)
303 StackedTransform::~StackedTransform(void)
307 /*----------------------------- class specific ----------------------------*/
309 void StackedTransform::changed(ConstFieldMaskArg whichField,
310 UInt32 origin,
311 BitVector details)
313 Inherited::changed(whichField, origin, details);
315 if((whichField & TransformElementsFieldMask))
317 updateTransform();
321 void StackedTransform::dump( UInt32 ,
322 const BitVector ) const
324 SLOG << "Dump StackedTransform NI" << std::endl;
327 OSG_END_NAMESPACE