Skip to content

Commit a00db70

Browse files
committed
can't remember all the changes :( added naive emst
1 parent 4e99696 commit a00db70

File tree

13 files changed

+631
-297
lines changed

13 files changed

+631
-297
lines changed

.idea/workspace.xml

Lines changed: 314 additions & 225 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/convex_dt/ConvexDT.java

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public class ConvexDT {
2929
private J2DScene scene;
3030
private DCEL dcel;
3131
private ArrayList<infCircleEnum> illegalValues;
32+
private double t = 0;
33+
34+
public double getTime() {
35+
return t;
36+
}
37+
38+
public void setTime(double t) {
39+
this.t = t;
40+
shape.setTime(t);
41+
}
3242

3343
public ConvexDT() {
3444
}
@@ -38,6 +48,7 @@ public ConvexDT(ArrayList<KDSPoint> points, ConvexShape shape, J2DScene scene) {
3848
this.shape = shape;
3949
this.scene = scene;
4050
this.dcel = new DCEL(scene, points);
51+
if (scene == null) this.dcel.setVisualize(false);
4152
illegalValues = new ArrayList<>();
4253
illegalValues.add(infCircleEnum.INSIDE);
4354
illegalValues.add(infCircleEnum.INVALID);
@@ -50,6 +61,9 @@ public DCEL getDcel() {
5061
public void setScene(J2DScene scene) {
5162
this.scene = scene;
5263
}
64+
public J2DScene getScene() {
65+
return scene;
66+
}
5367

5468
public HalfEdge computeSmallDelaunay(ArrayList<KDSPoint> points) throws Exception {
5569
if (points.size() == 2) {
@@ -59,7 +73,7 @@ public HalfEdge computeSmallDelaunay(ArrayList<KDSPoint> points) throws Exceptio
5973
KDSPoint b = points.get(1);
6074
HalfEdge e1 = dcel.createEdge(a, b);
6175
e1.setFace(dcel.createFace(e1));
62-
if (e1.getOrigin().getPoint(0).y() < e1.getDestination().getPoint(0).y())
76+
if (e1.getOrigin().getPoint(t).y() < e1.getDestination().getPoint(t).y())
6377
return e1;
6478
return e1.getTwin();
6579
} else if (points.size() == 3) {
@@ -140,8 +154,8 @@ else if (last != null) {
140154
// connect(e2, e1);
141155
// }
142156
// find the lowest point (y-coordinate only)
143-
dcel.draw(scene);
144-
try{Thread.sleep(5000);}catch (Exception ex){}
157+
//// dcel.draw(scene);
158+
try{Thread.sleep(0);}catch (Exception ex){}
145159
KDSPoint lowestPoint;
146160
if (a.getY() < b.getY()) lowestPoint = a;
147161
else lowestPoint = b;
@@ -169,9 +183,9 @@ infCircleEnum.INSIDE && lessThan(rNext(right).getOrigin(), right.getOrigin())) {
169183
right = rNext(right);
170184
}
171185
while (true) {
172-
right.draw(scene, 0, Color.BLACK);
173-
left.draw(scene, 0, Color.BLACK);
174-
try{Thread.sleep(100);}catch (Exception ex){}
186+
//// right.draw(scene, 0, Color.BLACK);
187+
//// left.draw(scene, 0, Color.BLACK);
188+
try{Thread.sleep(0);}catch (Exception ex){}
175189
if (shape.inInfCircle(right.getDestination(), left.getOrigin(), right.getOrigin()) == infCircleEnum.INSIDE) {
176190
// see below
177191
System.out.println("right");
@@ -197,10 +211,10 @@ infCircleEnum.INSIDE && lessThan(rNext(left).getOrigin(), left.getOrigin())) {
197211
left = rNext(left);
198212
}
199213
while (true) {
200-
right.draw(scene, 0, Color.ORANGE);
201-
left.draw(scene, 0, Color.ORANGE);
202-
try{Thread.sleep(100);}catch (Exception ex){}
203-
try {sleep(100);} catch (InterruptedException e) {}
214+
//// right.draw(scene, 0, Color.ORANGE);
215+
//// left.draw(scene, 0, Color.ORANGE);
216+
try{Thread.sleep(0);}catch (Exception ex){}
217+
try {sleep(0);} catch (InterruptedException e) {}
204218
// if rNext.org is inside the inf circle with left.org and right.org on boundary, then it's a better candidate
205219
if ((rNext(left) != null && illegalValues.contains(shape.inInfCircle(rNext(left).getOrigin(), left.getOrigin(), right.getOrigin())))
206220
&& illegalValues.contains(shape.inInfCircle(left.getDestination(), left.getOrigin(), right.getOrigin()))) {
@@ -325,8 +339,8 @@ public HalfEdge computeLcand() throws Exception {
325339
HalfEdge current = null, top = null, t = null;
326340
boolean foundLcand = false;
327341
lcand = rPrev(base);
328-
lcand.draw(scene, 0, Color.ORANGE);
329-
sleep(100);
342+
//// lcand.draw(scene, 0, Color.ORANGE);
343+
sleep(0);
330344
assert lcand != null;
331345
assert base != null;
332346

@@ -336,13 +350,13 @@ public HalfEdge computeLcand() throws Exception {
336350
delete(oPrev(lcand));
337351
if (produceONext(lcand) == null) break;
338352
lcand = produceONext(lcand);
339-
lcand.draw(scene, 0, Color.ORANGE);
340-
sleep(100);
353+
// lcand.draw(scene, 0, Color.ORANGE);
354+
sleep(0);
341355
}
342356
if (isValid(lcand)) delete(oPrev(lcand));
343357
else lcand = oPrev(lcand);
344-
lcand.draw(scene, 0, Color.ORANGE);
345-
sleep(100);
358+
// lcand.draw(scene, 0, Color.ORANGE);
359+
sleep(0);
346360
}
347361

348362
if (isValid(lcand)) {
@@ -390,10 +404,10 @@ public HalfEdge computeLcand() throws Exception {
390404
foundLcand = true;
391405
break;
392406
}
393-
lcand.draw(scene, 0, Color.ORANGE);
394-
sleep(100);
395-
current.draw(scene, 0, Color.YELLOW);
396-
sleep(100);
407+
// lcand.draw(scene, 0, Color.ORANGE);
408+
sleep(0);
409+
// current.draw(scene, 0, Color.YELLOW);
410+
sleep(0);
397411
System.out.println("Am I stuck in lcand?");
398412
}
399413
} else {
@@ -434,9 +448,9 @@ public HalfEdge computeRcand() throws Exception {
434448

435449
if (isValid(rcand)) {
436450
current = oPrev(rcand);
437-
rcand.draw(scene, 0, Color.green);
438-
current.draw(scene, 0, Color.BLUE);
439-
sleep(100);
451+
// rcand.draw(scene, 0, Color.green);
452+
// current.draw(scene, 0, Color.BLUE);
453+
sleep(0);
440454
top = rcand;
441455
while (true) {
442456
assert rcand != null;
@@ -501,6 +515,12 @@ public HalfEdge delaunay() throws Exception {
501515
}
502516

503517
public HalfEdge delaunay(ArrayList<KDSPoint> points) throws Exception {
518+
// kind of a hack to get updated positions without changing too much code
519+
for (KDSPoint p : points) {
520+
p.updatePosition(this.t);
521+
}
522+
Collections.sort(points);
523+
504524
if (points.size() < 4) return computeSmallDelaunay(points);
505525
else {
506526
int split = (int) Math.floor(points.size() / 2);
@@ -509,13 +529,13 @@ public HalfEdge delaunay(ArrayList<KDSPoint> points) throws Exception {
509529
ArrayList<KDSPoint> right = new ArrayList<>(points.subList(split, points.size()));
510530

511531
HalfEdge lleft = delaunay(left);
512-
lleft.draw(scene, 0, Color.PINK);
532+
// lleft.draw(scene, 0, Color.PINK);
513533
HalfEdge lright = delaunay(right);
514-
lright.draw(scene, 0, Color.PINK);
515-
//sleep(10000);
534+
// lright.draw(scene, 0, Color.PINK);
535+
//sleep(0);
516536
base = findLowerSupport(lleft, lright);
517-
base.draw(scene, 0, Color.black);
518-
//sleep(1000);
537+
// base.draw(scene, 0, Color.black);
538+
//sleep(0);
519539
boolean leftLower = lowerThan(lleft.getOrigin(), lright.getOrigin());
520540
HalfEdge lower;
521541
if (leftLower) {
@@ -529,41 +549,41 @@ public HalfEdge delaunay(ArrayList<KDSPoint> points) throws Exception {
529549
// merge step
530550
while (true) {
531551
boolean circleValid = true;
532-
base.draw(scene, 0, Color.BLACK);
533-
sleep(100);
552+
// base.draw(scene, 0, Color.BLACK);
553+
sleep(0);
534554
lcand = computeLcand();
535555
rcand = computeRcand();
536556
//System.out.println(lcand.getTwin() == rcand || lcand == rcand);
537557

538558
if (isValid(lcand)) System.out.println("lcand valid!");
539559
else System.out.println("lcand invalid!");
540-
lcand.draw(scene, 0, Color.MAGENTA);
541-
sleep(100);
560+
// lcand.draw(scene, 0, Color.MAGENTA);
561+
sleep(0);
542562
if (isValid(rcand)) System.out.println("rcand valid!");
543563
else System.out.println("rcand invalid!");
544-
rcand.draw(scene, 0, Color.CYAN);
545-
sleep(5000);
564+
// rcand.draw(scene, 0, Color.CYAN);
565+
sleep(0);
546566
if (isValid(lcand) && isValid(rcand)) {
547567
System.out.println("1");
548568
switch (shape.inCircle(base.getOrigin(), base.getDestination(), lcand.getDestination(), rcand.getDestination())) {
549569
case INSIDE:
550570
System.out.println("rcand.dest INSIDE");
551571
base = connectRight();
552-
base.draw(scene, 0, Color.BLACK);
553-
sleep(100);
572+
// base.draw(scene, 0, Color.BLACK);
573+
sleep(0);
554574
break;
555575
case ON:
556576
case ONBEFORE:
557577
case ONAFTER:
558578
System.out.println("rcand.dest ON/ONBEFORE/ONAFTER");
559579
if (rightOf(lcand.getOrigin(), lcand.getDestination(), rcand.getDestination())) {
560580
base = connectRight();
561-
base.draw(scene, 0, Color.BLACK);
562-
sleep(100);
581+
// base.draw(scene, 0, Color.BLACK);
582+
sleep(0);
563583
} else {
564584
base = connectLeft();
565-
base.draw(scene, 0, Color.BLACK);
566-
sleep(100);
585+
// base.draw(scene, 0, Color.BLACK);
586+
sleep(0);
567587
}
568588
break;
569589
case INVALID:
@@ -573,8 +593,8 @@ public HalfEdge delaunay(ArrayList<KDSPoint> points) throws Exception {
573593
default:
574594
System.out.println("rcand.dest OUTSIDE");
575595
base = connectLeft();
576-
base.draw(scene, 0, Color.BLACK);
577-
sleep(100);
596+
// base.draw(scene, 0, Color.BLACK);
597+
sleep(0);
578598
break;
579599
}
580600
}
@@ -586,7 +606,7 @@ else if (isValid(rcand)) {
586606
System.out.println("3");
587607
base = connectRight();
588608
}
589-
else if (shape.inInfCircle(rcand.getDestination(), base.getOrigin(), base.getDestination()) == infCircleEnum.AFTER) {
609+
else if (shape.inInfCircle(lcand.getDestination(), base.getOrigin(), base.getDestination()) == infCircleEnum.BEFORE) {
590610
System.out.println("4");
591611
while (shape.inInfCircle(lcand.getDestination(), lcand.getOrigin(), rcand.getDestination()) == infCircleEnum.INSIDE) {
592612
lcand = rPrev(lcand);

src/main/java/convex_dt/run.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex_dt;
22

33
import ProGAL.geom2d.viewer.J2DScene;
4+
import convex_dt.shapes.CircleShape;
45
import convex_dt.shapes.ConvexShape;
56
import convex_dt.shapes.PieShape;
67
import convex_dt.shapes.ZeroTriShape;
@@ -145,7 +146,7 @@ public static void main(String[] args) throws Exception {
145146
points.add(new KDSPoint(new double[]{1.413363682993329},new double[]{-1.498918100553464}));
146147
points.add(new KDSPoint(new double[]{1.7485472199546614},new double[]{-0.9006378901043264}));
147148

148-
} else if (true) {
149+
} else if (false) {
149150
points.add(new KDSPoint(new double[]{1.5279150010373113},new double[]{0.1998488094646622}));
150151
points.add(new KDSPoint(new double[]{1.593594590368593},new double[]{-1.320766348583168}));
151152
points.add(new KDSPoint(new double[]{1.5969757062078913},new double[]{-0.5331873245177161}));
@@ -179,7 +180,7 @@ public static void main(String[] args) throws Exception {
179180
scene.autoZoom();
180181
scene.repaint();
181182

182-
ConvexShape circle = new PieShape(scene);
183+
ConvexShape circle = new CircleShape(scene);
183184

184185
ConvexDT dt = new ConvexDT(points, circle, scene);
185186

src/main/java/convex_dt/shapes/CircleShape.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
*/
1313
public class CircleShape implements ConvexShape {
1414
private J2DScene scene;
15+
private double t;
16+
17+
public double getTime() {
18+
return t;
19+
}
20+
21+
public void setTime(double t) {
22+
this.t = t;
23+
}
1524

1625
@Override
1726
public J2DScene getScene() {

src/main/java/convex_dt/shapes/ConvexShape.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ enum infCircleEnum {
3535
enum circleEnum {
3636
INSIDE, OUTSIDE, ONBEFORE, ONAFTER, ON, INVALID
3737
}
38+
39+
void setTime(double t);
40+
double getTime();
3841
}

src/main/java/convex_dt/shapes/PieShape.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
*/
2323
public class PieShape implements ConvexShape {
2424
private J2DScene scene;
25+
private double t;
26+
27+
public double getTime() {
28+
return t;
29+
}
30+
31+
public void setTime(double t) {
32+
this.t = t;
33+
}
2534

2635
@Override
2736
public J2DScene getScene() {
@@ -217,7 +226,7 @@ else if (!isAbove(c, b, -60) && c.getX() >= b.getX() && !isBelow(c, b, 30)) {
217226
scene.addShape(line_c, Color.RED);
218227
scene.repaint();
219228
try {
220-
Thread.sleep(100);
229+
Thread.sleep(5000);
221230
} catch (InterruptedException ex) {}
222231
scene.removeShape(line_b);
223232
scene.removeShape(line_c);

src/main/java/convex_dt/shapes/ZeroTriShape.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
*/
2020
public class ZeroTriShape implements ConvexShape {
2121
private J2DScene scene;
22+
private double t;
23+
24+
public double getTime() {
25+
return t;
26+
}
27+
28+
public void setTime(double t) {
29+
this.t = t;
30+
}
2231

2332
public J2DScene getScene() {
2433
return scene;

src/main/java/dcel/DCEL.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ public class DCEL {
1818
private ArrayList<HalfEdge> edges;
1919
private ArrayList<KDSPoint> vertices;
2020
private J2DScene scene;
21+
private boolean visualize;
2122

2223
public DCEL(J2DScene scene, ArrayList<KDSPoint> vertices) {
2324
faces = new ArrayList<>();
2425
edges = new ArrayList<>();
2526
this.vertices = vertices;
2627
this.scene = scene;
28+
this.visualize = false;
29+
}
30+
31+
public boolean isVisualizing() {
32+
return visualize;
33+
}
34+
35+
public void setVisualize(boolean visualize) {
36+
this.visualize = visualize;
2737
}
2838

2939
public ArrayList<Face> getFaces() {
@@ -74,7 +84,7 @@ public HalfEdge connect(HalfEdge a, HalfEdge b) {
7484
c.setTwin(c_twin);
7585
c_twin.setTwin(c);
7686
edges.add(c);
77-
edges.add(c_twin);
87+
//edges.add(c_twin);
7888

7989
// Handling a
8090

@@ -200,11 +210,13 @@ public void deleteEdge(HalfEdge e) {
200210
edges.remove(e);
201211
edges.remove(e.getTwin());
202212

203-
e.undraw(scene);
204-
e.getTwin().undraw(scene);
205-
scene.repaint();
213+
if (visualize) {
214+
e.undraw(scene);
215+
e.getTwin().undraw(scene);
216+
scene.repaint();
217+
}
206218
System.out.println("INFO: Deleted an edge.");
207-
try {sleep(100);} catch (InterruptedException ex) {}
219+
try {sleep(0);} catch (InterruptedException ex) {}
208220
}
209221

210222
private void updateIncidentEdges(HalfEdge e) {
@@ -241,7 +253,7 @@ private void createTwin(HalfEdge e) {
241253
if (e.getDestination().getIncidentEdge() == null)
242254
e.getDestination().setIncidentEdge(twin);
243255
//twin.setFace(createFace(e));
244-
edges.add(twin);
256+
//edges.add(twin);
245257
e.setTwin(twin);
246258
twin.setTwin(e);
247259
}

0 commit comments

Comments
 (0)