ArrayPlot.cpp 4.0 KB

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