Developer Documentation
|
The last tutorial dealt with the implementation of a first plugin without any functionality at all. This tutorial will show you how to progam a simple mesh smoother. We are assuming that you have read A simple plugin and will only explain parts that have to be added in order to include new functionality.
To start off we first have to add two additional header files. The first one is the ToolboxInterface
. We need this interface because we want to add a Toolbox to our plugin in order to be able to set parameters. Additionally, we want to use the LoggingInterface
so that we can inform the user of our plugin when something went wrong.
In order to work with the Interfaces we have to define that the plugin will implement the additional interfaces. The class definition then looks as follows:
Furthermore we have to insert the Q_INTERFACES
macro. This makes the signals and slots from both interfaces available.
Now that we have defined which Interfaces to implement, we can look at which signals and slots we want to implement. Firstly, we need two additional signals from the BaseInterface
:
The first signal updateView() is emitted when we have finished computing the smoothed mesh. The signal tells OpenFlipper to redraw its viewer(s). The second signal updatedObjects() is emitted to inform all other plugins that an object has changed and therfore allows each plugin to react on this change.
Since we also want to use the LoggingInterface
we have to define the following signals which allow us to send log messages to the OpenFlipper Loggers.
The last Interface that's left over is the ToolboxInterface
from which we are only implementing one signal:
As we will see later, this function is used to add a ToolBox (i.e. a QWidget
) to the OpenFlipper user interface.
As a last step we have to add to include additional class members to control the parameter for the smoothing and to actually compute the smoothed mesh.
That's it for the header of the plugin. The complete header looks like this:
As we have already mentioned in Definition of the header, the tool box is generated inside the pluginsInitialized() function and then added by the addToolbox() signal passing a reference to our toolbox. So we begin by creating the elements of the ToolBox:
Later, the smoothButton is used to start the mesh smoothing and the iterationsSpinbox_ allows the user to control the number of smoothing iterations that should be performed.
The created Toolbox elements are then combined into a Layout
Here, the SpacerItem, which is added last, only helps aligning the elements at the top of the toolbox.
We have to connect the smoothButton to the simpleLaplace() slot of our class, so that after clicking the button the slot is called. Finally, when the tool box has been entirely created, we emit the signal to add the tool box to OpenFlipper:
Now that the Toolbox is completely setup, we can start implementing the smoother. We begin with searching objects on which we have to compute the smoothing. The algorithm takes all objects marked as target and tries to apply the smoothing.
To find all these Objects we use an ObjectIterator. This iterator can be found in the PluginFunctions. Every communication between OpenFlipper and its Plugins is accomplished through either an interface or the PluginFunctions. So these are the places to look for if you want to add additional functionality and therefore need to communicate with OpenFlipper.
We initialize the PluginFunctions::ObjectIterator with PluginFunctions::TARGET_OBJECTS and thereby make sure that the iteration is restricted to target objects only.
In the next line we test if we have to deal with a triangle mesh. Since computation on triangle meshes is in many cases more efficient than on arbitrary polygonal meshes OpenMesh treats them seperately (see OpenMesh documentation for further information on this topic). See also OpenFlipper/common/Types.hh for data types in OpenFlipper.
First off, we backup the current vertex positions by adding a custom property to our mesh. We assume that you already made yourself familiar with OpenMesh. Otherwise we recommend you to read the OpenMesh documentation first since we use many of OpenMeshes's core functions in this tutorial.
The smoothing algorithm itself depends on the number of iterations that one can adjust in the plugin's toolbox.
In a few words the algorithm iterates over each vertex of the mesh, accumulates the positions of it's neighboring vertices, devides the sum by it's valence and updates it's position to the computed value. If the current vertex is a boundary vertex, we skip over to the next one without updating the position.
Now that the smoothing operation is done, we want to get rid of the original vertex positions. Since we changed vertices, we also have to update the normals.
We now implement the same procedure for arbitrary polygonal meshes using the template types of OpenMesh. Starting at line (see The complete source code of the example for the complete source code of this example plugin):
The next step is to tell the OpenFlipper core that our plugin has updated an object in the scene. The affected object will then be redrawn.
The smoothing algorithm on arbitrary polygonal meshes in this case works analogously. See The complete source code of the example for the complete source code.
If the object's type is neither triangle nor polygonal mesh, we make use of the implemented LoggingInterface by dumping an error log message to OpenFlipper's log widget.
SmootherPlugin.hh
SmootherPlugin.cc
We use the cmake project file presented in How to build the plugin with CMake to build this plugin.