ctkTransferFunctionRepresentationTest1.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. //---------Test PosY--------------------------
  77. QVariant variant = 2.;
  78. if (representation.posY(variant) != 2.)
  79. {
  80. std::cerr << "Line " << __LINE__
  81. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  82. << " - for the qreal" << std::endl;
  83. return EXIT_FAILURE;
  84. }
  85. QColor defaultColor = QColor::fromRgbF(0., 0., 0., 0.);
  86. variant = defaultColor;
  87. if (representation.posY(variant) != defaultColor.alphaF())
  88. {
  89. std::cerr << "Line " << __LINE__
  90. << " - Problem with ctkTransfertFunctionRepresentation::posY"
  91. << " - for the color: " << representation.posY(variant)
  92. << " - " << defaultColor.alphaF() << std::endl;
  93. return EXIT_FAILURE;
  94. }
  95. //--------Test Color--------------------------
  96. QColor expectedColor = QColor::fromRgbF(1., 1., 1.);
  97. variant = expectedColor;
  98. if (representation.color(variant) != expectedColor)
  99. {
  100. std::cerr << "Line " << __LINE__
  101. << " - Problem with ctkTransfertFunctionRepresentation::color"
  102. << " when QVariant is a color"
  103. << std::endl;
  104. return EXIT_FAILURE;
  105. }
  106. variant = expectedPosX;
  107. if (representation.color(variant) != QColor::fromRgbF(0., 0., 0.))
  108. {
  109. std::cerr << "Line " << __LINE__
  110. << " - Problem with ctkTransfertFunctionRepresentation::color"
  111. << " when QVariant is a not a color"
  112. << std::endl;
  113. return EXIT_FAILURE;
  114. }
  115. //---------Test MapXToScene------------------
  116. qreal xPos = 2.;
  117. if (representation.mapXToScene(xPos) != 0)
  118. {
  119. std::cerr << "Line " << __LINE__
  120. << " - Problem with "
  121. << "ctkTransfertFunctionRepresentation::mapXToScene "
  122. << representation.mapXToScene(xPos)
  123. << std::endl;
  124. return EXIT_FAILURE;
  125. }
  126. //--------Test MapYToScene------------------
  127. qreal yPos = 2.;
  128. if (representation.mapYToScene(yPos) != 1) //Because la fonction height return 1.
  129. {
  130. std::cerr << "Line " << __LINE__
  131. << " - Problem with "
  132. << "ctkTransfertFunctionRepresentation::mapYToScene "
  133. << representation.mapYToScene(yPos)
  134. << std::endl;
  135. return EXIT_FAILURE;
  136. }
  137. //--------Test MapXFromScene----------------
  138. qreal defaultScenePosX = 2.;
  139. qreal mapX = representation.mapXFromScene(defaultScenePosX);
  140. if (mapX != std::numeric_limits<qreal>::infinity())
  141. {
  142. std::cerr << "Line " << __LINE__
  143. << " - Problem with "
  144. << "ctkTransfertFunctionRepresentation::mapXFromScene "
  145. << representation.mapXFromScene(defaultScenePosX)
  146. << std::endl;
  147. return EXIT_FAILURE;
  148. }
  149. //--------Test MapyFromScene----------------
  150. qreal defaultScenePosY = 2.;
  151. qreal mapY = representation.mapYFromScene(defaultScenePosY);
  152. if (mapY != - std::numeric_limits<qreal>::infinity())
  153. {
  154. std::cerr << "Line " << __LINE__
  155. << " - Problem with "
  156. << "ctkTransfertFunctionRepresentation::mapyFromScene "
  157. << representation.mapYFromScene(defaultScenePosY)
  158. << std::endl;
  159. return EXIT_FAILURE;
  160. }
  161. //--------Test Curve-----------------------
  162. QPainterPath defaultPath = representation.curve();
  163. if(!defaultPath.isEmpty())
  164. {
  165. std::cerr << "Line " << __LINE__
  166. << " - Problem with "
  167. << "ctkTransfertFunctionRepresentation::curve "
  168. << std::endl;
  169. return EXIT_FAILURE;
  170. }
  171. //--------Test gradient--------------------
  172. if(representation.gradient().type() != QGradient::LinearGradient)
  173. {
  174. std::cerr << "Line " << __LINE__
  175. << " - Problem with "
  176. << "ctkTransfertFunctionRepresentation::gradient "
  177. << std::endl;
  178. return EXIT_FAILURE;
  179. }
  180. //--------Test points----------------------
  181. QList<QPointF> expectedPoints;
  182. if(representation.points().size() != 0)
  183. {
  184. std::cerr << "Line " << __LINE__
  185. << " - Problem with "
  186. << "ctkTransfertFunctionRepresentation::points "
  187. << std::endl;
  188. return EXIT_FAILURE;
  189. }
  190. //--------Test bezierParams----------------
  191. ctkControlPoint startPoint;
  192. ctkControlPoint endPoint;
  193. if(representation.bezierParams(&startPoint,&endPoint).size() != 4)
  194. {
  195. std::cerr << "Line " << __LINE__
  196. << " - Problem with "
  197. << "ctkTransfertFunctionRepresentation::bezierParams "
  198. << std::endl;
  199. return EXIT_FAILURE;
  200. }
  201. //--------Test nonLinearPoints------------- // ? case subpoint ?
  202. if(representation.nonLinearPoints(&startPoint,&endPoint).size() != 2)
  203. {
  204. std::cerr << "Line " << __LINE__
  205. << " - Problem with "
  206. << "ctkTransfertFunctionRepresentation::nonLinearPoints "
  207. << std::endl;
  208. return EXIT_FAILURE;
  209. }
  210. //-------Test mapPointToScene-With q ctkPoint*----------
  211. qreal defaultX = 2.;
  212. QVariant defaultVariant = 1.;
  213. ctkPoint defaultPoint(defaultX,defaultVariant);
  214. QPointF defaultPointF(0,1);
  215. if(representation.mapPointToScene(defaultPoint) != defaultPointF)
  216. {
  217. std::cerr << "Line " << __LINE__
  218. << " - Problem with "
  219. << "ctkTransfertFunctionRepresentation::mapPointFromScene "
  220. << defaultPointF.x()
  221. << " "
  222. << defaultPointF.y()
  223. << std::endl;
  224. return EXIT_FAILURE;
  225. }
  226. //-------Test mapPointToScene-With a ctkControlPoint&------------
  227. ctkControlPoint defaultControlPoint;
  228. if(representation.mapPointToScene(&defaultControlPoint) != defaultPointF)
  229. {
  230. std::cerr << "Line " << __LINE__
  231. << " - Problem with "
  232. << "ctkTransfertFunctionRepresentation::mapPointFromScene "
  233. << defaultPointF.x()
  234. << " "
  235. << defaultPointF.y()
  236. << std::endl;
  237. return EXIT_FAILURE;
  238. }
  239. //-------Test computeCurve----------------
  240. representation.computeCurve();
  241. //-------Test computegradient-------------
  242. representation.computeGradient();
  243. return EXIT_SUCCESS;
  244. }