ctkTransferFunctionRepresentationTest1.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <QCoreApplication>
  16. #include <QColor>
  17. #include <QVariant>
  18. #include <QLocale>
  19. #include <QRectF>
  20. #include <QPainterPath>
  21. #include <QGradient>
  22. #include <QList>
  23. // CTK includes
  24. #include "ctkTransferFunctionRepresentation.h"
  25. #include "ctkTransferFunction.h"
  26. // STL includes
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <limits>
  30. int ctkTransferFunctionRepresentationTest1(int argc, char * argv [])
  31. {
  32. Q_UNUSED(argc);
  33. Q_UNUSED(argv);
  34. //--------------------------------------------------------------
  35. // Test 1 : test without transfert function
  36. //--------------------------------------------------------------
  37. //---------Test Constructor----------
  38. ctkTransferFunctionRepresentation representation;
  39. if(representation.transferFunction() != 0)
  40. {
  41. std::cerr << "Line " << __LINE__
  42. << " - Problem with ctkTransfertFunctionRepresentation::"
  43. << " ctkTransfertFunctionRepresentation - transfertFunction not null."
  44. << std::endl;
  45. return EXIT_FAILURE;
  46. }
  47. QColor expectedVerticalGradientColor = QColor::fromRgbF(1., 0., 0., 1.);
  48. if(representation.verticalGradientColor() != expectedVerticalGradientColor)
  49. {
  50. std::cerr << "Line " << __LINE__
  51. << " - Problem with ctkTransfertFunctionRepresentation:: "
  52. << "ctkTransfertFunctionRepresentation"
  53. << std::endl;
  54. return EXIT_FAILURE;
  55. }
  56. //---------Test setVerticalGradientColor------
  57. expectedVerticalGradientColor = QColor::fromRgbF(0., 1., 1., 0.);
  58. representation.setVerticalGradientColor(expectedVerticalGradientColor);
  59. if(representation.verticalGradientColor() != expectedVerticalGradientColor)
  60. {
  61. std::cerr << "Line " << __LINE__
  62. << " - Problem with "
  63. << "ctkTransfertFunctionRepresentation::setVerticalGradientColor"
  64. << std::endl;
  65. return EXIT_FAILURE;
  66. }
  67. //---------Test PosX--------------------------
  68. qreal expectedPosX = 2.;
  69. if (representation.posX(expectedPosX) != expectedPosX)
  70. {
  71. std::cerr << "Line " << __LINE__
  72. << " - Problem with ctkTransfertFunctionRepresentation::posX"
  73. << std::endl;
  74. return EXIT_FAILURE;
  75. }
  76. ctkControlPoint cp;
  77. if (representation.posX(&cp) != cp.x())
  78. {
  79. std::cerr << "Line " << __LINE__
  80. << " - Problem with ctkTransfertFunctionRepresentation::posX"
  81. << representation.posX(&cp);
  82. return EXIT_FAILURE;
  83. }
  84. ctkPoint point;
  85. if (representation.posX(point) != point.X)
  86. {
  87. std::cerr << "Line " << __LINE__
  88. << " - Problem with ctkTransfertFunctionRepresentation::posX"
  89. << representation.posX(point);
  90. return EXIT_FAILURE;
  91. }
  92. //---------Test PosY--------------------------
  93. QVariant variant = 2.;
  94. if (representation.posY(variant) != 2.)
  95. {
  96. std::cerr << "Line " << __LINE__
  97. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  98. << " - for the qreal" << std::endl;
  99. return EXIT_FAILURE;
  100. }
  101. QColor defaultColor = QColor::fromRgbF(0., 0., 0., 0.);
  102. variant = defaultColor;
  103. if (representation.posY(variant) != defaultColor.alphaF())
  104. {
  105. std::cerr << "Line " << __LINE__
  106. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  107. << " - for the color: " << representation.posY(variant);
  108. return EXIT_FAILURE;
  109. }
  110. if (representation.posY(&cp) != cp.value())
  111. {
  112. std::cerr << "Line " << __LINE__
  113. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  114. << representation.posY(&cp);
  115. return EXIT_FAILURE;
  116. }
  117. if (representation.posY(point) != point.Value)
  118. {
  119. std::cerr << "Line " << __LINE__
  120. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  121. << representation.posX(point);
  122. return EXIT_FAILURE;
  123. }
  124. //--------Test Color--------------------------
  125. QColor expectedColor = QColor::fromRgbF(1., 1., 1.);
  126. variant = expectedColor;
  127. if (representation.color(variant) != expectedColor)
  128. {
  129. std::cerr << "Line " << __LINE__
  130. << " - Problem with ctkTransfertFunctionRepresentation::color"
  131. << " when QVariant is a color"
  132. << std::endl;
  133. return EXIT_FAILURE;
  134. }
  135. variant = expectedPosX;
  136. if (representation.color(variant) != QColor::fromRgbF(0., 0., 0.))
  137. {
  138. std::cerr << "Line " << __LINE__
  139. << " - Problem with ctkTransfertFunctionRepresentation::color"
  140. << " when QVariant is a not a color"
  141. << std::endl;
  142. return EXIT_FAILURE;
  143. }
  144. if (representation.color(&cp) != representation.color(cp.value()))
  145. {
  146. std::cerr << "Line " << __LINE__
  147. << " - Problem with ctkTransfertFunctionRepresentation::color";
  148. return EXIT_FAILURE;
  149. }
  150. if (representation.color(point) != representation.color(point.Value))
  151. {
  152. std::cerr << "Line " << __LINE__
  153. << " - Problem with ctkTransfertFunctionRepresentation::color";
  154. return EXIT_FAILURE;
  155. }
  156. //---------Test MapXToScene------------------
  157. qreal xPos = 2.;
  158. if (representation.mapXToScene(xPos) != 0)
  159. {
  160. std::cerr << "Line " << __LINE__
  161. << " - Problem with "
  162. << "ctkTransfertFunctionRepresentation::mapXToScene "
  163. << representation.mapXToScene(xPos)
  164. << std::endl;
  165. return EXIT_FAILURE;
  166. }
  167. //--------Test MapYToScene------------------
  168. qreal yPos = 2.;
  169. if (representation.mapYToScene(yPos) != 1) //Because la fonction height return 1.
  170. {
  171. std::cerr << "Line " << __LINE__
  172. << " - Problem with "
  173. << "ctkTransfertFunctionRepresentation::mapYToScene "
  174. << representation.mapYToScene(yPos)
  175. << std::endl;
  176. return EXIT_FAILURE;
  177. }
  178. //--------Test MapXFromScene----------------
  179. qreal defaultScenePosX = 2.;
  180. qreal mapX = representation.mapXFromScene(defaultScenePosX);
  181. if (mapX != std::numeric_limits<qreal>::infinity())
  182. {
  183. std::cerr << "Line " << __LINE__
  184. << " - Problem with "
  185. << "ctkTransfertFunctionRepresentation::mapXFromScene "
  186. << representation.mapXFromScene(defaultScenePosX)
  187. << std::endl;
  188. return EXIT_FAILURE;
  189. }
  190. //--------Test MapyFromScene----------------
  191. qreal defaultScenePosY = 2.;
  192. qreal mapY = representation.mapYFromScene(defaultScenePosY);
  193. if (mapY != - std::numeric_limits<qreal>::infinity())
  194. {
  195. std::cerr << "Line " << __LINE__
  196. << " - Problem with "
  197. << "ctkTransfertFunctionRepresentation::mapyFromScene "
  198. << representation.mapYFromScene(defaultScenePosY)
  199. << std::endl;
  200. return EXIT_FAILURE;
  201. }
  202. //--------Test Curve-----------------------
  203. QPainterPath defaultPath = representation.curve();
  204. if(!defaultPath.isEmpty())
  205. {
  206. std::cerr << "Line " << __LINE__
  207. << " - Problem with "
  208. << "ctkTransfertFunctionRepresentation::curve "
  209. << std::endl;
  210. return EXIT_FAILURE;
  211. }
  212. //--------Test gradient--------------------
  213. if(representation.gradient().type() != QGradient::LinearGradient)
  214. {
  215. std::cerr << "Line " << __LINE__
  216. << " - Problem with "
  217. << "ctkTransfertFunctionRepresentation::gradient "
  218. << std::endl;
  219. return EXIT_FAILURE;
  220. }
  221. //--------Test points----------------------
  222. QList<QPointF> expectedPoints;
  223. if(representation.points().size() != 0)
  224. {
  225. std::cerr << "Line " << __LINE__
  226. << " - Problem with "
  227. << "ctkTransfertFunctionRepresentation::points "
  228. << std::endl;
  229. return EXIT_FAILURE;
  230. }
  231. //--------Test bezierParams----------------
  232. ctkControlPoint startPoint;
  233. ctkControlPoint endPoint;
  234. if(representation.bezierParams(&startPoint,&endPoint).size() != 4)
  235. {
  236. std::cerr << "Line " << __LINE__
  237. << " - Problem with "
  238. << "ctkTransfertFunctionRepresentation::bezierParams "
  239. << std::endl;
  240. return EXIT_FAILURE;
  241. }
  242. //--------Test nonLinearPoints------------- // ? case subpoint ?
  243. if(representation.nonLinearPoints(&startPoint,&endPoint).size() != 2)
  244. {
  245. std::cerr << "Line " << __LINE__
  246. << " - Problem with "
  247. << "ctkTransfertFunctionRepresentation::nonLinearPoints "
  248. << std::endl;
  249. return EXIT_FAILURE;
  250. }
  251. //-------Test mapPointToScene-With q ctkPoint*----------
  252. qreal defaultX = 2.;
  253. QVariant defaultVariant = 1.;
  254. ctkPoint defaultPoint(defaultX,defaultVariant);
  255. QPointF defaultPointF(0,1);
  256. if(representation.mapPointToScene(defaultPoint) != defaultPointF)
  257. {
  258. std::cerr << "Line " << __LINE__
  259. << " - Problem with "
  260. << "ctkTransfertFunctionRepresentation::mapPointFromScene "
  261. << defaultPointF.x()
  262. << " "
  263. << defaultPointF.y()
  264. << std::endl;
  265. return EXIT_FAILURE;
  266. }
  267. //-------Test mapPointToScene-With a ctkControlPoint&------------
  268. ctkControlPoint defaultControlPoint;
  269. if(representation.mapPointToScene(&defaultControlPoint) != defaultPointF)
  270. {
  271. std::cerr << "Line " << __LINE__
  272. << " - Problem with "
  273. << "ctkTransfertFunctionRepresentation::mapPointFromScene "
  274. << defaultPointF.x()
  275. << " "
  276. << defaultPointF.y()
  277. << std::endl;
  278. return EXIT_FAILURE;
  279. }
  280. //-------Test computeCurve----------------
  281. representation.computeCurve();
  282. //-------Test computegradient-------------
  283. representation.computeGradient();
  284. return EXIT_SUCCESS;
  285. }