From e6798c53cf417c97ad8d518d6095bcd6f17e739c Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 5 Oct 2022 18:58:51 +0200 Subject: [PATCH] Base: add helper function to convert a string of a triple of floats into Vector3f --- src/Base/Builder3D.cpp | 31 +++++++++++++++++++++++++++++++ src/Base/Builder3D.h | 7 +++++++ src/Base/PreCompiled.h | 2 ++ 3 files changed, 40 insertions(+) diff --git a/src/Base/Builder3D.cpp b/src/Base/Builder3D.cpp index e813daac55..62435301d6 100644 --- a/src/Base/Builder3D.cpp +++ b/src/Base/Builder3D.cpp @@ -26,9 +26,12 @@ #ifndef _PreComp_ # include # include +# include # include +# include # include # include +# include # include # include #endif @@ -1114,3 +1117,31 @@ bool InventorLoader::isValid() const return true; } + +namespace Base { +Vector3f to_vector(std::string str) +{ + std::string_view view = str; + if (!boost::starts_with(view, "(") || !boost::ends_with(str, ")")) + throw std::runtime_error("string is not a tuple"); + + view.remove_prefix(1); + view.remove_suffix(1); + + boost::char_separator sep(" ,"); + boost::tokenizer > tokens(view, sep); + std::vector token_results; + token_results.assign(tokens.begin(), tokens.end()); + + if (token_results.size() != 3) + throw std::runtime_error("not a tuple of three floats"); + + Base::Vector3f vec; + vec.x = boost::lexical_cast(token_results.at(0)); + vec.y = boost::lexical_cast(token_results.at(1)); + vec.z = boost::lexical_cast(token_results.at(2)); + + return vec; +} + +} diff --git a/src/Base/Builder3D.h b/src/Base/Builder3D.h index 6ab1ce698e..f9d5664640 100644 --- a/src/Base/Builder3D.h +++ b/src/Base/Builder3D.h @@ -402,6 +402,13 @@ private: std::istream &inp; }; +/*! + * Expects a string of the form "(x,y,z)" and creates a vector from it. + * If it fails then a std::exception is thrown. + * Supported type names are float or double + */ +Base::Vector3f to_vector(std::string); + } //namespace Base #endif // BASE_BUILDER3D_H diff --git a/src/Base/PreCompiled.h b/src/Base/PreCompiled.h index 25936cf8ec..2bef608c20 100644 --- a/src/Base/PreCompiled.h +++ b/src/Base/PreCompiled.h @@ -64,6 +64,7 @@ // STL #include +#include #include #include #include @@ -112,6 +113,7 @@ #include #include #include +#include #include #include #include