Skip to content

Commit 8906224

Browse files
authored
Merge pull request InsightSoftwareConsortium#5968 from N-Dekker/Add-OpenInputFile-to-MeshIOBase
Add `MeshIOBase::OpenInputFile()`, use it in OBJMeshIO, BYUMeshIO, and OFFMeshIO
2 parents 643cafb + b61a5e5 commit 8906224

7 files changed

Lines changed: 75 additions & 129 deletions

File tree

Modules/IO/MeshBYU/src/itkBYUMeshIO.cxx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,7 @@ void
6565
BYUMeshIO::ReadMeshInformation()
6666
{
6767
// Define input file stream and attach it to input file
68-
std::ifstream inputFile;
69-
70-
// Due to the windows couldn't work well for tellg() and seekg() for ASCII mode, hence we
71-
// open the file with std::ios::binary
72-
inputFile.open(this->m_FileName.c_str(), std::ios::in | std::ios::binary);
73-
74-
if (!inputFile.is_open())
75-
{
76-
itkExceptionMacro("Unable to open input file " << this->m_FileName);
77-
}
68+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
7869

7970
// Read the ASCII file information
8071
unsigned int numberOfParts = 0;
@@ -183,16 +174,7 @@ void
183174
BYUMeshIO::ReadPoints(void * buffer)
184175
{
185176
// Define input file stream and attach it to input file
186-
std::ifstream inputFile;
187-
188-
/** Due to the windows couldn't work well for tellg() and seekg() for ASCII mode, hence we
189-
open the file with std::ios::binary */
190-
inputFile.open(this->m_FileName.c_str(), std::ios::in | std::ios::binary);
191-
192-
if (!inputFile.is_open())
193-
{
194-
itkExceptionMacro("Unable to open input file " << this->m_FileName);
195-
}
177+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
196178

197179
// Set the position to points start
198180
inputFile.seekg(m_FilePosition, std::ios::beg);
@@ -221,14 +203,7 @@ void
221203
BYUMeshIO::ReadCells(void * buffer)
222204
{
223205
// Define input file stream and attach it to input file
224-
std::ifstream inputFile;
225-
226-
inputFile.open(this->m_FileName.c_str(), std::ios::in | std::ios::binary);
227-
228-
if (!inputFile.is_open())
229-
{
230-
itkExceptionMacro("Unable to open input file " << this->m_FileName);
231-
}
206+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
232207

233208
// Set the position to current position
234209
inputFile.seekg(m_FilePosition, std::ios::beg);

Modules/IO/MeshBase/include/itkMeshIOBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ class ITKIOMeshBase_EXPORT MeshIOBase : public LightProcessObject
640640
void
641641
AddSupportedWriteExtension(const char * extension);
642642

643+
/** Opens the input file specified by the current FileName */
644+
[[nodiscard]] std::ifstream
645+
OpenInputFile() const;
646+
643647
/** Read data from input file stream to buffer with ascii style */
644648
template <typename T>
645649
void

Modules/IO/MeshBase/src/itkMeshIOBase.cxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkMeshIOBase.h"
20+
#include "itksys/SystemTools.hxx"
2021

2122
namespace itk
2223
{
@@ -227,4 +228,30 @@ MeshIOBase::PrintSelf(std::ostream & os, Indent indent) const
227228
os << indent << "Point pixel component type: " << GetComponentTypeAsString(m_PointPixelComponentType) << std::endl;
228229
os << indent << "Cell pixel component type: " << GetComponentTypeAsString(m_CellPixelComponentType) << std::endl;
229230
}
231+
232+
233+
std::ifstream
234+
MeshIOBase::OpenInputFile() const
235+
{
236+
if (m_FileName.empty())
237+
{
238+
itkExceptionStringMacro("No input FileName");
239+
}
240+
241+
if (!itksys::SystemTools::FileExists(m_FileName.c_str()))
242+
{
243+
itkExceptionMacro("File " << m_FileName << " does not exist");
244+
}
245+
246+
// Open the input file in binary mode. (Text mode does not work well on Windows, when using tellg() and seekg().)
247+
std::ifstream inputFile(m_FileName, std::ios::binary);
248+
249+
// Test whether the file was opened
250+
if (!inputFile.is_open())
251+
{
252+
itkExceptionMacro("Unable to open file " << m_FileName);
253+
}
254+
255+
return inputFile;
256+
}
230257
} // namespace itk

Modules/IO/MeshOBJ/include/itkOBJMeshIO.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ class ITKIOMeshOBJ_EXPORT OBJMeshIO : public MeshIOBase
177177

178178
void
179179
PrintSelf(std::ostream & os, Indent indent) const override;
180-
181-
private:
182-
[[nodiscard]] std::ifstream
183-
OpenFile() const;
184180
};
185181
} // end namespace itk
186182

Modules/IO/MeshOBJ/src/itkOBJMeshIO.cxx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,6 @@ OBJMeshIO::CanWriteFile(const char * fileName)
5757
return true;
5858
}
5959

60-
std::ifstream
61-
OBJMeshIO::OpenFile() const
62-
{
63-
if (this->m_FileName.empty())
64-
{
65-
itkExceptionStringMacro("No input FileName");
66-
}
67-
68-
if (!itksys::SystemTools::FileExists(m_FileName.c_str()))
69-
{
70-
itkExceptionMacro("File " << this->m_FileName << " does not exist");
71-
}
72-
73-
// Read file as ascii
74-
// Due to the windows couldn't work well for tellg() and seekg() for ASCII
75-
// mode, hence we
76-
// open the file with std::ios::binary
77-
std::ifstream inputFile(m_FileName, std::ios::binary);
78-
79-
// Test whether the file was opened
80-
if (!inputFile.is_open())
81-
{
82-
itkExceptionMacro("Unable to open file " << this->m_FileName);
83-
}
84-
85-
return inputFile;
86-
}
87-
8860

8961
bool
9062
OBJMeshIO::SplitLine(const std::string & line, std::string & type, std::string & content)
@@ -118,7 +90,7 @@ void
11890
OBJMeshIO::ReadMeshInformation()
11991
{
12092
// Define input file stream and attach it to input file
121-
std::ifstream inputFile = OpenFile();
93+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
12294

12395
// Read and analyze the first line in the file
12496
SizeValueType numberOfCellPoints = 0;
@@ -188,7 +160,7 @@ void
188160
OBJMeshIO::ReadPoints(void * buffer)
189161
{
190162
// Define input file stream and attach it to input file
191-
std::ifstream inputFile = OpenFile();
163+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
192164

193165
// Number of data array
194166
auto * data = static_cast<float *>(buffer);
@@ -219,7 +191,7 @@ void
219191
OBJMeshIO::ReadCells(void * buffer)
220192
{
221193
// Define input file stream and attach it to input file
222-
std::ifstream inputFile = OpenFile();
194+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
223195

224196
// Read and analyze the first line in the file
225197
const auto data = make_unique_for_overwrite<long[]>(this->m_CellBufferSize - this->m_NumberOfCells);
@@ -273,7 +245,7 @@ void
273245
OBJMeshIO::ReadPointData(void * buffer)
274246
{
275247
// Define input file stream and attach it to input file
276-
std::ifstream inputFile = OpenFile();
248+
std::ifstream inputFile = MeshIOBase::OpenInputFile();
277249

278250
// Number of data array
279251
auto * data = static_cast<float *>(buffer);

Modules/IO/MeshOFF/include/itkOFFMeshIO.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,9 @@ class ITKIOMeshOFF_EXPORT OFFMeshIO : public MeshIOBase
196196
void
197197
PrintSelf(std::ostream & os, Indent indent) const override;
198198

199-
void
200-
OpenFile();
201-
202-
void
203-
CloseFile();
204-
205199
private:
206-
std::ifstream m_InputFile{};
207200
StreamOffsetType m_PointsStartPosition{}; // file position for points relative to std::ios::beg
201+
StreamOffsetType m_CellsStartPosition{}; // file position for cells relative to std::ios::beg
208202
bool m_TriangleCellType{}; // if all cells are triangle it is true. otherwise, it is false.
209203
};
210204
} // end namespace itk

0 commit comments

Comments
 (0)