forked from SynapticWall/SynapticWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.pde
331 lines (294 loc) · 8.61 KB
/
Path.pde
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
public abstract class Path extends Interactive implements Signalable {
protected ArrayList<PVector> fVertices;
protected ArrayList<Signal> fSignals;
protected ArrayList<Path> fConnectedPaths;
protected int fCurrIndex;
protected Signalable fDest;
protected Signalable fSrc;
protected boolean fClosed, fSmooth;
// Find a way to not special case this
protected PVector fSrcLoc;
protected int fDestPos;
public Path(Signalable src, float x, float y, color cc) {
super(x, y, cc);
fConnectedPaths = new ArrayList<Path>();
fVertices = new ArrayList<PVector>();
fSignals = new ArrayList<Signal>();
fCurrIndex = 0;
fDest = null;
fSrc = src;
fClosed = false;
fSmooth = false;
fSrcLoc = new PVector(x,y);
fVertices.add(new PVector(x, y));
}
public Path(Path p, float x, float y, color cc) {
this((Signalable)p, x, y, cc);
fSrcLoc = Util.clone(p.getCurrVertex());
}
public int size() {
return fVertices.size();
}
public void close() {
fClosed = true;
}
public Signalable getSrc() {
return fSrc;
}
public Signalable getDest() {
return fDest;
}
public void setDest(Signalable obj) {
fDest = obj;
}
public void setDest(Path p) {
fDest = p;
fDestPos = p.fCurrIndex;
}
public void attachToSource() {
fSrc.addPath(this);
}
public ArrayList<Path> getConnectedPaths() {
return fConnectedPaths;
}
public PVector getCurrVertex() {
return fVertices.get(fCurrIndex);
}
public PVector getVertex(int i) {
if (i < fVertices.size())
return fVertices.get(i);
else {
println("Invalid access: " + i + "th element in a list of " + fVertices.size());
return null;
}
}
public void add(float x, float y) {
if (fClosed) return;
if (fVertices.size() > 0) {
PVector last = fVertices.get(fVertices.size() - 1);
if (last.x == x && last.y == y) return;
}
fVertices.add(new PVector(x, y));
}
public void add(PVector p) {
if (fClosed) return;
add(p.x, p.y);
}
public void addPath(Path p) {
fConnectedPaths.add(p);
}
public void translate(PVector change) {
if (fMovable)
for (PVector vertex : fVertices) {
vertex.add(change);
}
}
public void update() {
processSignals();
}
public void drawBackground() {
for (Signal s : fSignals)
s.drawBackground();
}
public void drawForeground() {
drawPath();
// for (PVector p : fVertices)
// drawJunction(p.x, p.y);
if (fHover)
drawJunction(getVertex(fCurrIndex));
for (Signal s : fSignals)
s.drawForeground();
}
protected void drawPathShape(float offsetx, float offsety) {
beginShape();
if (fSmooth) {
for (PVector p : fVertices) {
curveVertex(p.x + offsetx, p.y + offsety);
}
}
else {
for (PVector p : fVertices) {
vertex(p.x + offsetx, p.y + offsety);
}
}
endShape();
}
protected void drawPath() {
pushStyle();
noFill();
strokeWeight(PATH_WIDTH);
stroke((fHover) ? fHighlightColor : fColor);
drawPathShape(0, 0);
popStyle();
}
protected void drawJunction(float x, float y) {
pushStyle();
fill((fHover) ? fHighlightColor : fColor);
float s = PATH_JUNCTION_WIDTH;
ellipse(x, y, s, s);
popStyle();
}
protected void drawJunction(PVector p) {
drawJunction(p.x, p.y);
}
private void processSignals() {
for (int i = fSignals.size() - 1; i >= 0; --i) {
Signal curr = fSignals.get(i);
curr.update();
if (curr.firingComplete())
fSignals.remove(curr);
else {
//Combine adjacent signals
for (int j = i; j >= 0; --j) {
Signal s = fSignals.get(j);
if (s != curr &&
PVector.dist(s.fLoc, curr.fLoc) <= (abs(s.fStrength) + abs(curr.fStrength)) &&
s.fType != 0 && curr.fType != 0) {
PostsynapticPotential p = new PostsynapticPotential(
(s.fSpeed + curr.fSpeed)/2,
(s.fLength + curr.fLength)/2,
(s.fDecay + curr.fDecay)/2,
s.fStrength + curr.fStrength,
this);
p.setIndex(s.fCurrIndex);
addSignal(p);
fSignals.remove(curr);
fSignals.remove(s);
break;
}
}
//Copy Signals to connected paths
// TODO: Make threshold more precise/use specific signal width
float distThreshold = PATH_WIDTH;
for (Path p : fConnectedPaths) {
if (PVector.dist(p.fSrcLoc, curr.fLoc) <= distThreshold)
p.addSignal(curr.makeCopy(p));
}
}
}
}
public void addSignal(Signal s) {
fSignals.add(s);
}
public void onSignal(Signal s) {
Signal copy = s.makeCopy(this);
copy.setIndex(s.fPath.fDestPos);
addSignal(copy);
}
public boolean isInBounds(float x, float y) {
PVector mouse = new PVector(x,y);
PVector temp;
float mindist = MAX, dist;
for (int i = 0; i < fVertices.size(); ++i) {
temp = fVertices.get(i);
dist = PVector.dist(mouse, temp);
if (dist <= PATH_WIDTH/2 &&
dist <= mindist) {
fCurrIndex = i;
mindist = dist;
}
}
return mindist != MAX;
}
public void flipColor() {
super.flipColor();
for (Path p : fConnectedPaths)
p.flipColor();
}
private void simplify() {
if (fVertices.size() <= 2)
return;
boolean changed = true;
// Continue until no change
while (changed) {
changed = false;
PVector p1 = fVertices.get(0);
int i = 1;
while (i < fVertices.size() - 1) {
PVector p2 = fVertices.get(i);
PVector p3 = fVertices.get(i + 1);
PVector d1 = PVector.sub(p2, p1);
PVector d2 = PVector.sub(p3, p2);
PVector sum = PVector.add(d1, d2);
// if the 2 segments are
if (// vertical and then horizontal
(d1.x == 0 && d1.mag() == gGrid.getCellHeight() &&
d2.y == 0 && d2.mag() == gGrid.getCellWidth()) ||
// horizontal and then vertical
(d1.y == 0 && d1.mag() == gGrid.getCellWidth() &&
d2.x == 0 && d2.mag() == gGrid.getCellHeight()) ||
// overlap
sum.mag() == 0 || sum.x == 0 || sum.y == 0 || d2.mag() == 0) {
fVertices.remove(p2);
changed = true;
}
else if (// 2 consecutive diagonals not in the same direction
PVector.sub(d1, d2).mag() != 0 &&
d1.mag() == gGrid.getCellDiagonal() &&
d2.mag() == gGrid.getCellDiagonal()) {
PVector mid = PVector.mult(PVector.add(p1, p3), 0.5);
fVertices.get(i).set(mid);
}
else {
p1 = p2;
i += 1;
}
}
}
}
private void straightLine(PVector start, PVector end) {
PVector diff = PVector.sub(end, start);
float incx = diff.x/abs(diff.x) * gGrid.getCellWidth();
float incy = diff.y/abs(diff.y) * gGrid.getCellHeight();
int count = round(diff.mag() / gGrid.getCellDiagonal());
for (int i = 0; i < count; i++)
fVertices.add(new PVector(start.x + i*incx, start.y + i*incy));
}
private void recontructOptimal() {
if (fVertices.size() <= 2)
return;
PVector first = fVertices.get(0);
PVector last = fVertices.get(fVertices.size()-1);
PVector diff = PVector.sub(last, first);
float ax = abs(diff.x);
float ay = abs(diff.y);
fVertices.clear();
fVertices.add(first);
PVector curr = first;
int maxcount = ceil(diff.mag()/gGrid.getCellDiagonal() * 2);
while (PVector.sub(last, curr).mag() != 0 &&
fVertices.size() < maxcount) {
// if it's a straight diagonal
if (ax == ay || ax == 0 || ay == 0) {
straightLine(curr, last);
break;
}
else {
PVector inc = (ax > ay) ? new PVector(diff.x/ax * gGrid.getCellWidth(), 0)
: new PVector(0, diff.y/ay * gGrid.getCellHeight());
curr = PVector.add(curr, inc);
fVertices.add(curr);
}
diff = PVector.sub(last, curr);
ax = abs(diff.x);
ay = abs(diff.y);
}
}
public boolean onSmoothToggle(boolean smooth) {
if (fSmooth == smooth) return false;
fSmooth = smooth;
int last = fVertices.size() - 1;
if (fSmooth) {
// The first and last points in a series of curveVertex() lines will be
// used to guide the beginning and end of a the curve.
// So the first and last vertices need to be replicated
fVertices.add(0, fVertices.get(0));
fVertices.add(fVertices.get(last));
}
else {
fVertices.remove(last);
fVertices.remove(0);
}
return false;
}
}