Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

MetricGrid.cpp 8.15 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
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GLViewer.h"
#include "MetricGrid.h"
#include <cmath>
#include <sstream>




void MetricGrid::displayGrid( GLViewer *viewer,
                              const double spacing,
                              const GLubyte alpha )
{
    const double spacing2 = 2.0 * spacing;
    const double step = 0.1 * spacing;

    // Display the points representing the current grid scale.
    glPointSize( 5.0f );
    glBegin( GL_POINTS );
        if( isAxisEnabled(X) )
        {
            glColor4ub( 255, 100, 100, alpha );
            glVertex3d(  spacing, 0, 0 );
            glVertex3d( -spacing, 0, 0 );
        }
        if( isAxisEnabled(Y) )
        {
            glColor4ub( 100, 255, 100, alpha );
            glVertex3d( 0,  spacing, 0 );
            glVertex3d( 0, -spacing, 0 );
        }
        if( isAxisEnabled(Z) )
        {
            glColor4ub( 100, 100, 255, alpha );
            glVertex3d( 0, 0,  spacing );
            glVertex3d( 0, 0, -spacing );
        }
    glEnd();

    // display the lines corresponding to the colored axis.
48
    glLineWidth( 1.5f );
49
50
51
    glBegin( GL_LINES );
        if( isAxisEnabled(X) )
        {
52
            glColor4ub( 255, 100, 100, 128 );
53
54
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
55
            glColor4ub( 255, 100, 100, alpha/2 );
56
57
58
59
60
61
62
            glVertex3d( -spacing2, 0, 0 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glVertex3d(  spacing2, 0, 0 );
        }
        else if( isGridEnabled(XY) || isGridEnabled(ZX) )
        {
63
            glColor4ub( 220, 220, 220, 128 );
64
65
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
66
            glColor4ub( 220, 220, 220, alpha/2 );
67
68
69
70
71
72
73
            glVertex3d( -spacing2, 0, 0 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glVertex3d(  spacing2, 0, 0 );
        }
        if( isAxisEnabled(Y) )
        {
74
            glColor4ub( 100, 255, 100, 128 );
75
76
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
77
            glColor4ub( 100, 255, 100, alpha/2 );
78
79
80
81
82
83
84
            glVertex3d( 0, -spacing2, 0 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glVertex3d( 0,  spacing2, 0 );
        }
        else if( isGridEnabled(XY) || isGridEnabled(YZ) )
        {
85
            glColor4ub( 220, 220, 220, 128 );
86
87
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
88
            glColor4ub( 220, 220, 220, alpha/2 );
89
90
91
92
93
94
95
            glVertex3d( 0, -spacing2, 0 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glVertex3d( 0,  spacing2, 0 );
        }
        if( isAxisEnabled(Z) )
        {
96
            glColor4ub( 100, 100, 255, 128 );
97
98
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
99
            glColor4ub( 100, 100, 255, alpha/2 );
100
101
102
103
104
105
106
            glVertex3d( 0, 0, -spacing2 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glVertex3d( 0, 0,  spacing2 );
        }
        else if( isGridEnabled(YZ) || isGridEnabled(ZX) )
        {
107
            glColor4ub( 220, 220, 220, 128 );
108
109
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
110
            glColor4ub( 220, 220, 220, alpha/2 );
111
112
113
114
115
116
117
118
119
            glVertex3d( 0, 0, -spacing2 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glVertex3d( 0, 0,  spacing2 );
        }
    glEnd();

    // Display lines corresponding to the grid spacing.
    glLineWidth( 1.0f );
120
    glColor4ub( 220, 220, 220, alpha/4 );
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
164
165
166
167
    glBegin( GL_LINES );
    if( isGridEnabled(XY) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( -spacing, f, 0 );
            glVertex3d(  spacing, f, 0 );
            glVertex3d( f, -spacing, 0 );
            glVertex3d( f,  spacing, 0 );
            glVertex3d( -spacing, -f, 0 );
            glVertex3d(  spacing, -f, 0 );
            glVertex3d( -f, -spacing, 0 );
            glVertex3d( -f,  spacing, 0 );
        }
    if( isGridEnabled(YZ) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( 0, -spacing, f );
            glVertex3d( 0,  spacing, f );
            glVertex3d( 0, f, -spacing );
            glVertex3d( 0, f,  spacing );
            glVertex3d( 0, -spacing, -f );
            glVertex3d( 0,  spacing, -f );
            glVertex3d( 0, -f, -spacing );
            glVertex3d( 0, -f,  spacing );
        }
    if( isGridEnabled(ZX) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( -spacing, 0, f );
            glVertex3d(  spacing, 0, f );
            glVertex3d( f, 0, -spacing );
            glVertex3d( f, 0,  spacing );
            glVertex3d( -spacing, 0, -f );
            glVertex3d(  spacing, 0, -f );
            glVertex3d( -f, 0, -spacing );
            glVertex3d( -f, 0,  spacing );
        }
    glEnd();

    // Display labels representing the current grid scale.
    std::stringstream strStream;
    strStream << spacing;
    QString spacingStr = QString::fromStdString( strStream.str() );

168
169
170
171
    QVector3D camCenter = viewer->viewpointLocation();
    QVector3D camFront  = viewer->viewRotationMatrix().row(2).toVector3D();

    if( isAxisEnabled(X)  &&  QVector3D::dotProduct( QVector3D(spacing,0,0)-camCenter, camFront ) < 0.0f )
172
    {
173
174
175
        float alphaX = 1.0f - std::abs(camFront.x());
        alphaX = std::max( 0.0f, std::min( 1.0f, (alphaX-0.025f)*10.0f ) );
        glColor4ub( 255, 130, 130, alpha*alphaX );
176
177
        viewer->renderText( spacing, spacing/40.0, spacing/40.0, "X = "+spacingStr );
    }
178
    if( isAxisEnabled(Y)  &&  QVector3D::dotProduct( QVector3D(0,spacing,0)-camCenter, camFront ) < 0.0f )
179
    {
180
181
182
        float alphaY = 1.0f - std::abs(camFront.y());
        alphaY = std::max( 0.0f, std::min( 1.0f, (alphaY-0.025f)*10.0f ) );
        glColor4ub( 200, 255, 200, alpha*alphaY );
183
184
        viewer->renderText( spacing/40.0, spacing, spacing/40.0, "Y = "+spacingStr );
    }
185
    if( isAxisEnabled(Z)  &&  QVector3D::dotProduct( QVector3D(0,0,spacing)-camCenter, camFront ) < 0.0f )
186
    {
187
188
189
        float alphaZ = 1.0f - std::abs(camFront.z());
        alphaZ = std::max( 0.0f, std::min( 1.0f, (alphaZ-0.025f)*10.0f ) );
        glColor4ub( 130, 130, 255, alpha*alphaZ );
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        viewer->renderText( spacing/40.0, spacing/40.0, spacing, "Z = "+spacingStr );
    }
}


void MetricGrid::display( GLViewer *viewer )
{
    glPushAttrib( GL_LIGHTING         |
                  GL_CURRENT_BIT      |
                  GL_LINE_BIT         |
                  GL_POINT_BIT        |
                  GL_HINT_BIT         |
                  GL_COLOR_BUFFER_BIT );


    // Determine the current grid spacing wrt. the distance between the viewpoint and the focus point.
    double log10Dist = std::log10( 1.75*viewer->distToFocusPoint() );
    int log10DistI = (int) std::floor( log10Dist );
    double scaleFactor = std::pow( 10.0, (double)log10DistI );


    // Determine the alpha value for smooth transitions between successive grid scales.
    GLubyte alpha;
    if( log10Dist-log10DistI < 0.5 )
    {
        double t = (log10Dist-log10DistI) / 0.5;
        t = 0.5 + 0.5*std::cos( t*3.1415926535897932384626433832795 );
        alpha = (GLubyte)( 255.0*t*t );
    }
    else
        alpha = 0;


    // Display the grid using three different grid spacings.
    glDisable( GL_LIGHTING );
    glEnable( GL_LINE_SMOOTH );
    glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
    glEnable( GL_POINT_SMOOTH );
    glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );

    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    if( alpha != 255 )
        displayGrid( viewer, 10.0*scaleFactor, 255-alpha );
    displayGrid( viewer, scaleFactor, 255 );
    if( alpha )
        displayGrid( viewer, 0.1*scaleFactor, alpha );


    glPopAttrib();
}