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