From ea4de24c2e30c5c2e48dac438866f10bd907979d Mon Sep 17 00:00:00 2001 From: Sameer Agarwal Date: Mon, 23 Mar 2026 11:48:42 -0700 Subject: [PATCH] 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 --- internal/ceres/parameter_block.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/ceres/parameter_block.h b/internal/ceres/parameter_block.h index 65bb708b3..a91408720 100644 --- a/internal/ceres/parameter_block.h +++ b/internal/ceres/parameter_block.h @@ -236,13 +236,16 @@ class CERES_NO_EXPORT ParameterBlock { } // 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]); }