| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2014-2023 3Dconnexion. * |
| 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 |
#ifdef USE_3DCONNEXION_NAVLIB |
| 24 |
|
| 25 |
#include <QImage> |
| 26 |
#include <array> |
| 27 |
|
| 28 |
#include <SpaceMouse/CNavigation3D.hpp> |
| 29 |
|
| 30 |
#include <Inventor/SbVec2f.h> |
| 31 |
#include <Inventor/SbVec3f.h> |
| 32 |
|
| 33 |
using CNav3D = TDx::SpaceMouse::Navigation3D::CNavigation3D; |
| 34 |
using TDxCategory = TDx::SpaceMouse::CCategory; |
| 35 |
using TDxCommand = TDx::SpaceMouse::CCommand; |
| 36 |
using TDxCommandSet = TDx::SpaceMouse::CCommandSet; |
| 37 |
using TDxImage = TDx::CImage; |
| 38 |
|
| 39 |
constexpr uint32_t hitTestingResolution = 30; |
| 40 |
constexpr std::string_view workbenchStr("Workbench"); |
| 41 |
constexpr std::string_view noneWorkbenchStr("NoneWorkbench"); |
| 42 |
|
| 43 |
class QGraphicsView; |
| 44 |
class QAction; |
| 45 |
class SoTransform; |
| 46 |
class SoSwitch; |
| 47 |
class SoResetTransform; |
| 48 |
class SoImage; |
| 49 |
class SoDepthBuffer; |
| 50 |
|
| 51 |
namespace Gui |
| 52 |
{ |
| 53 |
class MDIView; |
| 54 |
class View3DInventor; |
| 55 |
class View3DInventorViewer; |
| 56 |
class Document; |
| 57 |
class Command; |
| 58 |
class ActionGroup; |
| 59 |
}// namespace Gui |
| 60 |
|
| 61 |
class NavlibInterface: public CNav3D |
| 62 |
{ |
| 63 |
|
| 64 |
public: |
| 65 |
NavlibInterface(); |
| 66 |
~NavlibInterface(); |
| 67 |
void enableNavigation(); |
| 68 |
void disableNavigation(); |
| 69 |
|
| 70 |
private: |
| 71 |
long IsUserPivot(navlib::bool_t&) const override; |
| 72 |
long GetCameraMatrix(navlib::matrix_t&) const override; |
| 73 |
long GetPointerPosition(navlib::point_t&) const override; |
| 74 |
long GetViewExtents(navlib::box_t&) const override; |
| 75 |
long GetViewFOV(double&) const override; |
| 76 |
long GetViewFrustum(navlib::frustum_t&) const override; |
| 77 |
long GetIsViewPerspective(navlib::bool_t&) const override; |
| 78 |
long GetModelExtents(navlib::box_t&) const override; |
| 79 |
long GetSelectionExtents(navlib::box_t&) const override; |
| 80 |
long GetSelectionTransform(navlib::matrix_t&) const override; |
| 81 |
long GetIsSelectionEmpty(navlib::bool_t&) const override; |
| 82 |
long GetPivotPosition(navlib::point_t&) const override; |
| 83 |
long GetPivotVisible(navlib::bool_t&) const override; |
| 84 |
long GetHitLookAt(navlib::point_t&) const override; |
| 85 |
long GetFrontView(navlib::matrix_t&) const override; |
| 86 |
long GetCoordinateSystem(navlib::matrix_t&) const override; |
| 87 |
long GetIsViewRotatable(navlib::bool_t&) const override; |
| 88 |
long GetUnitsToMeters(double&) const override; |
| 89 |
|
| 90 |
long SetCameraMatrix(const navlib::matrix_t&) override; |
| 91 |
long SetViewExtents(const navlib::box_t&) override; |
| 92 |
long SetViewFOV(double) override; |
| 93 |
long SetViewFrustum(const navlib::frustum_t&) override; |
| 94 |
long SetSelectionTransform(const navlib::matrix_t&) override; |
| 95 |
long SetPivotPosition(const navlib::point_t&) override; |
| 96 |
long SetPivotVisible(bool) override; |
| 97 |
long SetHitAperture(double) override; |
| 98 |
long SetHitDirection(const navlib::vector_t&) override; |
| 99 |
long SetHitLookFrom(const navlib::point_t&) override; |
| 100 |
long SetHitSelectionOnly(bool) override; |
| 101 |
long SetActiveCommand(std::string) override; |
| 102 |
long SetTransaction(long) override; |
| 103 |
|
| 104 |
struct |
| 105 |
{ |
| 106 |
const Gui::View3DInventor* pView3d = nullptr; |
| 107 |
QGraphicsView* pView2d = nullptr; |
| 108 |
} currentView; |
| 109 |
|
| 110 |
struct |
| 111 |
{ |
| 112 |
SbVec3f origin; |
| 113 |
SbVec3f direction; |
| 114 |
float radius; |
| 115 |
bool selectionOnly; |
| 116 |
} ray; |
| 117 |
|
| 118 |
struct |
| 119 |
{ |
| 120 |
SoTransform* pTransform = nullptr; |
| 121 |
SoSwitch* pVisibility = nullptr; |
| 122 |
SoResetTransform* pResetTransform = nullptr; |
| 123 |
SoImage* pImage = nullptr; |
| 124 |
SoDepthBuffer* pDepthTestAlways = nullptr; |
| 125 |
SoDepthBuffer* pDepthTestLess = nullptr; |
| 126 |
QImage pivotImage; |
| 127 |
} pivot; |
| 128 |
|
| 129 |
struct ParsedData |
| 130 |
{ |
| 131 |
std::string groupName; |
| 132 |
std::string commandName; |
| 133 |
int32_t actionIndex; |
| 134 |
}; |
| 135 |
|
| 136 |
ParsedData parseCommandId(const std::string& commandId) const; |
| 137 |
std::string getId(const Gui::Command& command, const int32_t parameter) const; |
| 138 |
TDxImage getImage(const QAction& qaction, const std::string& id) const; |
| 139 |
TDxCommand getCCommand(const Gui::Command& command, |
| 140 |
const QAction& qAction, |
| 141 |
const int32_t parameter) const; |
| 142 |
// This method removes markups from text (markup is a |
| 143 |
// string enclosed with '<' and '>' characters). Paragraph |
| 144 |
// ending markups "</p>" are being replaced with "\n\n". |
| 145 |
void removeMarkups(std::string& text) const; |
| 146 |
void initializePivot(); |
| 147 |
void initializePattern() const; |
| 148 |
void connectActiveTab(); |
| 149 |
template<class CameraType> |
| 150 |
CameraType getCamera() const; |
| 151 |
void onViewChanged(const Gui::MDIView*); |
| 152 |
bool is3DView() const; |
| 153 |
bool is2DView() const; |
| 154 |
void exportCommands(const std::string& workbench); |
| 155 |
void unpackCommands(Gui::Command& command, |
| 156 |
TDxCategory& category, |
| 157 |
std::vector<TDxImage>& images); |
| 158 |
|
| 159 |
std::error_code errorCode; |
| 160 |
std::pair<int, std::string> activeTab; |
| 161 |
mutable std::array<SbVec2f, hitTestingResolution> hitTestPattern; |
| 162 |
mutable bool patternInitialized; |
| 163 |
std::vector<std::string> exportedCommandSets; |
| 164 |
mutable bool wasPointerPick = false; |
| 165 |
double orthoNearDistance = 0.0; |
| 166 |
}; |
| 167 |
#endif |