-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.c
More file actions
496 lines (400 loc) · 11.8 KB
/
volume.c
File metadata and controls
496 lines (400 loc) · 11.8 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "mesh.h"
#include "info/info.h"
#include "volume_internal.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#define THRESHOLD 128
bool* Volume_get(Volume v, size_t x, size_t y, size_t z) { return v->data[x][y]+z; }
Volume Volume_create()
{
Volume vol = malloc(sizeof(struct Volume));
memset(vol,0,sizeof(struct Volume));
return vol;
}
void Volume_internal_free(Volume vol)
{
for(int x=0; x<vol->x; x++)
{
for(int y=0; y<vol->y; y++)
{
free(vol->data[x][y]);
}
free(vol->data[x]);
}
free(vol->data);
}
bool Volume_allocate(Volume vol, int width, int height, int length)
{
if(vol->data)
Volume_internal_free(vol);
vol->x = width;
vol->y = height;
vol->z = length;
vol->data=malloc(width*sizeof(*vol->data));
for(int x=0; x<width; x++)
{
vol->data[x]=malloc(height*sizeof(**vol->data));
for(int y=0; y<height; y++)
{
vol->data[x][y]=malloc(length*sizeof(***vol->data));
memset(vol->data[x][y],0,length);
}
}
return false;
}
bool Volume_from_shadow_3(Volume vol, Image front, Image side, Image top)
{
if( Image_get_x(front)!=Image_get_x(top) ||
Image_get_x(side)!=Image_get_y(top) ){
ERROR("Top shadowmap doesn't align with sides")
return true;
}
if(Volume_from_shadow_2(vol, front, side))
return true;
bool *voxel;
for(int z=0; z<vol->z; z++)
for(int y=0; y<vol->y; y++)
for(int x=0; x<vol->x; x++){
voxel=Volume_get(vol,x,y,z);
*voxel = *voxel && (*Image_get(top, x, z)<THRESHOLD);
}
return false;
};
bool Volume_from_shadow_2(Volume vol, Image front, Image side)
{
if(Image_get_y(front)!=Image_get_y(side)){
ERROR("Side shadowmaps missalign in height! %d, %d", Image_get_y(front), Image_get_y(side))
return true;
}
int width=Image_get_x(front),
height=Image_get_y(front),
length=Image_get_x(side);
INFO("Voxel Volume dimensions: X %d Y %d Z %d", width, height, length)
Volume_allocate(vol, width, height, length);
for(int z=0; z<vol->z; z++)
for(int y=0; y<vol->y; y++)
for(int x=0; x<vol->x; x++)
*Volume_get(vol,x,y,z)= (
(*Image_get(front, x, height-y-1)<THRESHOLD) &&
(*Image_get(side, z, height-y-1)<THRESHOLD)
);
return false;
}
char *blocks_transparrent_names[] = {
"minecraft:air",
"minecraft:torch",
"minecraft:redstone"
};
struct Blocks_transparrent blocks_transparrent_default = { blocks_transparrent_names, sizeof(blocks_transparrent_names)/sizeof(*blocks_transparrent_names)};
bool util_contains_string(struct Blocks_transparrent *container, char *str)
{
for(size_t i=0; i<container->size; i++)
{
if(!strcmp(container->names[i], str))
return 1;
}
return 0;
}
int max(int a, int b)
{
if(a>b)
return a;
return b;
}
int min(int a, int b)
{
if(a<b)
return a;
return b;
}
bool Volume_from_NBT(Volume vol, NBT nbt, struct Blocks_transparrent *blocks_transparrent)
{
NBT_Data size = NBT_data(NBT_compound_get_name(NBT_data(nbt), "size"));
NBT_Data blocks = NBT_data(NBT_compound_get_name(NBT_data(nbt), "blocks"));
NBT_Data palette = NBT_data(NBT_compound_get_name(NBT_data(nbt), "palette"));
if(!blocks) {
ERROR("Could not load 'blocks'")
return true;
}
if(!palette) {
ERROR("Could not load 'palette'")
return true;
}
if(!size) {
ERROR("Could not load 'size'")
return true;
}
/*
if(NBT_list_length(size)!=3 || NBT_list_type_get(size)!=NBT_INT){
ERROR("'size' Tag malformed")
return true;
}
vol->x = NBT_integer_get(NBT_list_get(size,0));
vol->y = NBT_integer_get(NBT_list_get(size,1));
vol->z = NBT_integer_get(NBT_list_get(size,2));
*/
INFO("Creating transparrency Mask")
if(!blocks_transparrent)
blocks_transparrent = &blocks_transparrent_default;
size_t palette_count = NBT_list_length(palette);
bool *mask = malloc(palette_count);
char *name;
for(int i=0; i<palette_count; i++)
{
name = NBT_string_get(NBT_data(NBT_compound_get_index(NBT_list_get(palette,i),0)));
if(name)
mask[i] =!util_contains_string(blocks_transparrent, name);
else
goto cleanup2;
}
NBT_Data block, pos;
size_t block_count = NBT_list_length(blocks),
palette_index,
i;
INFO("Found %d blocks", block_count)
//calulate actual size of structure
int x_min=0,
y_min=0,
z_min=0,
x_max=0,
y_max=0,
z_max=0,
x,y,z;
for(i=0; i < block_count; i++)
{
block = NBT_list_get(blocks, i);
pos = NBT_data(NBT_compound_get_name(block, "pos"));
if(!pos){
ERROR("Could not find block 'pos'")
goto cleanup;
}
if(NBT_list_length(pos)!=3)
goto cleanup;
palette_index = NBT_integer_get(NBT_data(NBT_compound_get_name(block,"state")));
if(palette_index>palette_count)
goto cleanup;
x=NBT_integer_get(NBT_list_get(pos,0));
y=NBT_integer_get(NBT_list_get(pos,1));
z=NBT_integer_get(NBT_list_get(pos,2));
x_min = min(x_min, x);
y_min = min(y_min, y);
z_min = min(z_min, z);
x_max = max(x_max, x);
y_max = max(y_max, y);
z_max = max(z_max, z);
}
vol->x=x_max-x_min+1;
vol->y=y_max-y_min+1;
vol->z=z_max-z_min+1;
INFO("allocate Voxel Volume x: %d y: %d z: %d", vol->x, vol->y, vol->z);
Volume_allocate(vol, vol->x, vol->y, vol->z);
INFO("Writing Voxels")
for(i=0; i<block_count; i++)
{
block = NBT_list_get(blocks, i);
pos = NBT_data(NBT_compound_get_name(block, "pos"));
if(!pos){
ERROR("Could not find block 'pos'")
goto cleanup;
}
if(NBT_list_length(pos)!=3)
goto cleanup;
palette_index = NBT_integer_get(NBT_data(NBT_compound_get_name(block,"state")));
if(palette_index>palette_count)
goto cleanup;
x=NBT_integer_get(NBT_list_get(pos,0))-x_min;
y=NBT_integer_get(NBT_list_get(pos,1))-y_min;
z=NBT_integer_get(NBT_list_get(pos,2))-z_min;
*Volume_get(vol, x, y, z)=mask[palette_index];
}
free(mask);
SUCCESS("Volume generation!")
return false;
cleanup:
ERROR("block at index '%ld' is malformed", i);
cleanup2:
free(mask);
return true;
}
bool Volume_to_NBT(Volume vol, NBT nbt, char *material_name)
{
NBT tmp = NBT_create();
// Compound
NBT_type_set(nbt, NBT_COMPOUND);
// DataVersion
INFO("creating 'DataVersion'")
NBT data_version = NBT_create();
{
NBT_type_set(data_version, NBT_INT);
NBT_integer_set(NBT_data(data_version), 3105);
}
// size
INFO("creating 'size' -> %d | %d | %d", vol->x, vol->y, vol->z)
NBT size = NBT_create();
{
NBT_type_set(size, NBT_LIST);
NBT_list_type_set(NBT_data(size), NBT_INT);
NBT_integer_set(NBT_data(tmp), vol->x);
NBT_list_set(NBT_data(size), 0, NBT_data(tmp));
NBT_integer_set(NBT_data(tmp), vol->y);
NBT_list_set(NBT_data(size), 1, NBT_data(tmp));
NBT_integer_set(NBT_data(tmp), vol->z);
NBT_list_set(NBT_data(size), 2, NBT_data(tmp));
}
// blocks
INFO("writing block data");
NBT blocks = NBT_create();
{
NBT_type_set(blocks, NBT_LIST);
NBT_list_type_set(NBT_data(blocks), NBT_COMPOUND);
size_t blocks_index=0;
for(int z=0; z<vol->z; z++)
{
for(int y=0; y<vol->y; y++)
{
for(int x=0; x<vol->x; x++)
{
if(*Volume_get(vol, x, y, z)){
NBT block = NBT_create();
NBT_type_set(block, NBT_COMPOUND);
// pos
NBT pos = NBT_create();
NBT_type_set(pos, NBT_LIST);
NBT_list_type_set(NBT_data(pos), NBT_INT);
NBT_integer_set(NBT_data(tmp), x);
NBT_list_set(NBT_data(pos), 0, NBT_data(tmp));
NBT_integer_set(NBT_data(tmp), y);
NBT_list_set(NBT_data(pos), 1, NBT_data(tmp));
NBT_integer_set(NBT_data(tmp), z);
NBT_list_set(NBT_data(pos), 2, NBT_data(tmp));
NBT_compound_set_name(NBT_data(block), "pos", pos);
// palette
NBT palette = NBT_create();
NBT_type_set(palette, NBT_INT);
NBT_integer_set(NBT_data(palette), 0);
NBT_compound_set_name(NBT_data(block), "state", palette);
//INFO("appending block")
NBT_list_set(NBT_data(blocks), blocks_index++, NBT_data(block));
free(block);
free(palette);
free(pos);
}
}
}
}
INFO("Wrote %d blocks", NBT_list_length(NBT_data(blocks)))
}
// palette
INFO("creating 'palette' using '%s'", material_name)
NBT palette = NBT_create();
{
NBT_type_set(palette, NBT_LIST);
NBT_list_type_set(NBT_data(palette), NBT_COMPOUND);
NBT material = NBT_create();
NBT_type_set(material, NBT_COMPOUND);
NBT material_tag = NBT_create();
NBT_type_set(material_tag, NBT_STRING);
NBT_string_set(NBT_data(material_tag), material_name);
NBT_compound_set_name(NBT_data(material), "Name", material_tag);
NBT_list_set(NBT_data(palette), 0, NBT_data(material));
free(material);
free(material_tag);
}
INFO("Filling NBT")
NBT_compound_set_name(NBT_data(nbt), "DataVersion", data_version);
NBT_compound_set_name(NBT_data(nbt), "size", size);
NBT_compound_set_name(NBT_data(nbt), "blocks", blocks);
NBT_compound_set_name(NBT_data(nbt), "palette", palette);
// cleanup
free(tmp);
free(data_version);
free(size);
free(blocks);
free(palette);
return false;
}
bool Volume_to_shadow(Volume vol, Image *front, Image *side, Image *top)
{
*front = Image_create();
Image_from_color(*front, vol->x, vol->y, 255);
*side = Image_create();
Image_from_color(*side, vol->z, vol->y, 255);
*top = Image_create();
Image_from_color(*top, vol->x, vol->z, 255);
INFO("Voxel resolution: %dx%dx%d", vol->x, vol->y, vol->z)
for(int y=0; y<vol->y; y++)
{
for(int x=0; x<vol->x; x++)
{
for(int z=0; z<vol->z; z++)
{
if(*Volume_get(vol,x,y,z)){
*Image_get(*front,x,y)=0;
*Image_get(*side,z,y)=0;
*Image_get(*top,x,z)=0;
}
}
}
}
return false;
}
bool cmp(void *ptr_a, void *ptr_b)
{
float *a=ptr_a,
*b=ptr_b;
return *a<*b;
}
bool Volume_from_mesh(Volume vol, List trigs, size_t res[3])
{
Volume_allocate(vol, res[0], res[1], res[2]);
INFO("Voxel resolution: %dx%dx%d", vol->x, vol->y, vol->z)
struct Dimensions mesh_dimensions = Mesh_dimensions(trigs);
INFO("Mesh Dimensions %f <= x <= %f\n%f <= y <= %f\n%f <= z <= %f",
mesh_dimensions.min.v.x, mesh_dimensions.max.v.x,
mesh_dimensions.min.v.y, mesh_dimensions.max.v.y,
mesh_dimensions.min.v.z, mesh_dimensions.max.v.z)
INFO("Calculate Collisions")
size_t collisions_total = 0;
for(size_t y=0; y<res[1]; y++)
{
for(size_t x=0; x<res[0]; x++)
{
// Calculate space coordinates
float fx = (float)(x+.5)/res[0]*(mesh_dimensions.max.v.x-mesh_dimensions.min.v.x)+mesh_dimensions.min.v.x;
float fy = (float)(y+.5)/res[1]*(mesh_dimensions.max.v.y-mesh_dimensions.min.v.y)+mesh_dimensions.min.v.y;
List intersections = Mesh_intersects(trigs, (Vector){fx,fy, 0}, (Vector){0,0,1});
if(List_size(intersections)%2)
INFO("Non Manifold Object", x, y)
size_t intersections_count = List_size(intersections);
if(intersections_count){
collisions_total+=intersections_count;
List_sort(intersections, cmp);
// write collisions
size_t coll_index=0;
while(coll_index<intersections_count-1)
{
size_t start =Coord_remap(List_GET_REF(float, intersections,coll_index++), mesh_dimensions, 2)*res[2],
end = Coord_remap(List_GET_REF(float, intersections,coll_index++), mesh_dimensions, 2)*res[2];
if(start==end){
coll_index--;
continue;
}
if(end>res[2]) end=res[2];
for(; start<end; start++)
*Volume_get(vol, x, y, start) = 1;
}
}
List_free(intersections);
}
}
INFO("Collision count: %llu", collisions_total)
SUCCESS("Voxelisation done!")
return false;
}
void Volume_free(Volume v)
{
Volume_internal_free(v);
free(v);
}