Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/visuals/mesh.c
3203 views
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 mesh 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: [email protected]
38
*
39
* Date: 26 Sep 1995
40
*
41
*
42
* Modification history:
43
*
44
* 28 Sep 1995, modified vis_initialize_mesh_visual to set the VisualName field
45
* of the visual_type structure
46
*
47
* Juha R.
48
*
49
******************************************************************************/
50
51
#include "../elmerpost.h"
52
53
54
/******************************************************************************
55
*
56
* Parameter structure definitions for mesh visual class
57
*
58
******************************************************************************/
59
static char *mesh_style_names[] =
60
{
61
"none", "line", "surf", "line_and_surf", NULL
62
};
63
64
typedef struct mesh_s
65
{
66
scalar_t *ColorData;
67
68
mesh_style_t Style;
69
70
edge_style_t EdgeStyle;
71
72
material_t *Material;
73
material_t *EdgeMaterial;
74
colormap_t *ColorMap;
75
76
int LineQuality;
77
double LineWidth;
78
79
line_style_t LineStyle;
80
81
logical_t NodeNumbers;
82
} mesh_t;
83
84
85
/*******************************************************************************
86
*
87
* Name: vis_polygon
88
*
89
* Purpose:
90
*
91
*
92
* Parameters:
93
*
94
* Input: (polygon_t *) polygon
95
*
96
* Output: graphics
97
*
98
* Return value: void
99
*
100
******************************************************************************/
101
void vis_polygon( polygon_t *poly)
102
{
103
int n=3;
104
105
void gra_poly3();
106
gra_poly3( n,poly->x,poly->y,poly->z,poly->u,poly->v,poly->w,poly->c );
107
}
108
109
/*******************************************************************************
110
*
111
* Name: vis_triangle
112
*
113
* Purpose:
114
*
115
*
116
* Parameters:
117
*
118
* Input: (triangle_t *) triangle
119
* (vertex_t *) vertex array
120
* (double *) quantity to use color the edges (or NULL)
121
* (double,double ) CScl,CAdd are constant to scale color
122
* range (0-1)
123
* (line_style_t) line style, either line_style_line or
124
* line_style_cylider
125
*
126
* Output: graphics
127
*
128
* Return value: void
129
*
130
******************************************************************************/
131
void vis_triangle
132
(
133
triangle_t *t,vertex_t *v,double *color,double CScl,double CAdd
134
)
135
{
136
float x[3][3],n[3][3],c[3];
137
int j,k;
138
void gra_triangle();
139
140
for( j=0; j<3; j++ )
141
{
142
k = t->v[j];
143
x[j][0] = v[k].x[0];
144
x[j][1] = v[k].x[1];
145
x[j][2] = v[k].x[2];
146
147
n[j][0] = t->u[j][0];
148
n[j][1] = t->u[j][1];
149
n[j][2] = t->u[j][2];
150
151
if ( color ) c[j] = CScl * ( color[k]-CAdd );
152
}
153
154
gra_triangle( x,n,c );
155
}
156
157
/*******************************************************************************
158
*
159
* Name: vis_draw_edge
160
*
161
* Purpose: draw element edge as line or cylinder
162
*
163
* Parameters:
164
*
165
* Input: (vertex_t *) vertex array
166
* (int,int) edge vertex indices
167
* (double *) color quantity
168
* (double,double ) CScl,CAdd are constant to scale color
169
* range (0-1)
170
* (line_style_t) line style, either line_style_line or
171
* line_style_cylider
172
* (double) line width, cylinder radius.
173
*
174
*
175
* Output: graphics
176
*
177
* Return value: void
178
*
179
******************************************************************************/
180
static void vis_draw_edge(vertex_t *vertex,int v0,int v1,double *color,double CScl,
181
double CAdd,line_style_t style,double width)
182
{
183
double c0=0.0,c1=1.0;
184
void gra_line();
185
186
/*
187
* if color function given scale values to 0-1
188
*/
189
if ( color )
190
{
191
c0 = CScl*( color[v0] - CAdd );
192
c1 = CScl*( color[v1] - CAdd );
193
}
194
195
gra_line( vertex[v0].x,c0,vertex[v1].x,c1,style,width );
196
}
197
198
/*******************************************************************************
199
*
200
* Name: vis_mesh
201
*
202
* Purpose: draw mesh as lines or surface, with color codes or not
203
*
204
* Parameters:
205
*
206
* Input: (geometry_t *) triangles to draw
207
* (mesh_t *) mesh display parameters
208
* (double)
209
*
210
* Output: graphics
211
*
212
* Return value: if mouse interaction is going on, and time used exceeds
213
* given value (TooLong1,2) FALSE, otherwise TRUE
214
*
215
******************************************************************************/
216
static int vis_mesh( geometry_t *geometry, element_model_t *model, mesh_t *Mesh,double dt )
217
{
218
scalar_t *ColorData = Mesh->ColorData;
219
220
vertex_t *v = geometry->Vertices;
221
222
edge_list_t *edge;
223
224
double width = Mesh->LineWidth*0.005,CScl=1.0,CAdd=0.0,*C=NULL;
225
226
int i,j,quick,N=geometry->VertexCount,NT=geometry->TriangleCount;
227
228
element_t *elements = model->Elements;
229
230
static char str[100];
231
232
void gra_set_material(), gra_set_colormap(), gra_sphere_quality(), PrintString() ;
233
234
if ( !GlobalOptions.StereoMode )
235
if ( Mesh->Material->Diffuse[3] < 1.0 )
236
{
237
if ( GlobalPass != 0 ) return TRUE;
238
} else if ( GlobalPass == 0 )
239
{
240
return TRUE;
241
}
242
243
quick = (Mesh->Style == mesh_style_line && Mesh->LineStyle == line_style_line );
244
quick &= ~Mesh->NodeNumbers;
245
quick |= epMouseDown && epMouseDownTakesTooLong;
246
247
gra_set_material( Mesh->Material );
248
249
if ( quick && !(epMouseDown && epMouseDownTakesTooLong) )
250
{
251
gra_line_width( Mesh->LineWidth );
252
} else {
253
gra_line_width( 1.0 );
254
}
255
256
if ( ColorData && ColorData->f )
257
{
258
C = ColorData->f;
259
260
CAdd = ColorData->min;
261
if ( ABS(ColorData->max - ColorData->min)>0.0 )
262
CScl = 1.0 / (ColorData->max - ColorData->min);
263
else
264
CScl = 1.0;
265
266
gra_set_colormap( Mesh->ColorMap );
267
} else gra_set_colormap( NULL );
268
269
if ( quick )
270
{
271
gra_set_material( Mesh->EdgeMaterial );
272
gra_beg_lines();
273
274
for( i=0; i<N; i++ )
275
{
276
if ( v[i].ElementModelNode )
277
{
278
for( edge=geometry->Edges[i].EdgeList; edge != NULL; edge = edge->Next )
279
{
280
if ( edge->Element && !edge->Element->DisplayFlag ) continue;
281
if ( Mesh->EdgeStyle == edge_style_all || ABS(edge->Count) == 1 )
282
{
283
vis_draw_edge( v,i,edge->Entry,C,CScl,CAdd,line_style_line,width );
284
}
285
}
286
} else break;
287
288
if ( epMouseDown && (i & 128) )
289
{
290
if ( RealTime() - dt > TooLong2 )
291
if ( ++epMouseDownTakesTooLong > 3 )
292
{
293
gra_end_lines();
294
return FALSE;
295
} else dt = RealTime();
296
}
297
298
if ( BreakLoop ) break;
299
}
300
301
gra_end_lines();
302
gra_set_material( Mesh->Material );
303
304
return TRUE;
305
}
306
307
if ( Mesh->NodeNumbers )
308
{
309
for( i=0; i<N; i++ )
310
{
311
if ( v[i].ElementModelNode )
312
{
313
glRasterPos3f( v[i].x[0],v[i].x[1],v[i].x[2]);
314
sprintf( str, "N %d", i );
315
PrintString( str );
316
}
317
}
318
}
319
320
if ( Mesh->Style == mesh_style_surf || Mesh->Style == mesh_style_line_and_surf )
321
{
322
triangle_t *t = geometry->Triangles;
323
324
gra_begin( GRA_TRIANGLES );
325
for( i=0; i<NT; i++,t++ )
326
{
327
if ( t->Element && !t->Element->DisplayFlag ) continue;
328
329
if ( Mesh->EdgeStyle != edge_style_all && t->Count > 1 ) continue;
330
331
vis_triangle( t,v,C,CScl,CAdd );
332
333
if ( epMouseDown && (i & 8) )
334
if ( RealTime() - dt > TooLong1 )
335
{
336
epMouseDownTakesTooLong++;
337
gra_end();
338
return FALSE;
339
}
340
341
if ( BreakLoop ) break;
342
}
343
gra_end();
344
}
345
346
if ( Mesh->Style == mesh_style_line_and_surf ) gra_set_colormap( NULL );
347
348
if ( Mesh->Style == mesh_style_line || Mesh->Style == mesh_style_line_and_surf )
349
{
350
351
glMatrixMode(GL_PROJECTION);
352
glPushMatrix();
353
glTranslatef(0.0,0.0,0.005);
354
glMatrixMode(GL_MODELVIEW);
355
356
gra_set_material( Mesh->EdgeMaterial );
357
358
if ( Mesh->LineStyle == line_style_cylinder )
359
{
360
gra_sphere_quality( Mesh->LineQuality );
361
362
for( i=0; i<N; i++ )
363
{
364
if ( v[i].ElementModelNode )
365
{
366
for( edge=geometry->Edges[i].EdgeList; edge != NULL; edge = edge->Next )
367
{
368
if ( edge->Element && !edge->Element->DisplayFlag ) continue;
369
if ( Mesh->EdgeStyle == edge_style_all || ABS(edge->Count) == 1 )
370
{
371
j = edge->Entry;
372
vis_draw_edge( v,i,j,C,CScl,CAdd,line_style_cylinder,width );
373
}
374
}
375
} else break;
376
377
if ( epMouseDown && (i & 8) )
378
if ( RealTime() - dt > TooLong1 )
379
{
380
epMouseDownTakesTooLong++;
381
glMatrixMode(GL_PROJECTION);
382
glPopMatrix();
383
glMatrixMode( GL_MODELVIEW );
384
return FALSE;
385
}
386
387
if ( BreakLoop ) break;
388
}
389
} else
390
{
391
gra_line_width( Mesh->LineWidth );
392
gra_beg_lines();
393
for( i=0; i<N; i++ )
394
{
395
if ( v[i].ElementModelNode )
396
{
397
for( edge=geometry->Edges[i].EdgeList; edge != NULL; edge = edge->Next )
398
{
399
if ( edge->Element && !edge->Element->DisplayFlag ) continue;
400
if ( Mesh->EdgeStyle == edge_style_all || ABS(edge->Count) == 1 )
401
{
402
vis_draw_edge( v,i,edge->Entry,C,CScl,CAdd,line_style_line,width );
403
}
404
}
405
} else break;
406
407
if ( epMouseDown && (i & 32) )
408
if ( RealTime() - dt > TooLong1 )
409
{
410
epMouseDownTakesTooLong++;
411
gra_end_lines();
412
glMatrixMode(GL_PROJECTION);
413
glPopMatrix();
414
glMatrixMode( GL_MODELVIEW );
415
return FALSE;
416
}
417
418
if ( BreakLoop ) break;
419
}
420
gra_end_lines();
421
}
422
glMatrixMode(GL_PROJECTION);
423
glPopMatrix();
424
glMatrixMode( GL_MODELVIEW );
425
}
426
427
return TRUE;
428
}
429
430
/*******************************************************************************
431
*
432
* Name: vis_mesh_alloc
433
*
434
* Purpose: allocate memory for mesh_t structure
435
*
436
* Parameters:
437
*
438
* Input: none
439
*
440
* Output: none
441
*
442
* Return value: pointer to allocated memory
443
*
444
******************************************************************************/
445
static mesh_t *vis_mesh_alloc()
446
{
447
mesh_t *mesh = (mesh_t *)calloc(sizeof(mesh_t),1);
448
449
if ( !mesh )
450
{
451
fprintf( stderr, "vis_mesh_alloc: FATAL: can't alloc a few bytes of memory\n" );
452
}
453
454
return mesh;
455
}
456
457
/*******************************************************************************
458
*
459
* Name: vis_mesh_delete
460
*
461
* Purpose: free memory associated with mesh_t structure
462
*
463
* Parameters:
464
*
465
* Input: (mesh_t *) pointer to structure
466
*
467
* Output: none
468
*
469
* Return value: void
470
*
471
******************************************************************************/
472
static void vis_mesh_delete(mesh_t *mesh)
473
{
474
if ( mesh ) free( mesh );
475
}
476
477
/*******************************************************************************
478
*
479
* Name: vis_initialize_mesh_visual
480
*
481
* Purpose: Register "Mesh" visual type
482
*
483
* Parameters:
484
*
485
* Input: none
486
*
487
* Output: none
488
*
489
* Return value: vis_add_visual_type (malloc success probably)...
490
*
491
******************************************************************************/
492
int vis_initialize_mesh_visual()
493
{
494
static char *visual_name = "Mesh";
495
visual_type_t VisualDef;
496
497
static mesh_t mesh;
498
499
static visual_param_t MeshParams[] =
500
{
501
{ "ColorData", "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, NULL },
502
{ "Style", "%d", 0, VIS_VISUAL_PARAM_INT, mesh_style_line, 0.0, NULL },
503
{ "EdgeStyle", "%d", 0, VIS_VISUAL_PARAM_INT, edge_style_all, 0.0, NULL },
504
{ "Material", "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, &DefaultMaterial },
505
{ "EdgeMaterial", "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, &DefaultEdgeMaterial },
506
{ "ColorMap", "%s", 0, VIS_VISUAL_PARAM_POINTER, 0, 0.0, &DefaultColorMap },
507
{ "LineWidth", "%lf", 0, VIS_VISUAL_PARAM_FLOAT, 0, 1.0, NULL },
508
{ "LineQuality", "%d", 0, VIS_VISUAL_PARAM_INT, 1, 0.0, NULL },
509
{ "LineStyle", "%d", 0, VIS_VISUAL_PARAM_INT, 0, 0.0, NULL },
510
{ "NodeNumbers", "%d", 0, VIS_VISUAL_PARAM_LOGICAL, 0, 0.0, NULL },
511
{ NULL, NULL, 0, 0, 0, 0.0, NULL }
512
};
513
514
int n = 0;
515
516
MeshParams[n++].Offset = (char *)&mesh.ColorData - (char *)&mesh;
517
MeshParams[n++].Offset = (char *)&mesh.Style - (char *)&mesh;
518
MeshParams[n++].Offset = (char *)&mesh.EdgeStyle - (char *)&mesh;
519
MeshParams[n++].Offset = (char *)&mesh.Material - (char *)&mesh;
520
MeshParams[n++].Offset = (char *)&mesh.EdgeMaterial - (char *)&mesh;
521
MeshParams[n++].Offset = (char *)&mesh.ColorMap - (char *)&mesh;
522
MeshParams[n++].Offset = (char *)&mesh.LineWidth - (char *)&mesh;
523
MeshParams[n++].Offset = (char *)&mesh.LineQuality - (char *)&mesh;
524
MeshParams[n++].Offset = (char *)&mesh.LineStyle - (char *)&mesh;
525
MeshParams[n++].Offset = (char *)&mesh.NodeNumbers - (char *)&mesh;
526
527
VisualDef.VisualName = visual_name;
528
529
VisualDef.RealizeVisual = (int (*)()) vis_mesh;
530
VisualDef.AllocParams = (void *(*)()) vis_mesh_alloc;
531
VisualDef.DeleteParams = (void (*)()) vis_mesh_delete;
532
VisualDef.VisualParams = MeshParams;
533
534
return vis_add_visual_type( &VisualDef );
535
}
536
537