1 /*=Plus=header=begin======================================================
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
11 //----------------------------------------------------------------------------
13 PlusStatus SafeGetAttributeValueInsensitive(vtkXMLDataElement& element, const std::wstring& attributeName, T& value)
15 return PlusCommon::XML::SafeGetAttributeValueInsensitive(element, std::string(begin(attributeName), end(attributeName)), value);
18 //----------------------------------------------------------------------------
20 PlusStatus SafeGetAttributeValueInsensitive(vtkXMLDataElement& element, const std::string& attributeName, T& value)
22 if (element.GetAttribute(attributeName.c_str()) == NULL)
26 element.GetScalarAttribute(attributeName.c_str(), value);
31 //----------------------------------------------------------------------------
32 template<typename ElemType>
33 void JoinTokensIntoString(const std::vector<ElemType>& elems, std::string& output, char separator)
37 typedef std::vector<ElemType> List;
39 for (typename List::const_iterator it = elems.begin(); it != elems.end(); ++it)
42 if (it != elems.end() - 1)
51 //----------------------------------------------------------------------------
52 template<typename ScalarType>
53 PlusStatus DeepCopyVtkVolumeToItkVolume( vtkImageData* inFrame, typename itk::Image< ScalarType, 3 >::Pointer outFrame )
55 LOG_TRACE("PlusCommon::ConvertVtkImageToItkImage");
57 if ( inFrame == NULL )
59 LOG_ERROR("Failed to convert vtk image to itk image - input image is NULL!");
63 if ( outFrame.IsNull() )
65 LOG_ERROR("Failed to convert vtk image to itk image - output image is NULL!");
69 if( igsioVideoFrame::GetNumberOfBytesPerScalar(inFrame->GetScalarType()) != sizeof(ScalarType) )
71 LOG_ERROR("Mismatch between input and output scalar types. In: " << igsioVideoFrame::GetStringFromVTKPixelType(inFrame->GetScalarType()));
75 // convert vtkImageData to itkImage
76 vtkSmartPointer<vtkImageExport> imageExport = vtkSmartPointer<vtkImageExport>::New();
77 imageExport->SetInputData(inFrame);
78 imageExport->Update();
80 int extent[6]={0,0,0,0,0,0};
81 inFrame->GetExtent(extent);
83 double width = extent[1] - extent[0] + 1;
84 double height = extent[3] - extent[2] + 1;
85 double thickness = extent[5] - extent[4] + 1;
86 typename itk::Image< ScalarType, 3 >::SizeType size;
90 typename itk::Image< ScalarType, 3 >::IndexType start;
94 typename itk::Image< ScalarType, 3 >::RegionType region;
96 region.SetIndex(start);
97 outFrame->SetRegions(region);
100 outFrame->Allocate();
102 catch(itk::ExceptionObject & err)
104 LOG_ERROR("Failed to allocate memory for the image conversion: " << err.GetDescription() );
108 imageExport->Export( outFrame->GetBufferPointer() );
113 //----------------------------------------------------------------------------
114 template<typename ScalarType>
115 PlusStatus DeepCopyVtkVolumeToItkImage( vtkImageData* inFrame, typename itk::Image< ScalarType, 2 >::Pointer outFrame )
117 LOG_TRACE("PlusCommon::ConvertVtkImageToItkImage");
119 if ( inFrame == NULL )
121 LOG_ERROR("Failed to convert vtk image to itk image - input image is NULL!");
125 if ( outFrame.IsNull() )
127 LOG_ERROR("Failed to convert vtk image to itk image - output image is NULL!");
131 if( igsioVideoFrame::GetNumberOfBytesPerScalar(inFrame->GetScalarType()) != sizeof(ScalarType) )
133 LOG_ERROR("Mismatch between input and output scalar types. In: " << igsioVideoFrame::GetStringFromVTKPixelType(inFrame->GetScalarType()));
137 int extent[6]={0,0,0,0,0,0};
138 inFrame->GetExtent(extent);
140 if( extent[5]-extent[4] > 1 )
142 LOG_WARNING("3D volume sent in to PlusCommon::DeepCopyVtkVolumeToItkImage. Only first slice will be copied.");
145 // convert vtkImageData to itkImage
146 vtkSmartPointer<vtkImageExport> imageExport = vtkSmartPointer<vtkImageExport>::New();
147 imageExport->SetInputData(inFrame);
148 imageExport->Update();
150 double width = extent[1] - extent[0] + 1;
151 double height = extent[3] - extent[2] + 1;
153 typename itk::Image< ScalarType, 2 >::SizeType size;
156 typename itk::Image< ScalarType, 2 >::IndexType start;
159 typename itk::Image< ScalarType, 2 >::RegionType region;
160 region.SetSize(size);
161 region.SetIndex(start);
162 outFrame->SetRegions(region);
165 outFrame->Allocate();
167 catch(itk::ExceptionObject & err)
169 LOG_ERROR("Failed to allocate memory for the image conversion: " << err.GetDescription() );
173 imageExport->Export( outFrame->GetBufferPointer() );