Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid locale sensitive std::stof function #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions Source/OBJ_Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
// fStream - STD File I/O Library
#include <fstream>

// Math.h - STD math Library
#include <math.h>
// Cmath - STD math Library
#include <cmath>

// Locale.h - C locale library
#include <locale.h>

// Print progress to console while loading (large models)
#define OBJL_CONSOLE_OUTPUT
Expand Down Expand Up @@ -150,7 +153,6 @@ namespace objl
{
Material()
{
name;
Ns = 0.0f;
Ni = 0.0f;
d = 0.0f;
Expand Down Expand Up @@ -404,6 +406,26 @@ namespace objl
idx--;
return elements[idx];
}

// Locale insensitive stof
float li_stof (const std::string& str)
{
char* point = localeconv()->decimal_point;

if (std::string(".") != point)
{
unsigned long point_it = str.find(".");
std::string temp_str = str;

temp_str.replace(point_it, point_it + 1, point);

return std::stof(temp_str);
}
else
{
return std::stof(str);
}
}
}

// Class: Loader
Expand Down Expand Up @@ -542,9 +564,9 @@ namespace objl
Vector3 vpos;
algorithm::split(algorithm::tail(curline), spos, " ");

vpos.X = std::stof(spos[0]);
vpos.Y = std::stof(spos[1]);
vpos.Z = std::stof(spos[2]);
vpos.X = algorithm::li_stof(spos[0]);
vpos.Y = algorithm::li_stof(spos[1]);
vpos.Z = algorithm::li_stof(spos[2]);

Positions.push_back(vpos);
}
Expand All @@ -555,8 +577,8 @@ namespace objl
Vector2 vtex;
algorithm::split(algorithm::tail(curline), stex, " ");

vtex.X = std::stof(stex[0]);
vtex.Y = std::stof(stex[1]);
vtex.X = algorithm::li_stof(stex[0]);
vtex.Y = algorithm::li_stof(stex[1]);

TCoords.push_back(vtex);
}
Expand All @@ -567,9 +589,9 @@ namespace objl
Vector3 vnor;
algorithm::split(algorithm::tail(curline), snor, " ");

vnor.X = std::stof(snor[0]);
vnor.Y = std::stof(snor[1]);
vnor.Z = std::stof(snor[2]);
vnor.X = algorithm::li_stof(snor[0]);
vnor.Y = algorithm::li_stof(snor[1]);
vnor.Z = algorithm::li_stof(snor[2]);

Normals.push_back(vnor);
}
Expand Down Expand Up @@ -947,7 +969,7 @@ namespace objl

// If Vertex is not an interior vertex
float angle = math::AngleBetweenV3(pPrev.Position - pCur.Position, pNext.Position - pCur.Position) * (180 / 3.14159265359);
if (angle <= 0 && angle >= 180)
if (angle <= 0 || angle >= 180)
continue;

// If any vertices are within this triangle
Expand Down Expand Up @@ -1068,9 +1090,9 @@ namespace objl
if (temp.size() != 3)
continue;

tempMaterial.Ka.X = std::stof(temp[0]);
tempMaterial.Ka.Y = std::stof(temp[1]);
tempMaterial.Ka.Z = std::stof(temp[2]);
tempMaterial.Ka.X = algorithm::li_stof(temp[0]);
tempMaterial.Ka.Y = algorithm::li_stof(temp[1]);
tempMaterial.Ka.Z = algorithm::li_stof(temp[2]);
}
// Diffuse Color
if (algorithm::firstToken(curline) == "Kd")
Expand All @@ -1081,9 +1103,9 @@ namespace objl
if (temp.size() != 3)
continue;

tempMaterial.Kd.X = std::stof(temp[0]);
tempMaterial.Kd.Y = std::stof(temp[1]);
tempMaterial.Kd.Z = std::stof(temp[2]);
tempMaterial.Kd.X = algorithm::li_stof(temp[0]);
tempMaterial.Kd.Y = algorithm::li_stof(temp[1]);
tempMaterial.Kd.Z = algorithm::li_stof(temp[2]);
}
// Specular Color
if (algorithm::firstToken(curline) == "Ks")
Expand All @@ -1094,24 +1116,24 @@ namespace objl
if (temp.size() != 3)
continue;

tempMaterial.Ks.X = std::stof(temp[0]);
tempMaterial.Ks.Y = std::stof(temp[1]);
tempMaterial.Ks.Z = std::stof(temp[2]);
tempMaterial.Ks.X = algorithm::li_stof(temp[0]);
tempMaterial.Ks.Y = algorithm::li_stof(temp[1]);
tempMaterial.Ks.Z = algorithm::li_stof(temp[2]);
}
// Specular Exponent
if (algorithm::firstToken(curline) == "Ns")
{
tempMaterial.Ns = std::stof(algorithm::tail(curline));
tempMaterial.Ns = algorithm::li_stof(algorithm::tail(curline));
}
// Optical Density
if (algorithm::firstToken(curline) == "Ni")
{
tempMaterial.Ni = std::stof(algorithm::tail(curline));
tempMaterial.Ni = algorithm::li_stof(algorithm::tail(curline));
}
// Dissolve
if (algorithm::firstToken(curline) == "d")
{
tempMaterial.d = std::stof(algorithm::tail(curline));
tempMaterial.d = algorithm::li_stof(algorithm::tail(curline));
}
// Illumination
if (algorithm::firstToken(curline) == "illum")
Expand Down