Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support residual evaluation from ceres::Problem #50

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions _pyceres/core/problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@

namespace py = pybind11;

namespace {

// Set residual blocks for Ceres::Problem::EvaluateOptions
void SetResidualBlocks(
ceres::Problem::EvaluateOptions& self,
std::vector<ResidualBlockIDWrapper>& residual_block_ids) {
self.residual_blocks.clear();
self.residual_blocks.reserve(residual_block_ids.size());
for (auto it = residual_block_ids.begin(); it != residual_block_ids.end();
++it) {
self.residual_blocks.push_back(it->id);
}
}

} // namespace

// Function to create Problem::Options with DO_NOT_TAKE_OWNERSHIP
// This is cause we want Python to manage our memory not Ceres
ceres::Problem::Options CreateProblemOptions() {
Expand Down Expand Up @@ -42,15 +58,21 @@ void BindProblem(py::module& m) {

py::class_<ceres::Problem::EvaluateOptions>(m, "EvaluateOptions")
.def(py::init<>())
.def("set_parameter_blocks",
[](ceres::Problem::EvaluateOptions& self,
std::vector<py::array_t<double>>& blocks) {
self.parameter_blocks.clear();
for (auto it = blocks.begin(); it != blocks.end(); ++it) {
py::buffer_info info = it->request();
self.parameter_blocks.push_back(static_cast<double*>(info.ptr));
}
})
.def(
"set_parameter_blocks",
[](ceres::Problem::EvaluateOptions& self,
std::vector<py::array_t<double>>& blocks) {
self.parameter_blocks.clear();
self.parameter_blocks.reserve(blocks.size());
for (auto it = blocks.begin(); it != blocks.end(); ++it) {
py::buffer_info info = it->request();
self.parameter_blocks.push_back(static_cast<double*>(info.ptr));
}
},
py::arg("parameter_blocks"))
.def("set_residual_blocks",
&SetResidualBlocks,
py::arg("residual_block_ids"))
.def_readwrite("apply_loss_function",
&ceres::Problem::EvaluateOptions::apply_loss_function)
.def_readwrite("num_threads",
Expand Down Expand Up @@ -249,6 +271,18 @@ void BindProblem(py::module& m) {
return residuals;
},
py::arg("options") = ceres::Problem::EvaluateOptions())
.def(
"evaluate_residuals",
[](ceres::Problem& self,
std::vector<ResidualBlockIDWrapper>& residual_block_ids) {
ceres::Problem::EvaluateOptions eval_options =
ceres::Problem::EvaluateOptions();
SetResidualBlocks(eval_options, residual_block_ids);
std::vector<double> residuals;
self.Evaluate(eval_options, nullptr, &residuals, nullptr, nullptr);
return residuals;
},
py::arg("residual_block_ids"))
.def(
"evaluate_jacobian",
[](ceres::Problem& self,
Expand Down
Loading