Skip to content

Commit

Permalink
fixed stringstream to float bug under windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor-Haefner committed Mar 1, 2021
1 parent 936c372 commit 8a85c4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/core/scene/interfaces/VRScenegraphInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ void parseOSGVec(string& data, V& v) {
while (ss >> f) v->addValue(f);
}

template<class T, class U, typename V>
// T2 allows to pass type double because stringstreams may fail to convert scientific notations to float
template<class T, class U, class T2, typename V>
void parseOSGVec2(string& data, V& v) {
int N = sizeof(U)/sizeof(T);
stringstream ss(data);
T f;
T2 f;
U u;
int i=0;
while (ss >> f) {
Expand All @@ -120,6 +121,10 @@ void parseOSGVec2(string& data, V& v) {
v->addValue(u);
}
}

if (ss.fail()) {
cout << " parseOSGVec2 failed!" << endl;
}
}

VRObjectPtr VRScenegraphInterface::getObject(string objID) {
Expand Down Expand Up @@ -1070,7 +1075,7 @@ void VRScenegraphInterface::handle(string msg) {
if (geo && m.size() > 3) {
GeoPnt3fPropertyMTRecPtr pos = GeoPnt3fProperty::create();
replace( m[3].begin(), m[3].end(), ',', '.');
parseOSGVec2<float, Pnt3f>(m[3], pos);
parseOSGVec2<float, Pnt3f, double>(m[3], pos);
geo->setPositions(pos);
//cout << "set geo positions " << geo->getName() << " " << pos->size() << endl;
}
Expand All @@ -1080,17 +1085,17 @@ void VRScenegraphInterface::handle(string msg) {
if (geo && m.size() > 3) {
GeoVec3fPropertyMTRecPtr norms = GeoVec3fProperty::create();
replace( m[3].begin(), m[3].end(), ',', '.');
parseOSGVec2<float, Vec3f>(m[3], norms);
parseOSGVec2<float, Vec3f, double>(m[3], norms);
geo->setNormals(norms);
//cout << "set geo normals " << geo->getName() << " " << norms->size() << endl;
//cout << "set geo normals " << geo->getName() << " " << norms->size() << " " << m[3].size() << endl;
}
}

if (m[1] == "colors") {
if (geo && m.size() > 3) {
GeoColor4fPropertyMTRecPtr cols = GeoColor4fProperty::create();
replace( m[3].begin(), m[3].end(), ',', '.');
parseOSGVec2<float, Color4f>(m[3], cols);
parseOSGVec2<float, Color4f, double>(m[3], cols);
geo->setColors(cols);
//cout << "set geo colors " << geo->getName() << " " << cols->size() << endl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/toString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ template<> int toValue(stringstream& ss, int& v) { return ssToVal(ss, v); }
template<> int toValue(stringstream& ss, unsigned int& v) { return ssToVal(ss, v); }
template<> int toValue(stringstream& ss, long& v) { return ssToVal(ss, v); }
template<> int toValue(stringstream& ss, unsigned long& v) { return ssToVal(ss, v); }
template<> int toValue(stringstream& ss, float& v) { return ssToVal(ss, v); }
template<> int toValue(stringstream& ss, float& v) { double d; auto r = ssToVal(ss, d); v = d; return r; } // use double because stringstreams may fail to convert scientific notations to float
template<> int toValue(stringstream& ss, double& v) { return ssToVal(ss, v); }

int toInt (string s) { return toValue<int >(s); }
Expand Down

0 comments on commit 8a85c4c

Please sign in to comment.