From 7b546bbda2961c2cd57998c563906547a1a7153d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 26 Aug 2024 11:22:56 +0200 Subject: [PATCH 1/2] Add callbacks by move --- src/colmap/util/base_controller.cc | 9 ++++----- src/colmap/util/base_controller.h | 4 ++-- src/colmap/util/threading.cc | 4 ++-- src/colmap/util/threading.h | 2 +- src/pycolmap/pipeline/sfm.cc | 23 ++++++++++++----------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/colmap/util/base_controller.cc b/src/colmap/util/base_controller.cc index a16046d16b..5e794e3912 100644 --- a/src/colmap/util/base_controller.cc +++ b/src/colmap/util/base_controller.cc @@ -35,11 +35,10 @@ namespace colmap { BaseController::BaseController() {} -void BaseController::AddCallback(const int id, - const std::function& func) { +void BaseController::AddCallback(const int id, std::function func) { CHECK(func); CHECK_GT(callbacks_.count(id), 0) << "Callback not registered"; - callbacks_.at(id).push_back(func); + callbacks_.at(id).push_back(std::move(func)); } void BaseController::RegisterCallback(const int id) { @@ -53,8 +52,8 @@ void BaseController::Callback(const int id) const { } } -void BaseController::SetCheckIfStoppedFunc(const std::function& func) { - check_if_stopped_fn_ = func; +void BaseController::SetCheckIfStoppedFunc(std::function func) { + check_if_stopped_fn_ = std::move(func); } bool BaseController::CheckIfStopped() { diff --git a/src/colmap/util/base_controller.h b/src/colmap/util/base_controller.h index 98d62ec81f..34c20a9b47 100644 --- a/src/colmap/util/base_controller.h +++ b/src/colmap/util/base_controller.h @@ -45,7 +45,7 @@ class BaseController { virtual ~BaseController() = default; // Set callbacks that can be triggered within the main run function. - void AddCallback(int id, const std::function& func); + void AddCallback(int id, std::function func); // Call back to the function with the specified name, if it exists. void Callback(int id) const; @@ -57,7 +57,7 @@ class BaseController { virtual void Run() = 0; // check if the thread is stopped - void SetCheckIfStoppedFunc(const std::function& func); + void SetCheckIfStoppedFunc(std::function func); bool CheckIfStopped(); protected: diff --git a/src/colmap/util/threading.cc b/src/colmap/util/threading.cc index 645a982238..dc95028c92 100644 --- a/src/colmap/util/threading.cc +++ b/src/colmap/util/threading.cc @@ -112,10 +112,10 @@ bool Thread::IsFinished() { return finished_; } -void Thread::AddCallback(const int id, const std::function& func) { +void Thread::AddCallback(const int id, std::function func) { THROW_CHECK(func); THROW_CHECK_GT(callbacks_.count(id), 0) << "Callback not registered"; - callbacks_.at(id).push_back(func); + callbacks_.at(id).push_back(std::move(func)); } void Thread::RegisterCallback(const int id) { diff --git a/src/colmap/util/threading.h b/src/colmap/util/threading.h index 50cd1e2093..0ff18e8599 100644 --- a/src/colmap/util/threading.h +++ b/src/colmap/util/threading.h @@ -130,7 +130,7 @@ class Thread { bool CheckValidSetup(); // Set callbacks that can be triggered within the main run function. - void AddCallback(int id, const std::function& func); + void AddCallback(int id, std::function func); // Get timing information of the thread, properly accounting for pause times. const Timer& GetTimer() const; diff --git a/src/pycolmap/pipeline/sfm.cc b/src/pycolmap/pipeline/sfm.cc index 940a257b92..b4716abea7 100644 --- a/src/pycolmap/pipeline/sfm.cc +++ b/src/pycolmap/pipeline/sfm.cc @@ -48,8 +48,8 @@ std::map> IncrementalMapping( const std::string& output_path, const IncrementalPipelineOptions& options, const std::string& input_path, - const std::function& initial_image_pair_callback, - const std::function& next_image_callback) { + std::function initial_image_pair_callback, + std::function next_image_callback) { THROW_CHECK_FILE_EXISTS(database_path); THROW_CHECK_DIR_EXISTS(image_path); CreateDirIfNotExists(output_path); @@ -64,17 +64,18 @@ std::map> IncrementalMapping( options_, image_path, database_path, reconstruction_manager); PyInterrupt py_interrupt(1.0); // Check for interrupts every second - mapper.AddCallback(IncrementalPipeline::NEXT_IMAGE_REG_CALLBACK, [&]() { - if (py_interrupt.Raised()) { - throw py::error_already_set(); - } - if (next_image_callback) { - next_image_callback(); - } - }); + mapper.AddCallback(IncrementalPipeline::NEXT_IMAGE_REG_CALLBACK, + [next_image_callback = std::move(next_image_callback)]() { + if (py_interrupt.Raised()) { + throw py::error_already_set(); + } + if (next_image_callback) { + next_image_callback(); + } + }); if (initial_image_pair_callback) { mapper.AddCallback(IncrementalPipeline::INITIAL_IMAGE_PAIR_REG_CALLBACK, - initial_image_pair_callback); + std::move(initial_image_pair_callback)); } mapper.Run(); From 42ee2433ca54a46a6c41fe1f89650e6358c22bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 26 Aug 2024 13:05:27 +0200 Subject: [PATCH 2/2] d --- src/pycolmap/pipeline/sfm.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/pycolmap/pipeline/sfm.cc b/src/pycolmap/pipeline/sfm.cc index b4716abea7..e896ee0cf1 100644 --- a/src/pycolmap/pipeline/sfm.cc +++ b/src/pycolmap/pipeline/sfm.cc @@ -64,15 +64,16 @@ std::map> IncrementalMapping( options_, image_path, database_path, reconstruction_manager); PyInterrupt py_interrupt(1.0); // Check for interrupts every second - mapper.AddCallback(IncrementalPipeline::NEXT_IMAGE_REG_CALLBACK, - [next_image_callback = std::move(next_image_callback)]() { - if (py_interrupt.Raised()) { - throw py::error_already_set(); - } - if (next_image_callback) { - next_image_callback(); - } - }); + mapper.AddCallback( + IncrementalPipeline::NEXT_IMAGE_REG_CALLBACK, + [&py_interrupt, next_image_callback = std::move(next_image_callback)]() { + if (py_interrupt.Raised()) { + throw py::error_already_set(); + } + if (next_image_callback) { + next_image_callback(); + } + }); if (initial_image_pair_callback) { mapper.AddCallback(IncrementalPipeline::INITIAL_IMAGE_PAIR_REG_CALLBACK, std::move(initial_image_pair_callback));