grid.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 "grid.h"
31 #include "billboard_line.h"
32 
33 #include <OgreSceneManager.h>
34 #include <OgreSceneNode.h>
36 #include <OgreQuaternion.h>
37 #include <OgreManualObject.h>
38 #include <OgreMaterialManager.h>
39 #include <OgreTechnique.h>
40 
41 #include <sstream>
42 
43 namespace rviz
44 {
45 Grid::Grid(Ogre::SceneManager* scene_manager,
46  Ogre::SceneNode* parent_node,
47  Style style,
48  uint32_t cell_count,
49  float cell_length,
50  float line_width,
51  const Ogre::ColourValue& color)
52  : scene_manager_(scene_manager)
53  , style_(style)
54  , cell_count_(cell_count)
55  , cell_length_(cell_length)
56  , line_width_(line_width)
57  , height_(0)
58  , color_(color)
59 {
60  static uint32_t gridCount = 0;
61  std::stringstream ss;
62  ss << "Grid" << gridCount++;
63 
64  manual_object_ = scene_manager_->createManualObject(ss.str());
65 
66  if (!parent_node)
67  {
68  parent_node = scene_manager_->getRootSceneNode();
69  }
70 
71  scene_node_ = parent_node->createChildSceneNode();
72  scene_node_->attachObject(manual_object_);
73 
74  billboard_line_ = new BillboardLine(scene_manager, scene_node_);
75 
76  ss << "Material";
77  material_ = Ogre::MaterialManager::getSingleton().create(
78  ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
79  material_->setReceiveShadows(false);
80  material_->getTechnique(0)->setLightingEnabled(false);
81 
83 }
84 
86 {
87  delete billboard_line_;
88 
89  scene_manager_->destroySceneNode(scene_node_);
90  scene_manager_->destroyManualObject(manual_object_);
91  Ogre::MaterialManager::getSingleton().remove(material_->getName());
92 }
93 
94 void Grid::setCellCount(uint32_t count)
95 {
96  cell_count_ = count;
97 
98  create();
99 }
100 
101 void Grid::setCellLength(float len)
102 {
103  cell_length_ = len;
104 
105  create();
106 }
107 
108 void Grid::setLineWidth(float width)
109 {
110  line_width_ = width;
111 
112  create();
113 }
114 
115 void Grid::setColor(const Ogre::ColourValue& color)
116 {
117  color_ = color;
118 
119  if (color_.a < 0.9998)
120  {
121  material_->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
122  material_->setDepthWriteEnabled(false);
123  }
124  else
125  {
126  material_->setSceneBlending(Ogre::SBT_REPLACE);
127  material_->setDepthWriteEnabled(true);
128  }
129 
130  create();
131 }
132 
134 {
135  style_ = style;
136 
137  create();
138 }
139 
140 void Grid::setHeight(uint32_t height)
141 {
142  height_ = height;
143 
144  create();
145 }
146 
148 {
149  manual_object_->clear();
151 
152  float extent = (cell_length_ * ((double)cell_count_)) / 2;
153 
154  if (style_ == Billboards)
155  {
159  billboard_line_->setNumLines((cell_count_ + 1) * 2 * (height_ + 1) +
160  ((cell_count_ + 1) * (cell_count_ + 1)) * height_);
161  }
162  else
163  {
164  manual_object_->estimateVertexCount(cell_count_ * 4 * (height_ + 1) +
165  ((cell_count_ + 1) * (cell_count_ + 1) * height_));
166  manual_object_->begin(material_->getName(), Ogre::RenderOperation::OT_LINE_LIST);
167  }
168 
169  for (uint32_t h = 0; h <= height_; ++h)
170  {
171  float h_real = (height_ / 2.0f - (float)h) * cell_length_;
172  for (uint32_t i = 0; i <= cell_count_; i++)
173  {
174  float inc = extent - (i * cell_length_);
175 
176  Ogre::Vector3 p1(inc, h_real, -extent);
177  Ogre::Vector3 p2(inc, h_real, extent);
178  Ogre::Vector3 p3(-extent, h_real, inc);
179  Ogre::Vector3 p4(extent, h_real, inc);
180 
181  if (style_ == Billboards)
182  {
183  if (h != 0 || i != 0)
184  {
186  }
187 
190 
192 
195  }
196  else
197  {
198  manual_object_->position(p1);
199  manual_object_->colour(color_);
200  manual_object_->position(p2);
201  manual_object_->colour(color_);
202 
203  manual_object_->position(p3);
204  manual_object_->colour(color_);
205  manual_object_->position(p4);
206  manual_object_->colour(color_);
207  }
208  }
209  }
210 
211  if (height_ > 0)
212  {
213  for (uint32_t x = 0; x <= cell_count_; ++x)
214  {
215  for (uint32_t z = 0; z <= cell_count_; ++z)
216  {
217  float x_real = extent - x * cell_length_;
218  float z_real = extent - z * cell_length_;
219 
220  float y_top = (height_ / 2.0f) * cell_length_;
221  float y_bottom = -y_top;
222 
223  if (style_ == Billboards)
224  {
226 
227  billboard_line_->addPoint(Ogre::Vector3(x_real, y_bottom, z_real));
228  billboard_line_->addPoint(Ogre::Vector3(x_real, y_top, z_real));
229  }
230  else
231  {
232  manual_object_->position(x_real, y_bottom, z_real);
233  manual_object_->colour(color_);
234  manual_object_->position(x_real, y_top, z_real);
235  manual_object_->colour(color_);
236  }
237  }
238  }
239  }
240 
241  if (style_ == Lines)
242  {
243  manual_object_->end();
244  }
245 }
246 
247 void Grid::setUserData(const Ogre::Any& data)
248 {
249  manual_object_->getUserObjectBindings().setUserAny(data);
250 }
251 
252 } // namespace rviz
rviz::BillboardLine::clear
void clear()
Definition: billboard_line.cpp:105
rviz::BillboardLine
An object that displays a multi-segment line strip rendered as billboards.
Definition: billboard_line.h:58
rviz::BillboardLine::setLineWidth
void setLineWidth(float width)
Definition: billboard_line.cpp:237
rviz::Grid::cell_length_
float cell_length_
Definition: grid.h:154
rviz::Grid::~Grid
~Grid()
Definition: grid.cpp:85
rviz::BillboardLine::newLine
void newLine()
Definition: billboard_line.cpp:201
rviz::Grid::setLineWidth
void setLineWidth(float width)
Definition: grid.cpp:108
rviz::Grid::create
void create()
Definition: grid.cpp:147
grid.h
rviz::Grid::Grid
Grid(Ogre::SceneManager *manager, Ogre::SceneNode *parent_node, Style style, uint32_t cell_count, float cell_length, float line_width, const Ogre::ColourValue &color)
Constructor.
Definition: grid.cpp:45
rviz::Grid::setUserData
void setUserData(const Ogre::Any &data)
Sets user data on all ogre objects we own.
Definition: grid.cpp:247
rviz::BillboardLine::setMaxPointsPerLine
void setMaxPointsPerLine(uint32_t max)
Definition: billboard_line.cpp:169
rviz::Grid::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: grid.h:144
rviz::Grid::cell_count_
uint32_t cell_count_
Definition: grid.h:153
rviz::Grid::scene_node_
Ogre::SceneNode * scene_node_
The scene node that this grid is attached to.
Definition: grid.h:145
rviz::Grid::setColor
void setColor(const Ogre::ColourValue &color)
Definition: grid.cpp:115
rviz::Grid::setHeight
void setHeight(uint32_t count)
Definition: grid.cpp:140
rviz
Definition: add_display_dialog.cpp:54
rviz::Grid::height_
uint32_t height_
Definition: grid.h:156
rviz::Grid::Billboards
@ Billboards
Definition: grid.h:68
ogre_vector.h
rviz::Grid::setCellCount
void setCellCount(uint32_t count)
Definition: grid.cpp:94
rviz::Grid::Style
Style
Definition: grid.h:65
rviz::BillboardLine::setNumLines
void setNumLines(uint32_t num)
Definition: billboard_line.cpp:187
rviz::Grid::style_
Style style_
Definition: grid.h:152
rviz::Grid::manual_object_
Ogre::ManualObject * manual_object_
The manual object used to draw the grid.
Definition: grid.h:146
billboard_line.h
rviz::Grid::line_width_
float line_width_
Definition: grid.h:155
rviz::Grid::Lines
@ Lines
Definition: grid.h:67
rviz::Grid::setStyle
void setStyle(Style style)
Definition: grid.cpp:133
rviz::Grid::material_
Ogre::MaterialPtr material_
Definition: grid.h:150
rviz::Grid::setCellLength
void setCellLength(float len)
Definition: grid.cpp:101
rviz::BillboardLine::setColor
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: billboard_line.cpp:271
rviz::Grid::color_
Ogre::ColourValue color_
Definition: grid.h:157
rviz::Grid::billboard_line_
BillboardLine * billboard_line_
Definition: grid.h:148
rviz::BillboardLine::addPoint
void addPoint(const Ogre::Vector3 &point)
Definition: billboard_line.cpp:208


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