ViSP  2.8.0
vpPlot.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPlot.cpp 4151 2013-03-11 06:52:18Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
68 {
69  graphList = NULL;
70  display = NULL;
71 
72  margei = 30;
73  margej = 40;
74 }
91 vpPlot::vpPlot(const unsigned int graphNbr,
92  const unsigned int height, const unsigned int width,
93  const int x, const int y, const char *title)
94 {
95  graphList = NULL;
96  display = NULL;
97 
98  margei = 30;
99  margej = 40;
100 
101  init(graphNbr, height, width, x, y, title);
102 }
103 
116 void vpPlot::init(const unsigned int graphNbr,
117  const unsigned int height, const unsigned int width,
118  const int x, const int y, const char *title)
119 {
120  I.init(height,width,255);
121 
122 #if defined VISP_HAVE_X11
123  display = new vpDisplayX;
124 #elif defined VISP_HAVE_GDI
125  display = new vpDisplayGDI;
126 #elif defined VISP_HAVE_OPENCV
127  display = new vpDisplayOpenCV;
128 #elif defined VISP_HAVE_GTK
129  display = new vpDisplayGTK;
130 #elif defined VISP_HAVE_D3D9
131  display = new vpDisplayD3D;
132 #endif
133 
134  display->init(I, x, y, title);
135 
137 
138  factori = height/700.0f;
139  factorj = width/700.0f;
140 
141  initNbGraph(graphNbr);
142 }
143 
148 {
149  if (graphList != NULL)
150  {
151  delete[] graphList;
152  graphList = NULL;
153  }
154  if (display != NULL)
155  {
156  delete display;
157  display = NULL;
158  }
159 }
160 
169 void
170 vpPlot::initNbGraph (unsigned int nbGraph)
171 {
172  if(nbGraph > 4){
173  throw vpException(vpException::dimensionError, "Cannot create more than 4 graphs");
174  }
175  graphList = new vpPlotGraph[nbGraph];
176  graphNbr = nbGraph;
177 
178  switch (nbGraph)
179  {
180  case 1 :
181  graphList[0].initSize(vpImagePoint(0,0), (unsigned int)(700*factorj),(unsigned int)(700*factori),margei,margej);
182  break;
183  case 2 :
184  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
185  graphList[1].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
186  break;
187  case 3 :
188  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
189  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
190  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(700*factorj),(unsigned int)(350*factori),margei,margej);
191  break;
192  case 4 :
193  graphList[0].initSize(vpImagePoint(0,0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
194  graphList[1].initSize(vpImagePoint(0,(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
195  graphList[2].initSize(vpImagePoint((unsigned int)(350*factori),0),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
196  graphList[3].initSize(vpImagePoint((unsigned int)(350*factori),(unsigned int)(350*factorj)),(unsigned int)(350*factorj),(unsigned int)(350*factori),margei,margej);
197  break;
198  }
199 
200  for (unsigned int i = 0; i < graphNbr; i++)
201  {
202  strcpy(graphList[i].title, "");
203  strcpy(graphList[i].unitx, "");
204  strcpy(graphList[i].unity, "");
205  strcpy(graphList[i].unitz, "");
206  graphList[i].textdispayed=false;
207  }
208 }
209 
216 void
217 vpPlot::initGraph (unsigned int graphNum, unsigned int curveNbr)
218 {
219  (graphList+graphNum)->initGraph(curveNbr);
220 }
221 
222 
223 // void
224 // vpPlot::initRange (const int graphNum,
225 // double xmin, double xmax, double /*xdelt*/,
226 // double ymin, double ymax, double /*ydelt*/,
227 // const bool gx, const bool gy)
228 // {
229 // (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,gx,gy);
230 // }
231 
241 void
242 vpPlot::initRange (const unsigned int graphNum,
243  double xmin, double xmax,
244  double ymin, double ymax)
245 {
246  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,true,true);
247 }
248 
260 void
261 vpPlot::initRange (const unsigned int graphNum,
262  double xmin, double xmax, double ymin,
263  double ymax, double zmin, double zmax)
264 {
265  (graphList+graphNum)->initScale(I,xmin,xmax,10,ymin,ymax,10,zmin,zmax,10,true,true);
266 }
267 
275 void
276 vpPlot::setColor (const unsigned int graphNum, const unsigned int curveNum, vpColor color)
277 {
278  (graphList+graphNum)->setCurveColor(curveNum, color);
279 }
280 
284 void
285 vpPlot::displayGrid()
286 {
287  for (unsigned int i = 0; i < graphNbr; i++)
288  graphList[i].displayGrid(I);
289 }
290 
299 void
300 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
301 {
302  (graphList+graphNum)->plot(I,curveNum,x,y);
303 }
304 
312 void vpPlot::plot(const unsigned int graphNum,
313  const double x, const vpColVector &v_y)
314 {
315  if((graphList+graphNum)->curveNbr == v_y.getRows())
316  {
317  for(unsigned int i = 0;i < v_y.getRows();++i)
318  this->plot(graphNum, i, x, v_y[i]);
319  }
320  else
321  vpTRACE("error in plot vector : not the right dimension");
322 }
323 
324 
334 void
335 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y, const double z)
336 {
337  (graphList+graphNum)->plot(I,curveNum,x,y,z);
338 }
339 
348 void vpPlot::plot(const unsigned int graphNum, const double x, const vpColVector &v_y, const vpColVector &v_z)
349 {
350  if((graphList+graphNum)->curveNbr == v_y.getRows() && (graphList+graphNum)->curveNbr == v_z.getRows())
351  {
352  for(unsigned int i = 0;i < v_y.getRows();++i)
353  this->plot(graphNum, i, x, v_y[i], v_z[i]);
354  }
355  else
356  vpTRACE("error in plot vector : not the right dimension");
357 }
358 
362 void
364 {
365  vpImagePoint trash;
367 
368  bool blocked = false;
369  unsigned int iblocked = 0;
370  vpImagePoint iP;
371 
372  while (b != vpMouseButton::button3)
373  {
374  if (!blocked)
375  {
377  for (unsigned int i = 0; i < graphNbr ; i++)
378  {
379  if (iP.inRectangle((graphList+i)->graphZone))
380  {
381  iblocked = i;
382  break;
383  }
384  }
385  if ((graphList+iblocked)->move(I))
386  {
387  (graphList+iblocked)->replot3D(I);
388  }
389 
390  blocked = (graphList+iblocked)->blocked;
391  }
392  else
393  {
394  if ((graphList+iblocked)->move(I))
395  {
396  (graphList+iblocked)->replot3D(I);
397  }
398  blocked = (graphList+iblocked)->blocked;
399  }
400  }
401 }
402 
408 void
409 vpPlot::getPixelValue(const bool block)
410 {
411  vpImagePoint iP;
412 
413  if (block)
415  else
417 
418  for (unsigned int i = 0; i < graphNbr; i++)
419  {
420  if ((graphList+i)->getPixelValue(I,iP)) break;
421  }
422 }
423 
430 void
431 vpPlot::setTitle (const unsigned int graphNum, const char *title)
432 {
433  (graphList+graphNum)->setTitle(title);
434 }
435 
442 void
443 vpPlot::setUnitX (const unsigned int graphNum, const char *unitx)
444 {
445  (graphList+graphNum)->setUnitX(unitx);
446 }
447 
454 void
455 vpPlot::setUnitY (const unsigned int graphNum, const char *unity)
456 {
457  (graphList+graphNum)->setUnitY(unity);
458 }
459 
466 void
467 vpPlot::setUnitZ (const unsigned int graphNum, const char *unitz)
468 {
469  (graphList+graphNum)->setUnitZ(unitz);
470 }
471 
479 void
480 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const char *legend)
481 {
482  (graphList+graphNum)->setLegend(curveNum, legend);
483 }
484 
490 void
491 vpPlot::resetPointList (const unsigned int graphNum)
492 {
493  for (unsigned int i = 0; i < (graphList+graphNum)->curveNbr; i++)
494  (graphList+graphNum)->resetPointList(i);
495 }
496 
504 void
505 vpPlot::setThickness (const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
506 {
507  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
508 }
509 
516 void
517 vpPlot::setGraphThickness (const unsigned int graphNum, const unsigned int thickness)
518 {
519  for (unsigned int curveNum=0; curveNum < (graphList+graphNum)->curveNbr; curveNum++)
520  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
521 }
522 
529 void
530 vpPlot::setGridThickness (const unsigned int graphNum, const unsigned int thickness)
531 {
532  (graphList+graphNum)->setGridThickness(thickness);
533 }
534 
541 void
542 vpPlot::resetPointList (const unsigned int graphNum, const unsigned int curveNum)
543 {
544  (graphList+graphNum)->resetPointList(curveNum);
545 }
546 
563 void vpPlot::saveData(const unsigned int graphNum, const char* dataFile)
564 {
565  std::ofstream fichier;
566  fichier.open(dataFile);
567 
568  unsigned int ind;
569  double *p = new double[3];
570  bool end=false;
571 
572  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
573  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
574  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
575 
576  fichier << (graphList+graphNum)->title << std::endl;
577 
578  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
579  {
580  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
581  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
582  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
583 // (graphList+graphNum)->curveList[ind].pointListx.front();
584 // (graphList+graphNum)->curveList[ind].pointListy.front();
585 // (graphList+graphNum)->curveList[ind].pointListz.front();
586  }
587 
588  while (end == false)
589  {
590  end = true;
591  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
592  {
593 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
594 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
595 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
596  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
597  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
598  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
599  {
600  p[0] = *vec_iter_pointListx[ind];
601  p[1] = *vec_iter_pointListy[ind];
602  p[2] = *vec_iter_pointListz[ind];
603 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
604 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
605 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
606 
607  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
608  ++vec_iter_pointListx[ind];
609  ++vec_iter_pointListy[ind];
610  ++vec_iter_pointListz[ind];
611 // (graphList+graphNum)->curveList[ind].pointListx.next();
612 // (graphList+graphNum)->curveList[ind].pointListy.next();
613 // (graphList+graphNum)->curveList[ind].pointListz.next();
614 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
615 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
616 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
617  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
618  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
619  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
620  end = false;
621  }
622  else
623  {
624 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
625 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
626 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
627  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
628  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
629  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
630  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
631  }
632  }
633  fichier << std::endl;
634  }
635 
636  delete[] p;
637  fichier.close();
638 }
639 
640 #endif
void initRange(const unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition: vpPlot.cpp:242
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:276
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:377
#define vpTRACE
Definition: vpDebug.h:401
void setUnitY(const unsigned int graphNum, const char *unity)
Definition: vpPlot.cpp:455
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:75
void setLegend(const unsigned int graphNum, const unsigned int curveNum, const char *legend)
Definition: vpPlot.cpp:480
void plot(const unsigned int graphNum, const unsigned int curveNum, const double x, const double y)
Definition: vpPlot.cpp:300
void setTitle(const unsigned int graphNum, const char *title)
Definition: vpPlot.cpp:431
void setUnitZ(const unsigned int graphNum, const char *unitz)
Definition: vpPlot.cpp:467
void setUnitX(const unsigned int graphNum, const char *unitx)
Definition: vpPlot.cpp:443
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:530
void navigate(void)
Definition: vpPlot.cpp:363
virtual bool getPointerPosition(vpImagePoint &ip)=0
void saveData(const unsigned int graphNum, const char *dataFile)
Definition: vpPlot.cpp:563
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
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:116
~vpPlot()
Definition: vpPlot.cpp:147
void getPixelValue(const bool block)
Definition: vpPlot.cpp:409
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:217
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:491
void setGraphThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:517
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:505