-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalfEdge.java
149 lines (126 loc) · 4.1 KB
/
HalfEdge.java
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
import java.util.Optional;
public class HalfEdge {
private final int id;
private Optional<Vertex> v;
private Optional<Face> f;
private Optional<HalfEdge> twin;
private Optional<HalfEdge> next;
private Optional<HalfEdge> prev;
HalfEdge(int id) {
this(id, Optional.<Vertex>empty(), Optional.<Face>empty(),
Optional.<HalfEdge>empty(), Optional.<HalfEdge>empty(), Optional.<HalfEdge>empty());
}
private HalfEdge(int id, Optional<Vertex> v, Optional<Face> f,
Optional<HalfEdge> twin, Optional<HalfEdge> next, Optional<HalfEdge> prev) {
this.id = id;
this.v = v;
this.f = f;
this.twin = twin;
this.next = next;
this.prev = prev;
}
void setVertex(Vertex v) {
this.v = Optional.<Vertex>of(v);
}
void setFace(Face f) {
this.f = Optional.<Face>of(f);
}
void setTwin(HalfEdge twin) {
this.twin = Optional.<HalfEdge>of(twin);
}
void setNext(HalfEdge next) {
this.next = Optional.<HalfEdge>of(next);
}
void setNext(Optional<HalfEdge> next) {
this.next = next;
}
void setPrev(HalfEdge prev) {
this.prev = Optional.<HalfEdge>of(prev);
}
void setPrev(Optional<HalfEdge> prev) {
this.prev = prev;
}
HalfEdge copy() {
return new HalfEdge(this.id, this.v, this.f,
this.twin, this.next, this.prev);
}
public int getId() {
return this.id;
}
public Optional<Vertex> getVertex() {
return this.v;
}
public Optional<Face> getFace() {
return this.f;
}
public Optional<HalfEdge> getTwin() {
return this.twin;
}
public Optional<HalfEdge> getNext() {
return this.next;
}
public Optional<HalfEdge> getPrev() {
return this.prev;
}
public double angle() {
if (this.next.isEmpty() || this.v.isEmpty() ||
this.next.get().v.isEmpty()) {
return 2 * Math.PI;
}
Vertex v1 = this.v.get();
Vertex v2 = this.next.get().v.get();
return v2.translateAngle(v1.angle() + Math.PI,
v1.distanceFromOrigin()).angle() % (2 * Math.PI);
}
public double angleBetween(HalfEdge other) {
if (this.next.isEmpty() || this.v.isEmpty() ||
this.next.get().v.isEmpty() ||
other.next.isEmpty() || other.v.isEmpty() ||
other.next.get().v.isEmpty()) {
return 2 * Math.PI;
}
double angle = other.angle() - this.angle();
if (angle < 0) {
angle += 2 * Math.PI;
}
return angle;
}
public double length() {
if (this.next.isEmpty() || this.v.isEmpty() ||
this.next.get().v.isEmpty()) {
return -1.0;
}
return this.v.get().distanceBetween(
this.next.get().v.get());
}
public Point midpoint() {
if (this.next.isEmpty() || this.next.get().v.isEmpty()) {
if (this.v.isEmpty()) {
return Point.origin();
}
return this.v.get().getPoint();
}
return this.v.get().midpoint(
this.next.get().v.get());
}
public Point lerp(double ratio) {
if (this.next.isEmpty() || this.next.get().v.isEmpty()) {
if (this.v.isEmpty()) {
return Point.origin();
}
return this.v.get().getPoint();
}
return this.v.get().lerp(
this.next.get().v.get(), ratio);
}
@Override
public String toString() {
String output = "Edge " + this.id + ": " +
"Vertex " + (this.v.isPresent() ? this.v.get().getId() + "" : "") + ", " +
"Face " + (this.f.isPresent() ? this.f.get().getId() + "" : "") + ", " +
"Twin " + (this.twin.isPresent() ? this.twin.get().getId() + "" : "") + ", " +
"Next " + (this.next.isPresent() ? this.next.get().getId() + "" : "") + ", " +
"Prev " + (this.prev.isPresent() ? this.prev.get().getId() + "" : "");
return output;
}
}