SelectionToolDisk.cpp 5.65 KB
Newer Older
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 <GLViewer.h>
#include <SelectionToolDisk.h>




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<nSteps; ++i )
    {
        float theta = 2.0f * M_PI * i / nSteps;
        m_CircleVertices[i].setX( std::cos(theta)*m_Radius );
        m_CircleVertices[i].setY( std::sin(theta)*m_Radius );
    }
}


std::string SelectionToolDisk::getSelectionShaderFunctions()
{
61
    return  SelectionTool::getSelectionShaderFunctions() +
62

63
            "uniform vec2 u_DiskCenter;"
64 65 66 67 68 69 70 71
            "uniform vec2 u_DiskAxis;"
            "uniform float u_DiskAxisLen;"
            "uniform vec2 u_DiskRadiusInv;"

            "bool isInsideSelectionArea( vec3 clipCoordVertex )"
            "{"
            "   vec2 toCenter = clipCoordVertex.xy*u_DiskRadiusInv - u_DiskCenter;"
            "   float distAlongAxis = dot( toCenter, u_DiskAxis );"
72
            "   _toBrushCenter = max( min(distAlongAxis,u_DiskAxisLen), 0.0 )*u_DiskAxis - toCenter;"
73 74
            "   _squareDistanceToBrush = dot( _toBrushCenter, _toBrushCenter );"
            "   return _squareDistanceToBrush <= 1.0;"
75
            "}";
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}


void SelectionToolDisk::initSelectionShader( GPU::Shader &shader )
{
    SelectionTool::initSelectionShader( shader );

    QVector2D center( m_Center.x()*m_ScaledRadiusInv.x(), m_Center.y()*m_ScaledRadiusInv.y() );
    QVector2D axis( m_Axis.x()*m_ScaledRadiusInv.x(), m_Axis.y()*m_ScaledRadiusInv.y() );
    float axisLen = axis.length();
    if( axisLen )
        axis /= axisLen;

    shader.SetUniform( "u_DiskCenter"   , &center[0] );
    shader.SetUniform( "u_DiskAxis"     , &axis[0] );
    shader.SetUniform( "u_DiskAxisLen"  , &axisLen );
    shader.SetUniform( "u_DiskRadiusInv", &m_ScaledRadiusInv[0] );
}


void SelectionToolDisk::onEnabled()
{
    m_Center = m_Viewer.screenPosToClipCoord( m_Viewer.mapFromGlobal( m_Viewer.cursor().pos() ) );
    updateCircleVertices();
}


void SelectionToolDisk::preSelectionUpdate()
{
    float aspectRatio = (float) m_Viewer.width() / m_Viewer.height();

    m_ScaledRadiusInv.setX( 1.0f / m_Radius );
    m_ScaledRadiusInv.setY( 1.0f / (m_Radius*aspectRatio) );
}


void SelectionToolDisk::display()
{
    if( m_Center.x() < -1.0f  ||  m_Center.x() > 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;
164
        m_Context.selectionAction = ON_MOUSE_CLICK;
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 )
188 189
    {
        m_Context.selectionAction = ON_MOUSE_MOTION;
190
        m_Viewer.updateSelection( *this );
191
    }
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();
        }
    }
}