Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
flat_weighted_arrows_array.cpp
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * Copyright (c) 2018, Bosch Software Innovations GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the Willow Garage, Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 // Copyright (c) 2019 Intel Corporation
32 // Copyright (c) 2020 Sarthak Mittal
33 //
34 // Licensed under the Apache License, Version 2.0 (the "License");
35 // you may not use this file except in compliance with the License.
36 // You may obtain a copy of the License at
37 //
38 // http://www.apache.org/licenses/LICENSE-2.0
39 //
40 // Unless required by applicable law or agreed to in writing, software
41 // distributed under the License is distributed on an "AS IS" BASIS,
42 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 // See the License for the specific language governing permissions and
44 // limitations under the License.
45 
46 #include "nav2_rviz_plugins/particle_cloud_display/flat_weighted_arrows_array.hpp"
47 
48 #include <vector>
49 #include <string>
50 #include <algorithm>
51 
52 #include <OgreSceneManager.h>
53 #include <OgreTechnique.h>
54 
55 #include "rviz_rendering/material_manager.hpp"
56 
57 namespace nav2_rviz_plugins
58 {
59 
60 FlatWeightedArrowsArray::FlatWeightedArrowsArray(Ogre::SceneManager * scene_manager)
61 : scene_manager_(scene_manager), manual_object_(nullptr) {}
62 
63 FlatWeightedArrowsArray::~FlatWeightedArrowsArray()
64 {
65  if (manual_object_) {
66  scene_manager_->destroyManualObject(manual_object_);
67  }
68 }
69 
70 void FlatWeightedArrowsArray::createAndAttachManualObject(Ogre::SceneNode * scene_node)
71 {
72  manual_object_ = scene_manager_->createManualObject();
73  manual_object_->setDynamic(true);
74  scene_node->attachObject(manual_object_);
75 }
76 
77 void FlatWeightedArrowsArray::updateManualObject(
78  Ogre::ColourValue color,
79  float alpha,
80  float min_length,
81  float max_length,
82  const std::vector<nav2_rviz_plugins::OgrePoseWithWeight> & poses)
83 {
84  clear();
85 
86  color.a = alpha;
87  setManualObjectMaterial();
88  rviz_rendering::MaterialManager::enableAlphaBlending(material_, alpha);
89 
90  manual_object_->begin(
91  material_->getName(), Ogre::RenderOperation::OT_LINE_LIST, "rviz_rendering");
92  setManualObjectVertices(color, min_length, max_length, poses);
93  manual_object_->end();
94 }
95 
96 void FlatWeightedArrowsArray::clear()
97 {
98  if (manual_object_) {
99  manual_object_->clear();
100  }
101 }
102 
103 void FlatWeightedArrowsArray::setManualObjectMaterial()
104 {
105  static int material_count = 0;
106  std::string material_name = "FlatWeightedArrowsMaterial" + std::to_string(material_count++);
107  material_ = rviz_rendering::MaterialManager::createMaterialWithNoLighting(material_name);
108 }
109 
110 void FlatWeightedArrowsArray::setManualObjectVertices(
111  const Ogre::ColourValue & color,
112  float min_length,
113  float max_length,
114  const std::vector<nav2_rviz_plugins::OgrePoseWithWeight> & poses)
115 {
116  manual_object_->estimateVertexCount(poses.size() * 6);
117 
118  float scale = max_length - min_length;
119  float length;
120  for (const auto & pose : poses) {
121  length = std::min(std::max(pose.weight * scale + min_length, min_length), max_length);
122  Ogre::Vector3 vertices[6];
123  vertices[0] = pose.position; // back of arrow
124  vertices[1] =
125  pose.position + pose.orientation * Ogre::Vector3(length, 0, 0); // tip of arrow
126  vertices[2] = vertices[1];
127  vertices[3] = pose.position + pose.orientation * Ogre::Vector3(
128  0.75f * length, 0.2f * length, 0);
129  vertices[4] = vertices[1];
130  vertices[5] = pose.position + pose.orientation * Ogre::Vector3(
131  0.75f * length, -0.2f * length,
132  0);
133 
134  for (const auto & vertex : vertices) {
135  manual_object_->position(vertex);
136  manual_object_->colour(color);
137  }
138  }
139 }
140 
141 } // namespace nav2_rviz_plugins