DGtal 1.4.0
Loading...
Searching...
No Matches
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 );
typedef Viewer3D<> MyViewer;
K.init( p1, p2, true );
MyViewer viewer( K );
viewer.setExtension( new RandomPointKeyExtension );
viewer.show();
viewer << domain;
viewer << p1 << p2 << p3;
viewer << MyViewer::updateDisplay;
return application.exec();
}
// //
const KSpace & space() const
Definition Display3D.h:340
std::ostream & info()
virtual void init()
init
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition Common.h:153
STL namespace.
int main()
Definition testBits.cpp:56
MyPointD Point
KSpace K
Domain domain
HyperRectDomain< Space > Domain