CGAL 5.5.2  Principal Component Analysis

This package provides functions to analyze sets of objects in 2D and 3D. It provides the computation of axisaligned bounding boxes, centers of mass and principal component analysis for all bounded objects, as well as barycenters for sets of weighted points. Note that unlike most of the CGAL packages, this package uses approximation methods (in particular for linear least squares fitting) and is not intended to provide an exact canonical result in any sense.
A bounding box for a set of objects is a cuboid that contains the set. An axisaligned bounding box captures the maximum extents of all objects from the set within their coordinate system, i.e., a bounding box aligned with the axes of the coordinate system. Axisaligned bounding boxes are frequently used in geometric algorithms as an indication of the general location of a data set, for either display, firstapproximation spatial query, or spatial indexing purposes.
A centroid of a set of objects is their center of mass, i.e., the point whose coordinates are computed by means of coordinates of all points composing the objects. Note that although the general definition of center of mass incorporates a density function (and hence weighted means), the current implementation assumes a uniform density (see barycenter below defined for weighted points). For a point set \( \{X_1,X_2,...,X_N\}\) the centroid \( \bar{X}\) is computed as
\[ \bar{X} = \frac{1}{N} \sum_{i=1}^{N}{ X_i}. \]
For a set of segments \( \{S_1,S_2,...,S_N\}\) the centroid \( \bar{X}\) is computed as
\[ \bar{X} = \frac{1}{\sum_{i=1}^{N}{\leftS_i\right}} \sum_{i=1}^{N}{ \leftS_i\right \bar{S_i}}, \]
where \( \leftS_i\right\) stands for the length of a segment and \( \bar{S_i}\) stands for its uniform barycenter (midpoint). For a set of triangles \( \{T_1,T_2,...,T_N\}\) the centroid \( \bar{X}\) is computed as
\[ \bar{X} = \frac{1}{\sum_{i=1}^{N}{\leftT_i\right}} \sum_{i=1}^{N}{ \leftT_i\right \bar{T_i}}, \]
where \( \leftT_i\right\) stands for the area of a triangle and \( \bar{T_i}\) stands for its uniform barycenter. Such definition still holds for more general objects where the Lebesgue measure (length in 1D, area in 2D, volume in 3D) is used for weighting the object barycenters. Centers of mass are used to summarize data sets for either approximation, spatial query or spatial indexing purposes.
A barycenter of a set of weighted points is the point whose coordinates are computed by means of weighted coordinates of all weighted points from the set. When all weights are equal the barycenter coincides with the centroid.
Given a set of objects, linear least squares fitting amounts to finding the linear subspace which minimizes the sum of squared distances from all points composing the objects of the set, to their projection onto this linear subspace. Such linear subspace is obtained by socalled principal component analysis (PCA). PCA is defined as a transformation that transforms the objects to a new coordinate system such that the greatest variance by orthogonal projection of the objects comes to lie on the first coordinate (called the first principal component), the second greatest variance on the second coordinate, and so on. PCA is often used in geometric applications to reduce data sets to lower dimensions for analysis or approximation. Figure below illustrates (least squares) fitting of a line to a 2D point set, fitting of a line and a plane to a 3D point set and fitting of a plane to a set of 3D triangles.
In the following example we use STL containers of 2D points and 3D points, and compute their axisaligned bounding box.
File Principal_component_analysis/bounding_box.cpp
In the following example we use STL containers of 2D, 3D points and 3D triangles, and compute their centroid. The dimension tag indicates the dimension of the objects being fitted (0 for points, 2 for triangles). Note that it is also possible to compute the centroid of the triangle edges (3D segments) by specifying 1 as dimension tag, and to compute the centroid of the triangle vertices (3D points) by specifying 0 as dimension tag.
File Principal_component_analysis/centroid.cpp
In the following example we use STL containers of 2D and 3D weighted points, and compute their barycenter.
File Principal_component_analysis/barycenter.cpp
In the following example we use a STL container of 2D points, and compute the best fitting line in the least squares sense.
File Principal_component_analysis/linear_least_squares_fitting_points_2.cpp
In the following example we use a STL container of 3D triangles, and compute the best fitting line and plane in the least squares sense. Note that we can fit either the whole triangles, the triangle edges or the triangle vertices.
File Principal_component_analysis/linear_least_squares_fitting_triangles_3.cpp