11#ifndef EASY3D_CORE_PRINCIPAL_AXES_H
12#define EASY3D_CORE_PRINCIPAL_AXES_H
14#include <easy3d/core/types.h>
29 template <
int DIM,
typename FT =
double>
39 template<
typename FT2>
45 template <
typename InputIterator>
46 void add(InputIterator first, InputIterator last);
53 template<
typename FT2>
62 template<
typename FT2>
83#include <easy3d/core/eigen_solver.h>
89 template <
int DIM,
typename FT>
90 PrincipalAxes<DIM, FT>::PrincipalAxes() {
92 for (
int i = 0; i < DIM; ++i)
97 template <
int DIM,
typename FT>
98 PrincipalAxes<DIM, FT>::~PrincipalAxes() {
99 for (
int i = 0; i < DIM; ++i)
105 template <
int DIM,
typename FT>
106 template <
typename FT2>
112 template <
int DIM,
typename FT>
113 template <
typename FT2>
115 assert(i >= 0 && i < DIM) ;
120 template <
int DIM,
typename FT>
122 assert(i >= 0 && i < DIM) ;
123 return eigen_value_[i] ;
127 template <
int DIM,
typename FT>
131 for (
int i = 0; i < DIM; ++i) {
133 for (
int j = 0; j < DIM; ++j)
139 template <
int DIM,
typename FT>
141 for (
int i = 0; i < DIM; ++i)
142 center_[i] /= sum_weights_;
145 if (nb_points_ < DIM + 1) {
146 for (
int i = 0; i < DIM; ++i) {
147 eigen_value_[i] = FT(1);
148 for (
int j = 0; j < DIM; ++j)
149 axis_[i][j] = (i == j ? FT(1) : FT(0));
153 for (
int i = 0; i < DIM; ++i) {
154 for (
int j = i; j < DIM; ++j) {
155 M_[i][j] = M_[i][j]/sum_weights_ - center_[i]*center_[j];
160 if( M_[i][i] <= FT(0))
161 M_[i][i] = std::numeric_limits<FT>::min();
167 for (
int i=0; i<DIM; ++i) {
169 for (
int j=0; j<DIM; ++j)
174 for(
int i=0; i<DIM; i++) {
176 for(
int j=0; j<DIM; j++)
177 sqr_len += (axis_[i][j] * axis_[i][j]);
178 FT s = std::sqrt(sqr_len);
179 s = (s > std::numeric_limits<FT>::min()) ? FT(1) / s : FT(0);
180 for (
int j = 0; j < DIM; ++j)
190 template <
int DIM,
typename FT>
191 template <
typename FT2>
193 for (
int i = 0; i < DIM; ++i) {
194 center_[i] += p[i] * weight ;
195 for (
int j = i; j < DIM; ++j)
196 M_[i][j] += weight * p[i] * p[j];
199 sum_weights_ += weight ;
204 template <
int DIM,
typename FT>
205 template <
typename InputIterator >
207 assert(first != last);
208 for (InputIterator it = first; it != last; ++it) {
An easy-to-use eigen solver.
Definition: eigen_solver.h:25
FT eigen_value(int i) const
Returns the i_th eigenvalue.
Definition: eigen_solver.h:41
void solve(FT **mat, SortingMethod sm=NO_SORTING)
Computes the eigenvalues and eigenvectors of the input matrix mat.
Definition: eigen_solver.h:566
FT eigen_vector(int comp, int i) const
Returns the comp_th component of the i_th eigenvector.
Definition: eigen_solver.h:43
Computes the center and inertia axes of a set of 2D or 3D points.
Definition: principal_axes.h:30
FT eigen_value(int i) const
The i_th eigenvalue.
Definition: principal_axes.h:121
Vec< DIM, FT2 > center() const
The weighted average of the points. This function supports different types of vectors,...
Definition: principal_axes.h:107
void add(const Vec< DIM, FT2 > &p, FT2 weight=FT2(1))
Adds a point p with a weight. This function supports different types of vectors, e....
Definition: principal_axes.h:192
void end()
Ends adding points.
Definition: principal_axes.h:140
void begin()
Begins adding points.
Definition: principal_axes.h:128
Vec< DIM, FT2 > axis(int i) const
The i_th axis This function supports different types of vectors, e.g. vec3, dvec3,...
Definition: principal_axes.h:114
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
Definition: collider.cpp:182