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  * Objects 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: 2 Oct 1995
40  *
41  *                Modified by:
42  *
43  *       Date of modification:
44  *
45  ******************************************************************************/
46 
47 #define MODULE_OBJECTS
48 
49 #include "../elmerpost.h"
50 
51 /*******************************************************************************
52  *
53  *     Name:          obj_initialize_object
54  *
55  *     Purpose:       Initialize object to default values
56  *
57  *     Parameters:
58  *
59  *         Input:    none
60  *
61  *         Output:   (object_t *)
62  *
63  *   Return value:   void
64  *
65  ******************************************************************************/
obj_object_initialize(object_t * object)66 void obj_object_initialize( object_t *object )
67 {
68 	int i;
69 
70     obj_init_transform( &object->Transform );
71 	for( i=0; i<6;i++ ) object->ClipPlane[i] = -1;
72     PiDiv180 = acos(0.0) / 90.0;
73 }
74 
75 /*******************************************************************************
76  *
77  *     Name:          obj_new
78  *
79  *     Purpose:       Create a new object. Internal only.
80  *
81  *     Parameters:
82  *
83  *         Input:    Name of the object to create.
84  *
85  *         Output:   none
86  *
87  *   Return value:   (object_t *)
88  *
89  ******************************************************************************/
obj_new(char * name)90 object_t *obj_new(char *name)
91 {
92     object_t *object = calloc(1,sizeof(object_t));
93 
94     if ( !object )
95     {
96        fprintf( stderr, "object_new: FATAL: can't allocate a few bytes of memory.\n" );
97        return NULL;
98     }
99 
100     if ( name )
101     {
102         if ( !(object->Name = malloc( strlen(name)+1 ) ) )
103         {
104            fprintf( stderr, "object_new: FATAL: can't allocate a few bytes of memory.\n" );
105            free( object );
106            return NULL;
107         }
108 
109         strcpy( object->Name,name );
110     }
111 
112     obj_object_initialize( object );
113 
114     return object;
115 }
116 
117 /*******************************************************************************
118  *
119  *     Name:          obj_add_object
120  *
121  *     Purpose:       Add a new object to list of objects given.
122  *
123  *     Parameters:
124  *
125  *         Input:    (object_t *) input list
126  *                   (char *) name of the object to create
127  *
128  *         Output:   (object_t *) is modified
129  *
130  *   Return value:   pointer to (object_t *), the new entry in the list.
131  *
132  ******************************************************************************/
obj_add_object(object_t * object,char * name)133 object_t *obj_add_object( object_t *object,char *name )
134 {
135      object_t *new = obj_new(name);
136 
137      if ( !new ) return NULL;
138 
139      if ( object )
140      {
141          new->Id = 1;
142          while( object->Next ) { object = object->Next; new->Id++; }
143          object->Next = new;
144      }
145 
146      return new;
147 }
148 
149 /*******************************************************************************
150  *
151  *     Name:          obj_find
152  *
153  *     Purpose:       Return pointer to an object with given name if any.
154  *
155  *     Parameters:
156  *
157  *         Input:    (object_t *) input list of objects
158  *                   (char *) name of the object to find
159  *
160  *         Output:   none
161  *
162  *   Return value:   pointer to (object_t *) if found, NULL otherwise
163  *
164  ******************************************************************************/
obj_find(object_t * object,char * name)165 object_t *obj_find( object_t *object,char *name )
166 {
167      while( object )
168      {
169         if ( strcmp( object->Name, name ) == 0 ) return object;
170         object = object->Next;
171      }
172 
173      return NULL;
174 }
175 
176 /*******************************************************************************
177  *
178  *     Name:          obj_display_list
179  *
180  *     Purpose:       Display list of objects given.
181  *
182  *     Parameters:
183  *
184  *         Input:    (object_t *) input list of objects
185  *
186  *         Output:   graphics
187  *
188  *   Return value:   if mouse interaction is going on and too slow FALSE,
189  *                   otherwise true
190  *
191  ******************************************************************************/
obj_display_list(object_t * object,double t)192 int obj_display_list( object_t *object,double t )
193 {
194     extern double XMin,XMax,YMin,YMax,ZMin,ZMax;
195     int i;
196 
197     for( ; object != NULL; object = object->Next )
198     {
199         gra_push_matrix();
200         obj_set_matrix( object );
201 
202         for( i=0; i<6; i++ )
203   	   if ( object->ClipPlane[i] >= 0 )
204 		gra_clip_plane( object->ClipPlane[i],object->ClipEquation[i] );
205 
206         if ( user_hook_object_before )
207             (*user_hook_object_before)
208               (
209                  GlobalPass,object->Geometry,object->ElementModel,object->VisualList,t
210               );
211 
212         if ( epMouseDown && epMouseDownTakesTooLong > 3 )
213         {
214             gra_bbox( XMin,XMax,YMin,YMax,ZMin,ZMax );
215         }
216         else if ( !vis_display_list( object->Geometry,object->ElementModel,object->VisualList,t ) )
217         {
218             gra_pop_matrix();
219             return FALSE;
220         }
221 
222         if ( user_hook_object_after )
223             (*user_hook_object_after)
224               (
225                  GlobalPass,object->Geometry,object->ElementModel,object->VisualList,t
226               );
227 
228          for( i=0; i<6; i++ )
229            if ( object->ClipPlane[i] >= 0 ) gra_disable_clip( object->ClipPlane[i] );
230 
231 
232         gra_pop_matrix();
233     }
234 
235     return TRUE;
236 }
237