Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpPlot.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 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 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 http://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  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
38 
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_DISPLAY)
43 #include <visp3/gui/vpPlot.h>
44 #include <visp3/gui/vpDisplayOpenCV.h>
45 #include <visp3/gui/vpDisplayX.h>
46 #include <visp3/gui/vpDisplayGDI.h>
47 #include <visp3/gui/vpDisplayGTK.h>
48 #include <visp3/gui/vpDisplayD3D.h>
49 #include <visp3/core/vpMath.h>
50 #include <visp3/core/vpMeterPixelConversion.h>
51 #include <visp3/core/vpPixelMeterConversion.h>
52 #include <fstream>
53 #include <list>
54 #include <vector>
55 
62 vpPlot::vpPlot() : I(), display(NULL), graphNbr(1), graphList(NULL), margei(30), margej(40),
63  factori(1.f), factorj(1.)
64 {
65 }
82 vpPlot::vpPlot(const unsigned int graph_nbr,
83  const unsigned int height, const unsigned int width,
84  const int x, const int y, const std::string &title)
85  : I(), display(NULL), graphNbr(1), graphList(NULL), margei(30), margej(40),
86  factori(1.f), factorj(1.)
87 {
88  init(graph_nbr, height, width, x, y, title);
89 }
90 
103 void vpPlot::init(const unsigned int graph_nbr,
104  const unsigned int height, const unsigned int width,
105  const int x, const int y, const std::string &title)
106 {
107  I.init(height,width,255);
108 
109 #if defined VISP_HAVE_X11
110  display = new vpDisplayX;
111 #elif defined VISP_HAVE_GDI
112  display = new vpDisplayGDI;
113 #elif defined VISP_HAVE_OPENCV
114  display = new vpDisplayOpenCV;
115 #elif defined VISP_HAVE_GTK
116  display = new vpDisplayGTK;
117 #elif defined VISP_HAVE_D3D9
118  display = new vpDisplayD3D;
119 #endif
120 
121  display->init(I, x, y, title.c_str());
122 
124 
125  factori = height/700.0f;
126  factorj = width/700.0f;
127 
128  initNbGraph(graph_nbr);
129 }
130 
135 {
136  if (graphList != NULL)
137  {
138  delete[] graphList;
139  graphList = NULL;
140  }
141  if (display != NULL)
142  {
143  delete display;
144  display = NULL;
145  }
146 }
147 
156 void
157 vpPlot::initNbGraph (unsigned int nbGraph)
158 {
159  if(nbGraph > 4){
160  throw vpException(vpException::dimensionError, "Cannot create more than 4 graphs");
161  }
162  graphList = new vpPlotGraph[nbGraph];
163  graphNbr = nbGraph;
164 
165  switch (nbGraph)
166  {
167  case 1 :
168  graphList[0].initSize(vpImagePoint(0,0), (unsigned int)(700*factorj),(unsigned int)(700*factori),margei,margej);
169  break;
170  case 2 :
171  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
172  graphList[1].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
173  break;
174  case 3 :
175  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
176  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
177  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
178  break;
179  case 4 :
180  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
181  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
182  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
183  graphList[3].initSize(vpImagePoint((unsigned int)(350*factori),(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
184  break;
185  }
186 
187  for (unsigned int i = 0; i < graphNbr; i++)
188  {
189  graphList[i].title.clear();
190  graphList[i].unitx.clear();
191  graphList[i].unity.clear();
192  graphList[i].unitz.clear();
193  }
194 }
195 
202 void
203 vpPlot::initGraph (unsigned int graphNum, unsigned int curveNbr)
204 {
205  (graphList+graphNum)->initGraph(curveNbr);
206 }
207 
208 
209 // void
210 // vpPlot::initRange (const int graphNum,
211 // double xmin, double xmax, double /*xdelt*/,
212 // double ymin, double ymax, double /*ydelt*/,
213 // const bool gx, const bool gy)
214 // {
215 // (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,gx,gy);
216 // }
217 
227 void
228 vpPlot::initRange (const unsigned int graphNum,
229  double xmin, double xmax,
230  double ymin, double ymax)
231 {
232  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,true,true);
233 }
234 
246 void
247 vpPlot::initRange (const unsigned int graphNum,
248  double xmin, double xmax, double ymin,
249  double ymax, double zmin, double zmax)
250 {
251  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,zmin,zmax,10,true,true);
252 }
253 
261 void
262 vpPlot::setColor (const unsigned int graphNum, const unsigned int curveNum, vpColor color)
263 {
264  (graphList+graphNum)->setCurveColor(curveNum, color);
265 }
266 
270 void
271 vpPlot::displayGrid()
272 {
273  for (unsigned int i = 0; i < graphNbr; i++)
274  graphList[i].displayGrid(I);
275 }
276 
285 void
286 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
287 {
288  (graphList+graphNum)->plot(I,curveNum,x,y);
289 }
290 
298 void vpPlot::plot(const unsigned int graphNum,
299  const double x, const vpColVector &v_y)
300 {
301  if((graphList+graphNum)->curveNbr == v_y.getRows())
302  {
303  for(unsigned int i = 0;i < v_y.getRows();++i)
304  this->plot(graphNum, i, x, v_y[i]);
305  }
306  else
307  vpTRACE("error in plot vector : not the right dimension");
308 }
316 void vpPlot::plot(const unsigned int graphNum,
317  const double x, const vpRowVector &v_y)
318 {
319  if((graphList+graphNum)->curveNbr == v_y.getRows())
320  {
321  for(unsigned int i = 0;i < v_y.getRows();++i)
322  this->plot(graphNum, i, x, v_y[i]);
323  }
324  else
325  vpTRACE("error in plot vector : not the right dimension");
326 }
327 
335 void vpPlot::plot(const unsigned int graphNum,
336  const double x, const vpPoseVector &v_y)
337 {
338  if((graphList+graphNum)->curveNbr == v_y.getRows())
339  {
340  for(unsigned int i = 0;i < v_y.getRows();++i)
341  this->plot(graphNum, i, x, v_y[i]);
342  }
343  else
344  vpTRACE("error in plot vector : not the right dimension");
345 }
353 void vpPlot::plot(const unsigned int graphNum,
354  const double x, const vpTranslationVector &v_y)
355 {
356  if((graphList+graphNum)->curveNbr == v_y.getRows())
357  {
358  for(unsigned int i = 0;i < v_y.getRows();++i)
359  this->plot(graphNum, i, x, v_y[i]);
360  }
361  else
362  vpTRACE("error in plot vector : not the right dimension");
363 }
364 
372 void vpPlot::plot(const unsigned int graphNum,
373  const double x, const vpRotationVector &v_y)
374 {
375  if((graphList+graphNum)->curveNbr == v_y.size())
376  {
377  for(unsigned int i = 0;i < v_y.size();++i)
378  this->plot(graphNum, i, x, v_y[i]);
379  }
380  else
381  vpTRACE("error in plot vector : not the right dimension");
382 }
383 
384 
395 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y, const double z)
396 {
397  return (graphList+graphNum)->plot(I,curveNum,x,y,z);
398 }
399 
409 vpPlot::plot(const unsigned int graphNum, const double x, const vpColVector &v_y, const vpColVector &v_z)
410 {
412  if((graphList+graphNum)->curveNbr == v_y.getRows() && (graphList+graphNum)->curveNbr == v_z.getRows())
413  {
414  for(unsigned int i = 0;i < v_y.getRows();++i)
415  button = this->plot(graphNum, i, x, v_y[i], v_z[i]);
416  }
417  else
418  vpTRACE("error in plot vector : not the right dimension");
419  return button;
420 }
421 
429 void
431 {
433 
434  bool blocked = false;
435  unsigned int iblocked = 0;
436  vpImagePoint iP;
437 
438  while (b != vpMouseButton::button3)
439  {
440  if (!blocked)
441  {
443  for (unsigned int i = 0; i < graphNbr ; i++)
444  {
445  if (iP.inRectangle((graphList+i)->graphZone))
446  {
447  iblocked = i;
448  break;
449  }
450  }
451  if ((graphList+iblocked)->move(I, b))
452  {
453  (graphList+iblocked)->replot3D(I);
454  }
455  blocked = (graphList+iblocked)->blocked;
456  }
457  else
458  {
459  if ((graphList+iblocked)->move(I, b))
460  {
461  (graphList+iblocked)->replot3D(I);
462  }
463  blocked = (graphList+iblocked)->blocked;
464  }
465  vpTime::sleepMs(20);
466  }
467 }
468 
474 void
475 vpPlot::getPixelValue(const bool block)
476 {
477  vpImagePoint iP;
478 
479  if (block)
481  else
483 
484  for (unsigned int i = 0; i < graphNbr; i++)
485  {
486  if ((graphList+i)->getPixelValue(I,iP)) break;
487  }
488 }
489 
496 void
497 vpPlot::setTitle (const unsigned int graphNum, const std::string &title)
498 {
499  (graphList+graphNum)->setTitle(title);
500 }
501 
508 void
509 vpPlot::setUnitX (const unsigned int graphNum, const std::string &unitx)
510 {
511  (graphList+graphNum)->setUnitX(unitx);
512 }
513 
520 void
521 vpPlot::setUnitY (const unsigned int graphNum, const std::string &unity)
522 {
523  (graphList+graphNum)->setUnitY(unity);
524 }
525 
532 void
533 vpPlot::setUnitZ (const unsigned int graphNum, const std::string &unitz)
534 {
535  (graphList+graphNum)->setUnitZ(unitz);
536 }
537 
545 void
546 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const std::string &legend)
547 {
548  (graphList+graphNum)->setLegend(curveNum, legend);
549 }
550 
556 void
557 vpPlot::resetPointList (const unsigned int graphNum)
558 {
559  for (unsigned int i = 0; i < (graphList+graphNum)->curveNbr; i++)
560  (graphList+graphNum)->resetPointList(i);
561 }
562 
570 void
571 vpPlot::setThickness (const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
572 {
573  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
574 }
575 
582 void
583 vpPlot::setGraphThickness (const unsigned int graphNum, const unsigned int thickness)
584 {
585  for (unsigned int curveNum=0; curveNum < (graphList+graphNum)->curveNbr; curveNum++)
586  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
587 }
588 
595 void
596 vpPlot::setGridThickness (const unsigned int graphNum, const unsigned int thickness)
597 {
598  (graphList+graphNum)->setGridThickness(thickness);
599 }
600 
607 void
608 vpPlot::resetPointList (const unsigned int graphNum, const unsigned int curveNum)
609 {
610  (graphList+graphNum)->resetPointList(curveNum);
611 }
612 
634 void vpPlot::saveData(const unsigned int graphNum, const std::string &dataFile, const std::string &title_prefix)
635 {
636  std::ofstream fichier;
637  fichier.open(dataFile.c_str());
638 
639  unsigned int ind;
640  double *p = new double[3];
641  bool end=false;
642 
643  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
644  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
645  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
646 
647  fichier << title_prefix << (graphList+graphNum)->title << std::endl;
648 
649  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
650  {
651  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
652  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
653  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
654 // (graphList+graphNum)->curveList[ind].pointListx.front();
655 // (graphList+graphNum)->curveList[ind].pointListy.front();
656 // (graphList+graphNum)->curveList[ind].pointListz.front();
657  }
658 
659  while (end == false)
660  {
661  end = true;
662  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
663  {
664 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
665 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
666 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
667  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
668  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
669  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
670  {
671  p[0] = *vec_iter_pointListx[ind];
672  p[1] = *vec_iter_pointListy[ind];
673  p[2] = *vec_iter_pointListz[ind];
674 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
675 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
676 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
677 
678  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
679  ++vec_iter_pointListx[ind];
680  ++vec_iter_pointListy[ind];
681  ++vec_iter_pointListz[ind];
682 // (graphList+graphNum)->curveList[ind].pointListx.next();
683 // (graphList+graphNum)->curveList[ind].pointListy.next();
684 // (graphList+graphNum)->curveList[ind].pointListz.next();
685 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
686 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
687 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
688  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
689  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
690  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
691  end = false;
692  }
693  else
694  {
695 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
696 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
697 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
698  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
699  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
700  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
701  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
702  }
703  }
704  fichier << std::endl;
705  }
706 
707  delete[] p;
708  fichier.close();
709 }
710 
711 #elif !defined(VISP_BUILD_SHARED_LIBS)
712 // Work arround to avoid warning: libvisp_core.a(vpPlot.cpp.o) has no symbols
713 void dummy_vpPlot() {};
714 #endif
Implementation of a generic rotation vector.
void initRange(const unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition: vpPlot.cpp:228
bool inRectangle(const vpRect &rect) const
void setColor(const unsigned int graphNum, const unsigned int curveNum, vpColor color)
Definition: vpPlot.cpp:262
static bool getPointerPosition(const vpImage< unsigned char > &I, vpImagePoint &ip)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
vpImage< unsigned char > I
Definition: vpPlot.h:116
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:664
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpDisplayGDI()
Basic constructor.
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void setUnitX(const unsigned int graphNum, const std::string &unitx)
Definition: vpPlot.cpp:509
error that can be emited by ViSP classes.
Definition: vpException.h:73
void saveData(const unsigned int graphNum, const std::string &dataFile, const std::string &title_prefix="")
Definition: vpPlot.cpp:634
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:286
void setUnitY(const unsigned int graphNum, const std::string &unity)
Definition: vpPlot.cpp:521
void setTitle(const unsigned int graphNum, const std::string &title)
Definition: vpPlot.cpp:497
vpPlot()
Definition: vpPlot.cpp:62
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
void setGridThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:596
void navigate(void)
Definition: vpPlot.cpp:430
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:266
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
void init(const unsigned int nbGraph, const unsigned int height=700, const unsigned int width=700, const int x=-1, const int y=-1, const std::string &title="")
Definition: vpPlot.cpp:103
~vpPlot()
Definition: vpPlot.cpp:134
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
void getPixelValue(const bool block)
Definition: vpPlot.cpp:475
void setLegend(const unsigned int graphNum, const unsigned int curveNum, const std::string &legend)
Definition: vpPlot.cpp:546
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:203
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
void resetPointList(const unsigned int graphNum)
Definition: vpPlot.cpp:557
void setGraphThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:583
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setThickness(const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
Definition: vpPlot.cpp:571
Class that consider the case of a translation vector.
void setUnitZ(const unsigned int graphNum, const std::string &unitz)
Definition: vpPlot.cpp:533