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

Merrge branch: clamp_fix #19

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/CameraNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ CameraNode::declareParameters()
// clamp default ControlValue to min/max range and cast ParameterValue
rclcpp::ParameterValue value;
try {
value = cv_to_pv(clamp(info.def(), info.min(), info.max()));
value = cv_to_pv(clamp(info.def(), info.min(), info.max(), extent));
}
catch (const invalid_conversion &e) {
RCLCPP_ERROR_STREAM(get_logger(), "unsupported control '"
Expand Down
37 changes: 24 additions & 13 deletions src/clamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define CASE_CLAMP(T) \
case libcamera::ControlType##T: \
return clamp<ControlTypeMap<libcamera::ControlType##T>::type>(value, min, max);
return clamp<ControlTypeMap<libcamera::ControlType##T>::type>(value, min, max, extent);

#define CASE_NONE(T) \
case libcamera::ControlType##T: \
Expand Down Expand Up @@ -104,16 +104,27 @@ clamp(const CTPoint &val, const CTPoint &lo, const CTPoint &hi)
template<typename T>
libcamera::ControlValue
clamp_array(const libcamera::ControlValue &value, const libcamera::ControlValue &min,
const libcamera::ControlValue &max)
const libcamera::ControlValue &max, size_t extent)
{
const libcamera::Span<const T> v = value.get<libcamera::Span<const T>>();
const libcamera::Span<const T> a = min.get<libcamera::Span<const T>>();
const libcamera::Span<const T> b = max.get<libcamera::Span<const T>>();
std::vector<T> vclamp;

std::vector<T> vclamp(v.size());
if (!value.isArray() && extent != 0) {
if (extent == libcamera::dynamic_extent) {
vclamp.push_back(std::clamp(value.get<const T>(), min.get<const T>(), max.get<const T>()));
}
else {
throw std::runtime_error("cannot clamp scalar value for fixed-extent span control");
}
}
else {
const libcamera::Span<const T> v = value.get<libcamera::Span<const T>>();
const libcamera::Span<const T> a = min.get<libcamera::Span<const T>>();
const libcamera::Span<const T> b = max.get<libcamera::Span<const T>>();

for (size_t i = 0; i < v.size(); i++)
vclamp[i] = std::clamp(v[i], a[i], b[i]);
for (size_t i = 0; i < v.size(); ++i) {
vclamp.push_back(std::clamp(v[i], a[i], b[i]));
}
}

return libcamera::ControlValue(libcamera::Span<const T>(vclamp));
}
Expand All @@ -122,23 +133,23 @@ template<typename T,
std::enable_if_t<!std::is_same<std::remove_cv_t<T>, CTBool>::value, bool> = true>
libcamera::ControlValue
clamp(const libcamera::ControlValue &value, const libcamera::ControlValue &min,
const libcamera::ControlValue &max)
const libcamera::ControlValue &max, size_t extent)
{
return value.isArray() ? clamp_array<T>(value, min, max)
: std::clamp(value.get<T>(), min.get<T>(), max.get<T>());
return (extent != 0) ? clamp_array<T>(value, min, max, extent)
: std::clamp(value.get<T>(), min.get<T>(), max.get<T>());
}

template<typename T, std::enable_if_t<std::is_same<std::remove_cv_t<T>, CTBool>::value, bool> = true>
const libcamera::ControlValue &
clamp(const libcamera::ControlValue &value, const libcamera::ControlValue & /*min*/,
const libcamera::ControlValue & /*max*/)
const libcamera::ControlValue & /*max*/, size_t /*extent*/)
{
return value;
}

libcamera::ControlValue
clamp(const libcamera::ControlValue &value, const libcamera::ControlValue &min,
const libcamera::ControlValue &max)
const libcamera::ControlValue &max, size_t extent)
{
if (min.type() != max.type())
throw invalid_conversion("minimum (" + std::to_string(min.type()) + ") and maximum (" +
Expand Down
2 changes: 1 addition & 1 deletion src/clamp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ max(const libcamera::ControlValue &value);

libcamera::ControlValue
clamp(const libcamera::ControlValue &value, const libcamera::ControlValue &min,
const libcamera::ControlValue &max);
const libcamera::ControlValue &max, size_t extent);

bool
operator<(const libcamera::ControlValue &a, const libcamera::ControlValue &b);
Expand Down