-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyAlgorithmMaybe.prejava
1739 lines (1588 loc) · 90.2 KB
/
MyAlgorithmMaybe.prejava
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
#include "macros.h"
/*
failure: alg5killer1 outward: null pointer exception (now "null initial and final vertex" message ) in compute_priority2f
(actually I think this is structural badness-- doesn't happen if re-delaunayize)
assertion failure: if symmetrical and no vertex at origin
assertion failure: FaceSweepKiller, turn on delaunayize and calc underside (nothing really works in that case, but should maybe give better message)
*/
import com.donhatchsw.compat.IntArrayList;
import com.donhatchsw.util.SortStuff;
import com.donhatchsw.util.Arrays;
import com.donhatchsw.util.IndexBinaryHeap;
import com.donhatchsw.util.IndexBinaryHeapKeyed;
import com.donhatchsw.util.TopSorter;
import com.donhatchsw.util.VecMath;
public class MyAlgorithmMaybe
{
public final static int STRATEGY_BEST_GOODNESS = 1; // original implementation. prone to infinite looping, in ways the code doesn't detect yet.
public final static int STRATEGY_RANDOM_GOOD = 2; // second implementation. seen it bite own tail (Just Noise 10000, prewarp, apply radial match. more tailbiting with upward than outward). hard to make it infinite loop (radial heightfields don't do it) but I got it to, see DUMP.off.ConvexNoise1_144_makes_my_algorithm_infinite_loop
public final static int STRATEGY_BEST_COMPOSITE = 3; // third implementation.
public static int strategy = STRATEGY_RANDOM_GOOD; // TODO: make this a parameter to doIt, maybe? not sure
public static class Graphic
{
public int iPass; // 0: before dual, 1: after dual
public java.awt.Color color;
public double verts[][/*2*/];
public Graphic(int iPass, java.awt.Color color, double verts[][])
{
this.iPass = iPass;
this.color = color;
this.verts = verts; // note there may be some sharing
}
} // class Graphic
private MyAlgorithmMaybe(){ throw new AssertionError(); } // non-instantiatable util class
// So I can see more clearly,
// isolate exactly the data we will use during the algorithm(s).
private static class HelperDataStructure
{
//
// Representation of the original mesh with vertex weights.
// Immutable after initial construction.
//
public double vertsXY[/*nVerts*/][/*2*/];
public double v2momentAndArea[][/*4*/]; // from original verts... although I suspect [2] isn't meaningful for us
public double e2direction[/*nEdges*/][/*3*/];
public int e2v[/*nEdges*/][/*2*/]; // values are in [0..nVerts] (not 0..nVerts-1 !) since nVerts represents "the infinite vertex"
public int v2e[/*nVerts*/][];
public HelperDataStructure(Mesh mesh, boolean mergeInsideOutVertsIntoInfiniteVert)
{
int nVerts = mesh.verts.size();
int nEdges = mesh.edges.size();
this.vertsXY = MeshUtils.getMeshVertsXY(mesh);
CHECK_EQ(vertsXY.length, nVerts);
this.v2momentAndArea = new double[nVerts][];
FORI (iVert, nVerts)
this.v2momentAndArea[iVert] = mesh.getVert(iVert).momentAndArea;
this.e2direction = new double[nEdges][];
FORI (iEdge, nEdges)
this.e2direction[iEdge] = mesh.getEdge(iEdge).direction;
{
MeshUtils.SimplerMeshDataStructure simplerMeshDataStructure = new MeshUtils.SimplerMeshDataStructure(mesh, mergeInsideOutVertsIntoInfiniteVert);
this.e2v = simplerMeshDataStructure.e2v;
this.v2e = simplerMeshDataStructure.v2e;
}
CHECK_EQ(this.e2v.length, nEdges);
if (e2direction[0] != null) // is this the right condition? original primal doesn't have directions, original dual does
{
// Make sure neighbors of each vert are listed in ccw order.
FORI (iVert, nVerts)
{
// Note that there can be an isolated vertex,
// in the case that "calc underside when delaunayizing" was checked;
// the original inside-out vert gets isolated since everything
// got reconnected to the synthetic infinite vert.
if (v2e[iVert].length == 0) continue;
CHECK_EQ(v2e[iVert].length, 3);
if (VecMath.vxv2(e2direction[v2e[iVert][0]],
e2direction[v2e[iVert][1]]) < 0.)
{
CHECK(false); // yay! doesn't happen any more since SimplerMeshDataStructure gets it right
int temp;
SWAP(v2e[iVert][0], v2e[iVert][1], temp);
}
CHECK(VecMath.vxv2(e2direction[v2e[iVert][0]],
e2direction[v2e[iVert][1]]) > 0.);
CHECK(VecMath.vxv2(e2direction[v2e[iVert][1]],
e2direction[v2e[iVert][2]]) > 0.);
CHECK(VecMath.vxv2(e2direction[v2e[iVert][2]],
e2direction[v2e[iVert][0]]) > 0.);
}
}
}
}; // HelperDataStructure
// "v0->e3->v1->e2[->v100->e3->v200]"
private static String edgeListToDebugString(int edgeListSize, int edgeList[],
int e2v[][], int v2e[][],
int v2parentVert[],
boolean assertParentRelationshipsFlag) // can assert only *after* have changed parents
{
StringBuffer sb = new StringBuffer();
int nVerts = v2parentVert.length;
//sb.append("[nVerts="+nVerts+"]");
FORI (i, edgeListSize)
{
int e = edgeList[i];
int v0 = e2v[e][0];
int v1 = e2v[e][1];
if (assertParentRelationshipsFlag)
{
if (!(v2parentVert[v0]==v1 || (i>=edgeListSize-1 && v2parentVert[v0]==-1)))
{
sb.append("[XXX WTF? v2parentVert[v"+v0+"]==v"+v2parentVert[v0]+"]");
CHECK(false);
}
}
if (i==0 && v2parentVert[v0]!=-1 && v2parentVert[v0]!=v1)
{
sb.append("(v"+v2parentVert[v0]+"<-)");
}
sb.append("v"+v0);
CHECK_NE(v0, nVerts); // so don't need to emit "(infinite)"
if (i!=0 && v2parentVert[v0]!=-1 && v2parentVert[v0]!=v1)
{
sb.append("(->v"+v2parentVert[v0]+")");
}
if (false) // not of major interest, can hard-code to true when interested
{
sb.append("[-e"+e+"->]");
}
if (v2parentVert[v0] == v1)
{
sb.append("->");
}
else if (v1!=nVerts && v2parentVert[v1] == v0)
{
sb.append("<-");
}
else
{
sb.append(" ");
}
if (i+1<edgeListSize)
{
CHECK_EQ(v1, e2v[edgeList[i+1]][0]);
// will emit it as v0 on next iteration
}
else
{
sb.append("v"+v1);
if (v1==nVerts)
sb.append("(infinite)");
else if (v2parentVert[v1] != -1 && v2parentVert[v1]!=v0)
sb.append("(->v"+v2parentVert[v1]+")");
}
}
return sb.toString();
} // edgeListToDebugString
// helper function.
// computes and fills in v2momentAndWeightStrictlyUpstream[iVert]
// from those of its children and moments and areas of its children.
private static void computeMomentAndAreaStrictlyUpstreamFromUpstream(int iVert,
int e2v[][], // in
int v2e[][], // in
double v2momentAndArea[][], // in
int v2parentVert[], // in
double v2momentAndWeightStrictlyUpstream[][], // in/out
int verboseLevel)
{
int nVerts = v2momentAndWeightStrictlyUpstream.length;
VecMath.zerovec(v2momentAndWeightStrictlyUpstream[iVert]);
FORI (iEdgeThisVert, v2e[iVert].length)
{
int iNeighborVert = e2v[v2e[iVert][iEdgeThisVert]][1];
if (iNeighborVert < nVerts
&& v2parentVert[iNeighborVert] == iVert)
{
if (verboseLevel >= 2) OUT(" accumulating centroid from upstream v"+iNeighborVert+" into v"+iVert);
GeomUtils.accumulateMomentAndArea(v2momentAndWeightStrictlyUpstream[iVert],
v2momentAndWeightStrictlyUpstream[iNeighborVert]);
GeomUtils.accumulateMomentAndArea(v2momentAndWeightStrictlyUpstream[iVert],
v2momentAndArea[iNeighborVert]);
}
}
} // computeMomentAndAreaStrictlyUpstreamFromUpstream
// helper function.
static double computeExitGoodness(double v[/*2*/],
double momentAndWeightStrictlyUpstream[/*2*/],
double normalizedEdgeDirection[/*2*/],
double scratchCentroid[/*2*/],
double scratchVertMinusCentroid[/*2*/],
int verboseLevel)
{
double exitGoodness;
if (momentAndWeightStrictlyUpstream[2] > 0.)
{
VecMath.vxs(2, scratchCentroid, momentAndWeightStrictlyUpstream, 1./momentAndWeightStrictlyUpstream[2]);
VecMath.vmv(2, scratchVertMinusCentroid, v, scratchCentroid);
exitGoodness = VecMath.dot(2, normalizedEdgeDirection, scratchVertMinusCentroid);
if (verboseLevel >= 2) OUT(" exitGoodness = "+exitGoodness);
}
else
{
// There's no weight hanging from this vertex,
// So goodness is 0; it's good, but it's not really in danger of being bad.
//exitGoodness = 0.;
exitGoodness = Double.POSITIVE_INFINITY;
if (verboseLevel >= 2) OUT(" exitGoodness = 0 (setting to "+exitGoodness+" because no danger)");
}
return exitGoodness;
} // computeExitGoodness
public static int[][] compute_v2eSweepward_from_primal_and_priority2v(
Mesh mesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
int priority2v[])
{
int verboseLevel = 0; // 1: constant, 2: linear
if (verboseLevel >= 1) OUT(" in MyAlgorithmMaybe.compute_v2eSweepward_from_primal_and_priority2v");
int nVerts = mesh.verts.size();
int nEdges = mesh.edges.size();
if (verboseLevel >= 1) OUT(" nVerts = "+nVerts);
if (verboseLevel >= 1) OUT(" nEdges = "+nEdges);
if (verboseLevel >= 2) OUT(" priority2v = "+VecMath.toString(priority2v));
CHECK_EQ(priority2v.length, nVerts);
int v2priority[] = VecMath.invertperm(priority2v);
CHECK_EQ(v2priority.length, nVerts);
if (verboseLevel >= 2) OUT(" v2priority = "+VecMath.toString(v2priority));
int v2e[][];
int e2v[][];
{
MeshUtils.SimplerMeshDataStructure simplerMeshDataStructure = new MeshUtils.SimplerMeshDataStructure(mesh, true);
v2e = simplerMeshDataStructure.v2e;
e2v = simplerMeshDataStructure.e2v;
}
CHECK_EQ(v2e.length, nVerts+1);
CHECK_EQ(e2v.length, nEdges);
double vertsXY[/*nVerts*/][/*2*/] = MeshUtils.getMeshVertsXY(mesh);
CHECK_EQ(vertsXY.length, nVerts);
int v2eSweepward[][] = new int[nVerts][];
double sweepDirection[] = new double[2]; // scratch for loop
double normalizedEdgeDirection[] = new double[2]; // scratch for loop
{
int scratchEdgesSweepward[] = new int[nEdges/2];
FORI (iVert, nVerts)
{
int nSweepwardEdgesThisVert = 0; // and counting
int edgesThisVert[] = v2e[iVert];
FORI (iEdgeThisVert, edgesThisVert.length)
{
int iEdge = edgesThisVert[iEdgeThisVert];
CHECK_EQ(e2v[iEdge][0], iVert);
int iNeighborVert = e2v[iEdge][1];
CHECK_NE(iNeighborVert, iVert);
// It's upward if edge direction
// has positive dot product with (vert - sweepCenter).
double sweepGoodness;
{
// Get sweepDirection: unit, or zero (if sweep origin is exactly at vert).
// sweepDirection = vertsXY[iVert] - sweepOrigin, homogeneous
VecMath.sxvpsxv(2, sweepDirection,
sweepOriginHomo[2], vertsXY[iVert],
-1., sweepOriginHomo);
if (VecMath.normsqrd(2, sweepDirection) != 0.) // if vertex is not exactly on sweep origin
VecMath.normalize(2, sweepDirection,
sweepDirection);
VecMath.normalize(2, normalizedEdgeDirection, mesh.getEdge(iEdge).direction);
sweepGoodness = VecMath.dot(2, normalizedEdgeDirection, sweepDirection);
}
if (sweepGoodness > 0.)
{
// Must also check that it's going to something strictly later
// in the sweep, so that we don't accidentally form a cycle due to
// floating point roundoff error.
if (iNeighborVert == nVerts // the infinite synthetic vert
|| v2priority[iNeighborVert] > v2priority[iVert])
scratchEdgesSweepward[nSweepwardEdgesThisVert++] = iEdge;
else
OUT("HEY! looks like a backwards edge is sweepward: e"+iEdge+" from v"+iVert+" to v"+iNeighborVert+"");
}
}
v2eSweepward[iVert] = (int[])Arrays.subarray(scratchEdgesSweepward, 0,nSweepwardEdgesThisVert);
}
}
if (verboseLevel >= 1) OUT(" out MyAlgorithmMaybe.compute_v2eSweepward_from_primal_and_priority2v");
return v2eSweepward;
} // compute_v2eSweepward_from_primal_and_priority2v
public static int[][] compute_f2lightSides_from_dual_and_priority2f(
Mesh dualMesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
int priority2f[])
{
int verboseLevel = 0; // 1: constant, 2: linear
if (verboseLevel >= 1) OUT(" in MyAlgorithmMaybe.compute_f2lightSides_from_dual_and_priority2f");
int nEdges = dualMesh.edges.size();
int nFaces = dualMesh.verts.size();
if (verboseLevel >= 1) OUT(" nEdges = "+nEdges);
if (verboseLevel >= 1) OUT(" nFaces = "+nFaces);
if (verboseLevel >= 2) OUT(" priority2f = "+VecMath.toString(priority2f));
CHECK_EQ(priority2f.length, nFaces);
int f2priority[] = VecMath.invertperm(priority2f);
CHECK_EQ(f2priority.length, nFaces);
if (verboseLevel >= 2) OUT(" f2priority = "+VecMath.toString(f2priority));
int f2e[][];
int e2f[][];
{
MeshUtils.SimplerMeshDataStructure dualSimplerMeshDataStructure = new MeshUtils.SimplerMeshDataStructure(dualMesh, false); // all verts are finite, for this one
f2e = dualSimplerMeshDataStructure.v2e;
e2f = dualSimplerMeshDataStructure.e2v;
}
CHECK_EQ(e2f.length, nEdges);
CHECK_EQ(f2e.length, nFaces);
int f2lightSides[][] = new int[nFaces][];
{
int scratchFaceLightSides[] = new int[nEdges/2];
FORI (iFace, nFaces)
{
int nLightSidesThisFace = 0; // and counting
int iFacePriority = f2priority[iFace];
int edgesThisFace[] = f2e[iFace];
FORI (iEdgeThisFace, edgesThisFace.length)
{
int iEdge = edgesThisFace[iEdgeThisFace];
CHECK_EQ(e2f[iEdge][0], iFace);
int iNeighborFace = e2f[iEdge][1];
CHECK_NE(iNeighborFace, iFace);
if (f2priority[iNeighborFace] < iFacePriority)
scratchFaceLightSides[nLightSidesThisFace++] = iEdge;
}
f2lightSides[iFace] = (int[])Arrays.subarray(scratchFaceLightSides, 0,nLightSidesThisFace);
}
}
if (verboseLevel >= 2) OUT(" f2lightSides = "+VecMath.toString(f2lightSides));
if (verboseLevel >= 1) OUT(" out MyAlgorithmMaybe.compute_f2lightSides_from_dual_and_priority2f");
return f2lightSides;
} // compute_f2lightSides_from_dual_and_priority2f
public static BigInt countVertSweeps_from_v2eSweepward(int v2eSweepward[][])
{
BigInt answer = new BigInt(1);
int nVerts = v2eSweepward.length;
int nZeros = 0;
FORI (iVert, nVerts)
{
int nLightSides = v2eSweepward[iVert].length;
if (nLightSides == 0)
nZeros++;
else
answer.timesEquals(nLightSides);
}
CHECK(nZeros == 0 || nZeros == nVerts); // this is the only difference from countFaceSweeps_from_f2lightSides
return answer;
} // countVertSweeps_from_v2eSweepward
public static BigInt countFaceSweeps_from_f2lightSides(int f2lightSides[][])
{
BigInt answer = new BigInt(1);
int nFaces = f2lightSides.length;
int nZeros = 0;
FORI (iFace, nFaces)
{
int nLightSides = f2lightSides[iFace].length;
if (nLightSides == 0)
nZeros++;
else
answer.timesEquals(nLightSides);
}
CHECK(nZeros == 1 || nZeros == nFaces);
return answer;
} // countFaceSweeps_from_f2lightSides
public static Net selectVertSweep_from_priority2v_and_v2eSweepward(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
int priority2v[],
int v2eSweepward[][],
BigInt iNet)
{
CHECK(iNet.ge(0));
CHECK_EQ(priority2v.length, v2eSweepward.length);
BigInt scratch = iNet.copy();
Net net = new Net(mesh, dualMesh);
FORIDOWN (iiVert, v2eSweepward.length)
{
int iVert = priority2v[iiVert];
if (v2eSweepward[iVert].length > 0)
{
int remainder = scratch.divEqualsReturningRemainder(v2eSweepward[iVert].length);
int iEdge = v2eSweepward[iVert][remainder];
net.cut(iEdge, true);
}
}
CHECK(scratch.eq(0));
return net;
} // selectVertSweep_from_v2eSweepward
public static Net selectFaceSweep_from_priority2f_and_f2lightSides(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
int priority2f[],
int f2lightSides[][],
BigInt iNet)
{
CHECK(iNet.ge(0));
CHECK_EQ(priority2f.length, f2lightSides.length);
BigInt scratch = iNet.copy();
Net net = new Net(mesh, dualMesh);
FORIDOWN (iiFace, priority2f.length)
{
int iFace = priority2f[iiFace];
if (f2lightSides[iFace].length > 0)
{
int remainder = scratch.divEqualsReturningRemainder(f2lightSides[iFace].length);
int iEdge = f2lightSides[iFace][remainder];
net.fold(iEdge, true);
}
}
CHECK(scratch.eq(0));
return net;
} // selectFaceSweep_from_f2lightSides
public static Net selectVertSweepOrNull(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
BigInt iNet)
{
if (iNet.lt(0)) return null;
int priority2v[] = MeshSweepOrderUtils.compute_priority2v(sweepOriginHomo, mesh);
int v2eSweepward[][] = compute_v2eSweepward_from_primal_and_priority2v(mesh, sweepOriginHomo, priority2v);
BigInt nNets = countVertSweeps_from_v2eSweepward(v2eSweepward);
if (iNet.ge(nNets)) return null;
OUT("Vert sweep "+iNet+"/"+nNets); // XXX need to do this somewhere more graceful, in caller. but then caller needs to get n. think about it.
return selectVertSweep_from_priority2v_and_v2eSweepward(mesh, dualMesh, priority2v, v2eSweepward, iNet);
} // selectVertSweepOrNull
public static Net selectFaceSweepOrNull(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
BigInt iNet)
{
if (iNet.lt(0)) return null;
int priority2f[] = MeshSweepOrderUtils.compute_priority2f_from_dual(sweepOriginHomo, dualMesh);
int f2lightSides[][] = compute_f2lightSides_from_dual_and_priority2f(dualMesh, sweepOriginHomo, priority2f);
BigInt nNets = countFaceSweeps_from_f2lightSides(f2lightSides);
if (iNet.ge(nNets)) return null;
OUT("Face sweep "+iNet+"/"+nNets); // XXX need to do this somewhere more graceful, in caller. but then caller needs to get n. think about it.
return selectFaceSweep_from_priority2f_and_f2lightSides(mesh, dualMesh, priority2f, f2lightSides, iNet);
} // selectFaceSweepOrNull
public static Net selectNetOrNull(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
BigInt iNet)
{
OUT(" in selectNetOrNull");
int smartCuts[] = MeshUtils.selectSpanningTreeOrNull(mesh, dualMesh, iNet.i);
PRINTVEC(smartCuts);
//PRINTVEC(dumbCuts); // again
int dumbCuts[] = MeshUtils.selectSpanningTreeOrNullDumb(mesh, dualMesh, iNet);
PRINTVEC(dumbCuts);
PRINTVEC(smartCuts); // again
CHECK(VecMath.equals(dumbCuts, smartCuts));
OUT("WOOHOO!!!!!! SELECTED SAME MESH!!!!!");
if (smartCuts == null) return null;
int cuts[] = dumbCuts;
int nCuts = cuts.length;
CHECK_EQ(nCuts, mesh.verts.size()+1-1); // XXX assumes no underside... maybe get rid of this? or make it more lenient
Net net = new Net(mesh, dualMesh);
FORI (iCut, nCuts)
net.cut(cuts[iCut], true);
CHECK_EQ(net.nUndecideds(), 0);
OUT(" out selectNetOrNull");
return net;
} // selectNetOrNull
public static Net doItFaceSweep(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
int maxIters, // interpreted in funny way: on even iterations, sweep to next face but don't do anything with it.
int putProblematicVertsHereForDebugging[/*1*/][],
double putLastPointSweptHere[/*1*/][/*2*/],
java.util.ArrayList<Graphic> putGraphicsOfLastDecisionHere)
{
int verboseLevel = 1; // 1: constant, 2: linear
if (verboseLevel >= 1) OUT(" in MyAlgorithmMaybe.doItFaceSweep");
if (verboseLevel >= 1) OUT(" sweepOriginHomo = "+VecMath.toString(sweepOriginHomo));
CHECK_EQ(sweepOriginHomo.length, 3);
int nVerts = mesh.verts.size();
int nEdges = mesh.edges.size();
int nFaces = dualMesh.verts.size();
if (verboseLevel >= 1) OUT(" nVerts = "+nVerts);
if (verboseLevel >= 1) OUT(" nEdges = "+nEdges);
if (verboseLevel >= 1) OUT(" nFaces = "+nFaces);
if (nEdges == 0)
{
if (verboseLevel >= 1) OUT(" out MyAlgorithmMaybe.doIt (no edges, nothing to do)");
return new Net(mesh, dualMesh);
}
HelperDataStructure helperDataStructure = new HelperDataStructure(mesh, true);
double vertsXY[][] = helperDataStructure.vertsXY;
double v2momentAndArea[][] = helperDataStructure.v2momentAndArea;
double e2direction[][] = helperDataStructure.e2direction;
double e2normalizedDirection[][] = new double[nEdges][2];
FORI (iEdge, nEdges)
VecMath.normalize(2, e2normalizedDirection[iEdge], e2direction[iEdge]);
int e2v[][] = helperDataStructure.e2v;
int v2e[][] = helperDataStructure.v2e;
HelperDataStructure dualHelperDataStructure = new HelperDataStructure(dualMesh, false); // all verts are finite, for this one
double dualVertsXY[][] = dualHelperDataStructure.vertsXY;
int e2f[][] = dualHelperDataStructure.e2v;
int f2e[][] = dualHelperDataStructure.v2e;
int priority2f[] = MeshSweepOrderUtils.compute_priority2f_from_dual(sweepOriginHomo, dualMesh);
if (verboseLevel >= 2) OUT(" priority2f = "+VecMath.toString(priority2f));
int f2priority[] = VecMath.invertperm(priority2f);
{
// a bit of fudge... e2f and f2e have edges backwards from how I'd like them
int temp[];
FORI (iEdgePair, nEdges/2)
SWAP(e2f[2*iEdgePair], e2f[2*iEdgePair+1], temp);
FORI (iFace, f2e.length)
FORI (iSide, f2e[iFace].length)
f2e[iFace][iSide] ^= 1;
}
double v2momentAndWeightStrictlyUpstream[][] = new double[nVerts][3];
int v2parentEdge[] = VecMath.fillvec(nVerts, -1);
int v2parentVert[] = VecMath.fillvec(nVerts, -1); // redundant but convenient
// Note, f2parent isn't really needed for the algorithm,
// but it helps to visualize which folds have been decided so far
// when displaying a partial solution.
int f2parentEdge[] = VecMath.fillvec(nFaces, -1);
int f2parentFace[] = VecMath.fillvec(nFaces, -1); // redundant but convenient
// [1] = min(exitGoodness,0) // so first priority is to make exitGoodness >= 0
// [2] = sweepGoodness // tiebreaker, usually when both have exitGoodness >= 0
// [3] = exitGoodness // tiebreaker, seldom needed
double globalWorstGoodness[] = VecMath.fillvec(3, Double.POSITIVE_INFINITY);
double bestNeighborGoodness[] = new double[3]; // scratch for loop
double worstLagoonExitGoodness[] = new double[3]; // scratch for inner loop
double thisLagoonExitGoodness[] = new double[3]; // scratch for inner loop
boolean sideIsLight[] = new boolean[nEdges/2]; // scratch for loop
int lightSides[] = new int[nEdges/2]; // scratch for loop
int lightSideParts[][] = new int[2][nEdges/2]; // scratch for loop
int lightSidePartSizes[] = new int[2]; // scratch for loop
double scratchMomentAndArea[] = new double[3]; // scratch for loop
double scratchCentroid[] = new double[2]; // scratch for loop
double vertMinusCentroid[] = new double[2]; // scratch for loop
double sweepDirection[] = new double[2]; // scratch for loop
boolean isVeryFirstNontrivialFace = true;
int lastFaceConsidered = -1;
int iIter;
boolean fatalProblemDetected = false;
for (iIter = 0;
iIter/2 < nFaces && (maxIters<0 || iIter < maxIters) && !fatalProblemDetected;
++iIter)
{
// for each face, in order, in the sweep direction
int iFacePriority = iIter / 2;
int iFace = priority2f[iFacePriority];
lastFaceConsidered = iFace;
// On even iterations, we just sweep to the next face but don't do anything with it.
// This helps for a nicer visualization.
boolean justShowOptionsAndBreak = false;
if (iIter % 2 == 0)
{
if (maxIters >= 0 && iIter == maxIters-1)
justShowOptionsAndBreak = true; // have to proceed a bit farther to know the options
else
continue;
}
if (verboseLevel >= 2) OUT(" iIter="+iIter+": top of loop");
if (verboseLevel >= 2) OUT(" priority="+iFacePriority+" iFace="+iFace);
// Call the "light" ["dark"] sides of this face
// the sides that do [don't] border on previous faces.
int face[] = f2e[iFace];
int nSides = face.length;
if (nSides == 0)
{
// Isolated vert in original (our dual) leads to empty face in primal.
// Nothing to do.
if (verboseLevel >= 2) OUT(" (isolated)");
continue;
}
FORI (iSide, nSides)
{
int iEdge = face[iSide];
CHECK_EQ(e2f[iEdge][0], iFace);
int iNeighborFace = e2f[iEdge][1];
sideIsLight[iSide] = f2priority[iNeighborFace] < iFacePriority;
}
if (verboseLevel >= 2) OUT(" sideIsLight = "+VecMath.toString((boolean[])Arrays.subarray(sideIsLight,0,nSides)));
int iFirstLightSide = -1;
int iFirstDarkSide = -1;
{
boolean prevSideIsLight = sideIsLight[nSides-1];
FORI (iSide, nSides)
{
boolean thisSideIsLight = sideIsLight[iSide];
if (thisSideIsLight != prevSideIsLight)
{
if (thisSideIsLight)
{
CHECK_EQ(iFirstLightSide, -1);
iFirstLightSide = iSide;
}
else
{
CHECK_EQ(iFirstDarkSide, -1);
iFirstDarkSide = iSide;
}
}
prevSideIsLight = thisSideIsLight;
}
}
if (verboseLevel >= 2) OUT(" iFirstLightSide = "+iFirstLightSide);
if (verboseLevel >= 2) OUT(" iFirstDarkSide = "+iFirstDarkSide);
if (isVeryFirstNontrivialFace)
{
// should be all dark
CHECK_EQ(iFirstLightSide, -1);
CHECK_EQ(iFirstDarkSide, -1);
CHECK(!sideIsLight[0]);
f2parentEdge[iFace] = -1;
f2parentFace[iFace] = -1;
isVeryFirstNontrivialFace = false;
if (justShowOptionsAndBreak)
break;
}
else
{
if (iFirstLightSide == -1)
{
// TODO: this fails if symmetrical and no vertex at origin. should try to fix that.
CHECK(sideIsLight[0]);
// All sides are light.
// This is the last nontrivial face. (I think.)
// This face must be incident on the infinite vertex.
// Arrange so first light side is the one emanating from the infinite vertex.
FORI (iSide, nSides)
{
if (e2v[face[iSide]][0] == nVerts)
{
CHECK_EQ(iFirstLightSide, -1);
iFirstLightSide = iSide;
iFirstDarkSide = iSide;
if (verboseLevel >= 2) OUT(" iFirstLightSide = "+iFirstLightSide);
if (verboseLevel >= 2) OUT(" iFirstDarkSide = "+iFirstDarkSide);
}
}
CHECK_NE(iFirstLightSide, -1);
}
CHECK_NE(iFirstLightSide, -1);
CHECK_NE(iFirstDarkSide, -1);
int nLightSides = (iFirstDarkSide-iFirstLightSide+nSides-1) % nSides + 1;
if (verboseLevel >= 2) OUT(" nLightSides = "+nLightSides);
CHECK_LE_LE(0, nLightSides, nSides);
// Make linear list of light sides, for clarity
FORI (iLightSide, nLightSides)
{
int iSide = (iFirstLightSide+iLightSide) % nSides;
lightSides[iLightSide] = face[iSide];
if (iLightSide > 0)
{
CHECK_EQ(e2v[lightSides[iLightSide-1]][1],
e2v[lightSides[iLightSide]][0]);
}
}
if (verboseLevel >= 2) OUT(" lightSides = "+VecMath.toString((int[])Arrays.subarray(lightSides,0,nLightSides)));
if (justShowOptionsAndBreak)
{
FORI (iLightSide, nLightSides)
{
int iEdge = lightSides[iLightSide];
int i0 = e2v[iEdge][0];
int i1 = e2v[iEdge][1];
double v0[] = i0!=nVerts ? vertsXY[i0] : VecMath.vmv(vertsXY[i1], e2direction[iEdge]);
double v1[] = i1!=nVerts ? vertsXY[i1] : VecMath.vpv(vertsXY[i0], e2direction[iEdge]);
putGraphicsOfLastDecisionHere.add(new Graphic(
1, // iPass (after dual)
java.awt.Color.YELLOW.darker().darker(),
new double[][]{
v0,
v1,
}
));
}
break;
}
// Choose a parent face on the light side, to glue this face to.
int iBestLightSide = -1;
VecMath.fillvec(bestNeighborGoodness, Double.NEGATIVE_INFINITY);
FORI (iLightSide, nLightSides)
{
int iEdge = lightSides[iLightSide];
if (verboseLevel >= 2) OUT(" trying iLightSide="+iLightSide+": "+iEdge);
int iNeighborFace = e2f[iEdge][1];
// If we glue to this parent,
// the glued side will become a fold
// and all the other light sides will become cuts,
// each one forming a lagoon exit.
// Goodness of this potential parent
// will be the least goodness of all these lagoon exits.
// I.e. this parent isn't good (i.e. goodness >=0)
// unless all these lagoon exits are good.
VecMath.fillvec(worstLagoonExitGoodness, Double.POSITIVE_INFINITY);
lightSidePartSizes[0] = 0;
FORIDOWN(jLightSide, iLightSide)
lightSideParts[0][lightSidePartSizes[0]++] = lightSides[jLightSide] ^ 1; // backwards from the way we've been considering it
lightSidePartSizes[1] = 0;
for (int jLightSide = iLightSide+1; jLightSide < nLightSides; ++jLightSide)
lightSideParts[1][lightSidePartSizes[1]++] = lightSides[jLightSide];
FORI (jPart, 2)
{
if (verboseLevel >= 2) OUT(" "+(jPart==0 ? "first part, backwards" : "second part"));
int lightSidePart[] = lightSideParts[jPart];
int lightSidePartSize = lightSidePartSizes[jPart];
VecMath.zerovec(scratchMomentAndArea);
FORI (jLightSideInPart, lightSidePartSize)
{
int jEdge = lightSidePart[jLightSideInPart];
if (verboseLevel >= 2) OUT(" looking at jLightSideInPart="+jLightSideInPart+": "+jEdge);
int v0 = e2v[jEdge][0];
int v1 = e2v[jEdge][1];
// speculatively parent.
if (verboseLevel >= 2) OUT(" speculatively "+v0+"->"+v1);
CHECK_LT(v0, nVerts); // CBB: fails if "calc underside when delaunayizing"; should sanity check first or something?
CHECK_EQ(v2parentEdge[v0], -1);
CHECK_EQ(v2parentVert[v0], -1);
v2parentEdge[v0] = jEdge;
v2parentVert[v0] = v1;
computeMomentAndAreaStrictlyUpstreamFromUpstream(v0, e2v, v2e, v2momentAndArea, v2parentVert,
v2momentAndWeightStrictlyUpstream,
verboseLevel);
// check it
{
double exitGoodness = computeExitGoodness(vertsXY[v0],
v2momentAndWeightStrictlyUpstream[v0],
e2normalizedDirection[jEdge],
scratchCentroid,
vertMinusCentroid,
verboseLevel);
// Get sweepDirection: unit, or zero (if sweep origin is exactly at vert).
// sweepDirection = vertsXY[iVert] - sweepOrigin, homogeneous
VecMath.sxvpsxv(2, sweepDirection,
sweepOriginHomo[2], vertsXY[v0],
-1., sweepOriginHomo);
if (VecMath.normsqrd(2, sweepDirection) != 0.) // if vertex is not exactly on sweep origin
VecMath.normalize(2, sweepDirection,
sweepDirection);
double sweepGoodness;
{
// CBB: actually I think I should be measuring sweepGoodness at the end points? since that's the interface with other stuff.
sweepGoodness = VecMath.dot(2, e2normalizedDirection[jEdge], sweepDirection);
}
thisLagoonExitGoodness[0] = MIN(exitGoodness, 0.);
thisLagoonExitGoodness[1] = sweepGoodness;
thisLagoonExitGoodness[2] = exitGoodness;
if (verboseLevel >= 2) OUT(" thisLagoonExitGoodness = "+VecMath.toString(thisLagoonExitGoodness));
if (VecMath.cmp(thisLagoonExitGoodness, worstLagoonExitGoodness, 0.) < 0)
{
VecMath.copyvec(worstLagoonExitGoodness, thisLagoonExitGoodness);
}
}
}
}
if (verboseLevel >= 2) OUT(" thisNeighborGoodness = worstLagoonExitGoodness = "+VecMath.toString(worstLagoonExitGoodness));
double thisNeighborGoodness[] = worstLagoonExitGoodness; // clearer name
//CHECK_EQ((thisNeighborGoodness[0] < Double.POSITIVE_INFINITY), (nLightSides > 1)); // can't assert this because I play subtle games with infinity
if (VecMath.cmp(thisNeighborGoodness, bestNeighborGoodness, 0.) > 0)
{
VecMath.copyvec(bestNeighborGoodness, thisNeighborGoodness);
iBestLightSide = iLightSide;
}
// Clear speculative parenting of inter-light-side verts.
// Don't need to clear the speculative moments&weights,
// since those will get clobbered by future values as appropriate.
FORI (jLightSide, nLightSides-1)
{
int jEdge = lightSides[jLightSide];
int v1 = e2v[jEdge][1];
CHECK_NE(v2parentEdge[v1], -1);
CHECK_NE(v2parentVert[v1], -1);
v2parentEdge[v1] = -1;
v2parentVert[v1] = -1;
}
}
if (bestNeighborGoodness[0] < 0.)
{
if (verboseLevel >= 2) OUT(" OH NO!!! bestNeighborGoodness = "+VecMath.toString(bestNeighborGoodness));
}
else
{
if (verboseLevel >= 2) OUT(" bestNeighborGoodness = "+VecMath.toString(bestNeighborGoodness));
}
CHECK_GT(bestNeighborGoodness[0], Double.NEGATIVE_INFINITY);
CHECK_NE(iBestLightSide, -1);
if (VecMath.cmp(bestNeighborGoodness, globalWorstGoodness, 0.) < 0)
VecMath.copyvec(globalWorstGoodness, bestNeighborGoodness);
int iBestNeighborEdge = lightSides[iBestLightSide];
int iBestNeighborFace = e2f[iBestNeighborEdge][1];
if (verboseLevel >= 2) OUT(" bestLightSide = "+iBestLightSide+": "+iBestNeighborEdge+" to neighbor face "+iBestNeighborFace);
f2parentEdge[iFace] = iBestNeighborEdge;
f2parentFace[iFace] = iBestNeighborFace;
// That nails down some more of the vertex tree as well...
FORI (iPart, 2)
{
// First part: iBestLightSide-1 down to 0
// Second part: iBestLightSide+1 up to nLightSides-1
int iFirstLightSideInPart = (iPart==0 ? iBestLightSide-1 : iBestLightSide+1);
int iPastLastLightSideInPart = (iPart==0 ? -1 : nLightSides);
int incr = (iPart==0 ? -1 : 1);
for (int iLightSide = iFirstLightSideInPart;
iLightSide != iPastLastLightSideInPart;
iLightSide += incr) {
int iEdge = lightSides[iLightSide];
if (iPart == 0) iEdge ^= 1; // backwards;
int v0 = e2v[iEdge][0];
int v1 = e2v[iEdge][1];
if (verboseLevel >= 2) OUT(" parenting "+v0+"->"+v1);
v2parentEdge[v0] = iEdge;
v2parentVert[v0] = v1;
// since we're giving v0 a parent, we know its children are locked in
// so we can compute its moment.
computeMomentAndAreaStrictlyUpstreamFromUpstream(v0, e2v, v2e, v2momentAndArea, v2parentVert,
v2momentAndWeightStrictlyUpstream,
verboseLevel);
double exitGoodness = computeExitGoodness(vertsXY[v0],
v2momentAndWeightStrictlyUpstream[v0],
e2normalizedDirection[iEdge],
scratchCentroid,
vertMinusCentroid,
verboseLevel);
if (exitGoodness < 0.)
{
double v0coords[] = v0!=nVerts ? vertsXY[v0] : VecMath.vmv(vertsXY[v1], e2direction[iEdge]);
double v1coords[] = v1!=nVerts ? vertsXY[v1] : VecMath.vpv(vertsXY[v0], e2direction[iEdge]);
putGraphicsOfLastDecisionHere.add(new Graphic(
1, // iPass (after dual)
java.awt.Color.RED,
new double[][]{
v0coords,
v1coords,
}
));
}
}
}
} // iFacePriority > 0
} // for iFacePriority
if ((iIter/2 == nFaces || maxIters<0) && !fatalProblemDetected)
{
if (verboseLevel >= 1) OUT(" finished in "+iIter+" iterations!");
lastFaceConsidered = -1;
}
else
{
if (verboseLevel >= 1) OUT(" didn't finish in "+iIter+" iterations!");
}
// The result is now in v2parent.
if (verboseLevel >= 2) PRINTVEC(v2parentVert);
if (verboseLevel >= 2) PRINTVEC(v2parentEdge);
if (verboseLevel >= 2) PRINTVEC(f2parentEdge);
Net net = new Net(mesh, dualMesh);
FORI (iVert, nVerts)
{
if (v2parentEdge[iVert] != -1)
net.cut(v2parentEdge[iVert], true);
}
FORI (iFace, nFaces)
{
if (f2parentEdge[iFace] != -1)
net.fold(f2parentEdge[iFace], true);
}
if (lastFaceConsidered != -1 && putLastPointSweptHere != null)
{
putLastPointSweptHere[0] = dualVertsXY[lastFaceConsidered];
}
if (globalWorstGoodness[0] < 0.)
{
if (verboseLevel >= 1) OUT(" OH NO!!! globalWorstGoodness = "+VecMath.toString(globalWorstGoodness));
// I actually haven't seen this one fail yet,
// despite throwing stuff at it that has made every other algorithm
// fail (except the exit-swapping "polishing" non-algorithm heuristic
// which doesn't impress me).
// Oh wait! it does fail, on grid 8,10,11,...
// Argh, but it's by -5.5511e-17 :-(
//CHECK(false);
// And it really does fail, see "Face Sweep Killer" and "Simpler Face Sweep Killer" etc.
}
else
{
if (verboseLevel >= 1) OUT(" globalWorstGoodness = "+VecMath.toString(globalWorstGoodness));
}
if (verboseLevel >= 1) OUT(" out MyAlgorithmMaybe.doItFaceSweep");
return net;
} // doItFaceSweep
// Another attempt.
// This one is a bit more focused/restrictive:
// it tries to alter flow along only a single face that intersects the sweep line/circle.
// Assert-fails if it hits any vertices flowing from that face into the interior
// (which does happen sometimes, it turns out).
// I didn't realize it, but this is actually following largely the same logic
// as doItFaceSweep.
// (But there's a difference in how it handles that screwy case
// where something is a vertex sweep but not a face sweep: see SimplerFaceSweepKillerNotVertexSweepKiller.
// Yeah it succeeds on that one where face sweep fails.)
public static Net doIt2(
Mesh mesh, Mesh dualMesh, // generally reversed from the calling app's notion.
double sweepOriginHomo[/*3*/], // if [2] is 0, the vertex is infinitely far from origin
int maxIters, // interpreted in funny way: on even iterations, sweep to next vertex but don't do anything with it.
int putProblematicVertsHereForDebugging[/*1*/][],
double putLastPointSweptHere[/*1*/][/*2*/])
{
int verboseLevel = 1; // 1: constant, 2: linear
if (verboseLevel >= 1) OUT(" in MyAlgorithmMaybe.doIt2");
CHECK_EQ(sweepOriginHomo.length, 3);
int nVerts = mesh.verts.size();
int nEdges = mesh.edges.size();
if (verboseLevel >= 1) OUT(" nVerts = "+nVerts);
if (verboseLevel >= 1) OUT(" nEdges = "+nEdges);
if (nEdges == 0)
{
if (verboseLevel >= 1) OUT(" out MyAlgorithmMaybe.doIt2 (no edges, nothing to do)");
return new Net(mesh, dualMesh);
}
HelperDataStructure helperDataStructure = new HelperDataStructure(mesh, true);
double vertsXY[][] = helperDataStructure.vertsXY;
double v2momentAndArea[][] = helperDataStructure.v2momentAndArea;
double e2direction[][] = helperDataStructure.e2direction;