Skip to content

Commit

Permalink
corrections according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
remibettan committed Jun 8, 2021
1 parent c806c11 commit ac36bf2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
94 changes: 57 additions & 37 deletions common/model-views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,12 @@ namespace rs2
}
return option;
}
std::string option_model::get_option_description(rs2_option option, bool display_in_cm) const
std::string adjust_description(const std::string& str_in, const std::string& to_be_replaced, const std::string& to_replace)
{
std::string desc (endpoint->get_option_description(option));
std::string meters_str("meters");
if (display_in_cm)
{
auto pos = desc.find(meters_str);
desc.replace(pos, meters_str.size(), "cm");
}
return desc;
std::string adjusted_string(str_in);
auto pos = adjusted_string.find(to_be_replaced);
adjusted_string.replace(pos, to_be_replaced.size(), to_replace);
return adjusted_string;
}

bool option_model::draw(std::string& error_message, notifications_model& model, bool new_line, bool use_option_name)
Expand All @@ -571,12 +567,25 @@ namespace rs2
if (opt == RS2_OPTION_HOLES_FILL)
use_option_name = false;

bool should_be_displayed_in_cm = false;
// lambda function used to convert meters to cm - while the number is a string
auto convert_float_str = [](std::string float_str, float conversion_factor) {
if (float_str.size() == 0)
return float_str;
float number_float = std::stof(float_str);
return std::to_string(number_float * conversion_factor);
};

std::string desc_str (endpoint->get_option_description(opt));

// Device D405 is for short range, therefore, its units are in cm - for better UX
bool use_cm_units = false;
std::string device_pid = dev->s->get_info(RS2_CAMERA_INFO_PRODUCT_ID);
if (device_pid == "0B5B" && (opt == RS2_OPTION_MIN_DISTANCE || opt == RS2_OPTION_MAX_DISTANCE || opt == RS2_OPTION_DEPTH_UNITS))
should_be_displayed_in_cm = true;
if (device_pid == "0B5B" && val_in_range(opt, { RS2_OPTION_MIN_DISTANCE, RS2_OPTION_MAX_DISTANCE, RS2_OPTION_DEPTH_UNITS }))
{
use_cm_units = true;
desc_str = adjust_description(desc_str, "meters", "cm");
}

auto desc_str = get_option_description(opt, should_be_displayed_in_cm);
auto desc = desc_str.c_str();

// remain option to append to the current line
Expand Down Expand Up @@ -692,29 +701,24 @@ namespace rs2
}
else if (edit_mode)
{
std::string buff_str = edit_value;

// when cm must be used instead of meters
if (use_cm_units)
buff_str = convert_float_str(buff_str, 100.f);

char buff[TEXT_BUFF_SIZE];
memset(buff, 0, TEXT_BUFF_SIZE);
strcpy(buff, buff_str.c_str());

if (should_be_displayed_in_cm)
{
float edit_value_float = std::stof(edit_value);
edit_value_float *= 100.f;
std::string edit_value_cm(std::to_string(edit_value_float));
strcpy(buff, edit_value_cm.c_str());
}
else
strcpy(buff, edit_value.c_str());
if (ImGui::InputText(id.c_str(), buff, TEXT_BUFF_SIZE,
ImGuiInputTextFlags_EnterReturnsTrue))
{
if (should_be_displayed_in_cm)
if (use_cm_units)
{
std::string buff_str(buff);
float buff_float = std::stof(buff_str);
buff_float /= 100.f;
std::string buff_str_cm(std::to_string(buff_float));
buff_str = convert_float_str(std::string(buff), 0.01f);
memset(buff, 0, TEXT_BUFF_SIZE);
strcpy(buff, buff_str_cm.c_str());
strcpy(buff, buff_str.c_str());
}
float new_value;
if(!utilities::string::string_to_value<float>(buff, new_value))
Expand All @@ -733,14 +737,11 @@ namespace rs2
}
edit_mode = false;
}
else if (should_be_displayed_in_cm)
else if (use_cm_units)
{
std::string buff_str(buff);
float buff_float = std::stof(buff_str);
buff_float /= 100.f;
std::string buff_str_cm(std::to_string(buff_float));
buff_str = convert_float_str(buff_str, 0.01f);
memset(buff, 0, TEXT_BUFF_SIZE);
strcpy(buff, buff_str_cm.c_str());
strcpy(buff, buff_str.c_str());
}
edit_value = buff;
}
Expand All @@ -767,18 +768,37 @@ namespace rs2
float min_range_displayed = range.min;
float max_range_displayed = range.max;

// computing the number of decimal dggits in the range's step of the option
// this will then be used to format the displayed value
auto num_of_decimal_diggits = [](float f) {
int res = 0;
while (f && (int)f == 0)
{
f *= 10.f;
++res;
}
return res;
};
int num_of_decimal_diggits_displayed = num_of_decimal_diggits(range.step);

// displaying in cm instead of meters for D405
if (should_be_displayed_in_cm)
if (use_cm_units)
{
temp_value_displayed *= 100.f;
min_range_displayed *= 100.f;
max_range_displayed *= 100.f;
int updated_num_of_decimal_diggits_displayed = num_of_decimal_diggits_displayed - 2;
if (updated_num_of_decimal_diggits_displayed > 0)
num_of_decimal_diggits_displayed = updated_num_of_decimal_diggits_displayed;
}

std::stringstream formatting_ss;
formatting_ss << "%." << num_of_decimal_diggits_displayed << "f";

if (ImGui::SliderFloat(id.c_str(), &temp_value_displayed,
min_range_displayed, max_range_displayed, "%.4f"))
min_range_displayed, max_range_displayed, formatting_ss.str().c_str()))
{
if (should_be_displayed_in_cm)
if (use_cm_units)
tmp_value = temp_value_displayed / 100.f;
auto loffset = std::abs(fmod(tmp_value, range.step));
auto roffset = range.step - loffset;
Expand Down
1 change: 0 additions & 1 deletion common/model-views.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ namespace rs2
bool edit_mode = false;
std::string edit_value = "";
private:
std::string get_option_description(rs2_option option, bool display_in_cm) const;
bool is_all_integers() const;
bool is_enum() const;
bool is_checkbox() const;
Expand Down

0 comments on commit ac36bf2

Please sign in to comment.