Commit d5f746b6 authored by gsavin's avatar gsavin

Nettoyage de printemps.

parent e88cc204
......@@ -42,15 +42,16 @@ import org.miv.pherd.geom.Vector3;
public class Boid extends AdjacencyListNode {
protected final BoidSpecies species;
protected BoidParticle particle;
protected BoidSpecies species;
protected Forces forces;
public Boid(Graph graph, String id) {
super((AbstractGraph)graph, id);
particle = new BoidParticle((Context) graph);
species = ((Context) graph).getDefaultSpecies();
forces = getDefaultForces();
public Boid(Graph graph, BoidSpecies species, String id) {
super((AbstractGraph) graph, id);
this.particle = new BoidParticle((Context) graph);
this.species = species;
this.forces = getDefaultForces();
}
public void setPosition(double x, double y, double z) {
......@@ -65,10 +66,6 @@ public class Boid extends AdjacencyListNode {
return species;
}
public void setSpecies(BoidSpecies species) {
this.species = species;
}
public BoidParticle getParticle() {
return particle;
}
......@@ -77,31 +74,17 @@ public class Boid extends AdjacencyListNode {
return new Forces.BasicForces();
}
@Override
protected void attributeChanged(String sourceId, long timeId,
String attribute, AttributeChangeEvent event, Object oldValue,
Object newValue) {
if (attribute.equals("species")) {
Context ctx = (Context) getGraph();
BoidSpecies species = ctx.getOrCreateSpecies(newValue.toString());
this.species = species;
}
super.attributeChanged(sourceId, timeId, attribute, event, oldValue,
newValue);
}
protected void checkNeighborhood(BoidParticle... particles) {
//System.err.printf("Boid %s :%n", id);
// System.err.printf("Boid %s :%n", id);
if (particles != null) {
Iterator<Boid> it = getNeighborNodeIterator();
LinkedList<Boid> toRemove = null;
//System.err.printf("Sees [ ");
//for(BoidParticle p : particles) {
// System.err.printf("%s ", p.getBoid().id);
//}
//System.err.printf("]%nHas Neighbors [ ");
// System.err.printf("Sees [ ");
// for(BoidParticle p : particles) {
// System.err.printf("%s ", p.getBoid().id);
// }
// System.err.printf("]%nHas Neighbors [ ");
while (it.hasNext()) {
boolean found = false;
Boid b = it.next();
......@@ -112,17 +95,17 @@ public class Boid extends AdjacencyListNode {
break;
}
}
//System.err.printf("%s(%b)", b.id, found);
// System.err.printf("%s(%b)", b.id, found);
if (!found && !forces.isVisible(b.particle, this.getPosition())) {
if (toRemove == null)
toRemove = new LinkedList<Boid>();
//System.err.printf("(del)");
// System.err.printf("(del)");
toRemove.add(b);
}
}
//System.err.printf("%n");
// System.err.printf("%n");
if (toRemove != null) {
for (Boid b : toRemove)
......@@ -131,16 +114,16 @@ public class Boid extends AdjacencyListNode {
toRemove.clear();
toRemove = null;
}
//System.err.printf("adds link to [ ");
// System.err.printf("adds link to [ ");
for (BoidParticle p : particles) {
if (getEdgeBetween(p.getBoid().getId()) == null) {
getGraph().addEdge(getEdgeId(this, p.getBoid()), getId(),
p.getBoid().getId());
//System.err.printf("%s ", p.getBoid().id);
// System.err.printf("%s ", p.getBoid().id);
}
}
}
//System.err.printf("]%n");
// System.err.printf("]%n");
}
public static final String getEdgeId(Boid b1, Boid b2) {
......@@ -167,8 +150,8 @@ public class Boid extends AdjacencyListNode {
- ctx.area, ctx.random.nextDouble() * (ctx.area * 2)
- ctx.area, 0);
this.dir = new Vector3(ctx.random.nextDouble(),
ctx.random.nextDouble(), 0);
this.dir = new Vector3(ctx.random.nextDouble(), ctx.random
.nextDouble(), 0);
this.ctx = ctx;
}
......@@ -178,7 +161,7 @@ public class Boid extends AdjacencyListNode {
mySpeciesContacts = 0;
forces.compute(Boid.this, cell.getTree().getRootCell());
forces.direction.scalarMult(species.directionFactor);
forces.attraction.scalarMult(species.attractionFactor);
forces.repulsion.scalarMult(species.repulsionFactor);
......@@ -199,7 +182,7 @@ public class Boid extends AdjacencyListNode {
} else {
dir.scalarMult(species.speedFactor);
}
if (ctx.storeForcesAttributes)
forces.store(this);
......
......@@ -2,6 +2,7 @@ package org.graphstream.boids;
import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import org.graphstream.boids.Context;
......@@ -10,56 +11,38 @@ import org.graphstream.boids.Context;
*
* @author Antoine Dutot
*/
public class BoidSpecies extends HashMap<String, Boid> {
public class BoidSpecies implements Iterable<Boid> {
/**
*
*/
private static final long serialVersionUID = 6005548670964581065L;
// Attributes
public static enum Parameter {
COUNT, ANGLE_OF_VIEW, VIEW_ZONE, SPEED_FACTOR, MAX_SPEED, MIN_SPEED, WIDTH, TRAIL, DIRECTION_FACTOR, ATTRACTION_FACTOR, REPULSION_FACTOR, INERTIA, FEAR_FACTOR
}
/**
* Shared settings.
*/
protected Context ctx;
public static enum Parameter {
COUNT,
VIEW_ZONE,
SPEED_FACTOR,
MAX_SPEED,
MIN_SPEED,
WIDTH,
TRAIL,
DIRECTION_FACTOR,
ATTRACTION_FACTOR,
REPULSION_FACTOR,
INERTIA,
FEAR_FACTOR
}
/**
* Initial number of boids of this species.
*/
public int count = 200;
/**
* The species name.
*/
public String name = randomName();
protected final String name;
/**
* The distance at which a boid is seen.
*/
public double viewZone = 0.15f;
protected double viewZone = 0.15f;
/**
* The boid angle of view, [-1..1]. This is the cosine of the angle between the boid
* direction and the direction toward another boid. -1 Means 360 degrees, all is visible.
* 0 means 180 degrees only boids in front are visible, 0.5 means 90 degrees,
* 0.25 means 45 degrees.
* The boid angle of view, [-1..1]. This is the cosine of the angle between
* the boid direction and the direction toward another boid. -1 Means 360
* degrees, all is visible. 0 means 180 degrees only boids in front are
* visible, 0.5 means 90 degrees, 0.25 means 45 degrees.
*/
public double angleOfView = -1;
protected double angleOfView = -1;
/**
* The boid speed at each step. This is the factor by which the speedFactor
......@@ -67,17 +50,17 @@ public class BoidSpecies extends HashMap<String, Boid> {
* accelerate the boid displacement, it also impacts the boid behaviour
* (oscillation around the destination point, etc.)
*/
public double speedFactor = 0.3f;
protected double speedFactor = 0.3f;
/**
* Maximum speed bound.
*/
public double maxSpeed = 1f;
protected double maxSpeed = 1f;
/**
* Minimum speed bound.
*/
public double minSpeed = 0.04f;
protected double minSpeed = 0.04f;
/**
* The importance of the other boids direction "overall" vector. The
......@@ -85,7 +68,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* divided by the number of boids seen. This factor is the importance of
* this direction in the boid direction.
*/
public double directionFactor = 0.1f;
protected double directionFactor = 0.1f;
/**
* How much other visible boids attract a boid. The barycenter of the
......@@ -93,7 +76,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* boid an this barycenter. This factor is the importance of this vector in
* the boid direction.
*/
public double attractionFactor = 0.5f;
protected double attractionFactor = 0.5f;
/**
* All the visible boids repulse the boid. The repulsion vector is the sum
......@@ -101,69 +84,73 @@ public class BoidSpecies extends HashMap<String, Boid> {
* boid, scaled by the number of visible boids. This factor is the
* importance of this vector in the boid direction.
*/
public double repulsionFactor = 0.001f;
protected double repulsionFactor = 0.001f;
/**
* The inertia is the importance of the boid previous direction in the boid
* direction.
*/
public double inertia = 1.1f;
protected double inertia = 1.1f;
/**
* Factor for repulsion on boids of other species. The fear that this
* species produces on other species.
*/
public double fearFactor = 1;
protected double fearFactor = 1;
/**
* The species main colour.
*/
public Color color = new Color(1, 0, 0);
protected Color color = new Color(1, 0, 0);
/**
* The size of the trail in the GUI if any.
*/
public int trail = 0;
protected int trail = 0;
/**
* The width of the particle in the GUI if any.
*/
public int width = 4;
//protected DemographicManager demographicManager;
protected int width = 4;
protected HashMap<String, Boid> boids;
// Constructors
private int currentIndex = 0;
private long timestamp = System.nanoTime();
/**
* New default species with a random colour.
*/
public BoidSpecies(Context ctx, String name) {
this.boids = new HashMap<String, Boid>();
this.ctx = ctx;
this.name = name;
this.color = new Color(ctx.random.nextFloat(), ctx.random.nextFloat(),
ctx.random.nextFloat());
}
// Access
// Commands
public String getName() {
return name;
}
private int currentIndex = 0;
private long timestamp = System.nanoTime();
public Iterator<Boid> iterator() {
return boids.values().iterator();
}
public void set(String p, String val) throws IllegalArgumentException {
Parameter param = Parameter.valueOf(p.toUpperCase());
set(param, val);
}
public void set(Parameter p, String val) {
switch(p) {
System.out.printf("set %s of %s to %s\n", p.name(), name, val);
switch (p) {
case COUNT:
count = Integer.parseInt(val);
setCount(Integer.parseInt(val));
break;
case VIEW_ZONE:
viewZone = Double.parseDouble(val);
System.out.printf("%s view zone : %f\n", name, viewZone);
break;
case SPEED_FACTOR:
speedFactor = Double.parseDouble(val);
......@@ -195,52 +182,44 @@ public class BoidSpecies extends HashMap<String, Boid> {
case FEAR_FACTOR:
fearFactor = Double.parseDouble(val);
break;
case ANGLE_OF_VIEW:
angleOfView = Double.parseDouble(val);
break;
}
}
public String createNewId() {
return String.format("%s_%x_%x", name, timestamp, currentIndex++);
return String.format("%s.%x_%x", name, timestamp, currentIndex++);
}
public void register(Boid b) {
put(b.getId(), b);
}
public void unregister(Boid b) {
remove(b.getId());
public Boid createBoid() {
return createBoid(createNewId());
}
public void terminateLoop() {
// Do nothing.
// Can be used by extending classes.
}
protected static String randomName() {
return String.format("%c%c%c%c%c", (char) (64 + Math.random() * 26),
(char) (64 + Math.random() * 26),
(char) (64 + Math.random() * 26),
(char) (64 + Math.random() * 26),
(char) (64 + Math.random() * 26));
public Boid createBoid(String id) {
return new Boid(ctx, this, id);
}
public int getPopulation() {
return size();
void register(Boid b) {
boids.put(b.getId(), b);
}
public int getCount() {
return count;
void unregister(Boid b) {
boids.remove(b.getId());
}
public void setCount(int count) {
this.count = count;
public void terminateLoop() {
// Do nothing.
// Can be used by extending classes.
}
public String getName() {
return name;
public int getPopulation() {
return boids.size();
}
public void setName(String name) {
this.name = name;
public void setCount(int count) {
while (boids.size() < count)
ctx.addNode(createNewId());
}
public double getViewZone() {
......
This diff is collapsed.
......@@ -32,7 +32,6 @@ package org.graphstream.boids;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import org.graphstream.boids.Boid.BoidParticle;
......
DGS004
null 0 0
#
# GraphStream attributes
#
cg ui.quality ui.antialias
cg ui.stylesheet="node { size: 4px; } edge { fill-color: grey; }"
#
# gs-boids attributes
#
cg boids.max_steps=8000
cg boids.random_seed=2132134879
cg boids.area=1
cg boids.sleep_time=20
#
# Moustik species
#
cg boids.species.moustik=org.graphstream.boids.BoidSpecies
cg boids.species.moustik.angle_of_view=0
cg boids.species.moustik.view_zone=0.15
cg boids.species.moustik.speed_factor=0.3
cg boids.species.moustik.max_speed=1
cg boids.species.moustik.min_speed=0.04
cg boids.species.moustik.direction_factor=0.1
cg boids.species.moustik.attraction_factor=0.5
cg boids.species.moustik.repulsion_factor=0.001
cg boids.species.moustik.inertia=1.1
cg boids.species.moustik.trail=0
cg boids.species.moustik.width=4
cg boids.species.moustik.count=100
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment