Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpViewer.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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  : SoWinExaminerViewer(parent, (char *)NULL, false), viewerType(type), simu(_simu)
57 #elif defined(VISP_HAVE_SOQT)
58 vpViewer::vpViewer(QWidget *parent, vpSimulator *_simu, vpViewerType type)
59  : SoQtExaminerViewer(parent, (char *)NULL, false), viewerType(type), simu(_simu)
60 #elif defined(VISP_HAVE_SOXT)
61 vpViewer::vpViewer(Widget parent, vpSimulator *_simu, vpViewerType type)
62  : SoXtExaminerViewer(parent, (char *)NULL, false), viewerType(type), simu(_simu)
63 #endif
64 {
65  // Coin should not clear the pixel-buffer, so the background image
66  // is not removed.
67 
68  this->setClearBeforeRender(FALSE, TRUE);
69  // this->setAntialiasing(true, 2) ;
70  setAutoRedraw(false);
71 }
72 
74 
76 {
77 
78  {
79  const SbViewportRegion vp = this->getViewportRegion();
80  SbVec2s origin = vp.getViewportOriginPixels();
81  SbVec2s size = vp.getViewportSizePixels();
82  glViewport(origin[0], origin[1], size[0], size[1]);
83 
84  const SbColor col = this->getBackgroundColor();
85  glClearColor(col[0], col[1], col[2], 0.0f);
86  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87 
88  // this should be used only with the vpAR:vpSimulator
89  // to diplay an image background
90  if (simu->image_background != NULL) {
91  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
92  if (simu->typeImage == vpSimulator::grayImage)
93  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_LUMINANCE,
94  GL_UNSIGNED_BYTE, simu->image_background);
95  else
96  glDrawPixels((GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_RGB,
97  GL_UNSIGNED_BYTE, simu->image_background);
98 
99  glEnable(GL_DEPTH_TEST);
100  glClear(GL_DEPTH_BUFFER_BIT); // clear the z-buffer
101  glClearDepth(100.0); // Profondeur du Z-Buf
102  }
103 // Render normal scenegraph.
104 #if defined(VISP_HAVE_SOWIN)
105  SoWinExaminerViewer::actualRedraw();
106 #elif defined(VISP_HAVE_SOQT)
107  SoQtExaminerViewer::actualRedraw();
108 #elif defined(VISP_HAVE_SOXT)
109  SoXtExaminerViewer::actualRedraw();
110 #endif
111  glSwapBuffers();
112  if (viewerType == vpViewer::internalView) {
113  simu->get = 0;
114  glReadPixels(0, 0, (GLsizei)simu->getInternalWidth(), (GLsizei)simu->getInternalHeight(), (GLenum)GL_RGB,
115  GL_UNSIGNED_BYTE, simu->bufferView);
116  simu->get = 1;
117  }
118  }
119 }
120 
128 void
129 #if defined(VISP_HAVE_SOWIN) || defined(VISP_HAVE_SOQT)
130 vpViewer::resize(int x, int y, bool fixed)
131 #else
132 vpViewer::resize(int x, int y, bool /*fixed*/)
133 #endif
134 {
135  SbVec2s size(x, y);
136  setSize(size);
137  setGLSize(size);
138 
139 #if defined(VISP_HAVE_SOWIN)
140  HWND parent = getParentWidget();
141 
142  RECT rcClient, rcWindow;
143  POINT ptDiff;
144  GetClientRect(parent, &rcClient);
145  GetWindowRect(parent, &rcWindow);
146  ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
147  ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
148  MoveWindow(parent, rcWindow.left, rcWindow.top, x + ptDiff.x, y + ptDiff.y, TRUE);
149  if (fixed) {
150  DWORD dwStyle = GetWindowLong(parent, GWL_STYLE);
151  dwStyle &= ~(WS_SIZEBOX);
152  SetWindowLong(parent, GWL_STYLE, dwStyle);
153  }
154 #elif defined(VISP_HAVE_SOQT)
155  if (fixed) {
156  QWidget *parent = getParentWidget();
157  parent->setFixedSize(x, y);
158  }
159 #endif
160 }
161 
170 SbBool vpViewer::processSoEvent(const SoEvent *const event)
171 {
172  if (this->isViewing() && event->getTypeId() == SoKeyboardEvent::getClassTypeId()) {
173  SoKeyboardEvent *kbevent = (SoKeyboardEvent *)event;
174  switch (kbevent->getKey()) {
175  case SoKeyboardEvent::H:
176  if (kbevent->getState() == SoButtonEvent::DOWN) {
177  std::cout << "H : this help " << std::endl;
178  std::cout << "M : get and save the external camera location (matrix)" << std::endl;
179  std::cout << "V : get and save the external camera location (vector)" << std::endl;
180  std::cout << "M : load camera location (vector)" << std::endl;
181  std::cout << "P : get external camera location and set the internal one" << std::endl;
182  }
183  return TRUE;
184 
185  case SoKeyboardEvent::M:
186  if (kbevent->getState() == SoButtonEvent::DOWN) {
188  simu->getExternalCameraPosition(cMf);
189  std::ofstream f("cMf.dat");
190  cMf.save(f);
191  f.close();
192  }
193  return TRUE;
194  case SoKeyboardEvent::V:
195  if (kbevent->getState() == SoButtonEvent::DOWN) {
197  simu->getExternalCameraPosition(cMf);
198  vpPoseVector vcMf(cMf);
199  std::ofstream f("vcMf.dat");
200  vcMf.save(f);
201  f.close();
202  }
203  return TRUE;
204  case SoKeyboardEvent::L:
205  if (kbevent->getState() == SoButtonEvent::DOWN) {
206  vpPoseVector vcMf;
207  std::ifstream f("vcMf.dat");
208  vcMf.load(f);
209  f.close();
210  vpHomogeneousMatrix cMf(vcMf);
211  simu->setCameraPosition(cMf);
212  simu->moveInternalCamera(cMf);
213  }
214  return TRUE;
215  case SoKeyboardEvent::P:
216  if (kbevent->getState() == SoButtonEvent::DOWN) {
218  simu->getExternalCameraPosition(cMf);
219  vpPoseVector vcMf(cMf);
220  vcMf.print();
221  simu->setCameraPosition(cMf);
222  simu->moveInternalCamera(cMf);
223  }
224  return TRUE;
225  default:
226  break;
227  }
228  }
229 #if defined(VISP_HAVE_SOWIN)
230  return SoWinExaminerViewer::processSoEvent(event);
231 #elif defined(VISP_HAVE_SOQT)
232  return SoQtExaminerViewer::processSoEvent(event);
233 #elif defined(VISP_HAVE_SOXT)
234  return SoXtExaminerViewer::processSoEvent(event);
235 #endif
236 }
237 
238 #elif !defined(VISP_BUILD_SHARED_LIBS)
239 // Work arround to avoid warning: libvisp_ar.a(vpViewer.cpp.o) has no symbols
240 void dummy_vpViewer(){};
241 #endif
void load(std::ifstream &f)
virtual ~vpViewer()
Definition: vpViewer.cpp:73
void resize(int x, int y, bool fixed=false)
Definition: vpViewer.cpp:130
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
Viewer used by the simulator.
Definition: vpViewer.h:117
virtual void actualRedraw(void)
Definition: vpViewer.cpp:75
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:151
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