Skip to content

Commit 28f52e6

Browse files
hjmjohnsonclaude
andcommitted
ENH: Use lossless float serialization in SpatialObject and Mesh IO
Two persistence-layer paths used lossy default stream precision for floating-point coordinates: itkPolygonGroupSpatialObjectXMLFile.cxx: PolygonSpatialObject vertex coordinates (Point<double,3>) were streamed to the XML file with default 6-digit precision. Replace with itk::ConvertNumberToString() per component. itkFreeSurferAsciiMeshIO.h WritePoints<T>: hardcoded precision(6) with std::fixed limited output to 6 decimal places regardless of T. Replace with std::numeric_limits<T>::max_digits10 and remove std::fixed so that both float and double specializations produce shortest-round-trip output. See: #3249 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2bfe6ae commit 28f52e6

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

Modules/IO/MeshFreeSurfer/include/itkFreeSurferAsciiMeshIO.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "itkMakeUniqueForOverwrite.h"
2525

2626
#include <fstream>
27+
#include <limits>
2728

2829
namespace itk
2930
{
@@ -117,13 +118,13 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO : public MeshIOBase
117118
void
118119
WritePoints(T * buffer, std::ofstream & outputFile, T label = T{})
119120
{
120-
outputFile.precision(6);
121+
outputFile.precision(std::numeric_limits<T>::max_digits10);
121122
SizeValueType index = 0;
122123
for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
123124
{
124125
for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
125126
{
126-
outputFile << std::fixed << buffer[index++] << " ";
127+
outputFile << buffer[index++] << " ";
127128
}
128129
outputFile << label << '\n';
129130
}

Modules/IO/SpatialObjects/src/itkPolygonGroupSpatialObjectXMLFile.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "itksys/SystemTools.hxx"
2121
#include "itkMetaDataObject.h"
2222
#include "itkIOCommon.h"
23+
#include "itkNumberToString.h"
2324
#define RAISE_EXCEPTION(s) \
2425
{ \
2526
ExceptionObject exception(__FILE__, __LINE__); \
@@ -253,7 +254,8 @@ PolygonGroupSpatialObjectXMLFileWriter::WriteFile()
253254
{
254255
PolygonSpatialObjectType::PointType curpoint = pointIt->GetPositionInObjectSpace();
255256
WriteStartElement("POINT", output);
256-
output << curpoint[0] << ' ' << curpoint[1] << ' ' << curpoint[2];
257+
output << itk::ConvertNumberToString(curpoint[0]) << ' ' << itk::ConvertNumberToString(curpoint[1]) << ' '
258+
<< itk::ConvertNumberToString(curpoint[2]);
257259
WriteEndElement("POINT", output);
258260
output << std::endl;
259261
++pointIt;

0 commit comments

Comments
 (0)