ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkSWCMeshIO.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef itkSWCMeshIO_h
20#define itkSWCMeshIO_h
21#include "IOMeshSWCExport.h"
22
23#include "itkMeshIOBase.h"
24#include "itkVectorContainer.h"
25
26#include <fstream>
27#include <unordered_map>
28#include <algorithm>
29
30namespace itk
31{
32
40{
41public:
52};
53extern IOMeshSWC_EXPORT std::ostream &
54 operator<<(std::ostream & out, const SWCMeshIOEnums::SWCPointData value);
55
63
64class IOMeshSWC_EXPORT SWCMeshIO : public MeshIOBase
65{
66public:
67 ITK_DISALLOW_COPY_AND_MOVE(SWCMeshIO);
68
70 using Self = SWCMeshIO;
74
77
79 itkNewMacro(Self);
80
82 itkOverrideGetNameOfClassMacro(SWCMeshIO);
83
84 /*-------- This part of the interfaces deals with reading data. ----- */
85
91 bool
92 CanReadFile(const char * fileName) override;
93
95 void
97
99 void
100 ReadPoints(void * buffer) override;
101
102 void
103 ReadCells(void * buffer) override;
104
105 void
106 ReadPointData(void * buffer) override;
107
108 void
109 ReadCellData(void * buffer) override;
110
111 /*-------- This part of the interfaces deals with writing data. ----- */
112
118 bool
119 CanWriteFile(const char * fileName) override;
120
122 void
124
126 void
127 WritePoints(void * buffer) override;
128
129 void
130 WriteCells(void * buffer) override;
131
132 void
133 WritePointData(void * buffer) override;
134
135 void
136 WriteCellData(void * buffer) override;
137
138 void
139 Write() override;
140
141 using HeaderContentType = std::vector<std::string>;
142 void
143 SetHeaderContent(const HeaderContentType & headerContent);
144 itkGetConstReferenceMacro(HeaderContent, HeaderContentType);
145
146 // using SampleIdentifierType = int16_t;
147 // For Python wrapping
148 using SampleIdentifierType = float;
149 // using TypeIdentifierType = uint8_t;
150 using TypeIdentifierType = float;
151 using RadiusType = double;
152 // using ParentIdentifierType = int16_t;
153 // For Python wrapping
154 using ParentIdentifierType = float;
155
160
162 void
166
177 void
181
182
184 void
186 const RadiusContainerType *
187 GetRadii() const;
188
190 void
194
196 itkGetConstMacro(PointDataContent, SWCMeshIOEnums::SWCPointData);
198
199protected:
201 template <typename T>
202 void
203 WritePoints(T * buffer)
204 {
206 m_PointsBuffer->resize(this->GetNumberOfPoints());
207
208 for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
209 {
210 for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
211 {
212 m_PointsBuffer->SetElement(index, static_cast<float>(buffer[index]));
213 index++;
214 }
215 }
216 }
217
219 template <typename T>
220 void
221 WritePointData(T * buffer)
222 {
223 switch (m_PointDataContent)
224 {
226 {
227 m_SampleIdentifiers->resize(this->GetNumberOfPoints());
228
229 for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
230 {
231 m_SampleIdentifiers->SetElement(ii, static_cast<SampleIdentifierType>(buffer[ii]));
232 }
233 }
234 break;
236 {
237 m_TypeIdentifiers->resize(this->GetNumberOfPoints());
238
239 for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
240 {
241 m_TypeIdentifiers->SetElement(ii, static_cast<TypeIdentifierType>(buffer[ii]));
242 }
243 }
244 break;
246 {
247 m_Radii->resize(this->GetNumberOfPoints());
248
249 for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
250 {
251 m_Radii->SetElement(ii, static_cast<RadiusType>(buffer[ii]));
252 }
253 }
254 break;
256 {
257 m_ParentIdentifiers->resize(this->GetNumberOfPoints());
258
259 for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
260 {
261 m_ParentIdentifiers->SetElement(ii, static_cast<ParentIdentifierType>(buffer[ii]));
262 }
263 }
264 break;
265 }
266 }
267
268 template <typename T>
269 void
270 WriteCells(T * buffer)
271 {
272 m_ParentIdentifiers->resize(this->GetNumberOfPoints());
273 std::fill(m_ParentIdentifiers->begin(), m_ParentIdentifiers->end(), -1);
275
276 for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
277 {
278 if (static_cast<uint8_t>(buffer[index]) != static_cast<uint8_t>(CommonEnums::CellGeometry::LINE_CELL))
279 {
280 itkExceptionMacro("Unexpected cell type -- line cell expected. Found: " << buffer[index]);
281 }
282 ++index;
283 if (static_cast<uint8_t>(buffer[index]) != 2)
284 {
285 itkExceptionMacro("Unexpected number of cell points -- expected 2. Found: " << buffer[index]);
286 }
287 ++index;
288 const auto parentPoint = static_cast<IdentifierType>(buffer[index]);
289 ++index;
290 const auto samplePoint = static_cast<IdentifierType>(buffer[index]);
291 ++index;
292
293 if (parentPoint >= this->GetNumberOfPoints() || samplePoint >= this->GetNumberOfPoints())
294 {
295 itkExceptionMacro("SWC cell references point index out of range [0, "
296 << this->GetNumberOfPoints() << "): parent=" << parentPoint << " sample=" << samplePoint);
297 }
298 const auto parentEntry = m_PointIndexToSampleIdentifier.find(parentPoint);
299 const auto parentIdentifier = (parentEntry != m_PointIndexToSampleIdentifier.end())
300 ? parentEntry->second
301 : static_cast<SampleIdentifierType>(parentPoint);
302 m_ParentIdentifiers->SetElement(samplePoint, parentIdentifier);
303 }
304 }
305
306protected:
308 ~SWCMeshIO() override;
309
310 void
311 PrintSelf(std::ostream & os, Indent indent) const override;
312
315 using SampleIdentifierToPointIndexType = std::unordered_map<SampleIdentifierType, IdentifierType>;
316 using PointIndexToParentPointIndexType = std::unordered_map<IdentifierType, IdentifierType>;
317 using PointIndexToSampleIdentifierType = std::unordered_map<IdentifierType, SampleIdentifierType>;
318
319private:
330
332};
333} // end namespace itk
334
335#endif
Control indentation during Print() invocation.
Definition itkIndent.h:53
SizeValueType m_NumberOfPoints
IdentifierType SizeValueType
unsigned int m_PointDimension
std::streamoff StreamOffsetType
virtual SizeValueType GetNumberOfPoints() const
SizeValueType m_NumberOfCells
enums for the SWCMeshIO class.
std::unordered_map< SampleIdentifierType, IdentifierType > SampleIdentifierToPointIndexType
PointIndexToSampleIdentifierType m_PointIndexToSampleIdentifier
const SampleIdentifierContainerType * GetSampleIdentifiers() const
ParentIdentifierContainerType::Pointer m_ParentIdentifiers
void WriteMeshInformation() override
const ParentIdentifierContainerType * GetParentIdentifiers() const
SampleIdentifierContainerType::Pointer m_SampleIdentifiers
float SampleIdentifierType
void ReadPoints(void *buffer) override
void WriteCells(T *buffer)
PointsBufferContainerType::Pointer m_PointsBuffer
void SetRadii(const RadiusContainerType *)
void PrintSelf(std::ostream &os, Indent indent) const override
VectorContainer< float > PointsBufferContainerType
void WritePointData(void *buffer) override
VectorContainer< TypeIdentifierType > TypeIdentifierContainerType
VectorContainer< RadiusType > RadiusContainerType
const TypeIdentifierContainerType * GetTypeIdentifiers() const
void ReadPointData(void *buffer) override
VectorContainer< uint32_t > CellsBufferContainerType
PointIndexToParentPointIndexType m_PointIndexToParentPointIndex
SampleIdentifierToPointIndexType m_SampleIdentifierToPointIndex
void WritePoints(T *buffer)
std::unordered_map< IdentifierType, SampleIdentifierType > PointIndexToSampleIdentifierType
Superclass::SizeValueType SizeValueType
const RadiusContainerType * GetRadii() const
float TypeIdentifierType
~SWCMeshIO() override
void SetParentIdentifiers(const ParentIdentifierContainerType *)
SWCMeshIO Self
void WritePointData(T *buffer)
void SetSampleIdentifiers(const SampleIdentifierContainerType *)
void WritePoints(void *buffer) override
VectorContainer< SampleIdentifierType > SampleIdentifierContainerType
SmartPointer< const Self > ConstPointer
void WriteCellData(void *buffer) override
std::unordered_map< IdentifierType, IdentifierType > PointIndexToParentPointIndexType
MeshIOBase Superclass
void ReadCellData(void *buffer) override
float ParentIdentifierType
void ReadMeshInformation() override
HeaderContentType m_HeaderContent
void SetTypeIdentifiers(const TypeIdentifierContainerType *)
bool CanReadFile(const char *fileName) override
std::vector< std::string > HeaderContentType
void ReadCells(void *buffer) override
void SetHeaderContent(const HeaderContentType &headerContent)
SWCMeshIOEnums::SWCPointData m_PointDataContent
CellsBufferContainerType::Pointer m_CellsBuffer
Superclass::StreamOffsetType StreamOffsetType
VectorContainer< ParentIdentifierType > ParentIdentifierContainerType
RadiusContainerType::Pointer m_Radii
TypeIdentifierContainerType::Pointer m_TypeIdentifiers
bool CanWriteFile(const char *fileName) override
void Write() override
SmartPointer< Self > Pointer
void WriteCells(void *buffer) override
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
SizeValueType IdentifierType
Definition itkIntTypes.h:95
detail::VectorContainer< std::conditional_t< std::is_void_v< T2 >, SizeValueType, T1 >, std::conditional_t< std::is_void_v< T2 >, T1, T2 > > VectorContainer
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, AnatomicalOrientation::CoordinateEnum value)
#define itkSetMacro(name, type)