Improve slider precision and range clamping, fixes #3206
Add clamping to slider values in both ImGui widgets and mjUI to ensure they remain within the defined min/max ranges, preventing potential floating point inaccuracies from causing values to slightly exceed the bounds. PiperOrigin-RevId: 902660458 Change-Id: Ia153ae7907f07b890f2b5ac2e3a4be93ecae7bd3
This commit is contained in:
committed by
Copybara-Service
parent
a8a5afc8dc
commit
b2281883dd
@@ -1192,7 +1192,7 @@ void MakeJointSection(mj::Simulate* sim) {
|
|||||||
|
|
||||||
// set range
|
// set range
|
||||||
if (sim->jnt_range_[i].has_value())
|
if (sim->jnt_range_[i].has_value())
|
||||||
mju::sprintf_arr(defSlider[0].other, "%.4g %.4g",
|
mju::sprintf_arr(defSlider[0].other, "%.17g %.17g",
|
||||||
sim->jnt_range_[i]->first, sim->jnt_range_[i]->second);
|
sim->jnt_range_[i]->first, sim->jnt_range_[i]->second);
|
||||||
else if (sim->jnt_type_[i]==mjJNT_SLIDE) {
|
else if (sim->jnt_type_[i]==mjJNT_SLIDE) {
|
||||||
mju::strcpy_arr(defSlider[0].other, "-1 1");
|
mju::strcpy_arr(defSlider[0].other, "-1 1");
|
||||||
@@ -1251,7 +1251,7 @@ void MakeControlSection(mj::Simulate* sim) {
|
|||||||
|
|
||||||
// set range
|
// set range
|
||||||
if (sim->actuator_ctrlrange_[i].has_value())
|
if (sim->actuator_ctrlrange_[i].has_value())
|
||||||
mju::sprintf_arr(defSlider[0].other, "%.4g %.4g",
|
mju::sprintf_arr(defSlider[0].other, "%.17g %.17g",
|
||||||
sim->actuator_ctrlrange_[i]->first, sim->actuator_ctrlrange_[i]->second);
|
sim->actuator_ctrlrange_[i]->first, sim->actuator_ctrlrange_[i]->second);
|
||||||
else {
|
else {
|
||||||
mju::strcpy_arr(defSlider[0].other, "-1 1");
|
mju::strcpy_arr(defSlider[0].other, "-1 1");
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ bool ImGui_Slider(const char* name, mjtNum* value, mjtNum min, mjtNum max) {
|
|||||||
float f = *value;
|
float f = *value;
|
||||||
const bool res = ImGui::SliderFloat(name, &f, min, max);
|
const bool res = ImGui::SliderFloat(name, &f, min, max);
|
||||||
if (res) {
|
if (res) {
|
||||||
*value = f;
|
*value = mju_clip(f, min, max);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -849,8 +849,9 @@ static void setslider(mjuiItem* it, mjUI* ui,
|
|||||||
rx = mju_round(rx * it->slider.divisions) / mjMAX(1, it->slider.divisions);
|
rx = mju_round(rx * it->slider.divisions) / mjMAX(1, it->slider.divisions);
|
||||||
rx = mjMAX(0, mjMIN(1, rx));
|
rx = mjMAX(0, mjMIN(1, rx));
|
||||||
|
|
||||||
// compute value
|
// compute value, clamp to range
|
||||||
mjtNum val = (mjtNum)(it->slider.range[0]*(1-rx) + it->slider.range[1]*rx);
|
mjtNum val = mju_clip(it->slider.range[0]*(1-rx) + it->slider.range[1]*rx,
|
||||||
|
it->slider.range[0], it->slider.range[1]);
|
||||||
|
|
||||||
// set slider position
|
// set slider position
|
||||||
if (it->type == mjITEM_SLIDERINT) {
|
if (it->type == mjITEM_SLIDERINT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user