PCLPainter2D
PCLPainter2D class provides a very simple interface (just like PCLPlotter) to draw 2D figures in a canvas or a view. One can add figures by simple add*() methods and in the end, show the canvas by simple display*() methods.
Basic structure
Following is the usual way of using PCLPainter2D class.
//1. declare a Painter2D class
PCLPainter2D painter;
//2. add figures to the canvas by simple add*() methods. Use transform*() functions if required.
painter.addCircle (0,0,5);
painter.addLine (0,0, 5,0);
//3. call a display*() (display (), spin (), spinOnce ()) method for the display of the canvas
painter.display ();
Discussions
I am keeping this discussion here so that the design decision gets highlighted and is not lost in an unnoticed blog. Users who just want to learn this class can safely go ahead to the next section showing a complete example.
So, Lets see how 2D drawing works in VTK! The VTK user needs to first:
Make a subclass of vtkContextItem
Re-implement (override) Paint () of vtkContextItem. (shown in the figure)

It would be really nice to have a vtkContextItem class which cuts off the overhead of subclassing and allows user to draw directly from the function calls. Unfortunately, we don’t have any (out of vtkChart, vtkPlot, vtkAxis,…, etc.) vtkContextItem class with that kind of behavior.
Thus, it maybe wise to have a class like Painter2D which can avoid subclassing in PCL and its rendering could be further optimized in the future.
A complete example
Following is a complete example depcting many usage of the Plotter. Copy it into a file named pcl_painter2D_demo.cpp
.
1/* \author Kripasindhu Sarkar */
2
3#include <iostream>
4#include <map>
5#include <vector>
6#include <pcl/visualization/pcl_painter2D.h>
7//----------------------------------------------------------------------------
8
9int main ()
10{
11 pcl::visualization::PCLPainter2D *painter = new pcl::visualization::PCLPainter2D();
12
13 int winw = 800, winh = 600;
14 painter->setWindowSize (winw, winh);
15 int xpos = 0;
16 int r = winw;
17 int R = 50;
18 int inc = 5;
19 int noc = winw/R;
20
21 while (1)
22 {
23 //draw noc no of circles
24 for (int i = 0; i < noc; i++)
25 {
26 if (i % 2)
27 painter->setBrushColor (0, 0, 0, 200);
28 else
29 painter->setBrushColor (255, 255, 255, 200);
30
31 int rad = r - i*R;
32 if (rad < 0) { rad = winw + rad;}
33
34 painter->addCircle (winw/2, winh/2, rad);
35 }
36
37 r -= inc;
38 if (r < winw-R) r = winw + R;
39
40 painter->setBrushColor (255,0,0,100);
41 painter->addRect ((xpos += inc) % winw, 100, 100, 100);
42
43 //display
44 painter->spinOnce ();
45 painter->clearFigures ();
46 }
47
48
49 return 0;
50}
Compiling and running the program
Add the following lines to your CMakeLists.txt file:
1cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
2project(pcl_painter2D_demo)
3find_package(PCL 1.7)
4include_directories(${PCL_INCLUDE_DIRS})
5link_directories(${PCL_LIBRARY_DIRS})
6add_definitions(${PCL_DEFINITIONS})
7add_executable(pcl_painter2D_demo pcl_painter2D_demo.cpp)
8target_link_libraries(pcl_painter2D_demo ${PCL_LIBRARIES})
Compile and run the code by the following commands
$ cmake .
$ make
$ ./pcl_painter2D_demo
Video
The following video shows the the output of the demo.