-
Notifications
You must be signed in to change notification settings - Fork 0
/
voxelgrid.c
203 lines (151 loc) · 4.68 KB
/
voxelgrid.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include "voxelgrid.h"
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
static int voxelgrid_get_dimensions (FILE *voxelgrid_file, int *dim_x, int *dim_y, int *dim_z);
VoxelGrid *voxelgrid_load(const char *filename)
{
VoxelGrid *voxelgrid;
FILE *voxelgrid_file;
int dim_x, dim_y, dim_z;
int status;
int x,y,z;
log_printf (1, "reading voxels from: '%s'\n", filename);
voxelgrid_file = fopen(filename, "r");
if (voxelgrid_file == NULL) {
fprintf (stderr, "File read error: %s\n", strerror (errno));
return NULL;
}
status = voxelgrid_get_dimensions(voxelgrid_file, &dim_x, &dim_y, &dim_z);
if (status) {
fprintf(stderr, "Error reading voxelgrid dimensions from file\n");
fclose(voxelgrid_file);
return NULL;
}
voxelgrid = voxelgrid_create(filename, dim_x, dim_y, dim_z);
// load voxels
x = y = z = 0;
fseek (voxelgrid_file, 0L, SEEK_SET);
while (!feof(voxelgrid_file)) {
int c = fgetc (voxelgrid_file);
switch (c) {
case '#':
VOXEL(voxelgrid, x++, y, z) = 1;
break;
case ' ':
VOXEL(voxelgrid, x++, y, z) = 0;
break;
case '-':
if (y) z++;
y = 0;
while (!feof(voxelgrid_file) && fgetc(voxelgrid_file)!='\n') ; // flush line
// no break here!
case '\n':
if (x) y++;
x=0;
break;
}
}
fclose (voxelgrid_file);
return voxelgrid;
}
VoxelGrid *voxelgrid_create(const char *filename, int dim_x, int dim_y, int dim_z)
{
VoxelGrid *voxelgrid = (VoxelGrid*)malloc(sizeof(VoxelGrid));
voxelgrid->dim_x = dim_x;
voxelgrid->dim_y = dim_y;
voxelgrid->dim_z = dim_z;
if (filename == NULL)
voxelgrid->filename[0] = '\0';
else
strcpy (voxelgrid->filename, filename);
voxelgrid->tiles = (char *)malloc ( dim_x * dim_y * dim_z );
memset(voxelgrid->tiles,0,dim_x*dim_y*dim_z*sizeof(char));
return voxelgrid;
}
void voxelgrid_free (VoxelGrid *voxelgrid)
{
if(voxelgrid != NULL){
if (voxelgrid->tiles != NULL)
free (voxelgrid->tiles);
free (voxelgrid);
}
}
void voxelgrid_save(VoxelGrid *voxelgrid)
{
unsigned int x,y,z;
FILE *file;
fprintf (stderr, "Saving to '%s'\n", voxelgrid->filename);
file = fopen (voxelgrid->filename, "w");
if (file == NULL) {
fprintf (stderr, "Could not open file '%s' for writing\n", voxelgrid->filename);
return;
}
for (x=0; x<voxelgrid->dim_x; x++)
fputc ('-', file);
fputc ('\n', file);
for (z=0; z<voxelgrid->dim_z; z++) {
for (y=0; y<voxelgrid->dim_y; y++) {
for (x=0; x<voxelgrid->dim_x; x++) {
int voxel = VOXEL (voxelgrid, x, y, z);
fputc ( voxel ? '#' : ' ' , file);
}
fputc ('\n', file);
}
// add level separator
for (x=0; x<voxelgrid->dim_x; x++)
fputc ('-', file);
fputc ('\n', file);
}
fclose (file);
}
void voxelgrid_save_as(VoxelGrid *voxelgrid, const char *filename)
{
strcpy (voxelgrid->filename, filename);
voxelgrid_save (voxelgrid);
}
/*
* read voxelgrid dimensions from open file
*/
static int voxelgrid_get_dimensions (FILE *voxelgrid_file, int *dim_x, int *dim_y, int *dim_z)
{
if (voxelgrid_file == NULL)
return 1;
fseek (voxelgrid_file, 0L, SEEK_SET);
*dim_x = *dim_y = *dim_z = 0;
int x=0, y=0;
while (!feof (voxelgrid_file)) {
int c = fgetc (voxelgrid_file);
switch(c) {
// new tile
case ' ':
case '#':
x++;
break;
// new level
case '-':
if (y) (*dim_z)++; // new level (only if any rows was encountered)
while (!feof(voxelgrid_file) && fgetc(voxelgrid_file)!='\n') ; // flush line
*dim_y = MAX (*dim_y, y); // update depth
y = 0;
// no break here!
case '\n':
// new line... check and update width
*dim_x = MAX (*dim_x, x);
if (x) y++;
x=0;
break;
case EOF:
// just ignore eof. will handle in while-loop
break;
default:
// unrecognised character = ERROR...
fprintf (stderr, "Illegal character: '%c'\n", c);
return 1;
}
}
return 0;
}