Skip to content

Commit

Permalink
Move allocation out of consensus.
Browse files Browse the repository at this point in the history
  • Loading branch information
gpdaniels committed Jul 17, 2024
1 parent dc2f62e commit 2c3a547
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
31 changes: 11 additions & 20 deletions source/vision/consensus/consensus
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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;
}
};
Expand Down
19 changes: 11 additions & 8 deletions source/vision/consensus/estimator
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(data);
static_cast<void>(data_size);
static_cast<void>(model);
}
virtual void compute_residuals(
const data_type* const __restrict data,
size_t data_size,
const model_type& model,
float* const __restrict residuals
) = 0;
};
}

Expand Down
9 changes: 5 additions & 4 deletions tests/vision/consensus/consensus.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(data_size * 0.01f)));
}

0 comments on commit 2c3a547

Please sign in to comment.