1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 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 //---------------------------------------------------------------------------
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"
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 /***************************************************************************\
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 /***************************************************************************\
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(
97 reinterpret_cast<Action::Callback
>(
98 &StackedTransform::intersectEnter
));
100 IntersectAction::registerLeaveDefault(
102 reinterpret_cast<Action::Callback
>(
103 &StackedTransform::intersectLeave
));
108 /***************************************************************************\
110 \***************************************************************************/
112 void StackedTransform::updateTransform(void)
114 _mTransformation
.setIdentity();
116 for(UInt32 i
=0; i
< getMFTransformElements()->size(); ++i
)
118 getTransformElements(i
)->accumulateMatrix(_mTransformation
);
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
);
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(
163 TransformationElement
* const pValue
,
164 std::string
const & szName
)
166 setName(pValue
, szName
.c_str());
168 Inherited::insertIntoTransformElements(uiIndex
, pValue
);
171 void StackedTransform::replaceInNamedTransformElements(
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
);
209 /*-------------------------------------------------------------------------*/
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 /*-------------------------------------------------------------------------*/
237 Action::ResultE
StackedTransform::intersectEnter(Action
*action
)
239 // Use parent class for trivial reject
240 if(Inherited::intersectEnter(action
) == Action::Skip
)
243 // Need to check children
244 IntersectAction
*ia
= dynamic_cast<IntersectAction
*>(action
);
245 Matrix m
= this->_mTransformation
;
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!"
261 ia
->setLine(Line(pos
, dir
), ia
->getMaxDist());
264 return Action::Continue
;
267 Action::ResultE
StackedTransform::intersectLeave(Action
*action
)
269 IntersectAction
*ia
= dynamic_cast<IntersectAction
*>(action
);
270 Matrix m
= this->_mTransformation
;
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 /*-------------------------------------------------------------------------*\
286 \*-------------------------------------------------------------------------*/
288 /*----------------------- constructors & destructors ----------------------*/
290 StackedTransform::StackedTransform(void) :
294 _mTransformation
.setIdentity();
297 StackedTransform::StackedTransform(const StackedTransform
&source
) :
299 _mTransformation(source
._mTransformation
)
303 StackedTransform::~StackedTransform(void)
307 /*----------------------------- class specific ----------------------------*/
309 void StackedTransform::changed(ConstFieldMaskArg whichField
,
313 Inherited::changed(whichField
, origin
, details
);
315 if((whichField
& TransformElementsFieldMask
))
321 void StackedTransform::dump( UInt32
,
322 const BitVector
) const
324 SLOG
<< "Dump StackedTransform NI" << std::endl
;