fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / Misc / OSGTransform.cpp
blob224b2fbd9e3cf6f607298f81be994b19492339e8
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 #include <cstdlib>
40 #include <cstdio>
42 #include "OSGConfig.h"
44 #include "OSGGL.h"
46 #include "OSGIntersectAction.h"
48 #include "OSGRenderAction.h"
49 #include "OSGTransform.h"
50 #include "OSGVolume.h"
52 OSG_USING_NAMESPACE
54 // Documentation for this class is emited in the
55 // OSGTransformBase.cpp file.
56 // To modify it, please change the .fcd file (OSGTransform.fcd) and
57 // regenerate the base file.
59 /*-------------------------------------------------------------------------*/
60 /* Sync */
62 void Transform::changed(ConstFieldMaskArg whichField,
63 UInt32 origin,
64 BitVector details)
66 if(whichField & MatrixFieldMask)
68 invalidateVolume();
71 Inherited::changed(whichField, origin, details);
74 /*-------------------------------------------------------------------------*/
75 /* Helper */
77 void Transform::accumulateMatrix(Matrix &result)
79 result.mult(getMatrix());
82 void Transform::adjustVolume(Volume &volume)
84 volume.transform(_sfMatrix.getValue());
87 /*-------------------------------------------------------------------------*/
88 /* Dump */
90 void Transform::dump( UInt32 uiIndent,
91 const BitVector bvFlags ) const
93 Inherited::dump(uiIndent, bvFlags);
96 /*-------------------------------------------------------------------------*/
97 /* Constructors */
99 Transform::Transform(void) :
100 Inherited()
102 _sfMatrix.getValue().setIdentity();
105 Transform::Transform(const Transform &source) :
106 Inherited(source)
110 /*-------------------------------------------------------------------------*/
111 /* Destructor */
113 Transform::~Transform(void)
118 /*-------------------------------------------------------------------------*/
119 /* Render */
121 Action::ResultE Transform::renderEnter(Action *action)
123 RenderAction *pAction =
124 dynamic_cast<RenderAction *>(action);
126 pAction->pushVisibility();
128 pAction->pushMatrix(this->getMatrix());
130 return Action::Continue;
133 Action::ResultE Transform::renderLeave(Action *action)
135 RenderAction *pAction =
136 dynamic_cast<RenderAction *>(action);
138 pAction->popVisibility();
140 pAction->popMatrix();
142 return Action::Continue;
145 /*-------------------------------------------------------------------------*/
146 /* Intersect */
148 Action::ResultE Transform::intersectEnter(Action *action)
150 // Use parent class for trivial reject
151 if(Inherited::intersectEnter(action) == Action::Skip)
152 return Action::Skip;
154 // Need to check children
155 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
156 Matrix m = this->getMatrix();
158 m.invert();
160 Pnt3f pos;
161 Vec3f dir;
163 m.multFull(ia->getLine().getPosition (), pos);
164 m.mult (ia->getLine().getDirection(), dir);
166 Real32 length = dir.length();
168 if(length < TypeTraits<Real32>::getDefaultEps())
169 SWARNING << "Transform::intersectEnter: Near-zero scale!" << std::endl;
171 ia->setLine(Line(pos, dir), ia->getMaxDist());
172 ia->scale (length );
174 return Action::Continue;
177 Action::ResultE Transform::intersectLeave(Action *action)
179 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
180 Matrix m = this->getMatrix();
182 Pnt3f pos;
183 Vec3f dir;
185 m.multFull(ia->getLine().getPosition (), pos);
186 m.mult (ia->getLine().getDirection(), dir);
188 ia->setLine(Line(pos, dir), ia->getMaxDist());
189 ia->scale(dir.length());
191 return Action::Continue;
193 /*-------------------------------------------------------------------------*/
194 /* Init */
196 void Transform::initMethod(InitPhase ePhase)
198 Inherited::initMethod(ePhase);
200 if(ePhase == TypeObject::SystemPost)
202 IntersectAction::registerEnterDefault(
203 getClassType(),
204 reinterpret_cast<Action::Callback>(&Transform::intersectEnter));
205 IntersectAction::registerLeaveDefault(
206 getClassType(),
207 reinterpret_cast<Action::Callback>(&Transform::intersectLeave));
209 RenderAction::registerEnterDefault(
210 Transform::getClassType(),
211 reinterpret_cast<Action::Callback>(&Transform::renderEnter));
212 RenderAction::registerLeaveDefault(
213 Transform::getClassType(),
214 reinterpret_cast<Action::Callback>(&Transform::renderLeave));