DGtal  1.5.beta
viewer3D-11-extension.cpp
1 
42 #include <iostream>
43 
44 #include "DGtal/base/Common.h"
45 #include "DGtal/helpers/StdDefs.h"
46 #include "DGtal/io/viewers/Viewer3D.h"
48 
49 using namespace std;
50 using namespace DGtal;
51 using namespace Z3i;
52 
54 // Standard services - public :
55 
57 // Deriving from Viewer3D::Extension to add new callbacks to events.
58 struct RandomPointKeyExtension : public Viewer3D<Space, KSpace>::Extension
59 {
60  RandomPointKeyExtension()
61  {
62  }
63 
64  // Here we override the "key pressed" event, and a point randomly in
65  // the scene if the key "Shift+R" is pressed.
66  virtual bool keyPressEvent( Viewer & viewer, QKeyEvent * event )
67  {
68  bool handled = false;
69  // Get event modifiers key
70  const Qt::KeyboardModifiers modifiers = event->modifiers();
71  if ( ( event->key() == Qt::Key_R ) && ( modifiers == Qt::ShiftModifier ) )
72  {
73  Point p = viewer.space().lowerBound();
74  Point q = viewer.space().upperBound();
75  Point d = q - p;
76  Point a( ( rand() % d[ 0 ] ) + p[ 0 ], ( rand() % d[ 1 ] ) + p[ 1 ],
77  ( rand() % d[ 2 ] ) + p[ 2 ] );
78  viewer << a;
79  viewer << Viewer::updateDisplay;
80  trace.info() << "Adding point " << a << std::endl;
81  handled = true;
82  }
83  return handled;
84  }
85 
86  // We also override the Viewer3D::init method to add a key
87  // description in the help window.
88  virtual void init( Viewer & viewer )
89  {
90  viewer.setKeyDescription( Qt::ShiftModifier + Qt::Key_R,
91  "Creates a random digital point." );
92  }
93 
94  // We also override the Viewer3D::helpString method to add a
95  // description to the viewer.
96  virtual QString helpString( const Viewer & /*viewer*/ ) const
97  {
98  QString text( "<h2> Random point Viewer3D </h2>" );
99  text += "Press Shift+R to add points.";
100  return text;
101  }
102 };
104 
105 int main( int argc, char ** argv )
106 {
107 
108  QApplication application( argc, argv );
109 
110  Point p1( 0, 0, 0 );
111  Point p2( 5, 5, 5 );
112  Point p3( 2, 3, 4 );
113  Domain domain( p1, p2 );
114 
115  typedef Viewer3D<> MyViewer;
116  KSpace K;
117  K.init( p1, p2, true );
119  MyViewer viewer( K );
120  viewer.setExtension( new RandomPointKeyExtension );
122  viewer.show();
123  viewer << domain;
124  viewer << p1 << p2 << p3;
125 
126  viewer << MyViewer::updateDisplay;
127  return application.exec();
128 }
129 // //
const KSpace & space() const
Definition: Display3D.h:340
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
std::ostream & info()
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
void setExtension(Extension *ext)
Definition: Viewer3D.h:312
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
int main(int argc, char **argv)
K init(Point(0, 0, 0), Point(512, 512, 512), true)
KSpace K
Domain domain