ctkMaterialPropertyPreviewLabel.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <QColor>
  16. #include <QDebug>
  17. #include <QImage>
  18. #include <QPainter>
  19. #include <QVector3D>
  20. //CTK includes
  21. #include "ctkMaterialPropertyPreviewLabel.h"
  22. // STD includes
  23. #include <cmath>
  24. //ctkMaterialPropertyPreviewLabelPrivate
  25. //-----------------------------------------------------------------------------
  26. class ctkMaterialPropertyPreviewLabelPrivate
  27. {
  28. Q_DECLARE_PUBLIC(ctkMaterialPropertyPreviewLabel);
  29. protected:
  30. ctkMaterialPropertyPreviewLabel* const q_ptr;
  31. public:
  32. ctkMaterialPropertyPreviewLabelPrivate(ctkMaterialPropertyPreviewLabel& object);
  33. double Ambient;
  34. double Diffuse;
  35. double Specular;
  36. double SpecularPower;
  37. QColor Color;
  38. double GridOpacity;
  39. };
  40. //-----------------------------------------------------------------------------
  41. ctkMaterialPropertyPreviewLabelPrivate::ctkMaterialPropertyPreviewLabelPrivate(ctkMaterialPropertyPreviewLabel& object)
  42. :q_ptr(&object)
  43. {
  44. this->Ambient = 0.5;
  45. this->Diffuse = 0.5;
  46. this->Specular = 0.5;
  47. this->SpecularPower = 50;
  48. this->Color = Qt::red;
  49. this->GridOpacity = 0.6;
  50. }
  51. //ctkMaterialPropertyPreviewLabel
  52. //-----------------------------------------------------------------------------
  53. ctkMaterialPropertyPreviewLabel::ctkMaterialPropertyPreviewLabel(QWidget *newParent)
  54. : QFrame(newParent)
  55. , d_ptr(new ctkMaterialPropertyPreviewLabelPrivate(*this))
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. ctkMaterialPropertyPreviewLabel::~ctkMaterialPropertyPreviewLabel()
  60. {
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkMaterialPropertyPreviewLabel::setAmbient(double newAmbient)
  64. {
  65. Q_D(ctkMaterialPropertyPreviewLabel);
  66. d->Ambient = newAmbient;
  67. this->update();
  68. }
  69. //-----------------------------------------------------------------------------
  70. double ctkMaterialPropertyPreviewLabel::ambient()const
  71. {
  72. Q_D(const ctkMaterialPropertyPreviewLabel);
  73. return d->Ambient;
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkMaterialPropertyPreviewLabel::setDiffuse(double newDiffuse)
  77. {
  78. Q_D(ctkMaterialPropertyPreviewLabel);
  79. d->Diffuse = newDiffuse;
  80. this->update();
  81. }
  82. //-----------------------------------------------------------------------------
  83. double ctkMaterialPropertyPreviewLabel::diffuse()const
  84. {
  85. Q_D(const ctkMaterialPropertyPreviewLabel);
  86. return d->Diffuse;
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkMaterialPropertyPreviewLabel::setSpecular(double newSpecular)
  90. {
  91. Q_D(ctkMaterialPropertyPreviewLabel);
  92. d->Specular = newSpecular;
  93. this->update();
  94. }
  95. //-----------------------------------------------------------------------------
  96. double ctkMaterialPropertyPreviewLabel::specular()const
  97. {
  98. Q_D(const ctkMaterialPropertyPreviewLabel);
  99. return d->Specular;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void ctkMaterialPropertyPreviewLabel::setSpecularPower(double newSpecularPower)
  103. {
  104. Q_D(ctkMaterialPropertyPreviewLabel);
  105. d->SpecularPower = newSpecularPower;
  106. this->update();
  107. }
  108. //-----------------------------------------------------------------------------
  109. double ctkMaterialPropertyPreviewLabel::specularPower()const
  110. {
  111. Q_D(const ctkMaterialPropertyPreviewLabel);
  112. return d->SpecularPower;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkMaterialPropertyPreviewLabel::setColor(const QColor& newColor)
  116. {
  117. Q_D(ctkMaterialPropertyPreviewLabel);
  118. d->Color = newColor;
  119. this->update();
  120. }
  121. //-----------------------------------------------------------------------------
  122. QColor ctkMaterialPropertyPreviewLabel::color()const
  123. {
  124. Q_D(const ctkMaterialPropertyPreviewLabel);
  125. return d->Color;
  126. }
  127. //-----------------------------------------------------------------------------
  128. void ctkMaterialPropertyPreviewLabel::setGridOpacity(double newGridOpacity)
  129. {
  130. Q_D(ctkMaterialPropertyPreviewLabel);
  131. d->GridOpacity = newGridOpacity;
  132. this->update();
  133. }
  134. //-----------------------------------------------------------------------------
  135. double ctkMaterialPropertyPreviewLabel::gridOpacity()const
  136. {
  137. Q_D(const ctkMaterialPropertyPreviewLabel);
  138. return d->GridOpacity;
  139. }
  140. //-----------------------------------------------------------------------------
  141. int ctkMaterialPropertyPreviewLabel::heightForWidth(int w)const
  142. {
  143. return w;
  144. }
  145. //-----------------------------------------------------------------------------
  146. QSize ctkMaterialPropertyPreviewLabel::sizeHint()const
  147. {
  148. return QSize(30,30);
  149. }
  150. //-----------------------------------------------------------------------------
  151. void ctkMaterialPropertyPreviewLabel::paintEvent(QPaintEvent* event)
  152. {
  153. Q_UNUSED(event);
  154. QImage image(this->size(), QImage::Format_ARGB32);
  155. this->draw(image);
  156. QPainter widgetPainter(this);
  157. widgetPainter.drawImage(0, 0, image);
  158. }
  159. //-----------------------------------------------------------------------------
  160. //From vtkKWMaterialPropertyWidget::CreateImage
  161. void ctkMaterialPropertyPreviewLabel::draw(QImage& image)
  162. {
  163. Q_D(ctkMaterialPropertyPreviewLabel);
  164. qreal ambient = d->Ambient;
  165. qreal diffuse = d->Diffuse;
  166. qreal specular = d->Specular;
  167. qreal specular_power = d->SpecularPower;
  168. int size = qMin(image.width(), image.height());
  169. int size8 = qMax(size / 8, 1);
  170. qreal size2 = 0.5 * size;
  171. qreal radius2 = size2*size2 - 1;
  172. QRgb rgba;
  173. for (int i = 0; i < image.width(); ++i)
  174. {
  175. for (int j = 0; j < image.height(); ++j)
  176. {
  177. qreal dist = static_cast<qreal>((i-size2)*(i-size2) + (j-size2)*(j-size2));
  178. if (dist <= radius2)
  179. {
  180. QVector3D pt;
  181. pt.setX( (i-size2) / (size2-1) );
  182. pt.setY( (j-size2) / (size2-1) );
  183. pt.setZ( sqrt(1. - pt.x()*pt.x() - pt.y()*pt.y()) );
  184. QVector3D normal = pt;
  185. normal.normalize();
  186. QVector3D light;
  187. light.setX(-5 - pt.x());
  188. light.setY(-5 - pt.y());
  189. light.setZ( 5 - pt.z());
  190. light.normalize();
  191. QVector3D view;
  192. view.setX(-pt.x());
  193. view.setY(-pt.y());
  194. view.setZ(5 - pt.z());
  195. view.normalize();
  196. qreal dot = QVector3D::dotProduct(normal, light);
  197. QVector3D ref;
  198. ref.setX( 2.*normal.x()*dot - light.x());
  199. ref.setY( 2.*normal.y()*dot - light.y());
  200. ref.setZ( 2.*normal.z()*dot - light.z());
  201. ref.normalize();
  202. qreal diffuseComp = diffuse * .01 * dot;
  203. if (diffuseComp < 0)
  204. {
  205. diffuseComp = 0.;
  206. }
  207. qreal specularDot = QVector3D::dotProduct(ref, view);
  208. if (specularDot < 0)
  209. {
  210. specularDot = 0.;
  211. }
  212. qreal specularComp = specular*pow(specularDot, specular_power);
  213. QVector3D intensity;
  214. intensity.setX( qMin((ambient + diffuseComp)*d->Color.redF() + specularComp, 1.));
  215. intensity.setY( qMin((ambient + diffuseComp)*d->Color.greenF() + specularComp, 1.));
  216. intensity.setZ( qMin((ambient + diffuseComp)*d->Color.blueF() + specularComp, 1.));
  217. rgba = qRgba(static_cast<unsigned char>(255. * intensity.x()),
  218. static_cast<unsigned char>(255. * intensity.y()),
  219. static_cast<unsigned char>(255. * intensity.z()),
  220. 255);
  221. }
  222. else
  223. {
  224. int iGrid = i / size8;
  225. int jGrid = j / size8;
  226. if (((iGrid / 2) * 2 == iGrid &&
  227. (jGrid / 2) * 2 == jGrid) ||
  228. ((iGrid / 2) * 2 != iGrid &&
  229. (jGrid / 2) * 2 != jGrid))
  230. {
  231. rgba = qRgba(0, 0, 0, d->GridOpacity * 255);
  232. }
  233. else
  234. {
  235. rgba = qRgba(255, 255, 255, d->GridOpacity * 255);
  236. }
  237. }
  238. image.setPixel(i,j,rgba);
  239. }
  240. }
  241. }