Visual Servoing Platform  version 3.0.0
vpDisplayWin32.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Windows 32 display base class
32  *
33  * Authors:
34  * Bruno Renier
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
40 
41 #include <visp3/gui/vpDisplayWin32.h>
42 #include <visp3/core/vpDisplayException.h>
43 #include <string>
44 
45 const int vpDisplayWin32::MAX_INIT_DELAY = 5000;
46 
51 void vpCreateWindow(threadParam * param)
52 {
53  //char* title = param->title;
54  (param->vpDisp)->window.initWindow(param->title.c_str(), param->x, param->y,
55  param->w, param->h);
56  delete param;
57 }
58 
62 vpDisplayWin32::vpDisplayWin32(vpWin32Renderer * rend) :
63  iStatus(false), window(rend)
64 {
65 }
66 
67 
72 {
73  closeDisplay();
74 }
75 
76 
88  int x,
89  int y,
90  const char *title)
91 {
92  if ((I.getHeight() == 0) || (I.getWidth()==0))
93  {
94  vpERROR_TRACE("Image not initialized " ) ;
96  "Image not initialized")) ;
97  }
98 
99  window.renderer->setImg(I);
100 
101  init (I.getWidth(), I.getHeight(), x, y, title) ;
102  I.display = this ;
103 }
104 
105 
115  int x,
116  int y,
117  const char *title)
118 {
119  if ((I.getHeight() == 0) || (I.getWidth()==0))
120  {
121  vpERROR_TRACE("Image not initialized " ) ;
123  "Image not initialized")) ;
124  }
125 
126  window.renderer->setImg(I);
127 
128  init (I.getWidth(), I.getHeight(), x, y, title) ;
129  I.display = this ;
130 }
131 
132 
141 void vpDisplayWin32::init(unsigned int width, unsigned int height,
142  int x, int y,
143  const char *title)
144 {
145  if (title != NULL)
146  title_ = std::string(title);
147  else
148  title_ = std::string(" ");
149 
150  if (x != -1)
151  windowXPosition = x;
152  if (y != -1)
153  windowYPosition = y;
154 
155  //we prepare the window's thread creation
156  threadParam * param = new threadParam;
157  param->x = windowXPosition;
158  param->y = windowYPosition;
159  param->w = width;
160  param->h = height;
161  param->vpDisp = this;
162  param->title = this->title_;
163 
164  //creates the window in a separate thread
165  hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)vpCreateWindow,
166  param,0,&threadId);
167 
168  //the initialization worked
169  iStatus = (hThread != (HANDLE)NULL);
170 
172 }
173 
174 
180 {
181  //if the window is not initialized yet
182  if(!window.isInitialized())
183  {
184  //wait
185  if( WAIT_OBJECT_0 != WaitForSingleObject(window.semaInit,MAX_INIT_DELAY))
187  "Window not initialized")) ;
188  //problem : the window is not initialized
189  }
190 }
191 
192 
205 {
206  //waits if the window is not initialized
207  waitForInit();
208 
209  //sets the image to render
210  window.renderer->setImg(I);
211  //sends a message to the window
212  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
213 }
214 
215 
233 void vpDisplayWin32::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
234 {
235  //waits if the window is not initialized
236  waitForInit();
237 
238  //sets the image to render
239  window.renderer->setImgROI(I,iP,width,height);
240  //sends a message to the window
241  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
242 }
243 
244 
257 {
258  //wait if the window is not initialized
259  waitForInit();
260 
261  //sets the image to render
262  window.renderer->setImg(I);
263  //sends a message to the window
264  //PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
265 }
266 
284 void vpDisplayWin32::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
285 {
286  //waits if the window is not initialized
287  waitForInit();
288 
289  //sets the image to render
290  window.renderer->setImgROI(I,iP,width,height);
291  //sends a message to the window
292  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
293 }
294 
295 
311 bool vpDisplayWin32::getClick( bool blocking)
312 {
313  //wait if the window is not initialized
314  waitForInit();
315  bool ret = false;
316  //sends a message to the window
317 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
318 
319  //waits for a button to be pressed
320  if(blocking){
321  WaitForSingleObject(window.semaClick, 0);
322  WaitForSingleObject(window.semaClickUp, 0); //to erase previous events
323  WaitForSingleObject(window.semaClick, INFINITE);
324  ret = true;
325  }
326  else {
327  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
328  }
329 
330  return ret;
331 }
332 
333 
350 bool vpDisplayWin32::getClick(vpImagePoint &ip, bool blocking)
351 {
352  //wait if the window is not initialized
353  waitForInit();
354 
355  bool ret = false ;
356  double u, v;
357  //tells the window there has been a getclick demand
358 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
359  //waits for a click
360  if(blocking){
361  WaitForSingleObject(window.semaClick, 0);
362  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
363  WaitForSingleObject(window.semaClick, INFINITE);
364  ret = true;
365  }
366  else {
367  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
368  }
369 
370  u = window.clickX;
371  v = window.clickY;
372  ip.set_u( u );
373  ip.set_v( v );
374 
375  return ret;
376 }
377 
378 
399  bool blocking)
400 {
401  //wait if the window is not initialized
402  waitForInit();
403  bool ret = false;
404  double u, v;
405  //tells the window there has been a getclickup demand
406 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
407  //waits for a click
408  if(blocking){
409  WaitForSingleObject(window.semaClick, 0);
410  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
411  WaitForSingleObject(window.semaClick, INFINITE);
412  ret = true;
413  }
414  else
415  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
416 
417  u = window.clickX;
418  v = window.clickY;
419  ip.set_u( u );
420  ip.set_v( v );
421  button = window.clickButton;
422 
423  return ret;
424 }
425 
426 
451  bool blocking)
452 {
453  //wait if the window is not initialized
454  waitForInit();
455  bool ret = false;
456  double u, v;
457  //tells the window there has been a getclickup demand
458 // PostMessage(window.getHWnd(), vpWM_GETCLICKUP, 0,0);
459 
460  //waits for a click release
461  if(blocking){
462  WaitForSingleObject(window.semaClickUp, 0);
463  WaitForSingleObject(window.semaClick, 0);//to erase previous events
464  WaitForSingleObject(window.semaClickUp, INFINITE);
465  ret = true;
466  }
467  else
468  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClickUp, 0));
469 
470  u = window.clickXUp;
471  v = window.clickYUp;
472  ip.set_u( u );
473  ip.set_v( v );
474  button = window.clickButtonUp;
475 
476  return ret;
477 }
478 
494 bool vpDisplayWin32::getKeyboardEvent( bool blocking )
495 {
496  //wait if the window is not initialized
497  waitForInit();
498 
499  bool ret = false ;
500  //waits for a keyboard event
501  if(blocking){
502  WaitForSingleObject(window.semaKey, 0); // key down
503  WaitForSingleObject(window.semaKey, 0); // key up
504  WaitForSingleObject(window.semaKey, INFINITE);
505  ret = true;
506  }
507  else
508  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
509 
510  return ret;
511 }
531 bool vpDisplayWin32::getKeyboardEvent(char *string, bool blocking)
532 {
533  //wait if the window is not initialized
534  waitForInit();
535 
536  bool ret = false ;
537  //waits for a keyboard event
538  if(blocking){
539  WaitForSingleObject(window.semaKey, 0); // key down
540  WaitForSingleObject(window.semaKey, 0); // key up
541  WaitForSingleObject(window.semaKey, INFINITE);
542  ret = true;
543  }
544  else {
545  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
546  }
547  // printf("key: %ud\n", window.key);
548  sprintf(string, "%s", window.lpString);
549 
550  return ret;
551 }
562 bool
564 {
565  //wait if the window is not initialized
566  waitForInit();
567 
568  bool ret = false;
569 
570  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaMove, 0));
571  if (ret)
572  {
573  double u, v;
574  std::cout << "toto";
575  //tells the window there has been a getclick demand
576  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
577 
578  u = window.coordX;
579  v = window.coordY;
580  ip.set_u( u );
581  ip.set_v( v );
582  }
583 
584  return ret;
585 }
586 
597 bool
599 {
600  //wait if the window is not initialized
601  waitForInit();
602 
603  bool ret = true ;
604  double u, v;
605  //tells the window there has been a getclick demand
606  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
607 
608  u = window.coordX;
609  v = window.coordY;
610  ip.set_u( u );
611  ip.set_v( v );
612 
613  return ret;
614 }
615 
622 void vpDisplayWin32::setWindowPosition(int winx, int winy)
623 {
624  //wait if the window is not initialized
625  waitForInit();
626 
627  //cahange the window position only
628  SetWindowPos(window.hWnd,HWND_TOP, winx, winy, 0, 0,
629  SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOZORDER |SWP_NOSIZE);
630 
631 }
632 
633 
639 void vpDisplayWin32::setTitle(const char *windowtitle)
640 {
641  //wait if the window is not initialized
642  waitForInit();
643  SetWindowText(window.hWnd, windowtitle);
644 }
645 
646 
652 void vpDisplayWin32::setFont(const char * /* fontname */)
653 {
654  vpERROR_TRACE("Not yet implemented" ) ;
655 }
656 
657 
664 {
665  //waits if the window is not initialized
666  waitForInit();
667 
668  //sends a message to the window
669  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
670 }
671 
677 void vpDisplayWin32::flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
678 {
679  //waits if the window is not initialized
680  waitForInit();
681  /*
682  Under windows, flushing an ROI takes more time than
683  flushing the whole image.
684  Therefore, we update the maximum area even when asked to update a region.
685  */
686  WORD left = (WORD)iP.get_u();
687  WORD right = (WORD)(iP.get_u()+width-1);
688 
689  WORD top = (WORD)iP.get_v();
690  WORD bottom = (WORD)(iP.get_v()+height-1);
691 
692  //sends a message to the window
693  WPARAM wp = MAKEWPARAM(left, right);
694  LPARAM lp = MAKELPARAM(top, bottom);
695 
696  PostMessage(window.getHWnd(), vpWM_DISPLAY_ROI, wp,lp);
697 }
698 
699 
706  const vpColor &color )
707 {
708  //wait if the window is not initialized
709  waitForInit();
710  window.renderer->setPixel(ip, color);
711 }
712 
720  const vpImagePoint &ip2,
721  const vpColor &color,
722  unsigned int thickness )
723 {
724  //wait if the window is not initialized
725  waitForInit();
726  window.renderer->drawLine(ip1, ip2, color, thickness);
727 }
728 
729 
740  const vpImagePoint &ip2,
741  const vpColor &color,
742  unsigned int thickness )
743 {
744  //wait if the window is not initialized
745  waitForInit();
746  window.renderer->drawLine(ip1,ip2,color,thickness,PS_DASHDOT);
747 }
748 
763  unsigned int width, unsigned int height,
764  const vpColor &color, bool fill,
765  unsigned int thickness )
766 {
767  //wait if the window is not initialized
768  waitForInit();
769  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
770 }
771 
772 
786  const vpImagePoint &bottomRight,
787  const vpColor &color, bool fill,
788  unsigned int thickness )
789 {
790  //wait if the window is not initialized
791  waitForInit();
792  unsigned int width = static_cast<unsigned int>( bottomRight.get_j() - topLeft.get_j() );
793  unsigned int height = static_cast<unsigned int>(bottomRight.get_i() - topLeft.get_i() );
794  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
795 }
796 
809  const vpColor &color, bool fill,
810  unsigned int thickness )
811 {
812  //wait if the window is not initialized
813  waitForInit();
814  vpImagePoint topLeft;
815  topLeft.set_i(rectangle.getTop());
816  topLeft.set_j(rectangle.getLeft());
817  window.renderer->drawRect(topLeft,
818  static_cast<unsigned int>( rectangle.getWidth() ),
819  static_cast<unsigned int>( rectangle.getHeight() ),
820  color, fill, thickness);
821 }
822 
823 
834  unsigned int radius,
835  const vpColor &color,
836  bool fill,
837  unsigned int thickness )
838 {
839  //wait if the window is not initialized
840  waitForInit();
841  window.renderer->drawCircle(center,radius,color,fill,thickness);
842 }
843 
851  const char *text,
852  const vpColor &color )
853 {
854  //wait if the window is not initialized
855  waitForInit();
856  window.renderer->drawText(ip,text,color);
857 }
858 
867  unsigned int size,
868  const vpColor &color,
869  unsigned int thickness)
870 {
871  //wait if the window is not initialized
872  waitForInit();
873  window.renderer->drawCross(ip, size, color, thickness);
874 }
875 
876 
885  const vpImagePoint &ip2,
886  const vpColor &color,
887  unsigned int w,unsigned int h,
888  unsigned int thickness)
889 
890 {
891  //wait if the window is not initialized
892  waitForInit();
893  window.renderer->drawArrow(ip1, ip2, color, w, h, thickness);
894 }
895 
896 
902  //wait if the window is not initialized
903  waitForInit();
904  window.renderer->clear(color);
905 }
906 
907 
913 {
915  waitForInit();
916  PostMessage(window.getHWnd(), vpWM_CLOSEDISPLAY, 0,0);
917  //if the destructor is called for a reason different than a
918  //problem in the thread creation
919  if (iStatus) {
920  //waits for the thread to end
921  WaitForSingleObject(hThread, INFINITE);
922  CloseHandle(hThread);
923  }
924  displayHasBeenInitialized = false ;
925  window.initialized = false ;
926  }
927 }
928 
934 {
935  //wait if the window is not initialized
936  waitForInit();
937  window.renderer->getImage(I);
938 }
939 
940 #elif !defined(VISP_BUILD_SHARED_LIBS)
941 // Work arround to avoid warning: libvisp_core.a(vpDisplayWin32.cpp.o) has no symbols
942 void dummy_vpDisplayWin32() {};
943 #endif
vpDisplay * display
Definition: vpImage.h:117
static const int MAX_INIT_DELAY
Maximum delay for window initialization.
DWORD threadId
Id of the window's thread.
double getTop() const
Definition: vpRect.h:176
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
double get_v() const
Definition: vpImagePoint.h:259
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
unsigned int width
Definition: vpDisplay.h:179
double get_i() const
Definition: vpImagePoint.h:190
unsigned int getWidth() const
Definition: vpImage.h:161
virtual ~vpDisplayWin32()
bool getClick(bool blocking=true)
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
double get_u() const
Definition: vpImagePoint.h:248
void setFont(const char *fontname)
Set the font used to display text.
double getHeight() const
Definition: vpRect.h:151
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:174
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
bool iStatus
Initialization status.
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
double get_j() const
Definition: vpImagePoint.h:201
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
bool getKeyboardEvent(bool blocking=true)
vpDisplayWin32(vpWin32Renderer *rend=NULL)
vpWin32Window window
The window.
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
std::string title_
Definition: vpDisplay.h:181
void set_i(const double ii)
Definition: vpImagePoint.h:154
double getWidth() const
Definition: vpRect.h:195
void displayImage(const vpImage< vpRGBa > &I)
void set_u(const double u)
Definition: vpImagePoint.h:212
unsigned int height
Definition: vpDisplay.h:180
void set_v(const double v)
Definition: vpImagePoint.h:223
void setTitle(const char *windowtitle)
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
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 init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void displayPoint(const vpImagePoint &ip, const vpColor &color)
friend void vpCreateWindow(threadParam *param)
Function used to launch the window in a thread.
void set_j(const double jj)
Definition: vpImagePoint.h:165
bool getPointerMotionEvent(vpImagePoint &ip)
Error that can be emited by the vpDisplay class and its derivates.
HANDLE hThread
Handle of the window's thread.
int windowXPosition
display position
Definition: vpDisplay.h:176
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
flush the Win32 buffer It's necessary to use this function to see the results of any drawing ...
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void clearDisplay(const vpColor &color=vpColor::white)
void flushDisplay()
flush the Win32 buffer It's necessary to use this function to see the results of any drawing ...
int windowYPosition
display position
Definition: vpDisplay.h:178
void setWindowPosition(int winx, int winy)
double getLeft() const
Definition: vpRect.h:157
bool getPointerPosition(vpImagePoint &ip)
void getImage(vpImage< vpRGBa > &I)