PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
PlusCommon.txx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 namespace PlusCommon
8 {
9  namespace XML
10  {
11  //----------------------------------------------------------------------------
12  template<typename T>
13  PlusStatus SafeGetAttributeValueInsensitive(vtkXMLDataElement& element, const std::wstring& attributeName, T& value)
14  {
15  return PlusCommon::XML::SafeGetAttributeValueInsensitive(element, std::string(begin(attributeName), end(attributeName)), value);
16  }
17 
18  //----------------------------------------------------------------------------
19  template<typename T>
20  PlusStatus SafeGetAttributeValueInsensitive(vtkXMLDataElement& element, const std::string& attributeName, T& value)
21  {
22  if (element.GetAttribute(attributeName.c_str()) == NULL)
23  {
24  return PLUS_FAIL;
25  }
26  element.GetScalarAttribute(attributeName.c_str(), value);
27  return PLUS_SUCCESS;
28  }
29  }
30 
31  //----------------------------------------------------------------------------
32  template<typename ElemType>
33  void JoinTokensIntoString(const std::vector<ElemType>& elems, std::string& output, char separator)
34  {
35  output = "";
36  std::stringstream ss;
37  typedef std::vector<ElemType> List;
38 
39  for (typename List::const_iterator it = elems.begin(); it != elems.end(); ++it)
40  {
41  ss << *it;
42  if (it != elems.end() - 1)
43  {
44  ss << separator;
45  }
46  }
47 
48  output = ss.str();
49  }
50 
51  //----------------------------------------------------------------------------
52  template<typename ScalarType>
53  PlusStatus DeepCopyVtkVolumeToItkVolume( vtkImageData* inFrame, typename itk::Image< ScalarType, 3 >::Pointer outFrame )
54  {
55  LOG_TRACE("PlusCommon::ConvertVtkImageToItkImage");
56 
57  if ( inFrame == NULL )
58  {
59  LOG_ERROR("Failed to convert vtk image to itk image - input image is NULL!");
60  return PLUS_FAIL;
61  }
62 
63  if ( outFrame.IsNull() )
64  {
65  LOG_ERROR("Failed to convert vtk image to itk image - output image is NULL!");
66  return PLUS_FAIL;
67  }
68 
69  if( igsioVideoFrame::GetNumberOfBytesPerScalar(inFrame->GetScalarType()) != sizeof(ScalarType) )
70  {
71  LOG_ERROR("Mismatch between input and output scalar types. In: " << igsioVideoFrame::GetStringFromVTKPixelType(inFrame->GetScalarType()));
72  return PLUS_FAIL;
73  }
74 
75  // convert vtkImageData to itkImage
76  vtkSmartPointer<vtkImageExport> imageExport = vtkSmartPointer<vtkImageExport>::New();
77  imageExport->SetInputData(inFrame);
78  imageExport->Update();
79 
80  int extent[6]={0,0,0,0,0,0};
81  inFrame->GetExtent(extent);
82 
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;
87  size[0] = width;
88  size[1] = height;
89  size[2] = thickness;
90  typename itk::Image< ScalarType, 3 >::IndexType start;
91  start[0]=0;
92  start[1]=0;
93  start[2]=0;
94  typename itk::Image< ScalarType, 3 >::RegionType region;
95  region.SetSize(size);
96  region.SetIndex(start);
97  outFrame->SetRegions(region);
98  try
99  {
100  outFrame->Allocate();
101  }
102  catch(itk::ExceptionObject & err)
103  {
104  LOG_ERROR("Failed to allocate memory for the image conversion: " << err.GetDescription() );
105  return PLUS_FAIL;
106  }
107 
108  imageExport->Export( outFrame->GetBufferPointer() );
109 
110  return PLUS_SUCCESS;
111  }
112 
113  //----------------------------------------------------------------------------
114  template<typename ScalarType>
115  PlusStatus DeepCopyVtkVolumeToItkImage( vtkImageData* inFrame, typename itk::Image< ScalarType, 2 >::Pointer outFrame )
116  {
117  LOG_TRACE("PlusCommon::ConvertVtkImageToItkImage");
118 
119  if ( inFrame == NULL )
120  {
121  LOG_ERROR("Failed to convert vtk image to itk image - input image is NULL!");
122  return PLUS_FAIL;
123  }
124 
125  if ( outFrame.IsNull() )
126  {
127  LOG_ERROR("Failed to convert vtk image to itk image - output image is NULL!");
128  return PLUS_FAIL;
129  }
130 
131  if( igsioVideoFrame::GetNumberOfBytesPerScalar(inFrame->GetScalarType()) != sizeof(ScalarType) )
132  {
133  LOG_ERROR("Mismatch between input and output scalar types. In: " << igsioVideoFrame::GetStringFromVTKPixelType(inFrame->GetScalarType()));
134  return PLUS_FAIL;
135  }
136 
137  int extent[6]={0,0,0,0,0,0};
138  inFrame->GetExtent(extent);
139 
140  if( extent[5]-extent[4] > 1 )
141  {
142  LOG_WARNING("3D volume sent in to PlusCommon::DeepCopyVtkVolumeToItkImage. Only first slice will be copied.");
143  }
144 
145  // convert vtkImageData to itkImage
146  vtkSmartPointer<vtkImageExport> imageExport = vtkSmartPointer<vtkImageExport>::New();
147  imageExport->SetInputData(inFrame);
148  imageExport->Update();
149 
150  double width = extent[1] - extent[0] + 1;
151  double height = extent[3] - extent[2] + 1;
152 
153  typename itk::Image< ScalarType, 2 >::SizeType size;
154  size[0] = width;
155  size[1] = height;
156  typename itk::Image< ScalarType, 2 >::IndexType start;
157  start[0]=0;
158  start[1]=0;
159  typename itk::Image< ScalarType, 2 >::RegionType region;
160  region.SetSize(size);
161  region.SetIndex(start);
162  outFrame->SetRegions(region);
163  try
164  {
165  outFrame->Allocate();
166  }
167  catch(itk::ExceptionObject & err)
168  {
169  LOG_ERROR("Failed to allocate memory for the image conversion: " << err.GetDescription() );
170  return PLUS_FAIL;
171  }
172 
173  imageExport->Export( outFrame->GetBufferPointer() );
174 
175  return PLUS_SUCCESS;
176  }
177 
178 }