-
Notifications
You must be signed in to change notification settings - Fork 3
/
omesh2d.cc
1563 lines (1308 loc) · 46.7 KB
/
omesh2d.cc
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
/*
* Orthomesh -- Orthogonal Delaunay Mesh Generator
*
* Copyright (C) 2013 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "omesh2d.h"
#include <assert.h>
#include <algorithm>
#include <list>
#include <Eigen/Core>
#include <Eigen/LU>
#include <Eigen/Geometry>
USING_PART_OF_NAMESPACE_EIGEN
/*************************************************************************
* main API *
*************************************************************************/
Omesh2d::Omesh2d(int32_t major_width, int32_t major_height)
{
config_verbose = 0;
config_split_slice_aggressiveness = 0;
grid_w = major_width;
grid_h = major_height;
for (int32_t x = 0; x < grid_w; x++)
for (int32_t y = 0; y < grid_h; y++) {
Omesh2d::Coordinate coord;
coord.major_x = x;
coord.major_y = y;
coord.minor_x = 0;
coord.minor_y = 0;
coord.size = OMESH2D_FULL;
coord.check();
grid[coord] = Omesh2d::CellType();
}
}
void Omesh2d::run()
{
if (config_verbose > 0)
printf("Entering grid refinement loop.\n");
std::set<Omesh2d::Coordinate> check_queue;
std::set<Omesh2d::Coordinate> prop_queue;
for (auto &it : grid)
check_queue.insert(it.first);
while (!check_queue.empty())
{
std::vector<Omesh2d::Coordinate> split_queue;
for (auto &coord : check_queue)
{
if (grid.count(coord) == 0)
continue;
bool refine_x = false, refine_y = false;
refine(coord.major_x, coord.major_y, coord.minor_x, coord.minor_y, coord.size, coord.size, refine_x, refine_y);
if (refine_x && !refine_y) {
std::vector<int32_t> slice_at;
if (!run_slice_refine(coord, 'x', slice_at, 0, 1))
goto resolve_conflict_by_split;
fixup_slice_vector(slice_at);
grid[coord].slice_at[OMESH2D_TOP] = slice_at;
grid[coord].slice_at[OMESH2D_BOTTOM] = slice_at;
add_neighbourhood_to_queue(prop_queue, coord);
prop_queue.insert(coord);
} else
if (!refine_x && refine_y) {
std::vector<int32_t> slice_at;
if (!run_slice_refine(coord, 'y', slice_at, 0, 1))
goto resolve_conflict_by_split;
fixup_slice_vector(slice_at);
grid[coord].slice_at[OMESH2D_LEFT] = slice_at;
grid[coord].slice_at[OMESH2D_RIGHT] = slice_at;
add_neighbourhood_to_queue(prop_queue, coord);
prop_queue.insert(coord);
} else
if (refine_x && refine_y) {
resolve_conflict_by_split:
if (config_verbose > 3)
printf(" putting cell %s on split queue (refine).\n", coord.to_string().c_str());
split_queue.push_back(coord);
} else {
add_neighbourhood_to_queue(prop_queue, coord);
prop_queue.insert(coord);
}
}
check_queue.clear();
while (split_queue.empty() && !prop_queue.empty())
{
std::set<Omesh2d::Coordinate> next_prop_queue;
if (config_verbose > 1)
printf(" processing grid cells from prop queue: %zd\n", prop_queue.size());
for (auto &coord : prop_queue)
if (grid.count(coord) != 0)
propagate_into(next_prop_queue, split_queue, coord);
prop_queue.swap(next_prop_queue);
}
while (!split_queue.empty())
{
for (auto &coord : split_queue)
{
if (grid.count(coord) == 0)
continue;
if (config_verbose > 3)
printf(" splitting cell %s.\n", coord.to_string().c_str());
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) {
Omesh2d::Coordinate c = coord;
c.size = c.size >> 1;
c.minor_x += x*c.size;
c.minor_y += y*c.size;
c.check();
check_queue.insert(c);
grid[c] = Omesh2d::CellType();
}
grid.erase(coord);
}
std::vector<Omesh2d::Coordinate> next_split_queue;
for (auto &coord : split_queue)
find_neighbours_to_split(next_split_queue, coord);
split_queue.swap(next_split_queue);
}
if (config_verbose > 1)
printf(" grid cells after refinement iteration: %zd\n", grid.size());
}
}
/*************************************************************************
* geometry construction *
*************************************************************************/
bool Omesh2d::create_geometry(const Omesh2d::CellType &ctype)
{
if (geometries.count(ctype) == 0)
{
Omesh2d::Geometry &geom = geometries[ctype];
int split_points = 0, slice_points = 0;
for (int i = 0; i < 4; i++) {
if (ctype.split_at[i]) split_points++;
slice_points += ctype.slice_at[i].size();
}
if (config_verbose > 2)
printf(" creating geometry for a cell type with %d split points and %d slice points.\n", split_points, slice_points);
if (slice_points == 0)
create_simple_split_geometry(geom, ctype);
else if (split_points == 0)
create_simple_slice_geometry(geom, ctype);
else if (split_points == 1)
create_split_slice_geometry(geom, ctype);
if (!geom.segments.empty())
geom.check();
}
return !geometries.at(ctype).segments.empty();
}
void Omesh2d::create_simple_split_geometry(Omesh2d::Geometry &geom, const Omesh2d::CellType &ctype)
{
uint16_t id = 0;
if (ctype.split_at[OMESH2D_LEFT]) id |= 0x1000;
if (ctype.split_at[OMESH2D_RIGHT]) id |= 0x0100;
if (ctype.split_at[OMESH2D_TOP]) id |= 0x0010;
if (ctype.split_at[OMESH2D_BOTTOM]) id |= 0x0001;
// The P? macros are named after the layout of a numerical keypad.
// for example P1 is the lower left corner and P8 is the center point of the top edge.
#define S geom.segments.push_back(Omesh2d::Segment());
#define P1 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>( 0, OMESH2D_FULL));
#define P2 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_HALF, OMESH2D_FULL));
#define P3 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, OMESH2D_FULL));
#define P4 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>( 0, OMESH2D_HALF));
#define P5 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_HALF, OMESH2D_HALF));
#define P6 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, OMESH2D_HALF));
#define P7 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>( 0, 0));
#define P8 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_HALF, 0));
#define P9 geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, 0));
// Type segments as 'S172' or 'S7496' and use the following regex to create P? commands:
// s/S\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\)/S P\1 P\2 P\3 P\4/g
// s/S\([0-9]\)\([0-9]\)\([0-9]\)/S P\1 P\2 P\3/g
switch (id)
{
case 0x0000:
S P1 P3 P7 P9
break;
/* geometries with one split point */
case 0x0001:
S P7 P1 P2
S P7 P9 P2
S P2 P9 P3
break;
case 0x0010:
S P1 P7 P8
S P1 P8 P3
S P8 P9 P3
break;
case 0x0100:
S P7 P9 P6
S P7 P1 P6
S P1 P6 P3
break;
case 0x1000:
S P7 P4 P9
S P4 P9 P3
S P4 P1 P3
break;
/* geometries with two opposite split points */
case 0x0011:
S P7 P1 P8 P2
S P8 P2 P9 P3
break;
case 0x1100:
S P7 P9 P4 P6
S P4 P6 P1 P3
break;
/* geometries with two split points next to each other */
case 0x1010:
S P7 P4 P8
S P8 P9 P3
S P4 P8 P3
S P1 P4 P3
break;
case 0x0110:
S P1 P7 P8
S P8 P6 P9
S P1 P8 P6
S P1 P6 P3
break;
case 0x0101:
S P7 P1 P2
S P7 P2 P6
S P7 P6 P9
S P2 P6 P3
break;
case 0x1001:
S P9 P7 P4
S P9 P4 P2
S P9 P2 P3
S P4 P2 P1
break;
/* geometries with three split points */
case 0x1110:
S P8 P7 P4
S P8 P4 P6
S P8 P6 P9
S P1 P4 P6 P3
break;
case 0x0111:
S P6 P9 P8
S P6 P8 P2
S P6 P2 P3
S P7 P8 P1 P2
break;
case 0x1101:
S P2 P1 P4
S P2 P4 P6
S P2 P6 P3
S P7 P4 P9 P6
break;
case 0x1011:
S P4 P7 P8
S P4 P8 P2
S P4 P2 P1
S P8 P2 P9 P3
break;
default:
abort();
}
#undef S
#undef P1
#undef P2
#undef P3
#undef P4
#undef P5
#undef P6
#undef P7
#undef P8
#undef P9
for (auto &seg : geom.segments)
std::sort(seg.points.begin(), seg.points.end());
std::sort(geom.segments.begin(), geom.segments.end());
}
void Omesh2d::create_simple_slice_geometry(Omesh2d::Geometry &geom, const Omesh2d::CellType &ctype)
{
bool slices_x = !ctype.slice_at[OMESH2D_TOP].empty() || !ctype.slice_at[OMESH2D_BOTTOM].empty();
bool slices_y = !ctype.slice_at[OMESH2D_LEFT].empty() || !ctype.slice_at[OMESH2D_RIGHT].empty();
assert(slices_x != slices_y);
std::vector<int32_t> slice_a = slices_x ? ctype.slice_at[OMESH2D_TOP] : ctype.slice_at[OMESH2D_LEFT];
std::vector<int32_t> slice_b = slices_x ? ctype.slice_at[OMESH2D_BOTTOM] : ctype.slice_at[OMESH2D_RIGHT];
int32_t last_pos = 0;
slice_a.push_back(OMESH2D_FULL);
slice_b.push_back(OMESH2D_FULL);
size_t ia = 0, ib = 0;
while (ia < slice_a.size() && ib < slice_b.size())
{
if (slice_a[ia] == slice_b[ib])
{
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, slice_a[ia]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, slice_a[ia]));
last_pos = slice_a[ia], ia++, ib++;
continue;
}
if (slice_a[ia] < slice_b[ib])
{
int32_t tmp = slice_a[ia++];
assert(slice_a[ia] == slice_b[ib]);
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, slice_a[ia]));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, slice_a[ia]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, slice_a[ia]));
last_pos = slice_a[ia], ia++, ib++;
continue;
}
if (slice_a[ia] > slice_b[ib])
{
int32_t tmp = slice_b[ib++];
assert(slice_a[ia] == slice_b[ib]);
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, slice_a[ia]));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, slice_a[ia]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, tmp));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, slice_a[ia]));
last_pos = slice_a[ia], ia++, ib++;
continue;
}
}
assert(ia == slice_a.size());
assert(ib == slice_b.size());
if (slices_x)
for (auto &seg : geom.segments)
for (auto &p : seg.points) {
int32_t tmp = p.first;
p.first = p.second;
p.second = tmp;
}
for (auto &seg : geom.segments)
std::sort(seg.points.begin(), seg.points.end());
std::sort(geom.segments.begin(), geom.segments.end());
}
void Omesh2d::create_split_slice_geometry(Omesh2d::Geometry &geom, const Omesh2d::CellType &ctype)
{
bool slices_x = !ctype.slice_at[OMESH2D_TOP].empty() || !ctype.slice_at[OMESH2D_BOTTOM].empty();
bool slices_y = !ctype.slice_at[OMESH2D_LEFT].empty() || !ctype.slice_at[OMESH2D_RIGHT].empty();
assert(slices_x != slices_y);
std::vector<int32_t> slice_a = slices_x ? ctype.slice_at[OMESH2D_TOP] : ctype.slice_at[OMESH2D_LEFT];
std::vector<int32_t> slice_b = slices_x ? ctype.slice_at[OMESH2D_BOTTOM] : ctype.slice_at[OMESH2D_RIGHT];
if (slices_x ? ctype.split_at[OMESH2D_RIGHT] : ctype.split_at[OMESH2D_BOTTOM]) {
for (auto &s : slice_a) s = OMESH2D_FULL - s;
for (auto &s : slice_b) s = OMESH2D_FULL - s;
std::sort(slice_a.begin(), slice_a.end());
std::sort(slice_b.begin(), slice_b.end());
}
std::vector<int32_t> indent_a, indent_b;
if (!calc_indent_chain(indent_a, indent_b, slice_a, slice_b, config_split_slice_aggressiveness))
return;
int32_t last_pos = 0;
slice_a.push_back(OMESH2D_FULL);
slice_b.push_back(OMESH2D_FULL);
int32_t last_indent_a = OMESH2D_HALF;
int32_t last_indent_b = OMESH2D_HALF;
indent_a.push_back(0);
indent_b.push_back(0);
size_t ia = 0, ib = 0;
while (ia < slice_a.size() && ib < slice_b.size())
{
if (slice_a[ia] == slice_b[ib])
{
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, slice_a[ia]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(indent_a[ia], slice_a[ia]));
if (last_indent_a != indent_a[ia]) {
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(0, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(indent_a[ia], slice_a[ia]));
}
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(last_indent_a, last_pos));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(last_indent_a, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(indent_a[ia], slice_a[ia]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL - last_indent_b, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL - indent_b[ib], slice_b[ib]));
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, slice_b[ib]));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL - indent_b[ib], slice_b[ib]));
if (last_indent_b != indent_b[ib]) {
geom.segments.push_back(Omesh2d::Segment());
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL, last_pos));
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL - indent_b[ib], slice_b[ib]));
}
geom.segments.back().points.push_back(std::pair<int32_t, int32_t>(OMESH2D_FULL - last_indent_b, last_pos));
last_indent_a = indent_a[ia];
last_indent_b = indent_b[ib];
last_pos = slice_a[ia], ia++, ib++;
continue;
}
if (slice_a[ia] < slice_b[ib])
{
// FIXME
geom.segments.clear();
return;
}
if (slice_a[ia] > slice_b[ib])
{
// FIXME
geom.segments.clear();
return;
}
}
assert(ia == slice_a.size());
assert(ib == slice_b.size());
if (slices_x ? ctype.split_at[OMESH2D_RIGHT] : ctype.split_at[OMESH2D_BOTTOM])
for (auto &seg : geom.segments)
for (auto &p : seg.points)
p.second = OMESH2D_FULL - p.second;
if (slices_x)
for (auto &seg : geom.segments)
for (auto &p : seg.points) {
int32_t tmp = p.first;
p.first = p.second;
p.second = tmp;
}
std::vector<Omesh2d::Segment> new_segments;
for (auto &seg : geom.segments) {
std::sort(seg.points.begin(), seg.points.end());
std::vector<std::pair<int32_t, int32_t>> new_points;
for (auto &p : seg.points)
if (new_points.empty() || p != new_points.back())
new_points.push_back(p);
seg.points.swap(new_points);
if (seg.points.size() >= 3)
new_segments.push_back(seg);
}
std::sort(new_segments.begin(), new_segments.end());
geom.segments.swap(new_segments);
}
/*************************************************************************
* various helper functions *
*************************************************************************/
bool Omesh2d::run_slice_refine(const Omesh2d::Coordinate &coord, char axis, std::vector<int32_t> &slice_at, int32_t start, int shift)
{
bool refine_x = false, refine_y = false;
int32_t minor_x = coord.minor_x, size_x = coord.size;
int32_t minor_y = coord.minor_y, size_y = coord.size;
if (axis == 'x') {
size_x = size_x >> shift;
minor_x += start;
} else {
size_y = size_y >> shift;
minor_y += start;
}
refine(coord.major_x, coord.major_y, minor_x, minor_y, size_x, size_y, refine_x, refine_y);
if (axis == 'x') {
if (refine_y || (refine_x && !run_slice_refine(coord, axis, slice_at, start, shift+1)))
return false;
minor_x += size_x;
} else {
if (refine_x || (refine_y && !run_slice_refine(coord, axis, slice_at, start, shift+1)))
return false;
minor_y += size_y;
}
int32_t s = start + (coord.size >> shift);
for (int32_t i = coord.size; i < OMESH2D_FULL; i = i << 1)
s = s << 1;
slice_at.push_back(s);
refine(coord.major_x, coord.major_y, minor_x, minor_y, size_x, size_y, refine_x, refine_y);
if (axis == 'x') {
if (refine_y || (refine_x && !run_slice_refine(coord, axis, slice_at, start + size_x, shift+1)))
return false;
} else {
if (refine_x || (refine_y && !run_slice_refine(coord, axis, slice_at, start + size_y, shift+1)))
return false;
}
return true;
}
void Omesh2d::propagate_into(std::set<Omesh2d::Coordinate> &prop_queue, std::vector<Omesh2d::Coordinate> &split_queue, const Omesh2d::Coordinate &coord)
{
Omesh2d::CellType ctype = grid.at(coord);
std::set<Omesh2d::Coordinate> neigh;
add_neighbourhood_to_queue(neigh, coord);
for (auto &n : neigh)
{
const Omesh2d::CellType &nct = grid.at(n);
bool is_above = n.is_above(coord);
bool is_below = n.is_below(coord);
bool is_left_of = n.is_left_of(coord);
bool is_right_of = n.is_right_of(coord);
if (n.size < coord.size)
{
if (is_above) ctype.split_at[OMESH2D_TOP] = true;
if (is_below) ctype.split_at[OMESH2D_BOTTOM] = true;
if (is_left_of) ctype.split_at[OMESH2D_LEFT] = true;
if (is_right_of) ctype.split_at[OMESH2D_RIGHT] = true;
}
if (is_above || is_below)
{
int32_t last_step = 0;
if (!nct.slice_at[OMESH2D_LEFT].empty()) {
int32_t sl = is_above ? OMESH2D_FULL - nct.slice_at[OMESH2D_LEFT].back() : nct.slice_at[OMESH2D_LEFT].front();
last_step = std::max(last_step, coord.scale_from(n, sl));
}
if (!nct.slice_at[OMESH2D_RIGHT].empty()) {
int32_t sl = is_above ? OMESH2D_FULL - nct.slice_at[OMESH2D_RIGHT].back() : nct.slice_at[OMESH2D_RIGHT].front();
last_step = std::max(last_step, coord.scale_from(n, sl));
}
if (0 < last_step && last_step < OMESH2D_HALF) {
int32_t s = is_above ? (last_step << 1) : OMESH2D_FULL - (last_step << 1);
ctype.slice_at[OMESH2D_LEFT].push_back(s);
ctype.slice_at[OMESH2D_RIGHT].push_back(s);
}
int from_slice_at = is_above ? OMESH2D_BOTTOM : OMESH2D_TOP;
int to_slice_at = is_above ? OMESH2D_TOP : OMESH2D_BOTTOM;
for (auto s : nct.slice_at[from_slice_at])
ctype.slice_at[to_slice_at].push_back(coord.scale_x_from(n, s));
if (nct.split_at[from_slice_at])
ctype.slice_at[to_slice_at].push_back(coord.scale_x_from(n, OMESH2D_HALF));
}
else
if (is_left_of || is_right_of)
{
int32_t last_step = 0;
if (!nct.slice_at[OMESH2D_TOP].empty()) {
int32_t sl = is_left_of ? OMESH2D_FULL - nct.slice_at[OMESH2D_TOP].back() : nct.slice_at[OMESH2D_TOP].front();
last_step = std::max(last_step, coord.scale_from(n, sl));
}
if (!nct.slice_at[OMESH2D_BOTTOM].empty()) {
int32_t sl = is_left_of ? OMESH2D_FULL - nct.slice_at[OMESH2D_BOTTOM].back() : nct.slice_at[OMESH2D_BOTTOM].front();
last_step = std::max(last_step, coord.scale_from(n, sl));
}
if (0 < last_step && last_step < OMESH2D_HALF) {
int32_t s = is_left_of ? (last_step << 1) : OMESH2D_FULL - (last_step << 1);
ctype.slice_at[OMESH2D_TOP].push_back(s);
ctype.slice_at[OMESH2D_BOTTOM].push_back(s);
}
int from_slice_at = is_left_of ? OMESH2D_RIGHT : OMESH2D_LEFT;
int to_slice_at = is_left_of ? OMESH2D_LEFT : OMESH2D_RIGHT;
for (auto s : nct.slice_at[from_slice_at])
ctype.slice_at[to_slice_at].push_back(coord.scale_y_from(n, s));
if (nct.split_at[from_slice_at])
ctype.slice_at[to_slice_at].push_back(coord.scale_y_from(n, OMESH2D_HALF));
}
else
abort();
}
if (ctype.split_at[OMESH2D_TOP] && ctype.split_at[OMESH2D_BOTTOM] && ctype.split_at[OMESH2D_LEFT] && ctype.split_at[OMESH2D_RIGHT]) {
split_queue.push_back(coord);
return;
}
for (int i = 0; i < 4; i++)
fixup_slice_vector(ctype.slice_at[i]);
for (int i = 0; i < 4; i++)
if (ctype.slice_at[i].size() == 1 && ctype.slice_at[i ^ 1].size() <= 1) {
ctype.slice_at[i].clear();
ctype.split_at[i] = true;
}
for (int i = 0; i < 4; i++) {
for (auto s : ctype.slice_at[i])
if (s == OMESH2D_HALF) ctype.split_at[i] = false;
}
bool slices_x = !ctype.slice_at[OMESH2D_TOP].empty() || !ctype.slice_at[OMESH2D_BOTTOM].empty();
bool slices_y = !ctype.slice_at[OMESH2D_LEFT].empty() || !ctype.slice_at[OMESH2D_RIGHT].empty();
if (ctype.split_at[OMESH2D_TOP] && ctype.split_at[OMESH2D_BOTTOM]) slices_x = true;
if (ctype.split_at[OMESH2D_LEFT] && ctype.split_at[OMESH2D_RIGHT]) slices_y = true;
if ((slices_x && slices_y) || !check_slice_compatibility(ctype.slice_at[OMESH2D_TOP], ctype.slice_at[OMESH2D_BOTTOM]) ||
!check_slice_compatibility(ctype.slice_at[OMESH2D_LEFT], ctype.slice_at[OMESH2D_RIGHT])) {
if (config_verbose > 3)
printf(" putting cell %s on split queue (incompatible slices).\n", coord.to_string().c_str());
split_queue.push_back(coord);
return;
}
if (!create_geometry(ctype)) {
if (config_verbose > 3)
printf(" putting cell %s on split queue (no valid geometry).\n", coord.to_string().c_str());
split_queue.push_back(coord);
return;
}
if (ctype != grid.at(coord)) {
grid[coord] = ctype;
prop_queue.insert(neigh.begin(), neigh.end());
return;
}
}
// we are splitting at coord. if this cell has any neighbours larger then itself, we also need
// to split those. The list of neighbours that need splitting is returned in &neigh.
void Omesh2d::find_neighbours_to_split(std::vector<Omesh2d::Coordinate> &neigh, const Omesh2d::Coordinate &coord)
{
// list of candidates as pairs of x/y offsets (unit length is coord.size)
static const int neigh_map[][2] = {
// left
{ -2, -1 }, { -2, 0 },
// right
{ 1, -1 }, { 1, 0 },
// top
{ -1, -2 }, { 0, -2 },
// bottom
{ -1, 1 }, { 0, 1 }
};
for (size_t i = 0; i < sizeof(neigh_map)/sizeof(neigh_map[0]); i++) {
Omesh2d::Coordinate n = coord;
n.minor_x += neigh_map[i][0] * n.size;
n.minor_y += neigh_map[i][1] * n.size;
n.normalize();
n.size *= 2;
if (grid.count(n) > 0) {
if (config_verbose > 3)
printf(" putting cell %s on split queue (neighbour).\n", n.to_string().c_str());
neigh.push_back(n);
}
}
}
// add ALL the neighbours
void Omesh2d::add_neighbourhood_to_queue(std::set<Omesh2d::Coordinate> &neigh, const Omesh2d::Coordinate &coord)
{
// list of candidates as tuples of x_off, y_off, size (unit length is half coord.size)
static const int neigh_map[][3] =
{
// double size
/* left */ { -4, -2, 4 }, { -4, 0, 4 },
/* right */ { 2, -2, 4 }, { 2, 0, 4 },
/* top */ { -2, -4, 4 }, { 0, -4, 4 },
/* bottom */ { -2, 2, 4 }, { 0, 2, 4 },
// same size
/* left */ { -2, 0, 2 },
/* right */ { 2, 0, 2 },
/* top */ { 0, -2, 2 },
/* bottom */ { 0, 2, 2 },
// half size
/* left */ { -1, 0, 1 }, { -1, 1, 1 },
/* right */ { 2, 0, 1 }, { 2, 1, 1 },
/* top */ { 0, -1, 1 }, { 1, -1, 1 },
/* bottom */ { 0, 2, 1 }, { 1, 2, 1 }
};
for (size_t i = 0; i < sizeof(neigh_map)/sizeof(neigh_map[0]); i++) {
Omesh2d::Coordinate n = coord;
n.minor_x += neigh_map[i][0] * (n.size >> 1);
n.minor_y += neigh_map[i][1] * (n.size >> 1);
n.size = neigh_map[i][2] * (n.size >> 1);
n.normalize();
if (grid.count(n) > 0)
neigh.insert(n);
}
}
/*************************************************************************
* various static helper functions *
*************************************************************************/
int Omesh2d::count_trailing_zeros(int32_t v)
{
if (v == 0)
return 32;
int zeros = 0;
for (int i = 16; i != 0; i = i >> 1)
if ((v & ((1 << i) - 1)) == 0) {
zeros += i;
v = v >> i;
}
return zeros;
}
void Omesh2d::fixup_slice_vector(std::vector<int32_t> &slice_at)
{
// STEP 1: Make sure the slice hierarchy is correct
std::set<int32_t> slices;
for (auto s : slice_at)
add_slice_hierarchy(slices, s);
slice_at.clear();
slice_at.insert(slice_at.end(), slices.begin(), slices.end());
// STEP 2: Make sure we don't change step sizes by more than a factor two
std::list<int32_t> steps;
for (size_t i = 0; i <= slice_at.size(); i++) {
int32_t from = i > 0 ? slice_at[i-1] : 0;
int32_t to = i < slice_at.size() ? slice_at[i] : OMESH2D_FULL;
if (to != from)
steps.push_back(to - from);
}
int32_t position = 0;
for (std::list<int32_t>::iterator it = steps.begin(); it != steps.end(); it++)
{
if (it != steps.begin()) {
std::list<int32_t>::iterator prev = it;
int32_t max_step = *(--prev);
if (count_trailing_zeros(position + 2*max_step) > count_trailing_zeros(position + max_step))
max_step += max_step;
if (*it > max_step) {
position += max_step;
steps.insert(it, max_step);
*(it--) -= max_step;
continue;
}
}
std::list<int32_t>::iterator next = it;
if (++next != steps.end()) {
int32_t max_step = *next;
if (count_trailing_zeros(position + *it - 2*max_step) > count_trailing_zeros(position + *it - max_step))
max_step += max_step;
if (*it > max_step) {
steps.insert(next, max_step);
*(it--) -= max_step;
continue;
}
}
position += *it;
}
slice_at.clear();
for (auto it : steps)
slice_at.push_back(slice_at.empty() ? it : slice_at.back() + it);
assert(slice_at.back() == OMESH2D_FULL);
slice_at.pop_back();
}
void Omesh2d::add_slice_hierarchy(std::set<int32_t> &slices, int32_t s)
{
int32_t p = 0;
int32_t step = OMESH2D_FULL;
if (s == 0 || s == OMESH2D_FULL) {
slices.insert(s);
return;
}
while (p != s) {
step = step >> 1;
p += s < p ? -step : +step;
slices.insert(p);
}
}
void Omesh2d::create_reduced_slices_copy(std::vector<int32_t> &to, const std::vector<int32_t> &from)
{
to.clear();
for (size_t i = 0; i < from.size(); i++)
{
int32_t prev = i == 0 ? 0 : from[i-1];
int32_t next = i+1 < from.size() ? from[i+1] : OMESH2D_FULL;
int this_trailing_zeros = count_trailing_zeros(from[i]);
if (count_trailing_zeros(prev) < this_trailing_zeros || count_trailing_zeros(next) < this_trailing_zeros)
to.push_back(from[i]);
}
}
bool Omesh2d::check_slice_compatibility(const std::vector<int32_t> &sliceA, const std::vector<int32_t> &sliceB)
{
std::vector<int32_t> reducedA, reducedB;
create_reduced_slices_copy(reducedA, sliceA);
create_reduced_slices_copy(reducedB, sliceB);
size_t i = 0;
for (auto s : reducedA) {
while (i < sliceB.size() && sliceB[i] < s) i++;
if (sliceB[i] != s)
return false;
}
i = 0;
for (auto s : reducedB) {
while (i < sliceA.size() && sliceA[i] < s) i++;
if (sliceA[i] != s)
return false;
}
return true;
}
int32_t Omesh2d::next_indent(int32_t last_indent, int32_t last_gap, int32_t this_gap, int32_t next_gap)
{
// l .. width of half cell
// u .. height of this gap
// a .. horiz. distance of upper point from center line
// b .. horiz. distance of lower point from center line
// h .. max vert. distance of circumcenter of gap center
// Maxima Fomulas:
// solve((a+b)*(b-a)/(2*u) = h, b);
// solve((l-b)*(b-a)/(2*u) = h, b);
double l = OMESH2D_HALF;
double u = this_gap;
double a = OMESH2D_HALF - last_indent;
// case 1: limit from below
double h1 = (this_gap + next_gap) / 2.0;
double b1 = sqrt(a*a + 2*h1*u);
// case 2: limit from above
double h2 = (this_gap + last_gap) / 2.0;
double b2 = (l + a - sqrt(a*a - 2*a*l + l*l - 8*h2*u)) / 2.0;
return round(std::max(std::max(l - b1, l - b2), 0.0));
}
int32_t Omesh2d::prev_indent(int32_t this_indent, int32_t last_gap, int32_t this_gap, int32_t next_gap)
{
double l = OMESH2D_HALF;
double u = this_gap;
double b = OMESH2D_HALF - this_indent;
double h1 = (this_gap + next_gap) / 2.0;
double a1 = sqrt(b*b - 2*h1*u);
double h2 = (this_gap + last_gap) / 2.0;
double a2 = (2*h2*u - b*l + b*b) / (b-l);
return round(std::max(std::max(l - a1, l - a2), 0.0));
}
bool Omesh2d::calc_indent_chain(std::vector<int32_t> &indents, const std::vector<int32_t> &slices, int aggressiveness)
{
// the trivial cases
if (slices.size() > 0 && slices.at(0) >= OMESH2D_HALF) {
indents.insert(indents.end(), slices.size(), 0);
return true;
}
if (slices.size() > 1 && slices.at(slices.size()-2) <= OMESH2D_HALF) {
indents.insert(indents.end(), slices.size()-1, OMESH2D_HALF);
indents.push_back(0);
return true;
}
if (aggressiveness == 0)
return false;
int32_t last_pos = 0, last_gap = 0, last_indent = OMESH2D_HALF;
for (size_t i = 0; i < slices.size(); i++)
{
int32_t this_gap = slices[i] - last_pos;
int32_t next_gap = i+1 < slices.size() ? slices[i+1] - slices[i] : 0;
int32_t indent = last_indent;
if (aggressiveness == 2 || this_gap >= OMESH2D_FULL / 4) {
indent = next_indent(last_indent, last_gap * 0.9, this_gap, next_gap * (aggressiveness == 2 ? 1.0 : 1.1));
if (indent == 0)
indent = next_indent(last_indent, last_gap * 0.9, this_gap, next_gap * 0.9);
}
if (indent == 0 && last_indent != 0 && !indents.empty()) {
int32_t upd_last_indent = prev_indent(indent, last_gap * 0.9, this_gap, next_gap * 0.9);