ArrayPlot.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // C++ Implementation: ArrayPlot
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: Marcel Brueckner <brueckner [at] informatik [dot] uni-jena [dot] de>, (C) 2008
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. //
  12. #include "ArrayPlot.h"
  13. #include <iostream>
  14. #include <core/basics/numerictools.h>
  15. #ifdef NICE_USELIB_GLUT
  16. #include <GL/glut.h>
  17. #endif
  18. namespace NICE {
  19. ArrayPlot::ArrayPlot ( std::vector<double> probs,
  20. unsigned int width,
  21. unsigned int height,
  22. bool invertColors,
  23. QWidget* parent,
  24. const char* name ,
  25. Qt::WFlags flags )
  26. : QGLWidget ( parent, name, NULL, flags ),
  27. m_width ( width ),
  28. m_height ( height ),
  29. m_maxProb ( -std::numeric_limits<double>::infinity() ),
  30. m_minProb ( std::numeric_limits<double>::infinity() ),
  31. m_probs ( probs ),
  32. m_xPos ( 0 ),
  33. m_yPos ( 0 ),
  34. m_invertColors ( invertColors )
  35. {
  36. if ( ( m_width * m_height ) != m_probs.size() )
  37. std::cerr << "MProbDisplay: INCONSISTENT SIZES!" << std::endl;
  38. for ( unsigned int i = 0 ; i < m_probs.size(); ++i ) {
  39. if ( m_probs[i] > m_maxProb )
  40. m_maxProb = m_probs[i];
  41. if ( m_probs[i] < m_minProb )
  42. m_minProb = m_probs[i];
  43. }
  44. };
  45. ArrayPlot::~ArrayPlot() {
  46. }
  47. void ArrayPlot::paintGL ( void ) {
  48. glViewport ( 0, 0, width(), height() );
  49. glMatrixMode ( GL_PROJECTION );
  50. glLoadIdentity();
  51. gluOrtho2D ( 0.0, ( GLdouble ) width(), 0.0, ( GLdouble ) height() );
  52. double quadWidth = width() / ( double ) ( m_width );
  53. double quadHeight = height() / ( double ) ( m_height );
  54. double difference = m_maxProb - m_minProb;
  55. glEnable ( GL_BLEND );
  56. glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  57. if ( isZero ( difference, 1e-20 ) )
  58. difference = 1.0;
  59. for ( unsigned int i = 0; i < m_width; ++i ) {
  60. for ( unsigned int j = 0; j < m_height; ++j ) {
  61. float value = ( m_probs[i * m_height + j] - m_minProb ) / ( m_maxProb - m_minProb );
  62. double r, g, b;
  63. toColor ( value, r, g, b );
  64. glColor4f ( r, g, b, 1 );
  65. glBegin ( GL_QUADS );
  66. glVertex2f ( quadWidth*i, ( j + 1 ) *quadHeight );
  67. glVertex2f ( quadWidth*i, j*quadHeight );
  68. glVertex2f ( quadWidth* ( i + 1 ), j*quadHeight );
  69. glVertex2f ( quadWidth* ( i + 1 ), ( j + 1 ) *quadHeight );
  70. glEnd();
  71. }
  72. }
  73. glEnable ( GL_LINE_SMOOTH );
  74. glHint ( GL_LINE_SMOOTH_HINT, GL_DONT_CARE );
  75. glLineWidth ( 3 );
  76. glColor4f ( 1, 1, 1, 1 );
  77. glBegin ( GL_LINE_LOOP );
  78. glVertex2f ( quadWidth*m_xPos, ( m_yPos + 1 ) *quadHeight );
  79. glVertex2f ( quadWidth*m_xPos, m_yPos*quadHeight );
  80. glVertex2f ( quadWidth* ( m_xPos + 1 ), m_yPos*quadHeight );
  81. glVertex2f ( quadWidth* ( m_xPos + 1 ), ( m_yPos + 1 ) *quadHeight );
  82. glEnd();
  83. glFlush();
  84. };
  85. void ArrayPlot::mousePressEvent ( QMouseEvent* event )
  86. {
  87. QGLWidget::mousePressEvent ( event );
  88. switch ( event->button() )
  89. {
  90. case Qt::LeftButton:
  91. {
  92. m_xPos = static_cast<int> ( double ( event->x() * m_width ) / width() );
  93. m_yPos = static_cast<int> ( double ( ( height() - event->y() ) * m_height ) / height() );
  94. updateGL();
  95. }
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. void ArrayPlot::repaint() {
  102. updateGL();
  103. }
  104. void ArrayPlot::toColor ( double x, double & r, double & g, double & b )
  105. {
  106. if ( m_invertColors )
  107. x = 1.0 - x;
  108. int sector = static_cast<int> ( x * 4.0 );
  109. x = 4.0 * x - sector;
  110. switch ( sector ) {
  111. case 0:
  112. r = 1.0;
  113. g = x;
  114. b = 0.0;
  115. break;
  116. case 1:
  117. r = 1.0 - ( x );
  118. g = 1.0;
  119. b = 0.0;
  120. break;
  121. case 2:
  122. r = 0.0;
  123. g = 1.0;
  124. b = x;
  125. break;
  126. case 3:
  127. r = 0.0;
  128. g = 1.0 - ( x );
  129. b = 1.0;
  130. break;
  131. default:
  132. r = 0.0;
  133. g = 0.0;
  134. b = 1.0;
  135. break;
  136. }
  137. }
  138. }