Public Types | Public Member Functions | Public Attributes | Protected Attributes

TRTK::FitSphere< T > Class Template Reference

Estimates the parameters of a 3D sphere from a set of points lying on the surface. More...

#include <FitSphere.hpp>

Inheritance diagram for TRTK::FitSphere< T >:
Collaboration diagram for TRTK::FitSphere< T >:

List of all members.

Public Types

enum  Error { NOT_ENOUGH_POINTS, UNKNOWN_ERROR, WRONG_POINT_SIZE }
typedef super::Vector3T Vector3T
 3D column vector with value type T.
typedef super::Vector4T Vector4T
 4D column vector with value type T.
typedef super::VectorXT VectorXT
 Column vector of arbitrary size with value type T.
typedef super::MatrixXT MatrixXT
 Matrix of arbitrary size with value type T.
typedef super::ArrayXT ArrayXT
 General-purpose array of arbitrary size with value type T.
typedef super::Vector2T Vector2T
 2D column vector with value type T.
typedef super::Matrix2T Matrix2T
 2 x 2 matrix with value type T.
typedef super::Matrix3T Matrix3T
 3 x 3 matrix with value type T.

Public Member Functions

 FitSphere ()
 Constructs an empty FitSphere object.
 FitSphere (const std::vector< Coordinate< T > > &points)
 Constructs a FitSphere object.
 FitSphere (const std::vector< Vector3T > &points)
 Constructs a FitSphere object.
 FitSphere (const std::vector< Vector4T, Eigen::aligned_allocator< Vector4T > > &points)
 Constructs an FitSphere object.
virtual ~FitSphere ()
 Destructs the FitSphere object.
void compute ()
 Runs the fitting algorithm.
const Coordinate< T > & getCenterPoint () const
 Returns the center point.
getDistanceTo (const Coordinate< T > &point) const
 Returns the shortest distance from the given point to the sphere.
unsigned getNumberPointsRequired () const
 Returns the minimum number of data points required to compute a model.
getRadius () const
 Returns the radius of the sphere.
getRMS () const
 Returns the root mean square error.
void setPoints (const std::vector< Coordinate< T > > &)
 Sets the point set.
void setPoints (const std::vector< Vector3T > &)
 Sets the point set.
void setPoints (const std::vector< Vector4T, Eigen::aligned_allocator< Vector4T > > &)
 Sets the point set.

Public Attributes

EIGEN_MAKE_ALIGNED_OPERATOR_NEW
typedef T 
value_type
 Internally used value type (should be a floating point type).

Protected Attributes

MatrixXT m_points

Detailed Description

template<class T>
class TRTK::FitSphere< T >

Estimates the parameters of a 3D sphere from a set of points lying on the surface.

Template Parameters:
Tscalar type of the coordinates

This class allows estimating the center point and the radius of a 3D sphere whose surface points are known. The points may be erroneous since a least square fitting is done. If \( (x_i, y_i, z_i)^T \) are the data points, \( (x_0, y_0, z_0)^T \) the center point, and \( r \) the radius, then FitSphere tries to minimize the sum of the squared residuals as shown below:

\[ (x_0, y_0, z_0, r) := \arg\min_{(x_0, y_0, z_0, r)} \sum_{i=1}^N \left[(x_i - x_0)^2 + (y_i - y_0)^2 + (z_i - z_0)^2 - r^2 \right]^2 \]

Here is an more elaborate example to see how to use the class:

 #include <cstdlib>
 #include <iostream>

 #include <TRTK/FitSphere.hpp>
 #include <TRTK/Tools.hpp>

 using namespace std;
 using namespace TRTK;
 using namespace TRTK::Tools;

 int main()
 {
     double pi = 3.1415926535;

     vector<Coordinate<double> > surfacePoints;

     // Construct some points lying on a sphere.

     double radius = 7;
     Coordinate<double> centerPoint = Coordinate<double>(1, 8, 5);

     for (double theta = 0.0; theta < 2.0 * pi; theta += pi/10)
     {
         for (double phi = 0.0; phi < 2.0 * pi; phi += pi/10)
         {
             // Convert the speherical coordinates into cartesian coordinates
             // and add some noise.

             double x = radius * sin(theta) * cos(phi) + 0.1 * randn();
             double y = radius * sin(theta) * sin(phi) + 0.1 * randn();
             double z = radius * cos(theta) + 0.1 * randn();

             Coordinate<double> point = centerPoint + Coordinate<double>(x, y, z);

             surfacePoints.push_back(point);
         }
     }

     // Estimate the parameters of the sphere.

     FitSphere<double> fitSphere(surfacePoints);

     fitSphere.compute();

     cout << "Center point: " << fitSphere.getCenterPoint() << endl;
     cout << "Radius: " << fitSphere.getRadius() << endl;

     return 0;
 }

Output:

 Center point: (0.998629, 8.00457, 5.0021)
 Radius: 7.00053
Author:
Christoph Haenisch
Version:
0.3.0
Date:
last changed on 2012-01-25

Definition at line 126 of file FitSphere.hpp.


Constructor & Destructor Documentation

template<class T >
TRTK::FitSphere< T >::FitSphere (  )

Constructs an empty FitSphere object.

Template Parameters:
Tscalar type of the coordinates

Definition at line 173 of file FitSphere.hpp.

template<class T >
TRTK::FitSphere< T >::FitSphere ( const std::vector< Coordinate< T > > &  points )

Constructs a FitSphere object.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]pointsPoints lying on the surface of a sphere. The points can be 3D or 4D homogeneous coordinates.

Definition at line 189 of file FitSphere.hpp.

template<class T >
TRTK::FitSphere< T >::FitSphere ( const std::vector< Vector3T > &  points )

Constructs a FitSphere object.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]pointsPoints lying on the surface of a sphere.

Definition at line 205 of file FitSphere.hpp.

template<class T >
TRTK::FitSphere< T >::FitSphere ( const std::vector< Vector4T, Eigen::aligned_allocator< Vector4T > > &  points )

Constructs an FitSphere object.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]pointsPoints lying on the surface of a sphere. The points are assumed to be homogeneous coordinates, whose forth coordinate entry is equal to one.

Definition at line 223 of file FitSphere.hpp.

template<class T >
TRTK::FitSphere< T >::~FitSphere (  ) [virtual]

Destructs the FitSphere object.

Template Parameters:
Tscalar type of the coordinates

Definition at line 237 of file FitSphere.hpp.


Member Function Documentation

template<class T >
void TRTK::FitSphere< T >::compute (  ) [virtual]

Runs the fitting algorithm.

Template Parameters:
Tscalar type of the coordinates

There must be at least 4 surface points to perform the fitting.

Exceptions:
ErrorObjIf there are not enough points to fit the sphere, an error object is thrown and its error code is set to WRONG_POINT_SIZE.
See also:
setPoints(), getCenterPoint() and getRadius()

Implements TRTK::Fit3D< T >.

Definition at line 256 of file FitSphere.hpp.

template<class T >
const Coordinate< T > & TRTK::FitSphere< T >::getCenterPoint (  ) const

Returns the center point.

Template Parameters:
Tscalar type of the coordinates

Definition at line 364 of file FitSphere.hpp.

template<class T >
T TRTK::FitSphere< T >::getDistanceTo ( const Coordinate< T > &  point ) const [virtual]

Returns the shortest distance from the given point to the sphere.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]pointA 3D coordinate.

Implements TRTK::Fit3D< T >.

Definition at line 378 of file FitSphere.hpp.

template<class T >
T TRTK::FitSphere< T >::getRadius (  ) const

Returns the radius of the sphere.

Template Parameters:
Tscalar type of the coordinates

Definition at line 400 of file FitSphere.hpp.

template<class T >
T TRTK::FitSphere< T >::getRMS (  ) const [virtual]

Returns the root mean square error.

Template Parameters:
Tscalar type of the coordinates

Implements TRTK::Fit3D< T >.

Definition at line 412 of file FitSphere.hpp.

template<class T >
void TRTK::Fit3D< T >::setPoints ( const std::vector< Vector4T, Eigen::aligned_allocator< Vector4T > > &  points ) [inherited]

Sets the point set.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]points4D homogeneous coordinates.

Definition at line 187 of file Fit3D.hpp.

template<class T >
void TRTK::Fit3D< T >::setPoints ( const std::vector< Coordinate< T > > &  points ) [virtual, inherited]

Sets the point set.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]pointsThe points can either be plain 3D or 4D homogeneous coordinates.
Exceptions:
ErrorObjIf there are any coordinates other than 3D or 4D coordinates, an error object is thrown and its error code is set to WRONG_POINT_SIZE.

Implements TRTK::Fit< T >.

Definition at line 135 of file Fit3D.hpp.

template<class T >
void TRTK::Fit3D< T >::setPoints ( const std::vector< Vector3T > &  points ) [inherited]

Sets the point set.

Template Parameters:
Tscalar type of the coordinates
Parameters:
[in]points3D coordinates.

Definition at line 168 of file Fit3D.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines