Visual Servoing Platform  version 3.6.1 under development (2024-12-04)
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 #ifdef ENABLE_VISP_NAMESPACE
51  using namespace VISP_NAMESPACE_NAME;
52 #endif
53 
54  try {
55  // Create a window with one graphic
56  vpPlot plot(1);
57 
58  // Change the default font
59  // plot.setFont("-misc-fixed-bold-r-semicondensed--0-0-75-75-c-0-iso8859-10");
60 
61  // The graphic contains 2 curves
62  plot.initGraph(0, 2);
63 
64  // Set the graphic parameters
65  plot.setTitle(0, "First graphic");
66  plot.setUnitX(0, "time (s)");
67  plot.setUnitY(0, "y");
68  plot.setUnitZ(0, "z");
69  plot.setLegend(0, 0, "y^2+z^2=1 and y(0) = 1");
70  plot.setLegend(0, 1, "y^2+z^2=1 and y(0) = -1");
71  plot.setColor(0, 0, vpColor::red);
72  plot.setColor(0, 1, vpColor::green);
73 
74  double x = 0;
75  double y = 1;
76  double z = 0;
77  double dx = 0.08;
78  double dy = 0.04;
79  double zsign = 1.0;
80 
81  unsigned long iter = 0;
82 
83  std::cout << "Hit CTRL-C to or right mouse button to exit..." << std::endl;
84  bool end = false;
85  while (!end) {
86  if (iter < 300) {
87  // y*y+z*z = 1
88  if (fabs(y) < 1.0)
89  z = sqrt(1.0 - y * y);
90  else
91  z = 0;
92 
93  // Add points to the graphic
94  if (plot.plot(0, 0, x, y, z * zsign) == vpMouseButton::button3)
95  end = true;
96  if (plot.plot(0, 1, x, -y, -z * zsign) == vpMouseButton::button3)
97  end = true;
98 
99  x += dx;
100 
101  if (fabs(y) >= 1.0)
102  dy = -dy;
103  y += dy;
104  if (fabs(y) >= 1.0)
105  zsign = -zsign;
106  }
107  else {
108  // Tip: to allows modifying the point of view with the mouse we
109  // plot always the last point
110  if (plot.plot(0, 0, x, y, z * zsign) == vpMouseButton::button3)
111  end = true;
112  if (plot.plot(0, 1, x, -y, -z * zsign) == vpMouseButton::button3)
113  end = true;
114  }
115  iter++;
116  }
117  return EXIT_SUCCESS;
118  }
119  catch (const vpException &e) {
120  std::cout << "Catch an exception: " << e << std::endl;
121  return EXIT_FAILURE;
122  }
123 #else
124  std::cout << "Plot functionalities are not avalaible since no display is "
125  "available."
126  << std::endl;
127  return EXIT_SUCCESS;
128 #endif
129  }
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
error that can be emitted by ViSP classes.
Definition: vpException.h:60
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:112