Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- new Class "ForceVector" to manipulate forces in physical algorithms

- new Abstract Class "ForceCalc" to defines physical force calculation algorithms
- new Class implementation "ForceCalcLegacyPerson" to store the legacy algorithm for force calculation between persons

Work In Progress !

git-svn-id: http://codeswarm.googlecode.com/svn/trunk@176 eda9c206-d64f-0410-a2f2-67f3fa0499ed
  • Loading branch information...
commit 3aeb648be30604874a7f6f2568de4b289f48cf5f 1 parent eeedf72
sebastien.rombauts authored
View
2  data/sample.config
@@ -59,7 +59,7 @@ ShowHistory=false
ShowDate=true
# Show edges between authors and files, mostly for debug purpose
-ShowEdges=true
+ShowEdges=false
# Turn on Debug counts.
debug=false
View
81 src/ColorBins.java
@@ -1,47 +1,50 @@
-/*
- Copyright 2008 Michael Ogawa
-
- This file is part of code_swarm.
-
- code_swarm is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- code_swarm is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
+/**
+ * Copyright 2008 Michael Ogawa
+ *
+ * This file is part of code_swarm.
+ *
+ * code_swarm is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * code_swarm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
*/
import processing.core.PApplet;
+/**
+ * @brief Definition of the colored histogram elements
+ */
class ColorBins
{
- int [] colorList;
- int num;
-
- ColorBins()
- {
- colorList = new int[2];
- num = 0;
- }
-
- public void add( int c )
- {
- if ( num >= colorList.length )
- colorList = PApplet.expand( colorList );
-
- colorList[num] = c;
- num++;
- }
-
- public void sort()
- {
- colorList = PApplet.sort( colorList );
- }
+ int [] colorList;
+ int num;
+
+ ColorBins()
+ {
+ colorList = new int[2];
+ num = 0;
+ }
+
+ public void add( int c )
+ {
+ if ( num >= colorList.length )
+ colorList = PApplet.expand( colorList );
+
+ colorList[num] = c;
+ num++;
+ }
+
+ public void sort()
+ {
+ colorList = PApplet.sort( colorList );
+ }
}
View
39 src/ForceCalc.java
@@ -0,0 +1,39 @@
+/**
+ * Copyright 2008 code_swarm project team
+ *
+ * This file is part of code_swarm.
+ *
+ * code_swarm is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * code_swarm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+/**
+ * Abstract base of any force calculation in code_swarm.
+ *
+ * Need to be derived to define calculation a type of forces between to Nodes
+ */
+abstract class ForceCalc
+{
+ /**
+ * Method that calculate the force between to nodes.
+ *
+ * @param NodeA
+ * @param NodeB
+ * @param force
+ *
+ * @return a forceVector representing the force between to nodes
+ */
+ abstract void calculateForceBetween( code_swarm.Node NodeA, code_swarm.Node NodeB, ForceVector force );
+}
+
View
61 src/ForceCalcLegacyPerson.java
@@ -0,0 +1,61 @@
+/**
+ * Copyright 2008 code_swarm project team
+ *
+ * This file is part of code_swarm.
+ *
+ * code_swarm is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * code_swarm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+/**
+ * @brief Legacy force calculation between persons.
+ */
+public class ForceCalcLegacyPerson extends ForceCalc
+{
+ final private double randMultiplier;
+
+ /**
+ * Constructor for initializing parameters.
+ */
+ ForceCalcLegacyPerson(double paramMultiplier)
+ {
+ randMultiplier = paramMultiplier;
+ }
+
+ /**
+ * Legacy method that calculate the force between to Person nodes.
+ *
+ * @param[in] NodeA
+ * @param[in] NodeB
+ * @param[out] force
+ *
+ * @return a forceVector representing the force between to nodes
+ */
+ public void calculateForceBetween( code_swarm.Node NodeA, code_swarm.Node NodeB, ForceVector force )
+ {
+ float distx, disty;
+ float lensq;
+
+ /** @todo comment this algorithm */
+ distx = NodeA.x - NodeB.getX();
+ disty = NodeA.y - NodeB.getY();
+ lensq = distx * distx + disty * disty;
+ if (lensq == 0) {
+ force.set( Math.random()*randMultiplier, Math.random()*randMultiplier );
+ } else if (lensq < 10000) {
+ force.set( distx / lensq, disty / lensq );
+ }
+ }
+}
+
View
123 src/ForceVector.java
@@ -0,0 +1,123 @@
+/**
+ * Copyright 2008 code_swarm project team
+ *
+ * This file is part of code_swarm.
+ *
+ * code_swarm is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * code_swarm is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+/**
+ * A small object to manipulate multidimensional force vector (2D).
+ *
+ * Used by physical classes
+ *
+ * @remark currently 2D, but one day, 3D would be great
+ */
+class ForceVector
+{
+ protected double x, y;
+ protected double norm;
+
+ ForceVector()
+ {
+ x = 0;
+ y = 0;
+ }
+ /**
+ * Constructor, init the vector and calculate its norm.
+ *
+ * @param x x-axis component of the force
+ * @param y y-axis component of the force
+ */
+ ForceVector(double x, double y)
+ {
+ this.x = x;
+ this.x = x;
+ }
+
+ public void setX(double x)
+ {
+ this.x = x;
+ }
+
+ public void setY(double y)
+ {
+ this.y = y;
+ }
+
+ public void set(double x, double y)
+ {
+ this.x = x;
+ this.y = y;
+ }
+
+ public void set(ForceVector force)
+ {
+ this.x = force.getX();
+ this.y = force.getY();
+ }
+
+ public void add(double x, double y)
+ {
+ this.x += x;
+ this.y += y;
+ }
+
+ public void add(ForceVector force)
+ {
+ this.x += force.getX();
+ this.y += force.getY();
+ }
+
+ /**
+ * @return xx x-axis component of the force.
+ */
+ public double getX()
+ {
+ return x;
+ }
+
+ /**
+ * @return y-axis component of the force.
+ */
+ public double getY()
+ {
+ return y;
+ }
+
+ /**
+ * @return calculated norm of the vector (ie. its length).
+ */
+ public double norm()
+ {
+ return Math.sqrt(x*x + y*y);
+ }
+
+ /**
+ * multiply each component by the multiplier, recalculates the resulting norm.
+ *
+ * @param multiplier
+ *
+ * @todo transform to an operator redefinition
+ */
+ void multiply(double multiplier)
+ {
+ x *= multiplier;
+ y *= multiplier;
+ norm *= multiplier;
+ }
+
+}
+
View
154 src/code_swarm.java
@@ -15,11 +15,6 @@
*
* You should have received a copy of the GNU General Public License
* along with code_swarm. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * @file code_swarm.java
- *
- * @brief Definition of the code_swarm Application
*/
import processing.core.PApplet;
@@ -37,6 +32,9 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.PriorityBlockingQueue;
+/**
+ * Definition of the code_swarm Application.
+ */
public class code_swarm extends PApplet {
public static final long serialVersionUID = 0;
@@ -47,7 +45,7 @@
String SPRITE_FILE = "particle.png";
String SCREENSHOT_FILE;
int background;
-
+
// Data storage
PriorityBlockingQueue<FileEvent> eventsQueue; // USE PROCESSING 0142 or higher
CopyOnWriteArrayList<FileNode> nodes;
@@ -101,7 +99,7 @@
private String loadingMessage = "Reading input file";
/**
- * @brief Initialization
+ * Initialization
*/
public void setup() {
int width=cfg.getIntProperty(CodeSwarmConfig.WIDTH_KEY,640);
@@ -233,7 +231,7 @@ public void run() {
}
/**
- * @brief Load a colormap
+ * Load a colormap
*/
public void initColors() {
colorAssigner = new ColorAssigner();
@@ -282,7 +280,7 @@ public void eclipseColors() {
}
/**
- * @brief Main loop
+ * Main loop
*/
public void draw() {
long start = System.currentTimeMillis();
@@ -345,7 +343,7 @@ else if (showLegend) {
}
/**
- * @brief Surround names with aura
+ * Surround names with aura
*/
public void drawPeopleNodesBlur() {
colorMode(HSB);
@@ -362,7 +360,7 @@ public void drawPeopleNodesBlur() {
}
/**
- * @brief Draw person's name
+ * Draw person's name
*/
public void drawPeopleNodesSharp() {
colorMode(RGB);
@@ -374,7 +372,7 @@ public void drawPeopleNodesSharp() {
}
/**
- * @brief Draw date in lower-right corner
+ * Draw date in lower-right corner
*/
public void drawDate() {
fill(255);
@@ -385,7 +383,7 @@ public void drawDate() {
}
/**
- * @brief Draw histogram in lower-left
+ * Draw histogram in lower-left
*/
public void drawHistory() {
Iterator<ColorBins> itr = history.iterator();
@@ -412,7 +410,7 @@ public void drawLoading() {
}
/**
- * @brief Show color codings
+ * Show color codings
*/
public void drawLegend() {
noStroke();
@@ -428,25 +426,26 @@ public void drawLegend() {
}
/**
- * @brief Show short help on avaible commands
+ * Show short help on avaible commands
*/
public void drawHelp() {
+ int line = 0;
noStroke();
textFont(font);
textAlign(LEFT, TOP);
fill(255, 200);
- text("Help on Keyboard commands:", 0, 0);
- text("- space bar : pause", 0, 10);
- text("- h : show Histogram", 0, 20);
- text("- d : show Date", 0, 30);
- text("- l : show Legend", 0, 40);
- text("- e : show Edges", 0, 40);
- text("- b : show deBug", 0, 50);
- text("- ? : show help", 0, 60);
- text("- q : Quit code_swarm", 0, 70);
+ text("Help on Keyboard commands:", 0, 10*line++);
+ text("- space bar : pause", 0, 10*line++);
+ text("- h : show Histogram", 0, 10*line++);
+ text("- d : show Date", 0, 10*line++);
+ text("- l : show Legend", 0, 10*line++);
+ text("- e : show Edges", 0, 10*line++);
+ text("- b : show deBug", 0, 10*line++);
+ text("- ? : show help", 0, 10*line++);
+ text("- q : Quit code_swarm", 0, 10*line++);
}
/**
- * @brief Show debug information about all drawable objects
+ * Show debug information about all drawable objects
*/
public void drawDebugData() {
noStroke();
@@ -460,7 +459,7 @@ public void drawDebugData() {
}
/**
- * @brief Take screenshot
+ * Take screenshot
*/
public void dumpFrame() {
if (frameCount < 100000)
@@ -468,7 +467,7 @@ public void dumpFrame() {
}
/**
- * @brief Update the particle positions
+ * Update the particle positions
*/
public void update() {
// Create a new histogram line
@@ -625,7 +624,7 @@ public PersonNode findPerson(String name) {
}
/**
- * @brief Head function for loadRecurse
+ * Head function for loadRecurse
*/
public void loadRepository(String filename) {
XMLElement doc = new XMLElement(this, filename);
@@ -634,7 +633,7 @@ public void loadRepository(String filename) {
}
/**
- * @brief Load repository-formatted file
+ * Load repository-formatted file
*/
public void loadRepository(XMLElement xml, String path, String filename) {
String tag = xml.getName();
@@ -662,7 +661,7 @@ public void loadRepository(XMLElement xml, String path, String filename) {
}
/**
- * @brief Load event-formatted file
+ * Load event-formatted file
*/
public void loadRepEvents(String filename1) {
XMLElement doc = new XMLElement(this, filename1);
@@ -685,7 +684,7 @@ public void loadRepEvents(String filename1) {
}
/**
- * @brief Load SVN log formatted file
+ * Load SVN log formatted file
* @deprecated
*/
public void loadSVNRepository(XMLElement doc) {
@@ -739,7 +738,7 @@ public void loadSVNRepository(XMLElement doc) {
*/
/**
- * @brief Keystroke callback function
+ * Keystroke callback function
*/
public void keyPressed() {
switch (key) {
@@ -778,7 +777,7 @@ public void keyPressed() {
}
/**
- * @brief Toggle pause
+ * Toggle pause
*/
public void pauseButton() {
if (looping)
@@ -789,7 +788,7 @@ public void pauseButton() {
}
/**
- * @brief Describe an event on a file
+ * Describe an event on a file
*/
class FileEvent implements Comparable<Object> {
Date date;
@@ -800,14 +799,14 @@ public void pauseButton() {
int linesremoved;
/**
- * @brief short constructor with base data
+ * short constructor with base data
*/
FileEvent(long datenum, String author, String path, String filename) {
this(datenum, author, path, filename, 0, 0);
}
/**
- * @brief constructor with number of modified lines
+ * constructor with number of modified lines
*/
FileEvent(long datenum, String author, String path, String filename, int linesadded, int linesremoved) {
this.date = new Date(datenum);
@@ -819,7 +818,7 @@ public void pauseButton() {
}
/**
- * @brief Comparing two events by date (Not Used)
+ * Comparing two events by date (Not Used)
*/
public int compareTo(Object o) {
return date.compareTo(((FileEvent) o).date);
@@ -827,7 +826,7 @@ public int compareTo(Object o) {
}
/**
- * @brief Base class for all drawable objects
+ * Base class for all drawable objects
*
* Lists and implements features common to all drawable objects
* Edge and Node, FileNode and PersonNode
@@ -845,7 +844,7 @@ public int compareTo(Object o) {
final public int LIFE_DECREMENT;
/**
- * @brief 1) constructor(s)
+ * 1) constructor(s)
*
* Init jobs common to all objects
*/
@@ -859,19 +858,19 @@ public int compareTo(Object o) {
}
/**
- * @brief 2) calculating next frame state => done in derived class
+ * 2) calculating next frame state => done in derived class
*/
public abstract void relax();
/**
- * @brief 3) applying next frame state
+ * 3) applying next frame state
*/
public void update() {
decay();
}
/**
- * @brief 4) shortening life
+ * 4) shortening life.
*/
public void decay() {
if (life > 0) {
@@ -883,12 +882,12 @@ public void decay() {
}
/**
- * @brief 5) drawing the new state => done in derived class
+ * 5) drawing the new state => done in derived class.
*/
public abstract void draw();
/**
- * @brief 6) reseting life as if new
+ * 6) reseting life as if new.
*/
public void freshen() {
life = LIFE_INIT;
@@ -915,7 +914,7 @@ public void adjDY(float amt) {
}
/**
- * @brief An Edge link two nodes together : a File to a Person
+ * An Edge link two nodes together : a File to a Person.
*/
class Edge extends Drawable {
Node from;
@@ -923,7 +922,7 @@ public void adjDY(float amt) {
float len;
/**
- * @brief 1) constructor
+ * 1) constructor.
*/
Edge(Node from, Node to) {
super(EDGE_LIFE_INIT, EDGE_LIFE_DECREMENT); // 255, -2
@@ -933,7 +932,7 @@ public void adjDY(float amt) {
}
/**
- * @brief 2) calculating next frame state
+ * 2) calculating next frame state.
*/
public void relax() {
float distance;
@@ -960,7 +959,7 @@ public void relax() {
}
/**
- * @brief 5) drawing the new state
+ * 5) drawing the new state.
*/
public void draw() {
if (life > 240) {
@@ -973,9 +972,9 @@ public void draw() {
}
/**
- * @brief A node is an abstraction for a File or a Person
+ * A node is an abstraction for a File or a Person.
*/
- abstract class Node extends Drawable {
+ public abstract class Node extends Drawable {
String name;
float x, y;
@@ -989,7 +988,7 @@ public void draw() {
protected float maxSpeed = 7.0f;
/**
- * @brief 1) constructor
+ * 1) constructor.
*/
Node(int lifeInit, int lifeDecrement) {
super(lifeInit, lifeDecrement);
@@ -1000,7 +999,7 @@ public void draw() {
}
/**
- * @brief 3) applying next frame state
+ * 3) applying next frame state.
*
* This is a surdefinition of the Drawable update() method
*/
@@ -1036,21 +1035,21 @@ public float getY() {
}
/**
- * @brief A node describing a file, which is repulsed by other files
+ * A node describing a file, which is repulsed by other files.
*/
class FileNode extends Node {
int nodeHue;
int minBold;
/**
- * @brief getting file node as a string
+ * getting file node as a string
*/
public String toString() {
return "FileNode{" + "name='" + name + '\'' + ", nodeHue=" + nodeHue + ", touches=" + touches + '}';
}
/**
- * @brief 1) constructor
+ * 1) constructor.
*/
FileNode(FileEvent fe) {
super(FILE_LIFE_INIT, FILE_LIFE_DECREMENT); // 255, -2
@@ -1064,7 +1063,7 @@ public String toString() {
}
/**
- * @brief 2) calculating next frame state
+ * 2) calculating next frame state.
*
* @todo this physic job should be uniformed between file a person nodes
* => then it could be moved up
@@ -1102,7 +1101,7 @@ public void relax() {
}
/**
- * @brief 5) drawing the new state
+ * 5) drawing the new state.
*/
public void draw() {
if (life > 0) {
@@ -1164,7 +1163,7 @@ public void drawJelly() {
}
/**
- * @brief A node describing a person, which is repulsed by other persons
+ * A node describing a person, which is repulsed by other persons.
*/
class PersonNode extends Node {
int flavor = color(0);
@@ -1174,9 +1173,12 @@ public void drawJelly() {
/** @todo Not Used : to implements in physics */
float mass = 10;
float accel = 0.0f;
+
+ /** @todo Work In Progress */
+ ForceCalcLegacyPerson ForceCalcBetweenPersons = new ForceCalcLegacyPerson(0.01f);
/**
- * @brief 1) constructor
+ * 1) constructor.
*/
PersonNode(String n) {
super(PERSON_LIFE_INIT, PERSON_LIFE_DECREMENT); // -1
@@ -1188,7 +1190,7 @@ public void drawJelly() {
}
/**
- * @brief 2) calculating next frame state
+ * 2) calculating next frame state.
*
* @todo this physic job should be uniformed between file a person nodes => then
* it could be moved up
@@ -1197,39 +1199,33 @@ public void relax() {
if (life <= 0)
return;
- ddx = 0;
- ddy = 0;
-
+ // Calculation of repulsive force between persons
+ ForceVector forceSummation = new ForceVector();
for (int j = 0; j < people.size(); j++) {
Node n = (Node) people.get(j);
if (n.life <= 0)
continue;
if (n != this) {
- distx = x - n.getX();
- disty = y - n.getY();
- lensq = distx * distx + disty * disty;
- if (lensq == 0) {
- ddx += random(0.01f);
- ddy += random(0.01f);
- } else if (lensq < 10000) { /** @todo remove this not linear calculation */
- ddx += distx / lensq;
- ddy += disty / lensq;
- }
+ ForceVector forceBetween2Persons = new ForceVector();
+ ForceCalcBetweenPersons.calculateForceBetween(this, n, forceBetween2Persons);
+ forceSummation.add(forceBetween2Persons);
}
}
- dlen = mag(ddx, ddy) / 2;
+
+ // Apply repulsive force from other persons to this Node
+ /** @todo use same mechanism as the above "ForceCalc" */
+ dlen = (float)forceSummation.norm() / 2;
if (dlen > 0) {
- dx += ddx / dlen;
- dy += ddy / dlen;
+ dx += forceSummation.getX() / dlen;
+ dy += forceSummation.getY() / dlen;
}
-
dx /= 12;
dy /= 12;
}
/**
- * @brief 5) drawing the new state
+ * 5) drawing the new state.
*/
public void draw() {
if (life <= 0)
@@ -1255,7 +1251,7 @@ public void addColor(int c) {
}
/**
- * code_swarm Entry point
+ * code_swarm Entry point.
*/
static public void main(String args[]) {
try {
Please sign in to comment.
Something went wrong with that request. Please try again.