changed: gcc8 base update
[opensg.git] / Source / Contrib / Manipulators / OSGMoveManipulator.cpp
blobacbba0a91273be8d98d2db4fcc7628e7add672e6
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 <stdlib.h>
44 #include <stdio.h>
46 #define OSG_COMPILEMANIPULATORSLIB
48 #include "OSGConfig.h"
49 #include "OSGRenderAction.h"
50 #include "OSGIntersectAction.h"
51 #include "OSGCamera.h"
53 #include "OSGMoveManipulator.h"
55 OSG_USING_NAMESPACE
57 /***************************************************************************\
58 * Description *
59 \***************************************************************************/
61 /*! \class OSG::MoveManipulator
62 * The MoveHandle is used for moving objects. It consist of three axis which
63 * can be picked and translated and one center box to translate freely in 3D.
66 /***************************************************************************\
67 * Class variables *
68 \***************************************************************************/
70 /***************************************************************************\
71 * Class methods *
72 \***************************************************************************/
74 void MoveManipulator::initMethod(InitPhase ePhase)
76 Inherited::initMethod(ePhase);
78 if(ePhase == TypeObject::SystemPost)
80 IntersectAction::registerEnterDefault(
81 getClassType(),
82 reinterpret_cast<Action::Callback>(
83 &MoveManipulator::intersectEnter));
85 IntersectAction::registerLeaveDefault(
86 getClassType(),
87 reinterpret_cast<Action::Callback>(
88 &MoveManipulator::intersectLeave));
90 RenderAction::registerEnterDefault(
91 getClassType(),
92 reinterpret_cast<Action::Callback>(&MoveManipulator::renderEnter));
94 RenderAction::registerLeaveDefault(
95 getClassType(),
96 reinterpret_cast<Action::Callback>(&MoveManipulator::renderLeave));
101 /***************************************************************************\
102 * Instance methods *
103 \***************************************************************************/
105 /*-------------------------------------------------------------------------*\
106 - private -
107 \*-------------------------------------------------------------------------*/
109 /*----------------------- constructors & destructors ----------------------*/
111 MoveManipulator::MoveManipulator(void) :
112 Inherited()
116 MoveManipulator::MoveManipulator(const MoveManipulator &source) :
117 Inherited(source)
121 MoveManipulator::~MoveManipulator(void)
125 /*----------------------------- class specific ----------------------------*/
127 void MoveManipulator::changed(ConstFieldMaskArg whichField,
128 UInt32 origin,
129 BitVector details)
131 Inherited::changed(whichField, origin, details);
134 void MoveManipulator::dump( UInt32 uiIndent,
135 const BitVector bvFlags ) const
137 Inherited::dump(uiIndent, bvFlags);
140 NodeTransitPtr MoveManipulator::makeHandleGeo()
142 return makeCone(0.75f, 0.1f, 12, true, true);
145 /*! The mouseMove is called by the viewer when the mouse is moved in the
146 viewer and this handle is the active one.
148 \param x the x-pos of the mouse (pixel)
149 \param y the y-pos of the mouse (pixel)
151 void MoveManipulator::mouseMove(const Int16 x,
152 const Int16 y)
154 //SLOG << "==============================" << endLog;
155 //SLOG << "Manipulator::mouseMove() enter x=" << x << " y=" << y << endLog;
157 // get the beacon's core (must be ComponentTransform) and it's center
158 if( getTarget() != NULL )
160 // get transformation of beacon
161 Transform *t = dynamic_cast<Transform *>(getTarget()->getCore());
163 if( t != NULL )
165 Vec3f translation; // for matrix decomposition
166 Quaternion rotation;
167 Vec3f scaleFactor;
168 Quaternion scaleOrientation;
170 t->getMatrix().getTransform(translation, rotation, scaleFactor,
171 scaleOrientation);
173 OSG::Line viewray;
174 getViewport()->getCamera()->calcViewRay(viewray, x, y, *getViewport());
176 //SLOG << "Manipulator::mouseMove(): viewray: " << viewray << endLog;
178 Line axis(getAxisBase(), getAxisDirection()); // HACK: Should add a Line Fieldtype
180 //SLOG << "Manipulator::mouseMove(): axis: " << axis << endLog;
182 Real32 axist, viewrayt;
184 axis.getClosestPoints(viewray, axist, viewrayt);
186 // Only accept cases where the closest point is not behind the viewer
187 if (viewrayt >= 0)
189 axist /= getAxisDirection().length();
191 //SLOG << "Manipulator::mouseMove(): axist=" << axist << " viewrayt=" << viewrayt <<endLog;
193 Vec3f rot_axis;
195 rotation.multVec(getActiveAxis(), rot_axis);
196 Vec3f trans = getBaseTranslation() + rot_axis * axist;
198 Matrix m;
200 m.setTransform(trans, rotation, scaleFactor, scaleOrientation);
202 t->setMatrix(m);
205 else
207 SWARNING << "handled object has no parent transform!\n";
209 callExternalUpdateHandler();
211 else
213 SWARNING << "Handle has no target.\n";
216 setLastMousePos(Pnt2f(Real32(x), Real32(y)));
217 updateHandleTransform();
219 //SLOG << "Manipulator::mouseMove() leave\n" << std::flush;
222 /*! The mouseButtonPress is called by the viewer when the mouse is
223 pressed in the viewer above a subhandle of this handle.
225 \param button the button pressed
226 \param x the x-pos of the mouse (pixel)
227 \param y the y-pos of the mouse (pixel)
230 void MoveManipulator::mouseButtonPress(const UInt16 button,
231 const Int16 x,
232 const Int16 y )
234 Transform *t = dynamic_cast<Transform *>(getTarget()->getCore());
236 if (t == NULL)
238 SWARNING << "Manipulator::mouseButtonPress() target is not a Transform!" << endLog;
239 return;
242 //SLOG << "Manipulator::mouseButtonPress() button=" << button << " x=" << x << " y=" << y << std::endl << endLog;
244 OSG::Line viewray;
245 getViewport()->getCamera()->calcViewRay(viewray, x, y, *getViewport());
247 OSG::Node *scene = getTarget();
248 while (scene->getParent() != 0)
250 scene = scene->getParent();
253 OSG::IntersectActionRefPtr act = OSG::IntersectAction::create();
254 act->setLine( viewray );
255 act->apply( scene );
257 //SLOG << "Manipulator::mouseButtonPress() viewray=" << viewray << " scene=" << scene << endLog;
259 if ( act->didHit() )
261 //SLOG << "Manipulator::mouseButtonPress() hit! at " << act->getHitPoint() << endLog;
263 // Get manipulator axis into world space
264 OSG::Matrix m = getTarget()->getToWorld();
266 Pnt3f origin(0,0,0), base, dummy;
267 Vec3f dir;
269 m.multFull(origin, base);
270 m.multFull(getActiveAxis(), dir);
272 Line axis(base, dir);
274 //SLOG << "Manipulator::mouseButtonPress() world axis=" << axis << endLog;
276 Pnt3f apoint;
277 axis.getClosestPoints(viewray, apoint, dummy);
279 //SLOG << "Manipulator::mouseButtonPress() apoint " << apoint << endLog;
281 setAxisBase(apoint);
282 setAxisDirection(dir);
284 Vec3f translation; // for matrix decomposition
285 Quaternion rotation;
286 Vec3f scaleFactor;
287 Quaternion scaleOrientation;
289 t->getMatrix().getTransform(translation, rotation, scaleFactor,
290 scaleOrientation);
292 setBaseTranslation(translation);
294 setActive(true);
297 act = NULL;
300 void MoveManipulator::mouseButtonRelease(const UInt16 button,
301 const Int16 x,
302 const Int16 y )
304 //SLOG << "Manipulator::mouseButtonRelease() button=" << button << " x=" << x << " y=" << y << std::endl << endLog;
305 setActive(false);