Skip to content

Commit

Permalink
Change readers.ply output for integers with precision set (#2422)
Browse files Browse the repository at this point in the history
* Don't write ints using fixed precision.

* Test for issue 2421.

Close #2421
  • Loading branch information
abellgithub committed Mar 29, 2019
1 parent 888aadf commit 5309eeb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
13 changes: 8 additions & 5 deletions io/PlyWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ void PlyWriter::ready(PointTableRef table)
" points supported.");

m_stream = Utils::createFile(m_filename, true);
if (m_format == Format::Ascii && m_precisionArg->set())
{
*m_stream << std::fixed;
m_stream->precision(m_precision);
}
writeHeader(table.layout());
}

Expand All @@ -174,6 +169,14 @@ void PlyWriter::writeValue(PointRef& point, Dimension::Id dim,
if (m_format == Format::Ascii)
{
double d = point.getFieldAs<double>(dim);
if (m_precisionArg->set() &&
Dimension::base(type) == Dimension::BaseType::Floating)
{
*m_stream << std::fixed;
m_stream->precision(m_precision);
}
else
m_stream->unsetf(std::ios_base::fixed);
*m_stream << d;
}
else if (m_format == Format::BinaryLe)
Expand Down
10 changes: 10 additions & 0 deletions test/data/ply/issue_2421.ply
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ply
format ascii 1.0
comment Generated by PDAL
element vertex 1
property float64 x
property float64 y
property float64 z
property int32 i
end_header
1.23457 12345.67890 1234567890.12345 12345
38 changes: 38 additions & 0 deletions test/unit/io/PlyWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,42 @@ TEST(PlyWriter, precisionException)
EXPECT_THROW(writer.prepare(table), pdal_error);
}

// Make sure we don't write ints as floats.
TEST(PlyWriter, issue_2421)
{
std::string outfile(Support::temppath("out.ply"));
std::string referenceFile(Support::datapath("ply/issue_2421.ply"));

FileUtils::deleteFile(outfile);

PointTable t;

t.layout()->registerDim(Dimension::Id::X);
t.layout()->registerDim(Dimension::Id::Y);
t.layout()->registerDim(Dimension::Id::Z);
Dimension::Id iid = t.layout()->assignDim("I", Dimension::Type::Signed32);

PointViewPtr v(new PointView(t));
v->setField(Dimension::Id::X, 0, 1.23456789012345);
v->setField(Dimension::Id::Y, 0, 12345.6789012345);
v->setField(Dimension::Id::Z, 0, 1234567890.12345);
v->setField(iid, 0, 12345);

BufferReader r;
r.addView(v);

PlyWriter w;
Options wo;
wo.add("filename", outfile);
wo.add("storage_mode", "ascii");
wo.add("precision", 5);
w.setInput(r);
w.setOptions(wo);

w.prepare(t);
w.execute(t);

EXPECT_TRUE(Support::compare_text_files(outfile, referenceFile));
}

} // namespace pdal

0 comments on commit 5309eeb

Please sign in to comment.