PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
PlusCommon.cxx
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 // Local includes
8 #include "PlusConfigure.h"
9 #include "PlusCommon.h"
10 #include "PlusRevision.h"
11 
12 // VTK includes
13 #include <vtkMatrix4x4.h>
14 #include <vtkXMLDataElement.h>
15 #include <vtksys/SystemTools.hxx>
16 
17 // MetaIO includes
18 #include "metaImage.h"
19 
20 // STL includes
21 #include <algorithm>
22 #include <string>
23 
24 #ifdef PLUS_USE_OpenIGTLink
25 // IGTL includes
26 #include <igtlImageMessage.h>
27 #endif
28 
29 //----------------------------------------------------------------------------
31 {
32  std::string plusLibVersion = std::string("Plus-") + std::string(PLUSLIB_VERSION) + "." + std::string(PLUSLIB_SHORT_REVISION);
33 #ifdef _DEBUG
34  plusLibVersion += " (debug build)";
35 #endif
36 #if defined _WIN64
37  plusLibVersion += " - Win64";
38 #elif defined _WIN32
39  plusLibVersion += " - Win32";
40 #elif defined __APPLE__
41  plusLibVersion += " - Mac";
42 #else
43  plusLibVersion += " - Linux";
44 #endif
45  return plusLibVersion;
46 }
47 
48 
49 //----------------------------------------------------------------------------
50 PlusStatus PlusCommon::WriteToFile(igsioTrackedFrame* frame, const std::string& filename, vtkMatrix4x4* imageToTracker)
51 {
52  vtkImageData* volumeToSave = frame->GetImageData()->GetImage();
53 
54  MET_ValueEnumType scalarType = MET_NONE;
55  switch (volumeToSave->GetScalarType())
56  {
57  case VTK_UNSIGNED_CHAR:
58  scalarType = MET_UCHAR;
59  break;
60  case VTK_FLOAT:
61  scalarType = MET_FLOAT;
62  break;
63  default:
64  LOG_ERROR("Scalar type is not supported!");
65  return PLUS_FAIL;
66  }
67 
68  MetaImage metaImage(volumeToSave->GetDimensions()[0], volumeToSave->GetDimensions()[1], volumeToSave->GetDimensions()[2],
69  volumeToSave->GetSpacing()[0], volumeToSave->GetSpacing()[1], volumeToSave->GetSpacing()[2],
70  scalarType, volumeToSave->GetNumberOfScalarComponents(), volumeToSave->GetScalarPointer());
71  double origin[3];
72  origin[0] = imageToTracker->Element[0][3];
73  origin[1] = imageToTracker->Element[1][3];
74  origin[2] = imageToTracker->Element[2][3];
75  metaImage.Origin(origin);
76  for (int i = 0; i < 3; ++i)
77  {
78  for (int j = 0; j < 3; ++j)
79  {
80  metaImage.Orientation(i, j, imageToTracker->Element[i][j]);
81  }
82  }
83  // By definition, LPS orientation in DICOM sense = RAI orientation in MetaIO. See details at:
84  // http://www.itk.org/Wiki/Proposals:Orientation#Some_notes_on_the_DICOM_convention_and_current_ITK_usage
85  metaImage.AnatomicalOrientation("RAI");
86  metaImage.BinaryData(true);
87  metaImage.CompressedData(true);
88  metaImage.ElementDataFileName("LOCAL");
89  if (metaImage.Write(filename.c_str()) == false)
90  {
91  LOG_ERROR("Failed to save reconstructed volume in sequence metafile!");
92  return PLUS_FAIL;
93  }
94  return PLUS_SUCCESS;
95 }
96 
97 #ifdef PLUS_USE_OpenIGTLink
98 //----------------------------------------------------------------------------
99 // static
100 PlusCommon::VTKScalarPixelType PlusCommon::GetVTKScalarPixelTypeFromIGTL(PlusCommon::IGTLScalarPixelType igtlPixelType)
101 {
102  switch (igtlPixelType)
103  {
104  case igtl::ImageMessage::TYPE_INT8:
105  return VTK_CHAR;
106  case igtl::ImageMessage::TYPE_UINT8:
107  return VTK_UNSIGNED_CHAR;
108  case igtl::ImageMessage::TYPE_INT16:
109  return VTK_SHORT;
110  case igtl::ImageMessage::TYPE_UINT16:
111  return VTK_UNSIGNED_SHORT;
112  case igtl::ImageMessage::TYPE_INT32:
113  return VTK_INT;
114  case igtl::ImageMessage::TYPE_UINT32:
115  return VTK_UNSIGNED_INT;
116  case igtl::ImageMessage::TYPE_FLOAT32:
117  return VTK_FLOAT;
118  case igtl::ImageMessage::TYPE_FLOAT64:
119  return VTK_DOUBLE;
120  default:
121  return VTK_VOID;
122  }
123 }
124 
125 //----------------------------------------------------------------------------
126 // static
127 PlusCommon::IGTLScalarPixelType PlusCommon::GetIGTLScalarPixelTypeFromVTK(PlusCommon::VTKScalarPixelType pixelType)
128 {
129  switch (pixelType)
130  {
131  case VTK_CHAR:
132  return igtl::ImageMessage::TYPE_INT8;
133  case VTK_UNSIGNED_CHAR:
134  return igtl::ImageMessage::TYPE_UINT8;
135  case VTK_SHORT:
136  return igtl::ImageMessage::TYPE_INT16;
137  case VTK_UNSIGNED_SHORT:
138  return igtl::ImageMessage::TYPE_UINT16;
139  case VTK_INT:
140  return igtl::ImageMessage::TYPE_INT32;
141  case VTK_UNSIGNED_INT:
142  return igtl::ImageMessage::TYPE_UINT32;
143  case VTK_FLOAT:
144  return igtl::ImageMessage::TYPE_FLOAT32;
145  case VTK_DOUBLE:
146  return igtl::ImageMessage::TYPE_FLOAT64;
147  default:
148  // There is no unknown IGT scalar pixel type, so display an error message
149  //**LOG_ERROR("Unknown conversion between VTK scalar pixel type (" << pixelType << ") and IGT pixel type - return igtl::ImageMessage::TYPE_INT8 by default!");
150  return igtl::ImageMessage::TYPE_INT8;
151  }
152 }
153 
154 #endif
vtkPlusCommonExport PlusStatus WriteToFile(igsioTrackedFrame *frame, const std::string &filename, vtkMatrix4x4 *imageToTracker)
Definition: PlusCommon.cxx:50
igsioStatus PlusStatus
Definition: PlusCommon.h:40
for i
#define PLUS_FAIL
Definition: PlusCommon.h:43
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
int VTKScalarPixelType
Definition: PlusCommon.h:55
int IGTLScalarPixelType
Definition: PlusCommon.h:56
vtkPlusCommonExport std::string GetPlusLibVersionString()
Definition: PlusCommon.cxx:30