billboard_line.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 "billboard_line.h"
31 
32 #include <OgreSceneManager.h>
33 #include <OgreSceneNode.h>
34 #include <OgreVector3.h>
35 #include <OgreQuaternion.h>
36 #include <OgreBillboardChain.h>
37 #include <OgreMaterialManager.h>
38 #include <OgreTechnique.h>
39 
40 #include <sstream>
41 
42 #include <ros/assert.h>
43 
44 #define MAX_ELEMENTS (65536/4)
45 
46 namespace rviz
47 {
48 
49 BillboardLine::BillboardLine( Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
50 : Object( scene_manager )
51 , width_( 0.1f )
52 , current_line_(0)
53 , total_elements_(0)
54 , num_lines_(1)
55 , max_points_per_line_(100)
56 , lines_per_chain_(0)
57 , current_chain_(0)
58 , elements_in_current_chain_(0)
59 {
60  if ( !parent_node )
61  {
62  parent_node = scene_manager_->getRootSceneNode();
63  }
64 
65  scene_node_ = parent_node->createChildSceneNode();
66 
67  static int count = 0;
68  std::stringstream ss;
69  ss << "BillboardLineMaterial" << count++;
70  material_ = Ogre::MaterialManager::getSingleton().create( ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
71  material_->setReceiveShadows(false);
72  material_->getTechnique(0)->setLightingEnabled(false);
73 
76 }
77 
79 {
80  V_Chain::iterator it = chains_.begin();
81  V_Chain::iterator end = chains_.end();
82  for (;it != end; ++it)
83  {
84  scene_manager_->destroyBillboardChain(*it);
85  }
86 
87  scene_manager_->destroySceneNode( scene_node_->getName() );
88 
89  Ogre::MaterialManager::getSingleton().remove(material_->getName());
90 }
91 
92 Ogre::BillboardChain* BillboardLine::createChain()
93 {
94  std::stringstream ss;
95  static int count = 0;
96  ss << "BillboardLine chain" << count++;
97  Ogre::BillboardChain* chain = scene_manager_->createBillboardChain(ss.str());
98  chain->setMaterialName( material_->getName() );
99  scene_node_->attachObject( chain );
100 
101  chains_.push_back(chain);
102 
103  return chain;
104 }
105 
107 {
108  V_Chain::iterator it = chains_.begin();
109  V_Chain::iterator end = chains_.end();
110  for (; it != end; ++it)
111  {
112  (*it)->clearAllChains();
113  }
114 
115  current_line_ = 0;
116  total_elements_ = 0;
117  current_chain_ = 0;
119 
120  for (V_uint32::iterator it = num_elements_.begin(); it != num_elements_.end(); ++it)
121  {
122  *it = 0;
123  }
124 }
125 
127 {
128  uint32_t total_points = max_points_per_line_ * num_lines_;
129  uint32_t num_chains = total_points / MAX_ELEMENTS;
130  if (total_points % MAX_ELEMENTS != 0)
131  {
132  ++num_chains;
133  }
134 
135  for (uint32_t i = chains_.size(); i < num_chains; ++i)
136  {
137  createChain();
138  }
139 
141 
142  V_Chain::iterator it = chains_.begin();
143  V_Chain::iterator end = chains_.end();
144  for (;it != end; ++it)
145  {
146  (*it)->setMaxChainElements(max_points_per_line_);
147 
148  // shorten the number of chains in the last bbchain, to avoid memory wasteage
149  if (it + 1 == end)
150  {
151  uint32_t lines_left = num_lines_ % lines_per_chain_;
152 
153  // Handle the case where num_lines_ is a multiple of lines_per_chain
154  if (lines_left == 0) {
155  (*it)->setNumberOfChains(lines_per_chain_);
156  }
157  else
158  {
159  (*it)->setNumberOfChains(lines_left);
160  }
161  }
162  else
163  {
164  (*it)->setNumberOfChains(lines_per_chain_);
165  }
166  }
167 }
168 
170 {
171  max_points_per_line_ = max;
172 
173  setupChains();
174 }
175 
176 void BillboardLine::setNumLines(uint32_t num)
177 {
178  num_lines_ = num;
179 
180  setupChains();
181 
182  num_elements_.resize(num);
183 
184  for (V_uint32::iterator it = num_elements_.begin(); it != num_elements_.end(); ++it)
185  {
186  *it = 0;
187  }
188 }
189 
191 {
192  ++current_line_;
193 
195 }
196 
197 void BillboardLine::addPoint( const Ogre::Vector3& point )
198 {
199  addPoint(point, color_);
200 }
201 
202 void BillboardLine::addPoint( const Ogre::Vector3& point, const Ogre::ColourValue& color )
203 {
205  ++total_elements_;
206 
208 
211  {
212  ++current_chain_;
214  }
215 
216  Ogre::BillboardChain::Element e;
217  e.position = point;
218  e.width = width_;
219  e.colour = color;
220  chains_[current_chain_]->addChainElement(current_line_ % lines_per_chain_ , e);
221 }
222 
223 void BillboardLine::setLineWidth( float width )
224 {
225  width_ = width;
226 
227  for (uint32_t line = 0; line < num_lines_; ++line)
228  {
229  uint32_t element_count = num_elements_[line];
230 
231  for ( uint32_t i = 0; i < element_count; ++i )
232  {
233  Ogre::BillboardChain* c = chains_[line / lines_per_chain_];
234  Ogre::BillboardChain::Element e = c->getChainElement(line % lines_per_chain_, i);
235 
236  e.width = width_;
237  c->updateChainElement(line % lines_per_chain_, i, e);
238  }
239  }
240 }
241 
242 void BillboardLine::setPosition( const Ogre::Vector3& position )
243 {
244  scene_node_->setPosition( position );
245 }
246 
247 void BillboardLine::setOrientation( const Ogre::Quaternion& orientation )
248 {
249  scene_node_->setOrientation( orientation );
250 }
251 
252 void BillboardLine::setScale( const Ogre::Vector3& scale )
253 {
254  // Setting scale doesn't really make sense here
255 }
256 
257 void BillboardLine::setColor( float r, float g, float b, float a )
258 {
259  if ( a < 0.9998 )
260  {
261  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
262  material_->getTechnique(0)->setDepthWriteEnabled( false );
263  }
264  else
265  {
266  material_->getTechnique(0)->setSceneBlending( Ogre::SBT_REPLACE );
267  material_->getTechnique(0)->setDepthWriteEnabled( true );
268  }
269 
270  color_ = Ogre::ColourValue( r, g, b, a );
271 
272  for (uint32_t line = 0; line < num_lines_; ++line)
273  {
274  uint32_t element_count = num_elements_[line];
275 
276  for ( uint32_t i = 0; i < element_count; ++i )
277  {
278  Ogre::BillboardChain* c = chains_[line / lines_per_chain_];
279  Ogre::BillboardChain::Element e = c->getChainElement(line % lines_per_chain_, i);
280 
281  e.colour = color_;
282  c->updateChainElement(line % lines_per_chain_, i, e);
283  }
284  }
285 }
286 
287 const Ogre::Vector3& BillboardLine::getPosition()
288 {
289  return scene_node_->getPosition();
290 }
291 
292 const Ogre::Quaternion& BillboardLine::getOrientation()
293 {
294  return scene_node_->getOrientation();
295 }
296 
297 } // namespace rviz
Ogre::MaterialPtr material_
uint32_t elements_in_current_chain_
Ogre::ColourValue color_
void addPoint(const Ogre::Vector3 &point)
f
#define MAX_ELEMENTS
virtual void setPosition(const Ogre::Vector3 &position)
Set the position of this object.
Base class for visible objects, providing a minimal generic interface.
Definition: object.h:49
virtual void setColor(float r, float g, float b, float a)
Set the color of the object. Values are in the range [0, 1].
virtual void setOrientation(const Ogre::Quaternion &orientation)
Set the orientation of the object.
virtual const Ogre::Vector3 & getPosition()
Get the local position of this object.
BillboardLine(Ogre::SceneManager *manager, Ogre::SceneNode *parent_node=NULL)
Constructor.
void setNumLines(uint32_t num)
void setMaxPointsPerLine(uint32_t max)
Ogre::BillboardChain * createChain()
virtual const Ogre::Quaternion & getOrientation()
Get the local orientation of this object.
uint32_t max_points_per_line_
virtual void setScale(const Ogre::Vector3 &scale)
Set the scale of the object. Always relative to the identity orientation of the object.
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:103
#define ROS_ASSERT(cond)
void setLineWidth(float width)
Ogre::SceneNode * scene_node_


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