Debian Codegraph @

src/Gui/View3DInventorViewer.h

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef GUI_VIEW3DINVENTORVIEWER_H
25 #define GUI_VIEW3DINVENTORVIEWER_H
26
27 #include <list>
28 #include <map>
29 #include <memory>
30 #include <set>
31 #include <vector>
32
33 #include <QCursor>
34 #include <QImage>
35 #include <QLabel>
36
37 #include <Inventor/SbRotation.h>
38 #include <Inventor/nodes/SoEnvironment.h>
39 #include <Inventor/nodes/SoEventCallback.h>
40 #include <Inventor/nodes/SoRotation.h>
41 #include <Inventor/nodes/SoSwitch.h>
42
43 #include <FCConfig.h>
44
45 #ifdef FC_OS_MACOSX
46 # include <OpenGL/gl.h>
47 #else
48 # ifdef FC_OS_WIN32
49 # include <windows.h>
50 # endif // FC_OS_WIN32
51 # include <GL/gl.h>
52 #endif // FC_OS_MACOSX
53
54 #include <Base/Placement.h>
55
56 #include "Namespace.h"
57 #include "Selection/Selection.h"
58
59 #include "CornerCrossLetters.h"
60 #include "View3DInventorSelection.h"
61 #include "Quarter/SoQTQuarterAdaptor.h"
62
63 class QOpenGLFramebufferObject;
64 class QOpenGLWidget;
65 class QSurfaceFormat;
66
67 class SoTranslation;
68 class SoTransform;
69 class SoText2;
70
71 class SoSeparator;
72 class SoShapeHints;
73 class SoMaterial;
74 class SoRotationXYZ;
75 class SbSphereSheetProjector;
76 class SoEventCallback; // NOLINT
77 class SbBox2s;
78 class SoVectorizeAction;
79 class QImage;
80 class SoGroup; // NOLINT
81 class SoPickStyle;
82 class NaviCube;
83 class SoClipPlane;
84
85 namespace Quarter = SIM::Coin3D::Quarter;
86
87 namespace Base
88 {
89 class BoundBox2d;
90 }
91
92 namespace Gui
93 {
94 class NavigationAnimation;
95 class ViewProvider;
96 class SoFCBackgroundGradient;
97 class NavigationStyle;
98 class SoFCUnifiedSelection;
99 class Document;
100 class GLGraphicsItem;
101 class SoShapeScale;
102 class ViewerEventFilter;
103
104 /** GUI view into a 3D scene provided by View3DInventor
105 *
106 */
107 class GuiExport View3DInventorViewer: public Quarter::SoQTQuarterAdaptor, public SelectionObserver
108 {
109 using inherited = Quarter::SoQTQuarterAdaptor;
110 Q_OBJECT
111
112 public:
113 /// Pick modes for picking points in the scene
114 enum SelectionMode
115 {
116 Lasso = 0, /**< Select objects using a lasso. */
117 Rectangle = 1, /**< Select objects using a rectangle. */
118 Rubberband = 2, /**< Select objects using a rubberband. */
119 BoxZoom = 3, /**< Perform a box zoom. */
120 Clip = 4, /**< Clip objects using a lasso. */
121 };
122 /** @name Modus handling of the viewer
123 * Here you can switch several features on/off
124 * and modes of the Viewer
125 */
126 //@{
127 enum ViewerMod
128 {
129 ShowCoord = 1, /**< Enables the Coordinate system in the corner. */
130 ShowFPS = 2, /**< Enables the Frames Per Second counter. */
131 SimpleBackground = 4, /**< switch to a simple background. */
132 DisallowRotation = 8, /**< switch off the rotation. */
133 DisallowPanning = 16, /**< switch off the panning. */
134 DisallowZooming = 32, /**< switch off the zooming. */
135 };
136 //@}
137
138 /** @name Render mode
139 */
140 //@{
141 enum RenderType
142 {
143 Native,
144 Framebuffer,
145 Image
146 };
147 //@}
148
149 /** @name Background
150 */
151 //@{
152 enum Background
153 {
154 NoGradient,
155 LinearGradient,
156 RadialGradient
157 };
158 //@}
159
160 explicit View3DInventorViewer(QWidget* parent, const QOpenGLWidget* sharewidget = nullptr);
161 View3DInventorViewer(
162 const QSurfaceFormat& format,
163 QWidget* parent,
164 const QOpenGLWidget* sharewidget = nullptr
165 );
166 ~View3DInventorViewer() override;
167
168 void init();
169
170 /// Observer message from the Selection
171 void onSelectionChanged(const SelectionChanges& Reason) override;
172
173 SoDirectionalLight* getBacklight() const;
174 void setBacklightEnabled(bool on);
175 bool isBacklightEnabled() const;
176
177 SoDirectionalLight* getFillLight() const;
178 void setFillLightEnabled(bool on);
179 bool isFillLightEnabled() const;
180
181 SoEnvironment* getEnvironment() const;
182
183 void setSceneGraph(SoNode* root) override;
184 bool searchNode(SoNode*) const;
185
186 void setAnimationEnabled(bool enable);
187 void setSpinningAnimationEnabled(bool enable);
188 bool isAnimationEnabled() const;
189 bool isSpinningAnimationEnabled() const;
190 bool isAnimating() const;
191 bool isSpinning() const;
192 std::shared_ptr<NavigationAnimation> startAnimation(
193 const SbRotation& orientation,
194 const SbVec3f& rotationCenter,
195 const SbVec3f& translation,
196 int duration = -1,
197 bool wait = false
198 ) const;
199 void startSpinningAnimation(const SbVec3f& axis, float velocity);
200 void stopAnimating();
201
202 void setPopupMenuEnabled(bool on);
203 bool isPopupMenuEnabled() const;
204
205 void setFeedbackVisibility(bool enable);
206 bool isFeedbackVisible() const;
207
208 void setFeedbackSize(int size);
209 int getFeedbackSize() const;
210
211 /// Get the preferred samples from the user settings
212 static int getNumSamples();
213 void setRenderType(RenderType type);
214 RenderType getRenderType() const;
215 void renderToFramebuffer(QOpenGLFramebufferObject*);
216 QImage grabFramebuffer();
217 void imageFromFramebuffer(int width, int height, int samples, const QColor& bgcolor, QImage& img);
218
219 void setViewing(bool enable) override;
220 virtual void setCursorEnabled(bool enable);
221
222 void addGraphicsItem(GLGraphicsItem*);
223 void removeGraphicsItem(GLGraphicsItem*);
224 std::list<GLGraphicsItem*> getGraphicsItems() const;
225 std::list<GLGraphicsItem*> getGraphicsItemsOfType(const Base::Type&) const;
226 void clearGraphicsItems();
227
228 /** @name Handling of view providers */
229 //@{
230 /// Checks if the view provider is a top-level object of the scene
231 bool hasViewProvider(ViewProvider*) const;
232 /// Checks if the view provider is part of the scene.
233 /// In contrast to hasViewProvider() this method also checks if the view
234 /// provider is a child of another view provider
235 bool containsViewProvider(const ViewProvider*) const;
236 /// adds an ViewProvider to the view, e.g. from a feature
237 void addViewProvider(ViewProvider*);
238 /// remove a ViewProvider
239 void removeViewProvider(ViewProvider*);
240 /// get view provider by path
241 ViewProvider* getViewProviderByPath(SoPath*) const;
242 ViewProvider* getViewProviderByPathFromTail(SoPath*) const;
243 /// get all view providers of given type
244 std::vector<ViewProvider*> getViewProvidersOfType(const Base::Type& typeId) const;
245 /// set the ViewProvider in special edit mode
246 void setEditingViewProvider(Gui::ViewProvider* vp, int ModNum);
247 /// return whether a view provider is edited
248 bool isEditingViewProvider() const;
249 /// return currently editing view provider
250 ViewProvider* getEditingViewProvider() const;
251 /// reset from edit mode
252 void resetEditingViewProvider();
253 SoNode* getEditingRoot() const;
254 void setupEditingRoot(SoNode* node = nullptr, const Base::Matrix4D* mat = nullptr);
255 void resetEditingRoot(bool updateLinks = true);
256 void setEditingTransform(const Base::Matrix4D& mat);
257 /** Helper method to get picked entities while editing.
258 * It's in the responsibility of the caller to delete the returned instance.
259 */
260 SoPickedPoint* getPointOnRay(const SbVec2s& pos, const ViewProvider* vp) const;
261 /** Helper method to get picked entities while editing.
262 * It's in the responsibility of the caller to delete the returned instance.
263 */
264 SoPickedPoint* getPointOnRay(const SbVec3f& pos, const SbVec3f& dir, const ViewProvider* vp) const;
265 /// display override mode
266 void setOverrideMode(const std::string& mode);
267 void updateOverrideMode(const std::string& mode);
268 std::string getOverrideMode() const
269 {
270 return overrideMode;
271 }
272 //@}
273
274 /** @name Making pictures */
275 //@{
276 /**
277 * Creates an image with width \a width and height \a height of the current scene graph
278 * using a multi-sampling of \a sample and exports the rendered scenegraph to an image.
279 */
280 void savePicture(int width, int height, int sample, const QColor& bg, QImage& img) const;
281 void saveGraphic(int pagesize, const QColor&, SoVectorizeAction* va) const;
282 //@}
283 /**
284 * Writes the current scenegraph to an Inventor file, either in ascii or binary.
285 */
286 bool dumpToFile(SoNode* node, const char* filename, bool binary) const;
287
288 /** @name Selection methods */
289 //@{
290 void startSelection(SelectionMode = Lasso);
291 void abortSelection();
292 void stopSelection();
293 bool isSelecting() const;
294 std::vector<SbVec2f> getGLPolygon(SelectionRole* role = nullptr) const;
295 std::vector<SbVec2f> getGLPolygon(const std::vector<SbVec2s>&) const;
296 const std::vector<SbVec2s>& getPolygon(SelectionRole* role = nullptr) const;
297 void setSelectionEnabled(bool enable);
298 bool isSelectionEnabled() const;
299 //@}
300
301 /// Returns the screen coordinates of the origin of the path's tail object
302 /*! Return value is in floating-point pixels, origin at bottom-left. */
303 SbVec2f screenCoordsOfPath(SoPath* path) const;
304
305 /** @name Edit methods */
306 //@{
307 void setEditing(bool edit);
308 bool isEditing() const
309 {
310 return this->editing;
311 }
312 void setEditingCursor(const QCursor& cursor);
313 void setComponentCursor(const QCursor& cursor);
314 void setRedirectToSceneGraph(bool redirect)
315 {
316 this->redirected = redirect;
317 }
318 bool isRedirectedToSceneGraph() const
319 {
320 return this->redirected;
321 }
322 void setRedirectToSceneGraphEnabled(bool enable)
323 {
324 this->allowredir = enable;
325 }
326 bool isRedirectToSceneGraphEnabled() const
327 {
328 return this->allowredir;
329 }
330 //@}
331
332 /** @name Pick actions */
333 //@{
334 // calls a PickAction on the scene graph
335 bool pickPoint(const SbVec2s& pos, SbVec3f& point, SbVec3f& norm) const;
336 SoPickedPoint* pickPoint(const SbVec2s& pos) const;
337 const SoPickedPoint* getPickedPoint(SoEventCallback* n) const;
338 bool pubSeekToPoint(const SbVec2s& pos);
339 void pubSeekToPoint(const SbVec3f& pos);
340 //@}
341
342 /**
343 * Set up a callback function \a cb which will be invoked for the given eventtype.
344 * \a userdata will be given as the first argument to the callback function.
345 */
346 void addEventCallback(SoType eventtype, SoEventCallbackCB* cb, void* userdata = nullptr);
347 /**
348 * Unregister the given callback function \a cb.
349 */
350 void removeEventCallback(SoType eventtype, SoEventCallbackCB* cb, void* userdata = nullptr);
351
352 /** @name Clipping plane, near and far plane */
353 //@{
354 /** Returns the view direction from the user's eye point in direction to the
355 * viewport which is actually the negative normal of the near plane.
356 * The vector is normalized to length of 1.
357 */
358 SbVec3f getViewDirection() const;
359 void setViewDirection(SbVec3f);
360 /** Returns the up direction */
361 SbVec3f getUpDirection() const;
362
363 /** Returns the orientation of the camera. */
364 SbRotation getCameraOrientation() const;
365
366 /** Returns the 3d point on the focal plane to the given 2d point. */
367 SbVec3f getPointOnFocalPlane(const SbVec2s&) const;
368
369 /** Returns the 3d point on a line to the given 2d point. */
370 SbVec3f getPointOnLine(const SbVec2s&, const SbVec3f& axisCenter, const SbVec3f& axis) const;
371
372 /** Returns the 3d point on the XY plane of a placement to the given 2d point. */
373 SbVec3f getPointOnXYPlaneOfPlacement(const SbVec2s&, const Base::Placement&) const;
374
375 /** Returns the bounding box on the XY plane of a placement to the given 2d point. */
376 Base::BoundBox2d getViewportOnXYPlaneOfPlacement(Base::Placement plc) const;
377
378 /** Returns the 2d coordinates on the viewport to the given 3d point. */
379 SbVec2s getPointOnViewport(const SbVec3f&) const;
380
381 /** Converts Inventor coordinates into Qt coordinates.
382 * The conversion takes the device pixel ratio into account.
383 */
384 QPoint toQPoint(const SbVec2s&) const;
385
386 /** Converts Qt coordinates into Inventor coordinates.
387 * The conversion takes the device pixel ratio into account.
388 */
389 SbVec2s fromQPoint(const QPoint&) const;
390
391 /** Returns the near plane represented by its normal and base point. */
392 void getNearPlane(SbVec3f& rcPt, SbVec3f& rcNormal) const;
393
394 /** Returns the far plane represented by its normal and base point. */
395 void getFarPlane(SbVec3f& rcPt, SbVec3f& rcNormal) const;
396
397 /** Adds or remove a manipulator to/from the scenegraph. */
398 void toggleClippingPlane(
399 int toggle = -1,
400 bool beforeEditing = false,
401 bool noManip = false,
402 const Base::Placement& pla = Base::Placement()
403 );
404
405 /** Checks whether a clipping plane is set or not. */
406 bool hasClippingPlane() const;
407
408 /** Project the given normalized 2d point onto the near plane */
409 SbVec3f projectOnNearPlane(const SbVec2f&) const;
410
411 /** Project the given normalized 2d point onto the far plane */
412 SbVec3f projectOnFarPlane(const SbVec2f&) const;
413
414 /** Project the given 2d point to a line */
415 void projectPointToLine(const SbVec2s&, SbVec3f& pt1, SbVec3f& pt2) const;
416
417 /** Get the normalized position of the 2d point. */
418 SbVec2f getNormalizedPosition(const SbVec2s&) const;
419 //@}
420
421 /** @name Dimension controls
422 * the "turn*" functions are wired up to parameter groups through view3dinventor.
423 * don't call them directly. instead set the parameter groups.
424 * @see TaskDimension
425 */
426 //@{
427 void turnAllDimensionsOn();
428 void turnAllDimensionsOff();
429 void turn3dDimensionsOn();
430 void turn3dDimensionsOff();
431 void turnDeltaDimensionsOn();
432 void turnDeltaDimensionsOff();
433 void eraseAllDimensions();
434 void addDimension3d(SoNode* node);
435 void addDimensionDelta(SoNode* node);
436 //@}
437
438 /**
439 * Set the camera's orientation. If isAnimationEnabled() returns
440 * \a true the reorientation is animated and the animation is returned, otherwise its directly
441 * set.
442 */
443 std::shared_ptr<NavigationAnimation> setCameraOrientation(
444 const SbRotation& orientation,
445 bool moveToCenter = false
446 ) const;
447 void setCameraType(SoType type) override;
448 void moveCameraTo(const SbRotation& orientation, const SbVec3f& position, int duration = -1);
449 /**
450 * Zooms the viewport to the size of the bounding box.
451 */
452 void boxZoom(const SbBox2s&);
453 /**
454 * Scale the viewport by a linear amount
455 */
456 void scale(float factor);
457 /**
458 * Reposition the current camera so we can see the complete scene.
459 */
460 void viewAll() override;
461 void viewAll(float factor);
462
463 /// Breaks out a VR window for a Rift
464 void viewVR();
465
466 /**
467 * Returns the bounding box of the scene graph.
468 */
469 SbBox3f getBoundingBox() const;
470
471 /**
472 * Reposition the current camera so we can see all selected objects
473 * of the scene. Therefore we search for all SOFCSelection nodes, if
474 * none of them is selected nothing happens.
475 */
476 void viewSelection();
477
478 void alignToSelection();
479
480 void setGradientBackground(Background);
481 Background getGradientBackground() const;
482 void setGradientBackgroundColor(const SbColor& fromColor, const SbColor& toColor);
483 void setGradientBackgroundColor(
484 const SbColor& fromColor,
485 const SbColor& toColor,
486 const SbColor& midColor
487 );
488 void setNavigationType(Base::Type);
489
490 void setAxisLetterColor(const SbColor& color);
491 void setAxisCross(bool on);
492 bool hasAxisCross();
493
494 void showRotationCenter(bool show);
495 void changeRotationCenterPosition(const SbVec3f& newCenter);
496
497 void setEnabledFPSCounter(bool on);
498 void setEnabledNaviCube(bool on);
499 bool isEnabledNaviCube() const;
500 void setNaviCubeCorner(int);
501 NaviCube* getNaviCube() const;
502 void setEnabledVBO(bool on);
503 bool isEnabledVBO() const;
504 void setRenderCache(int);
505
506 //! Update colors of axis in corner to match preferences
507 void updateColors();
508
509 void getDimensions(float& fHeight, float& fWidth) const;
510 float getMaxDimension() const;
511 SbVec3f getCenterPointOnFocalPlane() const;
512
513 NavigationStyle* navigationStyle() const;
514
515 void setDocument(Gui::Document* pcDocument);
516 Gui::Document* getDocument();
517
518 virtual PyObject* getPyObject();
519
520 protected:
521 static GLenum getInternalTextureFormat();
522 void renderScene();
523 void renderFramebuffer();
524 void renderGLImage();
525 void animatedViewAll(int steps, int ms);
526 void actualRedraw() override;
527 void setSeekMode(bool on) override;
528 void afterRealizeHook() override;
529 bool processSoEvent(const SoEvent* ev) override;
530 void dropEvent(QDropEvent* ev) override;
531 void dragEnterEvent(QDragEnterEvent* ev) override;
532 void dragMoveEvent(QDragMoveEvent* ev) override;
533 void dragLeaveEvent(QDragLeaveEvent* ev) override;
534 bool processSoEventBase(const SoEvent* const ev);
535 void printDimension() const;
536 void selectAll();
537
538 private:
539 static void setViewportCB(void* userdata, SoAction* action);
540 static void clearBufferCB(void* userdata, SoAction* action);
541 static void setGLWidgetCB(void* userdata, SoAction* action);
542 static void handleEventCB(void* userdata, SoEventCallback* n);
543 static void interactionStartCB(void* data, Quarter::SoQTQuarterAdaptor* viewer);
544 static void interactionFinishCB(void* data, Quarter::SoQTQuarterAdaptor* viewer);
545 static void interactionLoggerCB(void* ud, SoAction* action);
546
547 private:
548 static void selectCB(void* viewer, SoPath* path);
549 static void deselectCB(void* viewer, SoPath* path);
550 static SoPath* pickFilterCB(void* viewer, const SoPickedPoint* pp);
551 void initialize();
552 void drawAxisCross();
553 static void drawArrow();
554 static void drawSingleBackground(const QColor&);
555 void setCursorRepresentation(int mode);
556 void aboutToDestroyGLContext();
557 void createStandardCursors();
558
559 private:
560 NaviCube* naviCube;
561 std::set<ViewProvider*> _ViewProviderSet;
562 std::map<SoSeparator*, ViewProvider*> _ViewProviderMap;
563 std::list<GLGraphicsItem*> graphicsItems;
564 ViewProvider* editViewProvider;
565 SoFCBackgroundGradient* pcBackGround;
566 SoSeparator* backgroundroot;
567 SoSeparator* foregroundroot;
568
569 SoDirectionalLight* backlight;
570 SoDirectionalLight* fillLight;
571 SoEnvironment* environment;
572
573 SoRotation* lightRotation;
574
575 // Scene graph root
576 SoSeparator* pcViewProviderRoot;
577 // Child group in the scene graph that contains view providers related to the physical object
578 SoGroup* objectGroup;
579
580 std::unique_ptr<View3DInventorSelection> inventorSelection;
581
582 SoSeparator* pcEditingRoot;
583 SoTransform* pcEditingTransform;
584 bool restoreEditingRoot;
585 SoEventCallback* pEventCallback;
586 NavigationStyle* navigation;
587 SoFCUnifiedSelection* selectionRoot;
588
589 SoClipPlane* pcClipPlane;
590
591 RenderType renderType;
592 QOpenGLFramebufferObject* framebuffer;
593 QImage glImage;
594 bool shading;
595 SoSwitch* dimensionRoot;
596
597 // small axis cross in the corner
598 bool axiscrossEnabled;
599 int axiscrossSize;
600 // big one in the middle
601 SoShapeScale* axisCross;
602 SoGroup* axisGroup;
603
604 SoGroup* rotationCenterGroup;
605
606 // stuff needed to draw the fps counter
607 bool fpsEnabled;
608 QLabel* fpsCounter = nullptr;
609 unsigned long previousAxisLetterColor = 0;
610 bool vboEnabled;
611 bool naviCubeEnabled;
612
613 Base::Color m_xColor;
614 Base::Color m_yColor;
615 Base::Color m_zColor;
616
617 bool editing;
618 QCursor editCursor, zoomCursor, panCursor, spinCursor;
619 bool redirected;
620 bool allowredir;
621
622 std::string overrideMode;
623 Gui::Document* guiDocument = nullptr;
624
625 ViewerEventFilter* viewerEventFilter;
626
627 PyObject* _viewerPy;
628
629 static unsigned char XPM_pixel_data[YPM_WIDTH * YPM_HEIGHT * YPM_BYTES_PER_PIXEL + 1];
630 static unsigned char YPM_pixel_data[YPM_WIDTH * YPM_HEIGHT * YPM_BYTES_PER_PIXEL + 1];
631 static unsigned char ZPM_pixel_data[ZPM_WIDTH * ZPM_HEIGHT * ZPM_BYTES_PER_PIXEL + 1];
632
633 // friends
634 friend class NavigationStyle;
635 friend class GLPainter;
636 friend class ViewerEventFilter;
637 };
638
639 } // namespace Gui
640
641 #endif // GUI_VIEW3DINVENTORVIEWER_H