ctkVTKRenderView.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QTimer>
  16. #include <QVBoxLayout>
  17. #include <QDebug>
  18. // CTK includes
  19. #include "ctkVTKRenderView.h"
  20. #include "ctkVTKRenderView_p.h"
  21. #include "ctkLogger.h"
  22. // VTK includes
  23. #include <vtkRendererCollection.h>
  24. #include <vtkRenderWindowInteractor.h>
  25. #include <vtkTextProperty.h>
  26. #include <vtkCamera.h>
  27. //--------------------------------------------------------------------------
  28. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKRenderView");
  29. //--------------------------------------------------------------------------
  30. // --------------------------------------------------------------------------
  31. // ctkVTKRenderViewPrivate methods
  32. // --------------------------------------------------------------------------
  33. ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate(ctkVTKRenderView& object)
  34. :q_ptr(&object)
  35. {
  36. qRegisterMetaType<ctkAxesWidget::Axis>("ctkAxesWidget::Axis");
  37. this->Renderer = vtkSmartPointer<vtkRenderer>::New();
  38. this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  39. this->Axes = vtkSmartPointer<vtkAxesActor>::New();
  40. this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  41. this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  42. this->RenderPending = false;
  43. this->RenderEnabled = false;
  44. this->ZoomFactor = 0.05;
  45. this->PitchRollYawIncrement = 5;
  46. this->PitchDirection = ctkVTKRenderView::PitchUp;
  47. this->RollDirection = ctkVTKRenderView::RollRight;
  48. this->YawDirection = ctkVTKRenderView::YawLeft;
  49. this->SpinDirection = ctkVTKRenderView::YawRight;
  50. this->SpinEnabled = false;
  51. this->AnimationIntervalMs = 5;
  52. this->SpinIncrement = 2;
  53. this->RockEnabled = false;
  54. this->RockIncrement = 0;
  55. this->RockLength = 200;
  56. }
  57. // --------------------------------------------------------------------------
  58. void ctkVTKRenderViewPrivate::setupCornerAnnotation()
  59. {
  60. logger.trace("setupCornerAnnotation");
  61. if (!this->Renderer->HasViewProp(this->CornerAnnotation))
  62. {
  63. this->Renderer->AddViewProp(this->CornerAnnotation);
  64. this->CornerAnnotation->SetMaximumLineHeight(0.07);
  65. vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
  66. tprop->ShadowOn();
  67. }
  68. this->CornerAnnotation->ClearAllTexts();
  69. }
  70. //---------------------------------------------------------------------------
  71. void ctkVTKRenderViewPrivate::setupRendering()
  72. {
  73. logger.trace("setupRendering");
  74. Q_ASSERT(this->RenderWindow);
  75. this->RenderWindow->SetAlphaBitPlanes(1);
  76. this->RenderWindow->SetMultiSamples(0);
  77. this->RenderWindow->StereoCapableWindowOn();
  78. this->RenderWindow->GetRenderers()->RemoveAllItems();
  79. // Add renderer
  80. this->RenderWindow->AddRenderer(this->Renderer);
  81. // Setup the corner annotation
  82. this->setupCornerAnnotation();
  83. this->VTKWidget->SetRenderWindow(this->RenderWindow);
  84. }
  85. //---------------------------------------------------------------------------
  86. void ctkVTKRenderViewPrivate::setupDefaultInteractor()
  87. {
  88. logger.trace("setupDefaultInteractor");
  89. Q_Q(ctkVTKRenderView);
  90. q->setInteractor(this->RenderWindow->GetInteractor());
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkVTKRenderViewPrivate::zoom(double zoomFactor)
  94. {
  95. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  96. vtkCamera * camera = this->Renderer->GetActiveCamera();
  97. if (camera->GetParallelProjection())
  98. {
  99. camera->SetParallelScale(camera->GetParallelScale() / (1 + zoomFactor));
  100. }
  101. else
  102. {
  103. camera->Dolly(1 + zoomFactor);
  104. this->Renderer->ResetCameraClippingRange();
  105. this->Renderer->UpdateLightsGeometryToFollowCamera();
  106. }
  107. }
  108. //---------------------------------------------------------------------------
  109. void ctkVTKRenderViewPrivate::pitch(int rotateDegrees,
  110. ctkVTKRenderView::RotateDirection pitchDirection)
  111. {
  112. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  113. Q_ASSERT(rotateDegrees >= 0);
  114. vtkCamera *cam = this->Renderer->GetActiveCamera();
  115. cam->Elevation(pitchDirection == ctkVTKRenderView::PitchDown ? rotateDegrees : -rotateDegrees);
  116. cam->OrthogonalizeViewUp();
  117. this->Renderer->UpdateLightsGeometryToFollowCamera();
  118. }
  119. //---------------------------------------------------------------------------
  120. void ctkVTKRenderViewPrivate::roll(int rotateDegrees,
  121. ctkVTKRenderView::RotateDirection rollDirection)
  122. {
  123. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  124. Q_ASSERT(rotateDegrees >= 0);
  125. vtkCamera *cam = this->Renderer->GetActiveCamera();
  126. cam->Roll(rollDirection == ctkVTKRenderView::RollLeft ? rotateDegrees : -rotateDegrees);
  127. cam->OrthogonalizeViewUp();
  128. this->Renderer->UpdateLightsGeometryToFollowCamera();
  129. }
  130. //---------------------------------------------------------------------------
  131. void ctkVTKRenderViewPrivate::yaw(int rotateDegrees,
  132. ctkVTKRenderView::RotateDirection yawDirection)
  133. {
  134. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  135. Q_ASSERT(rotateDegrees >= 0);
  136. vtkCamera *cam = this->Renderer->GetActiveCamera();
  137. cam->Azimuth(yawDirection == ctkVTKRenderView::YawLeft ? rotateDegrees : -rotateDegrees);
  138. cam->OrthogonalizeViewUp();
  139. this->Renderer->UpdateLightsGeometryToFollowCamera();
  140. }
  141. //---------------------------------------------------------------------------
  142. void ctkVTKRenderViewPrivate::doSpin()
  143. {
  144. if (!this->SpinEnabled)
  145. {
  146. return;
  147. }
  148. switch (this->SpinDirection)
  149. {
  150. case ctkVTKRenderView::PitchUp:
  151. case ctkVTKRenderView::PitchDown:
  152. this->pitch(this->SpinIncrement, this->SpinDirection);
  153. break;
  154. case ctkVTKRenderView::RollLeft:
  155. case ctkVTKRenderView::RollRight:
  156. this->roll(this->SpinIncrement, this->SpinDirection);
  157. break;
  158. case ctkVTKRenderView::YawLeft:
  159. case ctkVTKRenderView::YawRight:
  160. this->yaw(this->SpinIncrement, this->SpinDirection);
  161. break;
  162. }
  163. QTimer::singleShot(this->AnimationIntervalMs, this, SLOT(doSpin()));
  164. }
  165. //---------------------------------------------------------------------------
  166. void ctkVTKRenderViewPrivate::doRock()
  167. {
  168. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  169. if (!this->RockEnabled)
  170. {
  171. return;
  172. }
  173. vtkCamera *camera = this->Renderer->GetActiveCamera();
  174. double frac = static_cast<double>(this->RockIncrement) / static_cast<double>(this->RockLength);
  175. double az = 1.5 * cos (2.0 * 3.1415926 * (frac - floor(frac)));
  176. this->RockIncrement = 1 + this->RockIncrement;
  177. this->RockIncrement = this->RockIncrement % this->RockLength;
  178. // Move the camera
  179. camera->Azimuth(az);
  180. camera->OrthogonalizeViewUp();
  181. //Make the lighting follow the camera to avoid illumination changes
  182. this->Renderer->UpdateLightsGeometryToFollowCamera();
  183. QTimer::singleShot(this->AnimationIntervalMs, this, SLOT(doRock()));
  184. }
  185. //---------------------------------------------------------------------------
  186. // ctkVTKRenderView methods
  187. // --------------------------------------------------------------------------
  188. ctkVTKRenderView::ctkVTKRenderView(QWidget* _parent) : Superclass(_parent)
  189. , d_ptr(new ctkVTKRenderViewPrivate(*this))
  190. {
  191. Q_D(ctkVTKRenderView);
  192. d->VTKWidget = new QVTKWidget(this);
  193. this->setLayout(new QVBoxLayout);
  194. this->layout()->setMargin(0);
  195. this->layout()->setSpacing(0);
  196. this->layout()->addWidget(d->VTKWidget);
  197. d->setupRendering();
  198. d->setupDefaultInteractor();
  199. }
  200. //----------------------------------------------------------------------------
  201. ctkVTKRenderView::~ctkVTKRenderView()
  202. {
  203. }
  204. //----------------------------------------------------------------------------
  205. void ctkVTKRenderView::scheduleRender()
  206. {
  207. Q_D(ctkVTKRenderView);
  208. logger.trace(QString("scheduleRender - RenderEnabled: %1 - RenderPending: %2").
  209. arg(d->RenderEnabled ? "true" : "false")
  210. .arg(d->RenderPending ? "true:" : "false"));
  211. if (!d->RenderEnabled)
  212. {
  213. return;
  214. }
  215. if (!d->RenderPending)
  216. {
  217. d->RenderPending = true;
  218. QTimer::singleShot(0, this, SLOT(forceRender()));
  219. }
  220. }
  221. //----------------------------------------------------------------------------
  222. void ctkVTKRenderView::forceRender()
  223. {
  224. Q_D(ctkVTKRenderView);
  225. logger.trace(QString("forceRender - RenderEnabled: %1")
  226. .arg(d->RenderEnabled ? "true" : "false"));
  227. if (!d->RenderEnabled || !this->isVisible())
  228. {
  229. return;
  230. }
  231. d->RenderWindow->Render();
  232. d->RenderPending = false;
  233. }
  234. //----------------------------------------------------------------------------
  235. CTK_GET_CPP(ctkVTKRenderView, vtkRenderWindow*, renderWindow, RenderWindow);
  236. //----------------------------------------------------------------------------
  237. CTK_GET_CPP(ctkVTKRenderView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
  238. //----------------------------------------------------------------------------
  239. void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  240. {
  241. Q_D(ctkVTKRenderView);
  242. logger.trace("setInteractor");
  243. d->RenderWindow->SetInteractor(newInteractor);
  244. d->Orientation->SetOrientationMarker(d->Axes);
  245. d->Orientation->SetInteractor(newInteractor);
  246. d->Orientation->SetEnabled(1);
  247. d->Orientation->InteractiveOff();
  248. d->CurrentInteractor = newInteractor;
  249. }
  250. //----------------------------------------------------------------------------
  251. vtkInteractorObserver* ctkVTKRenderView::interactorStyle()
  252. {
  253. Q_D(ctkVTKRenderView);
  254. if (d->CurrentInteractor)
  255. {
  256. return d->CurrentInteractor->GetInteractorStyle();
  257. }
  258. else
  259. {
  260. return 0;
  261. }
  262. }
  263. //----------------------------------------------------------------------------
  264. void ctkVTKRenderView::setCornerAnnotationText(const QString& text)
  265. {
  266. Q_D(ctkVTKRenderView);
  267. logger.trace(QString("setCornerAnnotationText: %1").arg(text));
  268. d->CornerAnnotation->ClearAllTexts();
  269. d->CornerAnnotation->SetText(2, text.toLatin1());
  270. }
  271. //----------------------------------------------------------------------------
  272. QString ctkVTKRenderView::cornerAnnotationText() const
  273. {
  274. Q_D(const ctkVTKRenderView);
  275. return QLatin1String(d->CornerAnnotation->GetText(2));
  276. }
  277. // --------------------------------------------------------------------------
  278. void ctkVTKRenderView::setBackgroundColor(const QColor& newBackgroundColor)
  279. {
  280. Q_D(ctkVTKRenderView);
  281. logger.trace(QString("setBackgroundColor: %1").arg(newBackgroundColor.name()));
  282. d->Renderer->SetBackground(newBackgroundColor.redF(),
  283. newBackgroundColor.greenF(),
  284. newBackgroundColor.blueF());
  285. }
  286. //----------------------------------------------------------------------------
  287. QColor ctkVTKRenderView::backgroundColor() const
  288. {
  289. Q_D(const ctkVTKRenderView);
  290. double color[3] = {0, 0, 0};
  291. d->Renderer->GetBackground(color);
  292. return QColor::fromRgbF(color[0], color[1], color[2]);
  293. }
  294. //----------------------------------------------------------------------------
  295. void ctkVTKRenderView::setOrientationWidgetVisible(bool visible)
  296. {
  297. Q_D(ctkVTKRenderView);
  298. d->Orientation->SetEnabled(visible);
  299. }
  300. //----------------------------------------------------------------------------
  301. bool ctkVTKRenderView::orientationWidgetVisible()
  302. {
  303. Q_D(ctkVTKRenderView);
  304. return d->Orientation->GetEnabled();
  305. }
  306. //----------------------------------------------------------------------------
  307. vtkCamera* ctkVTKRenderView::activeCamera()
  308. {
  309. Q_D(ctkVTKRenderView);
  310. if (d->Renderer->IsActiveCameraCreated())
  311. {
  312. return d->Renderer->GetActiveCamera();
  313. }
  314. else
  315. {
  316. return 0;
  317. }
  318. }
  319. //----------------------------------------------------------------------------
  320. void ctkVTKRenderView::resetCamera()
  321. {
  322. Q_D(ctkVTKRenderView);
  323. logger.trace("resetCamera");
  324. d->Renderer->ResetCamera();
  325. }
  326. //----------------------------------------------------------------------------
  327. CTK_GET_CPP(ctkVTKRenderView, vtkRenderer*, renderer, Renderer);
  328. //----------------------------------------------------------------------------
  329. CTK_SET_CPP(ctkVTKRenderView, bool, setRenderEnabled, RenderEnabled);
  330. CTK_GET_CPP(ctkVTKRenderView, bool, renderEnabled, RenderEnabled);
  331. //----------------------------------------------------------------------------
  332. CTK_GET_CPP(ctkVTKRenderView, int, pitchRollYawIncrement, PitchRollYawIncrement);
  333. //----------------------------------------------------------------------------
  334. void ctkVTKRenderView::setPitchRollYawIncrement(int newPitchRollYawIncrement)
  335. {
  336. Q_D(ctkVTKRenderView);
  337. d->PitchRollYawIncrement = qAbs(newPitchRollYawIncrement);
  338. }
  339. //----------------------------------------------------------------------------
  340. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, pitchDirection, PitchDirection);
  341. //----------------------------------------------------------------------------
  342. void ctkVTKRenderView::setPitchDirection(ctkVTKRenderView::RotateDirection newPitchDirection)
  343. {
  344. Q_D(ctkVTKRenderView);
  345. if (newPitchDirection != ctkVTKRenderView::PitchUp &&
  346. newPitchDirection != ctkVTKRenderView::PitchDown)
  347. {
  348. return;
  349. }
  350. d->PitchDirection = newPitchDirection;
  351. }
  352. //----------------------------------------------------------------------------
  353. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, rollDirection, RollDirection);
  354. //----------------------------------------------------------------------------
  355. void ctkVTKRenderView::setRollDirection(ctkVTKRenderView::RotateDirection newRollDirection)
  356. {
  357. Q_D(ctkVTKRenderView);
  358. if (newRollDirection != ctkVTKRenderView::RollLeft &&
  359. newRollDirection != ctkVTKRenderView::RollRight)
  360. {
  361. return;
  362. }
  363. d->RollDirection = newRollDirection;
  364. }
  365. //----------------------------------------------------------------------------
  366. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, yawDirection, YawDirection);
  367. //----------------------------------------------------------------------------
  368. void ctkVTKRenderView::setYawDirection(ctkVTKRenderView::RotateDirection newYawDirection)
  369. {
  370. Q_D(ctkVTKRenderView);
  371. if (newYawDirection != ctkVTKRenderView::YawLeft &&
  372. newYawDirection != ctkVTKRenderView::YawRight)
  373. {
  374. return;
  375. }
  376. d->YawDirection = newYawDirection;
  377. }
  378. //----------------------------------------------------------------------------
  379. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, spinDirection, SpinDirection);
  380. CTK_SET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, setSpinDirection, SpinDirection);
  381. //----------------------------------------------------------------------------
  382. void ctkVTKRenderView::pitch()
  383. {
  384. Q_D(ctkVTKRenderView);
  385. if (!d->Renderer->IsActiveCameraCreated())
  386. {
  387. return;
  388. }
  389. d->pitch(d->PitchRollYawIncrement, d->PitchDirection);
  390. }
  391. //----------------------------------------------------------------------------
  392. void ctkVTKRenderView::roll()
  393. {
  394. Q_D(ctkVTKRenderView);
  395. if (!d->Renderer->IsActiveCameraCreated())
  396. {
  397. return;
  398. }
  399. d->roll(d->PitchRollYawIncrement, d->RollDirection);
  400. }
  401. //----------------------------------------------------------------------------
  402. void ctkVTKRenderView::yaw()
  403. {
  404. Q_D(ctkVTKRenderView);
  405. if (!d->Renderer->IsActiveCameraCreated())
  406. {
  407. return;
  408. }
  409. d->yaw(d->PitchRollYawIncrement, d->YawDirection);
  410. }
  411. //----------------------------------------------------------------------------
  412. void ctkVTKRenderView::setSpinEnabled(bool enabled)
  413. {
  414. Q_D(ctkVTKRenderView);
  415. if (enabled == d->SpinEnabled)
  416. {
  417. return;
  418. }
  419. d->SpinEnabled = enabled;
  420. d->RockEnabled = false;
  421. QTimer::singleShot(0, d, SLOT(doSpin()));
  422. }
  423. //----------------------------------------------------------------------------
  424. CTK_GET_CPP(ctkVTKRenderView, bool, spinEnabled, SpinEnabled);
  425. //----------------------------------------------------------------------------
  426. void ctkVTKRenderView::setSpinIncrement(int newSpinIncrement)
  427. {
  428. Q_D(ctkVTKRenderView);
  429. d->SpinIncrement = qAbs(newSpinIncrement);
  430. }
  431. //----------------------------------------------------------------------------
  432. CTK_GET_CPP(ctkVTKRenderView, int, spinIncrement, SpinIncrement);
  433. //----------------------------------------------------------------------------
  434. void ctkVTKRenderView::setAnimationIntervalMs(int newAnimationIntervalMs)
  435. {
  436. Q_D(ctkVTKRenderView);
  437. d->AnimationIntervalMs = qAbs(newAnimationIntervalMs);
  438. }
  439. //----------------------------------------------------------------------------
  440. CTK_GET_CPP(ctkVTKRenderView, int, animationIntervalMs, AnimationIntervalMs);
  441. //----------------------------------------------------------------------------
  442. void ctkVTKRenderView::setRockEnabled(bool enabled)
  443. {
  444. Q_D(ctkVTKRenderView);
  445. if (enabled == d->RockEnabled)
  446. {
  447. return;
  448. }
  449. d->RockEnabled = enabled;
  450. d->SpinEnabled = false;
  451. QTimer::singleShot(0, d, SLOT(doRock()));
  452. }
  453. //----------------------------------------------------------------------------
  454. CTK_GET_CPP(ctkVTKRenderView, bool, rockEnabled, RockEnabled);
  455. //----------------------------------------------------------------------------
  456. void ctkVTKRenderView::setRockLength(int newRockLength)
  457. {
  458. Q_D(ctkVTKRenderView);
  459. d->RockLength = qAbs(newRockLength);
  460. }
  461. //----------------------------------------------------------------------------
  462. CTK_GET_CPP(ctkVTKRenderView, int, rockLength, RockLength);
  463. //----------------------------------------------------------------------------
  464. void ctkVTKRenderView::setRockIncrement(int newRockIncrement)
  465. {
  466. Q_D(ctkVTKRenderView);
  467. d->RockIncrement = qAbs(newRockIncrement);
  468. }
  469. //----------------------------------------------------------------------------
  470. CTK_GET_CPP(ctkVTKRenderView, int, rockIncrement, RockIncrement);
  471. //----------------------------------------------------------------------------
  472. void ctkVTKRenderView::setZoomFactor(double newZoomFactor)
  473. {
  474. Q_D(ctkVTKRenderView);
  475. d->ZoomFactor = qBound(0.0, qAbs(newZoomFactor), 1.0);
  476. }
  477. //----------------------------------------------------------------------------
  478. CTK_GET_CPP(ctkVTKRenderView, double, zoomFactor, ZoomFactor);
  479. //----------------------------------------------------------------------------
  480. void ctkVTKRenderView::zoomIn()
  481. {
  482. Q_D(ctkVTKRenderView);
  483. if (!d->Renderer->IsActiveCameraCreated())
  484. {
  485. return;
  486. }
  487. d->zoom(d->ZoomFactor);
  488. }
  489. //----------------------------------------------------------------------------
  490. void ctkVTKRenderView::zoomOut()
  491. {
  492. Q_D(ctkVTKRenderView);
  493. if (!d->Renderer->IsActiveCameraCreated())
  494. {
  495. return;
  496. }
  497. d->zoom(-d->ZoomFactor);
  498. }
  499. //----------------------------------------------------------------------------
  500. void ctkVTKRenderView::setFocalPoint(int x, int y, int z)
  501. {
  502. Q_D(ctkVTKRenderView);
  503. if (!d->Renderer->IsActiveCameraCreated())
  504. {
  505. return;
  506. }
  507. vtkCamera * camera = d->Renderer->GetActiveCamera();
  508. camera->SetFocalPoint(x, y, z);
  509. camera->ComputeViewPlaneNormal();
  510. camera->OrthogonalizeViewUp();
  511. d->Renderer->UpdateLightsGeometryToFollowCamera();
  512. }
  513. //----------------------------------------------------------------------------
  514. void ctkVTKRenderView::resetFocalPoint()
  515. {
  516. Q_D(ctkVTKRenderView);
  517. double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  518. d->Renderer->ComputeVisiblePropBounds(bounds);
  519. double x_center = (bounds[1] + bounds[0]) / 2.0;
  520. double y_center = (bounds[3] + bounds[2]) / 2.0;
  521. double z_center = (bounds[5] + bounds[4]) / 2.0;
  522. this->setFocalPoint(x_center, y_center, z_center);
  523. }
  524. //----------------------------------------------------------------------------
  525. void ctkVTKRenderView::lookFromAxis(const ctkAxesWidget::Axis& axis, double fov)
  526. {
  527. Q_D(ctkVTKRenderView);
  528. Q_ASSERT(d->Renderer);
  529. if (!d->Renderer->IsActiveCameraCreated())
  530. {
  531. return;
  532. }
  533. vtkCamera * camera = d->Renderer->GetActiveCamera();
  534. Q_ASSERT(camera);
  535. double widefov = fov*3;
  536. double* focalPoint = camera->GetFocalPoint();
  537. switch (axis)
  538. {
  539. case ctkAxesWidget::Right:
  540. camera->SetPosition(focalPoint[0]+widefov, focalPoint[1], focalPoint[2]);
  541. camera->SetViewUp(0, 0, 1);
  542. break;
  543. case ctkAxesWidget::Left:
  544. camera->SetPosition(focalPoint[0]-widefov, focalPoint[1], focalPoint[2]);
  545. camera->SetViewUp(0, 0, 1);
  546. break;
  547. case ctkAxesWidget::Anterior:
  548. camera->SetPosition(focalPoint[0], focalPoint[1]+widefov, focalPoint[2]);
  549. camera->SetViewUp(0, 0, 1);
  550. break;
  551. case ctkAxesWidget::Posterior:
  552. camera->SetPosition(focalPoint[0], focalPoint[1]-widefov, focalPoint[2]);
  553. camera->SetViewUp(0, 0, 1);
  554. break;
  555. case ctkAxesWidget::Superior:
  556. camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]+widefov);
  557. camera->SetViewUp(0, 1, 0);
  558. break;
  559. case ctkAxesWidget::Inferior:
  560. camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]-widefov);
  561. camera->SetViewUp(0, 1, 0);
  562. break;
  563. case ctkAxesWidget::None:
  564. default:
  565. // do nothing
  566. return;
  567. break;
  568. }
  569. d->Renderer->ResetCameraClippingRange();
  570. camera->ComputeViewPlaneNormal();
  571. camera->OrthogonalizeViewUp();
  572. d->Renderer->UpdateLightsGeometryToFollowCamera();
  573. }