Skip to content

Commit 0d52dbf

Browse files
authored
Merge pull request #34 from MOLAorg/feat/mm2txt-mm2ply-more-precision
Exploit the maximum float32/float64 precision in exported txt formats
2 parents cebc610 + c6e1670 commit 0d52dbf

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

apps/mm2ply/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ void saveToPly(
271271
}
272272

273273
// 4. Data Loop
274-
auto write_val = [&](auto val)
274+
auto write_val = [&](auto val, const char* fmt)
275275
{
276276
if (binary)
277277
{
278278
f.write(reinterpret_cast<const char*>(&val), sizeof(val));
279279
}
280280
else
281281
{
282-
f << +val << " ";
282+
f << mrpt::format(fmt, val) << " ";
283283
}
284284
};
285285

@@ -296,29 +296,29 @@ void saveToPly(
296296
{
297297
const auto& buf =
298298
*static_cast<const mrpt::aligned_std_vector<float>*>(acc.bufPtr);
299-
write_val(buf[i]);
299+
write_val(buf[i], "%.8e");
300300
break;
301301
}
302302
#if MRPT_VERSION >= 0x020f03 // 2.15.3
303303
case FieldAccessor::DOUBLE:
304304
{
305305
const auto& buf =
306306
*static_cast<const mrpt::aligned_std_vector<double>*>(acc.bufPtr);
307-
write_val(buf[i]);
307+
write_val(buf[i], "%.16le");
308308
break;
309309
}
310310
case FieldAccessor::UINT16:
311311
{
312312
const auto& buf =
313313
*static_cast<const mrpt::aligned_std_vector<uint16_t>*>(acc.bufPtr);
314-
write_val(buf[i]);
314+
write_val(buf[i], "%u");
315315
break;
316316
}
317317
case FieldAccessor::UINT8:
318318
{
319319
const auto& buf =
320320
*static_cast<const mrpt::aligned_std_vector<uint8_t>*>(acc.bufPtr);
321-
write_val(buf[i]);
321+
write_val(buf[i], "%i");
322322
break;
323323
}
324324
#endif

apps/mm2txt/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ bool saveToTxt(
141141
// Check coordinate fields
142142
if (fieldName == "x")
143143
{
144-
mrpt::system::os::fprintf(f, "%f", xs.at(i));
144+
mrpt::system::os::fprintf(f, "%.8f", xs.at(i));
145145
}
146146
else if (fieldName == "y")
147147
{
148-
mrpt::system::os::fprintf(f, "%f", ys.at(i));
148+
mrpt::system::os::fprintf(f, "%.8f", ys.at(i));
149149
}
150150
else if (fieldName == "z")
151151
{
152-
mrpt::system::os::fprintf(f, "%f", zs.at(i));
152+
mrpt::system::os::fprintf(f, "%.8f", zs.at(i));
153153
}
154154
// Check float fields
155155
else if (floatFields.count(fieldName))
156156
{
157-
mrpt::system::os::fprintf(f, "%f", floatFields.at(fieldName).at(i));
157+
mrpt::system::os::fprintf(f, "%.8e", floatFields.at(fieldName).at(i));
158158
}
159159
// Check uint16 fields
160160
else if (uint16Fields.count(fieldName))
@@ -165,7 +165,7 @@ bool saveToTxt(
165165
// Check double fields
166166
else if (doubleFields.count(fieldName))
167167
{
168-
mrpt::system::os::fprintf(f, "%lf", doubleFields.at(fieldName).at(i));
168+
mrpt::system::os::fprintf(f, "%.16le", doubleFields.at(fieldName).at(i));
169169
}
170170
// Check uint8 fields
171171
else if (uint8Fields.count(fieldName))

0 commit comments

Comments
 (0)