select2DTool.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00039 
00040 #include <pcl/apps/point_cloud_editor/select2DTool.h>
00041 #include <pcl/apps/point_cloud_editor/cloud.h>
00042 #include <pcl/apps/point_cloud_editor/selection.h>
00043 
00044 const float Select2DTool::DEFAULT_TOOL_DISPLAY_SIZE_ = 2.0f;
00045 
00046 const float Select2DTool::DEFAULT_TOOL_DISPLAY_COLOR_RED_ = 1.0f;
00047 const float Select2DTool::DEFAULT_TOOL_DISPLAY_COLOR_GREEN_ = 1.0f;
00048 const float Select2DTool::DEFAULT_TOOL_DISPLAY_COLOR_BLUE_ = 1.0f;
00049 
00050 
00051 Select2DTool::Select2DTool (SelectionPtr selection_ptr, CloudPtr cloud_ptr)
00052   : selection_ptr_(selection_ptr), cloud_ptr_(cloud_ptr), display_box_(false)
00053 {
00054 }
00055 
00056 Select2DTool::~Select2DTool ()
00057 {
00058 }
00059 
00060 void
00061 Select2DTool::start (int x, int y, BitMask, BitMask)
00062 {
00063   if (!cloud_ptr_)
00064     return;
00065   origin_x_ = x;
00066   origin_y_ = y;
00067 }
00068 
00069 void
00070 Select2DTool::update (int x, int y, BitMask, BitMask)
00071 {
00072   if (!cloud_ptr_)
00073     return;
00074   final_x_ = x;
00075   final_y_ = y;
00076   display_box_ = true;
00077 }
00078 
00079 void
00080 Select2DTool::end (int x, int y, BitMask modifiers, BitMask)
00081 {
00082   if (!cloud_ptr_)
00083     return;
00084   final_x_ = x;
00085   final_y_ = y;
00086   display_box_ = false;
00087   // don't select anything if we don't have a selection box.
00088   if ((final_x_ == origin_x_) || (final_y_ == origin_y_))
00089     return;
00090 
00091   GLint viewport[4];
00092   glGetIntegerv(GL_VIEWPORT,viewport);
00093   IndexVector indices;
00094   GLfloat project[16];
00095   glGetFloatv(GL_PROJECTION_MATRIX, project);
00096 
00097   std::vector<Point3D> ptsvec;
00098   cloud_ptr_->getDisplaySpacePoints(ptsvec);
00099   for(unsigned int i = 0; i < ptsvec.size(); ++i)
00100   {
00101     Point3D pt = ptsvec[i];
00102     if (isInSelectBox(pt, project, viewport))
00103       indices.push_back(i);
00104   }
00105 
00106   if (modifiers & SHFT)
00107   {
00108     selection_ptr_->addIndex(indices);
00109   }
00110   else if (modifiers & CTRL)
00111   {
00112     selection_ptr_->removeIndex(indices);
00113   }
00114   else
00115   {
00116     selection_ptr_->clear();
00117     selection_ptr_->addIndex(indices);
00118   }
00119   cloud_ptr_->setSelection(selection_ptr_);
00120 }
00121 
00122 bool
00123 Select2DTool::isInSelectBox (const Point3D& pt,
00124                              const GLfloat* project,
00125                              const GLint* viewport) const
00126 {
00127   float w = pt.z * project[11];
00128   float x = (pt.x * project[0] + pt.z * project[8]) / w;
00129   float y = (pt.y * project[5] + pt.z * project[9]) / w;
00130   float min_x = std::min(origin_x_, final_x_)/(viewport[2]*0.5) - 1.0;
00131   float max_x = std::max(final_x_, origin_x_)/(viewport[2]*0.5) - 1.0;
00132   float max_y = (viewport[3] - std::min(origin_y_, final_y_))/(viewport[3]*0.5) - 1.0;
00133   float min_y = (viewport[3] - std::max(origin_y_, final_y_))/(viewport[3]*0.5) - 1.0;
00134   // Check the left and right sides
00135   if ((x < min_x) || (x > max_x))
00136     return (false);
00137   // Check the bottom and top
00138   if ((y < min_y) || (y > max_y))
00139     return (false);
00140   return (true);
00141 }
00142 
00143 void
00144 Select2DTool::draw() const
00145 {
00146   if (!display_box_)
00147     return;
00148   GLint viewport[4];
00149   glGetIntegerv(GL_VIEWPORT, viewport);
00150   // draw the selection box
00151   drawRubberBand(viewport);
00152   // highlight all the points in the rubberband
00153   highlightPoints(viewport);
00154 }
00155 
00156 void
00157 Select2DTool::drawRubberBand (GLint* viewport) const
00158 {
00159   // set the line width of the rubberband
00160   glLineWidth(DEFAULT_TOOL_DISPLAY_SIZE_);
00161   // set the color of the rubberband
00162   glColor3f(DEFAULT_TOOL_DISPLAY_COLOR_RED_,
00163             DEFAULT_TOOL_DISPLAY_COLOR_GREEN_,
00164             DEFAULT_TOOL_DISPLAY_COLOR_BLUE_);
00165   glMatrixMode(GL_PROJECTION);
00166   glPushMatrix();
00167   {
00168     glLoadIdentity();
00169     glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
00170     glMatrixMode(GL_MODELVIEW);
00171     glPushMatrix();
00172     {
00173       glLoadIdentity();
00174       glLineStipple (3, 0x8888);
00175       glEnable(GL_LINE_STIPPLE);
00176       glBegin(GL_LINE_LOOP);
00177       {
00178         glVertex2d(origin_x_, origin_y_); // Top Left
00179         glVertex2d(final_x_,  origin_y_); // Top Right
00180         glVertex2d(final_x_,  final_y_);  // Bottom Right
00181         glVertex2d(origin_x_, final_y_);  // Bottom Left
00182       }
00183       glEnd();
00184       glDisable(GL_LINE_STIPPLE);
00185     }
00186     glPopMatrix();
00187     glMatrixMode(GL_PROJECTION);
00188   }
00189   glPopMatrix();
00190   glMatrixMode(GL_MODELVIEW);
00191 }
00192 
00193 void
00194 Select2DTool::highlightPoints (GLint* viewport) const
00195 {
00196   double width = abs(origin_x_ - final_x_);
00197   double height = abs(origin_y_ - final_y_);
00198   glPushAttrib(GL_SCISSOR_BIT);
00199   {
00200     glEnable(GL_SCISSOR_TEST);
00201     glScissor(std::min(origin_x_, final_x_),
00202               std::min(viewport[3] - final_y_, viewport[3] - origin_y_),
00203               width, height);
00204     cloud_ptr_ -> drawWithHighlightColor();
00205   }
00206   glPopAttrib();
00207 }
00208 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:33:22