DGtal  1.3.beta
io/viewers/viewer3D-11-extension.cpp

Example of extension of Viewer3D interface by deriving the class Viewer3D::Extension. Here we have added a callback to the "Shift+R" keypressed event, which adds a point randomly in the domain.

See also
moduleQGLExtension
Extending the Viewer3D interface: just press

Shift+R and you have new points added randomly in the scene."

#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/io/viewers/Viewer3D.h"
using namespace std;
using namespace DGtal;
using namespace Z3i;
// Standard services - public :
// Deriving from Viewer3D::Extension to add new callbacks to events.
struct RandomPointKeyExtension : public Viewer3D<Space, KSpace>::Extension
{
RandomPointKeyExtension()
{
}
// Here we override the "key pressed" event, and a point randomly in
// the scene if the key "Shift+R" is pressed.
virtual bool keyPressEvent( Viewer & viewer, QKeyEvent * event )
{
bool handled = false;
// Get event modifiers key
const Qt::KeyboardModifiers modifiers = event->modifiers();
if ( ( event->key() == Qt::Key_R ) && ( modifiers == Qt::ShiftModifier ) )
{
Point p = viewer.space().lowerBound();
Point q = viewer.space().upperBound();
Point d = q - p;
Point a( ( rand() % d[ 0 ] ) + p[ 0 ], ( rand() % d[ 1 ] ) + p[ 1 ],
( rand() % d[ 2 ] ) + p[ 2 ] );
viewer << a;
trace.info() << "Adding point " << a << std::endl;
handled = true;
}
return handled;
}
// We also override the Viewer3D::init method to add a key
// description in the help window.
virtual void init( Viewer & viewer )
{
viewer.setKeyDescription( Qt::ShiftModifier + Qt::Key_R,
"Creates a random digital point." );
}
// We also override the Viewer3D::helpString method to add a
// description to the viewer.
virtual QString helpString( const Viewer & /*viewer*/ ) const
{
QString text( "<h2> Random point Viewer3D </h2>" );
text += "Press Shift+R to add points.";
return text;
}
};
int main( int argc, char ** argv )
{
QApplication application( argc, argv );
Point p1( 0, 0, 0 );
Point p2( 5, 5, 5 );
Point p3( 2, 3, 4 );
Domain domain( p1, p2 );
K.init( p1, p2, true );
MyViewer viewer( K );
viewer.setExtension( new RandomPointKeyExtension );
viewer.show();
viewer << domain;
viewer << p1 << p2 << p3;
return application.exec();
}
// //
DGtal::HyperRectDomain< Space >
DGtal::KhalimskySpaceND::init
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
DGtal::trace
Trace trace
Definition: Common.h:154
K
KSpace K
Definition: testCubicalComplex.cpp:62
DGtal::Display3D< SpaceND< 3 >, KhalimskySpaceND< 3 > >::updateDisplay
@ updateDisplay
Definition: Display3D.h:249
DGtal::Trace::info
std::ostream & info()
DGtal::Viewer3D
Definition: Viewer3D.h:135
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
domain
Domain domain
Definition: testProjection.cpp:88
Point
MyPointD Point
Definition: testClone2.cpp:383
init
K init(Point(0, 0, 0), Point(512, 512, 512), true)
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:64
DGtal::Display3D::space
const KSpace & space() const
Definition: Display3D.h:340