| 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 "NavlibInterface.h" |
| 24 |
|
| 25 |
#include <QMatrix4x4> |
| 26 |
#include <QMdiArea> |
| 27 |
#include <QTabBar> |
| 28 |
|
| 29 |
#include <Inventor/SbDPMatrix.h> |
| 30 |
#include <Inventor/SbMatrix.h> |
| 31 |
#include <Inventor/SbViewVolume.h> |
| 32 |
#include <Inventor/SoRenderManager.h> |
| 33 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 34 |
#include <Inventor/fields/SoSFVec3f.h> |
| 35 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 36 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 37 |
|
| 38 |
#include <Gui/Application.h> |
| 39 |
#include <Gui/MDIView.h> |
| 40 |
#include <Gui/MainWindow.h> |
| 41 |
#include <Gui/View3DInventor.h> |
| 42 |
#include <Gui/View3DInventorViewer.h> |
| 43 |
#include <Gui/ViewProvider.h> |
| 44 |
#include <Gui/Workbench.h> |
| 45 |
#include <Gui/WorkbenchManager.h> |
| 46 |
|
| 47 |
#include <Base/BoundBox.h> |
| 48 |
|
| 49 |
NavlibInterface::NavlibInterface() |
| 50 |
: CNavigation3D(false, navlib::nlOptions_t::no_ui), |
| 51 |
activeTab({-1, ""}), |
| 52 |
patternInitialized(false) |
| 53 |
{} |
| 54 |
|
| 55 |
NavlibInterface::~NavlibInterface() |
| 56 |
{ |
| 57 |
disableNavigation(); |
| 58 |
|
| 59 |
if (pivot.pVisibility != nullptr) |
| 60 |
pivot.pVisibility->unref(); |
| 61 |
} |
| 62 |
|
| 63 |
void NavlibInterface::initializePattern() const |
| 64 |
{ |
| 65 |
if (patternInitialized) |
| 66 |
return; |
| 67 |
|
| 68 |
if (hitTestingResolution > 0) { |
| 69 |
hitTestPattern[0][0] = 0.0; |
| 70 |
hitTestPattern[0][1] = 0.0; |
| 71 |
} |
| 72 |
|
| 73 |
for (uint32_t i = 1; i < hitTestingResolution; i++) { |
| 74 |
float coefficient = sqrt(static_cast<float>(i) / static_cast<float>(hitTestingResolution)); |
| 75 |
float angle = 2.4f * static_cast<float>(i); |
| 76 |
float x = coefficient * sin(angle); |
| 77 |
float y = coefficient * cos(angle); |
| 78 |
hitTestPattern[i][0] = x; |
| 79 |
hitTestPattern[i][1] = y; |
| 80 |
} |
| 81 |
|
| 82 |
patternInitialized = true; |
| 83 |
} |
| 84 |
|
| 85 |
long NavlibInterface::GetPointerPosition(navlib::point_t& position) const |
| 86 |
{ |
| 87 |
if (is2DView()) { |
| 88 |
QPoint point = currentView.pView2d->mapFromGlobal(QCursor::pos()); |
| 89 |
point = currentView.pView2d->mapToScene(point).toPoint(); |
| 90 |
position.x = point.x(); |
| 91 |
position.y = -point.y(); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
if (is3DView()) { |
| 97 |
const Gui::View3DInventorViewer* const inventorViewer = currentView.pView3d->getViewer(); |
| 98 |
if (inventorViewer == nullptr) |
| 99 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 100 |
|
| 101 |
QPoint viewPoint = currentView.pView3d->mapFromGlobal(QCursor::pos()); |
| 102 |
viewPoint.setY(currentView.pView3d->height() - viewPoint.y()); |
| 103 |
|
| 104 |
double scaling = inventorViewer->devicePixelRatio(); |
| 105 |
viewPoint *= scaling; |
| 106 |
|
| 107 |
SbVec3f worldPosition = |
| 108 |
inventorViewer->getPointOnFocalPlane(SbVec2s(viewPoint.x(), viewPoint.y())); |
| 109 |
std::copy(worldPosition.getValue(), worldPosition.getValue() + 3, &position.x); |
| 110 |
|
| 111 |
wasPointerPick = true; |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 116 |
} |
| 117 |
|
| 118 |
template<class CameraType> |
| 119 |
CameraType NavlibInterface::getCamera() const |
| 120 |
{ |
| 121 |
if (is3DView()) { |
| 122 |
const Gui::View3DInventorViewer* inventorViewer = currentView.pView3d->getViewer(); |
| 123 |
if (inventorViewer != nullptr) |
| 124 |
return dynamic_cast<CameraType>(inventorViewer->getCamera()); |
| 125 |
} |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
template SoCamera* NavlibInterface::getCamera<SoCamera*>() const; |
| 130 |
|
| 131 |
void NavlibInterface::onViewChanged(const Gui::MDIView* view) |
| 132 |
{ |
| 133 |
if (view == nullptr) |
| 134 |
return; |
| 135 |
|
| 136 |
currentView.pView3d = dynamic_cast<const Gui::View3DInventor*>(view); |
| 137 |
currentView.pView2d = nullptr; |
| 138 |
if (currentView.pView3d != nullptr) { |
| 139 |
const Gui::View3DInventorViewer* const inventorViewer = currentView.pView3d->getViewer(); |
| 140 |
if (inventorViewer == nullptr) |
| 141 |
return; |
| 142 |
|
| 143 |
auto pGroup = dynamic_cast<SoGroup* const>(inventorViewer->getSceneGraph()); |
| 144 |
if (pGroup == nullptr) |
| 145 |
return; |
| 146 |
|
| 147 |
if (pGroup->findChild(pivot.pVisibility) == -1) |
| 148 |
pGroup->addChild(pivot.pVisibility); |
| 149 |
|
| 150 |
navlib::box_t extents; |
| 151 |
navlib::matrix_t camera; |
| 152 |
|
| 153 |
GetModelExtents(extents); |
| 154 |
GetCameraMatrix(camera); |
| 155 |
|
| 156 |
Write(navlib::model_extents_k, extents); |
| 157 |
Write(navlib::view_affine_k, camera); |
| 158 |
|
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
for (auto viewinternal : view->findChildren<QGraphicsView*>()) { |
| 163 |
QList<QGraphicsView*> views = viewinternal->scene()->views(); |
| 164 |
for (QGraphicsView* view : views) { |
| 165 |
if (view->isActiveWindow()) { |
| 166 |
currentView.pView2d = view; |
| 167 |
return; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void NavlibInterface::enableNavigation() |
| 174 |
{ |
| 175 |
PutProfileHint("FreeCAD"); |
| 176 |
CNav3D::EnableNavigation(true, errorCode); |
| 177 |
if (errorCode) |
| 178 |
return; |
| 179 |
|
| 180 |
PutFrameTimingSource(TimingSource::SpaceMouse); |
| 181 |
|
| 182 |
Gui::Application::Instance->signalActivateView.connect( |
| 183 |
boost::bind(&NavlibInterface::onViewChanged, this, boost::placeholders::_1)); |
| 184 |
|
| 185 |
Gui::Application::Instance->signalActivateWorkbench.connect([this](const char* wb) { |
| 186 |
exportCommands(std::string(wb)); |
| 187 |
}); |
| 188 |
|
| 189 |
auto activeWorkbench = Gui::WorkbenchManager::instance()->activeName(); |
| 190 |
exportCommands(activeWorkbench); |
| 191 |
|
| 192 |
initializePivot(); |
| 193 |
connectActiveTab(); |
| 194 |
} |
| 195 |
|
| 196 |
void NavlibInterface::connectActiveTab() |
| 197 |
{ |
| 198 |
auto pQMdiArea = Gui::MainWindow::getInstance()->findChild<QMdiArea*>(); |
| 199 |
if (pQMdiArea == nullptr) |
| 200 |
return; |
| 201 |
|
| 202 |
auto pQTabBar = pQMdiArea->findChild<QTabBar*>(); |
| 203 |
if (pQTabBar == nullptr) |
| 204 |
return; |
| 205 |
|
| 206 |
pQTabBar->connect(pQTabBar, &QTabBar::currentChanged, [this, pQTabBar](int idx) { |
| 207 |
activeTab = {idx, idx >= 0 ? pQTabBar->tabText(idx).toStdString() : ""}; |
| 208 |
}); |
| 209 |
} |
| 210 |
|
| 211 |
void NavlibInterface::disableNavigation() |
| 212 |
{ |
| 213 |
CNav3D::EnableNavigation(false, errorCode); |
| 214 |
} |
| 215 |
|
| 216 |
long NavlibInterface::GetCameraMatrix(navlib::matrix_t& matrix) const |
| 217 |
{ |
| 218 |
if (activeTab.first == -1 || activeTab.second == "Start page") |
| 219 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 220 |
|
| 221 |
if (is3DView()) { |
| 222 |
auto pCamera = getCamera<SoCamera*>(); |
| 223 |
if (pCamera == nullptr) |
| 224 |
return navlib::make_result_code(navlib::navlib_errc::function_not_supported); |
| 225 |
|
| 226 |
SbMatrix cameraMatrix; |
| 227 |
|
| 228 |
pCamera->orientation.getValue().getValue(cameraMatrix); |
| 229 |
|
| 230 |
for (int i = 0; i < 4; i++) |
| 231 |
std::copy(cameraMatrix[i], cameraMatrix[i] + 4, &matrix.m00 + 4 * i); |
| 232 |
|
| 233 |
const SbVec3f position = pCamera->position.getValue(); |
| 234 |
std::copy(position.getValue(), position.getValue() + 3, &matrix.m30); |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
if (is2DView()) { |
| 239 |
QMatrix4x4 data; |
| 240 |
const QWidget* viewport = currentView.pView2d->viewport(); |
| 241 |
const QPointF viewportCenter(viewport->width() / 2.0, viewport->height() / 2.0); |
| 242 |
const QPointF scenePoint = currentView.pView2d->mapToScene(viewportCenter.toPoint()); |
| 243 |
|
| 244 |
// Only XY translations are considered for 2D view. The Z coordinate can be a constant value. |
| 245 |
data(0, 3) = scenePoint.x(); |
| 246 |
data(1, 3) = -scenePoint.y(); |
| 247 |
data(2, 3) = 0.0; |
| 248 |
|
| 249 |
std::copy(data.data(), data.data() + 16, &matrix.m00); |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
return navlib::make_result_code(navlib::navlib_errc::function_not_supported); |
| 254 |
} |
| 255 |
|
| 256 |
long NavlibInterface::SetCameraMatrix(const navlib::matrix_t& matrix) |
| 257 |
{ |
| 258 |
if (is2DView()) { |
| 259 |
QMatrix4x4 data; |
| 260 |
|
| 261 |
std::copy(&matrix.m00, &matrix.m33, data.data()); |
| 262 |
currentView.pView2d->centerOn(data(0, 3), -data(1, 3)); |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
if (is3DView()) { |
| 268 |
auto pCamera = getCamera<SoCamera*>(); |
| 269 |
if (pCamera == nullptr) |
| 270 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 271 |
|
| 272 |
SbMatrix cameraMatrix(matrix(0, 0), matrix(0, 1), matrix(0, 2), matrix(0, 3), |
| 273 |
matrix(1, 0), matrix(1, 1), matrix(1, 2), matrix(1, 3), |
| 274 |
matrix(2, 0), matrix(2, 1), matrix(2, 2), matrix(2, 3), |
| 275 |
matrix(3, 0), matrix(3, 1), matrix(3, 2), matrix(3, 3)); |
| 276 |
|
| 277 |
pCamera->orientation = SbRotation(cameraMatrix); |
| 278 |
pCamera->position.setValue(matrix(3, 0), matrix(3, 1), matrix(3, 2)); |
| 279 |
pCamera->touch(); |
| 280 |
|
| 281 |
return 0; |
| 282 |
} |
| 283 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 284 |
} |
| 285 |
|
| 286 |
long NavlibInterface::GetViewFrustum(navlib::frustum_t& frustum) const |
| 287 |
{ |
| 288 |
const auto pCamera = getCamera<SoPerspectiveCamera* const>(); |
| 289 |
if (pCamera == nullptr) |
| 290 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 291 |
|
| 292 |
const SbViewVolume viewVolume = pCamera->getViewVolume(pCamera->aspectRatio.getValue()); |
| 293 |
float halfHeight = viewVolume.getHeight() / 2.0f; |
| 294 |
float halfWidth = viewVolume.getWidth() / 2.0f; |
| 295 |
|
| 296 |
frustum = {-halfWidth, |
| 297 |
halfWidth, |
| 298 |
-halfHeight, |
| 299 |
halfHeight, |
| 300 |
viewVolume.getNearDist(), |
| 301 |
10.0f * (viewVolume.getNearDist() + viewVolume.nearToFar)}; |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
long NavlibInterface::SetViewFrustum(const navlib::frustum_t&) |
| 307 |
{ |
| 308 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 309 |
} |
| 310 |
|
| 311 |
long NavlibInterface::GetViewExtents(navlib::box_t& extents) const |
| 312 |
{ |
| 313 |
if (is2DView()) { |
| 314 |
const QRectF viewRectangle = currentView.pView2d->mapToScene( |
| 315 |
currentView.pView2d->viewport()->geometry()).boundingRect(); |
| 316 |
|
| 317 |
extents.min.x = viewRectangle.topLeft().x(); |
| 318 |
extents.min.y = viewRectangle.topLeft().y(); |
| 319 |
extents.max.x = viewRectangle.bottomRight().x(); |
| 320 |
extents.max.y = viewRectangle.bottomRight().y(); |
| 321 |
extents.min.z = -1; |
| 322 |
extents.max.z = 0; |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
const auto pCamera = getCamera<SoOrthographicCamera* const>(); |
| 328 |
if (pCamera == nullptr) |
| 329 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 330 |
|
| 331 |
const SbViewVolume viewVolume = pCamera->getViewVolume(pCamera->aspectRatio.getValue()); |
| 332 |
const double halfHeight = static_cast<double>(viewVolume.getHeight() / 2.0f); |
| 333 |
const double halfWidth = static_cast<double>(viewVolume.getWidth() / 2.0f); |
| 334 |
const double halfDepth = 1.0e8; |
| 335 |
|
| 336 |
extents = {{-halfWidth, |
| 337 |
-halfHeight, |
| 338 |
-halfDepth}, |
| 339 |
{halfWidth, |
| 340 |
halfHeight, |
| 341 |
halfDepth}}; |
| 342 |
|
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
long NavlibInterface::SetViewExtents(const navlib::box_t& extents) |
| 347 |
{ |
| 348 |
if (is2DView()) { |
| 349 |
const QRectF viewRectangle = currentView.pView2d->mapToScene( |
| 350 |
currentView.pView2d->viewport()->geometry()).boundingRect(); |
| 351 |
|
| 352 |
const float scaling = viewRectangle.height() / (extents.max.y - extents.min.y); |
| 353 |
QTransform transform = currentView.pView2d->transform(); |
| 354 |
|
| 355 |
transform.setMatrix(transform.m11() * scaling, |
| 356 |
transform.m12(), |
| 357 |
transform.m13(), |
| 358 |
transform.m21(), |
| 359 |
transform.m22() * scaling, |
| 360 |
transform.m23(), |
| 361 |
transform.m31(), |
| 362 |
transform.m32(), |
| 363 |
transform.m33()); |
| 364 |
|
| 365 |
currentView.pView2d->setTransform(transform); |
| 366 |
|
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
if (is3DView()) { |
| 371 |
auto pCamera = getCamera<SoOrthographicCamera* const>(); |
| 372 |
if (pCamera == nullptr) |
| 373 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 374 |
|
| 375 |
navlib::box_t oldExtents; |
| 376 |
GetViewExtents(oldExtents); |
| 377 |
|
| 378 |
pCamera->scaleHeight(extents.max.x / oldExtents.max.x); |
| 379 |
orthoNearDistance = pCamera->nearDistance.getValue(); |
| 380 |
|
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 385 |
} |
| 386 |
|
| 387 |
long NavlibInterface::GetViewFOV(double&) const |
| 388 |
{ |
| 389 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 390 |
} |
| 391 |
|
| 392 |
long NavlibInterface::SetViewFOV(double) |
| 393 |
{ |
| 394 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 395 |
} |
| 396 |
|
| 397 |
long NavlibInterface::GetIsViewPerspective(navlib::bool_t& perspective) const |
| 398 |
{ |
| 399 |
auto pPerspectiveCamera = getCamera<SoPerspectiveCamera* const>(); |
| 400 |
if (pPerspectiveCamera != nullptr) { |
| 401 |
perspective = true; |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
auto pOrthographicCamera = getCamera<SoOrthographicCamera* const>(); |
| 406 |
if (pOrthographicCamera != nullptr || is2DView()) { |
| 407 |
perspective = false; |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 412 |
} |
| 413 |
|
| 414 |
long NavlibInterface::GetModelExtents(navlib::box_t& extents) const |
| 415 |
{ |
| 416 |
if (is3DView()) { |
| 417 |
const Gui::View3DInventorViewer* const inventorViewer = currentView.pView3d->getViewer(); |
| 418 |
if (inventorViewer == nullptr) |
| 419 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 420 |
|
| 421 |
SoGetBoundingBoxAction action(inventorViewer->getSoRenderManager()->getViewportRegion()); |
| 422 |
|
| 423 |
action.apply(inventorViewer->getSceneGraph()); |
| 424 |
const SbBox3f boundingBox = action.getBoundingBox(); |
| 425 |
|
| 426 |
std::copy( |
| 427 |
boundingBox.getMin().getValue(), boundingBox.getMin().getValue() + 3, &extents.min.x); |
| 428 |
|
| 429 |
std::copy( |
| 430 |
boundingBox.getMax().getValue(), boundingBox.getMax().getValue() + 3, &extents.max.x); |
| 431 |
|
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
if (is2DView()) { |
| 436 |
const QRectF sceneExtents = currentView.pView2d->scene()->itemsBoundingRect(); |
| 437 |
|
| 438 |
extents.min.x = sceneExtents.topLeft().x(); |
| 439 |
extents.min.y = -sceneExtents.bottomRight().y(); |
| 440 |
extents.max.x = sceneExtents.bottomRight().x(); |
| 441 |
extents.max.y = -sceneExtents.topLeft().y(); |
| 442 |
extents.max.z = 0; |
| 443 |
extents.min.z = -1; |
| 444 |
|
| 445 |
return 0; |
| 446 |
} |
| 447 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 448 |
} |
| 449 |
|
| 450 |
long NavlibInterface::SetTransaction(long) |
| 451 |
{ |
| 452 |
return navlib::make_result_code(navlib::navlib_errc::no_data_available); |
| 453 |
} |
| 454 |
|
| 455 |
long NavlibInterface::GetFrontView(navlib::matrix_t& matrix) const |
| 456 |
{ |
| 457 |
matrix = {1., 0., 0., 0., |
| 458 |
0., 0., 1., 0., |
| 459 |
0., -1., 0., 0., |
| 460 |
0., 0., 0., 1.}; |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
long NavlibInterface::GetCoordinateSystem(navlib::matrix_t& matrix) const |
| 465 |
{ |
| 466 |
matrix = {1., 0., 0., 0., |
| 467 |
0., 0., -1., 0., |
| 468 |
0., 1., 0., 0., |
| 469 |
0., 0., 0., 1.}; |
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
long NavlibInterface::GetIsViewRotatable(navlib::bool_t& isRotatable) const |
| 474 |
{ |
| 475 |
isRotatable = is3DView(); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
long NavlibInterface::GetUnitsToMeters(double &units) const |
| 480 |
{ |
| 481 |
units = 0.001; |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
bool NavlibInterface::is3DView() const |
| 486 |
{ |
| 487 |
return currentView.pView3d != nullptr; |
| 488 |
} |
| 489 |
|
| 490 |
bool NavlibInterface::is2DView() const |
| 491 |
{ |
| 492 |
return currentView.pView2d != nullptr; |
| 493 |
} |