DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkFiberTractsWriter.hxx
Go to the documentation of this file.
1 
11 #ifndef __itkFiberTractsWriter_hxx
12 #define __itkFiberTractsWriter_hxx
13 
14 #include "itkFiberTractsWriter.h"
15 #include "utlDMRI.h"
16 
17 namespace itk
18 {
19 
21 {
23  if (format==TRACTS_TRK)
25  else if (format==TRACTS_TCK)
27  else if (format==TRACTS_VTK)
28  {
29  utlGlobalException(true, "use MeshFromTracts and itk::MeshFromFiberTractsFilter to write vtk file.");
30  // WriteTractsVTK();
31  }
32  else
33  {
34  utlGlobalException(true, "un-supported tract format");
35  }
36 }
37 
39 {
40  auto header = m_FiberTracts->GetHeader();
41  auto fibers = m_FiberTracts->GetFibers();
42 
43  FILE* file;
44  file = fopen(m_FileName.c_str(), "wb");
45  itk::WriteTrackVisHeader(*header, file);
46 
47  int n_count = header->n_count;
48  // int n_p = header->n_properties;
49  // int n_s = header->n_scalars;
50  int dim_p = itk::GetDimensionOfProperties(*header);
51  int dim_s = itk::GetDimensionOfScalars(*header);
52 
53  long offset=1000;
54  fseek (file, offset, SEEK_SET);
55  int numPoints;
56  float val[3+dim_s], val2[dim_p];
57  VertexType vertex;
58  for ( int n = 0; n < n_count; ++n )
59  {
60  FiberPointer fiber = fibers->GetElement(n);
61  utlSAGlobalException(dim_p!=fiber->GetDimensionOfProperties())(n)(dim_p)(fiber->GetDimensionOfProperties()).msg("dim_p in header is different from dim_p in fiber");
62  utlSAGlobalException(dim_s!=fiber->GetDimensionOfScalarsPerPoint())(n)(dim_s)(fiber->GetDimensionOfScalarsPerPoint()).msg("dim_s in header is different from dim_s in fiber");
63 
64  numPoints = fiber->GetNumberOfPoints();
65  fwrite((char*)&numPoints, sizeof(int), 1, file);
66 
67  auto tract = fiber->GetTract();
68  auto properties = fiber->GetProperties();
69  auto scalars = fiber->GetScalars();
70  auto vertexList = tract->GetVertexList();
71  for ( int i = 0; i < numPoints; ++i )
72  {
73  vertex = (*vertexList)[i];
74  const STDVectorType& ss = (*scalars)[i];
75  for ( int j = 0; j < 3; ++j )
76  val[j] = vertex[j];
77  for ( int j = 0; j < dim_s; ++j )
78  val[j+3] = ss[j];
79  fwrite((char*)&val, sizeof(float)*(3+dim_s), 1, file);
80  }
81  for ( int j = 0; j < dim_p; ++j )
82  val2[j+3] = (*properties)[j];
83  fwrite((char*)&val2, sizeof(float)*dim_p, 1, file);
84  }
85 
86  fclose (file);
87 }
88 
90 {
91  utlGlobalException(true, "TODO: read tracts in tck format");
92 }
93 
94 // void FiberTractsWriter::WriteTractsVTK()
95 // {
96 // }
97 
98 }
99 
100 
101 #endif
102 
103 
104 
int GetFiberTractsFormatFromFileExtension(const std::string &filename)
Definition: utlDMRI.h:520
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
FiberTractsPointer m_FiberTracts
int GetDimensionOfScalars(const TrackVisHeaderType &header)
Created "07-23-2017.
FiberType::VertexType VertexType
void WriteTrackVisHeader(const TrackVisHeaderType &header, FILE *file)
FiberTractsType::FiberPointer FiberPointer
FiberType::STDVectorType STDVectorType
int GetDimensionOfProperties(const TrackVisHeaderType &header)