Skip to content

Commit

Permalink
Add the ability to use callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
sudomakeinstall committed Dec 1, 2017
1 parent ef36583 commit 35caea6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ target_link_libraries(cpd-version PRIVATE Cpd::Library-C++)
add_executable(cpd-random random.cpp)
target_link_libraries(cpd-random PRIVATE Cpd::Library-C++)

add_executable(cpd-callback callback.cpp)
target_link_libraries(cpd-callback PRIVATE Cpd::Library-C++)

add_executable(cpd-transform transform.cpp)
target_link_libraries(cpd-transform PRIVATE Cpd::Library-C++)
45 changes: 45 additions & 0 deletions examples/callback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <string>

#include <cpd/nonrigid.hpp>
#include <cpd/rigid.hpp>

void RigidCallback(const cpd::Result &r) {
std::cout << r.points << std::endl << std::endl;
}

void NonrigidCallback(const cpd::NonrigidResult &r) {
std::cout << r.points << std::endl << std::endl;
}

int main(int argc, char** argv) {
if (argc != 4) {
std::cout << "Invalid usage: cpd-random <method> <rows> <cols>"
<< std::endl;
return 1;
}
std::string method = argv[1];
size_t rows = std::stoi(argv[2]);
size_t cols = std::stoi(argv[3]);
cpd::Matrix fixed = cpd::Matrix::Random(rows, cols);
cpd::Matrix moving = cpd::Matrix::Random(rows, cols);

if (method == "rigid") {
cpd::Rigid rigid;
auto *cb = RigidCallback;
rigid.add_callback(cb);
auto rigid_result = rigid.run(fixed, moving);
} else if (method == "nonrigid") {
cpd::Nonrigid nonrigid;
auto *cb = NonrigidCallback;
nonrigid.add_callback(cb);
auto nonrigid_result = nonrigid.run(fixed, moving);
} else {
std::cout << "Invalid method: " << method << std::endl;
return 1;
}
std::cout << "Registration completed OK" << std::endl;
return 0;
}


18 changes: 17 additions & 1 deletion include/cpd/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
#pragma once

#include <chrono>
#include <memory>
#include <functional>
#include <vector>

#include <cpd/gauss_transform.hpp>
#include <cpd/matrix.hpp>
#include <cpd/normalization.hpp>
#include <cpd/utils.hpp>
#include <memory>

namespace cpd {

Expand Down Expand Up @@ -83,6 +86,15 @@ class Transform {
, m_sigma2(DEFAULT_SIGMA2)
, m_tolerance(DEFAULT_TOLERANCE) {}

using TCallback = std::function<void(const Result&)>;
using TCallbackVector = std::vector<TCallback>;

/// Add a callback (function pointer, member function, or lambda).
Transform& add_callback(TCallback cb) {
this->m_callbacks.push_back(cb);
return *this;
}

/// Sets whether the correspondence vector will be computed.
Transform& correspondence(bool correspondence) {
m_correspondence = correspondence;
Expand Down Expand Up @@ -161,6 +173,9 @@ class Transform {

result =
this->compute_one(fixed, moving, probabilities, result.sigma2);
for (const auto &cb : this->m_callbacks) {
cb(result);
}
++iter;
}

Expand Down Expand Up @@ -211,5 +226,6 @@ class Transform {
double m_outliers;
double m_sigma2;
double m_tolerance;
TCallbackVector m_callbacks;
};
} // namespace cpd

0 comments on commit 35caea6

Please sign in to comment.