ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
polynomial.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 
12 namespace colmap {
13 
14 // All polynomials are assumed to be the form:
15 //
16 // sum_{i=0}^N polynomial(i) x^{N-i}.
17 //
18 // and are given by a vector of coefficients of size N + 1.
19 //
20 // The implementation is based on COLMAP's old polynomial functionality and is
21 // inspired by Ceres-Solver's/Theia's implementation to support complex
22 // polynomials. The companion matrix implementation is based on NumPy.
23 
24 // Evaluate the polynomial for the given coefficients at x using the Horner
25 // scheme. This function is templated such that the polynomial may be evaluated
26 // at real and/or imaginary points.
27 template <typename T>
28 T EvaluatePolynomial(const Eigen::VectorXd& coeffs, const T& x);
29 
30 // Find the root of polynomials of the form: a * x + b = 0.
31 // The real and/or imaginary variable may be NULL if the output is not needed.
32 bool FindLinearPolynomialRoots(const Eigen::VectorXd& coeffs,
33  Eigen::VectorXd* real,
34  Eigen::VectorXd* imag);
35 
36 // Find the roots of polynomials of the form: a * x^2 + b * x + c = 0.
37 // The real and/or imaginary variable may be NULL if the output is not needed.
38 bool FindQuadraticPolynomialRoots(const Eigen::VectorXd& coeffs,
39  Eigen::VectorXd* real,
40  Eigen::VectorXd* imag);
41 
42 // Find the roots of a polynomial using the Durand-Kerner method, based on:
43 //
44 // https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method
45 //
46 // The Durand-Kerner is comparatively fast but often unstable/inaccurate.
47 // The real and/or imaginary variable may be NULL if the output is not needed.
48 bool FindPolynomialRootsDurandKerner(const Eigen::VectorXd& coeffs,
49  Eigen::VectorXd* real,
50  Eigen::VectorXd* imag);
51 
52 // Find the roots of a polynomial using the companion matrix method, based on:
53 //
54 // R. A. Horn & C. R. Johnson, Matrix Analysis. Cambridge,
55 // UK: Cambridge University Press, 1999, pp. 146-7.
56 //
57 // Compared to Durand-Kerner, this method is slower but more stable/accurate.
58 // The real and/or imaginary variable may be NULL if the output is not needed.
59 bool FindPolynomialRootsCompanionMatrix(const Eigen::VectorXd& coeffs,
60  Eigen::VectorXd* real,
61  Eigen::VectorXd* imag);
62 
64 // Implementation
66 
67 template <typename T>
68 T EvaluatePolynomial(const Eigen::VectorXd& coeffs, const T& x) {
69  T value = 0.0;
70  for (Eigen::VectorXd::Index i = 0; i < coeffs.size(); ++i) {
71  value = value * x + coeffs(i);
72  }
73  return value;
74 }
75 
76 } // namespace colmap
colmap::Point3D::Track
const class Track & Track() const
Definition: point3d.h:106
colmap::Track::Length
size_t Length() const
Definition: track.h:82
x
normal_z x
Definition: my_point_types.h:165
colmap::Point3D::Color
const Eigen::Vector3ub & Color() const
Definition: point3d.h:90
colmap::Point3D::SetColor
void SetColor(const Eigen::Vector3ub &color)
Definition: point3d.h:98
polynomial.h
colmap::Point3D::Y
double Y() const
Definition: point3d.h:84
colmap::FindPolynomialRootsCompanionMatrix
bool FindPolynomialRootsCompanionMatrix(const Eigen::VectorXd &coeffs_all, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:208
testing.h
colmap::Point3D::Z
double Z() const
Definition: point3d.h:86
colmap::FindPolynomialRootsDurandKerner
bool FindPolynomialRootsDurandKerner(const Eigen::VectorXd &coeffs_all, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:143
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestDefault)
Definition: point3d_test.cc:39
colmap::Point3D::Error
double Error() const
Definition: point3d.h:100
e
const double * e
Definition: essential_matrix_poly.h:10
Eigen::Vector3ub
Matrix< uint8_t, 3, 1 > Vector3ub
Definition: types.h:42
knncpp::Index
Eigen::MatrixXd::Index Index
Definition: knncpp.h:26
coeffs
coeffs(0)
colmap::Point3D::SetTrack
void SetTrack(const class Track &track)
Definition: point3d.h:110
a
a[190]
Definition: essential_matrix_poly.h:19
colmap::Point3D::HasError
bool HasError() const
Definition: point3d.h:102
colmap::Point3D::SetXYZ
void SetXYZ(const Eigen::Vector3d &xyz)
Definition: point3d.h:88
colmap::FindLinearPolynomialRoots
bool FindLinearPolynomialRoots(const Eigen::VectorXd &coeffs, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:65
colmap::FindQuadraticPolynomialRoots
bool FindQuadraticPolynomialRoots(const Eigen::VectorXd &coeffs, Eigen::VectorXd *real, Eigen::VectorXd *imag)
Definition: polynomial.cc:86
colmap::Point3D::XYZ
const Eigen::Vector3d & XYZ() const
Definition: point3d.h:74
colmap::Track
Definition: track.h:30
colmap::Point3D::SetError
void SetError(const double error)
Definition: point3d.h:104
logging.h
colmap::Track::AddElement
void AddElement(const TrackElement &element)
Definition: track.h:103
colmap
Definition: AutomaticReconstructionController.h:17
point3d.h
colmap::Point3D::X
double X() const
Definition: point3d.h:82
colmap::EvaluatePolynomial
T EvaluatePolynomial(const Eigen::VectorXd &coeffs, const T &x)
Definition: polynomial.h:68
colmap::Point3D
Definition: point3d.h:23