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