Merge branch 'fix/objwriter-double-prec' into 'master'

OBJ Reader/Writer: avoid precision loss

See merge request OpenMesh/OpenMesh!349
This commit is contained in:
Jan Möbius
2024-05-07 08:42:15 +00:00
3 changed files with 23 additions and 15 deletions

View File

@@ -26,6 +26,7 @@
<b>IO</b> <b>IO</b>
<ul> <ul>
<li>PLY reader/writer: Fixed color trait Vec3f compilation for PLY writer</li> <li>PLY reader/writer: Fixed color trait Vec3f compilation for PLY writer</li>
<li>OBJ Reader/Writer: Avoid precision loss when writing coordinates and normals.</li>
</ul> </ul>
<b>Build System</b> <b>Build System</b>

View File

@@ -287,8 +287,8 @@ read_vertices(std::istream& _in, BaseImporter& _bi, Options& _opt,
std::vector<VertexHandle> & vertexHandles, std::vector<VertexHandle> & vertexHandles,
Options & fileOptions) Options & fileOptions)
{ {
float x, y, z, u, v, w; double x, y, z, u, v, w;
float r, g, b; double r, g, b;
std::string line; std::string line;
std::string keyWrd; std::string keyWrd;

View File

@@ -216,8 +216,6 @@ _OBJWriter_::
write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::streamsize _precision) const write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::streamsize _precision) const
{ {
unsigned int idx; unsigned int idx;
Vec3f v, n;
Vec2f t;
VertexHandle vh; VertexHandle vh;
std::vector<VertexHandle> vhandles; std::vector<VertexHandle> vhandles;
bool useMatrial = false; bool useMatrial = false;
@@ -296,7 +294,7 @@ write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::
for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i) for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
{ {
vh = VertexHandle(static_cast<int>(i)); vh = VertexHandle(static_cast<int>(i));
t = _be.texcoord(vh); Vec2f t = _be.texcoord(vh);
texMap[t] = static_cast<int>(i); texMap[t] = static_cast<int>(i);
} }
} }
@@ -313,18 +311,27 @@ write(std::ostream& _out, BaseExporter& _be, const Options& _writeOptions, std::
} }
} }
// vertex data (point, normals, texcoords) const bool normal_double = _be.is_normal_double();
const bool point_double = _be.is_point_double();
for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i) for (size_t i=0, nV=_be.n_vertices(); i<nV; ++i)
{ {
vh = VertexHandle(int(i)); vh = VertexHandle(int(i));
v = _be.point(vh); if (point_double) {
n = _be.normal(vh); auto v = _be.pointd(vh);
t = _be.texcoord(vh);
_out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n'; _out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n';
} else {
if (_writeOptions.check(Options::VertexNormal)) auto v = _be.point(vh);
_out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n';
}
if (_writeOptions.check(Options::VertexNormal)) {
if (normal_double) {
auto n = _be.normald(vh);
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n'; _out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n';
} else {
auto n = _be.normal(vh);
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n';
}
}
} }
size_t lastMat = std::numeric_limits<std::size_t>::max(); size_t lastMat = std::numeric_limits<std::size_t>::max();