Skip to content

Commit 1efb6dd

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 itk::ConvertNumberToString() for both float and double specializations, producing the shortest decimal string that round-trips exactly. See: #3249 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2bfe6ae commit 1efb6dd

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Modules/IO/MeshFreeSurfer/include/itkFreeSurferAsciiMeshIO.h

Lines changed: 2 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 "itkNumberToString.h"
2728

2829
namespace itk
2930
{
@@ -117,13 +118,12 @@ class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO : public MeshIOBase
117118
void
118119
WritePoints(T * buffer, std::ofstream & outputFile, T label = T{})
119120
{
120-
outputFile.precision(6);
121121
SizeValueType index = 0;
122122
for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
123123
{
124124
for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
125125
{
126-
outputFile << std::fixed << buffer[index++] << " ";
126+
outputFile << itk::ConvertNumberToString(buffer[index++]) << " ";
127127
}
128128
outputFile << label << '\n';
129129
}

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)