MEPP2 Project
PickHandler.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <osg/io_utils>
14 //#include <osgText/Text>
17 
18 //#include <sstream>
19 
20 namespace FEVV {
21 
22 // class to handle events with a pick
23 class PickHandler : public osgGA::GUIEventHandler
24 {
25 public:
27  osgText::Text *updateText)
28  : _smpViewer(smpViewer), _updateText(updateText)
29  {
30  }
31 
33 
34  bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa);
35 
36  virtual void pick(osgViewer::View *view, const osgGA::GUIEventAdapter &ea);
37 
38  void setLabel(const std::string &name)
39  {
40  if(_updateText.get())
41  _updateText->setText(name);
42  }
43 
44 protected:
46  osg::ref_ptr< osgText::Text > _updateText;
47 };
48 
49 
50 inline
51 bool
52 PickHandler::handle(const osgGA::GUIEventAdapter &ea,
53  osgGA::GUIActionAdapter &aa)
54 {
55  if(ea.getModKeyMask() == osgGA::GUIEventAdapter::MODKEY_SHIFT)
56  {
57  switch(ea.getEventType())
58  {
59  case(osgGA::GUIEventAdapter::PUSH):
60  {
61  osgViewer::View *view = dynamic_cast< osgViewer::View * >(&aa);
62  if(view)
63  pick(view, ea);
64  return false;
65  }
66  /*case(osgGA::GUIEventAdapter::KEYDOWN):
67  {
68  if (ea.getKey()=='c')
69  {
70  osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
71  osg::ref_ptr<osgGA::GUIEventAdapter> event = new
72  osgGA::GUIEventAdapter(ea); event->setX((ea.getXmin()+ea.getXmax())*0.5);
73  event->setY((ea.getYmin()+ea.getYmax())*0.5);
74  if (view) pick(view,*event);
75  }
76  return false;
77  }*/
78  default:
79  return false;
80  }
81  }
82 
83  return false;
84 }
85 
86 
87 inline
88 void
89 PickHandler::pick(osgViewer::View *view,
90  const osgGA::GUIEventAdapter &ea)
91 {
92  osgUtil::LineSegmentIntersector::Intersections intersections;
93 
94  osg::ref_ptr< osg::Node > node;
95 
96  std::string gdlist = "";
97 
98  if(view->computeIntersections(ea, intersections))
99  {
100  for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr =
101  intersections.begin();
102  hitr != intersections.end();
103  ++hitr)
104  {
105  std::ostringstream os;
106 
107  if(!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty()))
108  {
109  // the geodes are identified by name.
110  os << "Object \"" << hitr->nodePath.back()->getName() << "\""
111  << std::endl;
112 
113  if(hitr->nodePath.back()->getDescription(0) == "MESH")
114  {
115  std::cout << "Object \"" << hitr->nodePath.back()->getName() << "\""
116  << std::endl;
117 
118  node = hitr->drawable->getParent(0);
119 
120  // ------------------------------------------------------------------------------------------
122  node)) // [PICK] : IMPORTANT test --> see keyword [PICK] in
123  // SimpleWindow.inl
124  {
125  std::vector< osg::Geode * > geodes =
127  for(unsigned i = 0; i < geodes.size(); i++)
128  {
129  _smpViewer->setNodeSelected(geodes[i], false);
130  // std::cout << "FALSE : " << geodes[i]->getName() << " - " <<
131  // geodes[i] << std::endl;
132  }
133 
134  _smpViewer->setNodeSelected(node, true);
135  // std::cout << "TRUE : " << node->getName() << " - " << node <<
136  // std::endl;
137 
138  FEVV::SimpleWindow *sw =
139  static_cast< FEVV::SimpleWindow * >(_smpViewer->getWindow());
140  sw->update(true);
141  }
142 
143  // std::cout << std::endl;
144  // ------------------------------------------------------------------------------------------
145 
146  break; // TEMP : add for only one intersection
147  }
148  }
149  else if(hitr->drawable.valid())
150  {
151  os << "Object \"" << hitr->drawable->className() << "\"" << std::endl;
152 
153  // std::cout<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl;
154  }
155 
156  os << " local coords vertex(" << hitr->getLocalIntersectPoint()
157  << ")"
158  << " normal(" << hitr->getLocalIntersectNormal() << ")" << std::endl;
159  os << " world coords vertex(" << hitr->getWorldIntersectPoint()
160  << ")"
161  << " normal(" << hitr->getWorldIntersectNormal() << ")" << std::endl;
162 
163  const osgUtil::LineSegmentIntersector::Intersection::IndexList &vil =
164  hitr->indexList;
165  for(unsigned int i = 0; i < vil.size(); ++i)
166  {
167  os << " vertex indices [" << i << "] = " << vil[i] << std::endl;
168  }
169 
170  gdlist += os.str();
171  }
172  }
173 
174  setLabel(gdlist);
175 }
176 
177 } // namespace FEVV
FEVV::PickHandler::~PickHandler
~PickHandler()
Definition: PickHandler.h:32
FEVV::PickHandler::setLabel
void setLabel(const std::string &name)
Definition: PickHandler.h:38
FEVV::SimpleViewer::getSelectedGeodes
std::vector< osg::Geode * > getSelectedGeodes()
Definition: SimpleViewer.inl:375
SimpleWindow.h
FEVV::PickHandler::_updateText
osg::ref_ptr< osgText::Text > _updateText
Definition: PickHandler.h:46
FEVV::BaseViewer::getWindow
Window * getWindow()
Definition: BaseViewer.h:164
FEVV::SimpleWindow
SimpleWindow is a specialization of QMainWindow. This class the Main Window.
Definition: SimpleWindow.h:80
FEVV::SimpleViewer::setNodeSelected
void setNodeSelected(osg::Node *_geode, bool isSelected) override
Definition: SimpleViewer.inl:3492
FEVV::SimpleViewer
SimpleViewer is a specialization of osgViewer::CompositeViewer. This class is a widget where we are a...
Definition: SimpleViewer.h:180
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::PickHandler::PickHandler
PickHandler(FEVV::SimpleViewer *smpViewer, osgText::Text *updateText)
Definition: PickHandler.h:26
FEVV::PickHandler::handle
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
Definition: PickHandler.h:52
FEVV::SimpleViewer::isNodeSelected
bool isNodeSelected(osg::Node *_geode) override
Definition: SimpleViewer.inl:3584
FEVV::PickHandler
Definition: PickHandler.h:24
SimpleViewer.h
FEVV::PickHandler::pick
virtual void pick(osgViewer::View *view, const osgGA::GUIEventAdapter &ea)
Definition: PickHandler.h:89
FEVV::SimpleWindow::update
void update(bool pick=false)
Definition: SimpleWindow.inl:676
FEVV::PickHandler::_smpViewer
FEVV::SimpleViewer * _smpViewer
Definition: PickHandler.h:45