Visual Servoing Platform  version 3.1.0
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 modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulator based on Coin3d.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
45 #include <visp3/core/vpConfig.h>
46 
47 #ifdef VISP_HAVE_COIN3D_AND_GUI
48 #include <visp3/ar/vpSimulator.h>
49 #include <visp3/ar/vpViewer.h>
50 
51 #include <Inventor/events/SoKeyboardEvent.h>
52 #include <Inventor/nodes/SoEventCallback.h>
53 
54 #if defined(VISP_HAVE_SOWIN)
55 vpViewer::vpViewer(HWND parent, vpSimulator *_simu, vpViewerType type)
56  : viewerType(type), SoWinExaminerViewer(parent, (char *)NULL, false)
57 #elif defined(VISP_HAVE_SOQT)
58 vpViewer::vpViewer(QWidget *parent, vpSimulator *_simu, vpViewerType type)
59  : viewerType(type), SoQtExaminerViewer(parent, (char *)NULL, false)
60 #elif defined(VISP_HAVE_SOXT)
61 vpViewer::vpViewer(Widget parent, vpSimulator *_simu, vpViewerType type)
62  : viewerType(type), SoXtExaminerViewer(parent, (char *)NULL, false)
63 #endif
64 {
65  this->simu = _simu;
66  // Coin should not clear the pixel-buffer, so the background image
67  // is not removed.
68 
69  this->setClearBeforeRender(FALSE, TRUE);
70  // this->setAntialiasing(true, 2) ;
71  setAutoRedraw(false);
72 }
73 
75 
77 {
78 
79  {
80  const SbViewportRegion vp = this->getViewportRegion();
81  SbVec2s origin = vp.getViewportOriginPixels();
82  SbVec2s size = vp.getViewportSizePixels();
83  glViewport(origin[0], origin[1], size[0], size[1]);
84 
85  const SbColor col = this->getBackgroundColor();
86  glClearColor(col[0], col[1], col[2], 0.0f);
87  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88 
89  // this should be used only with the vpAR:vpSimulator
90  // to diplay an image background
91  if (simu->image_background != NULL) {
92  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
93  if (simu->typeImage == vpSimulator::grayImage)
94  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_LUMINANCE,
95  GL_UNSIGNED_BYTE, simu->image_background);
96  else
97  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_RGB,
98  GL_UNSIGNED_BYTE, simu->image_background);
99 
100  glEnable(GL_DEPTH_TEST);
101  glClear(GL_DEPTH_BUFFER_BIT); // clear the z-buffer
102  glClearDepth(100.0); // Profondeur du Z-Buf
103  }
104 // Render normal scenegraph.
105 #if defined(VISP_HAVE_SOWIN)
106  SoWinExaminerViewer::actualRedraw();
107 #elif defined(VISP_HAVE_SOQT)
108  SoQtExaminerViewer::actualRedraw();
109 #elif defined(VISP_HAVE_SOXT)
110  SoXtExaminerViewer::actualRedraw();
111 #endif
112  glSwapBuffers();
113  if (viewerType == vpViewer::internalView) {
114  simu->get = 0;
115  glReadPixels(0, 0, (GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_RGB,
116  GL_UNSIGNED_BYTE, simu->bufferView);
117  simu->get = 1;
118  }
119  }
120 }
121 
129 void
130 #if defined(VISP_HAVE_SOWIN) || defined(VISP_HAVE_SOQT)
131 vpViewer::resize(int x, int y, bool fixed)
132 #else
133 vpViewer::resize(int x, int y, bool /*fixed*/)
134 #endif
135 {
136  SbVec2s size(x, y);
137  setSize(size);
138  setGLSize(size);
139 
140 #if defined(VISP_HAVE_SOWIN)
141  HWND parent = getParentWidget();
142 
143  RECT rcClient, rcWindow;
144  POINT ptDiff;
145  GetClientRect(parent, &rcClient);
146  GetWindowRect(parent, &rcWindow);
147  ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
148  ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
149  MoveWindow(parent, rcWindow.left, rcWindow.top, x + ptDiff.x, y + ptDiff.y, TRUE);
150  if (fixed) {
151  DWORD dwStyle = GetWindowLong(parent, GWL_STYLE);
152  dwStyle &= ~(WS_SIZEBOX);
153  SetWindowLong(parent, GWL_STYLE, dwStyle);
154  }
155 #elif defined(VISP_HAVE_SOQT)
156  if (fixed) {
157  QWidget *parent = getParentWidget();
158  parent->setFixedSize(x, y);
159  }
160 #endif
161 }
162 
171 SbBool vpViewer::processSoEvent(const SoEvent *const event)
172 {
173  if (this->isViewing() && event->getTypeId() == SoKeyboardEvent::getClassTypeId()) {
174  SoKeyboardEvent *kbevent = (SoKeyboardEvent *)event;
175  switch (kbevent->getKey()) {
176  case SoKeyboardEvent::H:
177  if (kbevent->getState() == SoButtonEvent::DOWN) {
178  std::cout << "H : this help " << std::endl;
179  std::cout << "M : get and save the external camera location (matrix)" << std::endl;
180  std::cout << "V : get and save the external camera location (vector)" << std::endl;
181  std::cout << "M : load camera location (vector)" << std::endl;
182  std::cout << "P : get external camera location and set the internal one" << std::endl;
183  }
184  return TRUE;
185 
186  case SoKeyboardEvent::M:
187  if (kbevent->getState() == SoButtonEvent::DOWN) {
189  simu->getExternalCameraPosition(cMf);
190  std::ofstream f("cMf.dat");
191  cMf.save(f);
192  f.close();
193  }
194  return TRUE;
195  case SoKeyboardEvent::V:
196  if (kbevent->getState() == SoButtonEvent::DOWN) {
198  simu->getExternalCameraPosition(cMf);
199  vpPoseVector vcMf(cMf);
200  std::ofstream f("vcMf.dat");
201  vcMf.save(f);
202  f.close();
203  }
204  return TRUE;
205  case SoKeyboardEvent::L:
206  if (kbevent->getState() == SoButtonEvent::DOWN) {
207  vpPoseVector vcMf;
208  std::ifstream f("vcMf.dat");
209  vcMf.load(f);
210  f.close();
211  vpHomogeneousMatrix cMf(vcMf);
212  simu->setCameraPosition(cMf);
213  simu->moveInternalCamera(cMf);
214  }
215  return TRUE;
216  case SoKeyboardEvent::P:
217  if (kbevent->getState() == SoButtonEvent::DOWN) {
219  simu->getExternalCameraPosition(cMf);
220  vpPoseVector vcMf(cMf);
221  vcMf.print();
222  simu->setCameraPosition(cMf);
223  simu->moveInternalCamera(cMf);
224  }
225  return TRUE;
226  default:
227  break;
228  }
229  }
230 #if defined(VISP_HAVE_SOWIN)
231  return SoWinExaminerViewer::processSoEvent(event);
232 #elif defined(VISP_HAVE_SOQT)
233  return SoQtExaminerViewer::processSoEvent(event);
234 #elif defined(VISP_HAVE_SOXT)
235  return SoXtExaminerViewer::processSoEvent(event);
236 #endif
237 }
238 
239 #elif !defined(VISP_BUILD_SHARED_LIBS)
240 // Work arround to avoid warning: libvisp_ar.a(vpViewer.cpp.o) has no symbols
241 void dummy_vpViewer(){};
242 #endif
void load(std::ifstream &f)
virtual ~vpViewer()
Definition: vpViewer.cpp:74
void resize(int x, int y, bool fixed=false)
Definition: vpViewer.cpp:131
void save(std::ofstream &f) const
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:99
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:129
GLubyte * image_background
Definition: vpSimulator.h:131
Definition: vpContours.h:157
Viewer used by the simulator.
Definition: vpViewer.h:117
virtual void actualRedraw(void)
Definition: vpViewer.cpp:76
vpHomogeneousMatrix cMf
internal camera position
Definition: vpSimulator.h:239
unsigned int getInternalHeight() const
Definition: vpSimulator.h:180
unsigned char * bufferView
image of the internal view
Definition: vpSimulator.h:279
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
vpViewer(HWND parent, vpSimulator *simu, vpViewerType type)
Definition: vpViewer.cpp:55
void save(std::ofstream &f) const
unsigned int getInternalWidth() const
Definition: vpSimulator.h:174