ArrayPlot.cpp 4.0 KB

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