AGXDynamics  2.35.0.2
agxOSG/PickHandler.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
20 #include <osgText/Text>
21 #include <osg/Geometry>
22 #include <osgViewer/Viewer>
23 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
24 
25 #include <agxOSG/GeometryNode.h>
26 
27 #include <agx/RigidBody.h>
28 #include <agxSDK/PickHandler.h>
29 
30 namespace agxOSG
31 {
32  class AGXOSG_EXPORT Panel : public osg::Geode
33  {
34  public:
35  Panel();
36  Panel( const agx::Vec2& position, const agx::Vec2& size, const agx::Vec4& color );
37 
38  void setPosition( const agx::Vec2& position );
39  const agx::Vec2& getPosition() const { return m_position; }
40 
41  void setSize( const agx::Vec2& size );
42  const agx::Vec2& getSize() const { return m_size; }
43 
44  void setColor( const agx::Vec4& color );
45  agx::Vec4 getColor() const;
46 
47  virtual void uninitialize( osg::Group* sceneData ) = 0;
48  virtual bool isValid() const = 0;
49  virtual void update() = 0;
50  virtual bool match( const void* obj ) const = 0;
51 
52  protected:
53  virtual ~Panel() {}
54  void initialize();
55 
56  protected:
59 
60  osg::ref_ptr<osg::Geometry> m_geometry;
61  osg::ref_ptr<osg::Vec3Array> m_vertices;
62  osg::ref_ptr<osg::DrawElementsUInt> m_indices;
63  osg::ref_ptr<osg::Vec4Array> m_color;
64  osg::ref_ptr<osg::Vec3Array> m_normals;
65  };
66 
67  typedef osg::ref_ptr< Panel > PanelRef;
68 
70 
71  // class to handle events with a pick
72 
73  // Testclass for the new design of PickHandler
76  {
77  public:
79 
80  PickHandler( osgViewer::Viewer *viewer, osg::Group *scene=nullptr);
81 
82 
84  void reset();
85 
87  void setActivateModKeyMask(unsigned int modKey );
88 
90  void setActivateMouseButton(unsigned int mouse);
91 
93  unsigned int getActivateModKeyMask() const;
94 
96  unsigned int getActivateMouseButton() const;
97 
100  agxSDK::PickResult intersect(float screenX, float screenY, bool onlyDynamics);
101 
103  void getNearFarPoints(const osg::Camera* camera, agx::Vec3& near, agx::Vec3& far, float x, float y, bool useOldMatrix = false) const;
104 
106  const osg::Camera *getCamera() const;
107 
108  virtual bool keyboard(int key, unsigned int modKeyMask, float x, float y, bool keydown);
109 
115  virtual bool mouse(MouseButtonMask button, MouseState state, float x, float y);
116  virtual bool mouseDragged(MouseButtonMask buttonMask, float x, float y);
117  virtual void update(float x, float y);
118 
119  protected:
120 
121 
122  virtual ~PickHandler();
123 
124  private:
125 
126  unsigned int m_modKeyMask;
127  unsigned int m_key;
128  osg::Matrix m_cameraMatrix;
129  osg::observer_ptr< osgViewer::Viewer > m_viewer;
130  osg::observer_ptr< osg::Group > m_scene;
131  osg::observer_ptr< osg::Camera > m_camera;
132 
133  unsigned int m_activateModKeyMask;
134  unsigned int m_activateMouseKey;
135 
136 
137  public: // Old interface
138  virtual agxSDK::PickResult pick( float screenX, float screenY );
139 
140 
141  void createMouseSpringRendering(float scale);
143  void updateRenderState();
144  void setCamera(osg::Camera *camera);
145 
146  private:
147  void init();
148 
149  bool isInitialized() const { return m_initialized; }
150  void updateClickedGeometry(MouseState state, float x, float y);
151 
152  // void createConstraint( const agx::Vec3& worldPoint );
153  bool intersect( float screenX, float screenY, const osg::Camera* camera, agxCollide::LocalGeometryContactVector& result ) const;
154  void startPick(float x, float y);
155  void updatePanels(float x, float y);
156  void updateUnphysical(agx::Vec3 direction, agx::Vec3 currentMousePoint);
157 
158 
159  void traverseParticles();
160 
161  // Internal helper methods for mouse handling.
162  bool mouseDown(MouseButtonMask button, float x, float y);
163  bool mouseDownLeftButton(float x, float y);
164  bool mouseDownMiddleButton(float x, float y);
165  bool mouseDownRightButton(float x, float y);
166 
167 
168 
169  private: // Old interface
170 
171  agx::RigidBodyRef m_mousePickBody;
172 
173  bool m_initialized;
174  bool m_useLock;
175  bool m_moveUnphysically;
176  bool m_inDragging;
177 
178  agx::Vec3 m_rotationFromStart;
179  agx::Quat m_oldBodyRotation;
180  agx::Vec3 m_oldBodyCMOffset;
181  agx::Vec3 m_localPoint;
182  agx::Real m_distance;
183 
184 
185 
186  osg::PositionAttitudeTransform *m_mouseAttachTransform;
187  osg::PositionAttitudeTransform *m_mousePositionTransform;
188  osg::Geometry *m_mouseSpringLine;
189  osg::observer_ptr< osg::Switch > m_mouseMarkerSwitch;
190  PanelContainer m_panels;
191 
192  osg::observer_ptr< osg::Switch > m_sphereSwitch;
193 
194  float m_mousePickingGraphicsScale;
195 
196  };
197 }
agxSDK::PickResult
Definition: agxSDK/GuiEventListener.h:31
agxOSG::Panel::m_indices
osg::ref_ptr< osg::DrawElementsUInt > m_indices
Definition: agxOSG/PickHandler.h:62
agxOSG::Panel::m_position
agx::Vec2 m_position
Definition: agxOSG/PickHandler.h:57
agxOSG::Panel::~Panel
virtual ~Panel()
Definition: agxOSG/PickHandler.h:53
agx::Real
double Real
Definition: Real.h:42
agxOSG::Panel::getPosition
const agx::Vec2 & getPosition() const
Definition: agxOSG/PickHandler.h:39
agx::Vector< PanelRef >
agxOSG::PickHandler::setSimulation
void setSimulation(agxSDK::Simulation *simulation)
Definition: agxOSG/PickHandler.h:142
agxOSG::PickHandler
Definition: agxOSG/PickHandler.h:75
agxSDK::GuiEventListener::intersect
agxSDK::PickResult intersect(const agx::Vec3 &start, const agx::Vec3 &end, bool onlyDynamics)
Shoot a ray from start to end into the space and calculate the closest geometry.
agx::Vec4T
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
agxOSG::Panel::m_geometry
osg::ref_ptr< osg::Geometry > m_geometry
Definition: agxOSG/PickHandler.h:60
agxOSG::PickHandler::base
agxSDK::PickHandler base
Definition: agxOSG/PickHandler.h:78
agxOSG::Panel
Definition: agxOSG/PickHandler.h:32
agxSDK::EventListener::setSimulation
void setSimulation(Simulation *simulation)
Set the associated simulation for this EventListener.
agx::ref_ptr< RigidBody >
agxSDK::PickHandler
Definition: agxSDK/PickHandler.h:36
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agxOSG
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
PushDisableWarnings.h
agxOSG::Panel::m_normals
osg::ref_ptr< osg::Vec3Array > m_normals
Definition: agxOSG/PickHandler.h:64
agxOSG::Panel::m_vertices
osg::ref_ptr< osg::Vec3Array > m_vertices
Definition: agxOSG/PickHandler.h:61
GeometryNode.h
m_size
#define m_size
Definition: Vector.h:429
agx::Vec2T
Definition: Vec2Template.h:30
agxOSG::Panel::m_size
agx::Vec2 m_size
Definition: agxOSG/PickHandler.h:58
agxOSG::PanelContainer
agx::Vector< PanelRef > PanelContainer
Definition: agxOSG/PickHandler.h:69
agx::QuatT< Real >
RigidBody.h
agxSDK::GuiEventListener::MouseButtonMask
MouseButtonMask
Definition: agxSDK/GuiEventListener.h:63
agxOSG::Panel::getSize
const agx::Vec2 & getSize() const
Definition: agxOSG/PickHandler.h:42
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agxOSG::Panel::m_color
osg::ref_ptr< osg::Vec4Array > m_color
Definition: agxOSG/PickHandler.h:63
PickHandler.h
agxBrick::isInitialized
AGXBRICK_EXPORT bool isInitialized()
agxOSG::getNearFarPoints
AGXOSG_EXPORT void getNearFarPoints(const osg::Camera *camera, float x, float y, agx::Vec3 &near, agx::Vec3 &far)
Get the near and far points of the current window, given a camera and x and y position.
agxOSG::PanelRef
osg::ref_ptr< Panel > PanelRef
Definition: agxOSG/PickHandler.h:67
agx::init
void AGXPHYSICS_EXPORT init()
Initialize agx.
agxSDK::GuiEventListener::MouseState
MouseState
Definition: agxSDK/GuiEventListener.h:56
agx::Vec3T< Real >
AGXOSG_EXPORT
#define AGXOSG_EXPORT
PopDisableWarnings.h