-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvel2heat.c
80 lines (69 loc) · 2.07 KB
/
vel2heat.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "src/definitions.h"
#include "src/geometry.h"
float *mat, *mat2;
#define MAT(y,x) mat[(y)*L_WIDTH+(x)]
#define MAT2(y,x) mat2[(y)*L_WIDTH+(x)]
int
main ( int argc, char **argv )
{
if ( argc < 2 )
{
fputs ( "Need a file name\n", stderr );
exit ( EXIT_FAILURE );
}
mat = malloc ( L_WIDTH*L_HEIGHT*sizeof(float) );
int length = strlen(argv[1]);
char fname[length+1];
memcpy ( fname, argv[1], length );
fname[length] = 0;
fname[length-4] = '.';
fname[length-3] = 'm';
fname[length-2] = 'a';
fname[length-1] = 't';
/* Populate the matrix with z-values
* Using velocity vector norm to highlight areas with fast flow
*/
FILE *in = fopen ( argv[1], "r" );
for ( int i=0; i<(L_HEIGHT*L_WIDTH); i++ )
{
point_t *p, point;
p = &point;
int read;
read = fread ( &(p->position[0]), sizeof(float), 1, in );
read = fread ( &(p->position[1]), sizeof(float), 1, in );
read = fread ( &(p->velocity[0]), sizeof(float), 1, in );
read = fread ( &(p->velocity[1]), sizeof(float), 1, in );
if ( read != 1 )
fprintf ( stderr, "Warning, read error at %d\n", i );
float z = 0.0;
z = sqrtf (powf(p->velocity[0],2.0) + powf(p->velocity[1],2.0));
int x = (int) floor(p->position[0]);
int y = (int) floor(p->position[1]);
MAT(y,x) = z;
}
fclose ( in );
/* Write to gnuplot binary matrix file */
FILE *out = fopen ( fname, "w" );
float tmp = (float)L_WIDTH;
fwrite ( &tmp, sizeof(float), 1, out );
for ( int j=0; j<L_WIDTH; j++ )
{
tmp = (float)j;
fwrite ( &tmp, sizeof(float), 1, out );
}
for ( int i=0; i<L_HEIGHT; i++ )
{
tmp = (float)i;
fwrite ( &tmp, sizeof(float), 1, out );
for ( int j=0; j<L_WIDTH; j++ )
fwrite ( &MAT(i,j), sizeof(float), 1, out );
}
fclose ( out );
free ( mat );
free ( mat2 );
exit ( EXIT_SUCCESS );
}