Skip to main content

Sphere

Namespace: MATHLIB  ·  Library: Math Library

Python equivalent

mne.make_sphere_model in MNE-Python.

#include <math/sphere.h>

class UTILSLIB::Sphere

Best-fit sphere primitive carrying centre and radius, with static factories for the closed-form algebraic fit (Jennings) and the Nelder–Mead geometric-residual refinement.

Used to build spherical head models, fit HPI coils and digitiser scalps.

3-D sphere value type with algebraic and Nelder–Mead best-fit factories.

src/examples/ex_math/main.cpp
MatrixX3f points(6, 3); // six points on a sphere of radius 0.09 around (0, 0, 0.04)
points << 0.09f, 0.0f, 0.04f, -0.09f, 0.0f, 0.04f, 0.0f, 0.09f, 0.04f,
0.0f, -0.09f, 0.04f, 0.0f, 0.0f, 0.13f, 0.0f, 0.0f, -0.05f;
Sphere sphere = Sphere::fit_sphere(points);

Public Methods​

Sphere(center, radius)​

Constructs the Sphere.

Parameters:

  • center : const Eigen::Vector3f & The sphere's center.

  • radius : float The sphere's radius.


center()​

The center of the sphere.

Returns:

  • Eigen::Vector3f & — reference to the sphere's center.

radius()​

The radius of the sphere.

Returns:

  • float & — reference to the sphere's radius.

Static Methods​

fit_sphere(points)​

Fits a sphere to a point cloud.

Algorithm by Alan Jennings, University of Dayton

Parameters:

  • points : const Eigen::MatrixX3f & n x 3 matrix of cartesian data to fit the sphere to.

Returns:

  • Sphere — the fitted sphere.

fit_sphere_simplex(points, simplex_size)​

Fits a sphere to a point cloud.

Parameters:

  • points : const Eigen::MatrixX3f & n x 3 matrix of cartesian data to fit the sphere to.

  • simplex_size : double The simplex size.

Returns:

  • Sphere — the fitted sphere.

fit_sphere_to_points(rr, simplex_size, r0, R)​

Fits a sphere to a point cloud.

Parameters:

  • rr : const Eigen::MatrixXf & n x 3 matrix of cartesian data to fit the sphere to.

  • simplex_size : float The simplex size.

  • r0 : Eigen::VectorXf & center (1 x 3 matrix) of the sphere.

  • R : float & Radius.

Returns:

  • bool — true if successful.

fit_sphere_to_points(rr, np, simplex_size, r0, R)​


Example​

Used in examples​

ex_math​

Math library on inputs with known answers.

src/examples/ex_math/main.cpp, line 134
MatrixX3f points(6, 3); // six points on a sphere of radius 0.09 around (0, 0, 0.04)
points << 0.09f, 0.0f, 0.04f, -0.09f, 0.0f, 0.04f, 0.0f, 0.09f, 0.04f,
0.0f, -0.09f, 0.04f, 0.0f, 0.0f, 0.13f, 0.0f, 0.0f, -0.05f;
Sphere sphere = Sphere::fit_sphere(points);
ok &= expect((sphere.center() - Vector3f(0.0f, 0.0f, 0.04f)).norm() < 1e-5f && std::fabs(sphere.radius() - 0.09f) < 1e-5f,
QString("sphere centre (0, 0, 0.04), radius %1").arg(sphere.radius()));

Authors of this file​