Debian Codegraph @

src/Gui/Quarter/SoQTQuarterAdaptor.cpp

raw
1 /*
2 * <one line to give the library's name and an idea of what it does.>
3 * Copyright (C) 2014 Stefan Tröger <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include <FCConfig.h>
22
23 #include <numbers>
24
25 #include <Base/Console.h>
26 #include <Inventor/SbLine.h>
27 #include <Inventor/SbPlane.h>
28 #include <Inventor/SoEventManager.h>
29 #include <Inventor/SoPickedPoint.h>
30 #include <Inventor/actions/SoHandleEventAction.h>
31 #include <Inventor/actions/SoRayPickAction.h>
32 #include <Inventor/actions/SoSearchAction.h>
33 #include <Inventor/events/SoEvents.h>
34 #include <Inventor/nodes/SoLocateHighlight.h>
35 #include <Inventor/nodes/SoOrthographicCamera.h>
36 #include <Inventor/nodes/SoPerspectiveCamera.h>
37 #include <Inventor/nodes/SoSeparator.h>
38
39 # ifdef FC_OS_WIN32
40 # include <windows.h>
41 # endif
42 # ifdef FC_OS_MACOSX
43 # include <OpenGL/gl.h>
44 # else
45 # include <GL/gl.h>
46 # include <GL/glext.h>
47 # include <GL/glu.h>
48 # endif
49
50 #include "SoQTQuarterAdaptor.h"
51
52 #ifdef BUILD_TRACY_FRAME_PROFILER
53 #include <tracy/Tracy.hpp>
54 #endif
55
56 // NOLINTBEGIN
57 // clang-format off
58 static unsigned char fps2dfont[][12] = {
59 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, //
60 { 0, 0, 12, 12, 0, 8, 12, 12, 12, 12, 12, 0 }, // !
61 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 20 }, // \"
62 { 0, 0, 18, 18, 18, 63, 18, 18, 63, 18, 18, 0 }, // #
63 { 0, 8, 28, 42, 10, 10, 12, 24, 40, 42, 28, 8 }, // $
64 { 0, 0, 6, 73, 41, 22, 8, 52, 74, 73, 48, 0 }, // %
65 { 0, 12, 18, 18, 12, 25, 37, 34, 34, 29, 0, 0 }, // &
66 { 12, 12, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // '
67 { 0, 6, 8, 8, 16, 16, 16, 16, 16, 8, 8, 6 }, // (
68 { 0, 48, 8, 8, 4, 4, 4, 4, 4, 8, 8, 48 }, //)
69 { 0, 0, 0, 0, 0, 0, 8, 42, 20, 42, 8, 0 }, // *
70 { 0, 0, 0, 8, 8, 8,127, 8, 8, 8, 0, 0 }, // +
71 { 0, 24, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0 }, // ,
72 { 0, 0, 0, 0, 0, 0,127, 0, 0, 0, 0, 0 }, // -
73 { 0, 0, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0 }, // .
74 { 0, 32, 32, 16, 16, 8, 8, 8, 4, 4, 2, 2 }, // /
75 { 0, 0, 28, 34, 34, 34, 34, 34, 34, 34, 28, 0 }, // 0
76 { 0, 0, 8, 8, 8, 8, 8, 8, 40, 24, 8, 0 }, // 1
77 { 0, 0, 62, 32, 16, 8, 4, 2, 2, 34, 28, 0 }, // 2
78 { 0, 0, 28, 34, 2, 2, 12, 2, 2, 34, 28, 0 }, // 3
79 { 0, 0, 4, 4, 4,126, 68, 36, 20, 12, 4, 0 }, // 4
80 { 0, 0, 28, 34, 2, 2, 2, 60, 32, 32, 62, 0 }, // 5
81 { 0, 0, 28, 34, 34, 34, 60, 32, 32, 34, 28, 0 }, // 6
82 { 0, 0, 16, 16, 16, 8, 8, 4, 2, 2, 62, 0 }, // 7
83 { 0, 0, 28, 34, 34, 34, 28, 34, 34, 34, 28, 0 }, // 8
84 { 0, 0, 28, 34, 2, 2, 30, 34, 34, 34, 28, 0 }, // 9
85 { 0, 0, 24, 24, 0, 0, 0, 24, 24, 0, 0, 0 }, // :
86 { 0, 48, 24, 24, 0, 0, 0, 24, 24, 0, 0, 0 }, // ;
87 { 0, 0, 0, 2, 4, 8, 16, 8, 4, 2, 0, 0 }, // <
88 { 0, 0, 0, 0, 0,127, 0,127, 0, 0, 0, 0 }, // =
89 { 0, 0, 0, 16, 8, 4, 2, 4, 8, 16, 0, 0 }, // >
90 { 0, 0, 16, 16, 0, 16, 28, 2, 2, 2, 60, 0 }, // ?
91 { 0, 0, 28, 32, 73, 86, 82, 82, 78, 34, 28, 0 }, // @
92 { 0, 0, 33, 33, 33, 63, 18, 18, 18, 12, 12, 0 }, // A
93 { 0, 0, 60, 34, 34, 34, 60, 34, 34, 34, 60, 0 }, // B
94 { 0, 0, 14, 16, 32, 32, 32, 32, 32, 18, 14, 0 }, // C
95 { 0, 0, 56, 36, 34, 34, 34, 34, 34, 36, 56, 0 }, // D
96 { 0, 0, 62, 32, 32, 32, 60, 32, 32, 32, 62, 0 }, // E
97 { 0, 0, 16, 16, 16, 16, 30, 16, 16, 16, 30, 0 }, // F
98 { 0, 0, 14, 18, 34, 34, 32, 32, 32, 18, 14, 0 }, // G
99 { 0, 0, 34, 34, 34, 34, 62, 34, 34, 34, 34, 0 }, // H
100 { 0, 0, 62, 8, 8, 8, 8, 8, 8, 8, 62, 0 }, // I
101 { 0, 0,112, 8, 8, 8, 8, 8, 8, 8, 62, 0 }, // J
102 { 0, 0, 33, 33, 34, 36, 56, 40, 36, 34, 33, 0 }, // K
103 { 0, 0, 30, 16, 16, 16, 16, 16, 16, 16, 16, 0 }, // L
104 { 0, 0, 33, 33, 33, 45, 45, 45, 51, 51, 33, 0 }, // M
105 { 0, 0, 34, 34, 38, 38, 42, 42, 50, 50, 34, 0 }, // N
106 { 0, 0, 12, 18, 33, 33, 33, 33, 33, 18, 12, 0 }, // O
107 { 0, 0, 32, 32, 32, 60, 34, 34, 34, 34, 60, 0 }, // P
108 { 3, 6, 12, 18, 33, 33, 33, 33, 33, 18, 12, 0 }, // Q
109 { 0, 0, 34, 34, 34, 36, 60, 34, 34, 34, 60, 0 }, // R
110 { 0, 0, 60, 2, 2, 6, 28, 48, 32, 32, 30, 0 }, // S
111 { 0, 0, 8, 8, 8, 8, 8, 8, 8, 8,127, 0 }, // T
112 { 0, 0, 28, 34, 34, 34, 34, 34, 34, 34, 34, 0 }, // U
113 { 0, 0, 12, 12, 18, 18, 18, 33, 33, 33, 33, 0 }, // V
114 { 0, 0, 34, 34, 34, 54, 85, 73, 73, 73, 65, 0 }, // W
115 { 0, 0, 34, 34, 20, 20, 8, 20, 20, 34, 34, 0 }, // X
116 { 0, 0, 8, 8, 8, 8, 20, 20, 34, 34, 34, 0 }, // Y
117 { 0, 0, 62, 32, 16, 16, 8, 4, 4, 2, 62, 0 }, // Z
118 { 0, 14, 8, 8, 8, 8, 8, 8, 8, 8, 8, 14 }, // [
119 { 0, 2, 2, 4, 4, 8, 8, 8, 16, 16, 32, 32 }, // [backslash]
120 { 0, 56, 8, 8, 8, 8, 8, 8, 8, 8, 8, 56 }, // ]
121 { 0, 0, 0, 0, 0, 34, 34, 20, 20, 8, 8, 0 }, // ^
122 { 0,127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // _
123 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 24, 12 }, // `
124 { 0, 0, 29, 34, 34, 30, 2, 34, 28, 0, 0, 0 }, // a
125 { 0, 0, 60, 34, 34, 34, 34, 50, 44, 32, 32, 32 }, // b
126 { 0, 0, 14, 16, 32, 32, 32, 16, 14, 0, 0, 0 }, // c
127 { 0, 0, 26, 38, 34, 34, 34, 34, 30, 2, 2, 2 }, // d
128 { 0, 0, 28, 34, 32, 62, 34, 34, 28, 0, 0, 0 }, // e
129 { 0, 0, 16, 16, 16, 16, 16, 16, 62, 16, 16, 14 }, // f
130 { 28, 2, 2, 26, 38, 34, 34, 34, 30, 0, 0, 0 }, // g
131 { 0, 0, 34, 34, 34, 34, 34, 50, 44, 32, 32, 32 }, // h
132 { 0, 0, 8, 8, 8, 8, 8, 8, 56, 0, 8, 8 }, // i
133 { 56, 4, 4, 4, 4, 4, 4, 4, 60, 0, 4, 4 }, // j
134 { 0, 0, 33, 34, 36, 56, 40, 36, 34, 32, 32, 32 }, // k
135 { 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 56 }, // l
136 { 0, 0, 73, 73, 73, 73, 73,109, 82, 0, 0, 0 }, // m
137 { 0, 0, 34, 34, 34, 34, 34, 50, 44, 0, 0, 0 }, // n
138 { 0, 0, 28, 34, 34, 34, 34, 34, 28, 0, 0, 0 }, // o
139 { 32, 32, 60, 34, 34, 34, 34, 50, 44, 0, 0, 0 }, // p
140 { 2, 2, 26, 38, 34, 34, 34, 34, 30, 0, 0, 0 }, // q
141 { 0, 0, 16, 16, 16, 16, 16, 24, 22, 0, 0, 0 }, // r
142 { 0, 0, 60, 2, 2, 28, 32, 32, 30, 0, 0, 0 }, // s
143 { 0, 0, 14, 16, 16, 16, 16, 16, 62, 16, 16, 0 }, // t
144 { 0, 0, 26, 38, 34, 34, 34, 34, 34, 0, 0, 0 }, // u
145 { 0, 0, 8, 8, 20, 20, 34, 34, 34, 0, 0, 0 }, // v
146 { 0, 0, 34, 34, 34, 85, 73, 73, 65, 0, 0, 0 }, // w
147 { 0, 0, 34, 34, 20, 8, 20, 34, 34, 0, 0, 0 }, // x
148 { 48, 16, 8, 8, 20, 20, 34, 34, 34, 0, 0, 0 }, // y
149 { 0, 0, 62, 32, 16, 8, 4, 2, 62, 0, 0, 0 }, // z
150 { 0, 6, 8, 8, 8, 4, 24, 4, 8, 8, 8, 6 }, // {
151 { 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, // |
152 { 0, 48, 8, 8, 8, 16, 12, 16, 8, 8, 8, 48 }, // }
153 { 0, 0, 0, 0, 0, 0, 78, 57, 0, 0, 0, 0 } // ~
154 };
155 // clang-format on
156 // NOLINTEND
157
158 constexpr const int defaultSize = 100;
159
160 // NOLINTBEGIN(readability-implicit-bool-conversion)
161 SIM::Coin3D::Quarter::SoQTQuarterAdaptor::SoQTQuarterAdaptor(QWidget* parent,
162 const QOpenGLWidget* sharewidget,
163 Qt::WindowFlags flags)
164 : QuarterWidget(parent, sharewidget, flags)
165 , matrixaction(SbViewportRegion(defaultSize, defaultSize))
166 {
167 init();
168 }
169
170 SIM::Coin3D::Quarter::SoQTQuarterAdaptor::SoQTQuarterAdaptor(const QSurfaceFormat& format,
171 QWidget* parent,
172 const QOpenGLWidget* shareWidget,
173 Qt::WindowFlags flags)
174 : QuarterWidget(format, parent, shareWidget, flags)
175 , matrixaction(SbViewportRegion(defaultSize, defaultSize))
176 {
177 init();
178 }
179
180 SIM::Coin3D::Quarter::SoQTQuarterAdaptor::SoQTQuarterAdaptor(QOpenGLContext* context,
181 QWidget* parent,
182 const QOpenGLWidget* sharewidget,
183 Qt::WindowFlags flags)
184 : QuarterWidget(context, parent, sharewidget, flags)
185 , matrixaction(SbViewportRegion(defaultSize, defaultSize))
186 {
187 init();
188 }
189
190 SIM::Coin3D::Quarter::SoQTQuarterAdaptor::~SoQTQuarterAdaptor()
191 {
192 delete m_seeksensor;
193 }
194
195 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::init()
196 {
197 // NOLINTBEGIN
198 m_interactionnesting = 0;
199 m_seekdistance = 50.0F;
200 m_seekdistanceabs = false;
201 m_seekperiod = 2.0F;
202 m_inseekmode = false;
203 m_storedcamera = nullptr;
204 m_viewingflag = false;
205 pickRadius = 5.0;
206
207 m_seeksensor = new SoTimerSensor(SoQTQuarterAdaptor::seeksensorCB, (void*)this);
208 getSoEventManager()->setNavigationState(SoEventManager::NO_NAVIGATION);
209
210 resetFrameCounter();
211 // NOLINTEND
212 }
213
214
215 QWidget* SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getWidget()
216 {
217 //we keep the function from SoQt as we want to introduce the QGraphicsView and then the GLWidget
218 //is separated from the Widget used in layouts again
219 return this;
220 }
221
222 QWidget* SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getGLWidget()
223 {
224 return viewport();
225 }
226
227 QWidget* SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getWidget() const
228 {
229 //we keep the function from SoQt as we want to introduce the QGraphicsView and then the GLWidget
230 //is separated from the Widget used in layouts again
231 return const_cast<SoQTQuarterAdaptor*>(this); // NOLINT
232 }
233
234 QWidget* SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getGLWidget() const
235 {
236 return viewport();
237 }
238
239 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setCameraType(SoType type)
240 {
241 SoCamera* cam = getSoRenderManager()->getCamera();
242 if (cam && !cam->isOfType(SoPerspectiveCamera::getClassTypeId()) &&
243 !cam->isOfType(SoOrthographicCamera::getClassTypeId())) {
244 Base::Console().warning("Quarter::setCameraType",
245 "Only SoPerspectiveCamera and SoOrthographicCamera is supported.");
246 return;
247 }
248
249
250 SoType perspectivetype = SoPerspectiveCamera::getClassTypeId();
251 SbBool oldisperspective = cam ? cam->getTypeId().isDerivedFrom(perspectivetype) : false;
252 SbBool newisperspective = type.isDerivedFrom(perspectivetype);
253
254 // Same old, same old..
255 if (oldisperspective == newisperspective) {
256 return;
257 }
258
259 SoCamera* currentcam = getSoRenderManager()->getCamera();
260 SoCamera* newcamera = static_cast<SoCamera*>(type.createInstance()); // NOLINT
261
262 // Transfer and convert values from one camera type to the other.
263 if(newisperspective) {
264 convertOrtho2Perspective(dynamic_cast<SoOrthographicCamera*>(currentcam),
265 dynamic_cast<SoPerspectiveCamera*>(newcamera));
266 }
267 else {
268 convertPerspective2Ortho(dynamic_cast<SoPerspectiveCamera*>(currentcam),
269 dynamic_cast<SoOrthographicCamera*>(newcamera));
270 }
271
272 getSoRenderManager()->setCamera(newcamera);
273 getSoEventManager()->setCamera(newcamera);
274
275 //if the superscene has a camera we need to replace it too
276 auto superscene = dynamic_cast<SoSeparator*>(getSoRenderManager()->getSceneGraph());
277 SoSearchAction sa;
278 sa.setInterest(SoSearchAction::FIRST);
279 sa.setType(SoCamera::getClassTypeId());
280 sa.apply(superscene);
281
282 if (sa.getPath()) {
283 SoNode* node = sa.getPath()->getTail();
284 SoGroup* parent = static_cast<SoGroup*>(sa.getPath()->getNodeFromTail(1)); // NOLINT
285
286 if (node && node->isOfType(SoCamera::getClassTypeId())) {
287 parent->replaceChild(node, newcamera);
288 }
289 }
290 }
291
292 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::convertOrtho2Perspective(const SoOrthographicCamera* in,
293 SoPerspectiveCamera* out)
294 {
295 if (!in || !out) {
296 Base::Console().log("Quarter::convertOrtho2Perspective",
297 "Cannot convert camera settings due to wrong input.");
298 return;
299 }
300 out->aspectRatio.setValue(in->aspectRatio.getValue());
301 out->focalDistance.setValue(in->focalDistance.getValue());
302 out->orientation.setValue(in->orientation.getValue());
303 out->position.setValue(in->position.getValue());
304 out->viewportMapping.setValue(in->viewportMapping.getValue());
305
306 SbRotation camrot = in->orientation.getValue();
307
308 float focaldist = float(in->height.getValue() / (2.0*tan(std::numbers::pi / 8.0))); // NOLINT
309
310 SbVec3f offset(0,0,focaldist-in->focalDistance.getValue());
311
312 camrot.multVec(offset,offset);
313 out->position.setValue(offset+in->position.getValue());
314
315 out->focalDistance.setValue(focaldist);
316
317 // 45° is the default value of this field in SoPerspectiveCamera.
318 out->heightAngle = (float)(std::numbers::pi / 4.0); // NOLINT
319 }
320
321 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::convertPerspective2Ortho(const SoPerspectiveCamera* in,
322 SoOrthographicCamera* out)
323 {
324 out->aspectRatio.setValue(in->aspectRatio.getValue());
325 out->focalDistance.setValue(in->focalDistance.getValue());
326 out->orientation.setValue(in->orientation.getValue());
327 out->position.setValue(in->position.getValue());
328 out->viewportMapping.setValue(in->viewportMapping.getValue());
329
330 float focaldist = in->focalDistance.getValue();
331
332 out->height = 2.0F * focaldist * (float)tan(in->heightAngle.getValue() / 2.0); // NOLINT
333 }
334
335 SoCamera* SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getCamera() const
336 {
337 return getSoRenderManager()->getCamera();
338 }
339
340 const SbViewportRegion & SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getViewportRegion() const
341 {
342 return getSoRenderManager()->getViewportRegion();
343 }
344
345 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setViewing(bool enable)
346 {
347 m_viewingflag = enable;
348
349 // Turn off the selection indicators when we go back from picking
350 // mode into viewing mode.
351 if (m_viewingflag) {
352 SoGLRenderAction* action = getSoRenderManager()->getGLRenderAction();
353
354 if (action) {
355 SoLocateHighlight::turnOffCurrentHighlight(action);
356 }
357 }
358 }
359
360 bool SIM::Coin3D::Quarter::SoQTQuarterAdaptor::isViewing() const
361 {
362 return m_viewingflag;
363 }
364
365 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::interactiveCountInc()
366 {
367 // Catch problems with missing interactiveCountDec() calls.
368 assert(m_interactionnesting < 100);
369
370 if (++m_interactionnesting == 1) {
371 m_interactionStartCallback.invokeCallbacks(this);
372 }
373 }
374
375 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::interactiveCountDec()
376 {
377 if (--m_interactionnesting <= 0) {
378 m_interactionEndCallback.invokeCallbacks(this);
379 m_interactionnesting = 0;
380 }
381 }
382
383 int SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getInteractiveCount() const
384 {
385 return m_interactionnesting;
386 }
387
388 // clang-format off
389 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::addStartCallback(SIM::Coin3D::Quarter::SoQTQuarterAdaptorCB* func, void* data)
390 {
391 m_interactionStartCallback.addCallback((SoCallbackListCB*)func, data); // NOLINT
392 }
393
394 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::removeStartCallback(SIM::Coin3D::Quarter::SoQTQuarterAdaptorCB* func, void* data)
395 {
396 m_interactionStartCallback.removeCallback((SoCallbackListCB*)func, data); // NOLINT
397 }
398
399 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::addFinishCallback(SIM::Coin3D::Quarter::SoQTQuarterAdaptorCB* func, void* data)
400 {
401 m_interactionEndCallback.addCallback((SoCallbackListCB*)func, data); // NOLINT
402 }
403
404 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::removeFinishCallback(SIM::Coin3D::Quarter::SoQTQuarterAdaptorCB* func, void* data)
405 {
406 m_interactionEndCallback.removeCallback((SoCallbackListCB*)func, data); // NOLINT
407 }
408 // clang-format on
409
410 float SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getSeekDistance() const
411 {
412 return m_seekdistance;
413 }
414
415 float SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getSeekTime() const
416 {
417 return m_seekperiod;
418 }
419
420 bool SIM::Coin3D::Quarter::SoQTQuarterAdaptor::isSeekMode() const
421 {
422 return m_inseekmode;
423 }
424
425 bool SIM::Coin3D::Quarter::SoQTQuarterAdaptor::isSeekValuePercentage() const
426 {
427 return !m_seekdistanceabs;
428 }
429
430 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setPickRadius(float pickRadius)
431 {
432 this->pickRadius = pickRadius;
433 SoEventManager* evm = this->getSoEventManager();
434 if (evm){
435 SoHandleEventAction* hea = evm->getHandleEventAction();
436 if (hea){
437 hea->setPickRadius(pickRadius);
438 }
439 }
440 }
441
442 bool SIM::Coin3D::Quarter::SoQTQuarterAdaptor::seekToPoint(const SbVec2s& screenpos)
443 {
444
445 SoRayPickAction rpaction(getSoRenderManager()->getViewportRegion());
446 rpaction.setPoint(screenpos);
447 rpaction.setRadius(pickRadius);
448 rpaction.apply(getSoRenderManager()->getSceneGraph());
449
450 SoPickedPoint* picked = rpaction.getPickedPoint();
451
452 if (!picked) {
453 this->interactiveCountInc(); // decremented in setSeekMode(false)
454 this->setSeekMode(false);
455 return false;
456 }
457
458 SbVec3f hitpoint;
459 hitpoint = picked->getPoint();
460
461 this->seekToPoint(hitpoint);
462 return true;
463 }
464
465 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::seekToPoint(const SbVec3f& scenepos)
466 {
467 SbVec3f hitpoint(scenepos);
468
469 m_camerastartposition = getSoRenderManager()->getCamera()->position.getValue();
470 m_camerastartorient = getSoRenderManager()->getCamera()->orientation.getValue();
471
472 // move point to the camera coordinate system, consider
473 // transformations before camera in the scene graph
474 SbMatrix cameramatrix;
475 SbMatrix camerainverse;
476 getCameraCoordinateSystem(getSoRenderManager()->getCamera(),
477 getSceneGraph(),
478 cameramatrix,
479 camerainverse);
480 camerainverse.multVecMatrix(hitpoint, hitpoint);
481
482 float fd = m_seekdistance;
483
484 if(!m_seekdistanceabs) {
485 fd *= (hitpoint - getSoRenderManager()->getCamera()->position.getValue()).length()/100.0F;
486 }
487
488 getSoRenderManager()->getCamera()->focalDistance = fd;
489
490 SbVec3f dir = hitpoint - m_camerastartposition;
491 dir.normalize();
492
493 // find a rotation that rotates current camera direction into new
494 // camera direction.
495 SbVec3f olddir;
496 getSoRenderManager()->getCamera()->orientation.getValue().multVec(SbVec3f(0, 0, -1), olddir);
497 SbRotation diffrot(olddir, dir);
498 m_cameraendposition = hitpoint - fd * dir;
499 m_cameraendorient = getSoRenderManager()->getCamera()->orientation.getValue() * diffrot;
500
501 if(m_seeksensor->isScheduled()) {
502 m_seeksensor->unschedule();
503 interactiveCountDec();
504 }
505
506 m_seeksensor->setBaseTime(SbTime::getTimeOfDay());
507 m_seeksensor->schedule();
508 interactiveCountInc();
509 }
510
511 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setSeekDistance(const float distance)
512 {
513 m_seekdistance = distance;
514 }
515
516 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setSeekMode(bool enable)
517 {
518 if(!enable && m_seeksensor->isScheduled()) {
519 m_seeksensor->unschedule();
520 interactiveCountDec();
521 }
522
523 m_inseekmode = enable;
524 }
525
526 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setSeekTime(const float seconds)
527 {
528 m_seekperiod = seconds;
529 }
530
531 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::setSeekValueAsPercentage(bool on)
532 {
533 m_seekdistanceabs = !on;
534 }
535
536 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::getCameraCoordinateSystem(SoCamera* camera,
537 SoNode* root,
538 SbMatrix& matrix,
539 SbMatrix& inverse)
540 {
541 searchaction.reset();
542 searchaction.setSearchingAll(true);
543 searchaction.setInterest(SoSearchAction::FIRST);
544 searchaction.setNode(camera);
545 searchaction.apply(root);
546
547 matrix = inverse = SbMatrix::identity();
548
549 if(searchaction.getPath()) {
550 matrixaction.apply(searchaction.getPath());
551 matrix = matrixaction.getMatrix();
552 inverse = matrixaction.getInverse();
553 }
554
555 searchaction.reset();
556 }
557
558 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::seeksensorCB(void* data, SoSensor* sensor)
559 {
560 SoQTQuarterAdaptor* thisp = static_cast<SoQTQuarterAdaptor*>(data); // NOLINT
561 SbTime currenttime = SbTime::getTimeOfDay();
562
563 SoTimerSensor* timer = static_cast<SoTimerSensor*>(sensor); // NOLINT
564
565 float par = float((currenttime - timer->getBaseTime()).getValue()) / thisp->m_seekperiod;
566
567 if ((par > 1.0F) || (par + timer->getInterval().getValue() > 1.0F)) {
568 par = 1.0F;
569 }
570
571 bool end = (par == 1.0F);
572
573 par = (float)((1.0 - cos(std::numbers::pi * par)) * 0.5); // NOLINT
574
575 thisp->getSoRenderManager()->getCamera()->position = thisp->m_camerastartposition +
576 (thisp->m_cameraendposition - thisp->m_camerastartposition) * par;
577 thisp->getSoRenderManager()->getCamera()->orientation =
578 SbRotation::slerp(thisp->m_camerastartorient,
579 thisp->m_cameraendorient,
580 par);
581
582 if (end) {
583 thisp->setSeekMode(false);
584 }
585 }
586
587 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::saveHomePosition()
588 {
589 SoCamera* cam = getSoRenderManager()->getCamera();
590 if (!cam) {
591 return;
592 }
593
594 SoType type = cam->getTypeId();
595 assert(type.isDerivedFrom(SoNode::getClassTypeId()));
596 assert(type.canCreateInstance());
597
598 if(m_storedcamera) {
599 m_storedcamera->unref();
600 }
601
602 m_storedcamera = static_cast<SoNode*>(type.createInstance()); // NOLINT
603 m_storedcamera->ref();
604
605 m_storedcamera->copyFieldValues(getSoRenderManager()->getCamera());
606 }
607
608 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::resetToHomePosition()
609 {
610 SoCamera* cam = getSoRenderManager()->getCamera();
611 if (!cam) {
612 return;
613 }
614
615 if(!m_storedcamera) {
616 return;
617 }
618
619 SoType ttype = getSoRenderManager()->getCamera()->getTypeId();
620 SoType stype = m_storedcamera->getTypeId();
621
622 // most common case
623 if (ttype == stype) {
624 // We copy the field data directly, instead of using
625 // SoFieldContainer::copyContents(), for the reason described in
626 // detail in So@Gui@Viewer::saveHomePosition().
627 getSoRenderManager()->getCamera()->copyFieldValues(m_storedcamera);
628 }
629 // handle common case #1
630 else if(ttype == SoOrthographicCamera::getClassTypeId() &&
631 stype == SoPerspectiveCamera::getClassTypeId()) {
632 convertPerspective2Ortho(dynamic_cast<SoPerspectiveCamera*>(m_storedcamera),
633 dynamic_cast<SoOrthographicCamera*>(getSoRenderManager()->getCamera()));
634 }
635 // handle common case #2
636 else if(ttype == SoPerspectiveCamera::getClassTypeId() &&
637 stype == SoOrthographicCamera::getClassTypeId()) {
638 convertOrtho2Perspective(dynamic_cast<SoOrthographicCamera*>(m_storedcamera),
639 dynamic_cast<SoPerspectiveCamera*>(getSoRenderManager()->getCamera()));
640 }
641
642 // otherwise, cameras have changed in ways we don't understand since
643 // the last saveHomePosition() invocation, and so we're just going
644 // to ignore the reset request
645 }
646
647
648 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::draw2DString(
649 const char* str,
650 SbVec2s glsize,
651 SbVec2f position,
652 Base::Color color = Base::Color(1.0F, 1.0F, 0.0F)) // retains yellow as default color
653 {
654 // Store GL state.
655 glPushAttrib(GL_ENABLE_BIT|GL_CURRENT_BIT);
656
657 glDisable(GL_LIGHTING);
658 glDisable(GL_DEPTH_TEST);
659 glDisable(GL_TEXTURE_2D);
660 glDisable(GL_BLEND);
661
662 glMatrixMode(GL_MODELVIEW);
663 glPushMatrix();
664 glLoadIdentity();
665
666 glMatrixMode(GL_PROJECTION);
667 glPushMatrix();
668 glLoadIdentity();
669 glOrtho(0.0, glsize[0], 0.0, glsize[1], -1, 1);
670
671 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
672
673 // glColor3f(0.0, 0.0, 0.0);
674 // glRasterPos2f(position[0] + 1, position[1]);
675 // printString(str);
676 // glRasterPos2f(position[0] - 1, position[1]);
677 // printString(str);
678 // glRasterPos2f(position[0], position[1] + 1);
679 // printString(str);
680 // glRasterPos2f(position[0], position[1] - 1);
681 // printString(str);
682
683 glColor3f(color.r, color.g, color.b);
684 glRasterPos2f(position[0], position[1]);
685 printString(str);
686
687 glMatrixMode(GL_PROJECTION);
688 glPopMatrix();
689 glMatrixMode(GL_MODELVIEW);
690 glPopMatrix();
691
692 glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // restore default value
693
694 glPopAttrib();
695 }
696
697 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::printString(const char* str)
698 {
699 // NOLINTBEGIN
700 std::size_t len = strlen(str);
701
702 for(std::size_t i = 0; i < len; i++) {
703 glBitmap(8, 12, 0.0, 2.0, 10.0, 0.0, fps2dfont[str[i] - 32]);
704 }
705 // NOLINTEND
706 }
707
708 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::moveCameraScreen(const SbVec2f& screenpos)
709 {
710 SoCamera* cam = getSoRenderManager()->getCamera();
711 assert(cam);
712
713
714 SbViewVolume vv = cam->getViewVolume(getGLWidget()->width() / getGLWidget()->height());
715 SbPlane panplane = vv.getPlane(cam->focalDistance.getValue());
716
717 constexpr const float mid = 0.5F;
718 SbLine line;
719 vv.projectPointToLine(screenpos + SbVec2f(mid, mid), line);
720 SbVec3f current_planept;
721 panplane.intersect(line, current_planept);
722 vv.projectPointToLine(SbVec2f(mid, mid), line);
723 SbVec3f old_planept;
724 panplane.intersect(line, old_planept);
725
726 // Reposition camera according to the vector difference between the
727 // projected points.
728 cam->position = cam->position.getValue() - (current_planept - old_planept);
729 }
730
731 bool SIM::Coin3D::Quarter::SoQTQuarterAdaptor::processSoEvent(const SoEvent* event)
732 {
733 const SoType type(event->getTypeId());
734
735 constexpr const float delta = 0.1F;
736 if(type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
737 const SoKeyboardEvent* keyevent = static_cast<const SoKeyboardEvent*>(event); // NOLINT
738
739 if(keyevent->getState() == SoButtonEvent::DOWN) {
740 switch(keyevent->getKey()) {
741
742 case SoKeyboardEvent::LEFT_ARROW:
743 moveCameraScreen(SbVec2f(-delta, 0.0F));
744 return true;
745
746 case SoKeyboardEvent::UP_ARROW:
747 moveCameraScreen(SbVec2f(0.0F, delta));
748 return true;
749
750 case SoKeyboardEvent::RIGHT_ARROW:
751 moveCameraScreen(SbVec2f(delta, 0.0F));
752 return true;
753
754 case SoKeyboardEvent::DOWN_ARROW:
755 moveCameraScreen(SbVec2f(0.0F, -delta));
756 return true;
757
758 default:
759 break;
760 }
761 }
762 }
763
764 return SIM::Coin3D::Quarter::QuarterWidget::processSoEvent(event);
765 }
766
767 /*!
768 Overridden from QuarterWidget to render the scenegraph
769 */
770 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::paintEvent(QPaintEvent* event)
771 {
772 double start = SbTime::getTimeOfDay().getValue();
773 QuarterWidget::paintEvent(event);
774 this->framesPerSecond = addFrametime(start);
775
776 #ifdef BUILD_TRACY_FRAME_PROFILER
777 FrameMark;
778 #endif
779 }
780
781 void SIM::Coin3D::Quarter::SoQTQuarterAdaptor::resetFrameCounter()
782 {
783 this->framecount = 0;
784 this->frametime = 0.0F;
785 this->drawtime = 0.0F;
786 this->starttime = SbTime::getTimeOfDay().getValue();
787 this->framesPerSecond = SbVec2f(0, 0);
788 }
789
790 SbVec2f SIM::Coin3D::Quarter::SoQTQuarterAdaptor::addFrametime(double starttime)
791 {
792 constexpr const double FPS_FACTOR = 0.7;
793 constexpr const double FIVE_SECS = 5000.0;
794 constexpr const float ONE_SEC = 1000.0F;
795
796 this->framecount++;
797
798 double timeofday = SbTime::getTimeOfDay().getValue();
799
800 // draw time is the actual time spent on rendering
801 double drawtime = timeofday - starttime;
802 this->drawtime = (drawtime*FPS_FACTOR) + this->drawtime*(1.0 - FPS_FACTOR);
803
804 // frame time is the time spent since the last frame. There could an
805 // indefinite pause between the last frame because the scene is not
806 // changing. So we limit the skew to 5 second.
807 double frametime = std::min(timeofday-this->starttime, std::max(drawtime, FIVE_SECS));
808 this->frametime = (frametime*FPS_FACTOR) + this->frametime*(1.0 - FPS_FACTOR);
809
810 this->starttime = timeofday;
811 return {ONE_SEC * float(this->drawtime), 1.0F / float(this->frametime)};
812 }
813 // NOLINTEND(readability-implicit-bool-conversion)
814
815 #include "moc_SoQTQuarterAdaptor.cpp"