UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-05-20)
usImageDisplayWidgetQmlOverlay.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 
39 #include <visp3/ustk_gui/usImageDisplayWidgetQmlOverlay.h>
40 
41 #if (defined(USTK_HAVE_VTK_QT5) || defined(USTK_HAVE_QT5))
42 
43 #include <QQuickItem>
44 
49 {
50  this->setMinimumSize(200, 200);
51  m_qQuickOverlay = new QQuickWidget(m_label);
52  m_qQuickOverlay->setAttribute(Qt::WA_AlwaysStackOnTop);
53  m_qQuickOverlay->setClearColor(Qt::transparent);
54  m_qQuickOverlay->setSource(QUrl("qrc:/qml/overlay-tracking-rect.qml"));
55 
56  connect(m_qQuickOverlay->rootObject(), SIGNAL(startTracking()), this, SLOT(startTrackingSlot()));
57  connect(m_qQuickOverlay->rootObject(), SIGNAL(stopTracking()), this, SIGNAL(stopTracking()));
58 
59  QQuickWidget::Status status(QQuickWidget::Null);
60 
61  while (status != QQuickWidget::Ready) {
62  status = m_qQuickOverlay->status();
63  vpTime::wait(50);
64  }
65 }
66 
71 
73 {
74  event->accept();
76  if (m_qQuickOverlay->rootObject()) {
77  m_qQuickOverlay->rootObject()->setProperty("width", event->size().width());
78  m_qQuickOverlay->rootObject()->setProperty("height", event->size().height());
79  }
80 }
81 
82 vpImagePoint usImageDisplayWidgetQmlOverlay::displayImageToRealImageDimentions(const vpImagePoint displayPoint)
83 {
84  vpImagePoint p;
85  unsigned int imageHeight;
86  unsigned int imageWidth;
87  if (m_useScanConversion) {
88  imageHeight = m_postScan.getHeight();
89  imageWidth = m_postScan.getWidth();
90  } else {
91  imageHeight = m_image.getHeight();
92  imageWidth = m_image.getWidth();
93  }
94  p.set_i((displayPoint.get_i() * imageHeight) / (double)height());
95  p.set_j((displayPoint.get_j() * imageWidth) / (double)width());
96 
97  return p;
98 }
99 
100 vpImagePoint usImageDisplayWidgetQmlOverlay::realImageToDisplayImageDimentions(const vpImagePoint realImagePoint)
101 {
102  vpImagePoint p;
103  unsigned int imageHeight;
104  unsigned int imageWidth;
105  if (m_useScanConversion) {
106  imageHeight = m_postScan.getHeight();
107  imageWidth = m_postScan.getWidth();
108  } else {
109  imageHeight = m_image.getHeight();
110  imageWidth = m_image.getWidth();
111  }
112  p.set_i((realImagePoint.get_i() * height()) / (double)imageHeight);
113  p.set_j((realImagePoint.get_j() * width()) / (double)imageWidth);
114 
115  return p;
116 }
117 
118 vpRectOriented usImageDisplayWidgetQmlOverlay::displayImageToRealImageDimentions(const vpRectOriented displayRectangle)
119 {
120  // center point
121  vpImagePoint center;
122  center = displayImageToRealImageDimentions(displayRectangle.getCenter());
123 
124  // rectangle height & width scaling using angular parameter
125  unsigned int imageHeight;
126  unsigned int imageWidth;
127  if (m_useScanConversion) {
128  imageHeight = m_postScan.getHeight();
129  imageWidth = m_postScan.getWidth();
130  } else {
131  imageHeight = m_image.getHeight();
132  imageWidth = m_image.getWidth();
133  }
134  int newHeight =
135  (displayRectangle.getHeight() * imageHeight / (double)height()) * std::cos(displayRectangle.getOrientation()) +
136  (displayRectangle.getWidth() * imageWidth / (double)width()) * std::sin(displayRectangle.getOrientation());
137  int newWidth =
138  (displayRectangle.getHeight() * imageHeight / (double)height()) * std::sin(displayRectangle.getOrientation()) +
139  (displayRectangle.getWidth() * imageWidth / (double)width()) * std::cos(displayRectangle.getOrientation());
140 
141  return vpRectOriented(center, newWidth, newHeight, displayRectangle.getOrientation());
142 }
143 
144 vpRectOriented usImageDisplayWidgetQmlOverlay::realImageToDisplayImageDimentions(const vpRectOriented realRectangle)
145 {
146  // center point
147  vpImagePoint center;
148  center = realImageToDisplayImageDimentions(realRectangle.getCenter());
149 
150  // rectangle height & width scaling using angular parameter
151  unsigned int imageHeight;
152  unsigned int imageWidth;
153  if (m_useScanConversion) {
154  imageHeight = m_postScan.getHeight();
155  imageWidth = m_postScan.getWidth();
156  } else {
157  imageHeight = m_image.getHeight();
158  imageWidth = m_image.getWidth();
159  }
160  int newHeight =
161  (realRectangle.getHeight() * height() / (double)imageHeight) * std::cos(realRectangle.getOrientation()) +
162  (realRectangle.getWidth() * width() / (double)imageWidth) * std::sin(realRectangle.getOrientation());
163 
164  int newWidth =
165  (realRectangle.getHeight() * height() / (double)imageHeight) * std::sin(realRectangle.getOrientation()) +
166  (realRectangle.getWidth() * width() / (double)imageWidth) * std::cos(realRectangle.getOrientation());
167 
168  return vpRectOriented(center, newWidth, newHeight, realRectangle.getOrientation());
169 }
170 
175 void usImageDisplayWidgetQmlOverlay::updateRectPosition(vpRectOriented newRectangle)
176 {
177  // transform rectangle from real image size to display dimentions
178  vpRectOriented displayRectangle;
179  displayRectangle = realImageToDisplayImageDimentions(newRectangle);
180 
181  QObject *rectItem = m_qQuickOverlay->rootObject()->findChild<QObject *>("selectionRectangle");
182  rectItem->setProperty("x", displayRectangle.getCenter().get_j());
183  rectItem->setProperty("y", displayRectangle.getCenter().get_i());
184  rectItem->setProperty("rotation", vpMath::deg(displayRectangle.getOrientation()));
185  rectItem->setProperty("height", displayRectangle.getHeight());
186  rectItem->setProperty("width", displayRectangle.getWidth());
187 }
188 
193 {
194  QObject *rectangleObject = m_qQuickOverlay->rootObject()->findChild<QObject *>("selectionRectangle");
195  int centerX = rectangleObject->property("x").toInt();
196  int centerY = rectangleObject->property("y").toInt();
197  int height = rectangleObject->property("height").toInt();
198  int width = rectangleObject->property("width").toInt();
199  int rotation = rectangleObject->property("rotation").toInt();
200 
201  vpRectOriented displayRect(vpImagePoint(centerY, centerX), width, height, rotation);
202 
203  emit(startTracking(displayImageToRealImageDimentions(displayRect)));
204 }
205 
206 #endif
void startTracking(vpRectOriented)
void updateRectPosition(vpRectOriented newRectangle)
Qt widget class for 2D ultrasound image display.
vpImage< unsigned char > m_image
void resizeEvent(QResizeEvent *event)
usImagePostScan2D< unsigned char > m_postScan