Debian Codegraph @

src/Gui/3Dconnexion/navlib/NavlibPivot.cpp

raw
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 #include <limits>
24
25 #include <QImage>
26 #include <QScreen>
27 #include <QString>
28 #include <QWindow>
29
30 #include "NavlibInterface.h"
31 #include <Inventor/SbMatrix.h>
32 #include <Inventor/SbViewVolume.h>
33 #include <Inventor/SoPickedPoint.h>
34 #include <Inventor/SoRenderManager.h>
35 #include <Inventor/fields/SoSFVec3f.h>
36 #include <Inventor/nodes/SoCamera.h>
37 #include <Inventor/nodes/SoDepthBuffer.h>
38 #include <Inventor/nodes/SoImage.h>
39 #include <Inventor/nodes/SoResetTransform.h>
40 #include <Inventor/nodes/SoSeparator.h>
41 #include <Inventor/nodes/SoSwitch.h>
42 #include <Inventor/nodes/SoTransform.h>
43
44 #include <Gui/Application.h>
45 #include <Gui/BitmapFactory.h>
46 #include <Gui/Selection/Selection.h>
47 #include <Gui/View3DInventor.h>
48 #include <Gui/View3DInventorViewer.h>
49 #include <Gui/ViewProvider.h>
50
51 long NavlibInterface::GetSelectionTransform(navlib::matrix_t&) const
52 {
53 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
54 }
55
56 long NavlibInterface::GetIsSelectionEmpty(navlib::bool_t& empty) const
57 {
58 empty = !Gui::Selection().hasSelection();
59 return 0;
60 }
61
62 long NavlibInterface::SetSelectionTransform(const navlib::matrix_t&)
63 {
64 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
65 }
66
67 long NavlibInterface::GetPivotPosition(navlib::point_t&) const
68 {
69 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
70 }
71
72 long NavlibInterface::SetPivotPosition(const navlib::point_t& position)
73 {
74 if (pivot.pTransform == nullptr)
75 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
76
77 pivot.pTransform->translation.setValue(position.x, position.y, position.z);
78 return 0;
79 }
80
81 long NavlibInterface::IsUserPivot(navlib::bool_t& userPivot) const
82 {
83 userPivot = false;
84 return 0;
85 }
86
87 long NavlibInterface::GetPivotVisible(navlib::bool_t& visible) const
88 {
89 if (pivot.pVisibility == nullptr)
90 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
91
92 visible = pivot.pVisibility->whichChild.getValue() == SO_SWITCH_ALL;
93
94 return 0;
95 }
96
97 long NavlibInterface::SetPivotVisible(bool visible)
98 {
99 if (pivot.pVisibility == nullptr)
100 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
101
102 if (visible)
103 pivot.pVisibility->whichChild = SO_SWITCH_ALL;
104 else
105 pivot.pVisibility->whichChild = SO_SWITCH_NONE;
106
107 return 0;
108 }
109
110 extern template SoCamera* NavlibInterface::getCamera<SoCamera*>() const;
111
112 long NavlibInterface::GetHitLookAt(navlib::point_t& position) const
113 {
114 if (is2DView() || !is3DView())
115 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
116
117 const Gui::View3DInventorViewer* const inventorViewer = currentView.pView3d->getViewer();
118 if (inventorViewer == nullptr)
119 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
120
121 SoNode* pSceneGraph = inventorViewer->getSceneGraph();
122 if (pSceneGraph == nullptr)
123 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
124
125 // Prepare the ray-picking object
126 SoRayPickAction rayPickAction(inventorViewer->getSoRenderManager()->getViewportRegion());
127 SbMatrix cameraMatrix;
128 SbVec3f closestHitPoint;
129 float minLength = std::numeric_limits<float>::max();
130
131 // Get the camera rotation
132 SoCamera* pCamera = getCamera<SoCamera*>();
133
134 if (pCamera == nullptr)
135 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
136
137 pCamera->orientation.getValue().getValue(cameraMatrix);
138
139 // Initialize the samples array if it wasn't done before
140 initializePattern();
141
142 navlib::bool_t isPerspective;
143 GetIsViewPerspective(isPerspective);
144
145 for (uint32_t i = 0; i < hitTestingResolution; i++) {
146
147 SbVec3f origin;
148
149 if (wasPointerPick) {
150 origin = ray.origin;
151 }
152 else {
153 // Scale the sample like it was defined in camera space (placed on XY plane)
154 SbVec3f transform(hitTestPattern[i][0] * ray.radius,
155 hitTestPattern[i][1] * ray.radius,
156 0.0f);
157
158 // Apply the model-view transform to a sample (only the rotation)
159 cameraMatrix.multVecMatrix(transform, transform);
160
161 // Calculate origin of current hit-testing ray
162 origin = ray.origin + transform;
163 }
164
165 // Perform the hit-test
166 if (isPerspective) {
167 rayPickAction.setRay(origin,
168 ray.direction,
169 pCamera->nearDistance.getValue(),
170 pCamera->farDistance.getValue());
171 }
172 else {
173 rayPickAction.setRay(origin, ray.direction);
174 }
175
176 rayPickAction.apply(pSceneGraph);
177 SoPickedPoint* pickedPoint = rayPickAction.getPickedPoint();
178
179 // Check if there was a hit
180 if (pickedPoint != nullptr) {
181 SbVec3f hitPoint = pickedPoint->getPoint();
182 float distance = (origin - hitPoint).length();
183
184 // Save hit of the lowest depth
185 if (distance < minLength) {
186 minLength = distance;
187 closestHitPoint = hitPoint;
188 }
189 }
190
191 if (wasPointerPick) {
192 wasPointerPick = false;
193 break;
194 }
195 }
196
197 if (minLength < std::numeric_limits<float>::max()) {
198 std::copy(closestHitPoint.getValue(), closestHitPoint.getValue() + 3, &position.x);
199 return 0;
200 }
201
202 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
203 }
204
205 long NavlibInterface::GetSelectionExtents(navlib::box_t& extents) const
206 {
207 Base::BoundBox3d boundingBox;
208 auto selectionVector = Gui::Selection().getSelection();
209
210 std::for_each(selectionVector.begin(),
211 selectionVector.end(),
212 [&boundingBox](Gui::SelectionSingleton::SelObj& selection) {
213 Gui::ViewProvider* pViewProvider =
214 Gui::Application::Instance->getViewProvider(selection.pObject);
215
216 if (pViewProvider == nullptr)
217 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
218
219 boundingBox.Add(pViewProvider->getBoundingBox(selection.SubName, true));
220
221 return 0l;
222 });
223
224 extents = {{boundingBox.MinX,
225 boundingBox.MinY,
226 boundingBox.MinZ},
227 {boundingBox.MaxX,
228 boundingBox.MaxY,
229 boundingBox.MaxZ}};
230
231 return 0;
232 }
233
234 long NavlibInterface::SetHitAperture(double aperture)
235 {
236 ray.radius = aperture;
237 return 0;
238 }
239
240 long NavlibInterface::SetHitDirection(const navlib::vector_t& direction)
241 {
242 ray.direction.setValue(direction.x, direction.y, direction.z);
243 return 0;
244 }
245
246 long NavlibInterface::SetHitLookFrom(const navlib::point_t& eye)
247 {
248 navlib::bool_t isPerspective;
249
250 GetIsViewPerspective(isPerspective);
251
252 if (isPerspective) {
253 ray.origin.setValue(eye.x, eye.y, eye.z);
254 }
255 else {
256 auto pCamera = getCamera<SoCamera*>();
257 if (pCamera == nullptr) {
258 return navlib::make_result_code(navlib::navlib_errc::no_data_available);
259 }
260
261 SbVec3f position = pCamera->position.getValue();
262 ray.origin = position + orthoNearDistance * ray.direction;
263 }
264 return 0;
265 }
266
267 long NavlibInterface::SetHitSelectionOnly(bool hitSelection)
268 {
269 ray.selectionOnly = hitSelection;
270 return 0;
271 }
272
273 void NavlibInterface::initializePivot()
274 {
275 pivot.pVisibility = new SoSwitch;
276 pivot.pTransform = new SoTransform;
277 pivot.pResetTransform = new SoResetTransform;
278 pivot.pImage = new SoImage;
279 pivot.pDepthTestAlways = new SoDepthBuffer;
280 pivot.pDepthTestLess = new SoDepthBuffer;
281
282 pivot.pDepthTestAlways->function.setValue(SoDepthBufferElement::ALWAYS);
283 pivot.pDepthTestLess->function.setValue(SoDepthBufferElement::LESS);
284
285 pivot.pivotImage = QImage(QStringLiteral(":/icons/3dx_pivot.png"));
286 Gui::BitmapFactory().convert(pivot.pivotImage, pivot.pImage->image);
287
288 pivot.pVisibility->ref();
289 pivot.pVisibility->whichChild = SO_SWITCH_NONE;
290 pivot.pVisibility->addChild(pivot.pDepthTestAlways);
291 pivot.pVisibility->addChild(pivot.pTransform);
292 pivot.pVisibility->addChild(pivot.pImage);
293 pivot.pVisibility->addChild(pivot.pResetTransform);
294 pivot.pVisibility->addChild(pivot.pDepthTestLess);
295 }