Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpViewer.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Simulator based on Coin3d.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
43 #include <visp3/core/vpConfig.h>
44 
45 #ifdef VISP_HAVE_COIN3D_AND_GUI
46 #include <visp3/ar/vpViewer.h>
47 #include <visp3/ar/vpSimulator.h>
48 
49 
50 #include <Inventor/events/SoKeyboardEvent.h>
51 #include <Inventor/nodes/SoEventCallback.h>
52 
53 
54 #if defined(VISP_HAVE_SOWIN)
55 vpViewer::vpViewer(HWND parent, vpSimulator *_simu, vpViewerType viewerType):
56  SoWinExaminerViewer(parent,(char *)NULL,false)
57 #elif defined(VISP_HAVE_SOQT)
58 vpViewer::vpViewer(QWidget * parent, vpSimulator *_simu, vpViewerType viewerType) :
59  SoQtExaminerViewer(parent,(char *)NULL,false)
60 #elif defined(VISP_HAVE_SOXT)
61 vpViewer::vpViewer(Widget parent, vpSimulator *_simu, vpViewerType viewerType):
62  SoXtExaminerViewer(parent,(char *)NULL,false)
63 #endif
64 {
65  this->simu = _simu ;
66  this->viewerType = viewerType;
67  // Coin should not clear the pixel-buffer, so the background image
68  // is not removed.
69 
70  this->setClearBeforeRender(FALSE, TRUE);
71  // this->setAntialiasing(true, 2) ;
72  setAutoRedraw(false);
73 }
74 
76 {
77 
78 }
79 
80 void
82 {
83 
84  {
85  const SbViewportRegion vp = this->getViewportRegion();
86  SbVec2s origin = vp.getViewportOriginPixels();
87  SbVec2s size = vp.getViewportSizePixels();
88  glViewport(origin[0], origin[1], size[0], size[1]);
89 
90  const SbColor col = this->getBackgroundColor();
91  glClearColor(col[0], col[1], col[2], 0.0f);
92  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93 
94  // this should be used only with the vpAR:vpSimulator
95  // to diplay an image background
96  if (simu->image_background != NULL)
97  {
98  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
99  if (simu->typeImage == vpSimulator::grayImage)
100  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
101  (GLenum)GL_LUMINANCE,
102  GL_UNSIGNED_BYTE,
103  simu->image_background );
104  else
105  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
106  (GLenum)GL_RGB,
107  GL_UNSIGNED_BYTE,
108  simu->image_background );
109 
110  glEnable(GL_DEPTH_TEST);
111  glClear(GL_DEPTH_BUFFER_BIT); // clear the z-buffer
112  glClearDepth(100.0); // Profondeur du Z-Buf
113  }
114  // Render normal scenegraph.
115 #if defined(VISP_HAVE_SOWIN)
116  SoWinExaminerViewer::actualRedraw();
117 #elif defined(VISP_HAVE_SOQT)
118  SoQtExaminerViewer::actualRedraw();
119 #elif defined(VISP_HAVE_SOXT)
120  SoXtExaminerViewer::actualRedraw();
121 #endif
122  glSwapBuffers() ;
123  if(viewerType == vpViewer::internalView){
124  simu->get = 0 ;
125  glReadPixels(0, 0, (GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(),
126  (GLenum)GL_RGB,
127  GL_UNSIGNED_BYTE,
128  simu->bufferView ) ;
129  simu->get =1 ;
130  }
131  }
132 
133 }
134 
142 void
143 #if defined(VISP_HAVE_SOWIN) || defined (VISP_HAVE_SOQT)
144 vpViewer::resize(int x, int y, bool fixed)
145 #else
146 vpViewer::resize(int x, int y, bool /*fixed*/)
147 #endif
148 {
149  SbVec2s size(x,y) ;
150  setSize(size);
151  setGLSize(size) ;
152 
153 #if defined(VISP_HAVE_SOWIN)
154  HWND parent = getParentWidget();
155 
156  RECT rcClient, rcWindow;
157  POINT ptDiff;
158  GetClientRect(parent, &rcClient);
159  GetWindowRect(parent, &rcWindow);
160  ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
161  ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
162  MoveWindow(parent,rcWindow.left, rcWindow.top,
163  x + ptDiff.x, y + ptDiff.y, TRUE);
164  if(fixed){
165  DWORD dwStyle = GetWindowLong(parent, GWL_STYLE);
166  dwStyle &= ~(WS_SIZEBOX);
167  SetWindowLong(parent, GWL_STYLE,dwStyle);
168  }
169 #elif defined(VISP_HAVE_SOQT)
170  if(fixed){
171  QWidget * parent = getParentWidget();
172  parent->setFixedSize(x, y);
173  }
174 #endif
175 }
176 
185 SbBool
186 vpViewer::processSoEvent(const SoEvent * const event)
187 {
188  if ( this->isViewing() &&
189  event->getTypeId() == SoKeyboardEvent::getClassTypeId() )
190  {
191  SoKeyboardEvent * kbevent = (SoKeyboardEvent *) event;
192  switch ( kbevent->getKey() ) {
193  case SoKeyboardEvent::H:
194  if ( kbevent->getState() == SoButtonEvent::DOWN )
195  {
196  std::cout << "H : this help "<<std::endl ;
197  std::cout << "M : get and save the external camera location (matrix)"<<std::endl;
198  std::cout << "V : get and save the external camera location (vector)"<<std::endl;
199  std::cout << "M : load camera location (vector)"<<std::endl;
200  std::cout << "P : get external camera location and set the internal one"<<std::endl;
201  }
202  return TRUE;
203 
204  case SoKeyboardEvent::M:
205  if ( kbevent->getState() == SoButtonEvent::DOWN )
206  {
208  simu->getExternalCameraPosition(cMf) ;
209  std::ofstream f("cMf.dat") ;
210  cMf.save(f) ;
211  f.close() ;
212  }
213  return TRUE;
214  case SoKeyboardEvent::V:
215  if ( kbevent->getState() == SoButtonEvent::DOWN )
216  {
218  simu->getExternalCameraPosition(cMf) ;
219  vpPoseVector vcMf(cMf) ;
220  std::ofstream f("vcMf.dat") ;
221  vcMf.save(f) ;
222  f.close() ;
223  }
224  return TRUE;
225  case SoKeyboardEvent::L:
226  if ( kbevent->getState() == SoButtonEvent::DOWN )
227  {
228  vpPoseVector vcMf;
229  std::ifstream f("vcMf.dat") ;
230  vcMf.load(f) ;
231  f.close() ;
232  vpHomogeneousMatrix cMf(vcMf) ;
233  simu->setCameraPosition(cMf) ;
234  simu->moveInternalCamera(cMf) ;
235  }
236  return TRUE;
237  case SoKeyboardEvent::P:
238  if ( kbevent->getState() == SoButtonEvent::DOWN )
239  {
241  simu->getExternalCameraPosition(cMf) ;
242  vpPoseVector vcMf(cMf) ;
243  vcMf.print() ;
244  simu->setCameraPosition(cMf) ;
245  simu->moveInternalCamera(cMf) ;
246  }
247  return TRUE;
248  default:
249  break;
250  }
251  }
252 #if defined(VISP_HAVE_SOWIN)
253  return SoWinExaminerViewer::processSoEvent(event);
254 #elif defined(VISP_HAVE_SOQT)
255  return SoQtExaminerViewer::processSoEvent(event);
256 #elif defined(VISP_HAVE_SOXT)
257  return SoXtExaminerViewer::processSoEvent(event);
258 #endif
259 
260 }
261 
262 #elif !defined(VISP_BUILD_SHARED_LIBS)
263 // Work arround to avoid warning: libvisp_ar.a(vpViewer.cpp.o) has no symbols
264 void dummy_vpViewer() {};
265 #endif
void load(std::ifstream &f)
virtual ~vpViewer()
Definition: vpViewer.cpp:75
void resize(int x, int y, bool fixed=false)
Definition: vpViewer.cpp:144
int get
Flag to protect the read and write of the framebuffer (between the simulator and the viewer)...
Definition: vpSimulator.h:285
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a simulator based on Coin3d (www.coin3d.org).
Definition: vpSimulator.h:98
unsigned int getInternalHeight() const
Definition: vpSimulator.h:182
void getExternalCameraPosition(vpHomogeneousMatrix &cMf)
get the external camera position
void print() const
void moveInternalCamera(vpHomogeneousMatrix &cMf)
modify the position of the camera in the scene graph
vpViewerType
Definition: vpViewer.h:128
vpImageType typeImage
Definition: vpSimulator.h:131
GLubyte * image_background
Definition: vpSimulator.h:133
Viewer used by the simulator.
Definition: vpViewer.h:117
virtual void actualRedraw(void)
Definition: vpViewer.cpp:81
void save(std::ofstream &f) const
vpHomogeneousMatrix cMf
internal camera position
Definition: vpSimulator.h:242
unsigned char * bufferView
image of the internal view
Definition: vpSimulator.h:282
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
unsigned int getInternalWidth() const
Definition: vpSimulator.h:176
vpViewer(HWND parent, vpSimulator *simu, vpViewerType viewerType)
Definition: vpViewer.cpp:55