VMTK
Loading...
Searching...
No Matches
vtkvmtkCurvedMPRImageFilter.cxx
Go to the documentation of this file.
2#include "vtkObjectFactory.h"
3#include "vtkVersion.h"
4
5//------------------------------------------------------------------------------
6
7
9
11{
12 this->Centerline = NULL;
13 this->InplaneOutputSpacing[0] = 1.0;
14 this->InplaneOutputSpacing[1] = 1.0;
15 this->InplaneOutputSize[0] = 100;
16 this->InplaneOutputSize[1] = 100;
17 this->ReslicingBackgroundLevel = 0.0;
18
19 for (int i = 0;i<6;i++)
20 {
21 this->OutputExtent[i] = 0;
22 }
23 for (int i = 0;i<3;i++)
24 {
25 this->OutputOrigin[i] = 0.0;
26 }
27 for (int i = 0;i<3;i++)
28 {
29 this->OutputSpacing[i] = 0.0;
30 }
31
32 this->FrenetTangentArrayName = NULL;
34}
35
36
38{
39 if (this->Centerline)
40 {
41 this->Centerline->Delete();
42 this->Centerline = NULL;
43 }
44
45 if (this->FrenetTangentArrayName)
46 {
47 delete[] this->FrenetTangentArrayName;
48 this->FrenetTangentArrayName = NULL;
49 }
50
52 {
55 }
56}
57
58//This is called by the superclass
59//----------------------------------------------------------------------------
61 vtkInformation * vtkNotUsed(request),
62 vtkInformationVector **inputVector,
63 vtkInformationVector *vtkNotUsed( outputVector ))
64{
65 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
66
67 // always request the whole extent
68 inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
69 inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),6);
70 return 1;
71}
72
73//----------------------------------------------------------------------------
74// Compute new extent
76 vtkInformation * vtkNotUsed(request),
77 vtkInformationVector **inputVector,
78 vtkInformationVector *outputVector)
79{
80 // get the info objects
81 vtkInformation* outInfo = outputVector->GetInformationObject(0);
82 vtkImageData *outputImage = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
83 vtkInformation *inInfoImage = inputVector[0]->GetInformationObject(0);
84 vtkImageData *inputImage = vtkImageData::SafeDownCast(inInfoImage->Get(vtkDataObject::DATA_OBJECT()));
85
86 if (!this->Centerline)
87 {
88 vtkErrorMacro(<<"Centerline not set");
89 return 1;
90 }
91
92 //Check if the inputs are NULL
93 if (inputImage == NULL || this->Centerline == NULL || outputImage==NULL)
94 {
95 if (outputImage)
96 {
97 int outputExtent[6] = {0,-1,0,-1,0,-1};
98 outputImage->SetExtent(outputExtent);
99 if (this->GetOutputInformation(0))
100 {
101 this->GetOutputInformation(0)->Set(
102 vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
103 this->GetOutputInformation(0)->Get(
104 vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()), 6);
105 }
106 outputImage->AllocateScalars(outInfo);
107
108 }
109 if ( inputImage == NULL)
110 {
111 vtkErrorMacro(<< "RequestInformation: InputImageData is not set.");
112 return 1;
113 }
114 else //Centerline==NULL
115 {
116 vtkErrorMacro(<< "RequestInformation: Centerline is not set.");
117 return 1;
118 }
119 }
120
121 int firstLineInCells;
122 vtkCell* line = NULL;
123 //get the first line in the vktPolyData
124 for (firstLineInCells=0; firstLineInCells < this->Centerline->GetNumberOfCells(); firstLineInCells++)
125 {
126 line = this->Centerline->GetCell(firstLineInCells);
127 if (line->GetCellType() == VTK_LINE || line->GetCellType() == VTK_POLY_LINE)
128 {
129 break;
130 }
131 }
132 if (!line || (!(firstLineInCells < (int) this->Centerline->GetNumberOfCells())))
133 {
134 vtkErrorMacro(<< "RequestInformation: no line in input vtkPolyData.");
135 return 1;
136 }
137
138 vtkPoints* linePoints = line->GetPoints();
139
140 int numberOfLinePoints = linePoints->GetNumberOfPoints();
141
142 if (numberOfLinePoints < 2)
143 {
144 vtkErrorMacro(<< "RequestInformation: Too few points in Centerline.");
145 return 1;
146 }
147
148 int inDataExtent[6];
149 double inDataOrigin[3];
150 inInfoImage->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), inDataExtent);
151
152 inputImage->GetSpacing(this->OutputSpacing);
153 inputImage->GetOrigin(inDataOrigin);
154
155 this->OutputExtent[0] = 0;
156 this->OutputExtent[1] = this->InplaneOutputSize[0]-1;
157 this->OutputExtent[2] = 0;
158 this->OutputExtent[3] = this->InplaneOutputSize[1]-1;
159 this->OutputExtent[4] = 0;
160 this->OutputExtent[5] = numberOfLinePoints-1;
161
162 double firstPoint[3], secondPoint[3], zspace;
163 linePoints->GetPoint(0,firstPoint);
164 linePoints->GetPoint(1,secondPoint);
165
166 zspace = sqrt(vtkMath::Distance2BetweenPoints(firstPoint,secondPoint));
167
168 this->OutputSpacing[0]= this->InplaneOutputSpacing[0];
169 this->OutputSpacing[1]= this->InplaneOutputSpacing[1];
170 this->OutputSpacing[2]= zspace;
171
172 this->OutputOrigin[0]=-1*((this->OutputExtent[1]-this->OutputExtent[0])/2)*this->OutputSpacing[0];
173 this->OutputOrigin[1]=-1*((this->OutputExtent[3]-this->OutputExtent[2])/2)*this->OutputSpacing[1];
174 this->OutputOrigin[2]=0;
175
176 outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),this->OutputExtent,6);
177 outInfo->Set(vtkDataObject::SPACING(),this->OutputSpacing,3);
178 outInfo->Set(vtkDataObject::ORIGIN(),this->OutputOrigin,3);
179
180 return 1;
181}
182
183template <class T>
184void vtkvmtkCurvedMPRImageFilter::FillSlice(T* outReslicePtr, T* outputImagePtr, int* resliceUpdateExtent, int* outExtent, vtkIdType* outputInc, int slice)
185{
186 //write result to the new dataset
187 int size = (outExtent[1]-outExtent[0]+1)*(outExtent[3]-outExtent[2]+1);
188 for(int i=0; i<size; i++)
189 {
190 outputImagePtr[i + (slice-outExtent[4]) * outputInc[2]] = outReslicePtr[i];
191 }
192}
193
194//----------------------------------------------------------------------------
196 vtkInformation* vtkNotUsed( request ),
197 vtkInformationVector** inputVector,
198 vtkInformationVector* outputVector)
199{
200 // get the info objects
201 vtkInformation* outInfo = outputVector->GetInformationObject(0);
202 vtkImageData *outputImage = vtkImageData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
203 vtkInformation *inInfoImage = inputVector[0]->GetInformationObject(0);
204 vtkImageData *inputImage = vtkImageData::SafeDownCast(inInfoImage->Get(vtkDataObject::DATA_OBJECT()));
205
206 if (!this->Centerline)
207 {
208 vtkErrorMacro(<<"Centerline not set");
209 return 1;
210 }
211
212 if (inputImage == NULL)
213 {
214 vtkErrorMacro(<< "RequestData: InputImageData is not set.");
215 return 1;
216 }
217
218 //check if everything is all right
219 if (inputImage == NULL || outputImage==NULL)
220 {
221 if (outputImage)
222 {
223 int outputExtent[6] = {0,-1,0,-1,0,-1};
224 outputImage->SetExtent(outputExtent);
225 if (this->GetOutputInformation(0))
226 {
227 this->GetOutputInformation(0)->Set(
228 vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
229 this->GetOutputInformation(0)->Get(
230 vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()), 6);
231 }
232 outputImage->AllocateScalars(outInfo);
233 }
234
235 }
236
237 //check if FrenetTangent s are available
238 if ( this->FrenetTangentArrayName == NULL)
239 {
240 vtkErrorMacro(<< "RequestData: FrenetTangentArrayName is not set.");
241 return 1;
242 }
243
244 vtkDataArray* frenetArray = this->Centerline->GetPointData()->GetArray(this->FrenetTangentArrayName);
245 if (!frenetArray)
246 {
247 vtkErrorMacro(<< "RequestData: InputPolyData does not contain an array with the specified FrenetTangentArrayName.");
248 return 1;
249 }
250
251 //check if ParallelTransportNormals are available
252 if ( this->ParallelTransportNormalsArrayName == NULL)
253 {
254 vtkErrorMacro(<< "RequestData: ParallelTransportNormalsArrayName is not set.");
255 return 1;
256 }
257
258 //check if a Centerline is available
259 vtkDataArray* parallelArray = this->Centerline->GetPointData()->GetArray(this->ParallelTransportNormalsArrayName);
260 if (!parallelArray)
261 {
262 vtkErrorMacro(<< "RequestData: InputPolyData does not contain an array with the specified ParallelTransportNormalsArrayName.");
263 return 1;
264 }
265
266 int firstLineInCells;
267 vtkCell* line = NULL;
268 //get the first line in the vktPolyData
269 for (firstLineInCells=0; firstLineInCells<this->Centerline->GetNumberOfCells(); firstLineInCells++)
270 {
271 line = this->Centerline->GetCell(firstLineInCells);
272 if (line->GetCellType() == VTK_LINE || line->GetCellType() == VTK_POLY_LINE)
273 {
274 break;
275 }
276 }
277
278 if (!line || (!(firstLineInCells < (int) this->Centerline->GetNumberOfCells())))
279 {
280 vtkErrorMacro(<< "RequestData: no line in input vtkPolyData.");
281 return 1;
282 }
283
284 if (line->GetNumberOfPoints() < 2)
285 {
286 vtkErrorMacro(<< "RequestData: Too few points in Centerline.");
287 return 1;
288 }
289
290 vtkPoints* linePoints = line->GetPoints();
291
292 int outExtent[6];
293 outInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), outExtent);
294
295 outputImage->SetExtent(outExtent);
296 if (this->GetOutputInformation(0))
297 {
298 this->GetOutputInformation(0)->Set(
299 vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
300 this->GetOutputInformation(0)->Get(
301 vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()), 6);
302 }
303
304 vtkDataArray* frenetTangentArray = this->Centerline->GetPointData()->GetArray(this->FrenetTangentArrayName);
305 vtkDataArray* parallelTransportNormalsArray = this->Centerline->GetPointData()->GetArray(this->ParallelTransportNormalsArrayName);
306
307 //start computing
308 int inExtent[6];
309 inInfoImage->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), inExtent);
310
311 // if the input extent is empty then exit
312 if (inExtent[1] < inExtent[0] ||
313 inExtent[3] < inExtent[2] ||
314 inExtent[5] < inExtent[4])
315 {
316 return 1;
317 }
318
319 vtkImageReslice* reslice = vtkImageReslice::New();
320 reslice->SetOutputDimensionality(2);
321 reslice->SetInputData(inputImage);
322 reslice->SetInterpolationModeToCubic();
323 //turn off transformation of the input spacing, origin and extent, so we can define what we want
324 reslice->TransformInputSamplingOff();
325 //set the value of the voxels that are out of the input data
326 reslice->SetBackgroundLevel(this->ReslicingBackgroundLevel);
327 //set the outputspacing
328 //reslice->SetOutputSpacing(this->OutputSpacing[0],this->OutputSpacing[1],this->OutputSpacing[2]);
329 reslice->SetOutputSpacing(this->OutputSpacing);
330// going to MPR
331 for (int slice=outExtent[4]; slice<(outExtent[5] + 1);slice++)
332 {// for each slice (or point on the line)
333 double centerSlice[3];
334 linePoints->GetPoint(slice,centerSlice);
335
336 // To calculate the outputorigin & the necessary transform
337 // the vectors are retrieved from the array's
338 //t is the vector in the direction of the Centerline, so along the z-axis in the MPR volume
339 double t[3];
340 frenetTangentArray->GetTuple(slice,t);
341
342 //p is a normal of the Centerline, directed to the 'North' direction of the inputvolume,in the MPR volume this will be along the x-axis
343 double p[3];
344 parallelTransportNormalsArray->GetTuple(slice,p);
345
346 double tp[3];
347 //tp is the crossproduct of t and p, and will be directed to the 'West' direction of the inputvolume,in the MPR volume this will be along the y-axis
348 tp[0] = (t[1]*p[2]- t[2]*p[1]);
349 tp[1] = (t[2]*p[0]- t[0]*p[2]);
350 tp[2] = (t[0]*p[1]- t[1]*p[0]);
351
352 //set the axis of the first slice according to the vectors of the first point in the line
353 reslice->SetResliceAxesDirectionCosines(p[0],p[1],p[2],tp[0],tp[1],tp[2],t[0],t[1],t[2]);
354 // the firstPoint on the Centerline corresponds to the origin of the output axes unit vectors
355 reslice->SetResliceAxesOrigin(centerSlice[0],centerSlice[1],centerSlice[2]);
356 //the outputextent will be one slice
357 reslice->SetOutputExtent(this->OutputExtent[0],this->OutputExtent[1],this->OutputExtent[2],this->OutputExtent[3],0,0);
358 //the outputorigin in the input coordinate system is set to zero
359 double outputorigin[3];
360 outputorigin[0] = 0.0;
361 outputorigin[1] = 0.0;
362 outputorigin[2] = 0.0;
363
364 reslice->SetOutputOrigin(this->OutputOrigin);
365 reslice->Update();
366
367 int resliceUpdateExtent[6];
368 resliceUpdateExtent[0] = outExtent[0];
369 resliceUpdateExtent[1] = outExtent[1];
370 resliceUpdateExtent[2] = outExtent[2];
371 resliceUpdateExtent[3] = outExtent[3];
372 resliceUpdateExtent[4] = 0;
373 resliceUpdateExtent[5] = 0;
374
375 vtkIdType outputInc[3];
376 outputImage->GetIncrements(outputInc);
377
378 int scalarType = reslice->GetOutput()->GetScalarType();
379 switch (scalarType)
380 {
381 vtkTemplateMacro(
382 FillSlice(
383 static_cast<VTK_TT*>(reslice->GetOutput()->GetScalarPointerForExtent(resliceUpdateExtent)),
384 static_cast<VTK_TT*>(outputImage->GetScalarPointerForExtent(outExtent)),
385 resliceUpdateExtent, outExtent, outputInc, slice));
386 }
387 }// for each slice
388
389 reslice->Delete();
390 return 1;
391}
392
393void vtkvmtkCurvedMPRImageFilter::PrintSelf(std::ostream& os, vtkIndent indent)
394{
395 this->Superclass::PrintSelf(os,indent);
396
397 os << indent << "Centerline: " << this->Centerline << "\n";
398
399 os << indent << "InplaneOutputSpacing: (" << this->InplaneOutputSpacing[0] << " " << this->InplaneOutputSpacing[1] << ")\n";
400 os << indent << "InplaneOutputSize: (" << this->InplaneOutputSize[0] << " " << this->InplaneOutputSize[1] << ")\n";
401 os << indent << "OutputSpacing: (" << this->OutputSpacing[0] << ", "
402 << this->OutputSpacing[1] << ", " << this->OutputSpacing[2] << ")\n";
403 os << indent << "ReslicingBackgroundLevel: (" << this->ReslicingBackgroundLevel << ")\n";
404 os << indent << "OutputExtent: (" << this->OutputExtent[0] << ", "
405 << this->OutputExtent[1] << ", " << this->OutputExtent[2] << ")\n";
406 os << indent << "OutputExtent(3-5): (" << this->OutputExtent[3] << ", "
407 << this->OutputExtent[4] << ", " << this->OutputExtent[5] << ")\n";
408 os << indent << "OutputOrigin: (" << this->OutputOrigin[0] << ", "
409 << this->OutputOrigin[1] << ", " << this->OutputOrigin[2] << ")\n";
410 os << indent << "OutputSpacing: (" << this->OutputSpacing[0] << ", "
411 << this->OutputSpacing[1] << ", " << this->OutputSpacing[2] << ")\n";
412}
413
414
Creates a multiplanar reconstruction of an image along a centerline path.
void FillSlice(T *outReslicePtr, T *outputImagePtr, int *resliceUpdateExtent, int *outExtent, vtkIdType *outputInc, int slice)
virtual int RequestInformation(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestUpdateExtent(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkCurvedMPRImageFilter)