UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-05-15)
usMeshDeformation.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 
37 #include "usMeshDeformation.h"
38 
39 #ifdef USTK_HAVE_VTK_QT
40 
44 usMeshDeformation::usMeshDeformation(QWidget *parent, Qt::WindowFlags f) : usViewerWidget(parent, f)
45 {
46  // POLYDATA
47  m_meshPolyData = vtkUnstructuredGrid::New();
48 
49  // Setup renderer
50  renderer = vtkRenderer::New();
51  renderer->SetBackground(0.3, 0.3, 0.3);
52  renderer->ResetCamera();
53 
54  // Setup render window
55 #if USTK_HAVE_VTK_VERSION < 0x090000
56  vtkRenderWindow *renderWindow = this->GetRenderWindow();
57 #else
58  vtkRenderWindow *renderWindow = this->renderWindow();
59 #endif
60  renderWindow->AddRenderer(renderer);
61 }
62 
64 
69 void usMeshDeformation::paintEvent(QPaintEvent *event) { usViewerWidget::paintEvent(event); }
70 
75 void usMeshDeformation::keyPressEvent(QKeyEvent *event)
76 {
77  if (event->key() == Qt::Key_Up) { // move first point of the mesh along Y
78  double *point1 = m_meshPolyData->GetPoints()->GetPoint(0);
79  point1[1] += 1;
80  m_meshPolyData->GetPoints()->SetPoint(0, point1);
81  m_meshPolyData->GetPoints()->Modified();
82 #if USTK_HAVE_VTK_VERSION < 0x090000
83  this->GetRenderWindow()->Render();
84 #else
85  this->renderWindow()->Render();
86 #endif
87  } else if (event->key() == Qt::Key_Down) { // move first point of the mesh of along Y
88  double *point1 = m_meshPolyData->GetPoints()->GetPoint(0);
89  point1[1] -= 1;
90  m_meshPolyData->GetPoints()->SetPoint(0, point1);
91  m_meshPolyData->GetPoints()->Modified();
92 #if USTK_HAVE_VTK_VERSION < 0x090000
93  this->GetRenderWindow()->Render();
94 #else
95  this->renderWindow()->Render();
96 #endif
97  } else {
98  usViewerWidget::keyPressEvent(event);
99  }
100 }
101 
106 void usMeshDeformation::setMeshInScene(vtkUnstructuredGrid *mesh)
107 {
108  m_meshPolyData = mesh;
109 
110  vtkSmartPointer<vtkDataSetMapper> meshMapper = vtkSmartPointer<vtkDataSetMapper>::New();
111  meshMapper->SetInputData(mesh);
112  m_meshActor = vtkSmartPointer<vtkActor>::New();
113  m_meshActor->GetProperty()->SetColor(0, 0, 1.0); // blue
114  m_meshActor->SetMapper(meshMapper);
115 
116  renderer->AddActor(m_meshActor);
117 }
118 
124 void usMeshDeformation::updateMeshPosition(vpHomogeneousMatrix transform)
125 {
126  if (m_meshActor->GetUserMatrix() == NULL) { // init case
127  vtkSmartPointer<vtkMatrix4x4> vtkMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
128  usVTKConverter::convert(transform, vtkMatrix);
129  m_meshActor->SetUserMatrix(vtkMatrix);
130  } else {
131  // get current matrix
132  vpHomogeneousMatrix currentTransform;
133  currentTransform.eye();
134  usVTKConverter::convert(m_meshActor->GetUserMatrix(), currentTransform);
135 
136  // Conversion, taking in account current transform
137  vpHomogeneousMatrix newTransform = currentTransform * transform;
138 
139  // set new position
140  vtkMatrix4x4 *vtkNewtransform = vtkMatrix4x4::New();
141  usVTKConverter::convert(newTransform, vtkNewtransform);
142 
143  m_meshActor->SetUserMatrix(vtkNewtransform);
144 #if USTK_HAVE_VTK_VERSION < 0x090000
145  this->GetRenderWindow()->Render();
146 #else
147  this->renderWindow()->Render();
148 #endif
149  }
150 }
151 
157 vtkPoints *usMeshDeformation::getMeshPoints() { return m_meshPolyData->GetPoints(); }
158 
163 {
164 #if USTK_HAVE_VTK_VERSION < 0x090000
165  this->GetRenderWindow()->Render();
166 #else
167  this->renderWindow()->Render();
168 #endif
169 }
170 
171 #endif
void updateMeshPosition(vpHomogeneousMatrix transform)
void keyPressEvent(QKeyEvent *event)
usMeshDeformation(QWidget *parent=NULL, Qt::WindowFlags f=Qt::WindowFlags())
void setMeshInScene(vtkUnstructuredGrid *mesh)
vtkPoints * getMeshPoints()
void paintEvent(QPaintEvent *event)
static void convert(const usImagePostScan3D< unsigned char > &postScanImage, vtkSmartPointer< vtkImageData > &vtkPostScanImage, vtkSmartPointer< vtkImageImport > importer=NULL)
View used to render a vtk scene in a QWidget (based on QVTKWidget for Qt4, QVTKOpenGLWidget for Qt5)
void paintEvent(QPaintEvent *event)