OpenWalnut  1.4.0
WGEGridNode.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <sstream>
26 #include <string>
27 
28 #include <osg/LineWidth>
29 
30 #include "../callbacks/WGEFunctorCallback.h"
31 #include "../WGEGeodeUtils.h"
32 #include "WGEGridNode.h"
33 
35  m_boundaryGeode( new osg::Geode() ),
36  m_innerGridGeode( new osg::Geode() ),
37  m_labelGeode( new osg::Geode() ),
38  m_gridUpdate( true ),
39  m_gridGeometryUpdate( true ),
40  m_showLabels( true ),
41  m_bbColor( WColor( 0.3, 0.3, 0.3, 1.0 ) ),
42  m_gridColor( WColor( 0.1, 0.1, 0.1, 1.0 ) )
43 {
44  m_grid.getWriteTicket()->get() = grid;
45 
46  // init the boundary geometry
48 
49  // init labels
50  // Therefore: create prototype
51  WGELabel::SPtr label = new WGELabel();
52  label->setAlignment( osgText::Text::CENTER_TOP );
53  label->setColor( osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
54 
55  // add several copies and set position accordingly
56 
57  // Front face ( z = 0 )
58  // bottom left
59  label->setPosition( osg::Vec3( 0.0, 0.0, 0.0 ) );
60  label->setCharacterSize( 6 );
61  m_labelGeode->addDrawable( label );
62  m_borderLabels[0] = label;
63 
64  // bottom right
65  label = new WGELabel( *label );
66  label->setPosition( osg::Vec3( 1.0, 0.0, 0.0 ) );
67  m_labelGeode->addDrawable( label );
68  m_borderLabels[1] = label;
69 
70  // top right
71  label = new WGELabel( *label );
72  label->setPosition( osg::Vec3( 1.0, 1.0, 0.0 ) );
73  m_labelGeode->addDrawable( label );
74  m_borderLabels[2] = label;
75 
76  // top left
77  label = new WGELabel( *label );
78  label->setPosition( osg::Vec3( 0.0, 1.0, 0.0 ) );
79  m_labelGeode->addDrawable( label );
80  m_borderLabels[3] = label;
81 
82  // Back face ( z = 1 )
83  // bottom left
84  label = new WGELabel( *label );
85  label->setPosition( osg::Vec3( 0.0, 0.0, 1.0 ) );
86  m_labelGeode->addDrawable( label );
87  m_borderLabels[4] = label;
88 
89  // bottom right
90  label = new WGELabel( *label );
91  label->setPosition( osg::Vec3( 1.0, 0.0, 1.0 ) );
92  m_labelGeode->addDrawable( label );
93  m_borderLabels[5] = label;
94 
95  // top right
96  label = new WGELabel( *label );
97  label->setPosition( osg::Vec3( 1.0, 1.0, 1.0 ) );
98  m_labelGeode->addDrawable( label );
99  m_borderLabels[6] = label;
100 
101  // top left
102  label = new WGELabel( *label );
103  label->setPosition( osg::Vec3( 0.0, 1.0, 1.0 ) );
104  m_labelGeode->addDrawable( label );
105  m_borderLabels[7] = label;
106 
107  // add the others too
108  addChild( m_boundaryGeode );
109  addChild( m_innerGridGeode );
110  addChild( m_labelGeode );
111 
112  m_boundaryGeode->getOrCreateStateSet()->setAttributeAndModes( new osg::LineWidth( 4.0 ), osg::StateAttribute::ON );
113 
114  addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WGEGridNode::callback, this, _1 ) ) );
115 
116  // no blending
117  getOrCreateStateSet()->setMode( GL_BLEND, osg::StateAttribute::OFF );
118  // disable light for this node
119  getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
120 }
121 
123 {
124  // cleanup
125 }
126 
128 {
129  m_grid.getWriteTicket()->get() = grid;
130  m_gridUpdate = true;
131  m_gridGeometryUpdate = true;
132 }
133 
135 {
136  return m_grid.getReadTicket()->get();
137 }
138 
140 {
141  return m_showLabels;
142 }
143 
144 void WGEGridNode::setEnableLabels( bool enable )
145 {
146  m_showLabels = enable;
147  m_gridUpdate = true;
148 }
149 
151 {
152  return m_showBBox;
153 }
154 
155 void WGEGridNode::setEnableBBox( bool enable )
156 {
157  m_showBBox = enable;
158  m_gridUpdate = true;
159 }
160 
162 {
163  return m_showGrid;
164 }
165 
166 void WGEGridNode::setEnableGrid( bool enable )
167 {
168  m_showGrid = enable;
169  m_gridUpdate = true;
170 }
171 
172 const WColor& WGEGridNode::getBBoxColor() const
173 {
174  return m_bbColor;
175 }
176 
177 void WGEGridNode::setBBoxColor( const WColor& color )
178 {
179  m_bbColor = color;
180  m_gridUpdate = true;
181 }
182 
183 const WColor& WGEGridNode::getGridColor() const
184 {
185  return m_gridColor;
186 }
187 
188 void WGEGridNode::setGridColor( const WColor& color )
189 {
190  m_gridColor = color;
191  m_gridUpdate = true;
192 }
193 
194 /**
195  * Simply converts the vector to an string.
196  *
197  * \param vec the vector
198  *
199  * \return string representation
200  */
201 std::string vec2str( osg::Vec3 vec )
202 {
203  std::ostringstream os;
204  os.precision( 5 );
205  os << "(" << vec[0] << "," << vec[1] << "," << vec[2] << ")";
206  return os.str();
207 }
208 
209 void WGEGridNode::callback( osg::Node* /*node*/ )
210 {
211  if( m_gridUpdate )
212  {
213  // grab the grid
214  WGridRegular3D::ConstSPtr grid = m_grid.getReadTicket()->get();
215 
216  // apply the grid transformation
217  osg::Matrix m = osg::Matrix::scale( grid->getNbCoordsX() - 1, grid->getNbCoordsY() - 1, grid->getNbCoordsZ() - 1 ) *
218  static_cast< osg::Matrixd >( static_cast< WMatrix4d >( grid->getTransform() ) );
219  setMatrix( m );
220 
221  // set the labels correspondingly
222  for( size_t i = 0; i < 8; ++i )
223  {
224  m_borderLabels[i]->setText( vec2str( m_borderLabels[i]->getPosition() * m ) );
225  }
226 
227  // set node mask of labels, bbox and grid
228  m_labelGeode->setNodeMask( 0xFFFFFFFF * m_showLabels );
229  m_boundaryGeode->setNodeMask( 0xFFFFFFFF * m_showBBox );
230  m_innerGridGeode->setNodeMask( 0xFFFFFFFF * m_showGrid );
231 
232  // color
233  osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
234  colors->push_back( m_bbColor );
235  m_boundaryGeode->getDrawable( 0 )->asGeometry()->setColorArray( colors );
236  m_boundaryGeode->getDrawable( 0 )->asGeometry()->setColorBinding( osg::Geometry::BIND_OVERALL );
237 
238  // set color for grid too
239  if( m_innerGridGeode->getNumDrawables() )
240  {
241  osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
242  colors->push_back( m_gridColor );
243  m_innerGridGeode->getDrawable( 0 )->asGeometry()->setColorArray( colors );
244  m_innerGridGeode->getDrawable( 0 )->asGeometry()->setColorBinding( osg::Geometry::BIND_OVERALL );
245  }
246 
247  m_gridUpdate = false;
248  }
249 
250  // recreate grid?
252  {
253  // grab the grid
254  WGridRegular3D::ConstSPtr grid = m_grid.getReadTicket()->get();
255 
256  osg::Geometry* gridGeometry = new osg::Geometry();
257  osg::ref_ptr< osg::Vec3Array > vertArray = new osg::Vec3Array( grid->size() );
258 
259  osg::DrawElementsUInt* gridElement = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
260  gridElement->reserve( grid->size() * 2 );
261 
262  size_t sx = grid->getNbCoordsX();
263  size_t sy = grid->getNbCoordsY();
264  size_t sz = grid->getNbCoordsZ();
265  for( unsigned int vertIdX = 0; vertIdX < sx; ++vertIdX )
266  {
267  for( unsigned int vertIdY = 0; vertIdY < sy; ++vertIdY )
268  {
269  for( unsigned int vertIdZ = 0; vertIdZ < sz; ++vertIdZ )
270  {
271  size_t id = vertIdX + vertIdY * sx + vertIdZ * sx * sy;
272 
273  ( *vertArray )[id][0] = static_cast< float >( vertIdX ) / static_cast< float >( sx - 1 );
274  ( *vertArray )[id][1] = static_cast< float >( vertIdY ) / static_cast< float >( sy - 1 );
275  ( *vertArray )[id][2] = static_cast< float >( vertIdZ ) / static_cast< float >( sz - 1 );
276 
277  if( vertIdX < sx - 1 )
278  {
279  gridElement->push_back( id );
280  gridElement->push_back( id + 1 );
281  }
282 
283  if( vertIdY < sy - 1 )
284  {
285  gridElement->push_back( id );
286  gridElement->push_back( id + sx );
287  }
288 
289  if( vertIdZ < sz - 1 )
290  {
291  gridElement->push_back( id );
292  gridElement->push_back( id + sx * sy );
293  }
294  }
295  }
296  }
297 
298  // done. Add it
299  gridGeometry->setVertexArray( vertArray );
300  gridGeometry->addPrimitiveSet( gridElement );
301 
302  osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
303  // finally, the colors
304  colors->push_back( m_gridColor );
305  gridGeometry->setColorArray( colors );
306  gridGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
307 
308  if( m_innerGridGeode->getNumDrawables() )
309  {
310  m_innerGridGeode->setDrawable( 0, gridGeometry );
311  }
312  else
313  {
314  m_innerGridGeode->addDrawable( gridGeometry );
315  }
316 
317  // we create a unit cube here as the transformation matrix already contains the proper scaling.
318  m_gridGeometryUpdate = false;
319  }
320 }
321 
bool getEnableGrid() const
Returns whether grid mode is enabled or not.
void setEnableLabels(bool enable=true)
En- or disable labels on the boundary corners.
void setEnableGrid(bool enable=true)
En- or disable grid mode.
bool m_showLabels
If true, labels get used.
Definition: WGEGridNode.h:196
bool m_gridGeometryUpdate
If true, the inner grid geometry gets recreated.
Definition: WGEGridNode.h:191
osg::ref_ptr< osg::Geometry > createUnitCubeAsLines(const WColor &color)
Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates...
osg::ref_ptr< osg::Geode > m_labelGeode
The geode keeping the labels.
Definition: WGEGridNode.h:174
bool getEnableBBox() const
Returns whether bbox mode is enabled or not.
This callback allows you a simple usage of callbacks in your module.
void setEnableBBox(bool enable=true)
En- or disable bbox mode.
osg::ref_ptr< WGELabel > SPtr
Convenience typedef for a osg::ref_ptr< WGELabel >.
Definition: WGELabel.h:44
WColor m_bbColor
The color of bbox/grid.
Definition: WGEGridNode.h:211
bool m_gridUpdate
If true, the labels and geometry gets adapted properly.
Definition: WGEGridNode.h:186
WGridRegular3D::ConstSPtr getGrid() const
Returns the currently set grid.
void setBBoxColor(const WColor &color)
Sets the color of the rendered bbox.
bool m_showGrid
True if the grid should be rendered.
Definition: WGEGridNode.h:206
void callback(osg::Node *node)
The actual callback handling changes in the grid.
const WColor & getGridColor() const
The currently set color used for rendering the grid.
bool getEnableLabels() const
Returns whether labels on the corners are enabled or not.
osg::ref_ptr< osg::Geode > m_innerGridGeode
The geometry for the whole grid.
Definition: WGEGridNode.h:164
virtual ~WGEGridNode()
Destructor.
WColor m_gridColor
The color of the grid.
Definition: WGEGridNode.h:216
WGEGridNode(WGridRegular3D::ConstSPtr grid)
Creates a node representing the specified grid.
Definition: WGEGridNode.cpp:34
WGELabel::SPtr m_borderLabels[8]
The labels at the corner.
Definition: WGEGridNode.h:169
boost::shared_ptr< const WGridRegular3DTemplate > ConstSPtr
Convenience typedef for a boost::shared_ptr< const WGridRegular3DTemplate >.
void setGrid(WGridRegular3D::ConstSPtr grid)
Updates the node to use the new specified grid.
Label layout-item.
Definition: WGELabel.h:37
WSharedObject< WGridRegular3D::ConstSPtr > m_grid
The actual grid which should be represented by this node.
Definition: WGEGridNode.h:154
osg::ref_ptr< osg::Geode > m_boundaryGeode
The geometry for the boundary.
Definition: WGEGridNode.h:159
void setGridColor(const WColor &color)
Sets the color of the rendered grid.
const WColor & getBBoxColor() const
The currently set color used for rendering the bbox.
bool m_showBBox
True if the bbox should be rendered.
Definition: WGEGridNode.h:201