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 
32 #include "properties/property.h"
33 #include "visualization_manager.h"
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 
45 
46 namespace rviz
47 {
49  : context_(context), listener_(new Listener(this))
50 {
53 }
54 
56 {
57  S_Movable::iterator it = tracked_objects_.begin();
58  S_Movable::iterator end = tracked_objects_.end();
59  for (; it != end; ++it)
60  {
61  Ogre::MovableObject* m = *it;
62  m->setListener(nullptr);
63  }
64 
65  while (!boxes_.empty())
66  {
67  destroyBox(boxes_.begin()->first);
68  }
70 }
71 
72 void SelectionHandler::preRenderPass(uint32_t /*pass*/)
73 {
74  M_HandleToBox::iterator it = boxes_.begin();
75  M_HandleToBox::iterator end = boxes_.end();
76  for (; it != end; ++it)
77  {
78  Ogre::WireBoundingBox* box = it->second.second;
79  box->setVisible(false);
80  }
81 }
82 
83 void SelectionHandler::postRenderPass(uint32_t /*pass*/)
84 {
85  M_HandleToBox::iterator it = boxes_.begin();
86  M_HandleToBox::iterator end = boxes_.end();
87  for (; it != end; ++it)
88  {
89  Ogre::WireBoundingBox* box = it->second.second;
90  box->setVisible(true);
91  }
92 }
93 
94 
95 void SelectionHandler::addTrackedObjects(Ogre::SceneNode* node)
96 {
97  if (!node)
98  {
99  return;
100  }
101  // Loop over all objects attached to this node.
102  Ogre::SceneNode::ObjectIterator obj_it = node->getAttachedObjectIterator();
103  while (obj_it.hasMoreElements())
104  {
105  Ogre::MovableObject* obj = obj_it.getNext();
106  addTrackedObject(obj);
107  }
108  // Loop over and recurse into all child nodes.
109  Ogre::SceneNode::ChildNodeIterator child_it = node->getChildIterator();
110  while (child_it.hasMoreElements())
111  {
112  Ogre::SceneNode* child = dynamic_cast<Ogre::SceneNode*>(child_it.getNext());
113  addTrackedObjects(child);
114  }
115 }
116 
117 void SelectionHandler::addTrackedObject(Ogre::MovableObject* object)
118 {
119  tracked_objects_.insert(object);
120  object->setListener(listener_.get());
121 
123 }
124 
125 void SelectionHandler::removeTrackedObject(Ogre::MovableObject* object)
126 {
127  tracked_objects_.erase(object);
128  object->setListener(nullptr);
129 
131 }
132 
134 {
135  M_HandleToBox::iterator it = boxes_.begin();
136  M_HandleToBox::iterator end = boxes_.end();
137  for (; it != end; ++it)
138  {
139  V_AABB aabbs;
140  Picked p(it->first.first);
141  p.extra_handles.insert(it->first.second);
142  getAABBs(Picked(it->first.first), aabbs);
143 
144  if (!aabbs.empty())
145  {
146  Ogre::AxisAlignedBox combined;
147  V_AABB::iterator aabb_it = aabbs.begin();
148  V_AABB::iterator aabb_end = aabbs.end();
149  for (; aabb_it != aabb_end; ++aabb_it)
150  {
151  combined.merge(*aabb_it);
152  }
153 
154  createBox(std::make_pair(p.handle, it->first.second), combined, "RVIZ/Cyan");
155  }
156  }
157 }
158 
159 void SelectionHandler::getAABBs(const Picked& /*obj*/, V_AABB& aabbs)
160 {
161  S_Movable::iterator it = tracked_objects_.begin();
162  S_Movable::iterator end = tracked_objects_.end();
163  for (; it != end; ++it)
164  {
165  aabbs.push_back((*it)->getWorldBoundingBox());
166  }
167 }
168 
169 void SelectionHandler::destroyProperties(const Picked& /*obj*/, Property* /*parent_property*/)
170 {
171  for (int i = 0; i < properties_.size(); i++)
172  {
173  delete properties_.at(i);
174  }
175  properties_.clear();
176 }
177 
178 void SelectionHandler::createBox(const std::pair<CollObjectHandle, uint64_t>& handles,
179  const Ogre::AxisAlignedBox& aabb,
180  const std::string& material_name)
181 {
182  Ogre::WireBoundingBox* box = nullptr;
183  Ogre::SceneNode* node = nullptr;
184 
185  M_HandleToBox::iterator it = boxes_.find(handles);
186  if (it == boxes_.end())
187  {
188  Ogre::SceneManager* scene_manager = context_->getSceneManager();
189  node = scene_manager->getRootSceneNode()->createChildSceneNode();
190  box = new Ogre::WireBoundingBox;
191 
192  bool inserted = boxes_.insert(std::make_pair(handles, std::make_pair(node, box))).second;
193  ROS_ASSERT(inserted);
194  Q_UNUSED(inserted);
195  }
196  else
197  {
198  node = it->second.first;
199  box = it->second.second;
200  }
201 
202  box->setMaterial(material_name);
203 
204  box->setupBoundingBox(aabb);
205  node->detachAllObjects();
206  node->attachObject(box);
207 }
208 
209 void SelectionHandler::destroyBox(const std::pair<CollObjectHandle, uint64_t>& handles)
210 {
211  M_HandleToBox::iterator it = boxes_.find(handles);
212  if (it != boxes_.end())
213  {
214  Ogre::SceneNode* node = it->second.first;
215  Ogre::WireBoundingBox* box = it->second.second;
216 
217  node->detachAllObjects();
218  node->getParentSceneNode()->removeAndDestroyChild(node->getName());
219 
220  delete box;
221 
222  boxes_.erase(it);
223  }
224 }
225 
227 {
228  ROS_DEBUG("Selected 0x%08x", obj.handle);
229 
230  V_AABB aabbs;
231  getAABBs(obj, aabbs);
232 
233  if (!aabbs.empty())
234  {
235  Ogre::AxisAlignedBox combined;
236  V_AABB::iterator it = aabbs.begin();
237  V_AABB::iterator end = aabbs.end();
238  for (; it != end; ++it)
239  {
240  combined.merge(*it);
241  }
242 
243  createBox(std::make_pair(obj.handle, 0ULL), combined, "RVIZ/Cyan");
244  }
245 }
246 
248 {
249  ROS_DEBUG("Deselected 0x%08x", obj.handle);
250 
251  destroyBox(std::make_pair(obj.handle, 0ULL));
252 }
253 
255 {
256  interactive_object_ = object;
257 }
258 
260 {
261  return interactive_object_;
262 }
263 
264 } // namespace rviz
void removeTrackedObject(Ogre::MovableObject *object)
CollObjectHandle createHandle()
void destroyBox(const std::pair< CollObjectHandle, uint64_t > &handles)
Destroy the box associated with the given handle-int pair, if there is one.
virtual void updateTrackedBoxes()
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.
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
S_uint64 extra_handles
Definition: forwards.h:61
void removeObject(CollObjectHandle obj)
virtual InteractiveObjectWPtr getInteractiveObject()
Get the object to listen to mouse events and other interaction calls during use of the &#39;interact&#39; too...
virtual void preRenderPass(uint32_t pass)
void addObject(CollObjectHandle obj, SelectionHandler *handler)
CollObjectHandle handle
Definition: forwards.h:59
static void setPickHandle(CollObjectHandle handle, Ogre::SceneNode *node)
Pure-virtual base class for objects which give Display subclasses context in which to work...
std::vector< Ogre::AxisAlignedBox > V_AABB
DisplayContext * context_
void addTrackedObject(Ogre::MovableObject *object)
virtual void getAABBs(const Picked &obj, V_AABB &aabbs)
virtual SelectionManager * getSelectionManager() const =0
Return a pointer to the SelectionManager.
SelectionHandler(DisplayContext *context)
virtual void setInteractiveObject(InteractiveObjectWPtr object)
Set an object to listen to mouse events and other interaction calls during use of the &#39;interact&#39; tool...
InteractiveObjectWPtr interactive_object_
void addTrackedObjects(Ogre::SceneNode *node)
virtual void onSelect(const Picked &obj)
boost::weak_ptr< InteractiveObject > InteractiveObjectWPtr
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
virtual void destroyProperties(const Picked &obj, Property *parent_property)
Destroy all properties for the given picked object(s).
CollObjectHandle pick_handle_
virtual void postRenderPass(uint32_t pass)
QList< Property * > properties_
virtual void onDeselect(const Picked &obj)
#define ROS_ASSERT(cond)
#define ROS_DEBUG(...)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Aug 15 2022 02:05:17