MIRA
|
Previous: TutorialCreateBasic2DVizPage | Next: Tutorials for Developers |
---|
2D visualizations use the Qt GraphicsScene as render system. Therefore, all data can be displayed using QGraphicsItems. Here, step by step we will create a 2D visualization for a Point2f channel.
The full working example (source code, makefile) can be found in gui/examples/tutorials/Point2Visualization.C
.
First we will create our visualization class. We can do that by deriving a class from mira::Visualization2D. The meta information on that class will be used to group and display visualizations in a list view of all available visualizations. We can specify the name, the category, and a description that is displayed in the visualizations dialog.
We also need to implement the pure virtual method setupScene() from our base class to setup our graphics scene. We will fill it with life below.
In order to compile our visualization into a library, we need a CMakeLists.txt file:
The class can now be used by adding it via the "+"-Button of the Visualization Control view even if if it will not render anything yet. To change that, we need to setup our scene.
First, we need a member and initialize it in the setupScene method:
Now, an ellipse is rendered at the origin of the scenery. The position can be changed via:
But before changing the position of the ellipse, we first need to obtain it from a Point2f channel. Therefore, we add a ChannelProperty member and reflect it in our reflect() method:
To update the position of the ellipse corresponding to the channel, we need to listen for changes on that channel. This is done by specifying a callback, i.e. by binding a member method using setDataChangedCallback(). This is similar to subscribing to a "normal" channel:
That was easy. But what if the coordinates in that point channel are given in a coordinate system defined by the publisher of that channel instead of the world coordinate frame?
To render the ellipse at its real position corresponding to the current frame of the 2D editor view, we need to take the selected editor frame and the frame of the channel into account.
To be able to do this we need to create a new QGraphicsItem as a parent node item where we attach our ellipse item to.
Done. The ellipse item is now a child of the new node item and will be positioned relative to that item. We can now also update the position of the node item according to the channel frame. But let's consider that the channel is only updated once in a minute.
So far, when the user changes the transform frame of the 2D editor view, the position of our ellipse ss not updated until the data in the channel changes. Besides, our data frame could be part of a transform chain where other frames get updated more often than our point channel. To overcome this limitation, we can overwrite another method of our base class - "onUpdate", that is called periodically by the GUI thread. The time span since the last call of update is passed as parameter dt.
Now our ellipse is positioned relative to a node item that gets translated to the selected frame of our editor.
At the moment our visualization can be added via the "Add" button of the visualization control only. To enable drag'n'drop support, allowing us to drag the Point2f channel directly into the 2D view, we need to implement the getDataConnection() method:
This method tells the visualization view which type of data we can visualize and what is the main data channel for visualization.
Next: Tutorials for Developers |
---|