MRPT图形界面
mrpt-gui模块中提供了三个类以实现显示GUI窗口,每个都有特定的用途:
- mrpt::gui::CDisplayWindow : Displays 2D bitmap images, and optionally sets of points over them, etc.
- mrpt::gui::CDisplayWindow3D : A powerful 3D rendering window capable of displaying a mrpt::opengl::COpenGLScene. It features mouse navigation, Alt+Enter fullscreen switching, multiple viewports, etc. See this tutorial.
- mrpt::gui::CDisplayWindowPlots : Displays one or more 2D vectorial graphs, in a manner very similar to MATLAB "plot" commands.
All these window classes inherits from mrpt::gui::CBaseGUIWindow, which provides a set of methods and variables common to all the classes. It allow moving/resizing the windows, polling for key strokes, etc. All the classes in this library are in the namespace mrpt::gui
在Ubuntu中使用如下命令安装mrpt的APP和lib:
sudo apt-get install mrpt-apps libmrpt-dev
下面的代码绘制一幅正态分布概率密度函数图,自变量范围为0~5。
#include <mrpt/gui/CDisplayWindowPlots.h> #include <mrpt/math/CVectorTemplate.h> #include <mrpt/math/distributions.h> #include <mrpt/system/os.h> #include <mrpt/system/threads.h> using namespace std; using namespace mrpt; using namespace mrpt::math; using namespace mrpt::gui; // ------------------------------------------------------ // TestDisplayPlots // ------------------------------------------------------ int main() { CDisplayWindowPlots win("Example of function plot",500,400); //width:500, height:400 // Generate data for a 2D plot: CVectorDouble X,Y; for (double x=0;x<5;x+=0.01f) { //Evaluates the multivariate normal (Gaussian) distribution at a given point "x". double y = normalPDF(x, 2,0.3); X.push_back(x); Y.push_back(y); } //Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax. /********************************************************************* The lineFormat string is a combination of the following characters: Line styles: '.': One point for each data point '-': A continuous line ':': A dashed line Colors: k: black r: red g: green b: blue m: magenta c: cyan Line width: '1' to '9': The line width (default=1) Examples: 'r.' -> red points. 'k3' or 'k-3' -> A black line with a line width of 3 pixels. ********************************************************************/ win.plot(X,Y,"b-2"); //Enable/disable the feature of pan/zoom with the mouse (default=enabled) win.enableMousePanZoom(true); //Enable/disable the fixed X/Y aspect ratio fix feature win.axis_equal(false); //Fix automatically the view area according to existing graphs win.axis_fit(); //Changes the position of the window on the screen. win.setPos(10,10); cout << "Press any key to exit..." << endl; win.waitForKey(); //while (!mrpt::system::os::kbhit() &&win.isOpen()) //mrpt::system::sleep(50); return 0; }
使用CMake来生成makefile:
SET(sampleName displayPlots) SET(PRJ_NAME "EXAMPLE_${sampleName}") # --------------------------------------- # Declare a new CMake Project: # --------------------------------------- PROJECT(${PRJ_NAME}) # These commands are needed by modern versions of CMake: CMAKE_MINIMUM_REQUIRED(VERSION 2.6) FIND_PACKAGE(MRPT REQUIRED base;gui) # --------------------------------------------- # TARGET: # --------------------------------------------- # Define the executable target: ADD_EXECUTABLE(${sampleName} test.cpp ) # Add the required libraries for linking: TARGET_LINK_LIBRARIES(${sampleName} ${MRPT_LIBS} # This is filled by FIND_PACKAGE(MRPT ...) "" # Optional extra libs... )
其中,enableMousePanZoom函数参数为true时可以进行如下操作:滚动鼠标中键,可以上下平移函数图象;按住鼠标右键可以对图像进行拖动;鼠标左键可以进行框选将函数图象放大。当设为false时这些操作被禁用。axis_equal函数参数为true时,X、Y坐标轴的单位长度将相等,即以相同的比例显示函数图象。axis_fit()函数可以用来自动调整函数图象以适应窗口。
参考:
http://www.mrpt.org/MRPT_in_GNU/Linux_repositories
https://github.com/MRPT/mrpt/tree/master/samples/displayPlots