Visual Servoing Platform  version 3.0.0
vpPlot.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 char *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 char *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);
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  strcpy(graphList[i].title, "");
190  strcpy(graphList[i].unitx, "");
191  strcpy(graphList[i].unity, "");
192  strcpy(graphList[i].unitz, "");
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 char *title)
498 {
499  (graphList+graphNum)->setTitle(title);
500 }
501 
508 void
509 vpPlot::setUnitX (const unsigned int graphNum, const char *unitx)
510 {
511  (graphList+graphNum)->setUnitX(unitx);
512 }
513 
520 void
521 vpPlot::setUnitY (const unsigned int graphNum, const char *unity)
522 {
523  (graphList+graphNum)->setUnitY(unity);
524 }
525 
532 void
533 vpPlot::setUnitZ (const unsigned int graphNum, const char *unitz)
534 {
535  (graphList+graphNum)->setUnitZ(unitz);
536 }
537 
545 void
546 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const char *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 
629 void vpPlot::saveData(const unsigned int graphNum, const char* dataFile)
630 {
631  std::ofstream fichier;
632  fichier.open(dataFile);
633 
634  unsigned int ind;
635  double *p = new double[3];
636  bool end=false;
637 
638  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
639  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
640  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
641 
642  fichier << (graphList+graphNum)->title << std::endl;
643 
644  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
645  {
646  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
647  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
648  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
649 // (graphList+graphNum)->curveList[ind].pointListx.front();
650 // (graphList+graphNum)->curveList[ind].pointListy.front();
651 // (graphList+graphNum)->curveList[ind].pointListz.front();
652  }
653 
654  while (end == false)
655  {
656  end = true;
657  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
658  {
659 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
660 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
661 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
662  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
663  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
664  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
665  {
666  p[0] = *vec_iter_pointListx[ind];
667  p[1] = *vec_iter_pointListy[ind];
668  p[2] = *vec_iter_pointListz[ind];
669 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
670 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
671 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
672 
673  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
674  ++vec_iter_pointListx[ind];
675  ++vec_iter_pointListy[ind];
676  ++vec_iter_pointListz[ind];
677 // (graphList+graphNum)->curveList[ind].pointListx.next();
678 // (graphList+graphNum)->curveList[ind].pointListy.next();
679 // (graphList+graphNum)->curveList[ind].pointListz.next();
680 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
681 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
682 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
683  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
684  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
685  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
686  end = false;
687  }
688  else
689  {
690 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
691 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
692 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
693  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
694  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
695  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
696  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
697  }
698  }
699  fichier << std::endl;
700  }
701 
702  delete[] p;
703  fichier.close();
704 }
705 
706 #elif !defined(VISP_BUILD_SHARED_LIBS)
707 // Work arround to avoid warning: libvisp_core.a(vpPlot.cpp.o) has no symbols
708 void dummy_vpPlot() {};
709 #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
vpImage< unsigned char > I
Definition: vpPlot.h:116
unsigned int width
Definition: vpDisplay.h:179
void init(unsigned int height, unsigned int width)
set the size of the image
Definition: vpImage.h:377
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
void setUnitY(const unsigned int graphNum, const char *unity)
Definition: vpPlot.cpp:521
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpDisplayGDI()
Basic constructor.
Define the X11 console to display images.
Definition: vpDisplayX.h:148
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
void setLegend(const unsigned int graphNum, const unsigned int curveNum, const char *legend)
Definition: vpPlot.cpp:546
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:286
void setTitle(const unsigned int graphNum, const char *title)
Definition: vpPlot.cpp:497
void setUnitZ(const unsigned int graphNum, const char *unitz)
Definition: vpPlot.cpp:533
void setUnitX(const unsigned int graphNum, const char *unitx)
Definition: vpPlot.cpp:509
vpPlot()
Definition: vpPlot.cpp:62
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:105
void setGridThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:596
void navigate(void)
Definition: vpPlot.cpp:430
virtual bool getPointerPosition(vpImagePoint &ip)=0
#define vpTRACE
Definition: vpDebug.h:414
void saveData(const unsigned int graphNum, const char *dataFile)
Definition: vpPlot.cpp:629
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
unsigned int height
Definition: vpDisplay.h:180
The vpDisplayOpenCV allows to display image using the opencv library.
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:236
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
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 char *title=NULL)
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 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
virtual bool getClick(bool blocking=true)=0
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.