Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpPlot.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 ViSP 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 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Plot curves.
32 */
33
34#include <visp3/core/vpConfig.h>
35
36#if defined(VISP_HAVE_DISPLAY)
37#include <fstream>
38#include <list>
39#include <vector>
40#include <visp3/core/vpMath.h>
41#include <visp3/core/vpMeterPixelConversion.h>
42#include <visp3/core/vpPixelMeterConversion.h>
43#include <visp3/gui/vpDisplayD3D.h>
44#include <visp3/gui/vpDisplayGDI.h>
45#include <visp3/gui/vpDisplayGTK.h>
46#include <visp3/gui/vpDisplayOpenCV.h>
47#include <visp3/gui/vpDisplayX.h>
48#include <visp3/gui/vpPlot.h>
49
51
58vpPlot::vpPlot() : I(), display(nullptr), graphNbr(1), graphList(nullptr), margei(30), margej(40), factori(1.f), factorj(1.)
59{ }
60
78vpPlot::vpPlot(unsigned int graph_nbr, unsigned int height, unsigned int width, int x, int y, const std::string &title)
79 : I(), display(nullptr), graphNbr(1), graphList(nullptr), margei(30), margej(40), factori(1.f), factorj(1.)
80{
81 init(graph_nbr, height, width, x, y, title);
82}
83
97void vpPlot::init(unsigned int graph_nbr, unsigned int height, unsigned int width, int x, int y,
98 const std::string &title)
99{
100 I.init(height, width, 255);
101
102#if defined(VISP_HAVE_X11)
103 display = new vpDisplayX;
104#elif defined(VISP_HAVE_GDI)
105 display = new vpDisplayGDI;
106#elif defined(HAVE_OPENCV_HIGHGUI)
107 display = new vpDisplayOpenCV;
108#elif defined(VISP_HAVE_GTK)
109 display = new vpDisplayGTK;
110#elif defined(VISP_HAVE_D3D9)
111 display = new vpDisplayD3D;
112#endif
113
114 display->init(I, x, y, title);
115
117
118 factori = height / 700.0f;
119 factorj = width / 700.0f;
120
121 initNbGraph(graph_nbr);
122}
123
128{
129 if (graphList != nullptr) {
130 delete[] graphList;
131 graphList = nullptr;
132 }
133 if (display != nullptr) {
134 delete display;
135 display = nullptr;
136 }
137}
138
143{
144 closeDisplay();
145}
146
156void vpPlot::initNbGraph(unsigned int nbGraph)
157{
158 if (nbGraph > 4) {
159 throw vpException(vpException::dimensionError, "Cannot create more than 4 graphs");
160 }
161 graphList = new vpPlotGraph[nbGraph];
162 graphNbr = nbGraph;
163
164 switch (nbGraph) {
165 case 1:
166 graphList[0].initSize(vpImagePoint(0, 0), static_cast<unsigned int>(700 * factorj), static_cast<unsigned int>(700 * factori), margei,
167 margej);
168 break;
169 case 2:
170 graphList[0].initSize(vpImagePoint(0, 0), static_cast<unsigned int>(700 * factorj), static_cast<unsigned int>(350 * factori), margei,
171 margej);
172 graphList[1].initSize(vpImagePoint(static_cast<unsigned int>(350 * factori), 0), static_cast<unsigned int>(700 * factorj),
173 static_cast<unsigned int>(350 * factori), margei, margej);
174 break;
175 case 3:
176 graphList[0].initSize(vpImagePoint(0, 0), static_cast<unsigned int>(350 * factorj), static_cast<unsigned int>(350 * factori), margei,
177 margej);
178 graphList[1].initSize(vpImagePoint(0, static_cast<unsigned int>(350 * factorj)), static_cast<unsigned int>(350 * factorj),
179 static_cast<unsigned int>(350 * factori), margei, margej);
180 graphList[2].initSize(vpImagePoint(static_cast<unsigned int>(350 * factori), 0), static_cast<unsigned int>(700 * factorj),
181 static_cast<unsigned int>(350 * factori), margei, margej);
182 break;
183 case 4:
184 graphList[0].initSize(vpImagePoint(0, 0), static_cast<unsigned int>(350 * factorj), static_cast<unsigned int>(350 * factori), margei,
185 margej);
186 graphList[1].initSize(vpImagePoint(0, static_cast<unsigned int>(350 * factorj)), static_cast<unsigned int>(350 * factorj),
187 static_cast<unsigned int>(350 * factori), margei, margej);
188 graphList[2].initSize(vpImagePoint(static_cast<unsigned int>(350 * factori), 0), static_cast<unsigned int>(350 * factorj),
189 static_cast<unsigned int>(350 * factori), margei, margej);
190 graphList[3].initSize(vpImagePoint(static_cast<unsigned int>(350 * factori), static_cast<unsigned int>(350 * factorj)),
191 static_cast<unsigned int>(350 * factorj), static_cast<unsigned int>(350 * factori), margei, margej);
192 break;
193 }
194
195 for (unsigned int i = 0; i < graphNbr; ++i) {
196 graphList[i].title.clear();
197 graphList[i].unitx.clear();
198 graphList[i].unity.clear();
199 graphList[i].unitz.clear();
200 }
201}
202
212void vpPlot::initGraph(unsigned int graphNum, unsigned int curveNbr) { (graphList + graphNum)->initGraph(curveNbr); }
213
224void vpPlot::initRange(unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
225{
226 (graphList + graphNum)->initScale(I, xmin, xmax, 10, ymin, ymax, 10, true, true);
227}
228
241void vpPlot::initRange(unsigned int graphNum, double xmin, double xmax, double ymin, double ymax, double zmin,
242 double zmax)
243{
244 (graphList + graphNum)->initScale(I, xmin, xmax, 10, ymin, ymax, 10, zmin, zmax, 10, true, true);
245}
246
255void vpPlot::setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
256{
257 (graphList + graphNum)->setCurveColor(curveNum, color);
258}
259
263void vpPlot::displayGrid()
264{
265 for (unsigned int i = 0; i < graphNbr; ++i)
266 graphList[i].displayGrid(I);
267}
268
279void vpPlot::plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
280{
281 (graphList + graphNum)->plot(I, curveNum, x, y);
282}
283
295void vpPlot::plot(unsigned int graphNum, double x, const vpColVector &v_y)
296{
297 if ((graphList + graphNum)->curveNbr == v_y.getRows()) {
298 for (unsigned int i = 0; i < v_y.getRows(); ++i)
299 this->plot(graphNum, i, x, v_y[i]);
300 }
301 else {
302 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
303 }
304}
305
317void vpPlot::plot(unsigned int graphNum, double x, const vpRowVector &v_y)
318{
319 if ((graphList + graphNum)->curveNbr == v_y.getRows()) {
320 for (unsigned int i = 0; i < v_y.getRows(); ++i)
321 this->plot(graphNum, i, x, v_y[i]);
322 }
323 else {
324 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
325 }
326}
327
341void vpPlot::plot(unsigned int graphNum, double x, const vpPoseVector &v_y)
342{
343 if ((graphList + graphNum)->curveNbr == v_y.getRows()) {
344 for (unsigned int i = 0; i < v_y.getRows(); ++i)
345 this->plot(graphNum, i, x, v_y[i]);
346 }
347 else {
348 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
349 }
350}
351
364void vpPlot::plot(unsigned int graphNum, double x, const vpTranslationVector &v_y)
365{
366 if ((graphList + graphNum)->curveNbr == v_y.getRows()) {
367 for (unsigned int i = 0; i < v_y.getRows(); ++i)
368 this->plot(graphNum, i, x, v_y[i]);
369 }
370 else {
371 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
372 }
373}
374
388void vpPlot::plot(unsigned int graphNum, double x, const vpRotationVector &v_y)
389{
390 if ((graphList + graphNum)->curveNbr == v_y.size()) {
391 for (unsigned int i = 0; i < v_y.size(); ++i)
392 this->plot(graphNum, i, x, v_y[i]);
393 }
394 else {
395 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
396 }
397}
398
416vpMouseButton::vpMouseButtonType vpPlot::plot(unsigned int graphNum, unsigned int curveNum, double x, double y,
417 double z)
418{
419 return (graphList + graphNum)->plot(I, curveNum, x, y, z);
420}
421
437vpMouseButton::vpMouseButtonType vpPlot::plot(unsigned int graphNum, double x, const vpColVector &v_y,
438 const vpColVector &v_z)
439{
441 if ((graphList + graphNum)->curveNbr == v_y.getRows() && (graphList + graphNum)->curveNbr == v_z.getRows()) {
442 for (unsigned int i = 0; i < v_y.getRows(); ++i)
443 button = this->plot(graphNum, i, x, v_y[i], v_z[i]);
444 }
445 else {
446 throw(vpException(vpException::dimensionError, "Error in plot vector: not the right dimension"));
447 }
448 return button;
449}
450
459{
461
462 bool blocked = false;
463 unsigned int iblocked = 0;
464 vpImagePoint iP;
465
466 while (b != vpMouseButton::button3) {
467 if (!blocked) {
469 for (unsigned int i = 0; i < graphNbr; ++i) {
470 if (iP.inRectangle((graphList + i)->graphZone)) {
471 iblocked = i;
472 break;
473 }
474 }
475 if ((graphList + iblocked)->move(I, b)) {
476 (graphList + iblocked)->replot3D(I);
477 }
478 blocked = (graphList + iblocked)->blocked;
479 }
480 else {
481 if ((graphList + iblocked)->move(I, b)) {
482 (graphList + iblocked)->replot3D(I);
483 }
484 blocked = (graphList + iblocked)->blocked;
485 }
486 vpTime::sleepMs(20);
487 }
488}
489
496void vpPlot::getPixelValue(bool block)
497{
498 vpImagePoint iP;
499
500 if (block)
502 else
504
505 for (unsigned int i = 0; i < graphNbr; ++i) {
506 if ((graphList + i)->getPixelValue(I, iP))
507 break;
508 }
509}
510
519void vpPlot::setTitle(unsigned int graphNum, const std::string &title) { (graphList + graphNum)->setTitle(title); }
520
529void vpPlot::setUnitX(unsigned int graphNum, const std::string &unitx) { (graphList + graphNum)->setUnitX(unitx); }
530
539void vpPlot::setUnitY(unsigned int graphNum, const std::string &unity) { (graphList + graphNum)->setUnitY(unity); }
540
549void vpPlot::setUnitZ(unsigned int graphNum, const std::string &unitz) { (graphList + graphNum)->setUnitZ(unitz); }
550
561void vpPlot::setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
562{
563 (graphList + graphNum)->setLegend(curveNum, legend);
564}
565
574void vpPlot::resetPointList(unsigned int graphNum)
575{
576 for (unsigned int i = 0; i < (graphList + graphNum)->curveNbr; ++i)
577 (graphList + graphNum)->resetPointList(i);
578}
579
589void vpPlot::setThickness(unsigned int graphNum, unsigned int curveNum, unsigned int thickness)
590{
591 (graphList + graphNum)->setCurveThickness(curveNum, thickness);
592}
593
602void vpPlot::setGraphThickness(unsigned int graphNum, unsigned int thickness)
603{
604 for (unsigned int curveNum = 0; curveNum < (graphList + graphNum)->curveNbr; ++curveNum)
605 (graphList + graphNum)->setCurveThickness(curveNum, thickness);
606}
607
617void vpPlot::setGridThickness(unsigned int graphNum, unsigned int thickness)
618{
619 (graphList + graphNum)->setGridThickness(thickness);
620}
621
632void vpPlot::resetPointList(unsigned int graphNum, unsigned int curveNum)
633{
634 (graphList + graphNum)->resetPointList(curveNum);
635}
636
664void vpPlot::saveData(unsigned int graphNum, const std::string &dataFile, const std::string &title_prefix)
665{
666 std::ofstream fichier;
667 fichier.open(dataFile.c_str());
668
669 unsigned int ind;
670 double *p = new double[3];
671 bool end = false;
672
673 std::vector<std::list<double>::const_iterator> vec_iter_pointListx((graphList + graphNum)->curveNbr);
674 std::vector<std::list<double>::const_iterator> vec_iter_pointListy((graphList + graphNum)->curveNbr);
675 std::vector<std::list<double>::const_iterator> vec_iter_pointListz((graphList + graphNum)->curveNbr);
676
677 fichier << title_prefix << (graphList + graphNum)->title << std::endl;
678
679 for (ind = 0; ind < (graphList + graphNum)->curveNbr; ++ind) {
680 vec_iter_pointListx[ind] = (graphList + graphNum)->curveList[ind].pointListx.begin();
681 vec_iter_pointListy[ind] = (graphList + graphNum)->curveList[ind].pointListy.begin();
682 vec_iter_pointListz[ind] = (graphList + graphNum)->curveList[ind].pointListz.begin();
683 // (graphList+graphNum)->curveList[ind].pointListx.front();
684 // (graphList+graphNum)->curveList[ind].pointListy.front();
685 // (graphList+graphNum)->curveList[ind].pointListz.front();
686 }
687
688 while (end == false) {
689 end = true;
690 for (ind = 0; ind < (graphList + graphNum)->curveNbr; ++ind) {
691 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
692 // &&
693 // !(graphList+graphNum)->curveList[ind].pointListy.outside()
694 // &&
695 // !(graphList+graphNum)->curveList[ind].pointListz.outside())
696 if ((vec_iter_pointListx[ind] != (graphList + graphNum)->curveList[ind].pointListx.end()) &&
697 (vec_iter_pointListy[ind] != (graphList + graphNum)->curveList[ind].pointListy.end()) &&
698 (vec_iter_pointListz[ind] != (graphList + graphNum)->curveList[ind].pointListz.end())) {
699 p[0] = *vec_iter_pointListx[ind];
700 p[1] = *vec_iter_pointListy[ind];
701 p[2] = *vec_iter_pointListz[ind];
702 // p[0] =
703 // (graphList+graphNum)->curveList[ind].pointListx.value();
704 // p[1] =
705 // (graphList+graphNum)->curveList[ind].pointListy.value();
706 // p[2] =
707 // (graphList+graphNum)->curveList[ind].pointListz.value();
708
709 fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
710 ++vec_iter_pointListx[ind];
711 ++vec_iter_pointListy[ind];
712 ++vec_iter_pointListz[ind];
713 // (graphList+graphNum)->curveList[ind].pointListx.next();
714 // (graphList+graphNum)->curveList[ind].pointListy.next();
715 // (graphList+graphNum)->curveList[ind].pointListz.next();
716 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
717 // &&
718 // !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
719 // &&
720 // !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
721 if ((vec_iter_pointListx[ind] != (graphList + graphNum)->curveList[ind].pointListx.end()) &&
722 (vec_iter_pointListy[ind] != (graphList + graphNum)->curveList[ind].pointListy.end()) &&
723 (vec_iter_pointListz[ind] != (graphList + graphNum)->curveList[ind].pointListz.end()))
724 end = false;
725 }
726 else {
727 // p[0] =
728 // (graphList+graphNum)->curveList[ind].pointListx.value();
729 // p[1] =
730 // (graphList+graphNum)->curveList[ind].pointListy.value();
731 // p[2] =
732 // (graphList+graphNum)->curveList[ind].pointListz.value();
733 p[0] = (graphList + graphNum)->curveList[ind].pointListx.back();
734 p[1] = (graphList + graphNum)->curveList[ind].pointListy.back();
735 p[2] = (graphList + graphNum)->curveList[ind].pointListz.back();
736 fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
737 }
738 }
739 fichier << std::endl;
740 }
741
742 delete[] p;
743 fichier.close();
744}
745
746END_VISP_NAMESPACE
747
748#elif !defined(VISP_BUILD_SHARED_LIBS)
749// Work around to avoid warning: libvisp_gui.a(vpPlot.cpp.o) has no symbols
750void dummy_vpPlot() { }
751#endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
unsigned int getRows() const
Definition vpArray2D.h:433
Implementation of column vector and the associated operations.
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static bool getPointerPosition(const vpImage< unsigned char > &I, vpImagePoint &ip)
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
bool inRectangle(const vpRect &rect) const
void closeDisplay()
Definition vpPlot.cpp:127
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition vpPlot.cpp:212
vpImage< unsigned char > I
Definition vpPlot.h:119
void setUnitY(unsigned int graphNum, const std::string &unity)
Definition vpPlot.cpp:539
vpPlot()
Definition vpPlot.cpp:58
virtual ~vpPlot()
Definition vpPlot.cpp:142
void init(unsigned int nbGraph, unsigned int height=700, unsigned int width=700, int x=-1, int y=-1, const std::string &title="")
Definition vpPlot.cpp:97
void initRange(unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition vpPlot.cpp:224
void setGridThickness(unsigned int graphNum, unsigned int thickness)
Definition vpPlot.cpp:617
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
Definition vpPlot.cpp:561
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
Definition vpPlot.cpp:279
void setUnitX(unsigned int graphNum, const std::string &unitx)
Definition vpPlot.cpp:529
void setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
Definition vpPlot.cpp:255
void setThickness(unsigned int graphNum, unsigned int curveNum, unsigned int thickness)
Definition vpPlot.cpp:589
void navigate(void)
Definition vpPlot.cpp:458
void setGraphThickness(unsigned int graphNum, unsigned int thickness)
Definition vpPlot.cpp:602
void setUnitZ(unsigned int graphNum, const std::string &unitz)
Definition vpPlot.cpp:549
void setTitle(unsigned int graphNum, const std::string &title)
Definition vpPlot.cpp:519
void saveData(unsigned int graphNum, const std::string &dataFile, const std::string &title_prefix="")
Definition vpPlot.cpp:664
void resetPointList(unsigned int graphNum)
Definition vpPlot.cpp:574
void getPixelValue(bool block)
Definition vpPlot.cpp:496
Implementation of a pose vector and operations on poses.
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Class that consider the case of a translation vector.
VISP_EXPORT void sleepMs(double t)