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  * Visual classes main module + utilities
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: 26 Sep 1995
40  *
41  *
42  * Modification history:
43  *
44  * 28 Sep 1995, - added routine vis_get_visual_type, which gives a pointer to
45  *                visual_type_t structure given name of the visual type
46  *
47  *              - modified vis_add_visual_type according to change in
48  *                visual_type_t and visual_t structures (now holding list of
49  *                visual types rather than an array)
50  *
51  * 29 Sep 1995, - added routines vis_get_visual_type_name and vis_get_visual_name
52  *              - added routines vis_set_param and vis_new_visual
53  *              - added routines vis_add_visual, vis_delete_visual
54  *
55  * Juha R.
56  *
57  ******************************************************************************/
58 
59 #define MODULE_VISUALS
60 
61 #include "../elmerpost.h"
62 
63 /*******************************************************************************
64  *
65  *     Name:         vis_add_visual_type
66  *
67  *     Purpose:      Register a visual class
68  *
69  *     Parameters:
70  *
71  *         Input:    (visual_t *)  visual class to be added
72  *
73  *         Output:   Global variable VisualDefs is modified
74  *
75  *   Return value:   TRUE is success, FALSE if malloc() fails
76  *
77  ******************************************************************************/
vis_add_visual_type(visual_type_t * VisualDef)78 int vis_add_visual_type( visual_type_t *VisualDef )
79 {
80     visual_type_t *ptr;
81 
82     ptr = (visual_type_t *)calloc(sizeof(visual_type_t),1);
83     if ( !ptr )
84     {
85         fprintf( stderr, "FATAL: vis_visual_type_add: Can't allocate memory.\n" );
86         return FALSE;
87     }
88 
89     *ptr = *VisualDef;
90     ptr->Next = VisualDefs.VisualTypes;
91 
92     VisualDefs.VisualTypes = ptr;
93 
94     VisualDefs.NumberOfVisualTypes++;
95 
96     return TRUE;
97 }
98 
99 /*******************************************************************************
100  *
101  *     Name:         vis_get_visual_type
102  *
103  *     Purpose:      return visual type pointer given visual name
104  *
105  *     Parameters:
106  *
107  *         Input:    (char *) name of visual
108  *
109  *         Output:   none
110  *
111  *   Return value:   (visual_type_t *)visual if found, NULL otherwise
112  *
113  ******************************************************************************/
vis_get_visual_type(char * name)114 visual_type_t *vis_get_visual_type(char *name)
115 {
116     visual_type_t *type = VisualDefs.VisualTypes;
117 
118     for( type=VisualDefs.VisualTypes; type != NULL; type=type->Next )
119     {
120         if ( strcmp( name, type->VisualName ) == 0 ) return type;
121     }
122 
123     fprintf( stderr, "vis_get_visual_type: can't find visual type [%s]\n", name );
124 
125     return NULL;
126 }
127 
128 /*******************************************************************************
129  *
130  *     Name:         vis_get_visual_type_name
131  *
132  *     Purpose:      return pointer to visual type name given visual structure
133  *
134  *     Parameters:
135  *
136  *         Input:    (visual_t *) visual structure
137  *
138  *         Output:   none
139  *
140  *   Return value:   (char *) name of the visual type
141  *
142  ******************************************************************************/
vis_get_visual_type_name(visual_t * visual)143 char *vis_get_visual_type_name(visual_t *visual)
144 {
145     return visual->VisualType->VisualName;
146 }
147 
148 /*******************************************************************************
149  *
150  *     Name:         vis_get_visual_name
151  *
152  *     Purpose:      return pointer to visual name given visual structure
153  *
154  *     Parameters:
155  *
156  *         Input:    (visual_t *) visual structure
157  *
158  *         Output:   none
159  *
160  *   Return value:   (char *) name of the visual
161  *
162  ******************************************************************************/
vis_get_visual_name(visual_t * visual)163 char *vis_get_visual_name(visual_t *visual)
164 {
165     return visual->Name;
166 }
167 
168 /*******************************************************************************
169  *
170  *     Name:        vis_set_param
171  *
172  *     Purpose:     set visual parameters
173  *
174  *     Parameters:
175  *
176  *         Input:   (visual_t *) visual whose parameter is set
177  *                  (char *)     parameter name
178  *                  (intvalue,doublevalue,void *) pointer to param value
179  *
180  *         Output:  graphics
181  *
182  *   Return value:  success
183  *
184  ******************************************************************************/
vis_set_param(visual_t * visual,char * name,int intvalue,double doublevalue,void * ptrvalue)185 int vis_set_param
186   ( visual_t *visual, char *name,int intvalue,double doublevalue,void *ptrvalue )
187 {
188     visual_type_t  *VisualType   = visual->VisualType;
189     visual_param_t *VisualParams = VisualType->VisualParams;
190 
191     char *offset = (void *)visual->VisualParams;
192 
193     if ( !offset || !name )
194     {
195        fprintf( stderr, "vis_set_param: argument failure.\n" );
196        return FALSE;
197     }
198 
199     for( ; VisualParams->Name != NULL; VisualParams++ )
200     {
201         if ( strcmp( name, VisualParams->Name ) == 0 )
202         {
203             if ( VisualParams->ParamType == VIS_VISUAL_PARAM_LOGICAL )
204             {
205                 *(logical_t *)(offset + VisualParams->Offset) = intvalue;
206             }
207             else if ( VisualParams->ParamType == VIS_VISUAL_PARAM_INT )
208             {
209                 *(int *)(offset + VisualParams->Offset) = intvalue;
210             }
211             else if ( VisualParams->ParamType == VIS_VISUAL_PARAM_FLOAT )
212             {
213                 *(double *)(offset + VisualParams->Offset) = doublevalue;
214             }
215             else if ( VisualParams->ParamType == VIS_VISUAL_PARAM_POINTER )
216             {
217                 *(void **)(offset + VisualParams->Offset) = (void *)ptrvalue;
218             }
219             break;
220         }
221     }
222 
223     if ( !VisualParams->Name )
224     {
225         fprintf( stderr, "vis_set_param: no such param [%s,%s]\n", VisualType->VisualName,name );
226         return FALSE;
227     }
228 
229     return TRUE;
230 }
231 
232 /*******************************************************************************
233  *
234  *     Name:        vis_default_params
235  *
236  *     Purpose:     set default parameter values for a visual
237  *
238  *     Parameters:
239  *
240  *         Input:   (visual_t *) visual structure to modify
241  *
242  *         Output:
243  *
244  *   Return value:  void
245  *
246  ******************************************************************************/
vis_default_params(visual_t * visual)247 void vis_default_params( visual_t *visual )
248 {
249     visual_type_t  *VisualType   = visual->VisualType;
250     visual_param_t *VisualParams = VisualType->VisualParams;
251 
252     for( ;VisualParams->Name != NULL; VisualParams++ )
253     {
254         vis_set_param(  visual,VisualParams->Name, VisualParams->IntValue,
255              VisualParams->FloatValue, VisualParams->PointerValue );
256     }
257 }
258 
259 /*******************************************************************************
260  *
261  *     Name:        vis_new_visual
262  *
263  *     Purpose:     alloc memory for a new visual structure
264  *
265  *     Parameters:
266  *
267  *         Input:   (char *)  Name of the visual type
268  *
269  *         Output:  none
270  *
271  *   Return value:  pointer to visual_t or NULL if malloc fails
272  *
273  ******************************************************************************/
vis_new_visual(char * name)274 visual_t *vis_new_visual(char *name)
275 {
276     visual_t *visual = (visual_t *)calloc( sizeof(visual_t),1 );
277 
278     if ( !visual )
279     {
280        fprintf( stderr, "vis_new_visual: FATAL: can't allocate (a few bytes of) memory\n" );
281        return NULL;
282     }
283 
284     if ( !(visual->VisualType = vis_get_visual_type(name)) )
285     {
286         free( visual );
287         return NULL;
288     }
289 
290     if ( !(visual->VisualParams = (*visual->VisualType->AllocParams)()) )
291     {
292         free( visual );
293         return NULL;
294     }
295 
296     vis_default_params( visual );
297 
298     return visual;
299 }
300 
301 /*******************************************************************************
302  *
303  *     Name:        vis_link_visual
304  *
305  *     Purpose:     link a new visual to list of visuals given
306  *
307  *     Parameters:
308  *
309  *         Input:   (visual_t *) list of visuals (can be null)
310  *                  (visual_t *) visual to be added
311  *
312  *         Output:  (visual_t *) is modified
313  *
314  *   Return value:  (visual_t *) head of list
315  *
316  ******************************************************************************/
vis_link_visual(visual_t * list,visual_t * new)317 visual_t *vis_link_visual( visual_t *list,visual_t *new )
318 {
319     visual_t *ptr = list;
320 
321     if ( list )
322     {
323         while( ptr->Next != NULL ) ptr = ptr->Next;
324         ptr->Next = new;
325     } else list = new;
326 
327     return list;
328 }
329 
330 /*******************************************************************************
331  *
332  *     Name:        vis_add_visual
333  *
334  *     Purpose:     add a new visual to list of visuals given
335  *
336  *     Parameters:
337  *
338  *         Input:   (visual_t *) list of visuals (can be null)
339  *                  (char *)     name of visual type to add
340  *
341  *         Output:  (visual_t *) is modified
342  *
343  *   Return value:  pointer to new visual_t or NULL if malloc fails
344  *
345  ******************************************************************************/
vis_add_visual(visual_t * visual,char * name)346 visual_t *vis_add_visual(visual_t *visual,char *name)
347 {
348     visual_t *newvisual = (visual_t *)vis_new_visual(name);
349 
350     if ( !newvisual ) return NULL;
351 
352     if ( visual )
353     {
354         while( visual->Next != NULL ) visual = visual->Next;
355         visual->Next = newvisual;
356     }
357 
358     return newvisual;
359 }
360 
361 /*******************************************************************************
362  *
363  *     Name:        vis_delete_visual
364  *
365  *     Purpose:     delete list of visuals given as argument
366  *
367  *     Parameters:
368  *
369  *         Input:   (visual_t *) list to be deleted
370  *
371  *         Output:  none
372  *
373  *   Return value:  void
374  *
375  ******************************************************************************/
vis_delete_visual(visual_t * visual)376 void vis_delete_visual( visual_t *visual )
377 {
378     visual_t *ptr;
379 
380     while( visual != NULL )
381     {
382         (*visual->VisualType->DeleteParams)( visual->VisualParams );
383         visual->VisualParams = NULL;
384 
385         ptr = visual->Next;
386 
387         free( visual );
388         visual = ptr;
389     }
390 }
391 
392 /*******************************************************************************
393  *
394  *     Name:        vis_next_visual
395  *
396  *     Purpose:     return pointer to next visual in a list of given visual
397  *
398  *     Parameters:
399  *
400  *         Input:   (visual_t *)
401  *
402  *         Output:  none
403  *
404  *   Return value:  pointer to visual_t or NULL if no more visuals in the list
405  *
406  ******************************************************************************/
vis_next_visual(visual_t * visual)407 visual_t *vis_next_visual( visual_t *visual )
408 {
409     if ( visual ) return visual->Next;
410 
411     return NULL;
412 }
413 
414 /*******************************************************************************
415  *
416  *     Name:         vis_display_visual
417  *
418  *     Purpose:      Display one visual_t visual
419  *
420  *     Parameters:
421  *
422  *         Input:    (geometry_t *) geometry information
423  *                   (visual_t *)   visual
424  *
425  *         Output:   graphics
426  *
427  *   Return value:   if mouse interaction is going on and too slow FALSE,
428  *                   otherwise true
429  *
430  ******************************************************************************/
vis_display_visual(geometry_t * geometry,element_model_t * model,visual_t * VL,double t)431 int vis_display_visual( geometry_t *geometry, element_model_t *model, visual_t *VL,double t )
432 {
433     return (*VL->VisualType->RealizeVisual)( geometry, model, VL->VisualParams, t );
434 }
435 
436 /*******************************************************************************
437  *
438  *     Name:         vis_display_list
439  *
440  *     Purpose:      Display a list of visual_t visuals
441  *
442  *     Parameters:
443  *
444  *         Input:    (geometry_t *) geometry information
445  *                   (visual_t *)   visuals
446  *                   (double t)     real time at invocation
447  *
448  *         Output:   graphics
449  *
450  *   Return value:   if mouse interaction is going on and too slow FALSE,
451  *                   otherwise true
452  *
453  ******************************************************************************/
vis_display_list(geometry_t * geometry,element_model_t * model,visual_t * VL,double t)454 int vis_display_list( geometry_t *geometry, element_model_t *model, visual_t *VL,double t )
455 {
456     for( ; VL != NULL; VL = VL->Next )
457     {
458 #ifdef DEBUG
459 fprintf( stderr, "DISPLAYING VISUAL TYPE: [%s]\n", VL->VisualType->VisualName );
460 #endif
461         if ( !(*VL->VisualType->RealizeVisual)( geometry, model, VL->VisualParams, t ) )
462         {
463             return FALSE;
464         }
465 
466         if ( BreakLoop ) break;
467     }
468 
469     return TRUE;
470 }
471 
472 
473 /*******************************************************************************
474  *
475  *     Name:         vis_initialize_visual_types
476  *
477  *     Purpose:      Register all internal visual classes
478  *
479  *     Parameters:
480  *
481  *         Input:    none
482  *
483  *         Output:   Global variable VisualDefs is modified
484  *
485  *   Return value:   TRUE is success, FALSE if malloc() fails
486  *
487  ******************************************************************************/
vis_initialize_visual_types()488 int vis_initialize_visual_types()
489 {
490      if ( !vis_initialize_mesh_visual() ) return FALSE;
491      if ( !vis_initialize_arrow_visual() ) return FALSE;
492      if ( !vis_initialize_sphere_visual() ) return FALSE;
493      if ( !vis_initialize_contour_line_visual() ) return FALSE;
494      if ( !vis_initialize_isosurface_visual() ) return FALSE;
495      if ( !vis_initialize_particle_visual() ) return FALSE;
496      if ( !vis_initialize_colscale_visual() ) return FALSE;
497 
498      return TRUE;
499 }
500