ViSP  2.10.0
vpPlot.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPlot.cpp 5226 2015-01-29 17:05:35Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Plot curves.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
42 
43 
44 #include <visp/vpConfig.h>
45 
46 #if defined(VISP_HAVE_DISPLAY)
47 #include <visp/vpPlot.h>
48 #include <visp/vpDisplayOpenCV.h>
49 #include <visp/vpDisplayX.h>
50 #include <visp/vpDisplayGDI.h>
51 #include <visp/vpDisplayGTK.h>
52 #include <visp/vpDisplayD3D.h>
53 #include <visp/vpMath.h>
54 #include <visp/vpMeterPixelConversion.h>
55 #include <visp/vpPixelMeterConversion.h>
56 #include <visp/vpPose.h>
57 #include <fstream>
58 #include <list>
59 #include <vector>
60 
67 vpPlot::vpPlot() : I(), display(NULL), graphNbr(1), graphList(NULL), margei(30), margej(40),
68  factori(1.f), factorj(1.)
69 {
70 }
87 vpPlot::vpPlot(const unsigned int graph_nbr,
88  const unsigned int height, const unsigned int width,
89  const int x, const int y, const char *title)
90  : I(), display(NULL), graphNbr(1), graphList(NULL), margei(30), margej(40),
91  factori(1.f), factorj(1.)
92 {
93  init(graph_nbr, height, width, x, y, title);
94 }
95 
108 void vpPlot::init(const unsigned int graph_nbr,
109  const unsigned int height, const unsigned int width,
110  const int x, const int y, const char *title)
111 {
112  I.init(height,width,255);
113 
114 #if defined VISP_HAVE_X11
115  display = new vpDisplayX;
116 #elif defined VISP_HAVE_GDI
117  display = new vpDisplayGDI;
118 #elif defined VISP_HAVE_OPENCV
119  display = new vpDisplayOpenCV;
120 #elif defined VISP_HAVE_GTK
121  display = new vpDisplayGTK;
122 #elif defined VISP_HAVE_D3D9
123  display = new vpDisplayD3D;
124 #endif
125 
126  display->init(I, x, y, title);
127 
129 
130  factori = height/700.0f;
131  factorj = width/700.0f;
132 
133  initNbGraph(graph_nbr);
134 }
135 
140 {
141  if (graphList != NULL)
142  {
143  delete[] graphList;
144  graphList = NULL;
145  }
146  if (display != NULL)
147  {
148  delete display;
149  display = NULL;
150  }
151 }
152 
161 void
162 vpPlot::initNbGraph (unsigned int nbGraph)
163 {
164  if(nbGraph > 4){
165  throw vpException(vpException::dimensionError, "Cannot create more than 4 graphs");
166  }
167  graphList = new vpPlotGraph[nbGraph];
168  graphNbr = nbGraph;
169 
170  switch (nbGraph)
171  {
172  case 1 :
173  graphList[0].initSize(vpImagePoint(0,0), (unsigned int)(700*factorj),(unsigned int)(700*factori),margei,margej);
174  break;
175  case 2 :
176  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
177  graphList[1].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
178  break;
179  case 3 :
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)(700*factorj),(unsigned int)(350*factori),margei,margej);
183  break;
184  case 4 :
185  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
186  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
187  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
188  graphList[3].initSize(vpImagePoint((unsigned int)(350*factori),(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
189  break;
190  }
191 
192  for (unsigned int i = 0; i < graphNbr; i++)
193  {
194  strcpy(graphList[i].title, "");
195  strcpy(graphList[i].unitx, "");
196  strcpy(graphList[i].unity, "");
197  strcpy(graphList[i].unitz, "");
198  }
199 }
200 
207 void
208 vpPlot::initGraph (unsigned int graphNum, unsigned int curveNbr)
209 {
210  (graphList+graphNum)->initGraph(curveNbr);
211 }
212 
213 
214 // void
215 // vpPlot::initRange (const int graphNum,
216 // double xmin, double xmax, double /*xdelt*/,
217 // double ymin, double ymax, double /*ydelt*/,
218 // const bool gx, const bool gy)
219 // {
220 // (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,gx,gy);
221 // }
222 
232 void
233 vpPlot::initRange (const unsigned int graphNum,
234  double xmin, double xmax,
235  double ymin, double ymax)
236 {
237  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,true,true);
238 }
239 
251 void
252 vpPlot::initRange (const unsigned int graphNum,
253  double xmin, double xmax, double ymin,
254  double ymax, double zmin, double zmax)
255 {
256  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,zmin,zmax,10,true,true);
257 }
258 
266 void
267 vpPlot::setColor (const unsigned int graphNum, const unsigned int curveNum, vpColor color)
268 {
269  (graphList+graphNum)->setCurveColor(curveNum, color);
270 }
271 
275 void
276 vpPlot::displayGrid()
277 {
278  for (unsigned int i = 0; i < graphNbr; i++)
279  graphList[i].displayGrid(I);
280 }
281 
290 void
291 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
292 {
293  (graphList+graphNum)->plot(I,curveNum,x,y);
294 }
295 
303 void vpPlot::plot(const unsigned int graphNum,
304  const double x, const vpColVector &v_y)
305 {
306  if((graphList+graphNum)->curveNbr == v_y.getRows())
307  {
308  for(unsigned int i = 0;i < v_y.getRows();++i)
309  this->plot(graphNum, i, x, v_y[i]);
310  }
311  else
312  vpTRACE("error in plot vector : not the right dimension");
313 }
314 
315 
326 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y, const double z)
327 {
328  return (graphList+graphNum)->plot(I,curveNum,x,y,z);
329 }
330 
340 vpPlot::plot(const unsigned int graphNum, const double x, const vpColVector &v_y, const vpColVector &v_z)
341 {
343  if((graphList+graphNum)->curveNbr == v_y.getRows() && (graphList+graphNum)->curveNbr == v_z.getRows())
344  {
345  for(unsigned int i = 0;i < v_y.getRows();++i)
346  button = this->plot(graphNum, i, x, v_y[i], v_z[i]);
347  }
348  else
349  vpTRACE("error in plot vector : not the right dimension");
350  return button;
351 }
352 
360 void
362 {
364 
365  bool blocked = false;
366  unsigned int iblocked = 0;
367  vpImagePoint iP;
368 
369  while (b != vpMouseButton::button3)
370  {
371  if (!blocked)
372  {
374  for (unsigned int i = 0; i < graphNbr ; i++)
375  {
376  if (iP.inRectangle((graphList+i)->graphZone))
377  {
378  iblocked = i;
379  break;
380  }
381  }
382  if ((graphList+iblocked)->move(I, b))
383  {
384  (graphList+iblocked)->replot3D(I);
385  }
386  blocked = (graphList+iblocked)->blocked;
387  }
388  else
389  {
390  if ((graphList+iblocked)->move(I, b))
391  {
392  (graphList+iblocked)->replot3D(I);
393  }
394  blocked = (graphList+iblocked)->blocked;
395  }
396  vpTime::sleepMs(20);
397  }
398 }
399 
405 void
406 vpPlot::getPixelValue(const bool block)
407 {
408  vpImagePoint iP;
409 
410  if (block)
412  else
414 
415  for (unsigned int i = 0; i < graphNbr; i++)
416  {
417  if ((graphList+i)->getPixelValue(I,iP)) break;
418  }
419 }
420 
427 void
428 vpPlot::setTitle (const unsigned int graphNum, const char *title)
429 {
430  (graphList+graphNum)->setTitle(title);
431 }
432 
439 void
440 vpPlot::setUnitX (const unsigned int graphNum, const char *unitx)
441 {
442  (graphList+graphNum)->setUnitX(unitx);
443 }
444 
451 void
452 vpPlot::setUnitY (const unsigned int graphNum, const char *unity)
453 {
454  (graphList+graphNum)->setUnitY(unity);
455 }
456 
463 void
464 vpPlot::setUnitZ (const unsigned int graphNum, const char *unitz)
465 {
466  (graphList+graphNum)->setUnitZ(unitz);
467 }
468 
476 void
477 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const char *legend)
478 {
479  (graphList+graphNum)->setLegend(curveNum, legend);
480 }
481 
487 void
488 vpPlot::resetPointList (const unsigned int graphNum)
489 {
490  for (unsigned int i = 0; i < (graphList+graphNum)->curveNbr; i++)
491  (graphList+graphNum)->resetPointList(i);
492 }
493 
501 void
502 vpPlot::setThickness (const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
503 {
504  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
505 }
506 
513 void
514 vpPlot::setGraphThickness (const unsigned int graphNum, const unsigned int thickness)
515 {
516  for (unsigned int curveNum=0; curveNum < (graphList+graphNum)->curveNbr; curveNum++)
517  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
518 }
519 
526 void
527 vpPlot::setGridThickness (const unsigned int graphNum, const unsigned int thickness)
528 {
529  (graphList+graphNum)->setGridThickness(thickness);
530 }
531 
538 void
539 vpPlot::resetPointList (const unsigned int graphNum, const unsigned int curveNum)
540 {
541  (graphList+graphNum)->resetPointList(curveNum);
542 }
543 
560 void vpPlot::saveData(const unsigned int graphNum, const char* dataFile)
561 {
562  std::ofstream fichier;
563  fichier.open(dataFile);
564 
565  unsigned int ind;
566  double *p = new double[3];
567  bool end=false;
568 
569  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
570  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
571  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
572 
573  fichier << (graphList+graphNum)->title << std::endl;
574 
575  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
576  {
577  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
578  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
579  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
580 // (graphList+graphNum)->curveList[ind].pointListx.front();
581 // (graphList+graphNum)->curveList[ind].pointListy.front();
582 // (graphList+graphNum)->curveList[ind].pointListz.front();
583  }
584 
585  while (end == false)
586  {
587  end = true;
588  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
589  {
590 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
591 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
592 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
593  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
594  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
595  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
596  {
597  p[0] = *vec_iter_pointListx[ind];
598  p[1] = *vec_iter_pointListy[ind];
599  p[2] = *vec_iter_pointListz[ind];
600 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
601 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
602 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
603 
604  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
605  ++vec_iter_pointListx[ind];
606  ++vec_iter_pointListy[ind];
607  ++vec_iter_pointListz[ind];
608 // (graphList+graphNum)->curveList[ind].pointListx.next();
609 // (graphList+graphNum)->curveList[ind].pointListy.next();
610 // (graphList+graphNum)->curveList[ind].pointListz.next();
611 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
612 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
613 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
614  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
615  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
616  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
617  end = false;
618  }
619  else
620  {
621 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
622 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
623 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
624  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
625  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
626  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
627  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
628  }
629  }
630  fichier << std::endl;
631  }
632 
633  delete[] p;
634  fichier.close();
635 }
636 
637 #endif
void initRange(const unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition: vpPlot.cpp:233
bool inRectangle(const vpRect &rect) const
virtual void init(vpImage< unsigned char > &I, int x=-1, int y=-1, const char *title=NULL)=0
void setColor(const unsigned int graphNum, const unsigned int curveNum, vpColor color)
Definition: vpPlot.cpp:267
vpImage< unsigned char > I
Definition: vpPlot.h:120
void init(unsigned int height, unsigned int width)
set the size of the image
Definition: vpImage.h:386
#define vpTRACE
Definition: vpDebug.h:418
void setUnitY(const unsigned int graphNum, const char *unity)
Definition: vpPlot.cpp:452
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
static void sleepMs(double t)
Definition: vpTime.cpp:236
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
void setLegend(const unsigned int graphNum, const unsigned int curveNum, const char *legend)
Definition: vpPlot.cpp:477
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:291
void setTitle(const unsigned int graphNum, const char *title)
Definition: vpPlot.cpp:428
void setUnitZ(const unsigned int graphNum, const char *unitz)
Definition: vpPlot.cpp:464
void setUnitX(const unsigned int graphNum, const char *unitx)
Definition: vpPlot.cpp:440
vpPlot()
Definition: vpPlot.cpp:67
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void setGridThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:527
void navigate(void)
Definition: vpPlot.cpp:361
virtual bool getPointerPosition(vpImagePoint &ip)=0
void saveData(const unsigned int graphNum, const char *dataFile)
Definition: vpPlot.cpp:560
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
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:108
~vpPlot()
Definition: vpPlot.cpp:139
void getPixelValue(const bool block)
Definition: vpPlot.cpp:406
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:208
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void resetPointList(const unsigned int graphNum)
Definition: vpPlot.cpp:488
void setGraphThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:514
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:93
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
void setThickness(const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
Definition: vpPlot.cpp:502