selection_handler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "selection_handler.h"
31 
34 
35 #include <ros/assert.h>
36 
37 #include <OgreSceneNode.h>
38 #include <OgreSceneManager.h>
39 #include <OgreManualObject.h>
40 #include <OgreWireBoundingBox.h>
41 #include <OgreEntity.h>
42 #include <OgreSubEntity.h>
43 
46 
47 #include <utility>
48 
49 namespace rviz
50 {
52  : context_(context), listener_(new Listener(this))
53 {
56 }
57 
59 {
60  S_Movable::iterator it = tracked_objects_.begin();
61  S_Movable::iterator end = tracked_objects_.end();
62  for (; it != end; ++it)
63  {
64  Ogre::MovableObject* m = *it;
65  m->setListener(nullptr);
66  }
67 
68  while (!boxes_.empty())
69  {
70  destroyBox(boxes_.begin()->first);
71  }
73 }
74 
75 void SelectionHandler::preRenderPass(uint32_t /*pass*/)
76 {
77  M_HandleToBox::iterator it = boxes_.begin();
78  M_HandleToBox::iterator end = boxes_.end();
79  for (; it != end; ++it)
80  {
81  Ogre::WireBoundingBox* box = it->second.second;
82  box->setVisible(false);
83  }
84 }
85 
86 void SelectionHandler::postRenderPass(uint32_t /*pass*/)
87 {
88  M_HandleToBox::iterator it = boxes_.begin();
89  M_HandleToBox::iterator end = boxes_.end();
90  for (; it != end; ++it)
91  {
92  Ogre::WireBoundingBox* box = it->second.second;
93  box->setVisible(true);
94  }
95 }
96 
97 
98 void SelectionHandler::addTrackedObjects(Ogre::SceneNode* node)
99 {
100  if (!node)
101  {
102  return;
103  }
104  // Loop over all objects attached to this node.
105  Ogre::SceneNode::ObjectIterator obj_it = node->getAttachedObjectIterator();
106  while (obj_it.hasMoreElements())
107  {
108  Ogre::MovableObject* obj = obj_it.getNext();
109  addTrackedObject(obj);
110  }
111  // Loop over and recurse into all child nodes.
112  Ogre::SceneNode::ChildNodeIterator child_it = node->getChildIterator();
113  while (child_it.hasMoreElements())
114  {
115  Ogre::SceneNode* child = dynamic_cast<Ogre::SceneNode*>(child_it.getNext());
116  addTrackedObjects(child);
117  }
118 }
119 
120 void SelectionHandler::addTrackedObject(Ogre::MovableObject* object)
121 {
122  tracked_objects_.insert(object);
123  object->setListener(listener_.get());
124 
126 }
127 
128 void SelectionHandler::removeTrackedObject(Ogre::MovableObject* object)
129 {
130  tracked_objects_.erase(object);
131  object->setListener(nullptr);
132 
134 }
135 
137 {
138  M_HandleToBox::iterator it = boxes_.begin();
139  M_HandleToBox::iterator end = boxes_.end();
140  for (; it != end; ++it)
141  {
142  V_AABB aabbs;
143  Picked p(it->first.first);
144  p.extra_handles.insert(it->first.second);
145  getAABBs(Picked(it->first.first), aabbs);
146 
147  if (!aabbs.empty())
148  {
149  Ogre::AxisAlignedBox combined;
150  V_AABB::iterator aabb_it = aabbs.begin();
151  V_AABB::iterator aabb_end = aabbs.end();
152  for (; aabb_it != aabb_end; ++aabb_it)
153  {
154  combined.merge(*aabb_it);
155  }
156 
157  createBox(std::make_pair(p.handle, it->first.second), combined, "RVIZ/Cyan");
158  }
159  }
160 }
161 
162 void SelectionHandler::getAABBs(const Picked& /*obj*/, V_AABB& aabbs)
163 {
164  S_Movable::iterator it = tracked_objects_.begin();
165  S_Movable::iterator end = tracked_objects_.end();
166  for (; it != end; ++it)
167  {
168  aabbs.push_back((*it)->getWorldBoundingBox());
169  }
170 }
171 
172 void SelectionHandler::destroyProperties(const Picked& /*obj*/, Property* /*parent_property*/)
173 {
174  for (int i = 0; i < properties_.size(); i++)
175  {
176  delete properties_.at(i);
177  }
178  properties_.clear();
179 }
180 
181 void SelectionHandler::createBox(const std::pair<CollObjectHandle, uint64_t>& handles,
182  const Ogre::AxisAlignedBox& aabb,
183  const std::string& material_name)
184 {
185  Ogre::WireBoundingBox* box = nullptr;
186  Ogre::SceneNode* node = nullptr;
187 
188  M_HandleToBox::iterator it = boxes_.find(handles);
189  if (it == boxes_.end())
190  {
191  Ogre::SceneManager* scene_manager = context_->getSceneManager();
192  node = scene_manager->getRootSceneNode()->createChildSceneNode();
193  box = new Ogre::WireBoundingBox;
194 
195  bool inserted = boxes_.insert(std::make_pair(handles, std::make_pair(node, box))).second;
196  ROS_ASSERT(inserted);
197  Q_UNUSED(inserted);
198  }
199  else
200  {
201  node = it->second.first;
202  box = it->second.second;
203  }
204 
205  setMaterial(*box, material_name);
206 
207  box->setupBoundingBox(aabb);
208  node->detachAllObjects();
209  node->attachObject(box);
210 }
211 
212 void SelectionHandler::destroyBox(const std::pair<CollObjectHandle, uint64_t>& handles)
213 {
214  M_HandleToBox::iterator it = boxes_.find(handles);
215  if (it != boxes_.end())
216  {
217  Ogre::SceneNode* node = it->second.first;
218  Ogre::WireBoundingBox* box = it->second.second;
219 
220  node->detachAllObjects();
221  removeAndDestroyChildNode(node->getParentSceneNode(), node);
222 
223  delete box;
224  boxes_.erase(it);
225  }
226 }
227 
229 {
230  ROS_DEBUG("Selected 0x%08x", obj.handle);
231 
232  V_AABB aabbs;
233  getAABBs(obj, aabbs);
234 
235  if (!aabbs.empty())
236  {
237  Ogre::AxisAlignedBox combined;
238  V_AABB::iterator it = aabbs.begin();
239  V_AABB::iterator end = aabbs.end();
240  for (; it != end; ++it)
241  {
242  combined.merge(*it);
243  }
244 
245  createBox(std::make_pair(obj.handle, 0ULL), combined, "RVIZ/Cyan");
246  }
247 }
248 
250 {
251  ROS_DEBUG("Deselected 0x%08x", obj.handle);
252 
253  destroyBox(std::make_pair(obj.handle, 0ULL));
254 }
255 
257 {
258  interactive_object_ = std::move(object);
259 }
260 
262 {
263  return interactive_object_;
264 }
265 
266 } // namespace rviz
rviz::SelectionHandler::removeTrackedObject
void removeTrackedObject(Ogre::MovableObject *object)
Definition: selection_handler.cpp:128
rviz::SelectionHandler::updateTrackedBoxes
virtual void updateTrackedBoxes()
Definition: selection_handler.cpp:136
compatibility.h
rviz::Picked::handle
CollObjectHandle handle
Definition: forwards.h:59
property.h
rviz::SelectionManager::setPickHandle
static void setPickHandle(CollObjectHandle handle, Ogre::SceneNode *node)
Definition: selection_manager.h:146
rviz::SelectionHandler::~SelectionHandler
virtual ~SelectionHandler()
Definition: selection_handler.cpp:58
rviz::SelectionHandler::getAABBs
virtual void getAABBs(const Picked &obj, V_AABB &aabbs)
Definition: selection_handler.cpp:162
rviz::SelectionHandler::preRenderPass
virtual void preRenderPass(uint32_t pass)
Definition: selection_handler.cpp:75
rviz::SelectionHandler::tracked_objects_
S_Movable tracked_objects_
Definition: selection_handler.h:159
rviz::Picked::extra_handles
S_uint64 extra_handles
Definition: forwards.h:61
selection_manager.h
rviz::setMaterial
void setMaterial(Ogre::SimpleRenderable &renderable, const std::string &material_name)
Definition: compatibility.h:69
rviz::SelectionHandler::destroyBox
void destroyBox(const std::pair< CollObjectHandle, uint64_t > &handles)
Destroy the box associated with the given handle-int pair, if there is one.
Definition: selection_handler.cpp:212
rviz::DisplayContext::getSceneManager
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
rviz::SelectionHandler::createBox
void createBox(const std::pair< CollObjectHandle, uint64_t > &handles, const Ogre::AxisAlignedBox &aabb, const std::string &material_name)
Create or update a box for the given handle-int pair, with the box specified by aabb.
Definition: selection_handler.cpp:181
rviz::Picked
Definition: forwards.h:53
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
selection_handler.h
rviz::SelectionHandler::SelectionHandler
SelectionHandler(DisplayContext *context)
Definition: selection_handler.cpp:51
rviz::removeAndDestroyChildNode
void removeAndDestroyChildNode(Ogre::SceneNode *parent, Ogre::SceneNode *child)
Definition: compatibility.h:96
rviz::SelectionManager::addObject
void addObject(CollObjectHandle obj, SelectionHandler *handler)
Definition: selection_manager.cpp:459
ROS_DEBUG
#define ROS_DEBUG(...)
rviz::SelectionHandler::pick_handle_
CollObjectHandle pick_handle_
Definition: selection_handler.h:188
rviz
Definition: add_display_dialog.cpp:54
rviz::SelectionHandler::context_
DisplayContext * context_
Definition: selection_handler.h:156
rviz::SelectionHandler::Listener
Definition: selection_handler.h:161
rviz::SelectionHandler::setInteractiveObject
virtual void setInteractiveObject(InteractiveObjectWPtr object)
Set an object to listen to mouse events and other interaction calls during use of the 'interact' tool...
Definition: selection_handler.cpp:256
rviz::SelectionHandler::boxes_
M_HandleToBox boxes_
Definition: selection_handler.h:154
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::SelectionHandler::interactive_object_
InteractiveObjectWPtr interactive_object_
Definition: selection_handler.h:182
rviz::SelectionManager::removeObject
void removeObject(CollObjectHandle obj)
Definition: selection_manager.cpp:480
visualization_manager.h
rviz::SelectionHandler::addTrackedObjects
void addTrackedObjects(Ogre::SceneNode *node)
Definition: selection_handler.cpp:98
rviz::SelectionHandler::addTrackedObject
void addTrackedObject(Ogre::MovableObject *object)
Definition: selection_handler.cpp:120
rviz::V_AABB
std::vector< Ogre::AxisAlignedBox > V_AABB
Definition: selection_handler.h:60
rviz::SelectionHandler::onDeselect
virtual void onDeselect(const Picked &obj)
Definition: selection_handler.cpp:249
rviz::SelectionHandler::properties_
QList< Property * > properties_
Definition: selection_handler.h:149
rviz::SelectionHandler::onSelect
virtual void onSelect(const Picked &obj)
Definition: selection_handler.cpp:228
rviz::SelectionHandler::getInteractiveObject
virtual InteractiveObjectWPtr getInteractiveObject()
Get the object to listen to mouse events and other interaction calls during use of the 'interact' too...
Definition: selection_handler.cpp:261
rviz::DisplayContext::getSelectionManager
virtual SelectionManager * getSelectionManager() const =0
Return a pointer to the SelectionManager.
rviz::SelectionHandler::destroyProperties
virtual void destroyProperties(const Picked &obj, Property *parent_property)
Destroy all properties for the given picked object(s).
Definition: selection_handler.cpp:172
rviz::SelectionHandler::postRenderPass
virtual void postRenderPass(uint32_t pass)
Definition: selection_handler.cpp:86
assert.h
rviz::SelectionManager::createHandle
CollObjectHandle createHandle()
Definition: selection_manager.cpp:437
ROS_ASSERT
#define ROS_ASSERT(cond)
rviz::InteractiveObjectWPtr
boost::weak_ptr< InteractiveObject > InteractiveObjectWPtr
Definition: interactive_object.h:59
rviz::SelectionHandler::listener_
ListenerPtr listener_
Definition: selection_handler.h:180


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:53