Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
plot3d.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Example which describes how to use the vpPlot class
33  *
34  *
35 *****************************************************************************/
36 
43 #include <iostream>
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/gui/vpPlot.h>
46 
47 int main()
48 {
49 #if defined(VISP_HAVE_DISPLAY)
50  try {
51  // Create a window with one graphic
52  vpPlot plot(1);
53 
54  // Change the default font
55  // plot.setFont("-misc-fixed-bold-r-semicondensed--0-0-75-75-c-0-iso8859-10");
56 
57  // The graphic contains 2 curves
58  plot.initGraph(0, 2);
59 
60  // Set the graphic parameters
61  plot.setTitle(0, "First graphic");
62  plot.setUnitX(0, "time (s)");
63  plot.setUnitY(0, "y");
64  plot.setUnitZ(0, "z");
65  plot.setLegend(0, 0, "y^2+z^2=1 and y(0) = 1");
66  plot.setLegend(0, 1, "y^2+z^2=1 and y(0) = -1");
67  plot.setColor(0, 0, vpColor::red);
68  plot.setColor(0, 1, vpColor::green);
69 
70  double x = 0;
71  double y = 1;
72  double z = 0;
73  double dx = 0.08;
74  double dy = 0.04;
75  double zsign = 1.0;
76 
77  unsigned long iter = 0;
78 
79  std::cout << "Hit CTRL-C to or right mouse button to exit..." << std::endl;
80  bool end = false;
81  while (!end) {
82  if (iter < 300) {
83  // y*y+z*z = 1
84  if (fabs(y) < 1.0)
85  z = sqrt(1.0 - y * y);
86  else
87  z = 0;
88 
89  // Add points to the graphic
90  if (plot.plot(0, 0, x, y, z * zsign) == vpMouseButton::button3)
91  end = true;
92  if (plot.plot(0, 1, x, -y, -z * zsign) == vpMouseButton::button3)
93  end = true;
94 
95  x += dx;
96 
97  if (fabs(y) >= 1.0)
98  dy = -dy;
99  y += dy;
100  if (fabs(y) >= 1.0)
101  zsign = -zsign;
102  } else {
103  // Tip: to allows modifying the point of view with the mouse we
104  // plot always the last point
105  if (plot.plot(0, 0, x, y, z * zsign) == vpMouseButton::button3)
106  end = true;
107  if (plot.plot(0, 1, x, -y, -z * zsign) == vpMouseButton::button3)
108  end = true;
109  }
110  iter++;
111  }
112  return EXIT_SUCCESS;
113  } catch (const vpException &e) {
114  std::cout << "Catch an exception: " << e << std::endl;
115  return EXIT_FAILURE;
116  }
117 #else
118  std::cout << "Plot functionalities are not avalaible since no display is "
119  "available."
120  << std::endl;
121  return EXIT_SUCCESS;
122 #endif
123 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
error that can be emitted by ViSP classes.
Definition: vpException.h:59
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:109