EpipolarGeometryDisplay.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // C++ Implementation: EpipolarGeometryVisualizer
  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 "EpipolarGeometryDisplay.h"
  13. #include <QGridLayout>
  14. #ifdef NICE_USELIB_GLUT
  15. #include <GL/glut.h>
  16. #endif
  17. namespace NICE {
  18. EpipolarGeometryDisplay::EpipolarGeometryDisplay ( std::vector< CoordT<double> >* points,
  19. std::vector< std::pair<CoordT<double>, CoordT<double> > >* epipolarLines,
  20. QWidget* parent, const char* name,
  21. Qt::WFlags flags )
  22. : ImageDisplay ( parent, name, flags ), m_points ( points ), m_epipolarLines ( epipolarLines )
  23. {
  24. }
  25. void EpipolarGeometryDisplay::addEpipolarLine ( const Vector& epipolarLine ) {
  26. CoordT<double> start;
  27. CoordT<double> end;
  28. bool startOK = false;
  29. bool endOK = false;
  30. if ( isZero ( epipolarLine[1], 1e-20 ) ) {
  31. double x = -epipolarLine[2] / epipolarLine[0];
  32. if ( x >= 0.0 && x <= imageWidth() - 1.0 ) {
  33. start.x = ( x / imageWidth() );
  34. start.y = ( 1.0 );
  35. startOK = true;
  36. end.x = ( x / imageWidth() );
  37. end.y = ( 0.0 );
  38. endOK = true;
  39. }
  40. } else {
  41. double x1 = - epipolarLine[2] / epipolarLine[0];
  42. double x2 = - epipolarLine[1] / epipolarLine[0] * imageHeight() + x1;
  43. double y1 = - epipolarLine[2] / epipolarLine[1];
  44. double y2 = - epipolarLine[0] / epipolarLine[1] * imageWidth() + y1;
  45. if ( x1 >= 0.0 && x1 <= imageWidth() ) {
  46. if ( !startOK ) {
  47. start.x = ( x1 / imageWidth() );
  48. start.y = ( 1.0 );
  49. startOK = true;
  50. } else if ( !endOK ) {
  51. end.x = ( x1 / imageWidth() );
  52. end.y = ( 1.0 );
  53. endOK = true;
  54. }
  55. }
  56. if ( x2 >= 0.0 && x2 <= imageWidth() ) {
  57. if ( !startOK ) {
  58. start.x = ( x2 / imageWidth() );
  59. start.y = ( 0.0 );
  60. startOK = true;
  61. } else if ( !endOK ) {
  62. end.x = ( x2 / imageWidth() );
  63. end.y = ( 0.0 );
  64. endOK = true;
  65. }
  66. }
  67. if ( y1 >= 0.0 && y1 <= imageHeight() ) {
  68. if ( !startOK ) {
  69. start.x = ( 0.0 );
  70. start.y = ( 1.0f - y1 / imageHeight() );
  71. startOK = true;
  72. } else if ( !endOK ) {
  73. end.x = ( 0.0 );
  74. end.y = ( 1.0f - y1 / imageHeight() );
  75. endOK = true;
  76. }
  77. }
  78. if ( y2 >= 0.0 && y2 <= imageHeight() ) {
  79. if ( !startOK ) {
  80. start.x = ( 1.0 );
  81. start.y = ( 1.0f - y2 / imageHeight() );
  82. startOK = true;
  83. } else if ( !endOK ) {
  84. end.x = ( 1.0 );
  85. end.y = ( 1.0f - y2 / imageHeight() );
  86. endOK = true;
  87. }
  88. }
  89. }
  90. if ( startOK && endOK )
  91. m_epipolarLines->push_back ( std::pair<CoordT<double> , CoordT<double> > ( start, end ) );
  92. }
  93. void EpipolarGeometryDisplay::mousePressEvent ( QMouseEvent* event )
  94. {
  95. QGLWidget::mousePressEvent ( event );
  96. switch ( event->button() )
  97. {
  98. case Qt::LeftButton:
  99. {
  100. m_points->push_back ( CoordT<double> ( static_cast<float> ( event->x() ) / width(),
  101. 1.0f - static_cast<float> ( event->y() ) / height() ) );
  102. updateGL();
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. void EpipolarGeometryDisplay::setGLProjection ( void ) {
  110. #ifdef NICE_USELIB_OPENGL
  111. glViewport ( 0, 0, width(), height() );
  112. glMatrixMode ( GL_PROJECTION );
  113. glLoadIdentity();
  114. # ifdef NICE_USELIB_GLUT
  115. gluOrtho2D ( 0.0, ( GLdouble ) width(), 0.0, ( GLdouble ) height() );
  116. # else
  117. fthrow(Exception,"GLUT lib not availabe, recompile using GLUT!");
  118. # endif
  119. #else
  120. fthrow(Exception,"OpenGL lib not availabe, recompile using OpenGL!");
  121. #endif
  122. }
  123. void EpipolarGeometryDisplay::paintGLObjects ( void ) {
  124. #ifdef NICE_USELIB_OPENGL
  125. if ( m_points->size() > 0 ) {
  126. glPointSize ( 3 );
  127. glBegin ( GL_POINTS );
  128. for ( unsigned int i = 0; i < m_points->size(); ++i ) {
  129. float xPos = m_points->operator[] ( i ).x*width();
  130. float yPos = m_points->operator[] ( i ).y*height();
  131. glVertex2f ( xPos, yPos );
  132. }
  133. glEnd();
  134. glFlush();
  135. }
  136. if ( m_epipolarLines->size() > 0 ) {
  137. glEnable ( GL_LINE_SMOOTH );
  138. glEnable ( GL_BLEND );
  139. glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  140. glHint ( GL_LINE_SMOOTH_HINT, GL_DONT_CARE );
  141. glLineWidth ( 2 );
  142. glColor4f ( 1, 1, 1, 1 );
  143. for ( unsigned int i = 0; i < m_epipolarLines->size(); ++i ) {
  144. glBegin ( GL_LINE_LOOP );
  145. float xPos = m_epipolarLines->operator[] ( i ).first.x*width();
  146. float yPos = m_epipolarLines->operator[] ( i ).first.y*height();
  147. glVertex2f ( xPos, yPos );
  148. xPos = m_epipolarLines->operator[] ( i ).second.x*width();
  149. yPos = m_epipolarLines->operator[] ( i ).second.y*height();
  150. glVertex2f ( xPos, yPos );
  151. glEnd();
  152. }
  153. glFlush();
  154. }
  155. #else
  156. fthrow(Exception,"OpenGL lib not availabe, recompile using OpenGL!");
  157. #endif
  158. }
  159. void EpipolarGeometryDisplay::contextMenuEvent ( QContextMenuEvent* event ) {
  160. }
  161. }