ctkVTKAbstractViewTest1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <QApplication>
  16. #include <QDebug>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkCommandLineParser.h"
  20. #include "ctkVTKSliceView.h"
  21. #include "ctkVTKRenderView.h"
  22. #include "ctkCoreTestingMacros.h"
  23. // VTK includes
  24. #if CTK_USE_QVTKOPENGLWIDGET
  25. #include <QVTKOpenGLWidget.h>
  26. #endif
  27. #include <vtkRenderWindow.h>
  28. #include <vtkCallbackCommand.h>
  29. #ifdef Q_OS_WIN
  30. #include <windows.h>
  31. #else
  32. #include <time.h>
  33. #endif
  34. void sleep_ms(int ms)
  35. {
  36. #ifdef Q_OS_WIN
  37. Sleep(ms);
  38. #else
  39. struct timespec nanostep;
  40. nanostep.tv_sec = static_cast<time_t>(ms / 1000);
  41. nanostep.tv_nsec = ((ms % 1000) * 1000.0 * 1000.0);
  42. nanosleep(&nanostep, NULL);
  43. #endif
  44. }
  45. unsigned int RenderCount = 0;
  46. //-----------------------------------------------------------------------------
  47. void onRenderEvent(vtkObject *vtkNotUsed(caller), unsigned long vtkNotUsed(eid), void *vtkNotUsed(clientData), void *vtkNotUsed(callData))
  48. {
  49. ++RenderCount;
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool function2(ctkVTKAbstractView* view)
  53. {
  54. view->pauseRender();
  55. view->scheduleRender();
  56. sleep_ms(100);
  57. view->scheduleRender();
  58. view->resumeRender();
  59. if (RenderCount != 0)
  60. {
  61. std::cerr << "function2: Render count " << RenderCount
  62. << " does not match expected value of " << 0 << std::endl;
  63. }
  64. return RenderCount == 0;
  65. }
  66. //-----------------------------------------------------------------------------
  67. bool function1(ctkVTKAbstractView* view)
  68. {
  69. RenderCount = 0;
  70. view->pauseRender();
  71. bool success = function2(view);
  72. view->resumeRender();
  73. if (RenderCount == 0)
  74. {
  75. std::cerr << "function1: Render count " << RenderCount
  76. << " should be greater than " << 0 << std::endl;
  77. success = false;
  78. }
  79. return success;
  80. }
  81. //-----------------------------------------------------------------------------
  82. int ctkVTKAbstractViewTest1(int argc, char * argv [] )
  83. {
  84. #if CTK_USE_QVTKOPENGLWIDGET
  85. QSurfaceFormat format = QVTKOpenGLWidget::defaultFormat();
  86. format.setSamples(0);
  87. QSurfaceFormat::setDefaultFormat(format);
  88. #endif
  89. QApplication app(argc, argv);
  90. // Command line parser
  91. ctkCommandLineParser parser;
  92. parser.addArgument("", "-I", QVariant::Bool);
  93. QHash<QString, QVariant> parsedArgs = parser.parseArguments(app.arguments());
  94. bool interactive = parsedArgs["-I"].toBool();
  95. // Instantiate slice view
  96. ctkVTKSliceView sliceView;
  97. sliceView.setHighlightedBoxColor(QColor(Qt::yellow));
  98. sliceView.setCornerAnnotationText("SliceView");
  99. vtkNew<vtkCallbackCommand> renderEventCallback;
  100. renderEventCallback->SetCallback(onRenderEvent);
  101. sliceView.renderWindow()->AddObserver(vtkCommand::RenderEvent, renderEventCallback);
  102. sliceView.setMaximumUpdateRate(VTK_DOUBLE_MAX);
  103. sliceView.show();
  104. sliceView.scheduleRender();
  105. sleep_ms(100);
  106. sliceView.scheduleRender();
  107. // We expect that the rendering has been triggered at least once
  108. CHECK_BOOL(RenderCount == 0, false);
  109. bool sliceViewWasPaused = sliceView.pauseRender();
  110. RenderCount = 0;
  111. sliceView.scheduleRender();
  112. sleep_ms(100);
  113. sliceView.scheduleRender();
  114. // We expect that the rendering has not been triggered
  115. CHECK_BOOL(RenderCount == 0, true);
  116. sliceView.resumeRender();
  117. // We expect that the rendering has been triggered at least once
  118. CHECK_BOOL(RenderCount == 0, false);
  119. // Nested functions that call pauseRender
  120. CHECK_BOOL(function1(&sliceView), true);
  121. if (!interactive)
  122. {
  123. QTimer::singleShot(200, &app, SLOT(quit()));
  124. }
  125. return app.exec();
  126. }