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


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51