Modernize ParameterBlock::Plus with std::clamp Use std::clamp for projecting the updated parameter state onto its box constraints. This provides a more idiomatic and concise implementation than separate std::min/std::max loops, especially when both lower and upper bounds are present. Change-Id: I1f6248ed8f0313338cf9a1c98d79a023ae281e6c
diff --git a/internal/ceres/parameter_block.h b/internal/ceres/parameter_block.h index 65bb708..a914087 100644 --- a/internal/ceres/parameter_block.h +++ b/internal/ceres/parameter_block.h
@@ -236,13 +236,16 @@ } // Project onto the box constraints. - if (lower_bounds_.get() != nullptr) { + if (lower_bounds_ && upper_bounds_) { + for (int i = 0; i < size_; ++i) { + x_plus_delta[i] = + std::clamp(x_plus_delta[i], lower_bounds_[i], upper_bounds_[i]); + } + } else if (lower_bounds_) { for (int i = 0; i < size_; ++i) { x_plus_delta[i] = std::max(x_plus_delta[i], lower_bounds_[i]); } - } - - if (upper_bounds_.get() != nullptr) { + } else if (upper_bounds_) { for (int i = 0; i < size_; ++i) { x_plus_delta[i] = std::min(x_plus_delta[i], upper_bounds_[i]); }