-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.prejava
More file actions
2408 lines (2228 loc) · 102 KB
/
Mesh.prejava
File metadata and controls
2408 lines (2228 loc) · 102 KB
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"
import com.donhatchsw.util.Arrays;
import com.donhatchsw.util.LinearProgramming;
import com.donhatchsw.util.TriangulationOptimizer;
import com.donhatchsw.util.VecMath;
public class Mesh
{
private static int nonEventVerbose = 0; // so static classes can refer to it. CBB: not hooked up to anything any more (used to be in ShephardsPlayApplet)
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(""+verts.size()+" verts:\n");
FORI (iVert, verts.size())
{
Vertex vert = verts.get(iVert);
sb.append(" "+vert);
if (vert.weight != 1.)
{
sb.append(" * weight="+vert.weight); // I don't even remember what this is at the moment. for dual verts, it seems to be tri area, i.e. 1/2 W coord.
}
sb.append("\n");
}
sb.append(""+edges.size()+" edges:\n");
FORI (iEdge, edges.size())
{
Edge edge = edges.get(iEdge);
Vertex v0 = edge.initialVertex();
Vertex v1 = edge.finalVertex();
sb.append(" "+edge+"\n");
}
if (false)
{
// CBB: should do something like in DUMP.off
sb.append("faces (sort of):\n");
FORI (iEdge, edges.size())
{
}
}
return sb.toString();
}
public class Vertex
{
/*
X,Y,Z,W homo
x,y,z actual
h = z + (x^2+y^2)/2 = height above paraboloid z=-(x^2+y^2)/2 which has curvature 1 at origin
x = X/W
y = Y/W
z = Z/W
h = H/W^2
X = x*W
Y = y*W
Z = z*W
H = h*W^2
migration strategy:
a zillion callers assumed w=1 before and not clear
whether they really want x or X.
those have all been changed to:
Xnaive()
setXnaive()
and should be consciously changed to x()/setx() or X()/setXnaive()
as desired.
*/
private double max3abs(double a, double b, double c)
{
return Math.max(Math.max(Math.abs(a), Math.abs(b)), Math.abs(c));
}
// ISSUE: this assumes paraboloid. Will fail if sphere.
public void sanityCheckCoords(double tol)
{
// important to use relative error since absolute error will fail when zoomed way in.
CHECK_ALMOST_EQ_REL(_Z, (_H - .5 * (_X*_X + _Y*_Y)) / _W, tol);
CHECK_ALMOST_EQ_REL(_H, _Z*_W + .5 * (_X*_X + _Y*_Y), tol);
CHECK_ALMOST_EQ_REL(h(), z() + (SQR(x())+SQR(y()))/2., tol);
if (momentAndArea != null)
{
// momentAndArea should be exactly X,Y,H,W.
CHECK_EQ(momentAndArea.length, 4);
CHECK_EQ(momentAndArea[0], _X);
CHECK_EQ(momentAndArea[1], _Y);
CHECK_EQ(momentAndArea[2], _H);
CHECK_EQ(momentAndArea[3], _W);
}
}
private double _X;
private double _Y;
private double _Z;
private double _W; // homogeneous "thing to divide by"
private double _H; // height above the paraboloid z = -(x^2+y^2)/2 (has curvature 1 at origin). not all that meaningful when reference surface is sphere instead of that paraboloid.
// TODO: can weight be merged with w? I think currently they are always the same for dual verts, but maybe not for primals sometimes, when unwrapped/unwrapped from sphere. in that case w can convey infinite position
public double weight = 1.; // optional, if this is a computed dual vertex, this should be the original triangle area (projected to xy plane I think)
public double momentAndArea[/*4*/] = null; // optional... similar to weight. x*A,y*A,h*A^2,A where A is twice tri area (projected to xy plane I think)
public int arity = 0; // number of edges having this vertex as initial vertex
private int _myIndex = -1;
public Vertex(double x, double y, double h)
{
_W = 1.;
this.setXYHnaive(x,y,h);
verts.add(this);
fixVertIndices(verts.size()-1);
}
public Vertex(double X, double Y, double Z, double W)
{
this.setXYZW(X,Y,Z,W);
verts.add(this);
fixVertIndices(verts.size()-1);
}
public Vertex(double X, double Y, double Zignored, double W, double H) // Zignored is just to distinguish prototype from others
{
this.setXYHW(X,Y,H,W);
verts.add(this);
fixVertIndices(verts.size()-1);
}
// A Vertex can belong to only one Mesh.
// myIndex() returns the index into the Mesh's edges.
public int myIndex()
{
//return Arrays.indexOfUsingEqualsSymbol(verts, this);
return _myIndex;
}
public double X() { return _X; }
public double Y() { return _Y; }
public double Z() { return _Z; }
public double H() { return _H; }
public double W() { return _W; }
// XXX TODO: but what about when _W is 0? isn't that a legit case? I think that shows we shouldn't really ever be calling these! or, protect with W()!=0. first or something
public double x() { return _X/_W; }
public double y() { return _Y/_W; }
public double z() { return _Z/_W; }
public double h() { return _H/SQR(_W); } // seems to work, not quite sure of the logic
// TODO: maybe get rid of this?, it's just setXYHW(x,y,h,1.)
public void setxyh(double x, double y, double h) // calculate z
{
setXYHW(x, y, h, 1.);
// note, momentAndArea is now wrong
}
public void setXYZW(double X, double Y, double Z, double W) // calculate H
{
_X = X;
_Y = Y;
_Z = Z;
_W = W;
_H = _Z*_W + .5 * (_X*_X+_Y*_Y);
// note, momentAndArea is now wrong
}
public void setXYHW(double X, double Y, double H, double W) // calculate Z; unfortunate divide
{
_X = X;
_Y = Y;
_H = H;
_W = W;
_Z = (_H - .5 * (_X*_X+_Y*_Y)) / _W; // can be Inf or NaN
// note, momentAndArea is now wrong
}
//
// "Naive" setting functions,
// that set h in terms of z or vice versa.
// These require _W is 1 beforehand.
//
public void setXYHnaive(double X, double Y, double H) // infer Z
{
CHECK_EQ(_W, 1.);
_X = X;
_Y = Y;
_H = H;
_Z = _H - .5 * (_X*_X + _Y*_Y);
}
public void setXYZnaive(double X, double Y, double Z) // infer H
{
CHECK_EQ(_W, 1.);
_X = X;
_Y = Y;
_Z = Z;
_H = _Z + .5 * (_X*_X + _Y*_Y);
}
public void setXYnaive(double X, double Y) // leave H fixed, adjust Z
{
CHECK_EQ(_W, 1.);
_X = X;
_Y = Y;
_Z = _H - .5 * (_X*_X + _Y*_Y);
}
public void setXnaive(double X) // leave Y,H fixed, adjust Z
{
CHECK_EQ(_W, 1.);
_X = X;
_Z = _H - .5 * (_X*_X + _Y*_Y);
}
public void setYnaive(double Y) // leave X,H fixed, adjust Z
{
CHECK_EQ(_W, 1.);
_Y = Y;
_Z = _H - .5 * (_X*_X + _Y*_Y);
}
public void setHnaive(double H) // leave X,Y fixed, adjust Z
{
CHECK_EQ(_W, 1.);
_H = H;
_Z = _H - .5 * (_X*_X + _Y*_Y);
}
public void setZnaive(double Z) // leave X,Y fixed, adjust H
{
CHECK_EQ(_W, 1.);
_Z = Z;
_H = _Z + .5 * (_X*_X + _Y*_Y);
}
public double Xnaive()
{
CHECK_EQ(_W, 1.);
return X();
}
public double Ynaive()
{
CHECK_EQ(_W, 1.);
return Y();
}
public double Znaive()
{
CHECK_EQ(_W, 1.);
return Z();
}
public double Hnaive()
{
CHECK_EQ(_W, 1.);
return H();
}
public double Wnaive()
{
CHECK_EQ(_W, 1.);
return W();
}
public String toString()
{
// not too attached to this format
return "v"+myIndex()+" = ("+X()+" "+Y()+" "+Z()+" "+W()+" H="+H()+")"
+ " = "
+ "("+x()+" "+y()+" "+z()+" h="+h()+")" ;
}
} // public class Vertex
public class Edge
{
private Vertex _initialVertex;
private Edge _opposite; // opposite edge
private Edge _next; // next edge CCW around the face to my left
private Edge _prev; // prev edge (next edge CW) around the face to my left
private int _myIndex = -1;
public double direction[] = null; // optional, not kept up to date by mesh editing operations, really used only in dual, which is constructed and not edited
public Edge(boolean andBack)
{
this._myIndex = -1; // will get fixed
edges.add(this);
fixEdgeIndices(edges.size()-1);
if (andBack)
{
Edge opposite = new Edge(/*andBack=*/false);
this._opposite = opposite;
opposite._opposite = this;
// CBB: actually should maybe do this directly here, then everything else can be in terms of splices and can assert sanity? not sure
setNext(opposite, true);
setPrev(opposite, true);
}
}
//
// Accessor methods...
//
public Vertex initialVertex()
{
return _initialVertex;
}
public Edge opposite()
{
return _opposite;
}
// next edge CCW around the face to my left
public Edge next()
{
return _next;
}
// prev edge (next edge CW) around the face to my left
public Edge prev()
{
return _prev;
}
//
// Helper methods...
// From the caller's point of view,
// these are just like the accessor methods
//
public Vertex finalVertex()
{
return _opposite._initialVertex;
}
// An Edge can belong to only one Mesh.
// myIndex() returns the index into the Mesh's edges.
public int myIndex()
{
//return Arrays.indexOfUsingEqualsSymbol(edges, this);
return _myIndex;
}
public void setInitialVertex(Vertex v)
{
CHECK_EQ(_initialVertex, null); // initial vertex can't be changed (wait, what? that can't be true. but if it is changed, that must be in conjunction with something else)
_initialVertex = v;
_initialVertex.arity++;
}
public void setNext(Edge next, boolean andBack)
{
_next = next;
if (andBack)
next.setPrev(this, false);
}
public void setPrev(Edge prev, boolean andBack)
{
_prev = prev;
if (andBack)
prev.setNext(this, false);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("e");
sb.append(myIndex());
sb.append(" (");
if (initialVertex() != null)
{
sb.append("v");
sb.append(initialVertex().myIndex());
}
else
sb.append("null");
sb.append("->");
if (finalVertex() != null)
{
sb.append("v");
sb.append(finalVertex().myIndex());
}
else
sb.append("null");
sb.append(")");
return sb.toString();
}
} // public class Edge
// Called whenever verts array changes
private void fixVertIndices(int i0)
{
for (int iVert = i0; iVert < verts.size(); ++iVert)
verts.get(iVert)._myIndex = iVert;
}
// Called whenever edges array changes
private void fixEdgeIndices(int i0)
{
for (int iEdge = i0; iEdge < edges.size(); ++iEdge)
edges.get(iEdge)._myIndex = iEdge;
}
public void sanityCheckCoords(double tol)
{
int nVerts = this.verts.size();
FORI (iVert, nVerts)
{
Vertex vert = this.getVert(iVert);
vert.sanityCheckCoords(tol);
}
}
public void sanityCheckTopology()
{
int nVerts = this.verts.size();
int nEdges = this.edges.size();
Vertex verts[] = new Vertex[nVerts];
Edge edges[] = new Edge[nEdges];
FORI (iVert, nVerts)
{
Vertex vert = this.getVert(iVert);
CHECK_EQ(vert.myIndex(), iVert);
verts[iVert] = vert;
}
FORI (iEdge, nEdges)
{
Edge edge = this.getEdge(iEdge);
CHECK_EQ(edge.myIndex(), iEdge);
edges[iEdge] = edge;
}
int nTimesSawVertAsInitial[] = new int[nVerts]; // all zero
boolean sawEdgeAsOpposite[] = new boolean[nEdges]; // all false
boolean sawEdgeAsNext[] = new boolean[nEdges]; // all false
boolean sawEdgeAsPrev[] = new boolean[nEdges]; // all false
FORI (iEdge, nEdges)
{
Edge e = edges[iEdge];
Vertex initialVertex = e.initialVertex();
Edge opposite = e.opposite();
Edge next = e.next();
Edge prev = e.prev();
CHECK_NE(initialVertex, null);
CHECK_NE(opposite, null);
CHECK_NE(next, null);
CHECK_NE(prev, null);
int initialVertIndex = initialVertex.myIndex();
int oppositeIndex = opposite.myIndex();
int nextIndex = next.myIndex();
int prevIndex = prev.myIndex();
CHECK_EQ(verts[initialVertIndex], initialVertex);
CHECK_EQ(edges[oppositeIndex], opposite);
CHECK_EQ(edges[nextIndex], next);
CHECK_EQ(edges[prevIndex], prev);
CHECK_NE(initialVertex, e.finalVertex());
CHECK_NE(opposite, e);
CHECK_NE(next, e);
CHECK_NE(prev, e);
CHECK_EQ(opposite.opposite(), e);
CHECK_EQ(next.prev(), e);
CHECK_EQ(prev.next(), e);
// Okay for a vert to be initial more than once...
nTimesSawVertAsInitial[initialVertIndex]++;
// But not the others...
CHECK(!sawEdgeAsOpposite[oppositeIndex]);
sawEdgeAsOpposite[oppositeIndex] = true;
CHECK(!sawEdgeAsNext[nextIndex]);
sawEdgeAsNext[nextIndex] = true;
CHECK(!sawEdgeAsPrev[prevIndex]);
sawEdgeAsPrev[prevIndex] = true;
}
FORI (iVert, nVerts)
{
CHECK_EQ(nTimesSawVertAsInitial[iVert], verts[iVert].arity);
}
FORI (iEdge, nEdges)
{
CHECK(sawEdgeAsOpposite[iEdge]);
CHECK(sawEdgeAsNext[iEdge]);
CHECK(sawEdgeAsPrev[iEdge]);
}
} // sanityCheckTopology
/* ^
insertThisEdge | beforeThisEdge
<-----------------*
/|\
/ | \
*/
public void insertEdgeBefore(Edge insertThisEdge, Edge beforeThisEdge)
{
if (nonEventVerbose >= 1)
System.out.println(" In insertEdgeBefore(insertThisEdge="+insertThisEdge.myIndex()+", beforeThisEdge="+beforeThisEdge.myIndex()+"");
CHECK_EQ(insertThisEdge.prev(), insertThisEdge.opposite()); // CBB: really need to be using general splice
beforeThisEdge.prev().setNext(insertThisEdge, true);
insertThisEdge.opposite().setNext(beforeThisEdge, true);
insertThisEdge.setInitialVertex(beforeThisEdge.initialVertex());
if (nonEventVerbose >= 1)
System.out.println(" Out insertEdgeBefore(insertThisEdge="+insertThisEdge.myIndex()+", beforeThisEdge="+beforeThisEdge.myIndex()+"");
}
public void addWhisker(Edge beforeThisEdge,
Vertex v) // to this vertex
{
Edge e = new Edge(/*andBack=*/true);
e.opposite().setInitialVertex(v);
e.setNext(e.opposite(), true);
insertEdgeBefore(e, beforeThisEdge);
}
public void addDiagonal(Edge beforeThisEdge, Edge beforeThatEdge)
{
if (nonEventVerbose >= 1)
System.out.println(" In addDiagonal(beforeThisEdge="+beforeThisEdge.myIndex()+", beforeThatEdge="+beforeThatEdge.myIndex()+"");
Edge e = new Edge(/*andBack=*/true);
insertEdgeBefore(e, beforeThisEdge);
insertEdgeBefore(e.opposite(), beforeThatEdge);
if (nonEventVerbose >= 1)
System.out.println(" Out addDiagonal(beforeThisEdge="+beforeThisEdge.myIndex()+", beforeThatEdge="+beforeThatEdge.myIndex()+"");
}
// returns true iff did something
public boolean swapDiagonal(Edge e, boolean onlyIfMoreDelaunay, boolean verbose)
{
if (e.next().next().next() != e
|| e.opposite().next().next().next() != e.opposite())
{
if (verbose)
System.out.println(" Can't swap diagonal, it's not on a quad");
return false;
}
Edge beforeThisEdge = e.prev();
Edge beforeThatEdge = e.opposite().prev();
//
// Another subtle thing to check...
// Make sure the other diagonal doesn't
// already exist.
//
{
Vertex v0 = beforeThisEdge.initialVertex();
Vertex v1 = beforeThatEdge.initialVertex();
// Could do this faster by walking around the vert,
// but this is more complete since it checks for some pathological cases
FORIDOWN (iEdge, edges.size())
{
Edge edge = edges.get(iEdge);
if (edge.initialVertex() == v0
&& edge.finalVertex() == v1)
{
if (verbose)
System.out.println(" Can't swap diagonal, the other diagonal already exists");
return false;
}
}
}
if (onlyIfMoreDelaunay)
{
Vertex a = beforeThisEdge.finalVertex();
Vertex b = beforeThatEdge.initialVertex();
Vertex c = beforeThatEdge.finalVertex();
Vertex d = beforeThisEdge.initialVertex();
// comfort of ac relative to bd
double comfort = TriangulationOptimizer.calcQuadComfort(new double[]{a.Xnaive(),a.Ynaive()},
new double[]{b.Xnaive(),b.Ynaive()},
new double[]{c.Xnaive(),c.Ynaive()},
new double[]{d.Xnaive(),d.Ynaive()},
0, 1);
// XXX TODO: take height into account!!!!!
if (verbose)
System.out.println(" comfort = "+comfort);
if (comfort >= 0) // should this be fuzzy? I won't worry about it for now
{
if (verbose)
System.out.println(" Swapping diagonal would not improve it");
return false;
}
}
if (true)
{
// This works, but leaves dual verts in a different order
deleteEdge(e);
addDiagonal(beforeThisEdge, beforeThatEdge);
}
else
{
// TODO: why did this work in temp branch but fails here?
// OH, it's because the assertion in setInitialVertex is new.
// hmm, can try to fix it, or just get rid of this case since it was somewhat ill-advised anyway.
// Aww fooey, still in different order :-(
beforeThatEdge.setNext(beforeThisEdge.prev(), true);
beforeThisEdge.setNext(beforeThatEdge.prev(), true);
insertEdgeBefore(e, beforeThisEdge);
insertEdgeBefore(e.opposite(), beforeThatEdge);
// no need to fix up edge indices
}
return true;
} // swapDiagonal
public void addEdgeBetweenTwoIsolatedVertices(Vertex v0, Vertex v1)
{
Edge e = new Edge(/*andBack=*/true);
e.setInitialVertex(v0);
e.opposite().setInitialVertex(v1);
e.setNext(e.opposite(), true);
e.setPrev(e.opposite(), true);
}
// kind of automagic. probably not what we want for algebraic operations like delta-wye stuff.
public void addEdgeBetweenTwoVertices(Vertex v0, Vertex v1)
{
CHECK_NE(v0, v1);
int nEdges = edges.size();
// First find any edge beginning at v0,
// and any beginning at v1
Edge someEdgeStartingAtV0 = null;
Edge someEdgeStartingAtV1 = null;
FORI (iEdge, nEdges)
{
Edge e = edges.get(iEdge);
if (e.initialVertex() == v0)
{
someEdgeStartingAtV0 = e;
if (e.finalVertex() == v1)
return; // v0,v1 already connected-- definitely don't do anything!
}
else if (e.initialVertex() == v1)
{
someEdgeStartingAtV1 = e;
// no need to check whether final is v0, we'll catch it in the other direction
}
}
// Then walk around ccw to find the *best* place to insert the edge
// among v0's edges and among v1's edges
if (someEdgeStartingAtV1 != null)
{
// figure out the best place among v1's edges
// to add v0.
double bestGoodness = Double.NEGATIVE_INFINITY;
Edge bestBeforeThisEdge = null;
double ang = Math.atan2(v0.Ynaive()-v1.Ynaive(), v0.Xnaive()-v1.Xnaive());
// for each edge out of v1, ccw
Edge nextEdgeOutOfV1 = null;
for (Edge thisEdgeOutOfV1 = someEdgeStartingAtV1; ; thisEdgeOutOfV1 = nextEdgeOutOfV1)
{
// XXX dup code!
nextEdgeOutOfV1 = thisEdgeOutOfV1.prev().opposite();
//PRINT(thisEdgeOutOfV1);
//PRINT(nextEdgeOutOfV1);
Vertex thisNeighbor = thisEdgeOutOfV1.finalVertex();
Vertex nextNeighbor = nextEdgeOutOfV1.finalVertex();
double thisAng = Math.atan2(thisNeighbor.Ynaive()-v1.Ynaive(),
thisNeighbor.Xnaive()-v1.Xnaive());
// change thisAng by multiples of 2*Math.PI
// so that it's >= ang by as little as possible
// XXX not sure this is robust... can something fall between the cracks?
while (thisAng < ang) thisAng += 2*Math.PI;
while (thisAng > ang) thisAng -= 2*Math.PI;
double thisGoodness = -(ang-thisAng); // want to minimize ang-thisAng, so maximize -(ang-thisAng)
//PRINT(RTOD(thisGoodness));
if (thisGoodness > bestGoodness)
{
bestBeforeThisEdge = thisEdgeOutOfV1;
bestGoodness = thisGoodness;
}
if (nextEdgeOutOfV1 == someEdgeStartingAtV1)
break;
}
CHECK_NE(bestBeforeThisEdge, null);
System.out.println(" Sticking v"+v0.myIndex()+" between v"+bestBeforeThisEdge.finalVertex().myIndex()+" and v"+bestBeforeThisEdge.prev().opposite().finalVertex().myIndex()+" in v"+v1.myIndex()+"'s neighbors");
someEdgeStartingAtV1 = bestBeforeThisEdge;
}
if (someEdgeStartingAtV0 != null)
{
// figure out the best place among v0's edges
// to add v1.
double bestGoodness = Double.NEGATIVE_INFINITY;
Edge bestBeforeThisEdge = null;
double ang = Math.atan2(v1.Ynaive()-v0.Ynaive(), v1.Xnaive()-v0.Xnaive());
// for each edge out of v0, ccw
Edge nextEdgeOutOfV0 = null;
for (Edge thisEdgeOutOfV0 = someEdgeStartingAtV0; ; thisEdgeOutOfV0 = nextEdgeOutOfV0)
{
// XXX dup code!
nextEdgeOutOfV0 = thisEdgeOutOfV0.prev().opposite();
//PRINT(thisEdgeOutOfV0);
//PRINT(nextEdgeOutOfV0);
Vertex thisNeighbor = thisEdgeOutOfV0.finalVertex();
Vertex nextNeighbor = nextEdgeOutOfV0.finalVertex();
double thisAng = Math.atan2(thisNeighbor.Ynaive()-v0.Ynaive(),
thisNeighbor.Xnaive()-v0.Xnaive());
// change thisAng by multiples of 2*Math.PI
// so that it's >= ang by as little as possible
// XXX not sure this is robust... can something fall between the cracks? think about it
while (thisAng < ang) thisAng += 2*Math.PI;
while (thisAng > ang) thisAng -= 2*Math.PI;
double thisGoodness = -(ang-thisAng); // want to minimize ang-thisAng, so maximize -(ang-thisAng)
//PRINT(RTOD(thisGoodness));
if (thisGoodness > bestGoodness)
{
bestBeforeThisEdge = thisEdgeOutOfV0;
bestGoodness = thisGoodness;
}
if (nextEdgeOutOfV0 == someEdgeStartingAtV0)
break;
}
CHECK_NE(bestBeforeThisEdge, null);
System.out.println(" Sticking v"+v1.myIndex()+" between v"+bestBeforeThisEdge.finalVertex().myIndex()+" and v"+bestBeforeThisEdge.prev().opposite().finalVertex().myIndex()+" in v"+v0.myIndex()+"'s neighbors");
someEdgeStartingAtV0 = bestBeforeThisEdge;
}
if (someEdgeStartingAtV0 != null)
if (someEdgeStartingAtV1 != null)
addDiagonal(someEdgeStartingAtV0,
someEdgeStartingAtV1);
else
addWhisker(someEdgeStartingAtV0, v1);
else
if (someEdgeStartingAtV1 != null)
addWhisker(someEdgeStartingAtV1, v0);
else
addEdgeBetweenTwoIsolatedVertices(v0, v1);
} // addEdgeBetweenTwoVertices
// twice area (of projection in XY plane) of the face obtained by walking around the edge.
public double twiceFaceArea(Edge e0)
{
double sum = 0;
Vertex v0 = e0.initialVertex();
// All edges e not containing v0...
for (Edge e = e0.next(); e.finalVertex() != v0; e = e.next())
{
Vertex v1 = e.initialVertex();
Vertex v2 = e.finalVertex();
// TODO: this dangerous. should be using a different method to show what's inside out!
sum += GeomUtils.twiceTriangleArea(v0.x(), v0.y(),
v1.x(), v1.y(),
v2.x(), v2.y());
}
return sum;
} // twiceFaceArea
// XXX redundant with newVertex. get rid?
public void addIsolatedVertex(double x, double y, double h)
{
new Vertex(x,y,h); // adds itself to verts
}
public void addIsolatedVertex(double X, double Y, double Z, double W)
{
new Vertex(X,Y,Z,W); // adds itself to verts
}
// This is the "kis" operation, the dual of truncating a vertex.
public void kisIsolatedVertex(Vertex v, Edge someEdgeOnFace)
{
if (nonEventVerbose >= 1)
System.out.println("In kis(v="+v.myIndex()+", someEdgeOnFac="+someEdgeOnFace.myIndex()+"");
//
// Start by attaching a single whisker to the new vertex...
//
if (nonEventVerbose >= 1)
System.out.println(" adding a whisker from edge "+someEdgeOnFace.myIndex()+" to vert "+v.myIndex());
addWhisker(someEdgeOnFace, v);
if (nonEventVerbose >= 1)
{
System.out.println(" did that at least.");
PRINT(this);
}
//
// Now fill in with triangles,
// connecting everything to the new vertex v.
//
while (someEdgeOnFace.next()
.next()
.next() != someEdgeOnFace)
{
if (nonEventVerbose >= 1)
System.out.println(" adding diagonal from start of edge "+someEdgeOnFace.prev().myIndex()+" to start of edge "+someEdgeOnFace.prev().prev().prev().myIndex()+"");
addDiagonal(someEdgeOnFace.prev(),
someEdgeOnFace.prev()
.prev()
.prev());
if (nonEventVerbose >= 1)
{
System.out.println(" did that diagonal.");
PRINT(this);
}
}
if (nonEventVerbose >= 1)
System.out.println("Out kis(v="+v.myIndex()+", someEdgeOnFac="+someEdgeOnFace.myIndex()+"");
}
public void deleteEdge(Edge e) // and opposite
{
// TODO: use totally generic splice operation?
e.prev().setNext(e.opposite().next(), true);
e.opposite().prev().setNext(e.next(), true);
// these don't hold, currently, but might if we use splice
//CHECK_EQ(e.next(), e.opposite());
//CHECK_EQ(e.prev(), e.opposite());
int i = e.myIndex();
edges.remove(i);
fixEdgeIndices(i);
int j = e.opposite().myIndex();
edges.remove(j);
fixEdgeIndices(j);
e._initialVertex.arity--;
e._initialVertex = null;
e.opposite()._initialVertex.arity--;
e.opposite()._initialVertex = null;
}
public void deleteVertex(Vertex v, int oldToNew[], int newToOld[])
{
// CBB: don't need to traverse all edges, if v.arity is 0, I think?
FORI (iEdge, edges.size())
{
Edge edge = edges.get(iEdge);
if (edge.initialVertex() == v
|| edge.finalVertex() == v)
{
deleteEdge(edge);
iEdge--; // so when we increment we get back to here
}
}
CHECK_EQ(v.arity, 0);
int iVert = v.myIndex();
verts.remove(iVert);
fixVertIndices(iVert);
// Should we do the fast remove, moving the last one into the slot?
// Well, is there an advantage to keeping the vertices in order? Probably.
// adjust oldToNew and newToOld, assuming *not* fastRemove
if (oldToNew != null)
{
CHECK_NE(newToOld, null);
int nVertsNew = verts.size();
oldToNew[newToOld[iVert]] = -1; // using previous newToOld values
for (int jVert = iVert; jVert < nVertsNew; ++jVert)
oldToNew[newToOld[jVert+1]] = jVert; // using previous newToOld values
}
if (newToOld != null)
{
int nVertsNew = verts.size();
for (int jVert = iVert; jVert < nVertsNew; ++jVert)
newToOld[jVert] = newToOld[jVert+1];
newToOld[nVertsNew] = -1;
}
} // deleteVertex
// XXX TODO: this shouldn't need graphics, just the xform
public int indexOfClosestVert(double x, double y, MyGraphics3D mg3d)
{
double closestDistSqrd = Double.POSITIVE_INFINITY;
int closestVertIndex = -1;
double scratch[] = new double[3];
FORI (iVert, verts.size()) // beginning to end, so we choose first of equal
{
Vertex vert = verts.get(iVert);
mg3d.xform(vert.X(), vert.Y(), vert.Z(), vert.W(), scratch);
double thisDistSqrd = SQR(scratch[0]-x)
+ SQR(scratch[1]-y);
if (thisDistSqrd < closestDistSqrd)
{
closestDistSqrd = thisDistSqrd;
closestVertIndex = iVert;
}
}
return closestVertIndex; // -1 if none
} // indexOfClosestVert
public int indexOfClosestEdge(double point[/*2*/], boolean isDualMesh)
{
double x = point[0], y = point[1];
double closestDistSqrd = Double.POSITIVE_INFINITY;
int closestEdgeIndex = -1;
FORI (iEdge, edges.size()) // beginning to end, so we choose first of equal
{
Edge edge = edges.get(iEdge);
double thisDistSqrd =
edge.initialVertex() == null ? Double.POSITIVE_INFINITY : // no need to deal with edges whose initial vertex is NULL and final vertex isn't, since we'll encounter its opposite anyway
edge.finalVertex() == null ? GeomUtils.distSqrdFromPointToRay(x,y,
edge.initialVertex().x(),
edge.initialVertex().y(),
edge.direction[0],
edge.direction[1]) :
edge.initialVertex().myIndex() > edge.finalVertex().myIndex() ? Double.POSITIVE_INFINITY
: GeomUtils.distSqrdFromPointToSeg(x,y,
edge.initialVertex().x(),
edge.initialVertex().y(),
edge.finalVertex().x(),
edge.finalVertex().y());
if (thisDistSqrd < closestDistSqrd)
{
closestDistSqrd = thisDistSqrd;
closestEdgeIndex = iEdge;
}
}
if (closestEdgeIndex != -1)
{
// Make sure point is on left side of edge.
// If not, choose edge's opposite instead.
Edge edge = edges.get(closestEdgeIndex);
double edgeDirection[] = edge.direction;
if (edgeDirection == null) // currently it's null for the primal, non-null for dual
{
Vertex v0 = edge.initialVertex();
Vertex v1 = edge.finalVertex();
edgeDirection = new double[]{v1.x()-v0.x(), v1.y()-v0.y()};
}
Vertex v = edge.initialVertex();
if (v == null) v = edge.finalVertex();
double vToPoint[] = {point[0]-v.x(), point[1]-v.y()};
if (VecMath.vxv2(edgeDirection, vToPoint) * (isDualMesh ? -1. : 1.) < 0.)
{
closestEdgeIndex = edge.opposite().myIndex();
}
}
return closestEdgeIndex; // -1 if none
} // indexOfClosestEdge
// TODO: This stuff doesn't belong here, move it to MeshUtil or something
public static final int OPTIMIZE_BARYCENTRICS_LEAST_SQUARES = 0;
public static final int OPTIMIZE_BARYCENTRICS_INFINITY_NORM = 1;
public static final int OPTIMIZE_BARYCENTRICS_MAX_MIN = 2;
public static final int OPTIMIZE_LENGTHS_LEAST_SQUARES = 3;
public static final int OPTIMIZE_LENGTHS_INFINITY_NORM = 4;
public static final int OPTIMIZE_LENGTHS_MAX_MIN = 5;
public static final int OPTIMIZE_LENGTHS_SUM = 6;
public static final String optimizationTypeNames[] = {
"BARYCENTRICS_LEAST_SQUARES from all 1/3's",
"BARYCENTRICS_INFINITY_NORM from all 1/3's",
"BARYCENTRICS_MAX_MIN",
"LENGTHS_LEAST_SQUARES from all .25's",
"LENGTHS_INFINITY_NORM from all .25's",
"LENGTHS_MAX_MIN with fixed sum",
//"LENGTHS_SUM with every edge length >= .25",
};
public static final int availableOptimizationTypes[] = {
OPTIMIZE_BARYCENTRICS_LEAST_SQUARES,
OPTIMIZE_BARYCENTRICS_INFINITY_NORM,
OPTIMIZE_BARYCENTRICS_MAX_MIN,
OPTIMIZE_LENGTHS_LEAST_SQUARES,
OPTIMIZE_LENGTHS_INFINITY_NORM,
OPTIMIZE_LENGTHS_MAX_MIN,
//OPTIMIZE_LENGTHS_SUM,
};
public double[] findOptimalVertexHeights(int optimizationType)
{
if (verts.size() < 3)
return new double[verts.size()]; // zeros
if (nonEventVerbose >= 1)
System.out.println("in findOptimalVertexHeights");
int nDependentVerts = 3;
int dependentVertIndices[] = {0,1,2};
// Prefer the triangle that contains the origin...
{
FORI (iEdge, edges.size())
{
Edge edge = edges.get(iEdge);
if (edge.next().next().next() != edge)
continue;
Vertex v0 = edge.initialVertex();
Vertex v1 = edge.next().initialVertex();
Vertex v2 = edge.next().next().initialVertex();
if (GeomUtils.twiceTriangleArea(0,0,
v0.Xnaive(),v0.Ynaive(),
v1.Xnaive(),v1.Ynaive()) >= 0
&& GeomUtils.twiceTriangleArea(0,0,
v1.Xnaive(),v1.Ynaive(),
v2.Xnaive(),v2.Ynaive()) >= 0
&& GeomUtils.twiceTriangleArea(0,0,
v2.Xnaive(),v2.Ynaive(),
v0.Xnaive(),v0.Ynaive()) >= 0)
{
dependentVertIndices[0] = v0.myIndex();
dependentVertIndices[1] = v1.myIndex();
dependentVertIndices[2] = v2.myIndex();
break; // origin is inside this tri CCW
}
}
}
// Figure out the desired dualEdgeLength...
// XXX just hard code for now
double desiredDualEdgeLength = .25;