forked from SynapticWall/SynapticWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostsynapticPotential.pde
129 lines (121 loc) · 3.65 KB
/
PostsynapticPotential.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
class PostsynapticPotential extends Signal {
private float fEndParam;
private int fEndIndex;
private PVector fEndLoc;
PostsynapticPotential(float speed, float length, float decay, float strength, Path p) {
super((strength >= 0) ? EPSP : IPSP, speed, length, decay, strength, p);
setIndex(fCurrIndex);
}
public Signal makeCopy(Path p) {
return new PostsynapticPotential(fSpeed, fLength, fDecay, fStrength, p);
}
public void setIndex(int i) {
super.setIndex(i);
fEndLoc = Util.clone(fPath.getVertex(i));
fEndIndex = i;
fEndParam = 0;
}
private float calcDistanceTraveled() {
// TODO: amortized calculation needed
// BUG: this distance is incorrect for signals that get added through sub paths
float dist = 0;
PVector p = fPath.getVertex(0);
PVector p1;
for (int i = 1; i <= fCurrIndex; i++) {
p1 = fPath.getVertex(i);
dist += PVector.dist(p, p1);
p = p1;
}
dist += PVector.dist(fLoc, p);
return dist;
}
public void update() {
super.update();
// TODO: Should the signal stop decaying after initial firing?
// float time = Util.secondsElapsed(fBirthTime);
float time = float(millis() - fBirthTime)/1000;
float val;
if (SIGNAL_LINEAR_DECAY) {
val = Util.linear(- (1 - fDecay), 1, time, SIGNAL_DECAY_FACTOR);
}
else {
// No need to check for zero because fDecay will never to to 0
if (fDecay < 1)
val = Util.expDecay(1, time, lerp(0, SIGNAL_DECAY_FACTOR, fDecay));
else
val = fStrength;
}
fStrength = constrain(val, 0, 1);
if (fLength > 1 && calcDistanceTraveled() >= fLength) {
// Advance the end of the signal
fEndParam = advance(fEndIndex, fEndParam, fEndLoc);
if (fEndParam >= 1.0) {
// Move on to the next segment and reset parameter
fEndParam = fEndParam - 1;
fEndIndex += 1;
}
}
}
public boolean firingComplete() {
return super.firingComplete() && fEndIndex >= fFinalIndex;
}
private void drawSignal() {
if (gSmoothPaths) {
beginShape();
for (int i = fEndIndex + 1; i < fCurrIndex; i++) {
PVector p = fPath.getVertex(i);
curveVertex(p.x, p.y);
}
endShape();
}
else {
if (fCurrIndex == fEndIndex) {
if (gDebug) stroke(255, 0, 0);
line(fEndLoc.x, fEndLoc.y, fLoc.x, fLoc.y);
}
else if ((fCurrIndex - fEndIndex) == 1) {
if (gDebug) stroke(0, 255, 0);
PVector p = fPath.getVertex(fCurrIndex);
line(fEndLoc.x, fEndLoc.y, p.x, p.y);
line(p.x, p.y, fLoc.x, fLoc.y);
}
else {
if (gDebug) stroke(0, 0, 255);
PVector p = fPath.getVertex(fEndIndex + 1);
line(fEndLoc.x, fEndLoc.y, p.x, p.y);
beginShape();
for (int i = fEndIndex + 1; i <= fCurrIndex; i++) {
p = fPath.getVertex(i);
vertex(p.x, p.y);
}
endShape();
p = fPath.getVertex(fCurrIndex);
line(p.x, p.y, fLoc.x, fLoc.y);
}
}
}
public void drawForeground() {
pushStyle();
float n = abs(fStrength)/SIGNAL_MAX_STRENGTH;
color cc = lerpColor(fColor, EX_HIGHLIGHT_COLOR, n);
if (fLength > 1) {
noFill();
// Draw outer shape
stroke(fColor);
strokeWeight(3*PSP_BORDER_WIDTH);
drawSignal();
// Draw inner signal
stroke(lerpColor(fColor, EX_HIGHLIGHT_COLOR, n));
strokeWeight(PSP_BORDER_WIDTH);
drawSignal();
}
else {
fill(cc);
stroke(fColor);
strokeWeight(PSP_BORDER_WIDTH);
float s = PSP_WIDTH;
ellipse(fLoc.x, fLoc.y, s, s);
}
popStyle();
}
}