Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
plot2d.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 <visp3/core/vpConfig.h>
44 #include <visp3/core/vpDebug.h>
45 
46 #include <visp3/core/vpMath.h>
47 #include <visp3/gui/vpPlot.h>
48 
49 int main()
50 {
51 #if defined(VISP_HAVE_DISPLAY)
52  try {
53  vpPlot plot(2, 700, 700, 100, 200, "Curves...");
54 
55  // Change the default font
56  // plot.setFont("-misc-fixed-bold-r-semicondensed--0-0-75-75-c-0-iso8859-10");
57 
58  // Initialize the number of curve for each graphic
59  plot.initGraph(0, 1);
60  plot.initGraph(1, 1);
61 
62  // Set the color of the curves
63  plot.setColor(0, 0, vpColor::green);
64  plot.setColor(1, 0, vpColor::red);
65 
66  // Set the titles of the graphic
67  char title[40];
68  strncpy(title, "cos function", 40);
69  plot.setTitle(0, title);
70  strncpy(title, "sin function", 40);
71  plot.setTitle(1, title);
72 
73  // Set the legend of each curves
74  char legend[40];
75  strncpy(legend, "cos x", 40);
76  plot.setLegend(0, 0, legend);
77  strncpy(legend, "sin x", 40);
78  plot.setLegend(1, 0, legend);
79 
80  // Set the x axis legend of each curves
81  char unit[40];
82  strncpy(unit, "x", 40);
83  plot.setUnitX(0, unit);
84  strncpy(unit, "x", 40);
85  plot.setUnitX(1, unit);
86 
87  // Set the y axis legend of each curves
88  strncpy(unit, "y", 40);
89  plot.setUnitY(0, unit);
90  strncpy(unit, "y", 40);
91  plot.setUnitY(1, unit);
92 
93  // Plot the cosinus and sinus functions
94  double i = 0;
95  while (i <= 20 * 2 * M_PI) {
96  double co = cos(i);
97  double si = sin(i);
98  plot.plot(0, 0, i, co);
99  plot.plot(1, 0, i, si);
100  i += 0.1;
101  }
102 
103  vpDisplay::getClick(plot.I);
104 
105  // Save the datas as text files
106  plot.saveData(0, "dataCos.txt", "# ");
107  plot.saveData(1, "dataSin.txt", "# ");
108  return EXIT_SUCCESS;
109  } catch (const vpException &e) {
110  std::cout << "Catch an exception: " << e << std::endl;
111  return EXIT_FAILURE;
112  }
113 
114 #else
115  std::cout << "Plot functionalities are not avalaible since no display is "
116  "available."
117  << std::endl;
118  return EXIT_SUCCESS;
119 #endif
120 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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