-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibmapvxl.c
289 lines (250 loc) · 8.9 KB
/
libmapvxl.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
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
#include "libmapvxl.h"
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
static inline void _mapvxl_unset_block(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
if (x > map->size_x || y > map->size_y || z > map->size_z) {
printf("LIBMAPVXL ERROR: Coordinates X: %d Y: %d Z: %d are out of bounds during block clearing\n", x, y, z);
assert(1);
}
map->blocks[x][y][z].data &= ~0x01;
}
static inline int _mapvxl_is_solid(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
return map->blocks[x][y][z].data & 0x01;
}
static inline uint8_t* _mapvxl_write_block_color(uint8_t* out, mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
mapvxl_block_t* block = &map->blocks[x][y][z];
// assume color is ARGB native, but endianness is unknown
// file format endianness is ARGB little endian, i.e. B,G,R,A
*(out++) = block->b;
*(out++) = block->g;
*(out++) = block->r;
*(out++) = 0xFF;
return out;
}
void mapvxl_create(mapvxl_t* map, uint16_t size_x, uint16_t size_y, uint16_t size_z)
{
assert(size_x > 0 && size_x < 0xFFFF);
assert(size_y > 0 && size_y < 0xFFFF);
assert(size_z > 0 && size_z < 0xFFFF);
map->size_x = size_x;
map->size_y = size_y;
map->size_z = size_z;
uint32_t size_xy = size_x * size_y;
uint32_t size_xyz = size_xy * size_z;
map->memory_blocks_pp = (mapvxl_block_t***)calloc(size_x, sizeof(mapvxl_block_t**));
map->memory_blocks_p = (mapvxl_block_t**)calloc(size_xy, sizeof(mapvxl_block_t*));
map->memory_blocks = (mapvxl_block_t*)calloc(size_xyz, sizeof(mapvxl_block_t));
for (uint16_t i = 0; i < size_x; ++i) {
map->memory_blocks_pp[i] = map->memory_blocks_p + (i * size_y);
}
for (uint32_t i = 0; i < size_xy; ++i) {
map->memory_blocks_p[i] = map->memory_blocks + (i * size_z);
}
map->blocks = map->memory_blocks_pp;
}
void mapvxl_free(mapvxl_t* map)
{
free(map->memory_blocks_pp);
free(map->memory_blocks_p);
free(map->memory_blocks);
}
static inline uint8_t* _mapvxl_read_column(mapvxl_t* map, uint8_t* data, uint16_t x, uint16_t y)
{
for (uint32_t z = 0;;) {
uint8_t span_size = data[0]; // N
uint8_t top_start = data[1]; // S
uint8_t top_end = data[2]; // E
// mapvxl_byte_t air_start = data[3]; // A - unused
// air
for (; z < top_start; ++z) {
_mapvxl_unset_block(map, x, y, z);
}
// number of top blocks
uint8_t top_length = top_end - top_start + 1; // K = S - E + 1
// top
const uint32_t* colors = (const uint32_t*)(data + 4);
for (; z <= top_end; ++z, ++colors) {
mapvxl_set_color(map, x, y, z, *colors);
}
if (span_size == 0) { // last span in column
data += 4 * (top_length + 1);
break;
}
// number of bottom blocks
uint8_t bottom_length = (span_size - 1) - top_length; // Z = (N - 1) - K
// move to the next span
data += span_size * 4;
// bottom ends where air begins
uint8_t bottom_end = data[3]; // (M - 1) - block end, M - next span air
uint8_t bottom_start = bottom_end - bottom_length; // M - Z
// bottom
for (z = bottom_start; z < bottom_end; ++z, ++colors) {
mapvxl_set_color(map, x, y, z, *colors);
}
}
return data;
}
void mapvxl_read(mapvxl_t* map, uint8_t* src)
{
const unsigned int BLOCK_CLEAR = 0x01000000 | (MAPVXL_DEFAULT_COLOR & 0x00FFFFFF);
for (uint16_t y = 0; y < map->size_y; ++y) {
for (uint16_t x = 0; x < map->size_x; ++x) {
for (uint16_t z = 0; z < map->size_z; ++z) {
map->blocks[x][y][z].raw = BLOCK_CLEAR;
}
src = _mapvxl_read_column(map, src, x, y);
}
}
}
static inline uint8_t* _mapvxl_write_column(mapvxl_t* map, uint8_t* data, uint16_t i, uint16_t j)
{
for (uint16_t k = 0; k < map->size_z;) {
// find the air region
uint8_t air_start = k;
while (k < map->size_z && !_mapvxl_is_solid(map, i, j, k)) {
++k;
}
// find the top region
uint8_t top_start = k;
while (k < map->size_z && mapvxl_is_surface(map, i, j, k)) {
++k;
}
uint8_t top_end = k;
// now skip past the solid voxels
while (k < map->size_z && _mapvxl_is_solid(map, i, j, k) && !mapvxl_is_surface(map, i, j, k)) {
++k;
}
// at the end of the solid voxels, we have colored voxels.
// in the "normal" case they're bottom colors; but it's
// possible to have air-color-solid-color-solid-color-air,
// which we encode as air-color-solid-0, 0-color-solid-air
// so figure out if we have any bottom colors at this point
uint16_t z = k;
uint8_t bottom_start = k;
while (z < map->size_z && mapvxl_is_surface(map, i, j, z)) {
++z;
}
if (z != map->size_z) {
// these are real bottom colors so we can write them
// otherwise, the bottom colors of this span are empty, because we'l
// emit as top colors
while (mapvxl_is_surface(map, i, j, k)) {
++k;
}
}
uint8_t bottom_end = k;
// now we're ready to write a span
uint8_t top_length = top_end - top_start;
uint8_t bottom_length = bottom_end - bottom_start;
uint8_t colors_length = top_length + bottom_length;
// span size
if (k == map->size_z) {
*(data++) = 0;
} else {
*(data++) = colors_length + 1;
}
*(data++) = top_start;
*(data++) = top_end - 1;
*(data++) = air_start;
for (z = 0; z < top_length; ++z) {
data = _mapvxl_write_block_color(data, map, i, j, top_start + z);
}
for (z = 0; z < bottom_length; ++z) {
data = _mapvxl_write_block_color(data, map, i, j, bottom_start + z);
}
}
return data;
}
size_t mapvxl_write(mapvxl_t* map, uint8_t* out)
{
// The size of mapOut should be the max possible memory size of
// uncompressed VXL format in memory given the XYZ size
// which is map->MAP_X_MAX * map->MAP_Y_MAX * (map->MAP_Z_MAX / 2) * 8
uint8_t* begin = out;
for (uint16_t j = 0; j < map->size_x; ++j) {
for (uint16_t i = 0; i < map->size_y; ++i) {
out = _mapvxl_write_column(map, out, i, j);
}
}
return out - begin;
}
uint8_t mapvxl_find_top_block(mapvxl_t* map, uint16_t x, uint16_t y)
{
if (x > map->size_x || y > map->size_y) {
return 0;
}
for (uint8_t z = 0; z < map->size_z; ++z) {
if (map->blocks[x][y][z].data & 0x01) {
return z;
}
}
return 0;
}
void mapvxl_set_color(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z, uint32_t c)
{
if (x > map->size_x || y > map->size_y || z > map->size_z) {
printf("LIBMAPVXL ERROR: Coordinates X: %d Y: %d Z: %d are out of bounds during color set\n", x, y, z);
assert(1);
}
map->blocks[x][y][z].raw = 0x01000000 | (c & 0x00FFFFFF); // Set visible and copy RGB from ARGB
}
uint32_t mapvxl_get_color(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
if (x > map->size_x || y > map->size_y || z > map->size_z) {
printf("LIBMAPVXL ERROR: Coordinates X: %d Y: %d Z: %d are out of bounds during get color\n", x, y, z);
assert(1);
}
if (!_mapvxl_is_solid(map, x, y, z)) {
return MAPVXL_DEFAULT_COLOR;
}
return map->blocks[x][y][z].raw | 0xFF000000;
}
void mapvxl_set_air(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
if (x > map->size_x || y > map->size_y || z > map->size_z) {
printf("LIBMAPVXL ERROR: Coordinates X: %d Y: %d Z: %d are out of bounds during setting air\n", x, y, z);
assert(1);
}
map->blocks[x][y][z].raw = 0x00FFFFFF & MAPVXL_DEFAULT_COLOR;
}
int mapvxl_is_surface(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
if (!_mapvxl_is_solid(map, x, y, z)) {
return 0;
}
if (z == 0) {
return 1;
}
if (x > 0 && !_mapvxl_is_solid(map, x - 1, y, z)) {
return 1;
}
if (x + 1 < map->size_x && !_mapvxl_is_solid(map, x + 1, y, z)) {
return 1;
}
if (y > 0 && !_mapvxl_is_solid(map, x, y - 1, z)) {
return 1;
}
if (y + 1 < map->size_y && !_mapvxl_is_solid(map, x, y + 1, z)) {
return 1;
}
if (z > 0 && !_mapvxl_is_solid(map, x, y, z - 1)) {
return 1;
}
if (z + 1 < map->size_z && !_mapvxl_is_solid(map, x, y, z + 1)) {
return 1;
}
return 0;
}
int mapvxl_is_solid(mapvxl_t* map, uint16_t x, uint16_t y, uint16_t z)
{
if (x > map->size_x || y > map->size_y || z > map->size_z) {
printf("LIBMAPVXL ERROR: Coordinates X: %d Y: %d Z: %d are out of bounds during solid check\n", x, y, z);
assert(1);
}
return _mapvxl_is_solid(map, x, y, z);
}