ViSP  2.10.0
vpDisplayOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayOpenCV.cpp 5204 2015-01-24 13:18:18Z 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  * Image display.
36  *
37  * Authors:
38  * Christophe Collewet
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 
50 #include <visp/vpConfig.h>
51 
52 #if defined(VISP_HAVE_OPENCV)
53 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <iostream>
57 #include <cmath> // std::fabs
58 #include <limits> // numeric_limits
59 
60 // Display stuff
61 #include <visp/vpDisplay.h>
62 #include <visp/vpDisplayOpenCV.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpImageTools.h>
65 
66 //debug / exception
67 #include <visp/vpDebug.h>
68 #include <visp/vpDisplayException.h>
69 
70 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
71 
72 # include <opencv2/imgproc/imgproc.hpp>
73 # include <opencv2/core/core_c.h> // for CV_FILLED versus cv::FILLED
74 
75 # ifndef CV_RGB
76 # define CV_RGB( r, g, b ) cv::Scalar( (b), (g), (r), 0 )
77 # endif
78 #endif
79 
80 std::vector<std::string> vpDisplayOpenCV::m_listTitles = std::vector<std::string>();
81 unsigned int vpDisplayOpenCV::m_nbWindows = 0;
82 
94  int x,
95  int y,
96  const char *title)
97  :
98  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
99  background(NULL), col(NULL), cvcolor(), font(NULL),
100  #else
101  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
102  #endif
103  fontHeight(10), ncol(0), nrow(0), x_move(0), y_move(0) , move(false),
104  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
105  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
106  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
107  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
108  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
109  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
110 {
111  init(I, x, y, title) ;
112 }
113 
114 
124  int x,
125  int y,
126  const char *title)
127  :
128  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
129  background(NULL), col(NULL), cvcolor(), font(NULL),
130  #else
131  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
132  #endif
133  fontHeight(10), ncol(0), nrow(0), x_move(0), y_move(0) , move(false),
134  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
135  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
136  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
137  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
138  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
139  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
140 {
141  init(I, x, y, title) ;
142 }
143 
166 vpDisplayOpenCV::vpDisplayOpenCV ( int x, int y, const char *title )
167  :
168  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
169  background(NULL), col(NULL), cvcolor(), font(NULL),
170  #else
171  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
172  #endif
173  fontHeight(10), ncol(0), nrow(0), x_move(0), y_move(0) , move(false),
174  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
175  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
176  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
177  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
178  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
179  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
180 {
181  windowXPosition = x;
182  windowYPosition = y;
183 
184  if(title != NULL){
185  title_ = std::string(title);
186  }
187  else{
188  std::ostringstream s;
189  s << m_nbWindows++;
190  title_ = std::string("Window ") + s.str();
191  }
192 
193  bool isInList;
194  do{
195  isInList = false;
196  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
197  if(m_listTitles[i] == title_){
198  std::ostringstream s;
199  s << m_nbWindows++;
200  title_ = std::string("Window ") + s.str();
201  isInList = true;
202  break;
203  }
204  }
205  }
206  while(isInList);
207 
208  m_listTitles.push_back(title_);
209 }
210 
231  :
232  #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
233  background(NULL), col(NULL), cvcolor(), font(NULL),
234  #else
235  background(), col(NULL), cvcolor(), font(cv::FONT_HERSHEY_PLAIN), fontScale(0.8f),
236  #endif
237  fontHeight(10), ncol(0), nrow(0), x_move(0), y_move(0) , move(false),
238  x_lbuttondown(0), y_lbuttondown(0), lbuttondown(false),
239  x_mbuttondown(0), y_mbuttondown(0), mbuttondown(false),
240  x_rbuttondown(0), y_rbuttondown(0), rbuttondown(false),
241  x_lbuttonup(0), y_lbuttonup(0), lbuttonup(false),
242  x_mbuttonup(0), y_mbuttonup(0), mbuttonup(false),
243  x_rbuttonup(0), y_rbuttonup(0), rbuttonup(false)
244 {
245 }
246 
251 {
252  closeDisplay() ;
253 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
254  cvReleaseImage(&background);
255 #endif
256 }
257 
266 void
268  int x,
269  int y,
270  const char *title)
271 {
272 
273  if ((I.getHeight() == 0) || (I.getWidth()==0))
274  {
275  vpERROR_TRACE("Image not initialized " ) ;
277  "Image not initialized")) ;
278  }
279  init (I.getWidth(), I.getHeight(), x, y, title) ;
280  I.display = this ;
282 }
283 
293 void
295  int x,
296  int y,
297  const char *title)
298 {
299  if ((I.getHeight() == 0) || (I.getWidth()==0))
300  {
301  vpERROR_TRACE("Image not initialized " ) ;
303  "Image not initialized")) ;
304  }
305 
306  init (I.getWidth(), I.getHeight(), x, y, title) ;
307  I.display = this ;
309 }
310 
321 void
322 vpDisplayOpenCV::init(unsigned int w, unsigned int h,
323  int x, int y,
324  const char *title)
325 {
326  this->width = w;
327  this->height = h;
328 
329  if (x != -1)
330  this->windowXPosition = x;
331  if (y != -1)
332  this->windowYPosition = y;
333 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
334  int flags = CV_WINDOW_AUTOSIZE;
335 #else
336  int flags = cv::WINDOW_AUTOSIZE;
337 #endif
338 
339  if(title_.empty()){
340  if(title != NULL){
341  title_ = std::string(title);
342  }
343  else{
344 
345  std::ostringstream s;
346  s << m_nbWindows++;
347  title_ = std::string("Window ") + s.str();
348  }
349 
350  bool isInList;
351  do{
352  isInList = false;
353  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
354  if(m_listTitles[i] == title_){
355  std::ostringstream s;
356  s << m_nbWindows++;
357  title_ = std::string("Window ") + s.str();
358  isInList = true;
359  break;
360  }
361  }
362  }
363  while(isInList);
364 
365  m_listTitles.push_back(title_);
366  }
367 
368  /* Create the window*/
369 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
370  if (cvNamedWindow( this->title_.c_str(), flags ) < 0) {
371  vpERROR_TRACE("OpenCV was not built with a display device");
373  "OpenCV was not built with a display device")) ;
374  }
375 #else
376  cv::namedWindow( this->title_, flags );
377 #endif
378 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
379  cvMoveWindow( this->title_.c_str(), this->windowXPosition, this->windowYPosition );
380 #else
381  cv::moveWindow( this->title_.c_str(), this->windowXPosition, this->windowYPosition );
382 #endif
383  move = false;
384  lbuttondown = false;
385  mbuttondown = false;
386  rbuttondown = false;
387  lbuttonup = false;
388  mbuttonup = false;
389  rbuttonup = false;
390 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
391  cvSetMouseCallback( this->title_.c_str(), on_mouse, this );
392  col = new CvScalar[vpColor::id_unknown] ;
393 #else
394  cv::setMouseCallback( this->title_, on_mouse, this );
395  col = new cv::Scalar[vpColor::id_unknown] ;
396 #endif
397 
398  /* Create color */
399  vpColor pcolor; // Predefined colors
400  pcolor = vpColor::lightBlue;
401  col[vpColor::id_lightBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
402  pcolor = vpColor::blue;
403  col[vpColor::id_blue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
404  pcolor = vpColor::darkBlue;
405  col[vpColor::id_darkBlue] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
406  pcolor = vpColor::lightRed;
407  col[vpColor::id_lightRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
408  pcolor = vpColor::red;
409  col[vpColor::id_red] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
410  pcolor = vpColor::darkRed;
411  col[vpColor::id_darkRed] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
412  pcolor = vpColor::lightGreen;
413  col[vpColor::id_lightGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
414  pcolor = vpColor::green;
415  col[vpColor::id_green] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
416  pcolor = vpColor::darkGreen;
417  col[vpColor::id_darkGreen] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
418  pcolor = vpColor::yellow;
419  col[vpColor::id_yellow] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
420  pcolor = vpColor::cyan;
421  col[vpColor::id_cyan] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
422  pcolor = vpColor::orange;
423  col[vpColor::id_orange] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
424  pcolor = vpColor::purple;
425  col[vpColor::id_purple] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
426  pcolor = vpColor::white;
427  col[vpColor::id_white] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
428  pcolor = vpColor::black;
429  col[vpColor::id_black] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
430  pcolor = vpColor::lightGray;
431  col[vpColor::id_lightGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
432  pcolor = vpColor::gray;
433  col[vpColor::id_gray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
434  pcolor = vpColor::darkGray;
435  col[vpColor::id_darkGray] = CV_RGB(pcolor.R, pcolor.G, pcolor.B) ;
436 
437 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
438  font = new CvFont;
439  cvInitFont( font, CV_FONT_HERSHEY_PLAIN, 0.70f,0.70f);
440  CvSize fontSize;
441  int baseline;
442  cvGetTextSize( "A", font, &fontSize, &baseline );
443 #else
444  int thickness = 1;
445  cv::Size fontSize;
446  int baseline;
447  fontSize = cv::getTextSize( "A", font, fontScale, thickness, &baseline );
448 #endif
449 
450  fontHeight = fontSize.height + baseline;
452 }
453 
454 
470 void
471 vpDisplayOpenCV::setFont(const char * /* font */)
472 {
473  vpERROR_TRACE("Not yet implemented" ) ;
474 }
475 
483 void
484 vpDisplayOpenCV::setTitle(const char * /* title */)
485 {
486 // static bool warn_displayed = false;
487 // if (! warn_displayed) {
488 // vpTRACE("Not implemented");
489 // warn_displayed = true;
490 // }
491 }
492 
493 
502 void vpDisplayOpenCV::setWindowPosition(int winx, int winy)
503 {
505  this->windowXPosition = winx;
506  this->windowYPosition = winy;
507 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
508  cvMoveWindow( this->title_.c_str(), winx, winy );
509 #else
510  cv::moveWindow( this->title_.c_str(), winx, winy );
511 #endif
512  }
513  else
514  {
515  vpERROR_TRACE("OpenCV not initialized " ) ;
517  "OpenCV not initialized")) ;
518  }
519 }
532 {
534  {
535  vpImage<vpRGBa> Ic;
537  vpImageConvert::convert(Ic,background);
538  /* Copie de l'image dans le pixmap fond */
539  width = I.getWidth();
540  height = I.getHeight();
541  /* Le pixmap background devient le fond de la zone de dessin */
542  }
543  else
544  {
545  vpERROR_TRACE("openCV not initialized " ) ;
547  "OpenCV not initialized")) ;
548  }
549 }
550 
567  const unsigned int w, const unsigned int h )
568 {
570  {
572  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Itemp);
573  vpImage<vpRGBa> Ic;
574  vpImageConvert::convert(Itemp,Ic);
575 
576 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
577  int depth = 8;
578  int channels = 3;
579  CvSize size = cvSize((int)this->width, (int)this->height);
580  if (background != NULL){
581  if(background->nChannels != channels || background->depth != depth
582  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
583  if(background->nChannels != 0) cvReleaseImage(&background);
584  background = cvCreateImage( size, depth, channels );
585  }
586  }
587  else background = cvCreateImage( size, depth, channels );
588  IplImage* Ip = NULL;
589  vpImageConvert::convert(Ic, Ip);
590  unsigned char * input = (unsigned char*)Ip->imageData;
591  unsigned char * output = (unsigned char*)background->imageData;
592 
593  unsigned int iwidth = Ic.getWidth();
594 
595  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
596 
597  unsigned int i = 0;
598  while (i < h)
599  {
600  unsigned int j = 0;
601  while (j < w)
602  {
603  *(output+3*j) = *(input+j*3);
604  *(output+3*j+1) = *(input+j*3+1);
605  *(output+3*j+2) = *(input+j*3+2);
606  j++;
607  }
608  input = input + 3*iwidth;
609  output = output + 3*this->width;
610  i++;
611  }
612 
613  cvReleaseImage(&Ip);
614 #else
615  int depth = CV_8U;
616  int channels = 3;
617  cv::Size size((int)this->width, (int)this->height);
618  if(background.channels() != channels || background.depth() != depth
619  || background.rows != (int) I.getHeight() || background.cols != (int) I.getWidth()){
620  background = cv::Mat( size, CV_MAKETYPE(depth, channels) );
621  }
622 
623  cv::Mat Ip;
624  vpImageConvert::convert(Ic, Ip);
625 
626  unsigned char * input = (unsigned char*)Ip.data;
627  unsigned char * output = (unsigned char*)background.data;
628 
629  unsigned int iwidth = Ic.getWidth();
630 
631  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
632 
633  unsigned int i = 0;
634  while (i < h)
635  {
636  unsigned int j = 0;
637  while (j < w)
638  {
639  *(output+3*j) = *(input+j*3);
640  *(output+3*j+1) = *(input+j*3+1);
641  *(output+3*j+2) = *(input+j*3+2);
642  j++;
643  }
644  input = input + 3*iwidth;
645  output = output + 3*this->width;
646  i++;
647  }
648 #endif
649  }
650  else
651  {
652  vpERROR_TRACE("openCV not initialized " ) ;
654  "OpenCV not initialized")) ;
655  }
656 }
657 
658 
671 {
672 
674  {
675  /* Copie de l'image dans le pixmap fond */
676 
677  vpImageConvert::convert(I,background);
678  /* Copie de l'image dans le pixmap fond */
679  width = I.getWidth();
680  height = I.getHeight();
681  }
682  else
683  {
684  vpERROR_TRACE("OpenCV not initialized " ) ;
686  "OpenCV not initialized")) ;
687  }
688 }
689 
706  const unsigned int w, const unsigned int h )
707 {
709  {
710  vpImage<vpRGBa> Ic;
711  vpImageTools::createSubImage(I,(unsigned int)iP.get_i(),(unsigned int)iP.get_j(),h,w,Ic);
712 
713 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
714  CvSize size = cvSize((int)this->width, (int)this->height);
715  int depth = 8;
716  int channels = 3;
717  if (background != NULL){
718  if(background->nChannels != channels || background->depth != depth
719  || background->height != (int) I.getHeight() || background->width != (int) I.getWidth()){
720  if(background->nChannels != 0) cvReleaseImage(&background);
721  background = cvCreateImage( size, depth, channels );
722  }
723  }
724  else background = cvCreateImage( size, depth, channels );
725 
726  IplImage* Ip = NULL;
727  vpImageConvert::convert(Ic, Ip);
728 
729  unsigned char * input = (unsigned char*)Ip->imageData;
730  unsigned char * output = (unsigned char*)background->imageData;
731 
732  unsigned int iwidth = Ic.getWidth();
733 
734  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
735 
736  unsigned int i = 0;
737  while (i < h)
738  {
739  unsigned int j = 0;
740  while (j < w)
741  {
742  *(output+3*j) = *(input+j*3);
743  *(output+3*j+1) = *(input+j*3+1);
744  *(output+3*j+2) = *(input+j*3+2);
745  j++;
746  }
747  input = input + 3*iwidth;
748  output = output + 3*this->width;
749  i++;
750  }
751 
752  cvReleaseImage(&Ip);
753 #else
754  int depth = CV_8U;
755  int channels = 3;
756  cv::Size size((int)this->width, (int)this->height);
757  if(background.channels() != channels || background.depth() != depth
758  || background.rows != (int) I.getHeight() || background.cols != (int) I.getWidth()){
759  background = cv::Mat( size, CV_MAKETYPE(depth, channels) );
760  }
761  cv::Mat Ip;
762  vpImageConvert::convert(Ic, Ip);
763 
764  unsigned char * input = (unsigned char*)Ip.data;
765  unsigned char * output = (unsigned char*)background.data;
766 
767  unsigned int iwidth = Ic.getWidth();
768 
769  output = output + (int)(iP.get_i()*3*this->width+ iP.get_j()*3);
770 
771  unsigned int i = 0;
772  while (i < h)
773  {
774  unsigned int j = 0;
775  while (j < w)
776  {
777  *(output+3*j) = *(input+j*3);
778  *(output+3*j+1) = *(input+j*3+1);
779  *(output+3*j+2) = *(input+j*3+2);
780  j++;
781  }
782  input = input + 3*iwidth;
783  output = output + 3*this->width;
784  i++;
785  }
786 #endif
787  }
788  else
789  {
790  vpERROR_TRACE("openCV not initialized " ) ;
792  "OpenCV not initialized")) ;
793  }
794 }
795 
796 
802 void vpDisplayOpenCV::displayImage(const unsigned char * /* I */)
803 {
804  vpTRACE(" not implemented ") ;
805 }
806 
815 {
816  if (col != NULL)
817  {
818  delete [] col ; col = NULL ;
819  }
820 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
821  if (font != NULL)
822  {
823  delete font ;
824  font = NULL ;
825  }
826 #endif
828 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
829  cvDestroyWindow( this->title_.c_str() );
830 #else
831  cv::destroyWindow( this->title_ );
832 #endif
833 
834  for(size_t i = 0 ; i < m_listTitles.size() ; i++){
835  if(title_ == m_listTitles[i]){
836  m_listTitles.erase(m_listTitles.begin()+(long int)i);
837  break;
838  }
839  }
840 
841  title_.clear();
842 
844  }
845 }
846 
847 
854 {
856  {
857 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
858  cvShowImage(this->title_.c_str(), background );
859  cvWaitKey(5);
860 #else
861  cv::imshow(this->title_, background );
862  cv::waitKey(5);
863 #endif
864  }
865  else
866  {
867  vpERROR_TRACE("OpenCV not initialized " ) ;
869  "OpenCV not initialized")) ;
870  }
871 }
872 
878 void vpDisplayOpenCV::flushDisplayROI(const vpImagePoint &/*iP*/, const unsigned int /*width*/, const unsigned int /*height*/)
879 {
881  {
882 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
883  cvShowImage(this->title_.c_str(), background );
884  cvWaitKey(5);
885 #else
886  cv::imshow(this->title_.c_str(), background );
887  cv::waitKey(5);
888 #endif
889  }
890  else
891  {
892  vpERROR_TRACE("OpenCV not initialized " ) ;
894  "OpenCV not initialized")) ;
895  }
896 }
897 
898 
902 void vpDisplayOpenCV::clearDisplay(const vpColor & /* color */)
903 {
904  static bool warn_displayed = false;
905  if (! warn_displayed) {
906  vpTRACE("Not implemented");
907  warn_displayed = true;
908  }
909 }
910 
919  const vpImagePoint &ip2,
920  const vpColor &color,
921  unsigned int w, unsigned int h,
922  unsigned int thickness)
923 {
925  {
926  try{
927  double a = ip2.get_i() - ip1.get_i() ;
928  double b = ip2.get_j() - ip1.get_j() ;
929  double lg = sqrt(vpMath::sqr(a)+vpMath::sqr(b)) ;
930 
931  //if ((a==0)&&(b==0))
932  if ((std::fabs(a) <= std::numeric_limits<double>::epsilon())
933  &&(std::fabs(b)<= std::numeric_limits<double>::epsilon()))
934  {
935  // DisplayCrossLarge(i1,j1,3,col) ;
936  }
937  else
938  {
939  a /= lg ;
940  b /= lg ;
941 
942  vpImagePoint ip3;
943  ip3.set_i(ip2.get_i() - w*a);
944  ip3.set_j(ip2.get_j() - w*b);
945 
946  vpImagePoint ip4;
947  ip4.set_i( ip3.get_i() - b*h );
948  ip4.set_j( ip3.get_j() + a*h );
949 
950  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
951  displayLine ( ip2, ip4, color, thickness ) ;
952 
953  ip4.set_i( ip3.get_i() + b*h );
954  ip4.set_j( ip3.get_j() - a*h );
955 
956  if (lg > 2*vpImagePoint::distance(ip2, ip4) )
957  displayLine ( ip2, ip4, color, thickness ) ;
958 
959  displayLine ( ip1, ip2, color, thickness ) ;
960  }
961  }
962  catch (...)
963  {
964  vpERROR_TRACE("Error caught") ;
965  throw ;
966  }
967  }
968  else
969  {
970  vpERROR_TRACE("OpenCV not initialized " ) ;
972  "OpenCV not initialized")) ;
973  }
974 }
975 
988  const char *text,
989  const vpColor &color )
990 {
992  {
993  if (color.id < vpColor::id_unknown) {
994 #if VISP_HAVE_OPENCV_VERSION < 0x020408
995  cvPutText( background, text,
996  cvPoint( vpMath::round( ip.get_u() ),
997  vpMath::round( ip.get_v()+fontHeight ) ),
998  font, col[color.id] );
999 #else
1000  cv::putText( background, text,
1001  cv::Point( vpMath::round( ip.get_u() ),
1002  vpMath::round( ip.get_v()+fontHeight ) ),
1003  font, fontScale, col[color.id] );
1004 #endif
1005  }
1006  else {
1007  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1008 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1009  cvPutText( background, text,
1010  cvPoint( vpMath::round( ip.get_u() ),
1011  vpMath::round( ip.get_v()+fontHeight ) ),
1012  font, cvcolor );
1013 #else
1014  cv::putText( background, text,
1015  cv::Point( vpMath::round( ip.get_u() ),
1016  vpMath::round( ip.get_v()+fontHeight ) ),
1017  font, fontScale, cvcolor );
1018 #endif
1019  }
1020  }
1021  else
1022  {
1023  vpERROR_TRACE("OpenCV not initialized " ) ;
1025  "OpenCV not initialized")) ;
1026  }
1027 }
1038  unsigned int radius,
1039  const vpColor &color,
1040  bool fill ,
1041  unsigned int thickness)
1042 {
1044  {
1045  if (fill == false) {
1046  if (color.id < vpColor::id_unknown) {
1047 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1048  cvCircle( background,
1049  cvPoint( vpMath::round( center.get_u() ),
1050  vpMath::round( center.get_v() ) ),
1051  (int)radius, col[color.id], (int)thickness);
1052 #else
1053  cv::circle( background,
1054  cv::Point( vpMath::round( center.get_u() ),
1055  vpMath::round( center.get_v() ) ),
1056  (int)radius, col[color.id], (int)thickness);
1057 #endif
1058  }
1059  else {
1060  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1061 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1062  cvCircle( background,
1063  cvPoint( vpMath::round( center.get_u() ),
1064  vpMath::round( center.get_v() ) ),
1065  (int)radius, cvcolor, (int)thickness);
1066 #else
1067  cv::circle( background,
1068  cv::Point( vpMath::round( center.get_u() ),
1069  vpMath::round( center.get_v() ) ),
1070  (int)radius, cvcolor, (int)thickness);
1071 #endif
1072  }
1073  }
1074  else {
1075 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1076  int filled = cv::FILLED;
1077 #else
1078  int filled = CV_FILLED;
1079 #endif
1080  if (color.id < vpColor::id_unknown) {
1081 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1082  cvCircle( background,
1083  cvPoint( vpMath::round( center.get_u() ),
1084  vpMath::round( center.get_v() ) ),
1085  (int)radius, col[color.id], filled);
1086 #else
1087  cv::circle( background,
1088  cv::Point( vpMath::round( center.get_u() ),
1089  vpMath::round( center.get_v() ) ),
1090  (int)radius, col[color.id], filled);
1091 #endif
1092  }
1093  else {
1094  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1095 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1096  cvCircle( background,
1097  cvPoint( vpMath::round( center.get_u() ),
1098  vpMath::round( center.get_v() ) ),
1099  (int)radius, cvcolor, filled);
1100 #else
1101  cv::circle( background,
1102  cv::Point( vpMath::round( center.get_u() ),
1103  vpMath::round( center.get_v() ) ),
1104  (int)radius, cvcolor, filled);
1105 #endif
1106  }
1107  }
1108  }
1109  else
1110  {
1111  vpERROR_TRACE("OpenCV not initialized " ) ;
1113  "OpenCV not initialized")) ;
1114  }
1115 }
1116 
1124 void
1126  unsigned int size,
1127  const vpColor &color,
1128  unsigned int thickness)
1129 {
1131  {
1132  vpImagePoint top,bottom,left,right;
1133  top.set_i(ip.get_i()-size/2);
1134  top.set_j(ip.get_j());
1135  bottom.set_i(ip.get_i()+size/2);
1136  bottom.set_j(ip.get_j());
1137  left.set_i(ip.get_i());
1138  left.set_j(ip.get_j()-size/2);
1139  right.set_i(ip.get_i());
1140  right.set_j(ip.get_j()+size/2);
1141  try{
1142  displayLine(top, bottom, color, thickness) ;
1143  displayLine(left, right, color, thickness) ;
1144  }
1145  catch (...)
1146  {
1147  vpERROR_TRACE("Error caught") ;
1148  throw ;
1149  }
1150  }
1151 
1152  else
1153  {
1154  vpERROR_TRACE("OpenCV not initialized " ) ;
1156  "OpenCV not initialized")) ;
1157  }
1158 
1159 }
1160 
1171 void
1173  const vpImagePoint &ip2,
1174  const vpColor &color,
1175  unsigned int thickness)
1176 {
1177 
1179  {
1180  //vpTRACE("Dot lines are not yet implemented");
1181  if (color.id < vpColor::id_unknown) {
1182 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1183  cvLine( background,
1184  cvPoint( vpMath::round( ip1.get_u() ),
1185  vpMath::round( ip1.get_v() ) ),
1186  cvPoint( vpMath::round( ip2.get_u() ),
1187  vpMath::round( ip2.get_v() ) ),
1188  col[color.id], (int) thickness);
1189 #else
1190  cv::line( background,
1191  cv::Point( vpMath::round( ip1.get_u() ),
1192  vpMath::round( ip1.get_v() ) ),
1193  cv::Point( vpMath::round( ip2.get_u() ),
1194  vpMath::round( ip2.get_v() ) ),
1195  col[color.id], (int) thickness);
1196 #endif
1197  }
1198  else {
1199  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1200 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1201  cvLine( background,
1202  cvPoint( vpMath::round( ip1.get_u() ),
1203  vpMath::round( ip1.get_v() ) ),
1204  cvPoint( vpMath::round( ip2.get_u() ),
1205  vpMath::round( ip2.get_v() ) ),
1206  cvcolor, (int) thickness);
1207 #else
1208  cv::line( background,
1209  cv::Point( vpMath::round( ip1.get_u() ),
1210  vpMath::round( ip1.get_v() ) ),
1211  cv::Point( vpMath::round( ip2.get_u() ),
1212  vpMath::round( ip2.get_v() ) ),
1213  cvcolor, (int) thickness);
1214 #endif
1215  }
1216  }
1217  else
1218  {
1219  vpERROR_TRACE("OpenCV not initialized " ) ;
1221  "OpenCV not initialized")) ;
1222  }
1223 }
1224 
1225 
1232 void
1234  const vpImagePoint &ip2,
1235  const vpColor &color,
1236  unsigned int thickness)
1237 {
1239  {
1240  if (color.id < vpColor::id_unknown) {
1241 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1242  cvLine( background,
1243  cvPoint( vpMath::round( ip1.get_u() ),
1244  vpMath::round( ip1.get_v() ) ),
1245  cvPoint( vpMath::round( ip2.get_u() ),
1246  vpMath::round( ip2.get_v() ) ),
1247  col[color.id], (int) thickness);
1248 #else
1249  cv::line( background,
1250  cv::Point( vpMath::round( ip1.get_u() ),
1251  vpMath::round( ip1.get_v() ) ),
1252  cv::Point( vpMath::round( ip2.get_u() ),
1253  vpMath::round( ip2.get_v() ) ),
1254  col[color.id], (int) thickness);
1255 #endif
1256  }
1257  else {
1258  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1259 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1260  cvLine( background,
1261  cvPoint( vpMath::round( ip1.get_u() ),
1262  vpMath::round( ip1.get_v() ) ),
1263  cvPoint( vpMath::round( ip2.get_u() ),
1264  vpMath::round( ip2.get_v() ) ),
1265  cvcolor, (int) thickness);
1266 #else
1267  cv::line( background,
1268  cv::Point( vpMath::round( ip1.get_u() ),
1269  vpMath::round( ip1.get_v() ) ),
1270  cv::Point( vpMath::round( ip2.get_u() ),
1271  vpMath::round( ip2.get_v() ) ),
1272  cvcolor, (int) thickness);
1273 #endif
1274  }
1275  }
1276  else
1277  {
1278  vpERROR_TRACE("OpenCV not initialized " ) ;
1280  "OpenCV not initialized")) ;
1281  }
1282 }
1283 
1290  const vpColor &color)
1291 {
1293  {
1294  displayLine(ip,ip,color,1);
1295 // if (color.id < vpColor::id_unknown) {
1296 // ((uchar*)(background->imageData
1297 // + background->widthStep*vpMath::round( ip.get_i() )))
1298 // [vpMath::round( ip.get_j()*3 )] = (uchar)col[color.id].val[0];
1299 //
1300 // ((uchar*)(background->imageData
1301 // + background->widthStep*vpMath::round( ip.get_i() )))
1302 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)col[color.id].val[1];
1303 //
1304 // ((uchar*)(background->imageData
1305 // + background->widthStep*vpMath::round( ip.get_i() )))
1306 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)col[color.id].val[2];
1307 // }
1308 // else {
1309 // cvcolor = CV_RGB(color.R, color.G, color.B) ;
1310 // ((uchar*)(background->imageData
1311 // + background->widthStep*vpMath::round( ip.get_i() )))
1312 // [vpMath::round( ip.get_j()*3 )] = (uchar)cvcolor.val[0];
1313 //
1314 // ((uchar*)(background->imageData
1315 // + background->widthStep*vpMath::round( ip.get_i() )))
1316 // [vpMath::round( ip.get_j()*3+1 )] = (uchar)cvcolor.val[1];
1317 //
1318 // ((uchar*)(background->imageData
1319 // + background->widthStep*vpMath::round( ip.get_i() )))
1320 // [vpMath::round( ip.get_j()*3+2 )] = (uchar)cvcolor.val[2];
1321 //
1322 // }
1323  }
1324  else
1325  {
1326  vpERROR_TRACE("OpenCV not initialized " ) ;
1328  "OpenCV not initialized")) ;
1329  }
1330 }
1331 
1345 void
1347  unsigned int w, unsigned int h,
1348  const vpColor &color, bool fill,
1349  unsigned int thickness)
1350 {
1352  {
1353  if (fill == false) {
1354  if (color.id < vpColor::id_unknown) {
1355 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1356  cvRectangle( background,
1357  cvPoint( vpMath::round( topLeft.get_u() ),
1358  vpMath::round( topLeft.get_v() ) ),
1359  cvPoint( vpMath::round( topLeft.get_u()+w ),
1360  vpMath::round( topLeft.get_v()+h ) ),
1361  col[color.id], (int)thickness);
1362 #else
1363  cv::rectangle( background,
1364  cv::Point( vpMath::round( topLeft.get_u() ),
1365  vpMath::round( topLeft.get_v() ) ),
1366  cv::Point( vpMath::round( topLeft.get_u()+w ),
1367  vpMath::round( topLeft.get_v()+h ) ),
1368  col[color.id], (int)thickness);
1369 #endif
1370  }
1371  else {
1372  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1373 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1374  cvRectangle( background,
1375  cvPoint( vpMath::round( topLeft.get_u() ),
1376  vpMath::round( topLeft.get_v() ) ),
1377  cvPoint( vpMath::round( topLeft.get_u()+w ),
1378  vpMath::round( topLeft.get_v()+h ) ),
1379  cvcolor, (int)thickness);
1380 #else
1381  cv::rectangle( background,
1382  cv::Point( vpMath::round( topLeft.get_u() ),
1383  vpMath::round( topLeft.get_v() ) ),
1384  cv::Point( vpMath::round( topLeft.get_u()+w ),
1385  vpMath::round( topLeft.get_v()+h ) ),
1386  cvcolor, (int)thickness);
1387 #endif
1388  }
1389  }
1390  else {
1391 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1392  int filled = cv::FILLED;
1393 #else
1394  int filled = CV_FILLED;
1395 #endif
1396  if (color.id < vpColor::id_unknown) {
1397 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1398  cvRectangle( background,
1399  cvPoint( vpMath::round( topLeft.get_u() ),
1400  vpMath::round( topLeft.get_v() ) ),
1401  cvPoint( vpMath::round( topLeft.get_u()+w ),
1402  vpMath::round( topLeft.get_v()+h ) ),
1403  col[color.id], filled);
1404 #else
1405  cv::rectangle( background,
1406  cv::Point( vpMath::round( topLeft.get_u() ),
1407  vpMath::round( topLeft.get_v() ) ),
1408  cv::Point( vpMath::round( topLeft.get_u()+w ),
1409  vpMath::round( topLeft.get_v()+h ) ),
1410  col[color.id], filled);
1411 #endif
1412  }
1413  else {
1414  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1415 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1416  cvRectangle( background,
1417  cvPoint( vpMath::round( topLeft.get_u() ),
1418  vpMath::round( topLeft.get_v() ) ),
1419  cvPoint( vpMath::round( topLeft.get_u()+w ),
1420  vpMath::round( topLeft.get_v()+h ) ),
1421  cvcolor, filled);
1422 #else
1423  cv::rectangle( background,
1424  cv::Point( vpMath::round( topLeft.get_u() ),
1425  vpMath::round( topLeft.get_v() ) ),
1426  cv::Point( vpMath::round( topLeft.get_u()+w ),
1427  vpMath::round( topLeft.get_v()+h ) ),
1428  cvcolor, filled);
1429 #endif
1430  }
1431  }
1432  }
1433  else
1434  {
1435  vpERROR_TRACE("OpenCV not initialized " ) ;
1437  "OpenCV not initialized")) ;
1438  }
1439 }
1452 void
1454  const vpImagePoint &bottomRight,
1455  const vpColor &color, bool fill,
1456  unsigned int thickness )
1457 {
1459  {
1460  if (fill == false) {
1461  if (color.id < vpColor::id_unknown) {
1462 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1463  cvRectangle( background,
1464  cvPoint( vpMath::round( topLeft.get_u() ),
1465  vpMath::round( topLeft.get_v() ) ),
1466  cvPoint( vpMath::round( bottomRight.get_u() ),
1467  vpMath::round( bottomRight.get_v() ) ),
1468  col[color.id], (int)thickness);
1469 #else
1470  cv::rectangle( background,
1471  cv::Point( vpMath::round( topLeft.get_u() ),
1472  vpMath::round( topLeft.get_v() ) ),
1473  cv::Point( vpMath::round( bottomRight.get_u() ),
1474  vpMath::round( bottomRight.get_v() ) ),
1475  col[color.id], (int)thickness);
1476 #endif
1477  }
1478  else {
1479  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1480 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1481  cvRectangle( background,
1482  cvPoint( vpMath::round( topLeft.get_u() ),
1483  vpMath::round( topLeft.get_v() ) ),
1484  cvPoint( vpMath::round( bottomRight.get_u() ),
1485  vpMath::round( bottomRight.get_v() ) ),
1486  cvcolor, (int)thickness);
1487 #else
1488  cv::rectangle( background,
1489  cv::Point( vpMath::round( topLeft.get_u() ),
1490  vpMath::round( topLeft.get_v() ) ),
1491  cv::Point( vpMath::round( bottomRight.get_u() ),
1492  vpMath::round( bottomRight.get_v() ) ),
1493  cvcolor, (int)thickness);
1494 #endif
1495  }
1496  }
1497  else {
1498 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1499  int filled = cv::FILLED;
1500 #else
1501  int filled = CV_FILLED;
1502 #endif
1503  if (color.id < vpColor::id_unknown) {
1504 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1505  cvRectangle( background,
1506  cvPoint( vpMath::round( topLeft.get_u() ),
1507  vpMath::round( topLeft.get_v() ) ),
1508  cvPoint( vpMath::round( bottomRight.get_u() ),
1509  vpMath::round( bottomRight.get_v() ) ),
1510  col[color.id], filled);
1511 #else
1512  cv::rectangle( background,
1513  cv::Point( vpMath::round( topLeft.get_u() ),
1514  vpMath::round( topLeft.get_v() ) ),
1515  cv::Point( vpMath::round( bottomRight.get_u() ),
1516  vpMath::round( bottomRight.get_v() ) ),
1517  col[color.id], filled);
1518 #endif
1519  }
1520  else {
1521  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1522 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1523  cvRectangle( background,
1524  cvPoint( vpMath::round( topLeft.get_u() ),
1525  vpMath::round( topLeft.get_v() ) ),
1526  cvPoint( vpMath::round( bottomRight.get_u() ),
1527  vpMath::round( bottomRight.get_v() ) ),
1528  cvcolor, filled);
1529 #else
1530  cv::rectangle( background,
1531  cv::Point( vpMath::round( topLeft.get_u() ),
1532  vpMath::round( topLeft.get_v() ) ),
1533  cv::Point( vpMath::round( bottomRight.get_u() ),
1534  vpMath::round( bottomRight.get_v() ) ),
1535  cvcolor, filled);
1536 #endif
1537  }
1538  }
1539  }
1540  else
1541  {
1542  vpERROR_TRACE("OpenCV not initialized " ) ;
1544  "OpenCV not initialized")) ;
1545  }
1546 }
1547 
1560 void
1562  const vpColor &color, bool fill,
1563  unsigned int thickness)
1564 {
1566  {
1567  if (fill == false) {
1568  if (color.id < vpColor::id_unknown) {
1569 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1570  cvRectangle( background,
1571  cvPoint( vpMath::round( rectangle.getLeft() ),
1572  vpMath::round( rectangle.getBottom() ) ),
1573  cvPoint( vpMath::round( rectangle.getRight() ),
1574  vpMath::round( rectangle.getTop() ) ),
1575  col[color.id], (int)thickness);
1576 #else
1577  cv::rectangle( background,
1578  cv::Point( vpMath::round( rectangle.getLeft() ),
1579  vpMath::round( rectangle.getBottom() ) ),
1580  cv::Point( vpMath::round( rectangle.getRight() ),
1581  vpMath::round( rectangle.getTop() ) ),
1582  col[color.id], (int)thickness);
1583 #endif
1584  }
1585  else {
1586  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1587 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1588  cvRectangle( background,
1589  cvPoint( vpMath::round( rectangle.getLeft() ),
1590  vpMath::round( rectangle.getBottom() ) ),
1591  cvPoint( vpMath::round( rectangle.getRight() ),
1592  vpMath::round( rectangle.getTop() ) ),
1593  cvcolor, (int)thickness);
1594 
1595 #else
1596  cv::rectangle( background,
1597  cv::Point( vpMath::round( rectangle.getLeft() ),
1598  vpMath::round( rectangle.getBottom() ) ),
1599  cv::Point( vpMath::round( rectangle.getRight() ),
1600  vpMath::round( rectangle.getTop() ) ),
1601  cvcolor, (int)thickness);
1602 
1603 #endif
1604  }
1605  }
1606  else {
1607 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
1608  int filled = cv::FILLED;
1609 #else
1610  int filled = CV_FILLED;
1611 #endif
1612  if (color.id < vpColor::id_unknown) {
1613 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1614  cvRectangle( background,
1615  cvPoint( vpMath::round( rectangle.getLeft() ),
1616  vpMath::round( rectangle.getBottom() ) ),
1617  cvPoint( vpMath::round( rectangle.getRight() ),
1618  vpMath::round( rectangle.getTop() ) ),
1619  col[color.id], filled);
1620 #else
1621  cv::rectangle( background,
1622  cv::Point( vpMath::round( rectangle.getLeft() ),
1623  vpMath::round( rectangle.getBottom() ) ),
1624  cv::Point( vpMath::round( rectangle.getRight() ),
1625  vpMath::round( rectangle.getTop() ) ),
1626  col[color.id], filled);
1627 #endif
1628  }
1629  else {
1630  cvcolor = CV_RGB(color.R, color.G, color.B) ;
1631 #if VISP_HAVE_OPENCV_VERSION < 0x020408
1632  cvRectangle( background,
1633  cvPoint( vpMath::round( rectangle.getLeft() ),
1634  vpMath::round( rectangle.getBottom() ) ),
1635  cvPoint( vpMath::round( rectangle.getRight() ),
1636  vpMath::round( rectangle.getTop() ) ),
1637  cvcolor, filled);
1638 #else
1639  cv::rectangle( background,
1640  cv::Point( vpMath::round( rectangle.getLeft() ),
1641  vpMath::round( rectangle.getBottom() ) ),
1642  cv::Point( vpMath::round( rectangle.getRight() ),
1643  vpMath::round( rectangle.getTop() ) ),
1644  cvcolor, filled);
1645 #endif
1646  }
1647  }
1648  }
1649  else
1650  {
1651  vpERROR_TRACE("OpenCV not initialized " ) ;
1653  "OpenCV not initialized")) ;
1654  }
1655 }
1656 
1657 
1658 
1674 bool
1676 {
1677  bool ret = false;
1679  flushDisplay() ;
1680  if (blocking){
1681  lbuttondown = false;
1682  mbuttondown = false;
1683  rbuttondown = false;
1684  }
1685  do {
1686  if (lbuttondown){
1687  ret = true ;
1688  lbuttondown = false;
1689  }
1690  if (mbuttondown){
1691  ret = true ;
1692  mbuttondown = false;
1693  }
1694  if (rbuttondown){
1695  ret = true ;
1696  rbuttondown = false;
1697  }
1698  if (blocking)
1699 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1700  cvWaitKey(10);
1701 #else
1702  cv::waitKey(10);
1703 #endif
1704  } while ( ret == false && blocking == true);
1705  }
1706  else {
1707  vpERROR_TRACE("OpenCV not initialized " ) ;
1709  "OpenCV not initialized")) ;
1710  }
1711  return ret;
1712 }
1713 
1731 bool
1733 {
1734  bool ret = false;
1735 
1737  flushDisplay() ;
1738 
1739  double u, v;
1740 
1741  if (blocking){
1742  lbuttondown = false;
1743  mbuttondown = false;
1744  rbuttondown = false;
1745  }
1746  do {
1747  if (lbuttondown){
1748  ret = true ;
1749  u = (unsigned int)x_lbuttondown;
1750  v = (unsigned int)y_lbuttondown;
1751  ip.set_u( u );
1752  ip.set_v( v );
1753  lbuttondown = false;
1754  }
1755  if (mbuttondown){
1756  ret = true ;
1757  u = (unsigned int)x_mbuttondown;
1758  v = (unsigned int)y_mbuttondown;
1759  ip.set_u( u );
1760  ip.set_v( v );
1761  mbuttondown = false;
1762  }
1763  if (rbuttondown){
1764  ret = true ;
1765  u = (unsigned int)x_rbuttondown;
1766  v = (unsigned int)y_rbuttondown;
1767  ip.set_u( u );
1768  ip.set_v( v );
1769  rbuttondown = false;
1770  }
1771  if (blocking)
1772 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1773  cvWaitKey(10);
1774 #else
1775  cv::waitKey(10);
1776 #endif
1777  } while ( ret == false && blocking == true);
1778  }
1779  else {
1780  vpERROR_TRACE("OpenCV not initialized " ) ;
1782  "OpenCV not initialized")) ;
1783  }
1784  return ret ;
1785 }
1786 
1787 
1807 bool
1810  bool blocking)
1811 {
1812  bool ret = false;
1813 
1815  //flushDisplay() ;
1816  double u, v;
1817  if (blocking){
1818  lbuttondown = false;
1819  mbuttondown = false;
1820  rbuttondown = false;
1821  }
1822  do {
1823  if (lbuttondown){
1824  ret = true ;
1825  u = (unsigned int)x_lbuttondown;
1826  v = (unsigned int)y_lbuttondown;
1827  ip.set_u( u );
1828  ip.set_v( v );
1829  button = vpMouseButton::button1;
1830  lbuttondown = false;
1831  }
1832  if (mbuttondown){
1833  ret = true ;
1834  u = (unsigned int)x_mbuttondown;
1835  v = (unsigned int)y_mbuttondown;
1836  ip.set_u( u );
1837  ip.set_v( v );
1838  button = vpMouseButton::button2;
1839  mbuttondown = false;
1840  }
1841  if (rbuttondown){
1842  ret = true ;
1843  u = (unsigned int)x_rbuttondown;
1844  v = (unsigned int)y_rbuttondown;
1845  ip.set_u( u );
1846  ip.set_v( v );
1847  button = vpMouseButton::button3;
1848  rbuttondown = false;
1849  }
1850  if (blocking)
1851 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1852  cvWaitKey(10);
1853 #else
1854  cv::waitKey(10);
1855 #endif
1856  } while ( ret == false && blocking == true);
1857  }
1858  else {
1859  vpERROR_TRACE("OpenCV not initialized " ) ;
1861  "OpenCV not initialized")) ;
1862  }
1863  return ret;
1864 }
1865 
1889 bool
1892  bool blocking)
1893 {
1894  bool ret = false;
1896  //flushDisplay() ;
1897  double u, v;
1898  if (blocking){
1899  lbuttonup = false;
1900  mbuttonup = false;
1901  rbuttonup = false;
1902  }
1903  do {
1904  if (lbuttonup){
1905  ret = true ;
1906  u = (unsigned int)x_lbuttonup;
1907  v = (unsigned int)y_lbuttonup;
1908  ip.set_u( u );
1909  ip.set_v( v );
1910  button = vpMouseButton::button1;
1911  lbuttonup = false;
1912  }
1913  if (mbuttonup){
1914  ret = true ;
1915  u = (unsigned int)x_mbuttonup;
1916  v = (unsigned int)y_mbuttonup;
1917  ip.set_u( u );
1918  ip.set_v( v );
1919  button = vpMouseButton::button2;
1920  mbuttonup = false;
1921  }
1922  if (rbuttonup){
1923  ret = true ;
1924  u = (unsigned int)x_rbuttonup;
1925  v = (unsigned int)y_rbuttonup;
1926  ip.set_u( u );
1927  ip.set_v( v );
1928  button = vpMouseButton::button3;
1929  rbuttonup = false;
1930  }
1931  if (blocking)
1932 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1933  cvWaitKey(10);
1934 #else
1935  cv::waitKey(10);
1936 #endif
1937  } while ( ret == false && blocking == true);
1938  }
1939  else {
1940  vpERROR_TRACE ( "OpenCV not initialized " ) ;
1942  "OpenCV not initialized" ) ) ;
1943  }
1944  return ret;
1945 }
1946 
1947 /*
1948  \brief gets the displayed image (including the overlay plane)
1949  and returns an RGBa image
1950 */
1952 {
1953  vpImageConvert::convert(background,I);
1954  // should certainly be optimized.
1955 }
1956 
1957 void vpDisplayOpenCV::on_mouse( int event, int x, int y, int /*flags*/, void* display )
1958 {
1959  vpDisplayOpenCV* disp = (vpDisplayOpenCV*)display;
1960  switch ( event )
1961  {
1962 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1963  case CV_EVENT_MOUSEMOVE:
1964 #else
1965  case cv::EVENT_MOUSEMOVE:
1966 #endif
1967  {
1968  disp->move = true;
1969  disp->x_move = x;
1970  disp->y_move = y;
1971  break;
1972  }
1973 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1974  case CV_EVENT_LBUTTONDOWN:
1975 #else
1976  case cv::EVENT_LBUTTONDOWN:
1977 #endif
1978  {
1979  disp->lbuttondown = true;
1980  disp->x_lbuttondown = x;
1981  disp->y_lbuttondown = y;
1982  break;
1983  }
1984 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1985  case CV_EVENT_MBUTTONDOWN:
1986 #else
1987  case cv::EVENT_MBUTTONDOWN:
1988 #endif
1989  {
1990  disp->mbuttondown = true;
1991  disp->x_mbuttondown = x;
1992  disp->y_mbuttondown = y;
1993  break;
1994  }
1995 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1996  case CV_EVENT_RBUTTONDOWN:
1997 #else
1998  case cv::EVENT_RBUTTONDOWN:
1999 #endif
2000  {
2001  disp->rbuttondown = true;
2002  disp->x_rbuttondown = x;
2003  disp->y_rbuttondown = y;
2004  break;
2005  }
2006 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2007  case CV_EVENT_LBUTTONUP:
2008 #else
2009  case cv::EVENT_LBUTTONUP:
2010 #endif
2011  {
2012  disp->lbuttonup = true;
2013  disp->x_lbuttonup = x;
2014  disp->y_lbuttonup = y;
2015  break;
2016  }
2017 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2018  case CV_EVENT_MBUTTONUP:
2019 #else
2020  case cv::EVENT_MBUTTONUP:
2021 #endif
2022  {
2023  disp->mbuttonup = true;
2024  disp->x_mbuttonup = x;
2025  disp->y_mbuttonup = y;
2026  break;
2027  }
2028 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2029  case CV_EVENT_RBUTTONUP:
2030 #else
2031  case cv::EVENT_RBUTTONUP:
2032 #endif
2033  {
2034  disp->rbuttonup = true;
2035  disp->x_rbuttonup = x;
2036  disp->y_rbuttonup = y;
2037  break;
2038  }
2039 
2040  default :
2041  break;
2042  }
2043 }
2044 
2061 bool
2063 {
2064  int key_pressed;
2065  int delay;
2067  flushDisplay() ;
2068  if (blocking)
2069  delay = 0;
2070  else
2071  delay = 10;
2072 
2073 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2074  key_pressed = cvWaitKey(delay);
2075 #else
2076  key_pressed = cv::waitKey(delay);
2077 #endif
2078 
2079  if (key_pressed == -1)
2080  return false;
2081  return true;
2082  }
2083  else {
2084  vpERROR_TRACE("OpenCV not initialized " ) ;
2086  "OpenCV not initialized")) ;
2087  }
2088  //return false; // Never reached after throw()
2089 }
2109 bool
2110 vpDisplayOpenCV::getKeyboardEvent(char *string, bool blocking)
2111 {
2112  int key_pressed;
2113  int delay;
2115  flushDisplay() ;
2116  if (blocking)
2117  delay = 0;
2118  else
2119  delay = 10;
2120 
2121 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
2122  key_pressed = cvWaitKey(delay);
2123 #else
2124  key_pressed = cv::waitKey(delay);
2125 #endif
2126  if (key_pressed == -1)
2127  return false;
2128  else {
2129  //std::cout << "Key pressed: \"" << key_pressed << "\"" << std::endl;
2130  sprintf(string, "%c", key_pressed);
2131  }
2132  return true;
2133  }
2134  else {
2135  vpERROR_TRACE("OpenCV not initialized " ) ;
2137  "OpenCV not initialized")) ;
2138  }
2139  //return false; // Never reached after throw()
2140 }
2141 
2154 bool
2156 {
2157  bool ret = false;
2158 
2160  //flushDisplay() ;
2161  double u, v;
2162  if (move){
2163  ret = true ;
2164  u = (unsigned int)x_move;
2165  v = (unsigned int)y_move;
2166  ip.set_u( u );
2167  ip.set_v( v );
2168  move = false;
2169  }
2170  }
2171 
2172  else {
2173  vpERROR_TRACE("OpenCV not initialized " ) ;
2175  "OpenCV not initialized")) ;
2176  }
2177  return ret;
2178 }
2179 
2190 bool
2192 {
2194  //vpTRACE("Not implemented yet");
2195  bool moved = getPointerMotionEvent(ip);
2196  if (!moved)
2197  {
2198  double u, v;
2199  u = (unsigned int)x_move;
2200  v = (unsigned int)y_move;
2201  ip.set_u( u );
2202  ip.set_v( v );
2203  }
2204  return false;
2205  }
2206  else {
2207  vpERROR_TRACE("OpenCV not initialized " ) ;
2209  "OpenCV not initialized")) ;
2210  }
2211  //return false; // Never reached after throw()
2212 }
2213 
2214 #endif
2215 
2216 /*
2217  * Local variables:
2218  * c-basic-offset: 2
2219  * End:
2220  */
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
vpDisplay * display
Definition: vpImage.h:121
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
double getTop() const
Definition: vpRect.h:180
double get_v() const
Definition: vpImagePoint.h:264
void clearDisplay(const vpColor &color=vpColor::white)
unsigned int width
Definition: vpDisplay.h:183
double get_i() const
Definition: vpImagePoint.h:195
unsigned int getWidth() const
Definition: vpImage.h:161
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void displayPoint(const vpImagePoint &ip, const vpColor &color)
#define vpERROR_TRACE
Definition: vpDebug.h:395
unsigned char B
Blue component.
Definition: vpRGBa.h:148
#define vpTRACE
Definition: vpDebug.h:418
static const vpColor black
Definition: vpColor.h:161
static const vpColor darkRed
Definition: vpColor.h:168
bool getClick(bool blocking=true)
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
double get_u() const
Definition: vpImagePoint.h:253
static const vpColor lightGray
Definition: vpColor.h:163
bool getPointerPosition(vpImagePoint &ip)
static const vpColor darkBlue
Definition: vpColor.h:174
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:178
unsigned char G
Green component.
Definition: vpRGBa.h:147
double getRight() const
Definition: vpRect.h:167
static const vpColor green
Definition: vpColor.h:170
static int round(const double x)
Definition: vpMath.h:228
double get_j() const
Definition: vpImagePoint.h:206
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static const vpColor lightRed
Definition: vpColor.h:166
virtual ~vpDisplayOpenCV()
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
std::string title_
Definition: vpDisplay.h:185
static void on_mouse(int event, int x, int y, int flags, void *param)
double getBottom() const
Definition: vpRect.h:103
vpColorIdentifier id
Definition: vpColor.h:156
void set_i(const double ii)
Definition: vpImagePoint.h:159
static const vpColor cyan
Definition: vpColor.h:176
static const vpColor lightGreen
Definition: vpColor.h:169
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
void set_u(const double u)
Definition: vpImagePoint.h:217
static double sqr(double x)
Definition: vpMath.h:106
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
unsigned int height
Definition: vpDisplay.h:184
The vpDisplayOpenCV allows to display image using the opencv library.
void set_v(const double v)
Definition: vpImagePoint.h:228
void displayImage(const vpImage< vpRGBa > &I)
void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
static const vpColor gray
Definition: vpColor.h:164
static const vpColor darkGray
Definition: vpColor.h:165
void set_j(const double jj)
Definition: vpImagePoint.h:170
bool getPointerMotionEvent(vpImagePoint &ip)
Error that can be emited by the vpDisplay class and its derivates.
static void createSubImage(const vpImage< Type > &I, unsigned int i_sub, unsigned int j_sub, unsigned int nrow_sub, unsigned int ncol_sub, vpImage< Type > &S)
Definition: vpImageTools.h:133
void getImage(vpImage< vpRGBa > &I)
get the window pixmap and put it in vpRGBa image
int windowXPosition
display position
Definition: vpDisplay.h:180
unsigned char R
Red component.
Definition: vpRGBa.h:146
void setFont(const char *font)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:85
static const vpColor darkGreen
Definition: vpColor.h:171
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
void setTitle(const char *title)
static const vpColor yellow
Definition: vpColor.h:175
static const vpColor lightBlue
Definition: vpColor.h:172
int windowYPosition
display position
Definition: vpDisplay.h:182
static const vpColor purple
Definition: vpColor.h:178
static const vpColor white
Definition: vpColor.h:162
double getLeft() const
Definition: vpRect.h:161
bool getKeyboardEvent(bool blocking=true)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
Definition: vpImagePoint.h:275
void setWindowPosition(int winx, int winy)
static const vpColor blue
Definition: vpColor.h:173