ViSP  2.9.0
vpPlot.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPlot.cpp 4649 2014-02-07 14:57:11Z 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 
325 void
326 vpPlot::plot (const unsigned int graphNum, const unsigned int curveNum, const double x, const double y, const double z)
327 {
328  (graphList+graphNum)->plot(I,curveNum,x,y,z);
329 }
330 
339 void vpPlot::plot(const unsigned int graphNum, const double x, const vpColVector &v_y, const vpColVector &v_z)
340 {
341  if((graphList+graphNum)->curveNbr == v_y.getRows() && (graphList+graphNum)->curveNbr == v_z.getRows())
342  {
343  for(unsigned int i = 0;i < v_y.getRows();++i)
344  this->plot(graphNum, i, x, v_y[i], v_z[i]);
345  }
346  else
347  vpTRACE("error in plot vector : not the right dimension");
348 }
349 
353 void
355 {
357 
358  bool blocked = false;
359  unsigned int iblocked = 0;
360  vpImagePoint iP;
361 
362  while (b != vpMouseButton::button3)
363  {
364  if (!blocked)
365  {
367  for (unsigned int i = 0; i < graphNbr ; i++)
368  {
369  if (iP.inRectangle((graphList+i)->graphZone))
370  {
371  iblocked = i;
372  break;
373  }
374  }
375  if ((graphList+iblocked)->move(I))
376  {
377  (graphList+iblocked)->replot3D(I);
378  }
379 
380  blocked = (graphList+iblocked)->blocked;
381  }
382  else
383  {
384  if ((graphList+iblocked)->move(I))
385  {
386  (graphList+iblocked)->replot3D(I);
387  }
388  blocked = (graphList+iblocked)->blocked;
389  }
390  }
391 }
392 
398 void
399 vpPlot::getPixelValue(const bool block)
400 {
401  vpImagePoint iP;
402 
403  if (block)
405  else
407 
408  for (unsigned int i = 0; i < graphNbr; i++)
409  {
410  if ((graphList+i)->getPixelValue(I,iP)) break;
411  }
412 }
413 
420 void
421 vpPlot::setTitle (const unsigned int graphNum, const char *title)
422 {
423  (graphList+graphNum)->setTitle(title);
424 }
425 
432 void
433 vpPlot::setUnitX (const unsigned int graphNum, const char *unitx)
434 {
435  (graphList+graphNum)->setUnitX(unitx);
436 }
437 
444 void
445 vpPlot::setUnitY (const unsigned int graphNum, const char *unity)
446 {
447  (graphList+graphNum)->setUnitY(unity);
448 }
449 
456 void
457 vpPlot::setUnitZ (const unsigned int graphNum, const char *unitz)
458 {
459  (graphList+graphNum)->setUnitZ(unitz);
460 }
461 
469 void
470 vpPlot::setLegend (const unsigned int graphNum, const unsigned int curveNum, const char *legend)
471 {
472  (graphList+graphNum)->setLegend(curveNum, legend);
473 }
474 
480 void
481 vpPlot::resetPointList (const unsigned int graphNum)
482 {
483  for (unsigned int i = 0; i < (graphList+graphNum)->curveNbr; i++)
484  (graphList+graphNum)->resetPointList(i);
485 }
486 
494 void
495 vpPlot::setThickness (const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
496 {
497  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
498 }
499 
506 void
507 vpPlot::setGraphThickness (const unsigned int graphNum, const unsigned int thickness)
508 {
509  for (unsigned int curveNum=0; curveNum < (graphList+graphNum)->curveNbr; curveNum++)
510  (graphList+graphNum)->setCurveThickness(curveNum, thickness);
511 }
512 
519 void
520 vpPlot::setGridThickness (const unsigned int graphNum, const unsigned int thickness)
521 {
522  (graphList+graphNum)->setGridThickness(thickness);
523 }
524 
531 void
532 vpPlot::resetPointList (const unsigned int graphNum, const unsigned int curveNum)
533 {
534  (graphList+graphNum)->resetPointList(curveNum);
535 }
536 
553 void vpPlot::saveData(const unsigned int graphNum, const char* dataFile)
554 {
555  std::ofstream fichier;
556  fichier.open(dataFile);
557 
558  unsigned int ind;
559  double *p = new double[3];
560  bool end=false;
561 
562  std::vector< std::list<double>::const_iterator > vec_iter_pointListx((graphList+graphNum)->curveNbr);
563  std::vector< std::list<double>::const_iterator > vec_iter_pointListy((graphList+graphNum)->curveNbr);
564  std::vector< std::list<double>::const_iterator > vec_iter_pointListz((graphList+graphNum)->curveNbr);
565 
566  fichier << (graphList+graphNum)->title << std::endl;
567 
568  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
569  {
570  vec_iter_pointListx[ind] = (graphList+graphNum)->curveList[ind].pointListx.begin();
571  vec_iter_pointListy[ind] = (graphList+graphNum)->curveList[ind].pointListy.begin();
572  vec_iter_pointListz[ind] = (graphList+graphNum)->curveList[ind].pointListz.begin();
573 // (graphList+graphNum)->curveList[ind].pointListx.front();
574 // (graphList+graphNum)->curveList[ind].pointListy.front();
575 // (graphList+graphNum)->curveList[ind].pointListz.front();
576  }
577 
578  while (end == false)
579  {
580  end = true;
581  for(ind=0;ind<(graphList+graphNum)->curveNbr;ind++)
582  {
583 // if (!(graphList+graphNum)->curveList[ind].pointListx.outside()
584 // && !(graphList+graphNum)->curveList[ind].pointListy.outside()
585 // && !(graphList+graphNum)->curveList[ind].pointListz.outside())
586  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
587  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
588  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
589  {
590  p[0] = *vec_iter_pointListx[ind];
591  p[1] = *vec_iter_pointListy[ind];
592  p[2] = *vec_iter_pointListz[ind];
593 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
594 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
595 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
596 
597  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
598  ++vec_iter_pointListx[ind];
599  ++vec_iter_pointListy[ind];
600  ++vec_iter_pointListz[ind];
601 // (graphList+graphNum)->curveList[ind].pointListx.next();
602 // (graphList+graphNum)->curveList[ind].pointListy.next();
603 // (graphList+graphNum)->curveList[ind].pointListz.next();
604 // if(!(graphList+graphNum)->curveList[ind].pointListx.nextOutside()
605 // && !(graphList+graphNum)->curveList[ind].pointListy.nextOutside()
606 // && !(graphList+graphNum)->curveList[ind].pointListz.nextOutside())
607  if((vec_iter_pointListx[ind] != (graphList+graphNum)->curveList[ind].pointListx.end())
608  && (vec_iter_pointListy[ind] != (graphList+graphNum)->curveList[ind].pointListy.end())
609  && (vec_iter_pointListz[ind] != (graphList+graphNum)->curveList[ind].pointListz.end()))
610  end = false;
611  }
612  else
613  {
614 // p[0] = (graphList+graphNum)->curveList[ind].pointListx.value();
615 // p[1] = (graphList+graphNum)->curveList[ind].pointListy.value();
616 // p[2] = (graphList+graphNum)->curveList[ind].pointListz.value();
617  p[0] = (graphList+graphNum)->curveList[ind].pointListx.back();
618  p[1] = (graphList+graphNum)->curveList[ind].pointListy.back();
619  p[2] = (graphList+graphNum)->curveList[ind].pointListz.back();
620  fichier << p[0] << "\t" << p[1] << "\t" << p[2] << "\t";
621  }
622  }
623  fichier << std::endl;
624  }
625 
626  delete[] p;
627  fichier.close();
628 }
629 
630 #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:384
#define vpTRACE
Definition: vpDebug.h:418
void setUnitY(const unsigned int graphNum, const char *unity)
Definition: vpPlot.cpp:445
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
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:470
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:421
void setUnitZ(const unsigned int graphNum, const char *unitz)
Definition: vpPlot.cpp:457
void setUnitX(const unsigned int graphNum, const char *unitx)
Definition: vpPlot.cpp:433
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:520
void navigate(void)
Definition: vpPlot.cpp:354
virtual bool getPointerPosition(vpImagePoint &ip)=0
void saveData(const unsigned int graphNum, const char *dataFile)
Definition: vpPlot.cpp:553
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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:399
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:481
void setGraphThickness(const unsigned int graphNum, const unsigned int thickness)
Definition: vpPlot.cpp:507
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:161
void setThickness(const unsigned int graphNum, const unsigned int curveNum, const unsigned int thickness)
Definition: vpPlot.cpp:495