Commit 5882aa7d authored by gsavin's avatar gsavin

Make greedy forces working. Still got some bugs.

parent efd546db
......@@ -253,7 +253,6 @@ public abstract class BoidForces {
float aarea = 0.000001f;
Point3 lo = ((BoidGraph) boid.getGraph()).getLowAnchor();
Point3 hi = ((BoidGraph) boid.getGraph()).getHighAnchor();
Vector3 dir = getDirection();
Point3 nextPos = getNextPosition();
if (nextPos.x + dir.data[0] <= lo.x + aarea) {
......
......@@ -35,6 +35,7 @@ import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import org.graphstream.boids.forces.greedy.GreedyForcesFactory;
import org.graphstream.boids.forces.ntree.NTreeForcesFactory;
import org.graphstream.graph.Graph;
import org.graphstream.graph.NodeFactory;
......@@ -143,7 +144,8 @@ public class BoidGraph extends AdjacencyListGraph {
area = 1;
maxSteps = 0;
boidSpecies = new HashMap<String, BoidSpecies>();
forcesFactory = new NTreeForcesFactory(this);
forcesFactory = new GreedyForcesFactory(this);
//forcesFactory = new NTreeForcesFactory(this);
}
public BoidGraph(String dgsConfig) throws IOException {
......
......@@ -30,40 +30,34 @@ package org.graphstream.boids.forces.greedy;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Random;
import org.graphstream.boids.Boid;
import org.graphstream.boids.BoidForces;
import org.graphstream.boids.BoidGraph;
import org.miv.pherd.geom.Point3;
import org.miv.pherd.geom.Vector3;
public class GreedyForces extends BoidForces {
Point3 position;
Vector3 direction;
Point3 nextPosition;
public GreedyForces(Boid b) {
super(b);
}
/*
* (non-Javadoc)
*
* @see org.graphstream.boids.BoidForces#compute()
*/
public void compute() {
BoidGraph g = (BoidGraph) boid.getGraph();
LinkedList<Boid> contacts = new LinkedList<Boid>();
Vector3 rep = new Vector3();
BoidGraph ctx = (BoidGraph) b.getGraph();
Random r = ctx.getRandom();
Point3 lo = ctx.getLowAnchor();
Point3 hi = ctx.getHighAnchor();
for (Boid b : g.<Boid> getEachNode()) {
if (isVisible(boid, b.getPosition())) {
actionWithNeighboor(b, rep);
contacts.add(b);
}
}
position = new Point3();
nextPosition = new Point3();
position.x = r.nextDouble() * (hi.x - lo.x) + lo.x;
position.y = r.nextDouble() * (hi.y - lo.y) + lo.y;
position.z = r.nextDouble() * (hi.z - lo.z) + lo.z;
boid.checkNeighborhood(contacts.toArray(new Boid[contacts.size()]));
nextPosition.copy(position);
}
/*
......@@ -84,23 +78,21 @@ public class GreedyForces extends BoidForces {
position.set(x, y, z);
}
/*
* (non-Javadoc)
*
* @see org.graphstream.boids.BoidForces#getDirection()
*/
public Vector3 getDirection() {
return direction;
}
/*
* (non-Javadoc)
*
* @see org.graphstream.boids.BoidForces#getNeighborhood()
*/
public Collection<Boid> getNeighborhood() {
// TODO Auto-generated method stub
return null;
BoidGraph g = (BoidGraph) boid.getGraph();
LinkedList<Boid> contacts = new LinkedList<Boid>();
for (Boid b : g.<Boid> getEachNode()) {
if (isVisible(boid, b.getPosition()))
contacts.add(b);
}
return contacts;
}
/*
......@@ -109,7 +101,6 @@ public class GreedyForces extends BoidForces {
* @see org.graphstream.boids.BoidForces#getNextPosition()
*/
public Point3 getNextPosition() {
// TODO Auto-generated method stub
return null;
return nextPosition;
}
}
......@@ -31,10 +31,17 @@ package org.graphstream.boids.forces.greedy;
import org.graphstream.boids.Boid;
import org.graphstream.boids.BoidForces;
import org.graphstream.boids.BoidForcesFactory;
import org.graphstream.boids.BoidGraph;
import org.miv.pherd.geom.Point3;
public class GreedyForcesFactory implements BoidForcesFactory {
BoidGraph ctx;
public GreedyForcesFactory(BoidGraph ctx) {
this.ctx = ctx;
}
/*
* (non-Javadoc)
*
......@@ -43,8 +50,7 @@ public class GreedyForcesFactory implements BoidForcesFactory {
* .boids.Boid)
*/
public BoidForces createNewForces(Boid b) {
// TODO Auto-generated method stub
return null;
return new GreedyForces(b);
}
/*
......@@ -53,8 +59,13 @@ public class GreedyForcesFactory implements BoidForcesFactory {
* @see org.graphstream.boids.BoidForcesFactory#step()
*/
public void step() {
// TODO Auto-generated method stub
for (Boid b : ctx.<Boid> getEachNode())
b.getForces().compute();
for (Boid b : ctx.<Boid> getEachNode()) {
BoidForces f = b.getForces();
f.getPosition().copy(f.getNextPosition());
}
}
/*
......@@ -65,8 +76,5 @@ public class GreedyForcesFactory implements BoidForcesFactory {
* org.miv.pherd.geom.Point3)
*/
public void resize(Point3 low, Point3 high) {
// TODO Auto-generated method stub
}
}
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