1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
.. _project_inliers:
Projecting points using a parametric model
------------------------------------------
In this tutorial we will learn how to project points onto a parametric model
(e.g., plane, sphere, etc). The parametric model is given through a set of
coefficients -- in the case of a plane, through its equation: ax + by + cz + d
= 0.
The code
--------
First, create a file, let's say, ``project_inliers.cpp`` in your favorite
editor, and place the following inside it:
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:linenos:
The explanation
---------------
Now, let's break down the code piece by piece.
We first import the ModelCoefficients structure then the ProjectInliers filter.
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:lines: 4-5
We then create the point cloud structure, fill in the respective values, and
display the content on screen.
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:lines: 14-29
We fill in the ModelCoefficients values. In this case, we use a plane model,
with ax+by+cz+d=0, where a=b=d=0, and c=1, or said differently, the X-Y plane.
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:lines: 32-36
We create the ProjectInliers object and use the ModelCoefficients defined above
as the model to project onto.
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:lines: 39-43
Finally we show the content of the projected cloud.
.. literalinclude:: sources/project_inliers/project_inliers.cpp
:language: cpp
:lines: 45-49
Compiling and running the program
---------------------------------
Add the following lines to your CMakeLists.txt file:
.. literalinclude:: sources/project_inliers/CMakeLists.txt
:language: cmake
:linenos:
After you have made the executable, you can run it. Simply do::
$ ./project_inliers
You will see something similar to::
Cloud before projection:
0.352222 -0.151883 -0.106395
-0.397406 -0.473106 0.292602
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
Cloud after projection:
0.352222 -0.151883 0
-0.397406 -0.473106 0
-0.731898 0.667105 0
-0.734766 0.854581 0
-0.4607 -0.277468 0
A graphical display of the projection process is shown below.
.. image:: images/project_inliers_2.png
Note that the coordinate axes are represented as red (x), green (y), and blue
(z). The five points are represented with red as the points before projection
and green as the points after projection. Note that their z now lies on the X-Y
plane.
|