forked from prideout/par
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpar_msquares.h
2156 lines (2008 loc) · 79.8 KB
/
par_msquares.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
// MSQUARES :: https://github.com/prideout/par
// Converts fp32 grayscale images, or 8-bit color images, into triangles.
//
// THIS IS EXPERIMENTAL CODE, DO NOT USE IN PRODUCTION
//
// Note that a potentially more interesting project for converting bitmaps
// into vectors can be found at https://github.com/BlockoS/blob, which is an
// implementation of "A linear-time component-labeling algorithm using contour
// tracing technique" by Fu Chang, Chun-Jen Chen, and Chi-Jen Lu. I recommend
// using that in combination with a simple ear-clipping algorithm for triangle
// tessellation. (see https://prideout.net/polygon.js)
//
// For grayscale images, a threshold is specified to determine insideness.
// For color images, an exact color is specified to determine insideness.
// Color images can be r8, rg16, rgb24, or rgba32. For a visual overview of
// the API and all the flags, see:
//
// https://prideout.net/marching-squares
//
// Distributed under the MIT License, see bottom of file.
#ifndef PAR_MSQUARES_H
#define PAR_MSQUARES_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
// -----------------------------------------------------------------------------
// BEGIN PUBLIC API
// -----------------------------------------------------------------------------
#ifndef PAR_MSQUARES_T
#define PAR_MSQUARES_T uint16_t
#endif
typedef uint8_t par_byte;
typedef struct par_msquares_meshlist_s par_msquares_meshlist;
// Results of a marching squares operation. Triangles are counter-clockwise.
typedef struct {
float* points; // pointer to XY (or XYZ) vertex coordinates
int npoints; // number of vertex coordinates
PAR_MSQUARES_T* triangles; // pointer to 3-tuples of vertex indices
int ntriangles; // number of 3-tuples
int dim; // number of floats per point (either 2 or 3)
uint32_t color; // used only with par_msquares_color_multi
} par_msquares_mesh;
// Polyline boundary extracted from a mesh, composed of one or more chains.
// Counterclockwise chains are solid, clockwise chains are holes. So, when
// serializing to SVG, all chains can be aggregated in a single <path>,
// provided they each terminate with a "Z" and use the default fill rule.
typedef struct {
float* points; // list of XY vertex coordinates
int npoints; // number of vertex coordinates
float** chains; // list of pointers to the start of each chain
PAR_MSQUARES_T* lengths; // list of chain lengths
int nchains; // number of chains
} par_msquares_boundary;
// Reverses the "insideness" test.
#define PAR_MSQUARES_INVERT (1 << 0)
// Returns a meshlist with two meshes: one for the inside, one for the outside.
#define PAR_MSQUARES_DUAL (1 << 1)
// Requests that returned meshes have 3-tuple coordinates instead of 2-tuples.
// When using a color-based function, the Z coordinate represents the alpha
// value of the nearest pixel.
#define PAR_MSQUARES_HEIGHTS (1 << 2)
// Applies a step function to the Z coordinates. Requires HEIGHTS and DUAL.
#define PAR_MSQUARES_SNAP (1 << 3)
// Adds extrusion triangles to each mesh other than the lowest mesh. Requires
// the PAR_MSQUARES_HEIGHTS flag to be present.
#define PAR_MSQUARES_CONNECT (1 << 4)
// Enables quick & dirty (not best) simpification of the returned mesh.
#define PAR_MSQUARES_SIMPLIFY (1 << 5)
// Indicates that the "color" argument is ABGR instead of ARGB.
#define PAR_MSQUARES_SWIZZLE (1 << 6)
// Ensures there are no T-junction vertices. (par_msquares_color_multi only)
// Requires the PAR_MSQUARES_SIMPLIFY flag to be disabled.
#define PAR_MSQUARES_CLEAN (1 << 7)
par_msquares_meshlist* par_msquares_grayscale(float const* data, int width,
int height, int cellsize, float threshold, int flags);
par_msquares_meshlist* par_msquares_color(par_byte const* data, int width,
int height, int cellsize, uint32_t color, int bpp, int flags);
par_msquares_mesh const* par_msquares_get_mesh(par_msquares_meshlist*, int n);
int par_msquares_get_count(par_msquares_meshlist*);
void par_msquares_free(par_msquares_meshlist*);
void par_msquares_free_boundary(par_msquares_boundary*);
typedef int (*par_msquares_inside_fn)(int, void*);
typedef float (*par_msquares_height_fn)(float, float, void*);
par_msquares_meshlist* par_msquares_function(int width, int height,
int cellsize, int flags, void* context, par_msquares_inside_fn insidefn,
par_msquares_height_fn heightfn);
par_msquares_meshlist* par_msquares_grayscale_multi(float const* data,
int width, int height, int cellsize, float const* thresholds,
int nthresholds, int flags);
par_msquares_meshlist* par_msquares_color_multi(par_byte const* data, int width,
int height, int cellsize, int bpp, int flags);
par_msquares_boundary* par_msquares_extract_boundary(par_msquares_mesh const* );
#ifndef PAR_PI
#define PAR_PI (3.14159265359)
#define PAR_MIN(a, b) (a > b ? b : a)
#define PAR_MAX(a, b) (a > b ? a : b)
#define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
#define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
#define PAR_SQR(a) ((a) * (a))
#endif
#ifndef PAR_MALLOC
#define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
#define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
#define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
#define PAR_FREE(BUF) free(BUF)
#endif
#ifdef __cplusplus
}
#endif
// -----------------------------------------------------------------------------
// END PUBLIC API
// -----------------------------------------------------------------------------
#ifdef PAR_MSQUARES_IMPLEMENTATION
#include <stdlib.h>
#include <assert.h>
#include <float.h>
#include <string.h>
typedef struct {
PAR_MSQUARES_T* values;
size_t count;
size_t capacity;
} par__uint16list;
typedef struct {
float* points;
int npoints;
PAR_MSQUARES_T* triangles;
int ntriangles;
int dim;
uint32_t color;
int nconntriangles;
PAR_MSQUARES_T* conntri;
par__uint16list* tjunctions;
} par_msquares__mesh;
struct par_msquares_meshlist_s {
int nmeshes;
par_msquares__mesh** meshes;
};
static int** par_msquares_binary_point_table = 0;
static int** par_msquares_binary_triangle_table = 0;
static int* par_msquares_quaternary_triangle_table[64][4];
static int* par_msquares_quaternary_boundary_table[64][4];
static par_msquares_meshlist* par_msquares__merge(par_msquares_meshlist** lists,
int count, int snap);
static void par_init_tables()
{
char const* BINARY_TABLE =
"0"
"1017"
"1123"
"2023370"
"1756"
"2015560"
"2123756"
"3023035056"
"1345"
"4013034045057"
"2124451"
"3024045057"
"2734467"
"3013034046"
"3124146167"
"2024460";
char const* binary_token = BINARY_TABLE;
par_msquares_binary_point_table = PAR_CALLOC(int*, 16);
par_msquares_binary_triangle_table = PAR_CALLOC(int*, 16);
for (int i = 0; i < 16; i++) {
int ntris = *binary_token - '0';
binary_token++;
par_msquares_binary_triangle_table[i] =
PAR_CALLOC(int, (ntris + 1) * 3);
int* sqrtris = par_msquares_binary_triangle_table[i];
sqrtris[0] = ntris;
int mask = 0;
int* sqrpts = par_msquares_binary_point_table[i] = PAR_CALLOC(int, 7);
sqrpts[0] = 0;
for (int j = 0; j < ntris * 3; j++, binary_token++) {
int midp = *binary_token - '0';
int bit = 1 << midp;
if (!(mask & bit)) {
mask |= bit;
sqrpts[++sqrpts[0]] = midp;
}
sqrtris[j + 1] = midp;
}
}
char const* QUATERNARY_TABLE =
"2024046000"
"3346360301112300"
"3346360301112300"
"3346360301112300"
"3560502523013450"
"2015056212414500"
"4018087785756212313828348450"
"4018087785756212313828348450"
"3560502523013450"
"4018087785756212313828348450"
"2015056212414500"
"4018087785756212313828348450"
"3560502523013450"
"4018087785756212313828348450"
"4018087785756212313828348450"
"2015056212414500"
"3702724745001756"
"2018087212313828348452785756"
"4013034045057112301756"
"4013034045057112301756"
"2023037027347460"
"1701312414616700"
"2018087212313847857568348450"
"2018087212313847857568348450"
"4018087123138028348452785756"
"1701467161262363513450"
"2018087412313883484502785756"
"2018087212313828348452785756"
"4018087123138028348452785756"
"1701467161262363513450"
"2018087212313828348452785756"
"2018087412313883484502785756"
"3702724745001756"
"4013034045057112301756"
"2018087212313828348452785756"
"4013034045057112301756"
"4018087123138028348452785756"
"2018087412313883484502785756"
"1701467161262363513450"
"2018087212313828348452785756"
"2023037027347460"
"2018087212313847857568348450"
"1701312414616700"
"2018087212313847857568348450"
"4018087123138028348452785756"
"2018087212313828348452785756"
"1701467161262363513450"
"2018087412313883484502785756"
"3702724745001756"
"4013034045057112301756"
"4013034045057112301756"
"2018087212313828348452785756"
"4018087123138028348452785756"
"2018087412313883484502785756"
"2018087212313828348452785756"
"1701467161262363513450"
"4018087123138028348452785756"
"2018087212313828348452785756"
"2018087412313883484502785756"
"1701467161262363513450"
"2023037027347460"
"2018087212313847857568348450"
"2018087212313847857568348450"
"1701312414616700";
char const* quaternary_token = QUATERNARY_TABLE;
int* quaternary_values = PAR_CALLOC(int, strlen(QUATERNARY_TABLE));
int* vals = quaternary_values;
for (int i = 0; i < 64; i++) {
int ntris = *quaternary_token++ - '0';
*vals = ntris;
par_msquares_quaternary_triangle_table[i][0] = vals++;
for (int j = 0; j < ntris * 3; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
ntris = *quaternary_token++ - '0';
*vals = ntris;
par_msquares_quaternary_triangle_table[i][1] = vals++;
for (int j = 0; j < ntris * 3; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
ntris = *quaternary_token++ - '0';
*vals = ntris;
par_msquares_quaternary_triangle_table[i][2] = vals++;
for (int j = 0; j < ntris * 3; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
ntris = *quaternary_token++ - '0';
*vals = ntris;
par_msquares_quaternary_triangle_table[i][3] = vals++;
for (int j = 0; j < ntris * 3; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
}
assert(vals == quaternary_values + strlen(QUATERNARY_TABLE));
char const* QUATERNARY_EDGES =
"0000"
"11313100113131001131310013501530"
"115151002188523881258830218852388125883013501530"
"218852388125883011515100218852388125883013501530"
"218852388125883021885238812588301151510015700175"
"2188723881258832788521357131017521357131017513701730"
"11717100218872388127883021887238812788302388702588327885"
"1172713515302188725881027885218872388125883278852388702588327885"
"11727135153021887238812588327885218872588102788515700175"
"213571310175218872388125883278852135713101752388702588327885"
"21887258810278851172713515302188723881258832788513701730"
"21887238812788301171710021887238812788302388702588327885"
"21887238812588327885117271351530218872588102788515700175"
"213571310175213571310175218872388125883278852388702588327885"
"2188725881027885218872388125883278851172713515302388702588327885"
"21887238812588327885218872588102788511727135153013701730"
"2188723881278830218872388127883011717100";
quaternary_token = QUATERNARY_EDGES;
quaternary_values = PAR_CALLOC(int, strlen(QUATERNARY_EDGES));
vals = quaternary_values;
for (int i = 0; i < 64; i++) {
int nedges = *quaternary_token++ - '0';
*vals = nedges;
par_msquares_quaternary_boundary_table[i][0] = vals++;
for (int j = 0; j < nedges * 2; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
nedges = *quaternary_token++ - '0';
*vals = nedges;
par_msquares_quaternary_boundary_table[i][1] = vals++;
for (int j = 0; j < nedges * 2; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
nedges = *quaternary_token++ - '0';
*vals = nedges;
par_msquares_quaternary_boundary_table[i][2] = vals++;
for (int j = 0; j < nedges * 2; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
nedges = *quaternary_token++ - '0';
*vals = nedges;
par_msquares_quaternary_boundary_table[i][3] = vals++;
for (int j = 0; j < nedges * 2; j++) {
int pt = *quaternary_token++ - '0';
assert(pt >= 0 && pt < 9);
*vals++ = pt;
}
}
assert(vals == quaternary_values + strlen(QUATERNARY_EDGES));
}
typedef struct {
float const* data;
float threshold;
float lower_bound;
float upper_bound;
int width;
int height;
} par_gray_context;
static int gray_inside(int location, void* contextptr)
{
par_gray_context* context = (par_gray_context*) contextptr;
return context->data[location] > context->threshold;
}
static int gray_multi_inside(int location, void* contextptr)
{
par_gray_context* context = (par_gray_context*) contextptr;
float val = context->data[location];
float upper = context->upper_bound;
float lower = context->lower_bound;
return val >= lower && val < upper;
}
static float gray_height(float x, float y, void* contextptr)
{
par_gray_context* context = (par_gray_context*) contextptr;
int i = PAR_CLAMP(context->width * x, 0, context->width - 1);
int j = PAR_CLAMP(context->height * y, 0, context->height - 1);
return context->data[i + j * context->width];
}
typedef struct {
par_byte const* data;
par_byte color[4];
int bpp;
int width;
int height;
} par_color_context;
static int color_inside(int location, void* contextptr)
{
par_color_context* context = (par_color_context*) contextptr;
par_byte const* data = context->data + location * context->bpp;
for (int i = 0; i < context->bpp; i++) {
if (data[i] != context->color[i]) {
return 0;
}
}
return 1;
}
static float color_height(float x, float y, void* contextptr)
{
par_color_context* context = (par_color_context*) contextptr;
assert(context->bpp == 4);
int i = PAR_CLAMP(context->width * x, 0, context->width - 1);
int j = PAR_CLAMP(context->height * y, 0, context->height - 1);
int k = i + j * context->width;
return context->data[k * 4 + 3] / 255.0;
}
par_msquares_meshlist* par_msquares_color(par_byte const* data, int width,
int height, int cellsize, uint32_t color, int bpp, int flags)
{
par_color_context context;
context.bpp = bpp;
if (flags & PAR_MSQUARES_SWIZZLE) {
context.color[0] = (color >> 0) & 0xff;
context.color[1] = (color >> 8) & 0xff;
context.color[2] = (color >> 16) & 0xff;
context.color[3] = (color >> 24) & 0xff;
} else {
context.color[0] = (color >> 16) & 0xff;
context.color[1] = (color >> 8) & 0xff;
context.color[2] = (color >> 0) & 0xff;
context.color[3] = (color >> 24) & 0xff;
}
context.data = data;
context.width = width;
context.height = height;
return par_msquares_function(
width, height, cellsize, flags, &context, color_inside, color_height);
}
par_msquares_meshlist* par_msquares_grayscale(float const* data, int width,
int height, int cellsize, float threshold, int flags)
{
par_gray_context context;
context.width = width;
context.height = height;
context.data = data;
context.threshold = threshold;
return par_msquares_function(
width, height, cellsize, flags, &context, gray_inside, gray_height);
}
par_msquares_meshlist* par_msquares_grayscale_multi(float const* data,
int width, int height, int cellsize, float const* thresholds,
int nthresholds, int flags)
{
par_msquares_meshlist* mlists[2];
mlists[0] = PAR_CALLOC(par_msquares_meshlist, 1);
int connect = flags & PAR_MSQUARES_CONNECT;
int snap = flags & PAR_MSQUARES_SNAP;
int heights = flags & PAR_MSQUARES_HEIGHTS;
if (!heights) {
snap = connect = 0;
}
flags &= ~PAR_MSQUARES_INVERT;
flags &= ~PAR_MSQUARES_DUAL;
flags &= ~PAR_MSQUARES_CONNECT;
flags &= ~PAR_MSQUARES_SNAP;
par_gray_context context;
context.width = width;
context.height = height;
context.data = data;
context.lower_bound = -FLT_MAX;
for (int i = 0; i <= nthresholds; i++) {
int mergeconf = i > 0 ? connect : 0;
if (i == nthresholds) {
context.upper_bound = FLT_MAX;
mergeconf |= snap;
} else {
context.upper_bound = thresholds[i];
}
mlists[1] = par_msquares_function(width, height, cellsize, flags,
&context, gray_multi_inside, gray_height);
mlists[0] = par_msquares__merge(mlists, 2, mergeconf);
context.lower_bound = context.upper_bound;
flags |= connect;
}
return mlists[0];
}
par_msquares_mesh const* par_msquares_get_mesh(
par_msquares_meshlist* mlist, int mindex)
{
assert(mlist && mindex < mlist->nmeshes);
return (par_msquares_mesh const*) mlist->meshes[mindex];
}
int par_msquares_get_count(par_msquares_meshlist* mlist)
{
assert(mlist);
return mlist->nmeshes;
}
void par_msquares_free(par_msquares_meshlist* mlist)
{
if (!mlist) {
return;
}
par_msquares__mesh** meshes = mlist->meshes;
for (int i = 0; i < mlist->nmeshes; i++) {
free(meshes[i]->points);
free(meshes[i]->triangles);
free(meshes[i]);
}
free(meshes);
free(mlist);
}
// Combine multiple meshlists by moving mesh pointers, and optionally applying
// a snap operation that assigns a single Z value across all verts in each
// mesh. The Z value determined by the mesh's position in the final mesh list.
static par_msquares_meshlist* par_msquares__merge(par_msquares_meshlist** lists,
int count, int snap)
{
par_msquares_meshlist* merged = PAR_CALLOC(par_msquares_meshlist, 1);
merged->nmeshes = 0;
for (int i = 0; i < count; i++) {
merged->nmeshes += lists[i]->nmeshes;
}
merged->meshes = PAR_CALLOC(par_msquares__mesh*, merged->nmeshes);
par_msquares__mesh** pmesh = merged->meshes;
for (int i = 0; i < count; i++) {
par_msquares_meshlist* meshlist = lists[i];
for (int j = 0; j < meshlist->nmeshes; j++) {
*pmesh++ = meshlist->meshes[j];
}
free(meshlist);
}
if (!snap) {
return merged;
}
pmesh = merged->meshes;
float zmin = FLT_MAX;
float zmax = -zmin;
for (int i = 0; i < merged->nmeshes; i++, pmesh++) {
float* pzed = (*pmesh)->points + 2;
for (int j = 0; j < (*pmesh)->npoints; j++, pzed += 3) {
zmin = PAR_MIN(*pzed, zmin);
zmax = PAR_MAX(*pzed, zmax);
}
}
float zextent = zmax - zmin;
pmesh = merged->meshes;
for (int i = 0; i < merged->nmeshes; i++, pmesh++) {
float* pzed = (*pmesh)->points + 2;
float zed = zmin + zextent * i / (merged->nmeshes - 1);
for (int j = 0; j < (*pmesh)->npoints; j++, pzed += 3) {
*pzed = zed;
}
}
if (!(snap & PAR_MSQUARES_CONNECT)) {
return merged;
}
for (int i = 1; i < merged->nmeshes; i++) {
par_msquares__mesh* mesh = merged->meshes[i];
// Find all extrusion points. This is tightly coupled to the
// tessellation code, which generates two "connector" triangles for each
// extruded edge. The first two verts of the second triangle are the
// verts that need to be displaced.
char* markers = PAR_CALLOC(char, mesh->npoints);
int tri = mesh->ntriangles - mesh->nconntriangles;
while (tri < mesh->ntriangles) {
markers[mesh->triangles[tri * 3 + 3]] = 1;
markers[mesh->triangles[tri * 3 + 4]] = 1;
tri += 2;
}
// Displace all extrusion points down to the previous level.
float zed = zmin + zextent * (i - 1) / (merged->nmeshes - 1);
float* pzed = mesh->points + 2;
for (int j = 0; j < mesh->npoints; j++, pzed += 3) {
if (markers[j]) {
*pzed = zed;
}
}
free(markers);
}
return merged;
}
static void par_remove_unreferenced_verts(par_msquares__mesh* mesh)
{
if (mesh->npoints == 0) {
return;
}
char* markers = PAR_CALLOC(char, mesh->npoints);
PAR_MSQUARES_T const* ptris = mesh->triangles;
int newnpts = 0;
for (int i = 0; i < mesh->ntriangles * 3; i++, ptris++) {
if (!markers[*ptris]) {
newnpts++;
markers[*ptris] = 1;
}
}
float* newpts = PAR_CALLOC(float, newnpts * mesh->dim);
PAR_MSQUARES_T* mapping = PAR_CALLOC(PAR_MSQUARES_T, mesh->npoints);
float const* ppts = mesh->points;
float* pnewpts = newpts;
int j = 0;
if (mesh->dim == 3) {
for (int i = 0; i < mesh->npoints; i++, ppts += 3) {
if (markers[i]) {
*pnewpts++ = ppts[0];
*pnewpts++ = ppts[1];
*pnewpts++ = ppts[2];
mapping[i] = j++;
}
}
} else {
for (int i = 0; i < mesh->npoints; i++, ppts += 2) {
if (markers[i]) {
*pnewpts++ = ppts[0];
*pnewpts++ = ppts[1];
mapping[i] = j++;
}
}
}
free(mesh->points);
free(markers);
mesh->points = newpts;
mesh->npoints = newnpts;
for (int i = 0; i < mesh->ntriangles * 3; i++) {
mesh->triangles[i] = mapping[mesh->triangles[i]];
}
free(mapping);
}
par_msquares_meshlist* par_msquares_function(int width, int height,
int cellsize, int flags, void* context, par_msquares_inside_fn insidefn,
par_msquares_height_fn heightfn)
{
assert(width > 0 && width % cellsize == 0);
assert(height > 0 && height % cellsize == 0);
if (flags & PAR_MSQUARES_DUAL) {
int connect = flags & PAR_MSQUARES_CONNECT;
int snap = flags & PAR_MSQUARES_SNAP;
int heights = flags & PAR_MSQUARES_HEIGHTS;
if (!heights) {
snap = connect = 0;
}
flags ^= PAR_MSQUARES_INVERT;
flags &= ~PAR_MSQUARES_DUAL;
flags &= ~PAR_MSQUARES_CONNECT;
par_msquares_meshlist* m[2];
m[0] = par_msquares_function(width, height, cellsize, flags,
context, insidefn, heightfn);
flags ^= PAR_MSQUARES_INVERT;
if (connect) {
flags |= PAR_MSQUARES_CONNECT;
}
m[1] = par_msquares_function(width, height, cellsize, flags,
context, insidefn, heightfn);
return par_msquares__merge(m, 2, snap | connect);
}
int invert = flags & PAR_MSQUARES_INVERT;
// Create the two code tables if we haven't already. These are tables of
// fixed constants, so it's embarassing that we use dynamic memory
// allocation for them. However it's easy and it's one-time-only.
if (!par_msquares_binary_point_table) {
par_init_tables();
}
// Allocate the meshlist and the first mesh.
par_msquares_meshlist* mlist = PAR_CALLOC(par_msquares_meshlist, 1);
mlist->nmeshes = 1;
mlist->meshes = PAR_CALLOC(par_msquares__mesh*, 1);
mlist->meshes[0] = PAR_CALLOC(par_msquares__mesh, 1);
par_msquares__mesh* mesh = mlist->meshes[0];
mesh->dim = (flags & PAR_MSQUARES_HEIGHTS) ? 3 : 2;
int ncols = width / cellsize;
int nrows = height / cellsize;
// Worst case is four triangles and six verts per cell, so allocate that
// much.
int maxtris = ncols * nrows * 4;
int maxpts = ncols * nrows * 6;
int maxedges = ncols * nrows * 2;
// However, if we include extrusion triangles for boundary edges,
// we need space for another 4 triangles and 4 points per cell.
PAR_MSQUARES_T* conntris = 0;
int nconntris = 0;
PAR_MSQUARES_T* edgemap = 0;
if (flags & PAR_MSQUARES_CONNECT) {
conntris = PAR_CALLOC(PAR_MSQUARES_T, maxedges * 6);
maxtris += maxedges * 2;
maxpts += maxedges * 2;
edgemap = PAR_CALLOC(PAR_MSQUARES_T, maxpts);
for (int i = 0; i < maxpts; i++) {
edgemap[i] = 0xffff;
}
}
PAR_MSQUARES_T* tris = PAR_CALLOC(PAR_MSQUARES_T, maxtris * 3);
int ntris = 0;
float* pts = PAR_CALLOC(float, maxpts * mesh->dim);
int npts = 0;
// The "verts" x/y/z arrays are the 4 corners and 4 midpoints around the
// square, in counter-clockwise order. The origin of "triangle space" is at
// the lower-left, although we expect the image data to be in raster order
// (starts at top-left).
float vertsx[8], vertsy[8];
float normalization = 1.0f / PAR_MAX(width, height);
float normalized_cellsize = cellsize * normalization;
int maxrow = (height - 1) * width;
PAR_MSQUARES_T* ptris = tris;
PAR_MSQUARES_T* pconntris = conntris;
float* ppts = pts;
uint8_t* prevrowmasks = PAR_CALLOC(uint8_t, ncols);
int* prevrowinds = PAR_CALLOC(int, ncols * 3);
// If simplification is enabled, we need to track all 'F' cells and their
// respective triangle indices.
uint8_t* simplification_codes = 0;
PAR_MSQUARES_T* simplification_tris = 0;
uint8_t* simplification_ntris = 0;
if (flags & PAR_MSQUARES_SIMPLIFY) {
simplification_codes = PAR_CALLOC(uint8_t, nrows * ncols);
simplification_tris = PAR_CALLOC(PAR_MSQUARES_T, nrows * ncols);
simplification_ntris = PAR_CALLOC(uint8_t, nrows * ncols);
}
// Do the march!
for (int row = 0; row < nrows; row++) {
vertsx[0] = vertsx[6] = vertsx[7] = 0;
vertsx[1] = vertsx[5] = 0.5 * normalized_cellsize;
vertsx[2] = vertsx[3] = vertsx[4] = normalized_cellsize;
vertsy[0] = vertsy[1] = vertsy[2] = normalized_cellsize * (row + 1);
vertsy[4] = vertsy[5] = vertsy[6] = normalized_cellsize * row;
vertsy[3] = vertsy[7] = normalized_cellsize * (row + 0.5);
int northi = row * cellsize * width;
int southi = PAR_MIN(northi + cellsize * width, maxrow);
int northwest = invert ^ insidefn(northi, context);
int southwest = invert ^ insidefn(southi, context);
int previnds[8] = {0};
uint8_t prevmask = 0;
for (int col = 0; col < ncols; col++) {
northi += cellsize;
southi += cellsize;
if (col == ncols - 1) {
northi--;
southi--;
}
int northeast = invert ^ insidefn(northi, context);
int southeast = invert ^ insidefn(southi, context);
int code = southwest | (southeast << 1) | (northwest << 2) |
(northeast << 3);
int const* pointspec = par_msquares_binary_point_table[code];
int ptspeclength = *pointspec++;
int currinds[8] = {0};
uint8_t mask = 0;
uint8_t prevrowmask = prevrowmasks[col];
while (ptspeclength--) {
int midp = *pointspec++;
int bit = 1 << midp;
mask |= bit;
// The following six conditionals perform welding to reduce the
// number of vertices. The first three perform welding with the
// cell to the west; the latter three perform welding with the
// cell to the north.
if (bit == 1 && (prevmask & 4)) {
currinds[midp] = previnds[2];
continue;
}
if (bit == 128 && (prevmask & 8)) {
currinds[midp] = previnds[3];
continue;
}
if (bit == 64 && (prevmask & 16)) {
currinds[midp] = previnds[4];
continue;
}
if (bit == 16 && (prevrowmask & 4)) {
currinds[midp] = prevrowinds[col * 3 + 2];
continue;
}
if (bit == 32 && (prevrowmask & 2)) {
currinds[midp] = prevrowinds[col * 3 + 1];
continue;
}
if (bit == 64 && (prevrowmask & 1)) {
currinds[midp] = prevrowinds[col * 3 + 0];
continue;
}
ppts[0] = vertsx[midp];
ppts[1] = vertsy[midp];
// Adjust the midpoints to a more exact crossing point.
if (midp == 1) {
int begin = southi - cellsize / 2;
int previous = 0;
for (int i = 0; i < cellsize; i++) {
int offset = begin + i / 2 * ((i % 2) ? -1 : 1);
int inside = insidefn(offset, context);
if (i > 0 && inside != previous) {
ppts[0] = normalization *
(col * cellsize + offset - southi + cellsize);
break;
}
previous = inside;
}
} else if (midp == 5) {
int begin = northi - cellsize / 2;
int previous = 0;
for (int i = 0; i < cellsize; i++) {
int offset = begin + i / 2 * ((i % 2) ? -1 : 1);
int inside = insidefn(offset, context);
if (i > 0 && inside != previous) {
ppts[0] = normalization *
(col * cellsize + offset - northi + cellsize);
break;
}
previous = inside;
}
} else if (midp == 3) {
int begin = northi + width * cellsize / 2;
int previous = 0;
for (int i = 0; i < cellsize; i++) {
int offset = begin +
width * (i / 2 * ((i % 2) ? -1 : 1));
int inside = insidefn(offset, context);
if (i > 0 && inside != previous) {
ppts[1] = normalization *
(row * cellsize +
(offset - northi) / (float) width);
break;
}
previous = inside;
}
} else if (midp == 7) {
int begin = northi + width * cellsize / 2 - cellsize;
int previous = 0;
for (int i = 0; i < cellsize; i++) {
int offset = begin +
width * (i / 2 * ((i % 2) ? -1 : 1));
int inside = insidefn(offset, context);
if (i > 0 && inside != previous) {
ppts[1] = normalization *
(row * cellsize +
(offset - northi - cellsize) / (float) width);
break;
}
previous = inside;
}
}
if (mesh->dim == 3) {
if (width > height) {
ppts[2] = heightfn(ppts[0], ppts[1] * width / height,
context);
} else {
ppts[2] = heightfn(ppts[0] * height / width, ppts[1],
context);
}
}
ppts += mesh->dim;
currinds[midp] = npts++;
}
int const* trianglespec = par_msquares_binary_triangle_table[code];
int trispeclength = *trianglespec++;
if (flags & PAR_MSQUARES_SIMPLIFY) {
simplification_codes[ncols * row + col] = code;
simplification_tris[ncols * row + col] = ntris;
simplification_ntris[ncols * row + col] = trispeclength;
}
// Add triangles.
while (trispeclength--) {
int a = *trianglespec++;
int b = *trianglespec++;
int c = *trianglespec++;
*ptris++ = currinds[c];
*ptris++ = currinds[b];
*ptris++ = currinds[a];
ntris++;
}
// Create two extrusion triangles for each boundary edge.
if (flags & PAR_MSQUARES_CONNECT) {
trianglespec = par_msquares_binary_triangle_table[code];
trispeclength = *trianglespec++;
while (trispeclength--) {
int a = *trianglespec++;
int b = *trianglespec++;
int c = *trianglespec++;
int i = currinds[a];
int j = currinds[b];
int k = currinds[c];
int u = 0, v = 0, w = 0;
if ((a % 2) && (b % 2)) {
u = v = 1;
} else if ((a % 2) && (c % 2)) {
u = w = 1;
} else if ((b % 2) && (c % 2)) {
v = w = 1;
} else {
continue;
}
if (u && edgemap[i] == 0xffff) {
for (int d = 0; d < mesh->dim; d++) {
*ppts++ = pts[i * mesh->dim + d];
}
edgemap[i] = npts++;
}
if (v && edgemap[j] == 0xffff) {
for (int d = 0; d < mesh->dim; d++) {
*ppts++ = pts[j * mesh->dim + d];
}
edgemap[j] = npts++;
}
if (w && edgemap[k] == 0xffff) {
for (int d = 0; d < mesh->dim; d++) {
*ppts++ = pts[k * mesh->dim + d];
}
edgemap[k] = npts++;
}
if ((a % 2) && (b % 2)) {
*pconntris++ = i;
*pconntris++ = j;
*pconntris++ = edgemap[j];
*pconntris++ = edgemap[j];
*pconntris++ = edgemap[i];
*pconntris++ = i;
} else if ((a % 2) && (c % 2)) {
*pconntris++ = edgemap[k];
*pconntris++ = k;
*pconntris++ = i;
*pconntris++ = edgemap[i];
*pconntris++ = edgemap[k];
*pconntris++ = i;
} else if ((b % 2) && (c % 2)) {
*pconntris++ = j;
*pconntris++ = k;
*pconntris++ = edgemap[k];
*pconntris++ = edgemap[k];
*pconntris++ = edgemap[j];
*pconntris++ = j;
}
nconntris += 2;
}
}