Skip to content

Commit

Permalink
cgv::util: Introduce option to use fixed notation when converting flo…
Browse files Browse the repository at this point in the history
…ats to string
  • Loading branch information
brussig-tud committed Jan 15, 2024
1 parent 67598e0 commit a725692
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cgv/utils/convert_string.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace cgv {
namespace utils {

template <>
std::string to_string(const std::string& v, unsigned int w, unsigned int p)
std::string to_string(const std::string& v, unsigned int w, unsigned int p, bool)
{
return v;
}
Expand Down
6 changes: 4 additions & 2 deletions cgv/utils/convert_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace cgv {

/// convert a numeric type \c T into string of width \c w and precision \c p
template <typename T>
std::string to_string(const T& v, unsigned int w = -1, unsigned int p = -1)
std::string to_string(const T& v, unsigned int w = -1, unsigned int p = -1, bool fixed=false)
{
std::stringstream ss;
if (fixed)
ss << std::fixed;
if (w != (unsigned int)-1)
ss << std::setw(w);
if (p != (unsigned int)-1)
Expand All @@ -36,7 +38,7 @@ std::string to_string(const T& v, unsigned int w, char fill_char)
}

/// specialization of conversion from string to strings
template <> CGV_API std::string to_string(const std::string& v, unsigned int w, unsigned int p);
template <> CGV_API std::string to_string(const std::string& v, unsigned int w, unsigned int p, bool);

/// extract value from string
template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions libs/cgv_app/color_map_legend.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ void color_map_legend::create_labels() {
float t = fi / static_cast<float>(num_ticks - 1);
float val = cgv::math::lerp(range.x(), range.y(), t);

std::string str = "";
std::string str;

if(label_integer_mode)
str = std::to_string(static_cast<int>(round(val)));
else
str = cgv::utils::to_string(val, -1, precision);
str = cgv::utils::to_string(val, -1, precision, /*fixed*/true);

labels.add_text(str, ivec2(0), cgv::render::TextAlignment::TA_NONE);
max_length = std::max(max_length, labels.ref_texts().back().size.x());
Expand Down

0 comments on commit a725692

Please sign in to comment.