17 #ifndef __itkPolylineMaskImageFilter_txx
18 #define __itkPolylineMaskImageFilter_txx
38 template <
class TInputImage,
class TPolyline,
class TVector,
43 this->SetNumberOfRequiredInputs( 2 );
46 m_CameraCenterPoint.Fill(0);
47 m_FocalDistance = 0.0;
48 m_FocalPoint.Fill( 0.0 );
52 if( (TInputImage::ImageDimension != 3) ||
53 (TOutputImage::ImageDimension !=3 ) )
55 itkExceptionMacro( <<
"PolylineMaskImageFilter must be templated over "
56 <<
"input and output images of dimension 3" );
60 if(TVector::Length != 3)
62 itkExceptionMacro( <<
"PolylineMaskImageFilter must be templated over "
63 <<
"a view vector of length 3" );
70 template <
class TInputImage,
class TPolyline,
class TVector,
77 const_cast< InputImageType * >( input ) );
83 template <
class TInputImage,
class TPolyline,
class TVector,
90 const_cast< PolylineType * >( input ) );
96 template <
class TInputImage,
class TPolyline,
class TVector,
105 nUpVector = m_UpVector;
106 nUpVector.Normalize();
108 nViewVector = m_ViewVector;
109 nViewVector.Normalize();
111 itkDebugMacro(<<
"Normalized Up vector" <<nUpVector);
112 itkDebugMacro(<<
"Normalized View vector"<<nViewVector);
115 TVector nOrthogonalVector;
117 nOrthogonalVector = nUpVector - (nViewVector*(nUpVector*nViewVector));
119 itkDebugMacro(<<
"Up vector component orthogonal to View vector "<<nOrthogonalVector);
125 nThirdAxis =
CrossProduct(nOrthogonalVector,nViewVector);
127 itkDebugMacro(<<
"Third basis vector"<<nThirdAxis);
132 m_RotationMatrix[0][0] = nThirdAxis[0];
133 m_RotationMatrix[0][1] = nThirdAxis[1];
134 m_RotationMatrix[0][2] = nThirdAxis[2];
136 m_RotationMatrix[1][0] = nOrthogonalVector[0];
137 m_RotationMatrix[1][1] = nOrthogonalVector[1];
138 m_RotationMatrix[1][2] = nOrthogonalVector[2];
140 m_RotationMatrix[2][0] = nViewVector[0];
141 m_RotationMatrix[2][1] = nViewVector[1];
142 m_RotationMatrix[2][2] = nViewVector[2];
149 template<
class TInputImage,
class TPolyline,
class TVector,
class TOutputImage>
151 TOutputImage>::ProjPlanePointType
162 centered[i] = inputPoint[i] - m_CameraCenterPoint[i];
165 PointType rotated = m_RotationMatrix * centered;
169 double factor = m_FocalDistance / ( rotated[2] );
171 result[0] = m_FocalPoint[0] + (rotated[0] * factor);
172 result[1] = m_FocalPoint[1] + (rotated[1] * factor);
182 template <
class TInputImage,
class TPolyline,
class TVector,
188 typedef typename TInputImage::SizeType InputImageSizeType;
189 typedef typename TInputImage::PointType InputImagePointType;
190 typedef typename TInputImage::SpacingType InputImageSpacingType;
193 typedef typename TOutputImage::IndexType ImageIndexType;
194 typedef typename TOutputImage::PixelType PixelType;
198 typedef typename TPolyline::Pointer PolylinePointer;
199 typedef typename TPolyline::VertexType VertexType;
200 typedef typename TPolyline::VertexListType VertexListType;
201 typedef typename TPolyline::IndexType PolylineIndexType;
205 typename TInputImage::ConstPointer inputImagePtr(
206 dynamic_cast<const TInputImage * >(
208 typename TPolyline::ConstPointer polylinePtr(
209 dynamic_cast<const TPolyline * >(
211 typename TOutputImage::Pointer outputImagePtr(
212 dynamic_cast<TOutputImage * >(
216 OriginType originInput;
217 originInput.Fill(0.0);
219 outputImagePtr->SetOrigin(originInput);
220 outputImagePtr->SetSpacing(inputImagePtr->GetSpacing());
221 outputImagePtr->SetDirection(inputImagePtr->GetDirection());
223 outputImagePtr->SetRequestedRegion( inputImagePtr->GetRequestedRegion() );
224 outputImagePtr->SetBufferedRegion( inputImagePtr->GetBufferedRegion() );
225 outputImagePtr->SetLargestPossibleRegion( inputImagePtr->GetLargestPossibleRegion() );
226 outputImagePtr->Allocate();
227 outputImagePtr->FillBuffer(0);
229 InputImageConstIteratorType inputIt(inputImagePtr,inputImagePtr->GetLargestPossibleRegion());
230 OutputImageIteratorType outputIt(outputImagePtr,outputImagePtr->GetLargestPossibleRegion());
233 typedef typename InterpolatorType::OutputType OutputType;
234 typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
235 typedef typename InterpolatorType::PointType InterpolatorPointType;
239 this->GenerateRotationMatrix();
242 InterpolatorPointType inputPoint;
247 typedef typename ProjectionImageType::IndexType ProjectionImageIndexType;
248 typedef typename ProjectionImageType::IndexValueType ProjectionImageIndexValueType;
249 typedef typename ProjectionImageType::PointType ProjectionImagePointType;
250 typedef typename ProjectionImageType::SpacingType ProjectionImageSpacingType;
251 typedef typename ProjectionImageType::PixelType ProjectionImagePixelType;
252 typedef typename ProjectionImageType::RegionType ProjectionImageRegionType;
253 typedef typename ProjectionImageType::SizeType ProjectionImageSizeType;
256 ProjectionImageRegionType projectionRegion;
262 InputImageSizeType inputImageSize;
267 typedef BoundingBoxType::PointsContainer CornerPointProjectionContainer;
269 CornerPointProjectionContainer::Pointer cornerPointProjectionlist = CornerPointProjectionContainer::New();
270 CornerPointType cornerPoint;
271 CornerPointType originPoint;
272 CornerPointProjectionType cornerProjectionPoint;
274 originPoint[0] = 0.0;
275 originPoint[1] = 0.0;
276 originPoint[2] = 0.0;
278 originPoint = inputImagePtr->GetOrigin();
279 inputImageSize = inputImagePtr->GetLargestPossibleRegion().GetSize();
283 cornerPoint = originPoint;
284 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
285 cornerPointProjectionlist->push_back(cornerProjectionPoint);
290 cornerPoint[0] = originPoint[0];
291 cornerPoint[1] = originPoint[1];
292 cornerPoint[2] = originPoint[2] + inputImageSize[2];
293 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
294 cornerPointProjectionlist->push_back(cornerProjectionPoint);
299 cornerPoint[0] = originPoint[0];
300 cornerPoint[1] = originPoint[1] + inputImageSize[1];
301 cornerPoint[2] = originPoint[2];
302 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
303 cornerPointProjectionlist->push_back(cornerProjectionPoint);
309 cornerPoint[0] = originPoint[0];
310 cornerPoint[1] = originPoint[1] + inputImageSize[1];
311 cornerPoint[2] = originPoint[2] + inputImageSize[2];
312 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
313 cornerPointProjectionlist->push_back(cornerProjectionPoint);
318 cornerPoint[0] = originPoint[0] + inputImageSize[0];
319 cornerPoint[1] = originPoint[1];
320 cornerPoint[2] = originPoint[2];
322 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
323 cornerPointProjectionlist->push_back(cornerProjectionPoint);
328 cornerPoint[0] = originPoint[0] + inputImageSize[0];
329 cornerPoint[1] = originPoint[1];
330 cornerPoint[2] = originPoint[2] + inputImageSize[2];
332 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
333 cornerPointProjectionlist->push_back(cornerProjectionPoint);
338 cornerPoint[0] = originPoint[0] + inputImageSize[0];
339 cornerPoint[1] = originPoint[1] + inputImageSize[1];
340 cornerPoint[2] = originPoint[2];
342 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
343 cornerPointProjectionlist->push_back(cornerProjectionPoint);
349 cornerPoint[0] = originPoint[0] + inputImageSize[0];
350 cornerPoint[1] = originPoint[1] + inputImageSize[1];
351 cornerPoint[2] = originPoint[2] + inputImageSize[2];
353 cornerProjectionPoint = this->TransformProjectPoint(cornerPoint);
354 cornerPointProjectionlist->push_back(cornerProjectionPoint);
359 BoundingBoxType::Pointer boundingBox = BoundingBoxType::New();
361 boundingBox->SetPoints ( cornerPointProjectionlist );
363 if ( !boundingBox->ComputeBoundingBox() )
365 itkExceptionMacro(<<
"Bounding box computation error");
368 const BoundingBoxType::BoundsArrayType & bounds = boundingBox->GetBounds();
369 itkDebugMacro(<<
"Projection image bounding box="<<bounds);
371 ProjectionImageIndexType projectionStart;
372 projectionStart[0] = 0;
373 projectionStart[1] = 0;
375 ProjectionImageSizeType projectionSize;
376 ProjectionImageIndexValueType pad;
380 projectionSize[0] = (ProjectionImageIndexValueType) (bounds[1]-bounds[0]) + pad;
381 projectionSize[1] = (ProjectionImageIndexValueType) (bounds[3]-bounds[2]) + pad;
383 projectionRegion.SetIndex(projectionStart);
384 projectionRegion.SetSize(projectionSize);
386 typename ProjectionImageType::Pointer projectionImagePtr = ProjectionImageType::New();
388 ProjectionImagePointType origin;
389 origin[0] = bounds[0];
390 origin[1] = bounds[2];
392 projectionImagePtr->SetOrigin(origin);
394 ProjectionImageSpacingType spacing;
399 projectionImagePtr->SetSpacing(spacing);
401 itkDebugMacro(<<
"Projection image size:"<<projectionSize);
402 itkDebugMacro(<<
"Projection image start index:"<<projectionStart);
403 itkDebugMacro(<<
"Projection image origin:"<<origin);
405 projectionImagePtr->SetRequestedRegion( projectionRegion );
406 projectionImagePtr->SetBufferedRegion( projectionRegion );
407 projectionImagePtr->SetLargestPossibleRegion( projectionRegion );
408 projectionImagePtr->Allocate();
409 projectionImagePtr->FillBuffer(0);
412 ProjectionImageIteratorType projectionIt(projectionImagePtr,projectionImagePtr->GetLargestPossibleRegion());
414 itkDebugMacro(<<
"Rotation matrix"<<std::cout<<m_RotationMatrix);
416 typedef typename VertexListType::Pointer VertexListPointer;
418 const VertexListType * container = polylinePtr->GetVertexList();
420 typename VertexListType::ConstIterator piter = container->Begin();
424 VertexType startIndex;
426 VertexType projectionIndex;
427 VertexType pstartIndex;
433 PixelType u_val =
static_cast<ProjectionImagePixelType
> (0);
434 PixelType b_val =
static_cast<ProjectionImagePixelType
> (2);
435 PixelType f_val =
static_cast<ProjectionImagePixelType
> (255);
437 projectionImagePtr->FillBuffer(u_val);
440 pstartIndex = piter.Value();
441 projectionIndex = pstartIndex;
443 ProjectionImageIndexType startImageIndex;
444 ProjectionImageIndexType endImageIndex;
445 ProjectionImageIndexType projectionImageIndex;
447 typedef typename ProjectionImageIndexType::IndexValueType IndexValueType;
452 ImageLineIteratorType imit(projectionImagePtr, projectionImagePtr->GetLargestPossibleRegion());
455 while ( piter != container->End() )
458 startIndex = projectionIndex;
459 endIndex = piter.Value();
461 for(
unsigned int i=0; i < ProjectionImageType::ImageDimension; i++ )
463 startImageIndex[i] =
static_cast<IndexValueType
> (startIndex[i]);
464 endImageIndex[i] =
static_cast<IndexValueType
> (endIndex[i]);
468 if (endImageIndex[1] > startImageIndex[1])
473 itkDebugMacro(<<
"Polyline:"<<startImageIndex<<
","<<endImageIndex);
474 LineIteratorType it(projectionImagePtr, startImageIndex, endImageIndex);
477 while (!it.IsAtEnd())
479 projectionImageIndex[0] = it.GetIndex()[0];
480 projectionImageIndex[1] = it.GetIndex()[1];
483 imit.SetIndex(projectionImageIndex);
484 while ( ! imit.IsAtEndOfLine() )
488 if ( imit.Get() == u_val)
500 projectionIndex = endIndex;
506 startIndex = projectionIndex;
507 endIndex = pstartIndex;
509 for(
unsigned int i=0; i < ProjectionImageType::ImageDimension; i++ )
511 startImageIndex[i] =
static_cast<IndexValueType
> (startIndex[i]);
512 endImageIndex[i] =
static_cast<IndexValueType
> (endIndex[i]);
515 if (endImageIndex[1] > startImageIndex[1])
520 LineIteratorType it(projectionImagePtr, startImageIndex, endImageIndex);
523 while (!it.IsAtEnd())
525 projectionImageIndex[0] = it.GetIndex()[0];
526 projectionImageIndex[1] = it.GetIndex()[1];
529 imit.SetIndex(projectionImageIndex);
530 while ( ! imit.IsAtEndOfLine() )
534 if ( imit.Get() == u_val)
550 outputIt.GoToBegin();
553 InputImageSpacingType inputImageSpacing;
554 InputImagePointType inputImageOrigin;
556 inputImageSpacing = inputImagePtr->GetSpacing();
557 inputImageOrigin = inputImagePtr->GetOrigin();
558 inputImageSize = inputImagePtr->GetLargestPossibleRegion().GetSize();
560 while ( !inputIt.IsAtEnd() )
562 outputImagePtr->TransformIndexToPhysicalPoint( outputIt.GetIndex(), inputPoint );
563 outputPoint = this->TransformProjectPoint(inputPoint);
564 projectionImagePtr->TransformPhysicalPointToIndex(outputPoint,projectionImageIndex);
570 if ( ! projectionImagePtr->GetBufferedRegion().IsInside(projectionImageIndex))
572 itkExceptionMacro(<<
"Projection Image index out of bound:"<<projectionImageIndex);
575 if(projectionImagePtr->GetPixel(projectionImageIndex) == f_val)
577 outputIt.Set(inputIt.Get());
589 template <
class TInputImage,
class TPolyline,
class TVector,
594 Superclass::PrintSelf(os,indent);
595 os << indent <<
"Viewing vector: "
596 <<
static_cast<typename NumericTraits<VectorType>::PrintType
>(m_ViewVector)
598 os << indent <<
"Up Vector: "
599 <<
static_cast<typename NumericTraits<VectorType>::PrintType
>(m_UpVector)
601 os << indent <<
"Camera Center Point: " << m_CameraCenterPoint << std::endl;
602 os << indent <<
"Focal Point : " << m_FocalPoint << std::endl;
603 os << indent <<
"Focal Distance : " << m_FocalDistance << std::endl;
604 os << indent <<
"Rotation matrix : " << m_RotationMatrix << std::endl;