11#ifndef EASY3D_CORE_PRINCIPAL_AXES_H
12#define EASY3D_CORE_PRINCIPAL_AXES_H
14#include <easy3d/core/types.h>
28 template <
int DIM,
typename FT =
double>
52 template<
typename FT2>
66 template <
typename InputIterator>
67 void add(InputIterator first, InputIterator last);
78 template<
typename FT2>
92 template<
typename FT2>
106 FT eigen_value_[DIM];
117#include <easy3d/core/eigen_solver.h>
123 template <
int DIM,
typename FT>
125 for (
int i = 0; i < DIM; ++i) {
128 for (
int j = 0; j < DIM; ++j) {
136 template <
int DIM,
typename FT>
137 template <
typename FT2>
143 template <
int DIM,
typename FT>
144 template <
typename FT2>
146 assert(i >= 0 && i < DIM) ;
151 template <
int DIM,
typename FT>
153 assert(i >= 0 && i < DIM) ;
154 return eigen_value_[i] ;
158 template <
int DIM,
typename FT>
162 for (
int i = 0; i < DIM; ++i) {
164 for (
int j = 0; j < DIM; ++j)
170 template <
int DIM,
typename FT>
172 for (
int i = 0; i < DIM; ++i)
173 center_[i] /= sum_weights_;
176 if (nb_points_ < DIM + 1) {
177 for (
int i = 0; i < DIM; ++i) {
178 eigen_value_[i] = FT(1);
179 for (
int j = 0; j < DIM; ++j)
180 axis_[i][j] = (i == j ? FT(1) : FT(0));
184 for (
int i = 0; i < DIM; ++i) {
185 for (
int j = i; j < DIM; ++j) {
186 M_(i, j) = M_(i, j)/sum_weights_ - center_[i]*center_[j];
191 if( M_(i, i) <= FT(0))
192 M_(i, i) = std::numeric_limits<FT>::min();
198 for (
int i=0; i<DIM; ++i) {
200 for (
int j=0; j<DIM; ++j)
205 for(
int i=0; i<DIM; i++) {
207 for(
int j=0; j<DIM; j++)
208 sqr_len += (axis_[i][j] * axis_[i][j]);
209 FT s = std::sqrt(sqr_len);
210 s = (s > std::numeric_limits<FT>::min()) ? FT(1) / s : FT(0);
211 for (
int j = 0; j < DIM; ++j)
221 template <
int DIM,
typename FT>
222 template <
typename FT2>
224 for (
int i = 0; i < DIM; ++i) {
225 center_[i] += p[i] * weight ;
226 for (
int j = i; j < DIM; ++j)
227 M_(i, j) += weight * p[i] * p[j];
230 sum_weights_ += weight ;
235 template <
int DIM,
typename FT>
236 template <
typename InputIterator >
238 assert(first != last);
239 for (InputIterator it = first; it != last; ++it) {
An easy-to-use eigen solver.
Definition eigen_solver.h:30
T eigen_value(int i) const
Retrieves the eigenvalue at the specified index.
Definition eigen_solver.h:65
void solve(const MAT &mat, SortingMethod sm=NO_SORTING)
Computes the eigenvalues and eigenvectors of the specified input matrix.
Definition eigen_solver.h:620
T eigen_vector(int comp, int i) const
Retrieves the specified component of the eigenvector at the given index.
Definition eigen_solver.h:72
Base class for matrix types.
Definition mat.h:64
~PrincipalAxes()=default
Destructor.
FT eigen_value(int i) const
The i_th eigenvalue.
Definition principal_axes.h:152
Vec< DIM, FT2 > center() const
The weighted average of the points.
Definition principal_axes.h:138
void add(const Vec< DIM, FT2 > &p, FT2 weight=FT2(1))
Adds a point p with a weight.
Definition principal_axes.h:223
void end()
Ends adding points.
Definition principal_axes.h:171
void begin()
Begins adding points.
Definition principal_axes.h:159
Vec< DIM, FT2 > axis(int i) const
The i_th axis.
Definition principal_axes.h:145
PrincipalAxes()
Default constructor.
Definition principal_axes.h:124
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Definition collider.cpp:182