Commit bb6a8a08 authored by Ant01n3's avatar Ant01n3

More documentation.

parent ed05f3c6
......@@ -40,12 +40,35 @@ import org.miv.pherd.Particle;
import org.miv.pherd.geom.Point3;
import org.miv.pherd.geom.Vector3;
/**
* Represents a single bird-oid object.
*
* <p>
* A boid is both a particle in the force system used to compute the position and motion
* of the object, and a GraphStream node. This allows to consider a graph made of
* all the boids.
* </p>
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
public class Boid extends AdjacencyListNode {
/** The underlying particle in the force system. */
protected BoidParticle particle;
/** Parameters of this group of boids. */
protected BoidSpecies species;
/** The set of forces acting on this particle. */
protected Forces forces;
/**
* New boid as a node in the given graph.
*
* @param graph The graph this boids pertains to.
* @param id The boid identifier in the graph and in the force system.
*/
public Boid(Graph graph, String id) {
super((AbstractGraph)graph, id);
particle = new BoidParticle((Context) graph);
......@@ -53,33 +76,42 @@ public class Boid extends AdjacencyListNode {
forces = getDefaultForces();
}
/** Force the position of the boid in space. */
public void setPosition(double x, double y, double z) {
particle.setPosition(x, y, z);
}
/** Actual position of the boid in space. */
public Point3 getPosition() {
return particle.getPosition();
}
/** Set of parameters used by this boid group. */
public BoidSpecies getSpecies() {
return species;
}
/** Change boid group and set of parameters. */
public void setSpecies(BoidSpecies species) {
this.species = species;
}
/** The underlying particle of the force system this boids is linked to. */
public BoidParticle getParticle() {
return particle;
}
/**
* The forces acting on the boids, this is a set of vectors and parameters
* computed at each time step.
*/
public Forces getDefaultForces() {
return new Forces.BasicForces();
}
@Override
protected void attributeChanged(String sourceId, long timeId,
String attribute, AttributeChangeEvent event, Object oldValue,
String attribute, AttributeChangeEvent event, Object oldValue,
Object newValue) {
if (attribute.equals("species")) {
Context ctx = (Context) getGraph();
......@@ -92,16 +124,10 @@ public class Boid extends AdjacencyListNode {
}
protected void checkNeighborhood(BoidParticle... particles) {
//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 [ ");
while (it.hasNext()) {
boolean found = false;
Boid b = it.next();
......@@ -112,17 +138,14 @@ public class Boid extends AdjacencyListNode {
break;
}
}
//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)");
toRemove.add(b);
}
}
//System.err.printf("%n");
if (toRemove != null) {
for (Boid b : toRemove)
......@@ -131,18 +154,20 @@ public class Boid extends AdjacencyListNode {
toRemove.clear();
toRemove = null;
}
//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("]%n");
}
/**
* Compute the edge identifier between two boids knowing their individual identifiers.
* This method ensures the identifiers are always in the same order so that we get the
* same edge whatever the order of the parameters b1 and b2.
*/
public static final String getEdgeId(Boid b1, Boid b2) {
if (b1.hashCode() > b2.hashCode()) {
Boid t = b1;
......@@ -153,15 +178,32 @@ public class Boid extends AdjacencyListNode {
return String.format("%s--%s", b1.getId(), b2.getId());
}
/**
* Internal representation of the boid position, and direction in the forces system.
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
class BoidParticle extends Particle {
/** Direction of the boid. */
protected Vector3 dir;
/** Set of gloval parameters. */
protected Context ctx;
/** Number of boids in view at each step. */
protected int contacts = 0;
/** Nimber of boids of my group in view at each step. */
protected int mySpeciesContacts = 0;
/** ?? */
protected float energy = 0;
/**
* New particle.
* @param ctx The set of global parameters.
*/
public BoidParticle(Context ctx) {
super(Boid.this.getId(), ctx.random.nextDouble() * (ctx.area * 2)
- ctx.area, ctx.random.nextDouble() * (ctx.area * 2)
......@@ -206,6 +248,7 @@ public class Boid extends AdjacencyListNode {
checkWalls();
nextPos.move(dir);
// setAttribute("xyz", pos.x, pos.y, pos.z);
Boid.this.setAttribute("x", pos.x);
Boid.this.setAttribute("y", pos.y);
Boid.this.setAttribute("z", pos.z);
......
......@@ -35,8 +35,6 @@ import java.util.HashMap;
import org.graphstream.graph.Graph;
import org.graphstream.graph.NodeFactory;
import org.graphstream.graph.implementations.AdjacencyListGraph;
import org.graphstream.ui.spriteManager.Sprite;
import org.graphstream.ui.spriteManager.SpriteManager;
import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.util.Camera;
import org.miv.pherd.ParticleBox;
......
......@@ -30,10 +30,9 @@
*/
package org.graphstream.boids;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import org.graphstream.boids.Boid.BoidParticle;
import org.miv.pherd.Particle;
......@@ -43,9 +42,9 @@ import org.miv.pherd.ntree.Cell;
/**
* Models the forces applied to a boid.
*
*
* @author Guilhelm Savin
* @author Antoine Dutot
*
*/
public abstract class Forces {
public Point3 barycenter;
......
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