UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
usVTKConverter.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ustk with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Marc Pouliquen
30  *
31  *****************************************************************************/
32 
38 #include <visp3/ustk_gui/usVTKConverter.h>
39 
40 #ifdef USTK_HAVE_VTK_QT
41 
42 #include <vtkDataArray.h>
43 #include <vtkImageImport.h>
44 #include <vtkPointData.h>
45 
50  vtkSmartPointer<vtkImageData> &vtkPostScanImage, vtkSmartPointer<vtkImageImport> importer)
51 {
52  if (importer == NULL) {
53  importer = vtkSmartPointer<vtkImageImport>::New();
54  importer->SetDataScalarTypeToUnsignedChar();
55  importer->SetWholeExtent(0, postScanImage.getWidth() - 1, 0, postScanImage.getHeight() - 1, 0,
56  postScanImage.getNumberOfFrames() - 1);
57  importer->SetDataExtentToWholeExtent();
58  importer->SetNumberOfScalarComponents(1);
59  importer->SetImportVoidPointer((void *)postScanImage.getConstData(), 1);
60  } else
61  importer->SetImportVoidPointer((void *)postScanImage.getConstData());
62 
63  importer->Update();
64 
65  vtkPostScanImage = importer->GetOutput();
66  vtkPostScanImage->SetSpacing(postScanImage.getElementSpacingX(), postScanImage.getElementSpacingY(),
67  postScanImage.getElementSpacingZ());
68 }
69 
74  vtkSmartPointer<vtkImageData> &vtkPreScanImage, vtkSmartPointer<vtkImageImport> importer)
75 {
76  if (importer == NULL) {
77  importer = vtkSmartPointer<vtkImageImport>::New();
78  importer->SetDataScalarTypeToUnsignedChar();
79  importer->SetImportVoidPointer((void *)preScanImage.getConstData());
80  importer->SetWholeExtent(0, preScanImage.getWidth() - 1, 0, preScanImage.getHeight() - 1, 0,
81  preScanImage.getNumberOfFrames() - 1);
82  importer->SetDataExtentToWholeExtent();
83  importer->SetNumberOfScalarComponents(1);
84  } else
85  importer->SetImportVoidPointer((void *)preScanImage.getConstData());
86 
87  importer->Update();
88 
89  vtkPreScanImage = importer->GetOutput();
90 }
91 
95 void usVTKConverter::convert(vtkSmartPointer<vtkImageData> &vtkPostScanImage,
96  usImagePostScan2D<unsigned char> &postScanImage)
97 {
98  // get dimentions/spacing of the 3D image.
99  int imageDims[3];
100  vtkPostScanImage->GetDimensions(imageDims);
101  double spacing[3];
102  vtkPostScanImage->GetSpacing(spacing);
103 
104  postScanImage.resize(imageDims[1], imageDims[0]);
105 
106  for (int i = 0; i < imageDims[0]; i++) {
107  for (int j = 0; j < imageDims[1]; j++) {
108  postScanImage[j][i] =
109  (unsigned char)(vtkPostScanImage->GetScalarComponentAsDouble(i, imageDims[1] - j - 1, 0, 0));
110  }
111  }
112 
113  postScanImage.setWidthResolution(spacing[0]);
114  postScanImage.setHeightResolution(spacing[1]);
115 }
116 
120 void usVTKConverter::convert(const vpHomogeneousMatrix &vispMatrix, vtkMatrix4x4 *vtkMatrix)
121 {
122  for (int i = 0; i < 4; i++)
123  for (int j = 0; j < 4; j++)
124  vtkMatrix->SetElement(i, j, vispMatrix[i][j]);
125 }
126 
130 void usVTKConverter::convert(vtkMatrix4x4 *vtkMatrix, vpHomogeneousMatrix &vispMatrix)
131 {
132  for (int i = 0; i < 4; i++)
133  for (int j = 0; j < 4; j++)
134  vispMatrix[i][j] = vtkMatrix->GetElement(i, j);
135 }
136 
137 #endif
unsigned int getNumberOfFrames() const
Definition: usImage3D.h:137
Type * getConstData() const
Definition: usImage3D.h:104
unsigned int getHeight() const
Definition: usImage3D.h:131
unsigned int getWidth() const
Definition: usImage3D.h:125
void setHeightResolution(double heightResolution)
void setWidthResolution(double widthResolution)
double getElementSpacingX() const
double getElementSpacingY() const
double getElementSpacingZ() const
static void convert(const usImagePostScan3D< unsigned char > &postScanImage, vtkSmartPointer< vtkImageData > &vtkPostScanImage, vtkSmartPointer< vtkImageImport > importer=NULL)