UsTK : Ultrasound ToolKit  version 2.0.1 under development (2023-12-07)
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  vpMatrix imagePoints = curve.getPoints(params);
122 
123  double x0 = Xscale * imagePoints[0][0];
124  double y0 = Yscale * imagePoints[1][0];
125  double x1 = x0;
126  double y1 = y0;
127 
128  for (int i = 0; i < nbRenderingLines; i++) {
129  x0 = x1;
130  y0 = y1;
131 
132  x1 = Xscale * imagePoints[0][i + 1];
133  y1 = Yscale * imagePoints[1][i + 1];
134 
135  vpDisplay::displayLine(I, (int)y0, (int)x0, (int)y1, (int)x1, color);
136  }
137 }
138 template VISP_EXPORT void display<unsigned char>(const usPolynomialCurve2D &, const vpImage<unsigned char> &, double,
139  double, const vpColor &, int);
140 template VISP_EXPORT void display<vpRGBa>(const usPolynomialCurve2D &, const vpImage<vpRGBa> &, double, double,
141  const vpColor &, int);
142 
144 
145 template <class ImageDataType>
146 void display(const usPolynomialCurve3D &curve, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
147  double Xscale, double Yscale, const vpColor &color, int nbRenderingLines, double visibilityDistance)
148 {
149  unsigned int nbPoints = nbRenderingLines + 1;
150  double step = (curve.getEndParameter() - curve.getStartParameter()) / nbRenderingLines;
151 
152  std::vector<double> params(nbPoints);
153  params.front() = curve.getStartParameter();
154  for (unsigned int i = 1; i < nbPoints; i++)
155  params.at(i) = params.at(i - 1) + step;
156 
157  vpMatrix points = curve.getPoints(params);
158 
159  const vpMatrix R(imageMworld.getRotationMatrix());
160  const vpMatrix T(imageMworld.getTranslationVector());
161  const vpMatrix ones(1, nbPoints, 1);
162  vpMatrix imagePoints(R * points + vpMatrix::kron(T, ones));
163 
164  double x0 = Xscale * imagePoints[0][0];
165  double y0 = Yscale * imagePoints[1][0];
166  double z0 = imagePoints[2][0];
167  double x1 = x0;
168  double y1 = y0;
169  double z1 = z0;
170 
171  for (int i = 0; i < nbRenderingLines; i++) {
172  x0 = x1;
173  y0 = y1;
174  z0 = z1;
175 
176  x1 = Xscale * imagePoints[0][i + 1];
177  y1 = Yscale * imagePoints[1][i + 1];
178  z1 = imagePoints[2][i + 1];
179 
180  if ((vpMath::sign(z0) != vpMath::sign(z1)) || ((fabs(z0) < visibilityDistance) && (fabs(z1) < visibilityDistance)))
181  vpDisplay::displayLine(I, (int)y0, (int)x0, (int)y1, (int)x1, color);
182  }
183 }
184 
185 template VISP_EXPORT void display<unsigned char>(const usPolynomialCurve3D &, const vpImage<unsigned char> &,
186  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
187  double);
188 template VISP_EXPORT void display<vpRGBa>(const usPolynomialCurve3D &, const vpImage<vpRGBa> &,
189  const vpHomogeneousMatrix &, double, double, const vpColor &, int, double);
190 
191 template <class ImageDataType>
192 void displayCurvatureFromShape(const usPolynomialCurve3D &curve, const vpImage<ImageDataType> &I,
193  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale,
194  const vpColor &color)
195 {
196  vpColVector C(4);
197  vpColVector D(4);
198  double k = curve.getCurvatureFromShape(0, -1, C, D);
199  if (k == 0)
200  return;
201 
202  double r = 1 / k;
203 
204  C = imageMworld * C;
205  C.resize(3, false);
206  D = imageMworld * D;
207  D.resize(3, false);
208 
209  vpColVector Dxy(3, 0);
210  Dxy[2] = 1;
211  vpColVector Axy = vpColVector::cross(D, Dxy);
212  vpColVector Bxy = vpColVector::cross(D, Axy).normalize();
213 
214  double thetaxy = atan2(Axy[1], Axy[0]);
215  double axy = r;
216  double bxy = r * vpColVector::cross(Bxy, Dxy).frobeniusNorm();
217  if (Bxy.frobeniusNorm() < 0.5)
218  bxy = axy; // if normalization failed the circle is almost in the plane
219 
220  // Scaling
221  vpImagePoint centerXY(Yscale * C[1], Xscale * C[0]);
222  double A = sqrt(pow(Xscale * cos(thetaxy), 2) + pow(Yscale * sin(thetaxy), 2));
223  axy *= A;
224  bxy *= A;
225  thetaxy = atan2(axy * sin(thetaxy), bxy * cos(thetaxy));
226  vpDisplay::displayEllipse(I, centerXY, axy, bxy, thetaxy, false, color);
227 }
229  const vpImage<unsigned char> &,
230  const vpHomogeneousMatrix &, double, double,
231  const vpColor &);
232 template VISP_EXPORT void displayCurvatureFromShape<vpRGBa>(const usPolynomialCurve3D &, const vpImage<vpRGBa> &,
233  const vpHomogeneousMatrix &, double, double,
234  const vpColor &);
235 
237 
238 template <class ImageDataType>
239 void displayLine(const usBSpline3D &spline, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
240  double Xscale, double Yscale, const vpColor &color, int nbRenderingLinesPerSegment,
241  double visibilityDistance)
242 {
243  for (int i = 0; i < spline.getNbSegments(); i++)
244  usGeometryDisplayTools::display(spline.accessSegment(i), I, imageMworld, Xscale, Yscale, color,
245  nbRenderingLinesPerSegment, visibilityDistance);
246 }
247 template VISP_EXPORT void displayLine<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
248  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
249  double);
250 template VISP_EXPORT void displayLine<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &, const vpHomogeneousMatrix &,
251  double, double, const vpColor &, int, double);
252 
253 template <class ImageDataType>
254 void displayExtremities(const usBSpline3D &spline, const vpImage<ImageDataType> &I,
255  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale, const vpColor &color,
256  double visibilityDistance)
257 {
258  unsigned int nbPoints = spline.getNbSegments() + 1;
259  if (nbPoints < 2)
260  return;
261 
262  vpMatrix points(3, nbPoints);
263 
264  for (int i = 0; i < spline.getNbSegments(); i++)
265  points.insert(spline.accessSegment(i).getStartPoint(), 0, i);
266  points.insert(spline.accessLastSegment().getEndPoint(), 0, spline.getNbSegments());
267 
268  const vpMatrix R(imageMworld.getRotationMatrix());
269  const vpMatrix T(imageMworld.getTranslationVector());
270  const vpMatrix ones(1, nbPoints, 1);
271  vpMatrix imagePoints(R * points + vpMatrix::kron(T, ones));
272 
273  for (unsigned int i = 0; i < nbPoints; i++) {
274  double x = Xscale * imagePoints[0][i];
275  double y = Yscale * imagePoints[1][i];
276  double z = imagePoints[2][i];
277  if (fabs(z) < visibilityDistance)
278  vpDisplay::displayCross(I, (int)y, (int)x, 7, color);
279  }
280 }
281 template VISP_EXPORT void displayExtremities<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
282  const vpHomogeneousMatrix &, double, double,
283  const vpColor &, double);
284 template VISP_EXPORT void displayExtremities<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &,
285  const vpHomogeneousMatrix &, double, double, const vpColor &,
286  double);
287 
288 template <class ImageDataType>
289 void display(const usBSpline3D &spline, const vpImage<ImageDataType> &I, const vpHomogeneousMatrix &imageMworld,
290  double Xscale, double Yscale, const vpColor &color, int nbRenderingLines, double visibilityDistance)
291 {
292  displayLine(spline, I, imageMworld, Xscale, Yscale, color, nbRenderingLines, visibilityDistance);
293  displayExtremities(spline, I, imageMworld, Xscale, Yscale, color, visibilityDistance);
294 }
295 template VISP_EXPORT void display<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
296  const vpHomogeneousMatrix &, double, double, const vpColor &, int,
297  double);
298 template VISP_EXPORT void display<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &, const vpHomogeneousMatrix &,
299  double, double, const vpColor &, int, double);
300 
301 template <class ImageDataType>
302 void displayCurvatureFromShape(const usBSpline3D &spline, const vpImage<ImageDataType> &I,
303  const vpHomogeneousMatrix &imageMworld, double Xscale, double Yscale,
304  const vpColor &color)
305 {
306  vpColVector C(4);
307  vpColVector D(4);
308  double k = spline.getCurvatureFromShape(0, -1, C, D);
309  if (k == 0)
310  return;
311 
312  double r = 1 / k;
313 
314  C = imageMworld * C;
315  C.resize(3, false);
316  D = imageMworld * D;
317  D.resize(3, false);
318 
319  vpColVector Dxy(3, 0);
320  Dxy[2] = 1;
321  vpColVector Axy = vpColVector::cross(D, Dxy);
322  vpColVector Bxy = vpColVector::cross(D, Axy).normalize();
323 
324  double thetaxy = atan2(Axy[1], Axy[0]);
325  double axy = r;
326  double bxy = r * vpColVector::cross(Bxy, Dxy).frobeniusNorm();
327  if (Bxy.frobeniusNorm() < 0.5)
328  bxy = axy; // if normalization failed the circle is almost in the plane
329 
330  // Scaling
331  vpImagePoint centerXY(Yscale * C[1], Xscale * C[0]);
332  double A = sqrt(pow(Xscale * cos(thetaxy), 2) + pow(Yscale * sin(thetaxy), 2));
333  axy *= A;
334  bxy *= A;
335  thetaxy = atan2(axy * sin(thetaxy), bxy * cos(thetaxy));
336  vpDisplay::displayEllipse(I, centerXY, axy, bxy, thetaxy, false, color);
337 }
338 template VISP_EXPORT void displayCurvatureFromShape<unsigned char>(const usBSpline3D &, const vpImage<unsigned char> &,
339  const vpHomogeneousMatrix &, double, double,
340  const vpColor &);
341 template VISP_EXPORT void displayCurvatureFromShape<vpRGBa>(const usBSpline3D &, const vpImage<vpRGBa> &,
342  const vpHomogeneousMatrix &, double, double,
343  const vpColor &);
344 
345 } // 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)