Commit 0627ed6c authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[ADD] - pimp my bbox module. The BBox Module now supports grid rendering using...

[ADD] - pimp my bbox module. The BBox Module now supports grid rendering using the WGEGridNode. This makes gridRenderer obsolete.
parent 41ce9a7a
......@@ -52,6 +52,16 @@ class OWCOMMON_EXPORT WItemSelection: public boost::enable_shared_from_this< WIt
{
friend class WItemSelector; // for proper locking and unlocking
public:
/**
* Convenience typedef for a boost::shared_ptr< WItemSelection >
*/
typedef boost::shared_ptr< WItemSelection > SPtr;
/**
* Convenience typedef for a boost::shared_ptr< const WItemSelection >
*/
typedef boost::shared_ptr< const WItemSelection > ConstSPtr;
/**
* Default constructor.
*/
......
......@@ -94,7 +94,9 @@ public:
WItemSelector newSelector( IndexList selected ) const;
/**
* Creates a new valid instance with the specified items selected. This can be useful to add a certain index.
* Creates a new valid instance with the specified items selected. This can be useful to add a certain index. The new selector has the
* selection from this AND the specified one. If you want to create a selector containing only one selected item, use the method that uses
* the IndexList.
*
* \note Please be aware that, in the moment this method returns, another thread can make all selectors invalid again causing the returned
* one to be invalid too. To avoid this, use the newSelector method only if the old has locked the selection using ::lock and ::unlock.
......
......@@ -24,6 +24,8 @@
#include <sstream>
#include <osg/LineWidth>
#include "../callbacks/WGEFunctorCallback.h"
#include "../WGEGeodeUtils.h"
......@@ -34,12 +36,15 @@ WGEGridNode::WGEGridNode( WGridRegular3D::ConstSPtr grid ):
m_innerGridGeode( new osg::Geode() ),
m_labelGeode( new osg::Geode() ),
m_gridUpdate( true ),
m_showLabels( true )
m_gridGeometryUpdate( true ),
m_showLabels( true ),
m_bbColor( WColor( 0.3, 0.3, 0.3, 1.0 ) ),
m_gridColor( WColor( 0.1, 0.1, 0.1, 1.0 ) )
{
m_grid.getWriteTicket()->get() = grid;
// init the boundary geometry
m_boundaryGeode->addDrawable( wge::createUnitCubeAsLines( WColor( 0.3, 0.3, 0.3, 1.0 ) ) );
m_boundaryGeode->addDrawable( wge::createUnitCubeAsLines( m_bbColor ) );
// init labels
// Therefore: create prototype
......@@ -104,6 +109,8 @@ WGEGridNode::WGEGridNode( WGridRegular3D::ConstSPtr grid ):
addChild( m_innerGridGeode );
addChild( m_labelGeode );
m_boundaryGeode->getOrCreateStateSet()->setAttributeAndModes( new osg::LineWidth( 4.0 ), osg::StateAttribute::ON );
addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WGEGridNode::callback, this, _1 ) ) );
// no blending
......@@ -121,6 +128,7 @@ void WGEGridNode::setGrid( WGridRegular3D::ConstSPtr grid )
{
m_grid.getWriteTicket()->get() = grid;
m_gridUpdate = true;
m_gridGeometryUpdate = true;
}
WGridRegular3D::ConstSPtr WGEGridNode::getGrid() const
......@@ -139,6 +147,50 @@ void WGEGridNode::setEnableLabels( bool enable )
m_gridUpdate = true;
}
bool WGEGridNode::getEnableBBox() const
{
return m_showBBox;
}
void WGEGridNode::setEnableBBox( bool enable )
{
m_showBBox = enable;
m_gridUpdate = true;
}
bool WGEGridNode::getEnableGrid() const
{
return m_showGrid;
}
void WGEGridNode::setEnableGrid( bool enable )
{
m_showGrid = enable;
m_gridUpdate = true;
}
const WColor& WGEGridNode::getBBoxColor() const
{
return m_bbColor;
}
void WGEGridNode::setBBoxColor( const WColor& color )
{
m_bbColor = color;
m_gridUpdate = true;
}
const WColor& WGEGridNode::getGridColor() const
{
return m_gridColor;
}
void WGEGridNode::setGridColor( const WColor& color )
{
m_gridColor = color;
m_gridUpdate = true;
}
/**
* Simply converts the vector to an string.
*
......@@ -172,10 +224,96 @@ void WGEGridNode::callback( osg::Node* /*node*/ )
m_borderLabels[i]->setText( vec2str( m_borderLabels[i]->getPosition() * m ) );
}
// set node mask of labels
// set node mask of labels, bbox and grid
m_labelGeode->setNodeMask( 0xFFFFFFFF * m_showLabels );
m_boundaryGeode->setNodeMask( 0xFFFFFFFF * m_showBBox );
m_innerGridGeode->setNodeMask( 0xFFFFFFFF * m_showGrid );
// color
osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
colors->push_back( m_bbColor );
m_boundaryGeode->getDrawable( 0 )->asGeometry()->setColorArray( colors );
// set color for grid too
if ( m_innerGridGeode->getNumDrawables() )
{
osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
colors->push_back( m_gridColor );
m_innerGridGeode->getDrawable( 0 )->asGeometry()->setColorArray( colors );
}
m_gridUpdate = false;
}
// recreate grid?
if ( m_gridGeometryUpdate && m_showGrid )
{
// grab the grid
WGridRegular3D::ConstSPtr grid = m_grid.getReadTicket()->get();
osg::Geometry* gridGeometry = new osg::Geometry();
osg::ref_ptr< osg::Vec3Array > vertArray = new osg::Vec3Array( grid->size() );
osg::DrawElementsUInt* gridElement = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
gridElement->reserve( grid->size() * 2 );
size_t sx = grid->getNbCoordsX();
size_t sy = grid->getNbCoordsY();
size_t sz = grid->getNbCoordsZ();
for( unsigned int vertIdX = 0; vertIdX < sx; ++vertIdX )
{
for( unsigned int vertIdY = 0; vertIdY < sy; ++vertIdY )
{
for( unsigned int vertIdZ = 0; vertIdZ < sz; ++vertIdZ )
{
size_t id = vertIdX + vertIdY * sx + vertIdZ * sx * sy;
( *vertArray )[id][0] = static_cast< float >( vertIdX ) / static_cast< float >( sx );
( *vertArray )[id][1] = static_cast< float >( vertIdY ) / static_cast< float >( sy );
( *vertArray )[id][2] = static_cast< float >( vertIdZ ) / static_cast< float >( sz );
if( vertIdX < sx - 1 )
{
gridElement->push_back( id );
gridElement->push_back( id + 1 );
}
if( vertIdY < sy - 1 )
{
gridElement->push_back( id );
gridElement->push_back( id + sx );
}
if( vertIdZ < sz - 1 )
{
gridElement->push_back( id );
gridElement->push_back( id + sx * sy );
}
}
}
}
// done. Add it
gridGeometry->setVertexArray( vertArray );
gridGeometry->addPrimitiveSet( gridElement );
osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
// finally, the colors
colors->push_back( m_gridColor );
gridGeometry->setColorArray( colors );
gridGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
if ( m_innerGridGeode->getNumDrawables() )
{
m_innerGridGeode->setDrawable( 0, gridGeometry );
}
else
{
m_innerGridGeode->addDrawable( gridGeometry );
}
// we create a unit cube here as the transformation matrix already contains the proper scaling.
m_gridGeometryUpdate = false;
}
}
......@@ -95,6 +95,63 @@ public:
*/
void setEnableLabels( bool enable = true );
/**
* Returns whether bbox mode is enabled or not.
*
* \return true if bbox rendering are enabled
*/
bool getEnableBBox() const;
/**
* En- or disable bbox mode.
*
* \param enable true to enbable
*/
void setEnableBBox( bool enable = true );
/**
* Returns whether grid mode is enabled or not.
*
* \return true if grid rendering are enabled
*/
bool getEnableGrid() const;
/**
* En- or disable grid mode.
*
* \param enable true to enbable
*/
void setEnableGrid( bool enable = true );
/**
* The currently set color used for rendering the bbox.
*
* \return the current color.
*/
const WColor& getBBoxColor() const;
/**
* Sets the color of the rendered bbox.
*
* \param color the color
*/
void setBBoxColor( const WColor& color );
/**
* The currently set color used for rendering the grid.
*
* \return the current color.
*/
const WColor& getGridColor() const;
/**
* Sets the color of the rendered grid.
*
* \param color the color
*/
void setGridColor( const WColor& color );
protected:
private:
......@@ -136,10 +193,35 @@ private:
*/
bool m_gridUpdate;
/**
* If true, the inner grid geometry gets recreated.
*/
bool m_gridGeometryUpdate;
/**
* If true, labels get used.
*/
bool m_showLabels;
/**
* True if the bbox should be rendered
*/
bool m_showBBox;
/**
* True if the grid should be rendered.
*/
bool m_showGrid;
/**
* The color of bbox/grid
*/
WColor m_bbColor;
/**
* The color of the grid.
*/
WColor m_gridColor;
};
#endif // WGEGRIDNODE_H
......
......@@ -45,6 +45,9 @@
#include "../../kernel/WKernel.h"
#include "WMBoundingBox.h"
#include "WMBoundingBox.xpm"
#include "WMBoundingBox_boundary.xpm"
#include "WMBoundingBox_grid.xpm"
#include "WMBoundingBox_label.xpm"
// This line is needed by the module loader to actually find your module.
W_LOADABLE_MODULE( WMBoundingBox )
......@@ -70,7 +73,7 @@ boost::shared_ptr< WModule > WMBoundingBox::factory() const
const char** WMBoundingBox::getXPMIcon() const
{
return boundingbox_xpm;
return WMBoundingBox_xpm;
}
const std::string WMBoundingBox::getName() const
......@@ -127,7 +130,9 @@ void WMBoundingBox::moduleMain()
WGraphicsEngine::getGraphicsEngine()->getScene()->insert( m_gridNode );
}
m_gridNode->setEnableLabels( m_showCornerCoordinates->get( true ) );
m_gridNode->setBBoxColor( *m_bboxColor );
m_gridNode->setGridColor( *m_gridColor );
updateNode( m_mode );
m_gridNode->setGrid( regGrid );
}
......@@ -151,7 +156,67 @@ void WMBoundingBox::connectors()
void WMBoundingBox::properties()
{
m_showCornerCoordinates = m_properties->addProperty( "Show coordinates", "Show coordinates at the corners of the box.", true, m_recompute );
WPropertyBase::PropertyChangeNotifierType notifier = boost::bind( &WMBoundingBox::updateNode, this, _1 );
m_bboxColor = m_properties->addProperty( "Bounding Box Color", "The color of the bounding box.", WColor( 0.3, 0.3, 0.3, 1.0 ), notifier );
m_gridColor = m_properties->addProperty( "Grid Color", "The color of the grid.", WColor( 0.1, 0.1, 0.1, 1.0 ), notifier );
m_possibleModes = WItemSelection::SPtr( new WItemSelection() );
m_possibleModes->addItem( "Labels", "Show the boundary labels.", WMBoundingBox_label_xpm ); // NOTE: you can add XPM images here.
m_possibleModes->addItem( "Bounding Box", "Show the bounding box.", WMBoundingBox_boundary_xpm );
m_possibleModes->addItem( "Grid", "Show the inner grid.", WMBoundingBox_grid_xpm );
// selecting all at once might be a bad idea since the grid rendering can be very very slow. So, by default, only show bbox and labels.
WItemSelector sel = m_possibleModes->getSelectorFirst();
m_mode = m_properties->addProperty( "Mode", "What should be rendered.", sel.newSelector( 1 ), notifier );
WPropertyHelper::PC_NOTEMPTY::addTo( m_mode );
WModule::properties();
}
void WMBoundingBox::updateNode( WPropertyBase::SPtr property )
{
// only update if there is a grid node
if ( !m_gridNode )
{
return;
}
// color of bbox changed
if ( property == m_bboxColor )
{
m_gridNode->setBBoxColor( *m_bboxColor );
}
// color of grid changed
if ( property == m_gridColor )
{
m_gridNode->setGridColor( *m_gridColor );
}
// mode changed
if ( property == m_mode )
{
WItemSelector s = m_mode->get( true );
bool labels = false;
bool bbox = false;
bool grid = false;
// The multi property allows the selection of several items. So, iteration needs to be done here:
for( size_t i = 0; i < s.size(); ++i )
{
size_t idx = s.getItemIndexOfSelected( i );
// check what was selected
labels = labels || ( idx == 0 );
bbox = bbox || ( idx == 1 );
grid = grid || ( idx == 2 );
}
m_gridNode->setEnableLabels( labels );
m_gridNode->setEnableGrid( grid );
m_gridNode->setEnableBBox( bbox );
}
}
......@@ -109,7 +109,13 @@ private:
boost::shared_ptr< WModuleInputData< WDataSetSingle > > m_input; //!< Input connector required by this module.
WPropBool m_showCornerCoordinates; //!< Show/hide the coordinates in the rendering.
WItemSelection::SPtr m_possibleModes; //!< the modes available.
WPropSelection m_mode; //!< the mode (bbox, grid, both)
WPropColor m_bboxColor; //!< the color of the bounding box
WPropColor m_gridColor; //!< the color of the grid
/**
* This condition denotes whether we need to recompute the surface
......@@ -120,5 +126,12 @@ private:
* The node actually drawing the grid.
*/
WGEGridNode::SPtr m_gridNode;
/**
* Handles updates in properties.
*
* \param property the property that has updated.
*/
void updateNode( WPropertyBase::SPtr property );
};
#endif // WMBOUNDINGBOX_H
/* XPM */
static const char * boundingbox_xpm[] = {
"16 16 2 1",
static const char * WMBoundingBox_xpm[] = {
"32 32 3 1",
" c None",
". c #000000",
" . ",
" ..... ",
" .. . ... ",
" . . ... ",
"... . ..",
". .... .. .",
". .. . .",
". . ... .",
". .. . .",
". .. ... .",
". .. .. .",
".. . ... .",
".. . . ",
" ... . .. ",
" ... . . ",
" ... "};
". c #7D7D7D",
"+ c #000000",
" ",
" .............................. ",
" .............................. ",
" .............................. ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ... + + + + + + ... ",
" ...++++++++++++++++++++++++... ",
" ... + + + + + + ... ",
" .............................. ",
" .............................. ",
" .............................. ",
" "};
/* XPM */
static const char * WMBoundingBox_boundary_xpm[] = {
"32 32 2 1",
" c None",
". c #7D7D7D",
" ",
" .............................. ",
" .............................. ",
" .............................. ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" ... ... ",
" .............................. ",
" .............................. ",
" .............................. ",
" "};
/* XPM */
static const char * WMBoundingBox_grid_xpm[] = {
"32 32 2 1",
" c None",
". c #000000",
" ",
" ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" . . . . . . . . ",
" . . . . . . . . ",
" . . . . . . . . ",
" ............................. ",
" "};
/* XPM */
static const char * WMBoundingBox_label_xpm[] = {
"32 32 2 1",
" c None",
". c #000000",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ... ..... ...... ",
" ..... ....... ........ ",
" .. .. . ... . ... ",
" .. .. .. ",
" .. .. ... ",
" .. ... ..... ",
" .. .. ...... ",
" .. .. ... ",
" .. .. .. ",
" .. .. .. ",
" .. .. . ... ",
" ........ ........ ........ ",
" ........ ........ ...... ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment