ctkVTKMagnifyViewTest2.cpp 16 KB

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