ArrayPlot.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifdef NICE_USELIB_GLUT
  52. gluOrtho2D ( 0.0, ( GLdouble ) width(), 0.0, ( GLdouble ) height() );
  53. #endif
  54. double quadWidth = width() / ( double ) ( m_width );
  55. double quadHeight = height() / ( double ) ( m_height );
  56. double difference = m_maxProb - m_minProb;
  57. glEnable ( GL_BLEND );
  58. glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  59. if ( isZero ( difference, 1e-20 ) )
  60. difference = 1.0;
  61. for ( unsigned int i = 0; i < m_width; ++i ) {
  62. for ( unsigned int j = 0; j < m_height; ++j ) {
  63. float value = ( m_probs[i * m_height + j] - m_minProb ) / ( m_maxProb - m_minProb );
  64. double r, g, b;
  65. toColor ( value, r, g, b );
  66. glColor4f ( r, g, b, 1 );
  67. glBegin ( GL_QUADS );
  68. glVertex2f ( quadWidth*i, ( j + 1 ) *quadHeight );
  69. glVertex2f ( quadWidth*i, j*quadHeight );
  70. glVertex2f ( quadWidth* ( i + 1 ), j*quadHeight );
  71. glVertex2f ( quadWidth* ( i + 1 ), ( j + 1 ) *quadHeight );
  72. glEnd();
  73. }
  74. }
  75. glEnable ( GL_LINE_SMOOTH );
  76. glHint ( GL_LINE_SMOOTH_HINT, GL_DONT_CARE );
  77. glLineWidth ( 3 );
  78. glColor4f ( 1, 1, 1, 1 );
  79. glBegin ( GL_LINE_LOOP );
  80. glVertex2f ( quadWidth*m_xPos, ( m_yPos + 1 ) *quadHeight );
  81. glVertex2f ( quadWidth*m_xPos, m_yPos*quadHeight );
  82. glVertex2f ( quadWidth* ( m_xPos + 1 ), m_yPos*quadHeight );
  83. glVertex2f ( quadWidth* ( m_xPos + 1 ), ( m_yPos + 1 ) *quadHeight );
  84. glEnd();
  85. glFlush();
  86. };
  87. void ArrayPlot::mousePressEvent ( QMouseEvent* event )
  88. {
  89. QGLWidget::mousePressEvent ( event );
  90. switch ( event->button() )
  91. {
  92. case Qt::LeftButton:
  93. {
  94. m_xPos = static_cast<int> ( double ( event->x() * m_width ) / width() );
  95. m_yPos = static_cast<int> ( double ( ( height() - event->y() ) * m_height ) / height() );
  96. updateGL();
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. void ArrayPlot::repaint() {
  104. updateGL();
  105. }
  106. void ArrayPlot::toColor ( double x, double & r, double & g, double & b )
  107. {
  108. if ( m_invertColors )
  109. x = 1.0 - x;
  110. int sector = static_cast<int> ( x * 4.0 );
  111. x = 4.0 * x - sector;
  112. switch ( sector ) {
  113. case 0:
  114. r = 1.0;
  115. g = x;
  116. b = 0.0;
  117. break;
  118. case 1:
  119. r = 1.0 - ( x );
  120. g = 1.0;
  121. b = 0.0;
  122. break;
  123. case 2:
  124. r = 0.0;
  125. g = 1.0;
  126. b = x;
  127. break;
  128. case 3:
  129. r = 0.0;
  130. g = 1.0 - ( x );
  131. b = 1.0;
  132. break;
  133. default:
  134. r = 0.0;
  135. g = 0.0;
  136. b = 1.0;
  137. break;
  138. }
  139. }
  140. }