ctkVTKMagnifyViewTest2.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <QCursor>
  17. #include <QHBoxLayout>
  18. #include <QIcon>
  19. #include <QSignalSpy>
  20. #include <QStyle>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkVTKMagnifyView.h"
  24. #include "ctkCommandLineParser.h"
  25. #include "ctkVTKSliceView.h"
  26. // VTK includes
  27. #include <vtkImageReader2Factory.h>
  28. #include <vtkImageReader2.h>
  29. #include <vtkImageData.h>
  30. #include <vtkImageGaussianSmooth.h>
  31. #include <vtkSmartPointer.h>
  32. #include <vtkVersion.h>
  33. // STD includes
  34. #include <cstdlib>
  35. #include <iostream>
  36. //-----------------------------------------------------------------------------
  37. bool imageCompare(ctkVTKMagnifyView * magnify, QString baselineDirectory,
  38. QString baselineFilename)
  39. {
  40. QImage output = QPixmap::grabWidget(magnify).toImage();
  41. QImage baseline(baselineDirectory + "/" + baselineFilename);
  42. return output == baseline;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // (Used to create baselines, not during testing).
  46. void imageSave(ctkVTKMagnifyView * magnify, QString baselineDirectory,
  47. QString baselineFilename)
  48. {
  49. QImage output = QPixmap::grabWidget(magnify).toImage();
  50. output.save(baselineDirectory + "/" + baselineFilename);
  51. }
  52. //-----------------------------------------------------------------------------
  53. bool runBaselineTest(int time, QApplication& app, ctkVTKMagnifyView * magnify,
  54. QWidget * underWidget, bool shouldBeUnder,
  55. QString baselineDirectory, QString testName,
  56. QString testNumber, QString errorMessage)
  57. {
  58. QTimer::singleShot(time, &app, SLOT(quit()));
  59. if (app.exec() == EXIT_FAILURE)
  60. {
  61. std::cerr << "ctkVTKMagnifyView exec failed when "
  62. << qPrintable(errorMessage) << std::endl;
  63. return false;
  64. }
  65. if (underWidget->underMouse() != shouldBeUnder)
  66. {
  67. std::cerr << "ctkMagnifyView mouse position failed when "
  68. << qPrintable(errorMessage) << std::endl;
  69. return false;
  70. }
  71. QString baselineFilename
  72. = "ctkVTKMagnifyViewTest2" + testNumber + testName + ".png";
  73. if (!imageCompare(magnify, baselineDirectory, baselineFilename))
  74. {
  75. std::cerr << "ctkVTKMagnifyView baseline comparison failed when "
  76. << qPrintable(errorMessage) << "." << std::endl;
  77. return false;
  78. }
  79. return true;
  80. }
  81. //-----------------------------------------------------------------------------
  82. int ctkVTKMagnifyViewTest2(int argc, char * argv [] )
  83. {
  84. QApplication app(argc, argv);
  85. // Command line parser
  86. ctkCommandLineParser parser;
  87. parser.addArgument("", "-D", QVariant::String);
  88. parser.addArgument("", "-V", QVariant::String);
  89. parser.addArgument("", "-I", QVariant::String);
  90. parser.addArgument("", "-T", QVariant::String);
  91. parser.addArgument("", "-S", QVariant::String);
  92. parser.addArgument("", "-M", QVariant::String);
  93. bool ok = false;
  94. QHash<QString, QVariant> parsedArgs = parser.parseArguments(app.arguments(), &ok);
  95. if (!ok)
  96. {
  97. std::cerr << qPrintable(parser.errorString()) << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. QString dataDirectory = parsedArgs["-D"].toString();
  101. QString baselineDirectory = parsedArgs["-V"].toString();
  102. QString testType = parsedArgs["-T"].toString();
  103. bool interactive = parsedArgs["-I"].toBool();
  104. int size = parsedArgs["-S"].toInt();
  105. double magnification = parsedArgs["-M"].toDouble();
  106. // Create the parent widget
  107. QWidget parentWidget;
  108. QHBoxLayout layout(&parentWidget);
  109. // Magnify widget parameters (we want an odd widget size and odd bullsEye)
  110. bool showCrosshair = true;
  111. QPen crosshairPen(Qt::yellow);
  112. crosshairPen.setJoinStyle(Qt::MiterJoin);
  113. ctkCrosshairLabel::CrosshairType crosshairType
  114. = ctkCrosshairLabel::BullsEyeCrosshair;
  115. double bullsEyeWidth = magnification + 2;
  116. QColor marginColor = Qt::magenta;
  117. bool observeRenderWindowEvents = false;
  118. int updateInterval = 0;
  119. // Create the magnify widget
  120. ctkVTKMagnifyView * magnify = new ctkVTKMagnifyView(&parentWidget);
  121. magnify->setMinimumSize(size,size);
  122. magnify->setMaximumSize(size,size);
  123. magnify->setShowCrosshair(showCrosshair);
  124. magnify->setCrosshairPen(crosshairPen);
  125. magnify->setCrosshairType(crosshairType);
  126. magnify->setBullsEyeWidth(bullsEyeWidth);
  127. magnify->setMarginColor(marginColor);
  128. magnify->setMagnification(magnification);
  129. magnify->setObserveRenderWindowEvents(observeRenderWindowEvents);
  130. magnify->setUpdateInterval(updateInterval);
  131. layout.addWidget(magnify);
  132. // Test magnify widget parameters
  133. if (magnify->showCrosshair() != showCrosshair)
  134. {
  135. std::cerr << "ctkVTKMagnifyView:setShowCrosshair failed. "
  136. << magnify->showCrosshair() << std::endl;
  137. return EXIT_FAILURE;
  138. }
  139. if (magnify->crosshairPen() != crosshairPen)
  140. {
  141. std::cerr << "ctkVTKMagnifyView:setCrosshairPen failed. "
  142. << qPrintable(magnify->crosshairPen().color().name()) << std::endl;
  143. return EXIT_FAILURE;
  144. }
  145. if (magnify->crosshairType() != crosshairType)
  146. {
  147. std::cerr << "ctkVTKMagnifyView:setCrosshairType failed. "
  148. << magnify->crosshairType() << std::endl;
  149. return EXIT_FAILURE;
  150. }
  151. if (magnify->bullsEyeWidth() != bullsEyeWidth)
  152. {
  153. std::cerr << "ctkVTKMagnifyView:setBullsEyeWidth failed. "
  154. << magnify->bullsEyeWidth() << std::endl;
  155. return EXIT_FAILURE;
  156. }
  157. if (magnify->marginColor() != marginColor)
  158. {
  159. std::cerr << "ctkVTKMagnifyView:setMarginColor failed. "
  160. << qPrintable(magnify->marginColor().name()) << std::endl;
  161. return EXIT_FAILURE;
  162. }
  163. if (magnify->magnification() != magnification)
  164. {
  165. std::cerr << "ctkVTKMagnifyView:setMagnification failed. "
  166. << magnify->magnification() << std::endl;
  167. return EXIT_FAILURE;
  168. }
  169. if (magnify->observeRenderWindowEvents() != observeRenderWindowEvents)
  170. {
  171. std::cerr << "ctkVTKMagnifyView:setObserveRenderWindowEvents failed. "
  172. << magnify->observeRenderWindowEvents() << std::endl;
  173. }
  174. if (magnify->updateInterval() != updateInterval)
  175. {
  176. std::cerr << "ctkVTKMagnifyView:setUpdateInterval failed. "
  177. << magnify->updateInterval() << std::endl;
  178. return EXIT_FAILURE;
  179. }
  180. // The remainder is interactive, so abort now if command line args specify otherwise
  181. if (interactive)
  182. {
  183. return EXIT_SUCCESS;
  184. }
  185. // Add observed ctkVTKSliceViews (there are three, and the first two are observed)
  186. QList<ctkVTKSliceView *> allSliceViews;
  187. int numSliceViews = 3;
  188. for (int i = 0; i < numSliceViews; i++)
  189. {
  190. allSliceViews.append(new ctkVTKSliceView(&parentWidget));
  191. layout.addWidget(allSliceViews[i]);
  192. }
  193. // Observe the first two widgets
  194. magnify->observe(allSliceViews[0]->VTKWidget());
  195. magnify->observe(allSliceViews[1]->VTKWidget());
  196. if (!magnify->isObserved(allSliceViews[0]->VTKWidget()) ||
  197. !magnify->isObserved(allSliceViews[1]->VTKWidget()) ||
  198. magnify->isObserved(allSliceViews[2]->VTKWidget()) ||
  199. magnify->numberObserved() != 2)
  200. {
  201. std::cerr << "ctkVTKMagnifyView:observe(QVTKWidget*) failed. "
  202. << "Number observed = " << magnify->numberObserved() << std::endl;
  203. return EXIT_FAILURE;
  204. }
  205. QString imageFilename = dataDirectory + "/" + "computerIcon.png";
  206. // Instanciate the reader factory
  207. vtkSmartPointer<vtkImageReader2Factory> imageFactory =
  208. vtkSmartPointer<vtkImageReader2Factory>::New();
  209. // Instanciate an image reader
  210. vtkSmartPointer<vtkImageReader2> imageReader;
  211. imageReader.TakeReference(imageFactory->CreateImageReader2(imageFilename.toLatin1()));
  212. if (!imageReader)
  213. {
  214. std::cerr << "Failed to instanciate image reader using: "
  215. << qPrintable(imageFilename) << std::endl;
  216. return EXIT_FAILURE;
  217. }
  218. // Read image
  219. imageReader->SetFileName(imageFilename.toLatin1());
  220. imageReader->Update();
  221. #if (VTK_MAJOR_VERSION <= 5)
  222. vtkImageData* image = imageReader->GetOutput();
  223. #else
  224. vtkAlgorithmOutput* imagePort = imageReader->GetOutputPort();
  225. #endif
  226. // Setup the slice views
  227. for (int i = 0; i < numSliceViews; i++)
  228. {
  229. allSliceViews[i]->setRenderEnabled(true);
  230. allSliceViews[i]->setMinimumSize(350,350);
  231. #if (VTK_MAJOR_VERSION <= 5)
  232. allSliceViews[i]->setImageData(image);
  233. #else
  234. allSliceViews[i]->setImageDataConnection(imagePort);
  235. #endif
  236. allSliceViews[i]->setHighlightedBoxColor(Qt::yellow);
  237. allSliceViews[i]->scheduleRender();
  238. }
  239. int time = 200;
  240. // Get crosshair points of interest, used in the following tests
  241. parentWidget.move(0,0);
  242. parentWidget.show();
  243. QTimer::singleShot(time, &app, SLOT(quit()));
  244. if (app.exec() == EXIT_FAILURE)
  245. {
  246. std::cerr << "ctkVTKMagnifyView:show failed the first time." << std::endl;
  247. return EXIT_FAILURE;
  248. }
  249. QPoint insideSlice0 = allSliceViews[0]->mapToGlobal(
  250. QPoint(allSliceViews[0]->width()/2 + 100, allSliceViews[0]->height()/2 + 100));
  251. QPoint outside = parentWidget.mapToGlobal(
  252. QPoint(parentWidget.width(), parentWidget.height()));
  253. QPoint insideSlice1 = allSliceViews[1]->mapToGlobal(
  254. QPoint(allSliceViews[1]->width()/2 - 50, allSliceViews[1]->height() - 50));
  255. QPoint insideSlice1edge = allSliceViews[1]->mapToGlobal(
  256. QPoint(allSliceViews[1]->width() - 5, allSliceViews[1]->height() - 100));
  257. QPoint insideSlice2 = allSliceViews[2]->mapToGlobal(
  258. QPoint(allSliceViews[2]->width()/2, allSliceViews[2]->height()/2));
  259. QPoint insideSlice0bottomRightCorner = allSliceViews[0]->mapToGlobal(
  260. QPoint(allSliceViews[0]->width()-1, allSliceViews[0]->height()-1));
  261. QPoint insideSlice0topLeftCorner = allSliceViews[0]->mapToGlobal(
  262. QPoint(0,0));
  263. parentWidget.hide();
  264. // Make sure the magnify widget magnifies right away when shown with the crosshair inside
  265. // an observed QVTKWidget
  266. QCursor::setPos(insideSlice0);
  267. parentWidget.show();
  268. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  269. baselineDirectory, testType, "a",
  270. "magnify widget first shown with crosshair inside observed widget"))
  271. {
  272. return EXIT_FAILURE;
  273. }
  274. parentWidget.hide();
  275. // Make sure the magnify widget shows blank right away when shown with the crosshair
  276. // outside the observed QVTKWidgets
  277. QCursor::setPos(outside);
  278. parentWidget.show();
  279. if (!runBaselineTest(time, app, magnify, &parentWidget, false,
  280. baselineDirectory, testType, "b",
  281. "magnify widget first shown with crosshair outside observed widget"))
  282. {
  283. return EXIT_FAILURE;
  284. }
  285. // Test magnification after move to allSliceViews[1]
  286. QCursor::setPos(insideSlice1);
  287. if (!runBaselineTest(time, app, magnify, allSliceViews[1], true,
  288. baselineDirectory, testType, "c",
  289. "crosshair moved inside 2nd observed widget the first time"))
  290. {
  291. return EXIT_FAILURE;
  292. }
  293. // Test magnification after move within allSliceViews[1] close to border
  294. QCursor::setPos(insideSlice1edge);
  295. if (!runBaselineTest(time, app, magnify, allSliceViews[1], true,
  296. baselineDirectory, testType, "d",
  297. "crosshair moved inside 2nd observed widget the second time"))
  298. {
  299. return EXIT_FAILURE;
  300. }
  301. // Test magnification after move outside an observed widget (should be blank)
  302. QCursor::setPos(insideSlice2);
  303. if (!runBaselineTest(time, app, magnify, allSliceViews[2], true,
  304. baselineDirectory, testType, "e",
  305. "crosshair moved inside unobserved widget"))
  306. {
  307. return EXIT_FAILURE;
  308. }
  309. // Test magnification after move back inside an observed widget (at extreme bottom
  310. // right corner)
  311. QCursor::setPos(insideSlice0bottomRightCorner);
  312. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  313. baselineDirectory, testType, "f",
  314. "crosshair moved to bottom-right corner of observed widget"))
  315. {
  316. return EXIT_FAILURE;
  317. }
  318. // Test magnification after move back inside an observed widget (at extreme top left
  319. // corner)
  320. QCursor::setPos(insideSlice0topLeftCorner);
  321. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  322. baselineDirectory, testType, "g",
  323. "crosshair moved to top-left corner of observed widget"))
  324. {
  325. return EXIT_FAILURE;
  326. }
  327. // Go back inside the widget
  328. QCursor::setPos(insideSlice0);
  329. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  330. baselineDirectory, testType, "a",
  331. "crosshair moved again inside observed widget"))
  332. {
  333. return EXIT_FAILURE;
  334. }
  335. // Test enabling observing render window events (trigger a render window event by
  336. // changing the image data)
  337. observeRenderWindowEvents = true;
  338. magnify->setObserveRenderWindowEvents(observeRenderWindowEvents);
  339. if (magnify->observeRenderWindowEvents() != observeRenderWindowEvents)
  340. {
  341. std::cerr << "ctkVTKMagnifyView:setObserveRenderWindowEvents failed. "
  342. << magnify->observeRenderWindowEvents() << std::endl;
  343. return EXIT_FAILURE;
  344. }
  345. vtkImageGaussianSmooth * gaussian = vtkImageGaussianSmooth::New();
  346. #if (VTK_MAJOR_VERSION <= 5)
  347. gaussian->SetInput(image);
  348. #else
  349. gaussian->SetInputConnection(imagePort);
  350. #endif
  351. gaussian->SetRadiusFactors(5,5);
  352. gaussian->Update();
  353. #if (VTK_MAJOR_VERSION <= 5)
  354. allSliceViews[0]->setImageData(gaussian->GetOutput());
  355. #else
  356. allSliceViews[0]->setImageDataConnection(gaussian->GetOutputPort());
  357. #endif
  358. allSliceViews[0]->scheduleRender();
  359. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  360. baselineDirectory, testType, "h",
  361. "after Gaussian blur when observing render window events"))
  362. {
  363. return EXIT_FAILURE;
  364. }
  365. // Test disabling observing render window events (trigger a render window event by
  366. // changing the image data)
  367. observeRenderWindowEvents = false;
  368. magnify->setObserveRenderWindowEvents(observeRenderWindowEvents);
  369. if (magnify->observeRenderWindowEvents() != observeRenderWindowEvents)
  370. {
  371. std::cerr << "ctkVTKMagnifyView:setObserveRenderWindowEvents failed. "
  372. << magnify->observeRenderWindowEvents() << std::endl;
  373. return EXIT_FAILURE;
  374. }
  375. #if (VTK_MAJOR_VERSION <= 5)
  376. gaussian->SetInput(image);
  377. #else
  378. gaussian->SetInputConnection(imageReader->GetOutputPort());
  379. #endif
  380. gaussian->SetRadiusFactors(0,0);
  381. gaussian->Update();
  382. #if (VTK_MAJOR_VERSION <= 5)
  383. allSliceViews[0]->setImageData(gaussian->GetOutput());
  384. #else
  385. allSliceViews[0]->setImageDataConnection(gaussian->GetOutputPort());
  386. #endif
  387. allSliceViews[0]->scheduleRender();
  388. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  389. baselineDirectory, testType, "h",
  390. "after Gaussian blur when not observing render window events"))
  391. {
  392. return EXIT_FAILURE;
  393. }
  394. // Test changing the update interval
  395. magnify->setUpdateInterval(time * 2);
  396. magnify->setObserveRenderWindowEvents(true);
  397. #if (VTK_MAJOR_VERSION <= 5)
  398. allSliceViews[0]->setImageData(image);
  399. #else
  400. allSliceViews[0]->setImageDataConnection(imagePort);
  401. #endif
  402. allSliceViews[0]->scheduleRender();
  403. QCursor::setPos(insideSlice0bottomRightCorner);
  404. // It should be waiting to update here
  405. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  406. baselineDirectory, testType, "h",
  407. "after changing update interval: updated too quickly"))
  408. {
  409. return EXIT_FAILURE;
  410. }
  411. // It should have updated by now
  412. if (!runBaselineTest(time + 50, app, magnify, allSliceViews[0], true,
  413. baselineDirectory, testType, "f",
  414. "after changing update interval: didn't update after waiting"))
  415. {
  416. return EXIT_FAILURE;
  417. }
  418. gaussian->Delete();
  419. return EXIT_SUCCESS;
  420. }