-
Notifications
You must be signed in to change notification settings - Fork 0
/
df3.c
90 lines (78 loc) · 2.11 KB
/
df3.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
81
82
83
84
85
86
87
88
89
90
/* generate_df3.c - generates .df3 volume density files for PovRay
* Believe to be Public Domain & from http://astronomy.swin.edu.au/~pbourke/povray/df3/
* Modified for Linear Combination of Atomic Orbitals visualisation by Jarvist Frost 2004
*/
#include <stdio.h>
#include <math.h>
#define SWAP_2(x) ( (((x) & 0xff) << 8) | ((unsigned short)(x) >> 8) )
#define SWAP_4(x) ( ((x) << 24) | \
(((x) << 8) & 0x00ff0000) | \
(((x) >> 8) & 0x0000ff00) | \
((x) >> 24) )
#define FIX_SHORT(x) (*(unsigned short *)&(x) = SWAP_2(*(unsigned short *)&(x)))
#define FIX_INT(x) (*(unsigned int *)&(x) = SWAP_4(*(unsigned int *)&(x)))
int
generate_df3 (int count)
{
int i, j, k, x, y, z, n;
int ny=Y, nx=X, nz=Z;
char name[20];
short v;
float themin = 1e32, themax = -1e32;
FILE *fptr;
fprintf(stderr,"Generating df3 file: %d\n",count);
/* Calculate the bounds */
for (i = 0; i < X; i++)
{
for (j = 0; j < Y; j++)
{
for (k = 0; k < Z; k++)
{
if (lattice[i][j][k]<0.0) continue;
if (themax < lattice[i][j][k])
themax = lattice[i][j][k];
if (themin > lattice[i][j][k])
themin = lattice[i][j][k];
}
}
}
if (themin >= themax)
{ /* There is no variation */
themax = themin + 1;
themin -= 1;
}
// printf ("themax: %f\tthemin: %f\n", themax, themin);
/* Write it to a file */
sprintf(name,"snakes_%.5d.df3",count);
if ((fptr = fopen (name, "w")) == NULL)
return (-1);
fputc (nx >> 8, fptr);
fputc (nx & 0xff, fptr);
fputc (ny >> 8, fptr);
fputc (ny & 0xff, fptr);
fputc (nz >> 8, fptr);
fputc (nz & 0xff, fptr);
for (k = 0; k < nz; k++)
{
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
if (lattice[i][j][k]>=0)
{
if (perc[i][j][k]>0)
//v = (int) ((float) 65535 * (lattice[i][j][k] - themin) / (themax - themin));
v=65535;
else
v=65535/4;
}
else
v=0; //65535
FIX_SHORT (v);
fwrite (&v, 2, 1, fptr);
}
}
}
fclose (fptr);
fprintf(stderr,"Generated.\n");
}