Permalink
Browse files

Refactoring

  • Loading branch information...
1 parent 7677872 commit 5f39370b411b80ee98523cbfd0b4b8d30e418a47 @yangsu committed May 9, 2012
Showing with 48 additions and 57 deletions.
  1. +1 −2 Path.pde
  2. +30 −38 PostsynapticPotential.pde
  3. +16 −16 Signal.pde
  4. +1 −1 Soma.pde
View
@@ -29,8 +29,7 @@ public abstract class Path extends Interactive implements Signalable{
public Path(Path p, float x, float y, color cc) {
this((Signalable)p, x, y, cc);
- PVector temp = p.fVertices.get(p.fCurrIndex);
- fSrcLoc = new PVector(temp.x, temp.y);
+ fSrcLoc = Util.clone(p.getCurrVertex());
}
public int size() {
View
@@ -1,20 +1,26 @@
class PostsynapticPotential extends Signal {
- private float fPrevParam;
- private int fPrevIndex;
- private PVector fPrevLoc;
+ private float fEndParam;
+ private int fEndIndex;
+ private PVector fEndLoc;
PostsynapticPotential(float speed, float length, float decay, float strength, Path p) {
super((strength >= 0) ? Constants.EPSP : Constants.IPSP, speed, length, decay, strength, p);
- fPrevLoc = Util.clone(fPath.getVertex(fCurrIndex));
- fPrevIndex = fCurrIndex;
- fPrevParam = fParam;
+ 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;
@@ -44,61 +50,47 @@ class PostsynapticPotential extends Signal {
val = fStrength;
}
fStrength = constrain(val, 0, 1);
- if (fLength > 1)
- determineRange();
- }
-
- public boolean firingComplete() {
- return super.firingComplete() && fPrevIndex >= fEndIndex;
- }
-
- private PVector smoothSegment(int index, float t) {
- PVector a = fPath.getVertex(index);
- PVector b = fPath.getVertex(index + 1);
- PVector c = fPath.getVertex(index + 2);
- PVector d = fPath.getVertex(index + 3);
- float x = curvePoint(a.x, b.x, c.x, d.x, t);
- float y = curvePoint(a.y, b.y, c.y, d.y, t);
- return new PVector(x, y);
- }
-
- private void determineRange() {
- if (calcDistanceTraveled() >= fLength) {
- fPrevParam = advance(fPrevIndex, fPrevParam, fPrevLoc);
- if (fPrevParam >= 1.0) {
+ 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
- fPrevParam = fPrevParam - 1;
- fPrevIndex += 1;
+ fEndParam = fEndParam - 1;
+ fEndIndex += 1;
}
}
}
+ public boolean firingComplete() {
+ return super.firingComplete() && fEndIndex >= fFinalIndex;
+ }
+
private void drawSignal() {
if (gSmoothPaths) {
beginShape();
- for (int i = fPrevIndex + 1; i < fCurrIndex; i++) {
+ for (int i = fEndIndex + 1; i < fCurrIndex; i++) {
PVector p = fPath.getVertex(i);
curveVertex(p.x, p.y);
}
endShape();
}
else {
- if (fCurrIndex == fPrevIndex) {
+ if (fCurrIndex == fEndIndex) {
if (gDebug) stroke(255, 0, 0);
- line(fPrevLoc.x, fPrevLoc.y, fLoc.x, fLoc.y);
+ line(fEndLoc.x, fEndLoc.y, fLoc.x, fLoc.y);
}
- else if ((fCurrIndex - fPrevIndex) == 1) {
+ else if ((fCurrIndex - fEndIndex) == 1) {
if (gDebug) stroke(0, 255, 0);
PVector p = fPath.getVertex(fCurrIndex);
- line(fPrevLoc.x, fPrevLoc.y, p.x, p.y);
+ 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(fPrevIndex + 1);
- line(fPrevLoc.x, fPrevLoc.y, p.x, p.y);
+ PVector p = fPath.getVertex(fEndIndex + 1);
+ line(fEndLoc.x, fEndLoc.y, p.x, p.y);
beginShape();
- for (int i = fPrevIndex + 1; i <= fCurrIndex; i++) {
+ for (int i = fEndIndex + 1; i <= fCurrIndex; i++) {
p = fPath.getVertex(i);
vertex(p.x, p.y);
}
View
@@ -1,7 +1,7 @@
abstract class Signal extends Drawable {
- public int fCurrIndex, fEndTime;
+ public int fCurrIndex, fFiringTime;
public float fSpeed, fLength, fDecay, fStrength;
- protected int fType, fEndIndex;
+ protected int fType, fFinalIndex;
protected Path fPath;
protected Signalable fDest;
protected boolean fFired;
@@ -10,47 +10,47 @@ abstract class Signal extends Drawable {
Signal() {}
Signal (int type, float speed, float length, float decay, float strength, Path p) {
- super(p.fVertices.get(0).x, p.fVertices.get(0).y, (type == Constants.EPSP) ? Constants.EX_COLOR : Constants.IN_COLOR);
+ super(p.getVertex(0).x, p.getVertex(0).y, (type == Constants.EPSP) ? Constants.EX_COLOR : Constants.IN_COLOR);
fType = type;
fSpeed = speed;
fLength = length;
fDecay = decay;
fStrength = strength;
fPath = p;
+
//fDest is private ?
fDest = p.fDest;
fCurrIndex = 0;
- fEndIndex = p.fVertices.size() - ((gSmoothPaths) ? 4 : 1);
- fEndTime = 0;
- fFired = false;
-
fParam = 0;
+ fFinalIndex = p.size() - ((gSmoothPaths) ? 4 : 1);
+
+ fFiringTime = 0;
+ fFired = false;
}
public int getType() {
return Constants.SIGNAL;
}
public void setIndex(int i) {
- fCurrIndex = constrain(i, 0, fEndIndex);
- PVector temp = fPath.fVertices.get(fCurrIndex);
- fLoc = new PVector(temp.x, temp.y, temp.z);
+ fCurrIndex = constrain(i, 0, fFinalIndex);
+ fLoc = Util.clone(fPath.getVertex(fCurrIndex));
}
protected void fire() {
if (!fFired) {
fDest.onSignal(this);
- fEndTime = millis();
+ fFiringTime = millis();
fFired = true;
}
}
protected float advance(int index, float param, PVector p) {
- PVector a = fPath.fVertices.get(index);
- PVector b = fPath.fVertices.get(index + 1);
+ PVector a = fPath.getVertex(index);
+ PVector b = fPath.getVertex(index + 1);
if (gSmoothPaths) {
- PVector c = fPath.fVertices.get(index + 2);
- PVector d = fPath.fVertices.get(index + 3);
+ PVector c = fPath.getVertex(index + 2);
+ PVector d = fPath.getVertex(index + 3);
float x = curvePoint(a.x, b.x, c.x, d.x, param);
float y = curvePoint(a.y, b.y, c.y, d.y, param);
p.set(x, y, 0);
@@ -72,7 +72,7 @@ abstract class Signal extends Drawable {
// Move on to the next segment and reset parameter
fParam = fParam - 1;
fCurrIndex += 1;
- if (fCurrIndex >= fEndIndex) {
+ if (fCurrIndex >= fFinalIndex) {
fire();
}
}
View
@@ -128,7 +128,7 @@ class Soma extends Cell {
int now = millis();
for (int i = fReceivedSignals.size()-1; i >= 0; --i) {
Signal s = fReceivedSignals.get(i);
- int diff = now - s.fEndTime;
+ int diff = now - s.fFiringTime;
float actualLength = s.fLength*Constants.SIGNAL_FIRING_MULTIPLIER + Constants.SIGNAL_SINGLE_FIRING_TIME;
if (diff > actualLength)
fReceivedSignals.remove(s);

0 comments on commit 5f39370

Please sign in to comment.