ViSP  2.6.2
vpPlot.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPlot.cpp 3821 2012-06-27 13:50:37Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
47 #include <visp/vpPlot.h>
48 #include <visp/vpDisplayOpenCV.h>
49 #include <visp/vpDisplayX.h>
50 #include <visp/vpDisplayGDI.h>
51 #include <visp/vpMath.h>
52 #include <visp/vpMeterPixelConversion.h>
53 #include <visp/vpPixelMeterConversion.h>
54 #include <visp/vpPose.h>
55 #include <fstream>
56 #include <list>
57 #include <vector>
58 
66 {
67  graphList = NULL;
68  display = NULL;
69 
70  margei = 30;
71  margej = 40;
72 }
89 vpPlot::vpPlot(const unsigned int graphNbr,
90  const unsigned int height, const unsigned int width,
91  const int x, const int y, const char *title)
92 {
93  graphList = NULL;
94  display = NULL;
95 
96  margei = 30;
97  margej = 40;
98 
99  init(graphNbr, height, width, x, y, title);
100 }
101 
114 void vpPlot::init(const unsigned int graphNbr,
115  const unsigned int height, const unsigned int width,
116  const int x, const int y, const char *title)
117 {
118  I.init(height,width,255);
119 
120 #if defined VISP_HAVE_X11
121  display = new vpDisplayX;
122 #elif defined VISP_HAVE_GDI
123  display = new vpDisplayGDI;
124 #elif defined VISP_HAVE_OPENCV
125  display = new vpDisplayOpenCV;
126 #endif
127 
128  display->init(I, x, y, title);
129 
131 
132  factori = height/700.0f;
133  factorj = width/700.0f;
134 
135  initNbGraph(graphNbr);
136 }
137 
142 {
143  if (graphList != NULL)
144  {
145  delete[] graphList;
146  graphList = NULL;
147  }
148  if (display != NULL)
149  {
150  delete display;
151  display = NULL;
152  }
153 }
154 
163 void
164 vpPlot::initNbGraph (unsigned int nbGraph)
165 {
166  if(nbGraph > 4){
167  throw vpException(vpException::dimensionError, "Cannot create more than 4 graphs");
168  }
169  graphList = new vpPlotGraph[nbGraph];
170  graphNbr = nbGraph;
171 
172  switch (nbGraph)
173  {
174  case 1 :
175  graphList[0].initSize(vpImagePoint(0,0), (unsigned int)(700*factorj),(unsigned int)(700*factori),margei,margej);
176  break;
177  case 2 :
178  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
179  graphList[1].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
180  break;
181  case 3 :
182  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
183  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
184  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
185  break;
186  case 4 :
187  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
188  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
189  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
190  graphList[3].initSize(vpImagePoint((unsigned int)(350*factori),(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
191  break;
192  }
193 
194  for (unsigned int i = 0; i < graphNbr; i++)
195  {
196  strcpy(graphList[i].title, "");
197  strcpy(graphList[i].unitx, "");
198  strcpy(graphList[i].unity, "");
199  strcpy(graphList[i].unitz, "");
200  graphList[i].textdispayed=false;
201  }
202 }
203 
210 void
211 vpPlot::initGraph (unsigned int graphNum, unsigned int curveNbr)
212 {
213  (graphList+graphNum)->initGraph(curveNbr);
214 }
215 
216 
217 // void
218 // vpPlot::initRange (const int graphNum,
219 // double xmin, double xmax, double /*xdelt*/,
220 // double ymin, double ymax, double /*ydelt*/,
221 // const bool gx, const bool gy)
222 // {
223 // (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,gx,gy);
224 // }
225 
235 void
236 vpPlot::initRange (const unsigned int graphNum,
237  double xmin, double xmax,
238  double ymin, double ymax)
239 {
240  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,true,true);
241 }
242 
254 void
255 vpPlot::initRange (const unsigned int graphNum,
256  double xmin, double xmax, double ymin,
257  double ymax, double zmin, double zmax)
258 {
259  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,zmin,zmax,10,true,true);
260 }
261 
269 void
270 vpPlot::setColor (const unsigned int graphNum, const unsigned int curveNum, vpColor color)
271 {
272  (graphList+graphNum)->setCurveColor(curveNum, color);
273 }
274 
278 void
279 vpPlot::displayGrid()
280 {
281  for (unsigned int i = 0; i < graphNbr; i++)
282  graphList[i].displayGrid(I);
283 }
284 
293 void
294 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
295 {
296  (graphList+graphNum)->plot(I,curveNum,x,y);
297 }
298 
306 void vpPlot::plot(const unsigned int graphNum,
307  const double x, const vpColVector &v_y)
308 {
309  if((graphList+graphNum)->curveNbr == v_y.getRows())
310  {
311  for(unsigned int i = 0;i < v_y.getRows();++i)
312  this->plot(graphNum, i, x, v_y[i]);
313  }
314  else
315  vpTRACE("error in plot vector : not the right dimension");
316 }
317 
327 void
328 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y, const double z)
329 {
330  (graphList+graphNum)->plot(I,curveNum,x,y,z);
331 }
332 
341 void vpPlot::plot(const unsigned int graphNum, const double x, const vpColVector &v_y, const vpColVector &v_z)
342 {
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  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 }
351 
355 void
357 {
358  vpImagePoint trash;
360 
361  bool blocked = false;
362  unsigned int iblocked = 0;
363  vpImagePoint iP;
364 
365  while (b != vpMouseButton::button3)
366  {
367  if (!blocked)
368  {
370  for (unsigned int i = 0; i < graphNbr ; i++)
371  {
372  if (iP.inRectangle((graphList+i)->graphZone))
373  {
374  iblocked = i;
375  break;
376  }
377  }
378  if ((graphList+iblocked)->move(I))
379  {
380  (graphList+iblocked)->replot3D(I);
381  }
382 
383  blocked = (graphList+iblocked)->blocked;
384  }
385  else
386  {
387  if ((graphList+iblocked)->move(I))
388  {
389  (graphList+iblocked)->replot3D(I);
390  }
391  blocked = (graphList+iblocked)->blocked;
392  }
393  }
394 }
395 
401 void
402 vpPlot::getPixelValue(const bool block)
403 {
404  vpImagePoint iP;
405 
406  if (block)
408  else
410 
411  for (unsigned int i = 0; i < graphNbr; i++)
412  {
413  if ((graphList+i)->getPixelValue(I,iP)) break;
414  }
415 }
416 
423 void
424 vpPlot::setTitle (const unsigned int graphNum, const char *title)
425 {
426  (graphList+graphNum)->setTitle(title);
427 }
428 
435 void
436 vpPlot::setUnitX (const unsigned int graphNum, const char *unitx)
437 {
438  (graphList+graphNum)->setUnitX(unitx);
439 }
440 
447 void
448 vpPlot::setUnitY (const unsigned int graphNum, const char *unity)
449 {
450  (graphList+graphNum)->setUnitY(unity);
451 }
452 
459 void
460 vpPlot::setUnitZ (const unsigned int graphNum, const char *unitz)
461 {
462  (graphList+graphNum)->setUnitZ(unitz);
463 }
464 
472 void
473 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const char *legend)
474 {
475  (graphList+graphNum)->setLegend(curveNum, legend);
476 }
477 
483 void
484 vpPlot::resetPointList (const unsigned int graphNum)
485 {
486  for (unsigned int i = 0; i < (graphList+graphNum)->curveNbr; i++)
487  (graphList+graphNum)->resetPointList(i);
488 }
489 
497 void
498 vpPlot::setThickness (const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
499 {
500  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
501 }
502 
509 void
510 vpPlot::resetPointList (const unsigned int graphNum, const unsigned int curveNum)
511 {
512  (graphList+graphNum)->resetPointList(curveNum);
513 }
514 
531 void vpPlot::saveData(const unsigned int graphNum, const char* dataFile)
532 {
533  std::ofstream fichier;
534  fichier.open(dataFile);
535 
536  unsigned int ind;
537  double *p = new double[3];
538  bool end=false;
539 
540  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
541  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
542  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
543 
544  fichier << (graphList+graphNum)->title << std::endl;
545 
546  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
547  {
548  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
549  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
550  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
551 // (graphList+graphNum)->curveList[ind].pointListx.front();
552 // (graphList+graphNum)->curveList[ind].pointListy.front();
553 // (graphList+graphNum)->curveList[ind].pointListz.front();
554  }
555 
556  while (end == false)
557  {
558  end = true;
559  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
560  {
561 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
562 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
563 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
564  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
565  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
566  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
567  {
568  p[0] = *vec_iter_pointListx[ind];
569  p[1] = *vec_iter_pointListy[ind];
570  p[2] = *vec_iter_pointListz[ind];
571 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
572 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
573 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
574 
575  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
576  ++vec_iter_pointListx[ind];
577  ++vec_iter_pointListy[ind];
578  ++vec_iter_pointListz[ind];
579 // (graphList+graphNum)->curveList[ind].pointListx.next();
580 // (graphList+graphNum)->curveList[ind].pointListy.next();
581 // (graphList+graphNum)->curveList[ind].pointListz.next();
582 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
583 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
584 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
585  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
586  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
587  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
588  end = false;
589  }
590  else
591  {
592 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
593 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
594 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
595  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
596  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
597  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
598  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
599  }
600  }
601  fichier << std::endl;
602  }
603 
604  delete[] p;
605  fichier.close();
606 }
607 
608 #endif
void initRange(const unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition: vpPlot.cpp:236
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:270
vpImage< unsigned char > I
Definition: vpPlot.h:122
void init(unsigned int height, unsigned int width)
set the size of the image
Definition: vpImage.h:372
#define vpTRACE
Definition: vpDebug.h:401
void setUnitY(const unsigned int graphNum, const char *unity)
Definition: vpPlot.cpp:448
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:123
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setLegend(const unsigned int graphNum, const unsigned int curveNum, const char *legend)
Definition: vpPlot.cpp:473
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:294
void setTitle(const unsigned int graphNum, const char *title)
Definition: vpPlot.cpp:424
void setUnitZ(const unsigned int graphNum, const char *unitz)
Definition: vpPlot.cpp:460
void setUnitX(const unsigned int graphNum, const char *unitx)
Definition: vpPlot.cpp:436
vpPlot()
Definition: vpPlot.cpp:65
void navigate(void)
Definition: vpPlot.cpp:356
virtual bool getPointerPosition(vpImagePoint &ip)=0
void saveData(const unsigned int graphNum, const char *dataFile)
Definition: vpPlot.cpp:531
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
The vpDisplayOpenCV allows to display image using the opencv library.
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:114
~vpPlot()
Definition: vpPlot.cpp:141
void getPixelValue(const bool block)
Definition: vpPlot.cpp:402
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:211
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:484
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:92
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
void setThickness(const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
Definition: vpPlot.cpp:498