Ponca  f5b8b13495108d95baa74f687c24d962f21272fc
Point Cloud Analysis library
Loading...
Searching...
No Matches
Ponca basic line fit

This is an example of how to use Ponca to fit a Line on random 3D samples.

/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <numeric> // transform_reduce
#include <algorithm>
#include <iostream>
#include <vector>
#include <Ponca/Fitting>
#include <Ponca/src/Common/pointTypes.h>
using namespace Eigen;
using namespace std;
using namespace Ponca;
using MyPoint = PointPosition<double, 3>;
typedef MyPoint::Scalar Scalar;
typedef MyPoint::VectorType VectorType;
int main(int argc, char **argv) {
int n = 10000;
// Generate a random set of n points along a line
vector<MyPoint> points(n);
VectorType dir = VectorType::Random().normalized();
std::generate(points.begin(), points.end(), [&dir]() {
return (dir * Eigen::internal::random<Scalar>(0.1,2)).eval();
});
const VectorType& p = points.at(0).pos();
std::cout << "====================\nLeastSquareLineFit:\n";
cout << "Direction of the generated line: " << dir.transpose() << endl;
// Fit line on data
Fit _fit;
_fit.setNeighborFilter({p, 1});
_fit.compute(points.cbegin(), points.cend());
// Check Fit output
if( _fit.isStable() ) {
cout << "Direction of the fitted 3D line: " << _fit.direction().transpose() << endl;
cout << "Origin of the fitted line: " << _fit.origin().transpose() << endl;
cout << "Projection of the basis center on the fitted line: " << _fit.project(p).transpose() << endl;
Scalar dist = std::transform_reduce(points.cbegin(), points.cend(),
Scalar(0),
std::plus<>(),
[&_fit](const MyPoint &q) {
return (q.pos() - _fit.project(q.pos())).norm();
});
cout << "Mean error between samples and fitted line: " << dist / Scalar(n) << endl;
return EXIT_SUCCESS;
}
cerr << "Fit is not stable: " << _fit.getCurrentState() << endl;
return EXIT_FAILURE;
}
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:321
Weight neighbors according to the euclidean distance between a query and a reference position.
Definition weightFunc.h:127
[PointPositionNormal]
Definition pointTypes.h:46
This Source Code Form is subject to the terms of the Mozilla Public License, v.