Ponca  73247abfe24d29406a95aee1d4dfa2d34da85d4c
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 Scalar = MyPoint::Scalar;
using VectorType = MyPoint::VectorType;
int main(int /*argc*/, char** /*argv*/)
{
constexpr 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.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:318
This Source Code Form is subject to the terms of the Mozilla Public License, v.
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators Add neighbors stored in a container using STL-like iterat...
Definition basket.h:153