1 /*****************************************************************************
2  *
3  *  Elmer, A Finite Element Software for Multiphysical Problems
4  *
5  *  Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program (in file fem/GPL-2); if not, write to the
19  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301, USA.
21  *
22  *****************************************************************************/
23 
24 /*******************************************************************************
25  *
26  * Action routines for the colscale visual class.
27  *
28  *******************************************************************************
29  *
30  *                     Author:       Juha Ruokolainen
31  *
32  *                    Address: CSC - IT Center for Science Ltd.
33  *                                Keilaranta 14, P.O. BOX 405
34  *                                  02101 Espoo, Finland
35  *                                  Tel. +358 0 457 2723
36  *                                Telefax: +358 0 457 2302
37  *                              EMail: Juha.Ruokolainen@csc.fi
38  *
39  *                       Date: 15 Jan 1996
40  *
41  *
42  * Modification history:
43  *
44  *
45  ******************************************************************************/
46 
47 #include "../elmerpost.h"
48 
49 
50 /******************************************************************************
51  *
52  * Parameter sructure definitios for colscale visual class
53  *
54  ******************************************************************************/
55 
56 typedef struct colscale_s
57 {
58     double Length,Thickness,XPosition,YPosition,FontSize;
59     int Entries,Decimals,Style,FontColor;
60 
61     scalar_t   *ColorData;
62     colormap_t *ColorMap;
63     material_t *Material;
64 } colscale_t;
65 
66 
67 /*******************************************************************************
68  *
69  *     Name:        vis_colscale
70  *
71  *     Purpose:     draw colscale as lines or surface, with color codes or not
72  *
73  *     Parameters:
74  *
75  *         Input:   (geometry_t *) triangles to draw
76  *                  (colscale_t     *) colscale display parameters
77  *                  (double)
78  *
79  *         Output:  graphics
80  *
81  *   Return value:  if mouse interaction is going on, and time used exeeds
82  *                  given value (TooLong1,2) FALSE, otherwise TRUE
83  *
84  ******************************************************************************/
vis_colscale(geometry_t * geometry,element_model_t * model,colscale_t * ColorScale,double dt)85 static int vis_colscale( geometry_t *geometry, element_model_t *model,
86                     colscale_t *ColorScale,double dt )
87 {
88     scalar_t *ColorData = ColorScale->ColorData;
89 
90     static char str[120],fmt[120];
91     int i,j,n;
92 
93     double CScl,CAdd,*C,y,xl;
94 
95     GLboolean clip[6], lights_on;
96 
97     float coords[4][3];
98 
99     if ( epMouseDown && epMouseDownTakesTooLong ) return TRUE;
100 
101 #if 0
102     if ( !(ColorData && ColorData->f) ) return TRUE;
103 #endif
104 
105     for( i=0; i<6; i++ )
106     {
107       glGetBooleanv( GL_CLIP_PLANE0+i, &clip[i] );
108       if ( clip[i] ) glDisable( GL_CLIP_PLANE0+i );
109     }
110 
111     glMatrixMode( GL_MODELVIEW );
112     gra_push_matrix();
113     gra_load_identity();
114 
115     glMatrixMode(GL_PROJECTION);
116     gra_push_matrix();
117     gra_load_identity();
118 
119     glGetBooleanv( GL_LIGHTING, &lights_on );
120     glDisable( GL_LIGHTING );
121 
122     gra_polygon_mode( GRA_FILL );
123 
124     if ( !ColorData || !ColorData->f )
125     {
126        C = ColorData->f;
127 
128        CAdd = ColorData->min;
129        if ( ABS(ColorData->max - ColorData->min)>0.0 )
130          CScl = 1.0 / (ColorData->max - ColorData->min);
131        else
132          CScl = 1.0;
133     } else {
134        CAdd = 0.0;
135        CScl = 1.0;
136     }
137 
138     gra_set_colormap( ColorScale->ColorMap );
139     gra_set_material( ColorScale->Material );
140 
141     n = ColorScale->Entries;
142     gra_begin( GRA_QUADS );
143 
144     if ( ColorScale->Style ==  0 )
145     {
146       for( i=0; i<ColorScale->ColorMap->NumberOfEntries-1; i++  )
147       {
148          y = ColorScale->Length * i / (ColorScale->ColorMap->NumberOfEntries-1.0) + ColorScale->YPosition;
149          coords[0][0] = ColorScale->XPosition;
150          coords[1][0] = ColorScale->XPosition + ColorScale->Thickness;
151          coords[2][0] = ColorScale->XPosition + ColorScale->Thickness;
152          coords[3][0] = ColorScale->XPosition;
153 
154          coords[0][1] = y;
155          coords[1][1] = y;
156          coords[2][1] = y + ColorScale->Length/(ColorScale->ColorMap->NumberOfEntries-1.0);
157          coords[3][1] = y + ColorScale->Length/(ColorScale->ColorMap->NumberOfEntries-1.0);
158 
159          coords[0][2] = 0.0;
160          coords[1][2] = 0.0;
161          coords[2][2] = 0.0;
162          coords[3][2] = 0.0;
163 
164          gra_flat_quad( coords, i / (ColorScale->ColorMap->NumberOfEntries-1.0) );
165       }
166 
167       gra_end();
168 
169       glColor3f( ((ColorScale->FontColor & 0xff0000)>>16)/255.0,
170                  ((ColorScale->FontColor & 0xff00)>>8)/255.0,
171                     (ColorScale->FontColor & 0xff)/255.0 );
172 
173       sprintf( fmt, "%%.%de", ColorScale->Decimals );
174 
175       for( i=0; i<n; i++  )
176       {
177          sprintf( str, fmt, (ColorData->max - ColorData->min)*i/(n-1.0) + ColorData->min );
178          y = ColorScale->Length * i / (n-1.0) + ColorScale->YPosition;
179 
180          xl = (ColorScale->Decimals + 9) * ColorScale->FontSize / (double)GraphicsXSize;
181 #ifndef WIN32
182          if ( CurrentXFont ) {
183            xl = (XTextWidth( CurrentXFont, str, strlen(str) ) + 125.0 ) / (double)GraphicsXSize;
184          }
185 #endif
186          glRasterPos3f( ColorScale->XPosition - xl,y,0.0 );
187          PrintString( str );
188 
189          xl = ColorScale->FontSize / GraphicsXSize;
190          gra_beg_lines();
191            glVertex3f( ColorScale->XPosition-xl,y,0.01 );
192            glVertex3f( ColorScale->XPosition,y,0.01 );
193          gra_end_lines();
194       }
195 
196       if ( ColorScale->ColorData->name ) {
197          xl =  strlen(ColorScale->ColorData-name) * ColorScale->FontSize / (double)GraphicsXSize;
198 #ifndef WIN32
199          if ( CurrentXFont ) {
200            xl = (XTextWidth( CurrentXFont, str, strlen(ColorScale->ColorData->name)))/(double)GraphicsXSize;
201          }
202 #endif
203          glRasterPos3f( ColorScale->XPosition - xl,
204              ColorScale->YPosition-ColorScale->Thickness,0.0 );
205 
206          PrintString( ColorScale->ColorData->name );
207       }
208 
209     } else {
210 
211       for( i=0; i<ColorScale->ColorMap->NumberOfEntries; i++  )
212       {
213          y = ColorScale->Length * i / (ColorScale->ColorMap->NumberOfEntries-1.0) + ColorScale->XPosition;
214 
215          coords[0][0] = y;
216          coords[1][0] = y + ColorScale->Length/(ColorScale->ColorMap->NumberOfEntries-1.0);
217          coords[2][0] = y + ColorScale->Length/(ColorScale->ColorMap->NumberOfEntries-1.0);
218          coords[3][0] = y;
219 
220          coords[0][1] = ColorScale->YPosition;
221          coords[1][1] = ColorScale->YPosition;
222          coords[2][1] = ColorScale->YPosition + ColorScale->Thickness;
223          coords[3][1] = ColorScale->YPosition + ColorScale->Thickness;
224 
225          coords[0][2] = 0.0;
226          coords[1][2] = 0.0;
227          coords[2][2] = 0.0;
228          coords[3][2] = 0.0;
229 
230          gra_flat_quad( coords, i / (ColorScale->ColorMap->NumberOfEntries-1.0) );
231       }
232 
233       gra_end();
234 
235       glColor3f( ((ColorScale->FontColor & 0xff0000)>>16)/255.0,
236                  ((ColorScale->FontColor & 0xff00)>>8)/255.0,
237                     (ColorScale->FontColor & 0xff)/255.0 );
238 
239       sprintf( fmt, "%%-8.%dg",ColorScale->Decimals );
240 
241       for( i=0; i<n; i++  )
242       {
243          sprintf( str, fmt, (ColorData->max - ColorData->min)*i/(n-1.0) + ColorData->min );
244 
245          y = ColorScale->Length * i / (n-1.0) + ColorScale->XPosition;
246          glRasterPos3f( y,ColorScale->YPosition+ColorScale->Thickness+0.05,0.0 );
247          PrintString( str );
248 
249          gra_beg_lines();
250            glVertex3f( y,ColorScale->YPosition+ColorScale->Thickness,0.01 );
251            glVertex3f( y,ColorScale->YPosition+ColorScale->Thickness+0.025,0.01 );
252          gra_end_lines();
253       }
254 
255       if ( ColorScale->ColorData->name ) {
256          glRasterPos3f( ColorScale->XPosition,
257              ColorScale->YPosition-ColorScale->Thickness,0.0 );
258 
259          PrintString( ColorScale->ColorData->name );
260       }
261     }
262 
263     if ( lights_on ) glEnable( GL_LIGHTING );
264     gra_pop_matrix();
265 
266     glMatrixMode( GL_MODELVIEW );
267     gra_pop_matrix();
268 
269     for( i=0; i<6; i++ )
270       if ( clip[i] ) glEnable( GL_CLIP_PLANE0+i );
271 
272     return TRUE;
273 }
274 
275 
276 
277 /*******************************************************************************
278  *
279  *     Name:        vis_colscale_alloc
280  *
281  *     Purpose:     allocate memory for colscale_t structure
282  *
283  *     Parameters:
284  *
285  *         Input:   none
286  *
287  *         Output:  none
288  *
289  *   Return value:  pointer to allocated memory
290  *
291  ******************************************************************************/
vis_colscale_alloc()292 static colscale_t *vis_colscale_alloc()
293 {
294      colscale_t *colscale = (colscale_t *)calloc(sizeof(colscale_t),1);
295 
296      if ( !colscale )
297      {
298          fprintf( stderr, "vis_colscale_alloc: FATAL: can't alloc a few bytes of memory\n" );
299      }
300 
301      return colscale;
302 }
303 
304 /*******************************************************************************
305  *
306  *     Name:        vis_colscale_delete
307  *
308  *     Purpose:     free memory associated with colscale_t structure
309  *
310  *     Parameters:
311  *
312  *         Input:   (colscale_t *) pointer to structure
313  *
314  *         Output:  none
315  *
316  *   Return value:  void
317  *
318  ******************************************************************************/
vis_colscale_delete(colscale_t * colscale)319 static void vis_colscale_delete(colscale_t *colscale)
320 {
321     if ( colscale ) free( colscale );
322 }
323 
324 /*******************************************************************************
325  *
326  *     Name:        vis_initialize_colscale_visual
327  *
328  *     Purpose:     Register "ColorScale" visual type
329  *
330  *     Parameters:
331  *
332  *         Input:   none
333  *
334  *         Output:  none
335  *
336  *   Return value:  vis_add_visual_type (malloc success probably)...
337  *
338  ******************************************************************************/
vis_initialize_colscale_visual()339 int vis_initialize_colscale_visual()
340 {
341     static char *visual_name = "ColorScale";
342     visual_type_t VisualDef;
343 
344     static colscale_t colscale;
345 
346 
347     static visual_param_t ColorScaleParams[] =
348     {
349         { "Entries",       "%d", 0, VIS_VISUAL_PARAM_INT,  6, 0.0, NULL },
350         { "Decimals",      "%d", 0, VIS_VISUAL_PARAM_INT,  2, 0.0, NULL },
351         { "Style",         "%d", 0, VIS_VISUAL_PARAM_INT,  0,  0.0, NULL },
352         { "XPosition",     "%d", 0, VIS_VISUAL_PARAM_FLOAT,  0,  0.8, NULL },
353         { "YPosition",     "%d", 0, VIS_VISUAL_PARAM_FLOAT,  0, -0.8, NULL },
354         { "Length",        "%d", 0, VIS_VISUAL_PARAM_FLOAT,  0,  1.5, NULL },
355         { "Thickness",     "%d", 0, VIS_VISUAL_PARAM_FLOAT,  0,  0.1, NULL },
356         { "Font Color",    "%d", 0, VIS_VISUAL_PARAM_INT,  0xffffff,  0.0, NULL },
357         { "Font Size",     "%d", 0, VIS_VISUAL_PARAM_FLOAT, 0,  17.0, NULL },
358         { "ColorData",     "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0,  NULL },
359         { "ColorMap",      "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, &DefaultColorMap },
360         { "Material",      "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, &DefaultMaterial },
361         { NULL, NULL, 0, 0, 0, 0.0, NULL }
362     };
363 
364     int n = 0;
365 
366     ColorScaleParams[n++].Offset = (char *)&colscale.Entries     - (char *)&colscale;
367     ColorScaleParams[n++].Offset = (char *)&colscale.Decimals    - (char *)&colscale;
368     ColorScaleParams[n++].Offset = (char *)&colscale.Style       - (char *)&colscale;
369     ColorScaleParams[n++].Offset = (char *)&colscale.XPosition   - (char *)&colscale;
370     ColorScaleParams[n++].Offset = (char *)&colscale.YPosition   - (char *)&colscale;
371     ColorScaleParams[n++].Offset = (char *)&colscale.Length      - (char *)&colscale;
372     ColorScaleParams[n++].Offset = (char *)&colscale.Thickness   - (char *)&colscale;
373     ColorScaleParams[n++].Offset = (char *)&colscale.FontColor   - (char *)&colscale;
374     ColorScaleParams[n++].Offset = (char *)&colscale.FontSize    - (char *)&colscale;
375     ColorScaleParams[n++].Offset = (char *)&colscale.ColorData   - (char *)&colscale;
376     ColorScaleParams[n++].Offset = (char *)&colscale.ColorMap    - (char *)&colscale;
377     ColorScaleParams[n++].Offset = (char *)&colscale.Material    - (char *)&colscale;
378 
379     VisualDef.VisualName    = visual_name;
380 
381     VisualDef.RealizeVisual = (int   (*)()) vis_colscale;
382     VisualDef.AllocParams   = (void *(*)()) vis_colscale_alloc;
383     VisualDef.DeleteParams  = (void  (*)()) vis_colscale_delete;
384     VisualDef.VisualParams  = ColorScaleParams;
385 
386     return vis_add_visual_type( &VisualDef );
387 }
388