-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1259 from JacquesOlivierLachaud/ExtendViewer3D
Extend viewer3 d
- Loading branch information
Showing
10 changed files
with
756 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
**/ | ||
|
||
/** | ||
* @file io/viewers/viewer3D-11-extension.cpp | ||
* @ingroup examples/3dViewer | ||
* @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr ) | ||
* Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France | ||
* | ||
* @date 2017/03/05 | ||
* | ||
* Simple example of class Viewer3D. | ||
* | ||
* This file is part of the DGtal library. | ||
*/ | ||
|
||
/** | ||
* 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 moduleQGLExtension | ||
* \example io/viewers/viewer3D-11-extension.cpp | ||
* \image html simple3dVisu1.png "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 : | ||
|
||
|
||
//! [viewer3D-extension-derivation] | ||
// 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 ) ) | ||
{ | ||
typedef Viewer::KSpace KSpace; | ||
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; | ||
viewer << Viewer::updateDisplay; | ||
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; | ||
} | ||
|
||
}; | ||
//! [viewer3D-extension-derivation] | ||
|
||
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; | ||
KSpace K; | ||
K.init( p1, p2, true ); | ||
//! [viewer3D-extension-set-extension] | ||
MyViewer viewer( K ); | ||
viewer.setExtension( new RandomPointKeyExtension ); | ||
//! [viewer3D-extension-set-extension] | ||
viewer.show(); | ||
viewer << domain; | ||
viewer << p1 << p2 << p3; | ||
|
||
viewer<< MyViewer::updateDisplay; | ||
return application.exec(); | ||
} | ||
// // | ||
/////////////////////////////////////////////////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
|
||
/* | ||
* Useful to avoid writing DGtal:: in front of every class. | ||
*/ | ||
namespace DGtal { | ||
|
||
/** | ||
* @page moduleQGLExtension Extending the QGLViewer Viewer3D interface | ||
* | ||
* | ||
* This part of the manual describes how to extend the standard | ||
* Viewer3D interface of DGtal. For instance, you wish to assign new | ||
* handlers keys or to mouse event, or you wish to display or | ||
* select other graphical elements. To do that, you just have to write | ||
* a class that derived from Viewer3D::Extension and overrides some | ||
* methods to get your desired behaviour. | ||
* | ||
* @author Jacques-Olivier Lachaud | ||
* | ||
* @since 9.4 | ||
* | ||
* Related examples: viewer3D-11-extension.cpp | ||
* | ||
* Related modules: \ref moduleQGLInteraction | ||
* | ||
* [TOC] | ||
* | ||
* \section QGLExtension_sec1 Extending the standard behavior of Viewer3D | ||
* | ||
* The Viewer3D interface provides a very handy way of visualising 3D | ||
* scenes, digital objects or meshes in DGtal, and is bundled with | ||
* many functionalities (like moving the light, selecting some | ||
* objects, etc). However it is not directly extensible as is, and, | ||
* for several reasons, you cannot derive from the Viewer3D class | ||
* directly to add some new functionalities. | ||
* | ||
* Therefore we provide the class Viewer3D::Extension so that you can | ||
* extend the viewer in several ways. The principle is very easy. This | ||
* interface provides a set of empty virtual methods so you just have | ||
* to override them in order to change the behavior of the viewer. For | ||
* instance, you just have to override | ||
* Viewer3D::Extension::keyPressEvent to capture other key events. You | ||
* can override all the following methods: | ||
* | ||
* - Viewer3D::Extension::keyPressEvent. This method may be | ||
* overloaded to capture other key events. | ||
* | ||
* - Viewer3D::Extension::drawWithNames. This method is useful for | ||
* drawing elements with additional information for selection (see also \ref moduleQGLInteraction). | ||
* | ||
* - Viewer3D::Extension::draw. This method is useful for drawing elements that are not directly handled by Viewer3D. | ||
* | ||
* - Viewer3D::Extension::init. This method is useful at initialization (e.g. adding lights manipulators). | ||
* | ||
* - Viewer3D::Extension::helpString. This method is useful to change the help message. | ||
* | ||
* - Viewer3D::Extension::postSelection. This method is useful to take | ||
* care of mouse selection events. | ||
* | ||
* - Viewer3D::Extension::mouseMoveEvent. This method is useful to | ||
* take care of mouse movements. | ||
* | ||
* - Viewer3D::Extension::mousePressEvent. This method is useful to | ||
* take care of mouse button press events. | ||
* | ||
* - Viewer3D::Extension::mouseReleaseEvent. This method is useful to | ||
* take care of mouse button release events. | ||
* | ||
* The example below shows how to capture "Shift+R" key pressed | ||
* events, so that each time this event occurs random points are added | ||
* to the 3D scene. | ||
* | ||
* @snippet io/viewers/viewer3D-11-extension.cpp viewer3D-extension-derivation | ||
* | ||
* \section QGLExtension_sec2 Activating your extension in some viewer | ||
* | ||
* Once you have built a class that derives from Viewer3D::Extension, | ||
* it remains to create some instance of this extension and to hand it | ||
* to your viewer. This is done by the following lines. | ||
* | ||
* @snippet io/viewers/viewer3D-11-extension.cpp viewer3D-extension-set-extension | ||
* | ||
* The viewer will take care of freeing your instanciated extension object. | ||
* For a complete example, see viewer3D-11-extension.cpp. | ||
*/ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.