-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaballs.h
1092 lines (965 loc) · 44.3 KB
/
metaballs.h
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Metaballs library generator - v1.0 - public domain
// https://github.com/casensiom/metaballs
//
#ifndef _METABALLS_H_
#define _METABALLS_H_
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#define MB_DEFINE_ARRAY(TYPE) \
typedef struct TYPE##_struct_array \
{ \
TYPE *items; \
size_t count; \
size_t capacity; \
} TYPE##Array;
#define MB_DEFINE_FIXED_ARRAY(TYPE, SIZE) \
typedef struct TYPE##_struct_array_##SIZE \
{ \
TYPE items[SIZE]; \
size_t count; \
} TYPE##Array##SIZE;
#define MB_CREATE_ARRAY(TYPE, CAPACITY) \
(TYPE##Array) \
{ \
.items = (TYPE *)malloc((CAPACITY) * sizeof(TYPE)), \
.count = 0, \
.capacity = (CAPACITY) \
}
#define MB_DESTROY_ARRAY(arr) \
{ \
free(arr.items); \
arr.items = NULL; \
arr.count = 0; \
arr.capacity = 0; \
}
enum MB_COORDINATE
{
MB_COORD_X = 0,
MB_COORD_Y = 1,
MB_COORD_Z = 2,
MB_COORD_COUNT
};
typedef struct vector3_struct
{
float x;
float y;
float z;
} Vector3d;
typedef struct vector2_struct
{
float x;
float y;
} Vector2d;
typedef struct color_struct
{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} Color4C;
typedef struct vertex3_struct
{
Vector3d pos;
Vector3d normal;
Vector2d text_coord;
Color4C color;
} Vertex3d;
typedef struct index3_struct
{
size_t x;
size_t y;
size_t z;
} Index3d;
MB_DEFINE_ARRAY(Index3d);
typedef struct ball_struct
{
Vector3d pos;
float mass;
Color4C color;
} Ball;
MB_DEFINE_ARRAY(Ball);
typedef size_t Index;
MB_DEFINE_ARRAY(Index);
typedef struct cache_struct
{
bool dirty;
Vector3d value;
} Cache;
typedef struct node_struct
{
float energy;
Cache cache[MB_COORD_COUNT];
bool visited;
} Node;
MB_DEFINE_ARRAY(Node);
typedef struct voxelcorner_struct
{
Index3d grid_pos;
size_t grid_index;
float energy;
} VoxelCorner;
typedef struct triangle_struct
{
Vertex3d vertex1;
Vertex3d vertex2;
Vertex3d vertex3;
} Triangle;
typedef struct mesh3d_struct
{
size_t vertexCapacity;
size_t triangleCapacity;
size_t vertexCount;
size_t triangleCount;
float *vertices;
float *texcoords;
unsigned char *colors;
float *normals;
unsigned short *indices;
} Mesh3d;
typedef struct voxel_struct
{
VoxelCorner corners[8];
size_t bits;
uint8_t neighbors;
} Voxel;
typedef enum
{
MAPPING_PLANAR = 0,
MAPPING_SPHERIC
} Mapping;
typedef struct config_struct
{
bool wired;
Mapping mapping;
float threshold;
} Config;
typedef struct grid_struct
{
Vector3d pos;
Index3d count; // number of subdivision of space
Vector3d size; // size of separation between space subdivisions
Vector3d center;
NodeArray nodes;
Index3dArray queue; // list of pending voxels to compute
Mesh3d mesh;
Config config;
} Grid;
#if defined(__cplusplus)
extern "C"
{
#endif
Grid metaball_grid_create(Index3d count, Vector3d size, Vector3d pos);
void metaball_grid_destroy(Grid *grid);
void metaball_generate_mesh(Grid *grid, BallArray balls);
#if defined(__cplusplus)
}
#endif
#endif // _METABALLS_H_
#ifdef METABALLS_IMPLEMENTATION
static char marching_cube_triangles[256][16];
static char marching_cube_edges[12][2];
static int marching_cube_vertices[8][3];
static char marching_cube_neighbors[256];
static float clamp(float v, float min, float max)
{
return (v < min) ? min : (v > max) ? max : v;
}
static Vector3d vector3d_add(Vector3d orig, Vector3d val)
{
return (Vector3d){
.x = orig.x + val.x,
.y = orig.y + val.y,
.z = orig.z + val.z};
}
static Vector3d vector3d_substract(Vector3d orig, Vector3d val)
{
return (Vector3d){
.x = orig.x - val.x,
.y = orig.y - val.y,
.z = orig.z - val.z};
}
static Vector3d vector3d_multiply(Vector3d v, float factor)
{
return (Vector3d){
.x = v.x * factor,
.y = v.y * factor,
.z = v.z * factor};
}
static float vector3d_length(Vector3d start, Vector3d end)
{
float x = end.x - start.x;
float y = end.y - start.y;
float z = end.z - start.z;
return sqrtf(x * x + y * y + z * z);
}
static Vector3d vector3d_normalize(Vector3d v)
{
float magnitude = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
v.x /= magnitude;
v.y /= magnitude;
v.z /= magnitude;
return v;
}
static Vector3d vector3d_cross_product(Vector3d v1, Vector3d v2)
{
Vector3d result;
result.x = v1.y * v2.z - v1.z * v2.y;
result.y = v1.z * v2.x - v1.x * v2.z;
result.z = v1.x * v2.y - v1.y * v2.x;
return result;
}
static float vector3d_dot_product(Vector3d v1, Vector3d v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
static Vector3d compute_grid_point_coordinates(Index3d pos, Grid grid)
{
Vector3d out;
out.x = grid.pos.x + (pos.x * grid.size.x);
out.y = grid.pos.y + (pos.y * grid.size.y);
out.z = grid.pos.z + (pos.z * grid.size.z);
return out;
}
static size_t compute_grid_point_index(Index3d pos, Grid grid)
{
size_t ret = pos.x +
(pos.y * grid.count.x) +
(pos.z * grid.count.x * grid.count.y);
assert(ret < grid.count.x * grid.count.y * grid.count.z);
return ret;
}
static Index3d compute_grid_index(Vector3d pos, Grid grid)
{
Index3d out;
out.x = (size_t)floor((pos.x - grid.pos.x) / grid.size.x);
out.y = (size_t)floor((pos.y - grid.pos.y) / grid.size.y);
out.z = (size_t)floor((pos.z - grid.pos.z) / grid.size.z);
return out;
}
//--
static Mesh3d mesh_create(Grid grid)
{
Mesh3d mesh = {0};
size_t max_triangles = grid.count.x * grid.count.y * grid.count.z * 5; // maximum 5 triangles per voxel
size_t max_vertex = max_triangles * 3;
mesh.vertexCapacity = max_vertex;
mesh.triangleCapacity = max_triangles;
mesh.vertexCount = 0;
mesh.triangleCount = 0;
mesh.vertices = (float *)malloc(max_vertex * 3 * sizeof(float));
mesh.texcoords = (float *)malloc(max_vertex * 2 * sizeof(float));
mesh.colors = (unsigned char *)malloc(max_vertex * 4 * sizeof(unsigned char));
mesh.normals = (float *)malloc(max_vertex * 3 * sizeof(float));
mesh.indices = (unsigned short *)malloc(max_vertex * sizeof(unsigned short));
return mesh;
}
static void mesh_destroy(Mesh3d *mesh)
{
free(mesh->vertices);
free(mesh->texcoords);
free(mesh->colors);
free(mesh->normals);
free(mesh->indices);
*mesh = (Mesh3d){0};
}
static void mesh_add_vertex(Mesh3d *mesh, Vertex3d v)
{
mesh->vertices[3 * mesh->vertexCount + 0] = v.pos.x;
mesh->vertices[3 * mesh->vertexCount + 1] = v.pos.y;
mesh->vertices[3 * mesh->vertexCount + 2] = v.pos.z;
mesh->texcoords[2 * mesh->vertexCount + 0] = v.text_coord.x;
mesh->texcoords[2 * mesh->vertexCount + 1] = v.text_coord.y;
mesh->colors[4 * mesh->vertexCount + 0] = v.color.r;
mesh->colors[4 * mesh->vertexCount + 1] = v.color.g;
mesh->colors[4 * mesh->vertexCount + 2] = v.color.b;
mesh->colors[4 * mesh->vertexCount + 3] = v.color.a;
mesh->normals[3 * mesh->vertexCount + 0] = v.normal.x;
mesh->normals[3 * mesh->vertexCount + 1] = v.normal.y;
mesh->normals[3 * mesh->vertexCount + 2] = v.normal.z;
mesh->indices[mesh->vertexCount] = mesh->vertexCount;
mesh->vertexCount++;
}
static void mesh_add_triangle(Mesh3d *mesh, Triangle t)
{
mesh_add_vertex(mesh, t.vertex1);
mesh_add_vertex(mesh, t.vertex2);
mesh_add_vertex(mesh, t.vertex3);
mesh->triangleCount++;
}
static void set_energy_point(Index3d grid_pos, Grid grid, float energy)
{
assert(grid_pos.x < grid.count.x);
assert(grid_pos.y < grid.count.y);
assert(grid_pos.z < grid.count.z);
size_t grid_index = compute_grid_point_index(grid_pos, grid);
grid.nodes.items[grid_index].energy = energy;
grid.nodes.items[grid_index].cache[MB_COORD_X].dirty = true;
grid.nodes.items[grid_index].cache[MB_COORD_Y].dirty = true;
grid.nodes.items[grid_index].cache[MB_COORD_Z].dirty = true;
grid.nodes.items[grid_index].visited = false;
}
static float compute_energy(Vector3d world_pos, BallArray balls)
{
// e = mass/distance^2
size_t i;
float energy = 0;
for (i = 0; i < balls.count; i++)
{
float dist_x = world_pos.x - balls.items[i].pos.x;
float dist_y = world_pos.y - balls.items[i].pos.y;
float dist_z = world_pos.z - balls.items[i].pos.z;
float dist = (dist_x * dist_x + dist_y * dist_y + dist_z * dist_z);
if (dist < 0.0001f)
{
dist = 0.0001f;
}
energy += balls.items[i].mass / dist;
// energy += 1.0f / sqrtf(dist);
}
return energy;
}
static Vector3d compute_normal_balls(Vector3d pos, BallArray balls)
{
Vector3d nor = (Vector3d){.x = 0.0, .y = 0.0001, .z = 0.0};
for (size_t i = 0; i < balls.count; ++i)
{
Vector3d dir = vector3d_substract(pos, balls.items[i].pos);
Vector3d v = {0};
#if 1 // simplification
float dist = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z;
if (dist > 1e-6)
{
v.x = dir.x / dist;
v.y = dir.y / dist;
v.z = dir.z / dist;
}
#endif
#if 0 // derive energy over position => 2 * mass / distance^4
float dist = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z;
v.x = 2 * balls.items[i].mass * dir.x / (dist * dist);
v.y = 2 * balls.items[i].mass * dir.y / (dist * dist);
v.z = 2 * balls.items[i].mass * dir.z / (dist * dist);
#endif
#if 0
float db = vector3d_length(balls.items[i].pos, pos);
float x = clamp(db/balls.items[i].mass, 0.0, 1.0);
float p = x*x*(30.0*x*x - 60.0*x + 30.0);
v = vector3d_normalize( dir );
v = vector3d_multiply(v, p / balls.items[i].mass);
#endif
nor = vector3d_add(nor, v);
}
return vector3d_normalize(nor);
}
static Vertex3d compute_vertex(Vector3d vertex, Grid grid, BallArray balls)
{
Vertex3d v;
v.pos = vertex;
// Compute vertex normal
v.normal = compute_normal_balls(v.pos, balls);
float total_energy = 0;
for (size_t i = 0; i < balls.count; i++)
{
if (balls.items[i].mass < 0)
{
continue;
}
Vector3d dir = {
.x = v.pos.x - balls.items[i].pos.x,
.y = v.pos.y - balls.items[i].pos.y,
.z = v.pos.z - balls.items[i].pos.z};
float dist = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z;
total_energy += balls.items[i].mass / dist;
}
Color4C color = {0};
for (size_t i = 0; i < balls.count; i++)
{
if (balls.items[i].mass < 0)
{
continue;
}
Vector3d dir = {
.x = v.pos.x - balls.items[i].pos.x,
.y = v.pos.y - balls.items[i].pos.y,
.z = v.pos.z - balls.items[i].pos.z};
float dist = dir.x * dir.x + dir.y * dir.y + dir.z * dir.z;
float energy = balls.items[i].mass / dist;
float factor = energy / total_energy;
color.r += (balls.items[i].color.r * factor);
color.g += (balls.items[i].color.g * factor);
color.b += (balls.items[i].color.b * factor);
}
color.a = 0xff;
if (grid.config.mapping == MAPPING_PLANAR)
{
v.text_coord.x = (v.normal.x / 2.0) + 0.5;
v.text_coord.y = -(v.normal.y / 2.0) + 0.5;
}
else
{
const float pi = 3.14159265;
float theta = atan2(v.normal.x, v.normal.z);
float phi = acos(v.normal.y);
v.text_coord.x = (theta + pi) / (2.0 * pi);
v.text_coord.y = (phi / pi);
}
v.color = color;
return v;
}
static Voxel compute_voxel(Index3d grid_pos, Grid grid)
{
assert(grid_pos.x < grid.count.x - 1);
assert(grid_pos.y < grid.count.y - 1);
assert(grid_pos.z < grid.count.z - 1);
Voxel voxel = {0};
voxel.bits = 0;
for (size_t c = 0; c < 8; ++c)
{
Index3d pos = {
.x = grid_pos.x + marching_cube_vertices[c][0],
.y = grid_pos.y + marching_cube_vertices[c][1],
.z = grid_pos.z + marching_cube_vertices[c][2]};
size_t index = compute_grid_point_index(pos, grid);
voxel.corners[c].grid_pos = pos;
voxel.corners[c].grid_index = index;
voxel.corners[c].energy = grid.nodes.items[index].energy;
voxel.bits |= voxel.corners[c].energy > grid.config.threshold ? (1 << c) : 0;
}
voxel.neighbors = marching_cube_neighbors[voxel.bits];
return voxel;
}
static Vector3d compute_edge_point(char edge, Vector3d orig, Voxel *voxel, Grid grid)
{
Vector3d computedPoint;
char index0 = marching_cube_edges[edge][0];
char index1 = marching_cube_edges[edge][1];
// This table indicates what axis variations have the second index of marching_cube_edges
size_t coords[12] = {MB_COORD_X, MB_COORD_Z, MB_COORD_X, MB_COORD_Z,
MB_COORD_X, MB_COORD_Z, MB_COORD_X, MB_COORD_Z,
MB_COORD_Y, MB_COORD_Y, MB_COORD_Y, MB_COORD_Y};
size_t coord = coords[edge];
if (!grid.nodes.items[voxel->corners[index0].grid_index].cache[coord].dirty)
{
computedPoint = grid.nodes.items[voxel->corners[index0].grid_index].cache[coord].value;
}
else
{
float t = (grid.config.threshold - voxel->corners[index0].energy) / (voxel->corners[index1].energy - voxel->corners[index0].energy);
float field_value[MB_COORD_COUNT];
for (size_t i = 0; i < MB_COORD_COUNT; ++i)
{
float field_intensity_0 = marching_cube_vertices[index0][i];
float field_intensity_1 = marching_cube_vertices[index1][i];
field_value[i] = field_intensity_0 + (field_intensity_1 - field_intensity_0) * t;
}
computedPoint = (Vector3d){
orig.x + field_value[MB_COORD_X] * grid.size.x,
orig.y + field_value[MB_COORD_Y] * grid.size.y,
orig.z + field_value[MB_COORD_Z] * grid.size.z};
grid.nodes.items[voxel->corners[index0].grid_index].cache[coord].value = computedPoint;
grid.nodes.items[voxel->corners[index0].grid_index].cache[coord].dirty = false;
}
return computedPoint;
}
static void compute_voxel_triangles(Voxel *voxel, Grid *grid, BallArray balls)
{
// This table indicates what axis variations have the second index of marching_cube_edges
size_t coords[12] = {MB_COORD_X, MB_COORD_Z, MB_COORD_X, MB_COORD_Z,
MB_COORD_X, MB_COORD_Z, MB_COORD_X, MB_COORD_Z,
MB_COORD_Y, MB_COORD_Y, MB_COORD_Y, MB_COORD_Y};
Index3d grid_pos = voxel->corners[0].grid_pos;
Vector3d world_pos = compute_grid_point_coordinates(grid_pos, *grid);
Vector3d vertices[3];
size_t i = 0;
for (size_t t = 0; marching_cube_triangles[voxel->bits][t] != -1; t += 3)
{
Vector3d p1 = compute_edge_point(marching_cube_triangles[voxel->bits][t + 0], world_pos, voxel, *grid);
Vector3d p2 = compute_edge_point(marching_cube_triangles[voxel->bits][t + 1], world_pos, voxel, *grid);
Vector3d p3 = compute_edge_point(marching_cube_triangles[voxel->bits][t + 2], world_pos, voxel, *grid);
Triangle triangle;
triangle.vertex1 = compute_vertex(p1, *grid, balls);
triangle.vertex2 = compute_vertex(p2, *grid, balls);
triangle.vertex3 = compute_vertex(p3, *grid, balls);
mesh_add_triangle(&(grid->mesh), triangle);
}
}
static void compute_energy_field(BallArray balls, Grid grid)
{
size_t x, y, z;
for (x = 0; x < grid.count.x; x++)
{
for (y = 0; y < grid.count.y; y++)
{
for (z = 0; z < grid.count.z; z++)
{
Index3d grid_pos = {.x = x, .y = y, .z = z};
Vector3d world_pos = compute_grid_point_coordinates(grid_pos, grid);
float energy = compute_energy(world_pos, balls);
set_energy_point(grid_pos, grid, energy);
}
}
}
}
static void queue_voxel_push(Index3d pos, Grid *grid)
{
assert(grid->queue.count < grid->queue.capacity);
assert(pos.x <= grid->count.x - 1);
assert(pos.y <= grid->count.y - 1);
assert(pos.z <= grid->count.z - 1);
size_t index = compute_grid_point_index(pos, *grid);
if (grid->nodes.items[index].visited == false)
{
grid->nodes.items[index].visited = true;
grid->queue.items[grid->queue.count++] = pos;
}
}
static Index3d queue_voxel_pop(Grid *grid)
{
assert(grid->queue.count >= 0);
return grid->queue.items[--grid->queue.count];
}
static bool queue_voxel_empty(Grid *grid)
{
return (grid->queue.count == 0);
}
static void queue_voxel_push_neighbors(Voxel voxel, Grid *grid)
{
Index3d origin = voxel.corners[0].grid_pos;
if ((voxel.neighbors & 1) != 0 && origin.x + 1 < grid->count.x - 1)
queue_voxel_push((Index3d){origin.x + 1, origin.y + 0, origin.z + 0}, grid);
if ((voxel.neighbors & 2) != 0 && origin.x >= 1)
queue_voxel_push((Index3d){origin.x - 1, origin.y + 0, origin.z + 0}, grid);
if ((voxel.neighbors & 4) != 0 && origin.y + 1 < grid->count.y - 1)
queue_voxel_push((Index3d){origin.x + 0, origin.y + 1, origin.z + 0}, grid);
if ((voxel.neighbors & 8) != 0 && origin.y >= 1)
queue_voxel_push((Index3d){origin.x + 0, origin.y - 1, origin.z + 0}, grid);
if ((voxel.neighbors & 16) != 0 && origin.z + 1 < grid->count.z - 1)
queue_voxel_push((Index3d){origin.x + 0, origin.y + 0, origin.z + 1}, grid);
if ((voxel.neighbors & 32) != 0 && origin.z >= 1)
queue_voxel_push((Index3d){origin.x + 0, origin.y + 0, origin.z - 1}, grid);
}
#define SEARCH_FIELD_LIMIT_LOOP(COMPARE, UPDATE) \
{ \
Index3d pos = orig; \
while (COMPARE) \
{ \
Voxel voxel = compute_voxel(pos, grid); \
if (voxel.bits == 0 || voxel.bits == 0xFF) \
{ \
UPDATE; \
} \
else \
{ \
*found = pos; \
return true; \
} \
} \
}
static bool search_field_limit(Index3d orig, Index3d *found, Grid grid)
{
if (orig.x >= grid.count.x - 1 ||
orig.y >= grid.count.y - 1 ||
orig.z >= grid.count.z - 1)
{
return false;
}
// pos component are size_t, can't be negative
SEARCH_FIELD_LIMIT_LOOP(pos.x < grid.count.x - 1, pos.x--);
SEARCH_FIELD_LIMIT_LOOP(pos.x < grid.count.x - 1, pos.x++);
SEARCH_FIELD_LIMIT_LOOP(pos.y < grid.count.y - 1, pos.y--);
SEARCH_FIELD_LIMIT_LOOP(pos.y < grid.count.y - 1, pos.y++);
SEARCH_FIELD_LIMIT_LOOP(pos.z < grid.count.z - 1, pos.z--);
SEARCH_FIELD_LIMIT_LOOP(pos.z < grid.count.z - 1, pos.z++);
return false;
}
// --
void metaball_generate_mesh(Grid *grid, BallArray balls)
{
grid->mesh.triangleCount = 0;
grid->mesh.vertexCount = 0;
compute_energy_field(balls, *grid);
for (size_t i = 0; i < balls.count; i++)
{
Vector3d pos = balls.items[i].pos;
Index3d pos_voxel = compute_grid_index(pos, *grid);
if (!search_field_limit(pos_voxel, &pos_voxel, *grid))
{
continue;
}
queue_voxel_push(pos_voxel, grid);
while (!queue_voxel_empty(grid))
{
pos_voxel = queue_voxel_pop(grid);
Voxel voxel = compute_voxel(pos_voxel, *grid);
compute_voxel_triangles(&voxel, grid, balls);
queue_voxel_push_neighbors(voxel, grid);
}
}
}
Grid metaball_grid_create(Index3d count, Vector3d size, Vector3d pos)
{
Grid grid;
Vector3d center = {
.x = pos.x + ((count.x - 1.0f) / 2.0f) * size.x,
.y = pos.y + ((count.y - 1.0f) / 2.0f) * size.y,
.z = pos.z + ((count.z - 1.0f) / 2.0f) * size.z};
grid.count = count;
grid.size = size;
grid.pos = pos;
grid.center = center;
grid.nodes = MB_CREATE_ARRAY(Node, grid.count.x * grid.count.y * grid.count.z);
grid.queue = MB_CREATE_ARRAY(Index3d, grid.count.x * grid.count.y * grid.count.z);
grid.mesh = mesh_create(grid);
grid.config.threshold = 0.3;
grid.config.wired = true;
grid.config.mapping = MAPPING_SPHERIC;
return grid;
}
void metaball_grid_destroy(Grid *grid)
{
grid->count = (Index3d){.x = 0, .y = 0, .z = 0};
grid->size = (Vector3d){.x = 0, .y = 0, .z = 0};
grid->pos = (Vector3d){.x = 0, .y = 0, .z = 0};
grid->center = (Vector3d){.x = 0, .y = 0, .z = 0};
MB_DESTROY_ARRAY(grid->nodes);
MB_DESTROY_ARRAY(grid->queue);
mesh_destroy(&(grid->mesh));
}
//=============================================================================
//==============================================================================
// +----------+
// /|7 /|6
// / | / |
// +----------+ |
// 4| | |5 |
// | | | |
// | | | |
// | +-------|--+
// | / 3 | / 2
// |/ |/
// +----------+
// 0 1
// +----------+
// 7/| 6 /|
// / | /5|
// +----------+ |
// | | 4 | |11
// |10| | |
// 8| | |9 |
// | +-------|--+
// | / 2 | /
// |/3 |/1
// +----------+
// 0
//==============================================================================
//==============================================================================
// Cube neighbors
//
// bit 0 : x + 1
// bit 1 : x - 1
// bit 2 : y + 1
// bit 3 : y - 1
// bit 4 : z + 1
// bit 5 : z - 1
static char marching_cube_neighbors[256] =
{
0, 42, 41, 43, 25, 59, 57, 59, 26, 58, 59, 59, 27, 59, 59, 51,
38, 46, 47, 47, 63, 63, 63, 63, 62, 62, 63, 63, 63, 63, 63, 55,
37, 47, 45, 47, 61, 63, 61, 63, 63, 63, 63, 63, 63, 63, 63, 55,
39, 47, 47, 15, 63, 63, 63, 31, 63, 63, 63, 31, 63, 63, 63, 23,
21, 63, 61, 63, 29, 63, 61, 63, 31, 63, 63, 63, 31, 63, 63, 55,
55, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 55,
53, 63, 61, 63, 61, 63, 60, 62, 63, 63, 63, 63, 63, 63, 62, 54,
55, 63, 63, 31, 63, 63, 62, 30, 63, 63, 63, 31, 63, 63, 62, 22,
22, 62, 63, 63, 31, 63, 63, 63, 30, 62, 63, 63, 31, 63, 63, 55,
54, 62, 63, 63, 63, 63, 63, 63, 62, 60, 63, 61, 63, 61, 63, 53,
55, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 55,
55, 63, 63, 31, 63, 63, 63, 31, 63, 61, 63, 29, 63, 61, 63, 21,
23, 63, 63, 63, 31, 63, 63, 63, 31, 63, 63, 63, 15, 47, 47, 39,
55, 63, 63, 63, 63, 63, 63, 63, 63, 61, 63, 61, 47, 45, 47, 37,
55, 63, 63, 63, 63, 63, 62, 62, 63, 63, 63, 63, 47, 47, 46, 38,
51, 59, 59, 27, 59, 59, 58, 26, 59, 57, 59, 25, 43, 41, 42, 0};
// Cube vertices
static int marching_cube_vertices[8][3] =
{
{0, 0, 0},
{1, 0, 0},
{1, 0, 1},
{0, 0, 1},
{0, 1, 0},
{1, 1, 0},
{1, 1, 1},
{0, 1, 1}};
// This is the edges and the direction on them. They are designed so
// that edges of neighboring cubes are in the same direction.
static char marching_cube_edges[12][2] =
{
{0, 1}, {1, 2}, {3, 2}, {0, 3}, {4, 5}, {5, 6}, {7, 6}, {4, 7}, {0, 4}, {1, 5}, {3, 7}, {2, 6}};
// This list gives the edges that the triangles in each case intersect.
static char marching_cube_triangles[256][16] =
{
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 0
{3, 0, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1
{9, 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 2
{3, 1, 8, 1, 9, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 3
{11, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 4
{3, 0, 8, 11, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 5
{11, 9, 2, 9, 0, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 6
{3, 2, 8, 8, 2, 11, 8, 11, 9, -1, -1, -1, -1, -1, -1, -1}, // 7
{2, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 8
{2, 0, 10, 0, 8, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 9
{0, 1, 9, 10, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 10
{2, 1, 10, 10, 1, 9, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1}, // 11
{1, 3, 11, 3, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 12
{1, 0, 11, 11, 0, 8, 11, 8, 10, -1, -1, -1, -1, -1, -1, -1}, // 13
{0, 3, 9, 9, 3, 10, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1}, // 14
{11, 9, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 15
{8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 16
{0, 4, 3, 4, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 17
{9, 0, 1, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 18
{9, 4, 1, 1, 4, 7, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1}, // 19
{11, 1, 2, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 20
{7, 3, 4, 4, 3, 0, 11, 1, 2, -1, -1, -1, -1, -1, -1, -1}, // 21
{11, 9, 2, 2, 9, 0, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1}, // 22
{9, 2, 11, 7, 2, 9, 3, 2, 7, 4, 7, 9, -1, -1, -1, -1}, // 23
{7, 8, 4, 2, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 24
{7, 10, 4, 4, 10, 2, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1}, // 25
{1, 9, 0, 7, 8, 4, 10, 2, 3, -1, -1, -1, -1, -1, -1, -1}, // 26
{10, 4, 7, 10, 9, 4, 2, 9, 10, 1, 9, 2, -1, -1, -1, -1}, // 27
{1, 3, 11, 11, 3, 10, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1}, // 28
{11, 1, 10, 10, 1, 4, 4, 1, 0, 4, 7, 10, -1, -1, -1, -1}, // 29
{8, 4, 7, 10, 9, 0, 11, 9, 10, 3, 10, 0, -1, -1, -1, -1}, // 30
{10, 4, 7, 9, 4, 10, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1}, // 31
{4, 9, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 32
{4, 9, 5, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 33
{4, 0, 5, 0, 1, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 34
{4, 8, 5, 5, 8, 3, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1}, // 35
{11, 1, 2, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 36
{8, 3, 0, 11, 1, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1}, // 37
{11, 5, 2, 2, 5, 4, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1}, // 38
{5, 2, 11, 5, 3, 2, 4, 3, 5, 8, 3, 4, -1, -1, -1, -1}, // 39
{4, 9, 5, 10, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 40
{2, 0, 10, 10, 0, 8, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1}, // 41
{4, 0, 5, 5, 0, 1, 10, 2, 3, -1, -1, -1, -1, -1, -1, -1}, // 42
{5, 2, 1, 8, 2, 5, 10, 2, 8, 5, 4, 8, -1, -1, -1, -1}, // 43
{10, 11, 3, 3, 11, 1, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1}, // 44
{5, 4, 9, 1, 0, 8, 1, 8, 11, 11, 8, 10, -1, -1, -1, -1}, // 45
{0, 5, 4, 10, 5, 0, 11, 5, 10, 3, 10, 0, -1, -1, -1, -1}, // 46
{8, 5, 4, 11, 5, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1}, // 47
{8, 9, 7, 9, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 48
{0, 9, 3, 3, 9, 5, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1}, // 49
{8, 0, 7, 7, 0, 1, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1}, // 50
{3, 1, 5, 7, 3, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 51
{8, 9, 7, 7, 9, 5, 2, 11, 1, -1, -1, -1, -1, -1, -1, -1}, // 52
{2, 11, 1, 0, 9, 5, 0, 5, 3, 3, 5, 7, -1, -1, -1, -1}, // 53
{2, 8, 0, 5, 8, 2, 7, 8, 5, 2, 11, 5, -1, -1, -1, -1}, // 54
{5, 2, 11, 3, 2, 5, 7, 3, 5, -1, -1, -1, -1, -1, -1, -1}, // 55
{5, 7, 9, 9, 7, 8, 2, 3, 10, -1, -1, -1, -1, -1, -1, -1}, // 56
{7, 9, 5, 2, 9, 7, 0, 9, 2, 10, 2, 7, -1, -1, -1, -1}, // 57
{10, 2, 3, 8, 0, 1, 8, 1, 7, 7, 1, 5, -1, -1, -1, -1}, // 58
{1, 10, 2, 7, 10, 1, 5, 7, 1, -1, -1, -1, -1, -1, -1, -1}, // 59
{8, 9, 5, 7, 8, 5, 3, 11, 1, 10, 11, 3, -1, -1, -1, -1}, // 60
{0, 5, 7, 9, 5, 0, 0, 7, 10, 11, 1, 0, 0, 10, 11, -1}, // 61
{0, 10, 11, 3, 10, 0, 0, 11, 5, 7, 8, 0, 0, 5, 7, -1}, // 62
{5, 10, 11, 5, 7, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 63
{5, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 64
{3, 0, 8, 6, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 65
{1, 9, 0, 6, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 66
{3, 1, 8, 8, 1, 9, 6, 5, 11, -1, -1, -1, -1, -1, -1, -1}, // 67
{5, 1, 6, 1, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 68
{5, 1, 6, 6, 1, 2, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1}, // 69
{5, 9, 6, 6, 9, 0, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1}, // 70
{8, 5, 9, 2, 5, 8, 6, 5, 2, 8, 3, 2, -1, -1, -1, -1}, // 71
{10, 2, 3, 5, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 72
{8, 10, 0, 0, 10, 2, 5, 11, 6, -1, -1, -1, -1, -1, -1, -1}, // 73
{9, 0, 1, 10, 2, 3, 6, 5, 11, -1, -1, -1, -1, -1, -1, -1}, // 74
{6, 5, 11, 2, 1, 9, 2, 9, 10, 10, 9, 8, -1, -1, -1, -1}, // 75
{10, 6, 3, 3, 6, 5, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1}, // 76
{10, 0, 8, 5, 0, 10, 1, 0, 5, 6, 5, 10, -1, -1, -1, -1}, // 77
{6, 3, 10, 6, 0, 3, 5, 0, 6, 9, 0, 5, -1, -1, -1, -1}, // 78
{9, 6, 5, 10, 6, 9, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1}, // 79
{6, 5, 11, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 80
{0, 4, 3, 3, 4, 7, 11, 6, 5, -1, -1, -1, -1, -1, -1, -1}, // 81
{0, 1, 9, 6, 5, 11, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1}, // 82
{5, 11, 6, 7, 1, 9, 3, 1, 7, 4, 7, 9, -1, -1, -1, -1}, // 83
{2, 6, 1, 1, 6, 5, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1}, // 84
{5, 1, 2, 6, 5, 2, 4, 3, 0, 7, 3, 4, -1, -1, -1, -1}, // 85
{7, 8, 4, 5, 9, 0, 5, 0, 6, 6, 0, 2, -1, -1, -1, -1}, // 86
{9, 7, 3, 4, 7, 9, 9, 3, 2, 6, 5, 9, 9, 2, 6, -1}, // 87
{2, 3, 10, 4, 7, 8, 5, 11, 6, -1, -1, -1, -1, -1, -1, -1}, // 88
{6, 5, 11, 2, 4, 7, 0, 4, 2, 10, 2, 7, -1, -1, -1, -1}, // 89
{9, 0, 1, 8, 4, 7, 10, 2, 3, 6, 5, 11, -1, -1, -1, -1}, // 90
{1, 9, 2, 2, 9, 10, 10, 9, 4, 4, 7, 10, 6, 5, 11, -1}, // 91
{7, 8, 4, 5, 3, 10, 1, 3, 5, 6, 5, 10, -1, -1, -1, -1}, // 92
{10, 5, 1, 6, 5, 10, 10, 1, 0, 4, 7, 10, 10, 0, 4, -1}, // 93
{9, 0, 5, 5, 0, 6, 6, 0, 3, 3, 10, 6, 7, 8, 4, -1}, // 94
{9, 6, 5, 10, 6, 9, 9, 4, 7, 9, 7, 10, -1, -1, -1, -1}, // 95
{9, 11, 4, 11, 6, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 96
{6, 4, 11, 11, 4, 9, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1}, // 97
{1, 11, 0, 0, 11, 6, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1}, // 98
{1, 8, 3, 6, 8, 1, 4, 8, 6, 11, 6, 1, -1, -1, -1, -1}, // 99
{9, 1, 4, 4, 1, 2, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1}, // 100
{8, 3, 0, 9, 1, 2, 9, 2, 4, 4, 2, 6, -1, -1, -1, -1}, // 101
{4, 0, 2, 6, 4, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 102
{2, 8, 3, 4, 8, 2, 6, 4, 2, -1, -1, -1, -1, -1, -1, -1}, // 103
{9, 11, 4, 4, 11, 6, 3, 10, 2, -1, -1, -1, -1, -1, -1, -1}, // 104
{2, 0, 8, 10, 2, 8, 11, 4, 9, 6, 4, 11, -1, -1, -1, -1}, // 105
{2, 3, 10, 6, 0, 1, 4, 0, 6, 11, 6, 1, -1, -1, -1, -1}, // 106
{1, 6, 4, 11, 6, 1, 1, 4, 8, 10, 2, 1, 1, 8, 10, -1}, // 107
{4, 9, 6, 6, 9, 3, 3, 9, 1, 3, 10, 6, -1, -1, -1, -1}, // 108
{1, 8, 10, 0, 8, 1, 1, 10, 6, 4, 9, 1, 1, 6, 4, -1}, // 109
{6, 3, 10, 0, 3, 6, 4, 0, 6, -1, -1, -1, -1, -1, -1, -1}, // 110
{8, 6, 4, 8, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 111
{6, 7, 11, 11, 7, 8, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1}, // 112
{3, 0, 7, 7, 0, 11, 11, 0, 9, 11, 6, 7, -1, -1, -1, -1}, // 113
{7, 11, 6, 7, 1, 11, 8, 1, 7, 0, 1, 8, -1, -1, -1, -1}, // 114
{7, 11, 6, 1, 11, 7, 3, 1, 7, -1, -1, -1, -1, -1, -1, -1}, // 115
{6, 1, 2, 8, 1, 6, 9, 1, 8, 7, 8, 6, -1, -1, -1, -1}, // 116
{9, 2, 6, 1, 2, 9, 9, 6, 7, 3, 0, 9, 9, 7, 3, -1}, // 117
{0, 7, 8, 6, 7, 0, 2, 6, 0, -1, -1, -1, -1, -1, -1, -1}, // 118
{2, 7, 3, 2, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 119
{10, 2, 3, 8, 11, 6, 9, 11, 8, 7, 8, 6, -1, -1, -1, -1}, // 120
{7, 2, 0, 10, 2, 7, 7, 0, 9, 11, 6, 7, 7, 9, 11, -1}, // 121
{0, 1, 8, 8, 1, 7, 7, 1, 11, 11, 6, 7, 10, 2, 3, -1}, // 122
{1, 10, 2, 7, 10, 1, 1, 11, 6, 1, 6, 7, -1, -1, -1, -1}, // 123
{6, 8, 9, 7, 8, 6, 6, 9, 1, 3, 10, 6, 6, 1, 3, -1}, // 124
{1, 0, 9, 7, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 125
{0, 7, 8, 6, 7, 0, 0, 3, 10, 0, 10, 6, -1, -1, -1, -1}, // 126
{6, 7, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 127
{10, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 128
{8, 3, 0, 6, 10, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 129
{9, 0, 1, 6, 10, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 130
{9, 8, 1, 1, 8, 3, 6, 10, 7, -1, -1, -1, -1, -1, -1, -1}, // 131
{2, 11, 1, 7, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 132
{11, 1, 2, 8, 3, 0, 7, 6, 10, -1, -1, -1, -1, -1, -1, -1}, // 133
{0, 2, 9, 9, 2, 11, 7, 6, 10, -1, -1, -1, -1, -1, -1, -1}, // 134
{7, 6, 10, 3, 2, 11, 3, 11, 8, 8, 11, 9, -1, -1, -1, -1}, // 135
{3, 7, 2, 7, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 136
{8, 7, 0, 0, 7, 6, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1}, // 137
{6, 2, 7, 7, 2, 3, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1}, // 138
{2, 1, 6, 6, 1, 8, 8, 1, 9, 6, 8, 7, -1, -1, -1, -1}, // 139
{6, 11, 7, 7, 11, 1, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1}, // 140
{6, 11, 7, 11, 1, 7, 7, 1, 8, 8, 1, 0, -1, -1, -1, -1}, // 141
{7, 0, 3, 11, 0, 7, 9, 0, 11, 7, 6, 11, -1, -1, -1, -1}, // 142
{11, 7, 6, 8, 7, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1}, // 143
{4, 6, 8, 6, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 144
{10, 3, 6, 6, 3, 0, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1}, // 145
{10, 8, 6, 6, 8, 4, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1}, // 146
{6, 9, 4, 3, 9, 6, 1, 9, 3, 6, 10, 3, -1, -1, -1, -1}, // 147
{4, 6, 8, 8, 6, 10, 1, 2, 11, -1, -1, -1, -1, -1, -1, -1}, // 148
{11, 1, 2, 10, 3, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1}, // 149
{8, 4, 10, 10, 4, 6, 9, 0, 2, 9, 2, 11, -1, -1, -1, -1}, // 150
{3, 11, 9, 2, 11, 3, 3, 9, 4, 6, 10, 3, 3, 4, 6, -1}, // 151
{3, 8, 2, 2, 8, 4, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1}, // 152
{2, 0, 4, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 153
{0, 1, 9, 4, 2, 3, 6, 2, 4, 8, 4, 3, -1, -1, -1, -1}, // 154
{4, 1, 9, 2, 1, 4, 6, 2, 4, -1, -1, -1, -1, -1, -1, -1}, // 155
{3, 8, 1, 1, 8, 6, 6, 8, 4, 1, 6, 11, -1, -1, -1, -1}, // 156
{0, 11, 1, 6, 11, 0, 4, 6, 0, -1, -1, -1, -1, -1, -1, -1}, // 157
{3, 4, 6, 8, 4, 3, 3, 6, 11, 9, 0, 3, 3, 11, 9, -1}, // 158
{4, 11, 9, 4, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 159
{5, 4, 9, 10, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 160
{3, 0, 8, 5, 4, 9, 6, 10, 7, -1, -1, -1, -1, -1, -1, -1}, // 161
{1, 5, 0, 0, 5, 4, 10, 7, 6, -1, -1, -1, -1, -1, -1, -1}, // 162
{6, 10, 7, 4, 8, 3, 4, 3, 5, 5, 3, 1, -1, -1, -1, -1}, // 163
{4, 9, 5, 2, 11, 1, 10, 7, 6, -1, -1, -1, -1, -1, -1, -1}, // 164
{7, 6, 10, 11, 1, 2, 3, 0, 8, 5, 4, 9, -1, -1, -1, -1}, // 165
{10, 7, 6, 11, 5, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1}, // 166