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>
35 #include <OgreVector3.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_->getName());
90  scene_manager_->destroyManualObject(manual_object_);
91 
92  material_->unload();
93 }
94 
95 void Grid::setCellCount(uint32_t count)
96 {
97  cell_count_ = count;
98 
99  create();
100 }
101 
102 void Grid::setCellLength(float len)
103 {
104  cell_length_ = len;
105 
106  create();
107 }
108 
109 void Grid::setLineWidth(float width)
110 {
111  line_width_ = width;
112 
113  create();
114 }
115 
116 void Grid::setColor(const Ogre::ColourValue& color)
117 {
118  color_ = color;
119 
120  if (color_.a < 0.9998)
121  {
122  material_->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
123  material_->setDepthWriteEnabled(false);
124  }
125  else
126  {
127  material_->setSceneBlending(Ogre::SBT_REPLACE);
128  material_->setDepthWriteEnabled(true);
129  }
130 
131  create();
132 }
133 
135 {
136  style_ = style;
137 
138  create();
139 }
140 
141 void Grid::setHeight(uint32_t height)
142 {
143  height_ = height;
144 
145  create();
146 }
147 
149 {
150  manual_object_->clear();
152 
153  float extent = (cell_length_ * ((double)cell_count_)) / 2;
154 
155  if (style_ == Billboards)
156  {
160  billboard_line_->setNumLines((cell_count_ + 1) * 2 * (height_ + 1) +
161  ((cell_count_ + 1) * (cell_count_ + 1)) * height_);
162  }
163  else
164  {
165  manual_object_->estimateVertexCount(cell_count_ * 4 * (height_ + 1) +
166  ((cell_count_ + 1) * (cell_count_ + 1) * height_));
167  manual_object_->begin(material_->getName(), Ogre::RenderOperation::OT_LINE_LIST);
168  }
169 
170  for (uint32_t h = 0; h <= height_; ++h)
171  {
172  float h_real = (height_ / 2.0f - (float)h) * cell_length_;
173  for (uint32_t i = 0; i <= cell_count_; i++)
174  {
175  float inc = extent - (i * cell_length_);
176 
177  Ogre::Vector3 p1(inc, h_real, -extent);
178  Ogre::Vector3 p2(inc, h_real, extent);
179  Ogre::Vector3 p3(-extent, h_real, inc);
180  Ogre::Vector3 p4(extent, h_real, inc);
181 
182  if (style_ == Billboards)
183  {
184  if (h != 0 || i != 0)
185  {
187  }
188 
191 
193 
196  }
197  else
198  {
199  manual_object_->position(p1);
200  manual_object_->colour(color_);
201  manual_object_->position(p2);
202  manual_object_->colour(color_);
203 
204  manual_object_->position(p3);
205  manual_object_->colour(color_);
206  manual_object_->position(p4);
207  manual_object_->colour(color_);
208  }
209  }
210  }
211 
212  if (height_ > 0)
213  {
214  for (uint32_t x = 0; x <= cell_count_; ++x)
215  {
216  for (uint32_t z = 0; z <= cell_count_; ++z)
217  {
218  float x_real = extent - x * cell_length_;
219  float z_real = extent - z * cell_length_;
220 
221  float y_top = (height_ / 2.0f) * cell_length_;
222  float y_bottom = -y_top;
223 
224  if (style_ == Billboards)
225  {
227 
228  billboard_line_->addPoint(Ogre::Vector3(x_real, y_bottom, z_real));
229  billboard_line_->addPoint(Ogre::Vector3(x_real, y_top, z_real));
230  }
231  else
232  {
233  manual_object_->position(x_real, y_bottom, z_real);
234  manual_object_->colour(color_);
235  manual_object_->position(x_real, y_top, z_real);
236  manual_object_->colour(color_);
237  }
238  }
239  }
240  }
241 
242  if (style_ == Lines)
243  {
244  manual_object_->end();
245  }
246 }
247 
248 void Grid::setUserData(const Ogre::Any& data)
249 {
250  manual_object_->getUserObjectBindings().setUserAny(data);
251 }
252 
253 } // namespace rviz
void addPoint(const Ogre::Vector3 &point)
void setUserData(const Ogre::Any &data)
Sets user data on all ogre objects we own.
Definition: grid.cpp:248
An object that displays a multi-segment line strip rendered as billboards.
uint32_t cell_count_
Definition: grid.h:153
Ogre::MaterialPtr material_
Definition: grid.h:150
void setHeight(uint32_t count)
Definition: grid.cpp:141
void setStyle(Style style)
Definition: grid.cpp:134
Ogre::ColourValue color_
Definition: grid.h:157
void setCellCount(uint32_t count)
Definition: grid.cpp:95
void setCellLength(float len)
Definition: grid.cpp:102
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
BillboardLine * billboard_line_
Definition: grid.h:148
~Grid()
Definition: grid.cpp:85
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
void setNumLines(uint32_t num)
Ogre::SceneNode * scene_node_
The scene node that this grid is attached to.
Definition: grid.h:145
void setMaxPointsPerLine(uint32_t max)
void create()
Definition: grid.cpp:148
void setLineWidth(float width)
Definition: grid.cpp:109
uint32_t height_
Definition: grid.h:156
Style style_
Definition: grid.h:152
Ogre::ManualObject * manual_object_
The manual object used to draw the grid.
Definition: grid.h:146
float line_width_
Definition: grid.h:155
float cell_length_
Definition: grid.h:154
void setColor(const Ogre::ColourValue &color)
Definition: grid.cpp:116
void setLineWidth(float width)
Ogre::SceneManager * scene_manager_
Definition: grid.h:144


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