UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
testUsNeedleInsertionModelVirtualSprings.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 
41 #include <visp3/core/vpConfig.h>
42 
43 #include <iostream>
44 #include <stdlib.h>
45 #include <string>
46 
47 #include <visp3/gui/vpDisplayD3D.h>
48 #include <visp3/gui/vpDisplayGDI.h>
49 #include <visp3/gui/vpDisplayGTK.h>
50 #include <visp3/gui/vpDisplayOpenCV.h>
51 #include <visp3/gui/vpDisplayX.h>
52 
53 #include <visp3/io/vpParseArgv.h>
54 
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpImage.h>
57 #include <visp3/core/vpPoseVector.h>
58 
59 #include <visp3/ustk_needle_modeling/usNeedleInsertionModelVirtualSprings.h>
60 #include <visp3/ustk_needle_modeling/usNeedleModelingDisplayTools.h>
61 
62 // List of allowed command line options
63 #define GETOPTARGS "hlt:cd"
64 
65 typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
66 
67 void usage(const char *name, const char *badparam, vpDisplayType &dtype);
68 bool getOptions(int argc, const char **argv, vpDisplayType &dtype, bool &list, bool &display);
69 
80 void usage(const char *name, const char *badparam, vpDisplayType &dtype)
81 {
82  fprintf(stdout, "\n\
83 Test the class usNeedleInsertionModelVirtualSprings.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-t <type of video device>] [-l] [-d] [-h]\n\
87 ", name);
88 
89  std::string display;
90  switch (dtype) {
91  case vpX11:
92  display = "X11";
93  break;
94  case vpGTK:
95  display = "GTK";
96  break;
97  case vpGDI:
98  display = "GDI";
99  break;
100  case vpD3D:
101  display = "D3D";
102  break;
103  case vpCV:
104  display = "CV";
105  break;
106  }
107 
108  fprintf(stdout, "\n\
109 OPTIONS: Default\n\
110 \n\
111  -t <type of video device> \"%s\"\n\
112  String specifying the video device to use.\n\
113  Possible values:\n\
114  \"X11\": only on UNIX platforms,\n\
115  \"GTK\": on all plaforms,\n\
116  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
117  \"D3D\": only on Windows platform (Direct3D).\n\
118  \"CV\" : (OpenCV).\n\
119 \n\
120  -l\n\
121  Print the list of video-devices available and exit.\n\
122 \n\
123  -d \n\
124  Turn off the display.\n\
125 \n\
126  -h\n\
127  Print the help.\n\n", display.c_str());
128 
129  if (badparam)
130  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
131 }
132 
149 bool getOptions(int argc, const char **argv, vpDisplayType &dtype, bool &list, bool &display)
150 {
151  const char *optarg_;
152  int c;
153  std::string sDisplayType;
154  while((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1)
155  {
156 
157  switch (c)
158  {
159  case 'l':
160  list = true;
161  break;
162 
163  case 't':
164  sDisplayType = optarg_;
165  // Parse the display type option
166  if(sDisplayType.compare("X11") == 0) dtype = vpX11;
167  else if (sDisplayType.compare("GTK") == 0) dtype = vpGTK;
168  else if (sDisplayType.compare("GDI") == 0) dtype = vpGDI;
169  else if (sDisplayType.compare("D3D") == 0) dtype = vpD3D;
170  else if (sDisplayType.compare("CV") == 0) dtype = vpCV;
171  break;
172 
173  case 'h':
174  usage(argv[0], NULL, dtype);
175  return false;
176  break;
177 
178  case 'c':
179  break;
180 
181  case 'd':
182  display = false;
183  break;
184 
185  default:
186  usage(argv[0], optarg_, dtype);
187  return false;
188  break;
189  }
190  }
191 
192  if((c == 1) || (c == -1))
193  {
194  // standalone param or error
195  usage(argv[0], NULL, dtype);
196  std::cerr << "ERROR: " << std::endl;
197  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
198  return false;
199  }
200 
201  return true;
202 }
203 
204 
205 
206 int main(int argc, const char **argv)
207 {
208  bool opt_list = false; // To print the list of video devices
209  vpDisplayType opt_dtype; // Type of display to use
210  bool opt_display = true;
211 
212 // Default display is one available
213 #if defined VISP_HAVE_GTK
214  opt_dtype = vpGTK;
215 #elif defined VISP_HAVE_X11
216  opt_dtype = vpX11;
217 #elif defined VISP_HAVE_GDI
218  opt_dtype = vpGDI;
219 #elif defined VISP_HAVE_D3D9
220  opt_dtype = vpD3D;
221 #elif defined VISP_HAVE_OPENCV
222  opt_dtype = vpCV;
223 #endif
224 
225  // Read the command line options
226  if(!getOptions(argc, argv, opt_dtype, opt_list, opt_display)) exit(-1);
227 
228  // Print the list of video-devices available
229  if (opt_list) {
230  unsigned nbDevices = 0;
231  std::cout << "List of video-devices available: \n";
232 #if defined VISP_HAVE_GTK
233  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
234  nbDevices++;
235 #endif
236 #if defined VISP_HAVE_X11
237  std::cout << " X11 (use \"-t X11\" option to use it)\n";
238  nbDevices++;
239 #endif
240 #if defined VISP_HAVE_GDI
241  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
242  nbDevices++;
243 #endif
244 #if defined VISP_HAVE_D3D9
245  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
246  nbDevices++;
247 #endif
248 #if defined VISP_HAVE_OPENCV
249  std::cout << " CV (use \"-t CV\" option to use it)\n";
250  nbDevices++;
251 #endif
252  if (!nbDevices) {
253  std::cout << " No display is available\n";
254  }
255  return (0);
256  }
257 
258  vpImage<unsigned char> I(700, 500, 255);
259 
260  vpDisplay *display = nullptr;
261 
262  if(opt_display)
263  {
264  switch (opt_dtype)
265  {
266  case vpX11:
267  std::cout << "Requested X11 display functionnalities..." << std::endl;
268 #if defined VISP_HAVE_X11
269  display = new vpDisplayX;
270 #else
271  std::cout << " Sorry, X11 video device is not available.\n";
272  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
273  return 0;
274 #endif
275  break;
276  case vpGTK:
277  std::cout << "Requested GTK display functionnalities..." << std::endl;
278 #if defined VISP_HAVE_GTK
279  display = new vpDisplayGTK;
280 #else
281  std::cout << " Sorry, GTK video device is not available.\n";
282  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
283  return 0;
284 #endif
285  break;
286  case vpGDI:
287  std::cout << "Requested GDI display functionnalities..." << std::endl;
288 #if defined VISP_HAVE_GDI
289  display = new vpDisplayGDI;
290 #else
291  std::cout << " Sorry, GDI video device is not available.\n";
292  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
293  return 0;
294 #endif
295  break;
296  case vpD3D:
297  std::cout << "Requested D3D display functionnalities..." << std::endl;
298 #if defined VISP_HAVE_D3D9
299  display = new vpDisplayD3D;
300 #else
301  std::cout << " Sorry, D3D video device is not available.\n";
302  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
303  return 0;
304 #endif
305  break;
306  case vpCV:
307  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
308 #if defined(VISP_HAVE_OPENCV)
309  display = new vpDisplayOpenCV;
310 #else
311  std::cout << " Sorry, OpenCV video device is not available.\n";
312  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
313  return 0;
314 #endif
315  break;
316  }
317  }
318 
319  if(opt_display) display->init(I);
320 
322 
324  n.accessNeedle().setOuterDiameter(0.001);
325  n.accessNeedle().setInsideDiameter(0.0007);
327  n.accessNeedle().setFullLength(0.1);
328 
330  n.setStiffnessPerUnitLength(10000);
331 
332  n.setInterSpringDistance(0.005);
333  n.setInterTipSpringDistance(0.001);
334  n.setNbMinTipSprings(5);
335  n.setNbMaxTipSprings(10);
337 
338  n.setBasePose(vpPoseVector(0,0,0.1, M_PI/sqrt(2),M_PI/sqrt(2),0));
339 
341  n1.moveBaseWorldFrame(0.01,0,0,0,0,0);
342 
343  n1.setInterSpringDistance(0.01);
344  n1.setInterTipSpringDistance(0.002);
345  n1.setNbMinTipSprings(4);
346  n1.setNbMaxTipSprings(5);
347 
348  n.setSurfaceAtTip();
349  n1.setSurfaceAtTip();
350 
351  for(int i=0 ; i<300 ; i++)
352  {
353  if(i < 100)
354  {
355  n.moveBase(0,0,0.001,0,0,0);
356  n1.moveBase(0,0,0.001,0,0,0);
357  }
358  else if(i < 200)
359  {
360  n.moveBase(0,0,-0.001,0,0,0);
361  n1.moveBase(0,0,-0.001,0,0,0);
362  }
363  else
364  {
365  n.moveBase(0,0,0.001,0,0,0.05);
366  n1.moveBase(0,0,0.001,0,0,0.05);
367  }
368 
369  if(opt_display)
370  {
371  vpDisplay::display(I);
372 
373  usNeedleModelingDisplayTools::display(n, I, vpHomogeneousMatrix(0.08 ,0.1, 0.2, M_PI/2,0,0), 3000,3000);
374  usNeedleModelingDisplayTools::display(n1, I, vpHomogeneousMatrix(0.08 ,0.1, 0.2, M_PI/2,0,0), 3000,3000);
375 
376  vpDisplay::flush(I);
377  }
378  }
379 
380  if(display) delete display;
381 
382  return 0;
383 
384 }
const usPolynomialCurve3D & accessSegment(int i) const
bool moveBase(const vpColVector &v, double time)
bool setBasePose(const vpPoseVector &p)
The following methods should be redefined in the derived classes.
void loadPreset(const ModelPreset preset)
Parameters saving and loading.
const usNeedleModelSpline & accessNeedle() const
Model parameters.
void setInterTipSpringDistance(double interTipSpringDistance)
void setFullLength(double length)
Parameters setters and getters.
void setNeedleYoungModulus(double E)
void setInsideDiameter(double diameter)
void setOuterDiameter(double diameter)
void setOrder(unsigned int order)
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 display(const usNeedleModelBaseTip &needleModel, vpImage< unsigned char > &I, const vpHomogeneousMatrix &imageMworld, double Xscale=3000, double Yscale=3000)