From 2c3a547100702afebd803798b9cd936bb80e06e6 Mon Sep 17 00:00:00 2001 From: Geoffrey Daniels Date: Wed, 17 Jul 2024 21:11:01 +0100 Subject: [PATCH] Move allocation out of consensus. --- source/vision/consensus/consensus | 31 ++++++++--------------- source/vision/consensus/estimator | 19 ++++++++------ tests/vision/consensus/consensus.test.cpp | 9 ++++--- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/source/vision/consensus/consensus b/source/vision/consensus/consensus index 44bbc85..c94af87 100644 --- a/source/vision/consensus/consensus +++ b/source/vision/consensus/consensus @@ -69,9 +69,9 @@ namespace gtl { public: ~consensus() = default; consensus( - sampler_type& sampler, - estimator_type& estimator, - evaluator_type& evaluator, + const sampler_type& sampler, + const estimator_type& estimator, + const evaluator_type& evaluator, const float inlier_ratio, const float probability_failure, const size_t iterations_minimum, @@ -152,12 +152,17 @@ namespace gtl { } public: - bool estimate(const data_type* const __restrict data, size_t data_size, model_type& best_model) { + bool estimate( + const data_type* const __restrict data, + const size_t data_size, + float* const __restrict residuals, + size_t* const __restrict inliers, + size_t& inliers_size, + model_type& best_model + ) { best_model = {}; bool solution_found = false; float best_cost = 0; - float* residuals = new float[data_size]; - size_t* inliers = new size_t[data_size]; this->consensus_sampler.prepare(data_size); for (size_t i = 0; i < this->criteria_iterations_maximum; ++i) { size_t subset_indices[sample_size]; @@ -171,7 +176,6 @@ namespace gtl { for (size_t j = 0; j < model_count; ++j) { const model_type& model = models[j]; this->consensus_estimator.compute_residuals(data, data_size, model, residuals); - size_t inliers_size = 0; const float cost = this->consensus_evaluator.evaluate(residuals, data_size, inliers, inliers_size); if ((cost < best_cost) || (solution_found == false)) { solution_found = true; @@ -186,24 +190,11 @@ namespace gtl { } } } - delete[] residuals; - delete[] inliers; if (!solution_found) { return false; } - this->consensus_estimator.refine_model(data, data_size, best_model); - return true; - } - - bool estimate(const data_type* const __restrict data, size_t data_size, model_type& best_model, size_t* const __restrict inliers, size_t& inliers_size) { - bool valid = this->estimate(data, data_size, best_model); - if (!valid) { - return false; - } - float* residuals = new float[data_size]; this->consensus_estimator.compute_residuals(data, data_size, best_model, residuals); this->consensus_evaluator.evaluate(residuals, data_size, inliers, inliers_size); - delete[] residuals; return true; } }; diff --git a/source/vision/consensus/estimator b/source/vision/consensus/estimator index 2655caa..2ad4c8e 100644 --- a/source/vision/consensus/estimator +++ b/source/vision/consensus/estimator @@ -47,15 +47,18 @@ namespace gtl { estimator& operator=(estimator&&) = default; public: - virtual size_t generate_models(const data_type* const __restrict data, size_t data_size, model_type* const __restrict models) = 0; + virtual size_t generate_models( + const data_type* const __restrict data, + size_t data_size, + model_type* const __restrict models + ) = 0; - virtual void compute_residuals(const data_type* const __restrict data, size_t data_size, const model_type& model, float* const __restrict residuals) = 0; - - virtual void refine_model(const data_type* const __restrict data, size_t data_size, model_type& model) { - static_cast(data); - static_cast(data_size); - static_cast(model); - } + virtual void compute_residuals( + const data_type* const __restrict data, + size_t data_size, + const model_type& model, + float* const __restrict residuals + ) = 0; }; } diff --git a/tests/vision/consensus/consensus.test.cpp b/tests/vision/consensus/consensus.test.cpp index cb29e27..9b9716d 100644 --- a/tests/vision/consensus/consensus.test.cpp +++ b/tests/vision/consensus/consensus.test.cpp @@ -125,12 +125,13 @@ TEST(consensus, evaluate, line) { iterations_maximum ); - line best; + float residuals[data_size]; std::size_t inliers[data_size]; std::size_t inliers_size = 0; - REQUIRE(consensus.estimate(data, data_size, best, inliers, inliers_size)); + line best_model; + REQUIRE(consensus.estimate(data, data_size, residuals, inliers, inliers_size, best_model)); - REQUIRE(testbench::is_value_approx(best.gradient, gradient, 0.05f)); - REQUIRE(testbench::is_value_approx(best.intercept, intercept, 0.05f)); + REQUIRE(testbench::is_value_approx(best_model.gradient, gradient, 0.05f)); + REQUIRE(testbench::is_value_approx(best_model.intercept, intercept, 0.05f)); REQUIRE(testbench::is_value_approx(inliers_size, data_size / 2, static_cast(data_size * 0.01f))); }