Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTrackvis.cpp
Go to the documentation of this file.
1 #include <mitkTrackvis.h>
2 #include <vtkTransformPolyDataFilter.h>
3 
4 TrackVisFiberReader::TrackVisFiberReader() { m_Filename = ""; m_FilePointer = nullptr; }
5 
6 TrackVisFiberReader::~TrackVisFiberReader() { if (m_FilePointer) fclose( m_FilePointer ); }
7 
8 
9 // Create a TrackVis file and store standard metadata. The file is ready to append fibers.
10 // ---------------------------------------------------------------------------------------
12 {
13  // prepare the header
14  for(int i=0; i<3 ;i++)
15  {
16  if (fib->GetReferenceGeometry().IsNotNull())
17  {
18  m_Header.dim[i] = fib->GetReferenceGeometry()->GetExtent(i);
19  m_Header.voxel_size[i] = fib->GetReferenceGeometry()->GetSpacing()[i];
20  m_Header.origin[i] = fib->GetReferenceGeometry()->GetOrigin()[i];
21  }
22  else
23  {
24  m_Header.dim[i] = fib->GetGeometry()->GetExtent(i);
25  m_Header.voxel_size[i] = fib->GetGeometry()->GetSpacing()[i];
26  m_Header.origin[i] = fib->GetGeometry()->GetOrigin()[i];
27  }
28  }
29  m_Header.n_scalars = 0;
31  sprintf(m_Header.voxel_order,"LPS");
38  m_Header.pad1[0] = 0;
39  m_Header.pad1[1] = 0;
40  m_Header.pad2[0] = 0;
41  m_Header.pad2[1] = 0;
42  m_Header.invert_x = 0;
43  m_Header.invert_y = 0;
44  m_Header.invert_z = 0;
45  m_Header.swap_xy = 0;
46  m_Header.swap_yz = 0;
47  m_Header.swap_zx = 0;
48  m_Header.n_count = 0;
49  m_Header.version = 1;
50  m_Header.hdr_size = 1000;
51 
52  // write the header to the file
53  m_FilePointer = fopen(filename.c_str(),"w+b");
54  if (m_FilePointer == nullptr)
55  {
56  printf("[ERROR] Unable to create file '%s'\n",filename.c_str());
57  return 0;
58  }
59  sprintf(m_Header.id_string,"TRACK");
60  if (fwrite((char*)&m_Header, 1, 1000, m_FilePointer) != 1000)
61  MITK_ERROR << "TrackVis::create : Error occurding during writing fiber.";
62 
63  this->m_Filename = filename;
64 
65  return 1;
66 }
67 
68 
69 // Open an existing TrackVis file and read metadata information.
70 // The file pointer is positiond at the beginning of fibers data
71 // -------------------------------------------------------------
73 {
74  m_FilePointer = fopen(filename.c_str(),"r+b");
75  if (m_FilePointer == nullptr)
76  {
77  printf("[ERROR] Unable to open file '%s'\n",filename.c_str());
78  return 0;
79  }
80  this->m_Filename = filename;
81 
82  return fread((char*)(&m_Header), 1, 1000, m_FilePointer);
83 }
84 
85 
86 
87 // Append a fiber to the file
88 // --------------------------
90 {
91  vtkPolyData* poly = fib->GetFiberPolyData();
92  for (int i=0; i<fib->GetNumFibers(); i++)
93  {
94  vtkCell* cell = poly->GetCell(i);
95  int numPoints = cell->GetNumberOfPoints();
96  vtkPoints* points = cell->GetPoints();
97 
98  unsigned int numSaved, pos = 0;
99  //float* tmp = new float[3*maxSteps];
100  std::vector< float > tmp;
101  tmp.reserve(3*numPoints);
102 
103  numSaved = numPoints;
104  for(unsigned int i=0; i<numSaved ;i++)
105  {
106  double* p = points->GetPoint(i);
107 
108  tmp[pos++] = p[0];
109  tmp[pos++] = p[1];
110  tmp[pos++] = p[2];
111  }
112 
113  // write the coordinates to the file
114  if ( fwrite((char*)&numSaved, 1, 4, m_FilePointer) != 4 )
115  {
116  printf( "[ERROR] Problems saving the fiber!\n" );
117  return 1;
118  }
119  if ( fwrite((char*)&(tmp.front()), 1, 4*pos, m_FilePointer) != 4*pos )
120  {
121  printf( "[ERROR] Problems saving the fiber!\n" );
122  return 1;
123  }
124  }
125 
126  return 0;
127 }
128 
132 {
133  int numPoints;
134  vtkSmartPointer<vtkPoints> vtkNewPoints = vtkSmartPointer<vtkPoints>::New();
135  vtkSmartPointer<vtkCellArray> vtkNewCells = vtkSmartPointer<vtkCellArray>::New();
136 
137  while (fread((char*)&numPoints, 1, 4, m_FilePointer)==4)
138  {
139  if ( numPoints <= 0 )
140  {
141  printf( "[ERROR] Trying to read a fiber with %d points!\n", numPoints );
142  return -1;
143  }
144  vtkSmartPointer<vtkPolyLine> container = vtkSmartPointer<vtkPolyLine>::New();
145 
146  float tmp[3];
147  for(int i=0; i<numPoints; i++)
148  {
149  if (fread((char*)tmp, 1, 12, m_FilePointer) == 0)
150  MITK_ERROR << "TrackVis::read: Error during read.";
151 
152  vtkIdType id = vtkNewPoints->InsertNextPoint(tmp);
153  container->GetPointIds()->InsertNextId(id);
154  }
155  vtkNewCells->InsertNextCell(container);
156  }
157 
158  vtkSmartPointer<vtkPolyData> fiberPolyData = vtkSmartPointer<vtkPolyData>::New();
159  fiberPolyData->SetPoints(vtkNewPoints);
160  fiberPolyData->SetLines(vtkNewCells);
161 
162  MITK_INFO << "Coordinate convention: " << m_Header.voxel_order;
163 
165  vtkSmartPointer< vtkMatrix4x4 > matrix = vtkSmartPointer< vtkMatrix4x4 >::New();
166  matrix->Identity();
167 
168  if (m_Header.voxel_order[0]=='R')
169  matrix->SetElement(0,0,-matrix->GetElement(0,0));
170  if (m_Header.voxel_order[1]=='A')
171  matrix->SetElement(1,1,-matrix->GetElement(1,1));
172  if (m_Header.voxel_order[2]=='I')
173  matrix->SetElement(2,2,-matrix->GetElement(2,2));
174 
175  geometry->SetIndexToWorldTransformByVtkMatrix(matrix);
176 
177  vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
178  transformFilter->SetInputData(fiberPolyData);
179  transformFilter->SetTransform(geometry->GetVtkTransform());
180  transformFilter->Update();
181  fib->SetFiberPolyData(transformFilter->GetOutput());
182 
183  mitk::Point3D origin;
184  origin[0]=m_Header.origin[0];
185  origin[1]=m_Header.origin[1];
186  origin[2]=m_Header.origin[2];
187  geometry->SetOrigin(origin);
188 
189  mitk::Vector3D spacing;
190  spacing[0]=m_Header.voxel_size[0];
191  spacing[1]=m_Header.voxel_size[1];
192  spacing[2]=m_Header.voxel_size[2];
193  geometry->SetSpacing(spacing);
194 
195  geometry->SetExtentInMM(0, m_Header.voxel_size[0]*m_Header.dim[0]);
196  geometry->SetExtentInMM(1, m_Header.voxel_size[1]*m_Header.dim[1]);
197  geometry->SetExtentInMM(2, m_Header.voxel_size[2]*m_Header.dim[2]);
198 
199  fib->SetReferenceGeometry(dynamic_cast<mitk::BaseGeometry*>(geometry.GetPointer()));
200 
201  return numPoints;
202 }
203 
204 
205 
206 // Update the field in the header to the new FIBER TOTAL.
207 // ------------------------------------------------------
208 void TrackVisFiberReader::updateTotal( int totFibers )
209 {
210  fseek(m_FilePointer, 1000-12, SEEK_SET);
211  if (fwrite((char*)&totFibers, 1, 4, m_FilePointer) != 4)
212  MITK_ERROR << "[ERROR] Problems saving the fiber!";
213 }
214 
215 
217 {
218  fseek(m_FilePointer, 0, SEEK_SET);
219  if (fwrite((char*)&m_Header, 1, 1000, m_FilePointer) != 1000)
220  MITK_ERROR << "[ERROR] Problems saving the fiber!";
221 }
222 
223 
224 // Close the TrackVis file, but keep the metadata in the header.
225 // -------------------------------------------------------------
227 {
228  fclose(m_FilePointer);
229  m_FilePointer = nullptr;
230 }
231 
233 {
234  if (fabs(m_Header.image_orientation_patient[0])<=0.001 || fabs(m_Header.image_orientation_patient[3])<=0.001 || fabs(m_Header.image_orientation_patient[5])<=0.001)
235  return false;
236  return true;
237 }
ScalarType GetExtent(unsigned int direction) const
Set the time bounds (in ms)
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
unsigned char swap_yz
Definition: mitkTrackvis.h:51
unsigned char invert_z
Definition: mitkTrackvis.h:49
short int n_properties
Definition: mitkTrackvis.h:40
#define MITK_INFO
Definition: mitkLogMacros.h:22
short int dim[3]
Definition: mitkTrackvis.h:35
#define MITK_ERROR
Definition: mitkLogMacros.h:24
short open(string m_Filename)
float origin[3]
Definition: mitkTrackvis.h:37
short int n_scalars
Definition: mitkTrackvis.h:38
virtual mitk::BaseGeometry::Pointer GetReferenceGeometry() const
char id_string[6]
Definition: mitkTrackvis.h:34
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
static Pointer New()
short append(const mitk::FiberBundle *fib)
unsigned char invert_x
Definition: mitkTrackvis.h:47
char voxel_order[4]
Definition: mitkTrackvis.h:43
unsigned char swap_xy
Definition: mitkTrackvis.h:50
unsigned char invert_y
Definition: mitkTrackvis.h:48
vtkSmartPointer< vtkPolyData > GetFiberPolyData() const
static const std::string filename
short create(string m_Filename, const mitk::FiberBundle *fib)
Base Class for Fiber Bundles;.
TrackVis_header m_Header
Definition: mitkTrackvis.h:67
unsigned char swap_zx
Definition: mitkTrackvis.h:52
short read(mitk::FiberBundle *fib)
float image_orientation_patient[6]
Definition: mitkTrackvis.h:45
virtual void SetReferenceGeometry(mitk::BaseGeometry::Pointer _arg)
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:129
void updateTotal(int totFibers)
virtual int GetNumFibers()
void SetFiberPolyData(vtkSmartPointer< vtkPolyData >, bool updateGeometry=true)
float voxel_size[3]
Definition: mitkTrackvis.h:36
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.