SelectionToolDisk.cpp 5.65 KB
 Frédéric Larue committed Apr 06, 2018 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ``````/* * (c) LSIIT, UMR CNRS/UdS * Authors: F. Larue. * * See licence.txt for additional information. */ #include #include SelectionToolDisk::SelectionToolDisk( GLViewer &viewer, SelectionTool::Context &context ) : SelectionTool(viewer,context), m_Radius(0.1f) { m_CircleVertices.resize( 32 ); } void SelectionToolDisk::updateCircleVertices() { const float pixelUnit = m_Radius * m_Viewer.width(); const float threshold = 0.5f / pixelUnit; // Compute the maximum distance (in pixels) between polygon segments and the circle arcs they approximate. int nSteps = m_CircleVertices.size(); float deltaPx = 1.0f - std::cos(M_PI/nSteps); // If this distance exceeds a threshold, the number of segments is increased. // Conversely, if the distance is below the threshold, the segment count is decreased. if( deltaPx >= threshold ) while( deltaPx >= threshold ) deltaPx = 1.0f - std::cos(M_PI/(++nSteps)); else { while( deltaPx < threshold ) deltaPx = 1.0f - std::cos(M_PI/(--nSteps)); ++ nSteps; } // Polygon vertex coordinates are recomputed accordingly. m_CircleVertices.resize( nSteps ); for( int i=0; i 1.0f || m_Center.y() < -1.0f || m_Center.y() > 1.0f ) return; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( m_Center.x(), m_Center.y(), 0.0f ); glScalef( 1.0f, float(m_Viewer.width()) / m_Viewer.height(), 1.0f ); glEnable( GL_MULTISAMPLE ); glDisable( GL_LIGHTING ); glDisable( GL_DEPTH_TEST ); glLineStipple( 1, 0xF3F3 ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glDisable( GL_LINE_STIPPLE ); glColor4ub( 64, 64, 64, 128 ); glLineWidth( 5.0f ); glBegin( GL_LINE_LOOP ); for( auto &v : m_CircleVertices ) glVertex2fv( &v[0] ); glEnd(); glDisable( GL_BLEND ); glEnable( GL_LINE_STIPPLE ); glColor3ub( 255, 255, 255 ); glLineWidth( 1.5f ); glBegin( GL_LINE_LOOP ); for( auto &v : m_CircleVertices ) glVertex2fv( &v[0] ); glEnd(); glDisable( GL_LINE_STIPPLE ); } void SelectionToolDisk::mousePressEvent( QMouseEvent *evt ) { m_Center = m_Viewer.screenPosToClipCoord( m_Viewer.mapFromGlobal( m_Viewer.cursor().pos() ) ); m_Axis = QVector2D( 0.0f, 0.0f ); if( evt->buttons() == Qt::LeftButton ) { m_Context.isSelecting = true; `````` Frédéric Larue committed Sep 19, 2018 164 `````` m_Context.selectionAction = ON_MOUSE_CLICK; `````` Frédéric Larue committed Apr 06, 2018 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 `````` if( m_Context.selectionMode == SelectionTool::SELECTION_MODE_SET ) m_Viewer.clearSelection( m_Context.entityToSelect ); m_Viewer.updateSelection( *this ); } } void SelectionToolDisk::mouseReleaseEvent( QMouseEvent *evt ) { if( evt->button() == Qt::LeftButton ) m_Context.isSelecting = false; } void SelectionToolDisk::mouseMoveEvent( QMouseEvent *evt ) { QVector2D newCenter = m_Viewer.screenPosToClipCoord( m_Viewer.mapFromGlobal( m_Viewer.cursor().pos() ) ); m_Axis = m_Center - newCenter; m_Center = newCenter; if( evt->buttons() & Qt::LeftButton ) `````` Frédéric Larue committed Sep 19, 2018 188 189 `````` { m_Context.selectionAction = ON_MOUSE_MOTION; `````` Frédéric Larue committed Apr 06, 2018 190 `````` m_Viewer.updateSelection( *this ); `````` Frédéric Larue committed Sep 19, 2018 191 `````` } `````` Frédéric Larue committed Apr 06, 2018 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 ``````} void SelectionToolDisk::wheelEvent( QWheelEvent* evt ) { if(evt->orientation() == Qt::Vertical) { float deltaD = -0.08 * m_Viewer.speedFactor() * m_Radius * evt->delta() / std::abs(evt->delta()); if( m_Radius + deltaD > 0.0f ) { m_Radius += deltaD; updateCircleVertices(); } } }``````