Base: add helper function to convert a string of a triple of floats into Vector3f

This commit is contained in:
wmayer
2022-10-05 18:58:51 +02:00
parent 3433520a74
commit e6798c53cf
3 changed files with 40 additions and 0 deletions

View File

@@ -26,9 +26,12 @@
#ifndef _PreComp_
# include <algorithm>
# include <cassert>
# include <exception>
# include <string>
# include <string_view>
# include <fstream>
# include <boost/algorithm/string.hpp>
# include <boost/algorithm/string/predicate.hpp>
# include <boost/lexical_cast.hpp>
# include <boost/tokenizer.hpp>
#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<char> sep(" ,");
boost::tokenizer<boost::char_separator<char> > tokens(view, sep);
std::vector<std::string> 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<float>(token_results.at(0));
vec.y = boost::lexical_cast<float>(token_results.at(1));
vec.z = boost::lexical_cast<float>(token_results.at(2));
return vec;
}
}

View File

@@ -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

View File

@@ -64,6 +64,7 @@
// STL
#include <string>
#include <string_view>
#include <list>
#include <map>
#include <unordered_map>
@@ -112,6 +113,7 @@
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>