EpipolarGeometryDisplay.cpp 5.0 KB

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