MCPcopy Create free account
hub / github.com/OSVR/OSVR-Core / vectorFromJson

Function vectorFromJson

src/osvr/Common/JSONTransformVisitor.cpp:49–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47 static const char MINUS[] = "-";
48 template <typename T = Eigen::Vector3d>
49 inline T vectorFromJson(Json::Value const &v) {
50 T ret = T::Zero();
51 if (v.isString()) {
52 std::string inVal = v.asString();
53 if (inVal.empty()) {
54 throw std::runtime_error(
55 "Empty string can't be turned into a vector!");
56 }
57 using boost::is_any_of;
58 std::string val(boost::to_upper_copy(inVal));
59 if ((!boost::algorithm::all(
60 val, is_any_of(AXIS_NAMES) || is_any_of(MINUS) ||
61 boost::algorithm::is_space())) ||
62 boost::count_if(val, is_any_of(AXIS_NAMES)) != 1 ||
63 boost::count_if(val, is_any_of(MINUS)) > 1) {
64 throw std::runtime_error(
65 "Cannot turn the specified string into a vector: " + inVal);
66 }
67 double factor =
68 (val.find(MINUS[0]) == std::string::npos) ? 1.0 : -1.0;
69 const std::string axisnames(AXIS_NAMES);
70 for (const char c : val) {
71 auto location = axisnames.find(c);
72 if (location != std::string::npos) {
73 ret[location] = factor;
74 return ret;
75 }
76 }
77 BOOST_ASSERT_MSG(false, "Should never reach here!");
78 }
79 if (v.isArray()) {
80 if (v.size() != T::RowsAtCompileTime) {
81 throw std::runtime_error(
82 "Vector size wrong when converting from JSON!");
83 }
84 for (Json::ArrayIndex i = 0, e = v.size(); i < e; ++i) {
85 ret[i] = v[i].asFloat();
86 }
87 return ret;
88 }
89 throw std::runtime_error("Could not convert JSON to vector: " +
90 v.toStyledString());
91 }
92
93 static const char DEGREES_KEY[] = "degrees";
94 static const char RADIANS_KEY[] = "radians";

Callers

nothing calls this directly

Calls 3

emptyMethod · 0.45
findMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected