ctkVTKRenderView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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.apache.org/licenses/LICENSE-2.0.txt
  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. // VTK includes
  22. #include <vtkRendererCollection.h>
  23. #include <vtkRenderWindowInteractor.h>
  24. #include <vtkTextProperty.h>
  25. #include <vtkCamera.h>
  26. // --------------------------------------------------------------------------
  27. // ctkVTKRenderViewPrivate methods
  28. // --------------------------------------------------------------------------
  29. ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate(ctkVTKRenderView& object)
  30. :ctkVTKAbstractViewPrivate(object)
  31. {
  32. qRegisterMetaType<ctkAxesWidget::Axis>("ctkAxesWidget::Axis");
  33. this->Renderer = vtkSmartPointer<vtkRenderer>::New();
  34. this->Axes = vtkSmartPointer<vtkAxesActor>::New();
  35. this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  36. this->ZoomFactor = 0.05;
  37. this->PitchRollYawIncrement = 5;
  38. this->PitchDirection = ctkVTKRenderView::PitchUp;
  39. this->RollDirection = ctkVTKRenderView::RollRight;
  40. this->YawDirection = ctkVTKRenderView::YawLeft;
  41. this->SpinDirection = ctkVTKRenderView::YawRight;
  42. this->SpinEnabled = false;
  43. this->AnimationIntervalMs = 5;
  44. this->SpinIncrement = 2;
  45. this->RockEnabled = false;
  46. this->RockIncrement = 0;
  47. this->RockLength = 200;
  48. this->Orientation->SetOrientationMarker(this->Axes);
  49. }
  50. // --------------------------------------------------------------------------
  51. void ctkVTKRenderViewPrivate::setupCornerAnnotation()
  52. {
  53. this->ctkVTKAbstractViewPrivate::setupCornerAnnotation();
  54. if (!this->Renderer->HasViewProp(this->CornerAnnotation))
  55. {
  56. this->Renderer->AddViewProp(this->CornerAnnotation);
  57. }
  58. }
  59. //---------------------------------------------------------------------------
  60. void ctkVTKRenderViewPrivate::setupRendering()
  61. {
  62. // Add renderer
  63. this->RenderWindow->AddRenderer(this->Renderer);
  64. this->ctkVTKAbstractViewPrivate::setupRendering();
  65. // The interactor in RenderWindow exists after the renderwindow is set to
  66. // the QVTKWidet
  67. this->Orientation->SetInteractor(this->RenderWindow->GetInteractor());
  68. this->Orientation->SetEnabled(1);
  69. this->Orientation->InteractiveOff();
  70. }
  71. //----------------------------------------------------------------------------
  72. void ctkVTKRenderViewPrivate::zoom(double zoomFactor)
  73. {
  74. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  75. vtkCamera * camera = this->Renderer->GetActiveCamera();
  76. if (camera->GetParallelProjection())
  77. {
  78. camera->SetParallelScale(camera->GetParallelScale() / (1 + zoomFactor));
  79. }
  80. else
  81. {
  82. camera->Dolly(1 + zoomFactor);
  83. this->Renderer->ResetCameraClippingRange();
  84. this->Renderer->UpdateLightsGeometryToFollowCamera();
  85. }
  86. }
  87. //---------------------------------------------------------------------------
  88. void ctkVTKRenderViewPrivate::pitch(int rotateDegrees,
  89. ctkVTKRenderView::RotateDirection pitchDirection)
  90. {
  91. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  92. Q_ASSERT(rotateDegrees >= 0);
  93. vtkCamera *cam = this->Renderer->GetActiveCamera();
  94. cam->Elevation(pitchDirection == ctkVTKRenderView::PitchDown ? rotateDegrees : -rotateDegrees);
  95. cam->OrthogonalizeViewUp();
  96. this->Renderer->UpdateLightsGeometryToFollowCamera();
  97. }
  98. //---------------------------------------------------------------------------
  99. void ctkVTKRenderViewPrivate::roll(int rotateDegrees,
  100. ctkVTKRenderView::RotateDirection rollDirection)
  101. {
  102. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  103. Q_ASSERT(rotateDegrees >= 0);
  104. vtkCamera *cam = this->Renderer->GetActiveCamera();
  105. cam->Roll(rollDirection == ctkVTKRenderView::RollLeft ? rotateDegrees : -rotateDegrees);
  106. cam->OrthogonalizeViewUp();
  107. this->Renderer->UpdateLightsGeometryToFollowCamera();
  108. }
  109. //---------------------------------------------------------------------------
  110. void ctkVTKRenderViewPrivate::yaw(int rotateDegrees,
  111. ctkVTKRenderView::RotateDirection yawDirection)
  112. {
  113. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  114. Q_ASSERT(rotateDegrees >= 0);
  115. vtkCamera *cam = this->Renderer->GetActiveCamera();
  116. cam->Azimuth(yawDirection == ctkVTKRenderView::YawLeft ? rotateDegrees : -rotateDegrees);
  117. cam->OrthogonalizeViewUp();
  118. this->Renderer->UpdateLightsGeometryToFollowCamera();
  119. }
  120. //---------------------------------------------------------------------------
  121. void ctkVTKRenderViewPrivate::doSpin()
  122. {
  123. Q_Q(ctkVTKRenderView);
  124. if (!this->SpinEnabled)
  125. {
  126. return;
  127. }
  128. switch (this->SpinDirection)
  129. {
  130. case ctkVTKRenderView::PitchUp:
  131. case ctkVTKRenderView::PitchDown:
  132. this->pitch(this->SpinIncrement, this->SpinDirection);
  133. break;
  134. case ctkVTKRenderView::RollLeft:
  135. case ctkVTKRenderView::RollRight:
  136. this->roll(this->SpinIncrement, this->SpinDirection);
  137. break;
  138. case ctkVTKRenderView::YawLeft:
  139. case ctkVTKRenderView::YawRight:
  140. this->yaw(this->SpinIncrement, this->SpinDirection);
  141. break;
  142. }
  143. q->forceRender();
  144. QTimer::singleShot(this->AnimationIntervalMs, this, SLOT(doSpin()));
  145. }
  146. //---------------------------------------------------------------------------
  147. void ctkVTKRenderViewPrivate::doRock()
  148. {
  149. Q_Q(ctkVTKRenderView);
  150. Q_ASSERT(this->Renderer->IsActiveCameraCreated());
  151. if (!this->RockEnabled)
  152. {
  153. return;
  154. }
  155. vtkCamera *camera = this->Renderer->GetActiveCamera();
  156. double frac = static_cast<double>(this->RockIncrement) / static_cast<double>(this->RockLength);
  157. double az = 1.5 * cos (2.0 * 3.1415926 * (frac - floor(frac)));
  158. this->RockIncrement = 1 + this->RockIncrement;
  159. this->RockIncrement = this->RockIncrement % this->RockLength;
  160. // Move the camera
  161. camera->Azimuth(az);
  162. camera->OrthogonalizeViewUp();
  163. //Make the lighting follow the camera to avoid illumination changes
  164. this->Renderer->UpdateLightsGeometryToFollowCamera();
  165. q->forceRender();
  166. QTimer::singleShot(this->AnimationIntervalMs, this, SLOT(doRock()));
  167. }
  168. //---------------------------------------------------------------------------
  169. // ctkVTKRenderView methods
  170. // --------------------------------------------------------------------------
  171. ctkVTKRenderView::ctkVTKRenderView(QWidget* parentWidget)
  172. : Superclass(new ctkVTKRenderViewPrivate(*this), parentWidget)
  173. {
  174. Q_D(ctkVTKRenderView);
  175. d->init();
  176. }
  177. //----------------------------------------------------------------------------
  178. ctkVTKRenderView::~ctkVTKRenderView()
  179. {
  180. }
  181. //----------------------------------------------------------------------------
  182. void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  183. {
  184. Q_D(ctkVTKRenderView);
  185. this->Superclass::setInteractor(newInteractor);
  186. d->Orientation->SetInteractor(newInteractor);
  187. }
  188. //----------------------------------------------------------------------------
  189. void ctkVTKRenderView::setOrientationWidgetVisible(bool visible)
  190. {
  191. Q_D(ctkVTKRenderView);
  192. d->Orientation->SetEnabled(visible);
  193. }
  194. //----------------------------------------------------------------------------
  195. bool ctkVTKRenderView::orientationWidgetVisible()
  196. {
  197. Q_D(ctkVTKRenderView);
  198. return d->Orientation->GetEnabled();
  199. }
  200. //----------------------------------------------------------------------------
  201. vtkCamera* ctkVTKRenderView::activeCamera()
  202. {
  203. Q_D(ctkVTKRenderView);
  204. if (d->Renderer->IsActiveCameraCreated())
  205. {
  206. return d->Renderer->GetActiveCamera();
  207. }
  208. else
  209. {
  210. return 0;
  211. }
  212. }
  213. //----------------------------------------------------------------------------
  214. void ctkVTKRenderView::resetCamera()
  215. {
  216. Q_D(ctkVTKRenderView);
  217. d->Renderer->ResetCamera();
  218. }
  219. //----------------------------------------------------------------------------
  220. CTK_GET_CPP(ctkVTKRenderView, vtkRenderer*, renderer, Renderer);
  221. //----------------------------------------------------------------------------
  222. CTK_GET_CPP(ctkVTKRenderView, int, pitchRollYawIncrement, PitchRollYawIncrement);
  223. //----------------------------------------------------------------------------
  224. void ctkVTKRenderView::setPitchRollYawIncrement(int newPitchRollYawIncrement)
  225. {
  226. Q_D(ctkVTKRenderView);
  227. d->PitchRollYawIncrement = qAbs(newPitchRollYawIncrement);
  228. }
  229. //----------------------------------------------------------------------------
  230. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, pitchDirection, PitchDirection);
  231. //----------------------------------------------------------------------------
  232. void ctkVTKRenderView::setPitchDirection(ctkVTKRenderView::RotateDirection newPitchDirection)
  233. {
  234. Q_D(ctkVTKRenderView);
  235. if (newPitchDirection != ctkVTKRenderView::PitchUp &&
  236. newPitchDirection != ctkVTKRenderView::PitchDown)
  237. {
  238. return;
  239. }
  240. d->PitchDirection = newPitchDirection;
  241. }
  242. //----------------------------------------------------------------------------
  243. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, rollDirection, RollDirection);
  244. //----------------------------------------------------------------------------
  245. void ctkVTKRenderView::setRollDirection(ctkVTKRenderView::RotateDirection newRollDirection)
  246. {
  247. Q_D(ctkVTKRenderView);
  248. if (newRollDirection != ctkVTKRenderView::RollLeft &&
  249. newRollDirection != ctkVTKRenderView::RollRight)
  250. {
  251. return;
  252. }
  253. d->RollDirection = newRollDirection;
  254. }
  255. //----------------------------------------------------------------------------
  256. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, yawDirection, YawDirection);
  257. //----------------------------------------------------------------------------
  258. void ctkVTKRenderView::setYawDirection(ctkVTKRenderView::RotateDirection newYawDirection)
  259. {
  260. Q_D(ctkVTKRenderView);
  261. if (newYawDirection != ctkVTKRenderView::YawLeft &&
  262. newYawDirection != ctkVTKRenderView::YawRight)
  263. {
  264. return;
  265. }
  266. d->YawDirection = newYawDirection;
  267. }
  268. //----------------------------------------------------------------------------
  269. CTK_GET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, spinDirection, SpinDirection);
  270. CTK_SET_CPP(ctkVTKRenderView, ctkVTKRenderView::RotateDirection, setSpinDirection, SpinDirection);
  271. //----------------------------------------------------------------------------
  272. void ctkVTKRenderView::pitch()
  273. {
  274. Q_D(ctkVTKRenderView);
  275. if (!d->Renderer->IsActiveCameraCreated())
  276. {
  277. return;
  278. }
  279. d->pitch(d->PitchRollYawIncrement, d->PitchDirection);
  280. }
  281. //----------------------------------------------------------------------------
  282. void ctkVTKRenderView::roll()
  283. {
  284. Q_D(ctkVTKRenderView);
  285. if (!d->Renderer->IsActiveCameraCreated())
  286. {
  287. return;
  288. }
  289. d->roll(d->PitchRollYawIncrement, d->RollDirection);
  290. }
  291. //----------------------------------------------------------------------------
  292. void ctkVTKRenderView::yaw()
  293. {
  294. Q_D(ctkVTKRenderView);
  295. if (!d->Renderer->IsActiveCameraCreated())
  296. {
  297. return;
  298. }
  299. d->yaw(d->PitchRollYawIncrement, d->YawDirection);
  300. }
  301. //----------------------------------------------------------------------------
  302. void ctkVTKRenderView::setSpinEnabled(bool enabled)
  303. {
  304. Q_D(ctkVTKRenderView);
  305. if (enabled == d->SpinEnabled)
  306. {
  307. return;
  308. }
  309. d->SpinEnabled = enabled;
  310. d->RockEnabled = false;
  311. QTimer::singleShot(0, d, SLOT(doSpin()));
  312. }
  313. //----------------------------------------------------------------------------
  314. CTK_GET_CPP(ctkVTKRenderView, bool, spinEnabled, SpinEnabled);
  315. //----------------------------------------------------------------------------
  316. void ctkVTKRenderView::setSpinIncrement(int newSpinIncrement)
  317. {
  318. Q_D(ctkVTKRenderView);
  319. d->SpinIncrement = qAbs(newSpinIncrement);
  320. }
  321. //----------------------------------------------------------------------------
  322. CTK_GET_CPP(ctkVTKRenderView, int, spinIncrement, SpinIncrement);
  323. //----------------------------------------------------------------------------
  324. void ctkVTKRenderView::setAnimationIntervalMs(int newAnimationIntervalMs)
  325. {
  326. Q_D(ctkVTKRenderView);
  327. d->AnimationIntervalMs = qAbs(newAnimationIntervalMs);
  328. }
  329. //----------------------------------------------------------------------------
  330. CTK_GET_CPP(ctkVTKRenderView, int, animationIntervalMs, AnimationIntervalMs);
  331. //----------------------------------------------------------------------------
  332. void ctkVTKRenderView::setRockEnabled(bool enabled)
  333. {
  334. Q_D(ctkVTKRenderView);
  335. if (enabled == d->RockEnabled)
  336. {
  337. return;
  338. }
  339. d->RockEnabled = enabled;
  340. d->SpinEnabled = false;
  341. QTimer::singleShot(0, d, SLOT(doRock()));
  342. }
  343. //----------------------------------------------------------------------------
  344. CTK_GET_CPP(ctkVTKRenderView, bool, rockEnabled, RockEnabled);
  345. //----------------------------------------------------------------------------
  346. void ctkVTKRenderView::setRockLength(int newRockLength)
  347. {
  348. Q_D(ctkVTKRenderView);
  349. d->RockLength = qAbs(newRockLength);
  350. }
  351. //----------------------------------------------------------------------------
  352. CTK_GET_CPP(ctkVTKRenderView, int, rockLength, RockLength);
  353. //----------------------------------------------------------------------------
  354. void ctkVTKRenderView::setRockIncrement(int newRockIncrement)
  355. {
  356. Q_D(ctkVTKRenderView);
  357. d->RockIncrement = qAbs(newRockIncrement);
  358. }
  359. //----------------------------------------------------------------------------
  360. CTK_GET_CPP(ctkVTKRenderView, int, rockIncrement, RockIncrement);
  361. //----------------------------------------------------------------------------
  362. void ctkVTKRenderView::setZoomFactor(double newZoomFactor)
  363. {
  364. Q_D(ctkVTKRenderView);
  365. d->ZoomFactor = qBound(0.0, qAbs(newZoomFactor), 1.0);
  366. }
  367. //----------------------------------------------------------------------------
  368. CTK_GET_CPP(ctkVTKRenderView, double, zoomFactor, ZoomFactor);
  369. //----------------------------------------------------------------------------
  370. void ctkVTKRenderView::zoomIn()
  371. {
  372. Q_D(ctkVTKRenderView);
  373. if (!d->Renderer->IsActiveCameraCreated())
  374. {
  375. return;
  376. }
  377. d->zoom(d->ZoomFactor);
  378. }
  379. //----------------------------------------------------------------------------
  380. void ctkVTKRenderView::zoomOut()
  381. {
  382. Q_D(ctkVTKRenderView);
  383. if (!d->Renderer->IsActiveCameraCreated())
  384. {
  385. return;
  386. }
  387. d->zoom(-d->ZoomFactor);
  388. }
  389. //----------------------------------------------------------------------------
  390. void ctkVTKRenderView::setFocalPoint(int x, int y, int z)
  391. {
  392. Q_D(ctkVTKRenderView);
  393. if (!d->Renderer->IsActiveCameraCreated())
  394. {
  395. return;
  396. }
  397. vtkCamera * camera = d->Renderer->GetActiveCamera();
  398. camera->SetFocalPoint(x, y, z);
  399. camera->ComputeViewPlaneNormal();
  400. camera->OrthogonalizeViewUp();
  401. d->Renderer->UpdateLightsGeometryToFollowCamera();
  402. }
  403. //----------------------------------------------------------------------------
  404. void ctkVTKRenderView::resetFocalPoint()
  405. {
  406. Q_D(ctkVTKRenderView);
  407. double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  408. d->Renderer->ComputeVisiblePropBounds(bounds);
  409. double x_center = (bounds[1] + bounds[0]) / 2.0;
  410. double y_center = (bounds[3] + bounds[2]) / 2.0;
  411. double z_center = (bounds[5] + bounds[4]) / 2.0;
  412. this->setFocalPoint(x_center, y_center, z_center);
  413. }
  414. //----------------------------------------------------------------------------
  415. void ctkVTKRenderView::lookFromAxis(const ctkAxesWidget::Axis& axis, double fov)
  416. {
  417. Q_D(ctkVTKRenderView);
  418. Q_ASSERT(d->Renderer);
  419. if (!d->Renderer->IsActiveCameraCreated())
  420. {
  421. return;
  422. }
  423. vtkCamera * camera = d->Renderer->GetActiveCamera();
  424. Q_ASSERT(camera);
  425. double widefov = fov*3;
  426. double* focalPoint = camera->GetFocalPoint();
  427. switch (axis)
  428. {
  429. case ctkAxesWidget::Right:
  430. camera->SetPosition(focalPoint[0]+widefov, focalPoint[1], focalPoint[2]);
  431. camera->SetViewUp(0, 0, 1);
  432. break;
  433. case ctkAxesWidget::Left:
  434. camera->SetPosition(focalPoint[0]-widefov, focalPoint[1], focalPoint[2]);
  435. camera->SetViewUp(0, 0, 1);
  436. break;
  437. case ctkAxesWidget::Anterior:
  438. camera->SetPosition(focalPoint[0], focalPoint[1]+widefov, focalPoint[2]);
  439. camera->SetViewUp(0, 0, 1);
  440. break;
  441. case ctkAxesWidget::Posterior:
  442. camera->SetPosition(focalPoint[0], focalPoint[1]-widefov, focalPoint[2]);
  443. camera->SetViewUp(0, 0, 1);
  444. break;
  445. case ctkAxesWidget::Superior:
  446. camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]+widefov);
  447. camera->SetViewUp(0, 1, 0);
  448. break;
  449. case ctkAxesWidget::Inferior:
  450. camera->SetPosition(focalPoint[0], focalPoint[1], focalPoint[2]-widefov);
  451. camera->SetViewUp(0, 1, 0);
  452. break;
  453. case ctkAxesWidget::None:
  454. default:
  455. // do nothing
  456. return;
  457. break;
  458. }
  459. d->Renderer->ResetCameraClippingRange();
  460. camera->ComputeViewPlaneNormal();
  461. camera->OrthogonalizeViewUp();
  462. d->Renderer->UpdateLightsGeometryToFollowCamera();
  463. }