fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / Misc / OSGInverseTransform.cpp
blobb7e850a2f26b0fa7f921606293a6e3c39f953492
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 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"
47 #include "OSGGL.h"
49 #include "OSGInverseTransform.h"
50 #include "OSGIntersectAction.h"
51 #include "OSGRenderAction.h"
52 #include "OSGNode.h"
54 OSG_USING_NAMESPACE
56 // Documentation for this class is emited in the
57 // OSGInverseTransformBase.cpp file.
58 // To modify it, please change the .fcd file (OSGInverseTransform.fcd) and
59 // regenerate the base file.
61 /***************************************************************************\
62 * Class variables *
63 \***************************************************************************/
65 /***************************************************************************\
66 * Class methods *
67 \***************************************************************************/
69 void InverseTransform::initMethod(InitPhase ePhase)
71 Inherited::initMethod(ePhase);
73 if(ePhase == TypeObject::SystemPost)
75 IntersectAction::registerEnterDefault(
76 getClassType(),
77 reinterpret_cast<Action::Callback>(
78 &InverseTransform::intersectEnter));
80 IntersectAction::registerLeaveDefault(
81 getClassType(),
82 reinterpret_cast<Action::Callback>(
83 &InverseTransform::intersectLeave));
85 RenderAction::registerEnterDefault(
86 InverseTransform::getClassType(),
87 reinterpret_cast<Action::Callback>(&InverseTransform::renderEnter));
89 RenderAction::registerLeaveDefault(
90 InverseTransform::getClassType(),
91 reinterpret_cast<Action::Callback>(&InverseTransform::renderLeave));
96 /***************************************************************************\
97 * Instance methods *
98 \***************************************************************************/
100 /*-------------------------------------------------------------------------*\
101 - private -
102 \*-------------------------------------------------------------------------*/
104 /*----------------------- constructors & destructors ----------------------*/
106 InverseTransform::InverseTransform(void) :
107 Inherited(),
108 _invWorld ()
112 InverseTransform::InverseTransform(const InverseTransform &source) :
113 Inherited(source ),
114 _invWorld (source._invWorld)
118 InverseTransform::~InverseTransform(void)
122 /*----------------------------- class specific ----------------------------*/
124 void InverseTransform::changed(ConstFieldMaskArg whichField,
125 UInt32 origin,
126 BitVector details)
128 Inherited::changed(whichField, origin, details);
131 void InverseTransform::dump( UInt32 uiIndent,
132 const BitVector bvFlags ) const
134 Inherited::dump(uiIndent, bvFlags);
137 /*------------------------- volume update -------------------------------*/
139 void InverseTransform::adjustVolume(Volume &volume)
141 volume.transform(_invWorld);
144 void InverseTransform::accumulateMatrix(Matrix &result)
146 result.mult(_invWorld);
149 /*------------------------- calc matrix ---------------------------------*/
151 void InverseTransform::calcMatrix(const Matrix &mToWorld,
152 Matrix &mResult)
154 mResult.invertFrom(mToWorld);
156 _invWorld = mResult; // remember dynamically set matrix field
159 void InverseTransform::initMatrix(const Matrix &mToWorld)
161 _invWorld.invertFrom(mToWorld);
164 /*-------------------------------------------------------------------------*/
165 /* Draw */
167 /*-------------------------------------------------------------------------*/
168 /* Intersect */
170 Action::ResultE InverseTransform::intersectEnter(Action *action)
172 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
173 Matrix m(_invWorld);
175 m.invert();
177 Pnt3f pos;
178 Vec3f dir;
180 m.multFull(ia->getLine().getPosition (), pos);
181 m.mult (ia->getLine().getDirection(), dir);
183 ia->setLine(Line(pos, dir), ia->getMaxDist());
184 ia->scale(dir.length());
186 return Action::Continue;
189 Action::ResultE InverseTransform::intersectLeave(Action *action)
191 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
192 Matrix m(_invWorld);
194 Pnt3f pos;
195 Vec3f dir;
197 m.multFull(ia->getLine().getPosition (), pos);
198 m.mult (ia->getLine().getDirection(), dir);
200 ia->setLine(Line(pos, dir), ia->getMaxDist());
201 ia->scale(dir.length());
203 return Action::Continue;
206 /*-------------------------------------------------------------------------*/
207 /* Render */
209 Action::ResultE InverseTransform::renderEnter(Action *action)
211 RenderAction *pAction =
212 dynamic_cast<RenderAction *>(action);
214 Matrix mMat; // will be set to World^-1
216 calcMatrix(pAction->topMatrix(), mMat);
218 pAction->pushVisibility();
220 pAction->pushMatrix(mMat);
222 return Action::Continue;
225 Action::ResultE InverseTransform::renderLeave(Action *action)
227 RenderAction *pAction =
228 dynamic_cast<RenderAction *>(action);
230 pAction->popVisibility();
232 pAction->popMatrix();
234 return Action::Continue;