UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-03)
usGeometryDisplayTools.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  * Author:
29  * Jason Chevrie
30  *
31  *****************************************************************************/
32 
33 #include <visp3/ustk_core/usGeometryDisplayTools.h>
34 
35 #include <visp3/core/vpColVector.h>
36 #include <visp3/core/vpDisplay.h>
37 #include <visp3/core/vpMatrix.h>
38 #include <visp3/core/vpRGBa.h>
39 
40 namespace usGeometryDisplayTools
41 {
42 
43 template <class ImageDataType>
44 void displayFrame(const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMframe, double Xscale, double Yscale)
45 {
46  double origin_x = Xscale * imageMframe[0][3];
47  double origin_y = Yscale * imageMframe[1][3];
48 
49  double zmax = -2;
50  double zmin = 2;
51  int zmax_index = 0;
52  int zmin_index = 0;
53 
54  for (int j = 0; j < 3; j++) {
55  double z = imageMframe[2][j];
56  if (z >= zmax) {
57  zmax = z;
58  zmax_index = j;
59  }
60  if (z <= zmin) {
61  zmin = z;
62  zmin_index = j;
63  }
64  }
65  double order[3];
66  order[0] = zmax_index;
67  order[1] = 3 - zmin_index - zmax_index;
68  order[2] = zmin_index;
69 
70  // Display frame
71  vpColor color[3] = { vpColor::red, vpColor::green, vpColor::blue };
72  for (unsigned int i = 0; i < 3; i++) {
73  unsigned int j = (unsigned int)order[i];
74  vpDisplay::displayArrow(I, (int)origin_y, (int)origin_x, (int)(origin_y + 20 * imageMframe[1][j]), (int)(origin_x + 20 * imageMframe[0][j]),
75  color[j], 5, 5, 1);
76  }
77 }
78 template VISP_EXPORT void displayFrame<unsigned char>(const vpImage<unsigned char> &, const vpHomogeneousMatrix &,
79  double, double);
80 template VISP_EXPORT void displayFrame<vpRGBa>(const vpImage<vpRGBa> &, const vpHomogeneousMatrix &, double, double);
81 
83 
84 template <class ImageDataType>
85 void display(const usOrientedPlane3D &plane, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
86  double Xscale, double Yscale, const vpColor &color)
87 {
88  vpMatrix R(imageMworld.getRotationMatrix());
89  vpColVector T(imageMworld.getTranslationVector());
90 
91  vpColVector imagePoint(R * plane.getPosition() + T);
92  vpColVector imageDirection = R * plane.getDirection();
93 
94  double x = Xscale * imagePoint[0];
95  double y = Yscale * imagePoint[1];
96 
97  double dx = 0.1 * Xscale * imageDirection[0];
98  double dy = 0.1 * Yscale * imageDirection[1];
99 
100  vpDisplay::displayCross(I, (int)y, (int)x, 7, color);
101  vpDisplay::displayLine(I, (int)(y - dx), (int)(x + dy), (int)(y + dx), (int)(x - dy), color);
102 }
103 template VISP_EXPORT void display(const usOrientedPlane3D &, const vpImage<unsigned char> &,
104  const vpHomogeneousMatrix &, double, double, const vpColor &);
105 template VISP_EXPORT void display(const usOrientedPlane3D &, const vpImage<vpRGBa> &, const vpHomogeneousMatrix &,
106  double, double, const vpColor &);
107 
109 
110 template <class ImageDataType>
111 void display(const usPolynomialCurve2D &curve, const vpImage<ImageDataType> &I, double Xscale, double Yscale,
112  const vpColor &color, int nbRenderingLines)
113 {
114  double step = (curve.getEndParameter() - curve.getStartParameter()) / nbRenderingLines;
115 
116  std::vector<double> params(nbRenderingLines + 1);
117  params.front() = curve.getStartParameter();
118  for (int i = 1; i < nbRenderingLines + 1; i++) {
119  params.at(i) = params.at(i - 1) + step;
120  }
121 
122  vpMatrix imagePoints = curve.getPoints(vpColVector(params));
123 
124  double x0 = Xscale * imagePoints[0][0];
125  double y0 = Yscale * imagePoints[1][0];
126  double x1 = x0;
127  double y1 = y0;
128 
129  for (int i = 0; i < nbRenderingLines; i++) {
130  x0 = x1;
131  y0 = y1;
132 
133  x1 = Xscale * imagePoints[0][i + 1];
134  y1 = Yscale * imagePoints[1][i + 1];
135 
136  vpDisplay::displayLine(I, (int)y0, (int)x0, (int)y1, (int)x1, color);
137  }
138 }
139 template VISP_EXPORT void display<unsigned char>(const usPolynomialCurve2D &, const vpImage<unsigned char> &, double,
140  double, const vpColor &, int);
141 template VISP_EXPORT void display<vpRGBa>(const usPolynomialCurve2D &, const vpImage<vpRGBa> &, double, double,
142  const vpColor &, int);
143 
145 
146 template <class ImageDataType>
147 void display(const usPolynomialCurve3D &curve, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
148  double Xscale, double Yscale, const vpColor &color, int nbRenderingLines, double visibilityDistance)
149 {
150  unsigned int nbPoints = nbRenderingLines + 1;
151  double step = (curve.getEndParameter() - curve.getStartParameter()) / nbRenderingLines;
152 
153  std::vector<double> params(nbPoints);
154  params.front() = curve.getStartParameter();
155  for (unsigned int i = 1; i < nbPoints; i++)
156  params.at(i) = params.at(i - 1) + step;
157 
158  vpMatrix points = curve.getPoints(vpColVector(params));
159 
160  const vpMatrix R(imageMworld.getRotationMatrix());
161  const vpMatrix T(imageMworld.getTranslationVector());
162  const vpMatrix ones(1, nbPoints, 1);
163  vpMatrix imagePoints(R * points + vpMatrix::kron(T, ones));
164 
165  double x0 = Xscale * imagePoints[0][0];
166  double y0 = Yscale * imagePoints[1][0];
167  double z0 = imagePoints[2][0];
168  double x1 = x0;
169  double y1 = y0;
170  double z1 = z0;
171 
172  for (int i = 0; i < nbRenderingLines; i++) {
173  x0 = x1;
174  y0 = y1;
175  z0 = z1;
176 
177  x1 = Xscale * imagePoints[0][i + 1];
178  y1 = Yscale * imagePoints[1][i + 1];
179  z1 = imagePoints[2][i + 1];
180 
181  if ((vpMath::sign(z0) != vpMath::sign(z1)) || ((fabs(z0) < visibilityDistance) && (fabs(z1) < visibilityDistance)))
182  vpDisplay::displayLine(I, (int)y0, (int)x0, (int)y1, (int)x1, color);
183  }
184 }
185 
186 template VISP_EXPORT void display<unsigned char>(const usPolynomialCurve3D &, const vpImage<unsigned char> &,
187  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
188  double);
189 template VISP_EXPORT void display<vpRGBa>(const usPolynomialCurve3D &, const vpImage<vpRGBa> &,
190  const vpHomogeneousMatrix &, double, double, const vpColor &, int, double);
191 
192 template <class ImageDataType>
193 void displayCurvatureFromShape(const usPolynomialCurve3D &curve, const vpImage<ImageDataType> &I,
194  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale,
195  const vpColor &color)
196 {
197  vpColVector C(4);
198  vpColVector D(4);
199  double k = curve.getCurvatureFromShape(0, -1, C, D);
200  if (k == 0)
201  return;
202 
203  double r = 1 / k;
204 
205  C = imageMworld * C;
206  C.resize(3, false);
207  D = imageMworld * D;
208  D.resize(3, false);
209 
210  vpColVector Dxy(3, 0);
211  Dxy[2] = 1;
212  vpColVector Axy = vpColVector::cross(D, Dxy);
213  vpColVector Bxy = vpColVector::cross(D, Axy).normalize();
214 
215  double thetaxy = atan2(Axy[1], Axy[0]);
216  double axy = r;
217  double bxy = r * vpColVector::cross(Bxy, Dxy).frobeniusNorm();
218  if (Bxy.frobeniusNorm() < 0.5)
219  bxy = axy; // if normalization failed the circle is almost in the plane
220 
221  // Scaling
222  vpImagePoint centerXY(Yscale * C[1], Xscale * C[0]);
223  double A = sqrt(pow(Xscale * cos(thetaxy), 2) + pow(Yscale * sin(thetaxy), 2));
224  axy *= A;
225  bxy *= A;
226  thetaxy = atan2(axy * sin(thetaxy), bxy * cos(thetaxy));
227  vpDisplay::displayEllipse(I, centerXY, axy, bxy, thetaxy, false, color);
228 }
230  const vpImage<unsigned char> &,
231  const vpHomogeneousMatrix &, double, double,
232  const vpColor &);
233 template VISP_EXPORT void displayCurvatureFromShape<vpRGBa>(const usPolynomialCurve3D &, const vpImage<vpRGBa> &,
234  const vpHomogeneousMatrix &, double, double,
235  const vpColor &);
236 
238 
239 template <class ImageDataType>
240 void displayLine(const usBSpline3D &spline, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
241  double Xscale, double Yscale, const vpColor &color, int nbRenderingLinesPerSegment,
242  double visibilityDistance)
243 {
244  for (int i = 0; i < spline.getNbSegments(); i++)
245  usGeometryDisplayTools::display(spline.accessSegment(i), I, imageMworld, Xscale, Yscale, color,
246  nbRenderingLinesPerSegment, visibilityDistance);
247 }
248 template VISP_EXPORT void displayLine<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
249  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
250  double);
251 template VISP_EXPORT void displayLine<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &, const vpHomogeneousMatrix &,
252  double, double, const vpColor &, int, double);
253 
254 template <class ImageDataType>
255 void displayExtremities(const usBSpline3D &spline, const vpImage<ImageDataType> &I,
256  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale, const vpColor &color,
257  double visibilityDistance)
258 {
259  unsigned int nbPoints = spline.getNbSegments() + 1;
260  if (nbPoints < 2)
261  return;
262 
263  vpMatrix points(3, nbPoints);
264 
265  for (int i = 0; i < spline.getNbSegments(); i++)
266  points.insert(spline.accessSegment(i).getStartPoint(), 0, i);
267  points.insert(spline.accessLastSegment().getEndPoint(), 0, spline.getNbSegments());
268 
269  const vpMatrix R(imageMworld.getRotationMatrix());
270  const vpMatrix T(imageMworld.getTranslationVector());
271  const vpMatrix ones(1, nbPoints, 1);
272  vpMatrix imagePoints(R * points + vpMatrix::kron(T, ones));
273 
274  for (unsigned int i = 0; i < nbPoints; i++) {
275  double x = Xscale * imagePoints[0][i];
276  double y = Yscale * imagePoints[1][i];
277  double z = imagePoints[2][i];
278  if (fabs(z) < visibilityDistance)
279  vpDisplay::displayCross(I, (int)y, (int)x, 7, color);
280  }
281 }
282 template VISP_EXPORT void displayExtremities<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
283  const vpHomogeneousMatrix &, double, double,
284  const vpColor &, double);
285 template VISP_EXPORT void displayExtremities<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &,
286  const vpHomogeneousMatrix &, double, double, const vpColor &,
287  double);
288 
289 template <class ImageDataType>
290 void display(const usBSpline3D &spline, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
291  double Xscale, double Yscale, const vpColor &color, int nbRenderingLines, double visibilityDistance)
292 {
293  displayLine(spline, I, imageMworld, Xscale, Yscale, color, nbRenderingLines, visibilityDistance);
294  displayExtremities(spline, I, imageMworld, Xscale, Yscale, color, visibilityDistance);
295 }
296 template VISP_EXPORT void display<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
297  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
298  double);
299 template VISP_EXPORT void display<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &, const vpHomogeneousMatrix &,
300  double, double, const vpColor &, int, double);
301 
302 template <class ImageDataType>
303 void displayCurvatureFromShape(const usBSpline3D &spline, const vpImage<ImageDataType> &I,
304  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale,
305  const vpColor &color)
306 {
307  vpColVector C(4);
308  vpColVector D(4);
309  double k = spline.getCurvatureFromShape(0, -1, C, D);
310  if (k == 0)
311  return;
312 
313  double r = 1 / k;
314 
315  C = imageMworld * C;
316  C.resize(3, false);
317  D = imageMworld * D;
318  D.resize(3, false);
319 
320  vpColVector Dxy(3, 0);
321  Dxy[2] = 1;
322  vpColVector Axy = vpColVector::cross(D, Dxy);
323  vpColVector Bxy = vpColVector::cross(D, Axy).normalize();
324 
325  double thetaxy = atan2(Axy[1], Axy[0]);
326  double axy = r;
327  double bxy = r * vpColVector::cross(Bxy, Dxy).frobeniusNorm();
328  if (Bxy.frobeniusNorm() < 0.5)
329  bxy = axy; // if normalization failed the circle is almost in the plane
330 
331  // Scaling
332  vpImagePoint centerXY(Yscale * C[1], Xscale * C[0]);
333  double A = sqrt(pow(Xscale * cos(thetaxy), 2) + pow(Yscale * sin(thetaxy), 2));
334  axy *= A;
335  bxy *= A;
336  thetaxy = atan2(axy * sin(thetaxy), bxy * cos(thetaxy));
337  vpDisplay::displayEllipse(I, centerXY, axy, bxy, thetaxy, false, color);
338 }
339 template VISP_EXPORT void displayCurvatureFromShape<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
340  const vpHomogeneousMatrix &, double, double,
341  const vpColor &);
342 template VISP_EXPORT void displayCurvatureFromShape<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &,
343  const vpHomogeneousMatrix &, double, double,
344  const vpColor &);
345 
346 } // namespace usGeometryDisplayTools
const usPolynomialCurve3D & accessSegment(int i) const
int getNbSegments() const
Parameters setters and getters.
Definition: usBSpline3D.cpp:59
const usPolynomialCurve3D & accessLastSegment() const
double getCurvatureFromShape(double start, double end, vpColVector &center3D, vpColVector &direction3D) const
Curvature.
vpColVector getDirection() const
vpColVector getPosition() const
double getEndParameter() const
double getStartParameter() const
vpMatrix getPoints(vpColVector parameters) const
double getStartParameter() const
vpMatrix getPoints(const vpColVector &parameters) const
vpColVector getEndPoint() const
double getCurvatureFromShape(double start, double end, vpColVector &center3D, vpColVector &direction3D) const
double getEndParameter() const
vpColVector getStartPoint() const
template VISP_EXPORT void displayLine< vpRGBa >(const usBSpline3D &, const vpImage< vpRGBa > &, const vpHomogeneousMatrix &, double, double, const vpColor &, int, double)
template VISP_EXPORT void display< unsigned char >(const usPolynomialCurve2D &, const vpImage< unsigned char > &, double, double, const vpColor &, int)
template VISP_EXPORT void displayFrame< unsigned char >(const vpImage< unsigned char > &, const vpHomogeneousMatrix &, double, double)
template VISP_EXPORT void displayCurvatureFromShape< unsigned char >(const usPolynomialCurve3D &, const vpImage< unsigned char > &, const vpHomogeneousMatrix &, double, double, const vpColor &)
VISP_EXPORT void displayExtremities(const usBSpline3D &spline, const vpImage< ImageDataType > &I, const vpHomogeneousMatrix &imageMworld, double Xscale=3000, double Yscale=3000, const vpColor &color=vpColor::red, double visibilityDistance=std::numeric_limits< double >::infinity())
VISP_EXPORT void display(const usOrientedPlane3D &plane, const vpImage< ImageDataType > &I, const vpHomogeneousMatrix &imageMworld, double Xscale=3000, double Yscale=3000, const vpColor &color=vpColor::green)
Display usOrientedPlane3D.
VISP_EXPORT void displayLine(const usBSpline3D &spline, const vpImage< ImageDataType > &I, const vpHomogeneousMatrix &imageMworld, double Xscale=3000, double Yscale=3000, const vpColor &color=vpColor::red, int nbRenderingLinesPerSegment=10, double visibilityDistance=std::numeric_limits< double >::infinity())
Display usBSpline3D.
VISP_EXPORT void displayFrame(const vpImage< ImageDataType > &I, const vpHomogeneousMatrix &imageMFrame, double Xscale=3000, double Yscale=3000)
template VISP_EXPORT void displayExtremities< vpRGBa >(const usBSpline3D &, const vpImage< vpRGBa > &, const vpHomogeneousMatrix &, double, double, const vpColor &, double)
VISP_EXPORT void displayCurvatureFromShape(const usPolynomialCurve3D &spline, const vpImage< ImageDataType > &I, const vpHomogeneousMatrix &imageMworld, double Xscale=3000, double Yscale=3000, const vpColor &color=vpColor::black)
template VISP_EXPORT void display< vpRGBa >(const usPolynomialCurve2D &, const vpImage< vpRGBa > &, double, double, const vpColor &, int)
template VISP_EXPORT void displayCurvatureFromShape< vpRGBa >(const usPolynomialCurve3D &, const vpImage< vpRGBa > &, const vpHomogeneousMatrix &, double, double, const vpColor &)
template VISP_EXPORT void displayLine< unsigned char >(const usBSpline3D &, const vpImage< unsigned char > &, const vpHomogeneousMatrix &, double, double, const vpColor &, int, double)
template VISP_EXPORT void displayFrame< vpRGBa >(const vpImage< vpRGBa > &, const vpHomogeneousMatrix &, double, double)
template VISP_EXPORT void displayExtremities< unsigned char >(const usBSpline3D &, const vpImage< unsigned char > &, const vpHomogeneousMatrix &, double, double, const vpColor &, double)