Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
graphstream
gs-boids
Commits
c526c2c0
Commit
c526c2c0
authored
Mar 13, 2012
by
Ant01n3
Browse files
Added more javadoc.
parent
bb6a8a08
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/org/graphstream/boids/Boid.java
View file @
c526c2c0
...
...
@@ -44,11 +44,20 @@ 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
* A boid is both a particle in the force
s
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>
*
* <p>
* The boid is in fact split in two parts, the {@link Boid} class itself and the
* {@link BoidParticle} inner class that represents the boid in the forces system. The
* boid particle in turn contains a {@link Forces} object that represents all the forces
* exercising on the boid. Globally, the {@link Boid} class acts on the graph and updates
* its position, creating links toward other boids/nodes that it sees, whereas the
* {@link BoidParticle} and the {@link Forces} are used to compute the boid position.
* </p>
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
...
...
@@ -188,18 +197,15 @@ public class Boid extends AdjacencyListNode {
/** Direction of the boid. */
protected
Vector3
dir
;
/** Set of glo
v
al parameters. */
/** Set of glo
b
al parameters. */
protected
Context
ctx
;
/** Number of boids in view at each step. */
protected
int
contacts
=
0
;
/** N
i
mber of boids of my group in view at each step. */
/** N
u
mber 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.
...
...
@@ -248,22 +254,17 @@ 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
);
Boid
.
this
.
setAttribute
(
"xyz"
,
pos
.
x
,
pos
.
y
,
pos
.
z
);
moved
=
true
;
}
@Override
public
void
inserted
()
{
}
@Override
public
void
removed
()
{
}
public
Boid
getBoid
()
{
...
...
@@ -310,4 +311,4 @@ public class Boid extends AdjacencyListNode {
}
}
}
}
}
\ No newline at end of file
src/org/graphstream/boids/BoidCellData.java
View file @
c526c2c0
...
...
@@ -15,11 +15,11 @@ import org.miv.pherd.ntree.NTreeListener;
* box.
*
* XXX TODO there are bugs here, verify this code and reuse it in the boid
* computation (actually the ntree is used only to go faster in searching
* computation (actually the n
-
tree is used only to go faster in searching
* neighbors).
*
*
* @author Guilhelm Savin
* @author Antoine Dutot
* @since 2007
*/
public
class
BoidCellData
extends
BarycenterCellData
{
// Attributes
...
...
src/org/graphstream/boids/BoidSpecies.java
View file @
c526c2c0
...
...
@@ -8,21 +8,18 @@ import org.graphstream.boids.Context;
/**
* Parameters for each boids species.
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
public
class
BoidSpecies
extends
HashMap
<
String
,
Boid
>
{
/**
*
*/
private
static
final
long
serialVersionUID
=
6005548670964581065L
;
// Attributes
/**
* Shared settings.
*/
protected
Context
ctx
;
/** Kinds of parameters. */
public
static
enum
Parameter
{
COUNT
,
VIEW_ZONE
,
...
...
@@ -30,7 +27,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
MAX_SPEED
,
MIN_SPEED
,
WIDTH
,
TRAIL
,
//
TRAIL,
DIRECTION_FACTOR
,
ATTRACTION_FACTOR
,
REPULSION_FACTOR
,
...
...
@@ -64,7 +61,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
/**
* The boid speed at each step. This is the factor by which the speedFactor
* vector is scaled to move the boid at each step. This therefore not only
* accelerate the boid displacement, it also impacts the boid behavio
u
r
* accelerate the boid displacement, it also impacts the boid behavior
* (oscillation around the destination point, etc.)
*/
public
double
speedFactor
=
0.3f
;
...
...
@@ -116,26 +113,22 @@ public class BoidSpecies extends HashMap<String, Boid> {
public
double
fearFactor
=
1
;
/**
* The species main colo
u
r.
* The species main color.
*/
public
Color
color
=
new
Color
(
1
,
0
,
0
);
/**
* The size of the trail in the GUI if any.
*/
public
int
trail
=
0
;
//
/**
//
* The size of the trail in the GUI if any.
//
*/
//
public int trail = 0;
/**
* The width of the particle in the GUI if any.
*/
public
int
width
=
4
;
//protected DemographicManager demographicManager;
// Constructors
/**
* New default species with a random colo
u
r.
* New default species with a random color.
*/
public
BoidSpecies
(
Context
ctx
,
String
name
)
{
this
.
ctx
=
ctx
;
...
...
@@ -144,11 +137,8 @@ public class BoidSpecies extends HashMap<String, Boid> {
ctx
.
random
.
nextFloat
());
}
// Access
// Commands
private
int
currentIndex
=
0
;
private
long
timestamp
=
System
.
nanoTime
();
public
void
set
(
String
p
,
String
val
)
throws
IllegalArgumentException
{
...
...
@@ -177,9 +167,9 @@ public class BoidSpecies extends HashMap<String, Boid> {
case
WIDTH:
width
=
Integer
.
parseInt
(
val
);
break
;
case
TRAIL:
trail
=
Integer
.
parseInt
(
val
);
break
;
//
case TRAIL:
//
trail = Integer.parseInt(val);
//
break;
case
DIRECTION_FACTOR:
directionFactor
=
Double
.
parseDouble
(
val
);
break
;
...
...
@@ -227,106 +217,196 @@ public class BoidSpecies extends HashMap<String, Boid> {
return
size
();
}
/**
* Initial number of boids of this species.
*/
public
int
getCount
()
{
return
count
;
}
/**
* Change the initial number of boids of this species.
*/
public
void
setCount
(
int
count
)
{
this
.
count
=
count
;
}
/**
* The species name.
*/
public
String
getName
()
{
return
name
;
}
/**
* Change the species name.
*/
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
/**
* The distance at which a boid is seen.
*/
public
double
getViewZone
()
{
return
viewZone
;
}
/**
* Change the distance at which a boid is seen.
*/
public
void
setViewZone
(
double
viewZone
)
{
this
.
viewZone
=
viewZone
;
}
/**
* The boid speed at each step. This is the factor by which the speedFactor
* vector is scaled to move the boid at each step. This therefore not only
* accelerate the boid displacement, it also impacts the boid behavior
* (oscillation around the destination point, etc.)
*/
public
double
getSpeedFactor
()
{
return
speedFactor
;
}
/**
* Change the boid speed at each step.
*/
public
void
setSpeedFactor
(
double
speedFactor
)
{
this
.
speedFactor
=
speedFactor
;
}
/**
* Maximum speed bound.
*/
public
double
getMaxSpeed
()
{
return
maxSpeed
;
}
/**
* Change the maximum speed bound.
*/
public
void
setMaxSpeed
(
double
maxSpeed
)
{
this
.
maxSpeed
=
maxSpeed
;
}
/**
* Minimum speed bound.
*/
public
double
getMinSpeed
()
{
return
minSpeed
;
}
/**
* Change the minimum speed bound.
*/
public
void
setMinSpeed
(
double
minSpeed
)
{
this
.
minSpeed
=
minSpeed
;
}
/**
* The importance of the other boids direction "overall" vector. The
* direction vector is the sum of all the visible boids direction vector
* divided by the number of boids seen. This factor is the importance of
* this direction in the boid direction.
*/
public
double
getDirectionFactor
()
{
return
directionFactor
;
}
/**
* Change the importance of the other boids direction "overall" vector.
*/
public
void
setDirectionFactor
(
double
directionFactor
)
{
this
.
directionFactor
=
directionFactor
;
}
/**
* How much other visible boids attract a boid. The barycenter of the
* visible boids attract a boid. The attraction is the vector between the
* boid an this barycenter. This factor is the importance of this vector in
* the boid direction.
*/
public
double
getAttractionFactor
()
{
return
attractionFactor
;
}
/**
* Change how much other visible boids attract a boid.
*/
public
void
setAttractionFactor
(
double
attractionFactor
)
{
this
.
attractionFactor
=
attractionFactor
;
}
/**
* All the visible boids repulse the boid. The repulsion vector is the sum
* of all the vectors between all other visible boids and the considered
* boid, scaled by the number of visible boids. This factor is the
* importance of this vector in the boid direction.
*/
public
double
getRepulsionFactor
()
{
return
repulsionFactor
;
}
/**
* Change how all the visible boids repulse the boid.
*/
public
void
setRepulsionFactor
(
double
repulsionFactor
)
{
this
.
repulsionFactor
=
repulsionFactor
;
}
/**
* The inertia is the importance of the boid previous direction in the boid
* direction.
*/
public
double
getInertia
()
{
return
inertia
;
}
/**
* Change the inertia is the importance of the boid previous direction in the boid
* direction.
*/
public
void
setInertia
(
double
inertia
)
{
this
.
inertia
=
inertia
;
}
/**
* Factor for repulsion on boids of other species. The fear that this
* species produces on other species.
*/
public
double
getFearFactor
()
{
return
fearFactor
;
}
/**
* Change the factor for repulsion on boids of other species.
*/
public
void
setFearFactor
(
double
fearFactor
)
{
this
.
fearFactor
=
fearFactor
;
}
/**
* The species main color.
*/
public
Color
getColor
()
{
return
color
;
}
/**
* Change the species main color.
*/
public
void
setColor
(
Color
color
)
{
this
.
color
=
color
;
}
public
void
setTrail
(
int
trail
)
{
this
.
trail
=
trail
;
}
//
public void setTrail(int trail) {
//
this.trail = trail;
//
}
/**
* Change the width of the particle in the GUI if any.
*/
public
void
setWidth
(
int
width
)
{
this
.
width
=
width
;
}
...
...
src/org/graphstream/boids/Forces.java
View file @
c526c2c0
...
...
@@ -41,19 +41,39 @@ import org.miv.pherd.geom.Vector3;
import
org.miv.pherd.ntree.Cell
;
/**
* Models the forces applied to a boid.
* Models the forces applied to a boid at each step.
*
* <p>
* This object is modified at each computation step to represent the forces applied to
* a boid particle in the forces system. It is in charge of going down the n-tree used
* to represent space and collect all the other boids in the field of view that could influence
* this boid. It then integrates these forces and compute a direction and barycenter (the
* point the boid tries to reach).
* </p>
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
public
abstract
class
Forces
{
/** The position the boid tries to reach at each step. */
public
Point3
barycenter
;
/** The direction of the boid at each step. */
public
Vector3
direction
;
/** The integrated attraction toward other boids in view at each step. */
public
Vector3
attraction
;
/** The integrated repulsion from the other boids in view at each step. */
public
Vector3
repulsion
;
/** The number of boids we are attracted to. */
public
int
countAtt
;
/** The number of boids we are repulsed from. */
public
int
countRep
;
/** Forces all set at zero. */
public
Forces
()
{
barycenter
=
new
Point3
();
direction
=
new
Vector3
();
...
...
@@ -91,30 +111,42 @@ public abstract class Forces {
boid
.
setAttribute
(
"force2"
,
repulsion
);
}
/** Integrate a repulsion vector. */
public
void
addRepulsion
(
Vector3
rep
)
{
repulsion
.
add
(
rep
);
countRep
++;
}
/** Integrate a direction influence. */
public
void
addDirection
(
Vector3
dir
)
{
direction
.
add
(
dir
);
countAtt
++;
}
/** Integrate an attraction vector. */
public
void
addAttraction
(
Vector3
att
)
{
attraction
.
add
(
att
);
countAtt
++;
}
/** Integrate a new point of influence. */
public
void
moveBarycenter
(
Point3
p
)
{
barycenter
.
move
(
p
);
}
/** Is the another point is space in the field of view? */
public
abstract
boolean
isVisible
(
BoidParticle
boid
,
Point3
other
);
/**
* A definition of the forces.
* A basic definition of forces for a boid.
*
* <p>
* The kind of forces exercising on a boid can be changed to either use a n-tree or not,
* or to account for other kind of forces or another force model. This is the default
* force system that matches the basic boid definition as defined by Craig Reynolds.
* </p>
*
* @author Guilhelm Savin
* @author Antoine Dutot
*/
public
static
class
BasicForces
extends
Forces
{
...
...
@@ -199,10 +231,6 @@ public abstract class Forces {
}
}
// source.getBoid().checkNeighborhood(
// contacts == null ? null : contacts
// .toArray(new BoidParticle[contacts.size()]));
// barycenter.move( data.getCenter() );
// direction.add( data.getDirection() );
}
...
...
@@ -238,14 +266,14 @@ public abstract class Forces {
// intersection (cases c, d, e and f).
//
// |-a-| +---------+ |-b-|
//
|
|
// |-c-| |-d-|
//
|
|
//
|
|-e-| |
//
|
|
// |-+----f----+-|
//
|
|
// +---------+
//
|
|
//
|-c-|
|-d-|
//
|
|
//
|
|-e-|
|
//
|
|
//
|-+----f----+-|
//
|
|
//
+---------+
if
(
X2
<
x1
||
X1
>
x2
)
return
false
;
...
...
@@ -262,15 +290,23 @@ public abstract class Forces {
/**
* True if the given position is visible by the boid.
*
* <p>
* This method first check if the given point is under the max distance of
* view. If so, it checks if the point is in the angle of view. The angle
* of view is specified as the cosine of the angle between the boid direction
* vector and the vector between the boid and the given point. This means
* that -1 is equal to a 360 degree of vision (the angle of view test is
* deactivated in this case), 0 means 180 degree angle, and 0.5 a 90 degree
* angle for example.
* </p>
*
* @param source
* The source boid.
* @param point
* The point to consider.
* @return A true if point is visible.
*
* @return True if point is visible by source.
*/
/* protected boolean isVisible(BoidParticle source, Point3 point) {
double d = point.distance(source.getPosition());
return (d <= source.getBoid().getSpecies().viewZone);
}
*/
@Override
public
boolean
isVisible
(
BoidParticle
source
,
Point3
point
)
{
// Check both the distance and angle of view according to the direction
...
...
@@ -305,6 +341,13 @@ public abstract class Forces {
return
false
;
}
/**
* A boid particle p2 that is visible by p1 as been found, integrate it in the forces that
* apply to the boid p1.
* @param p1 The source boid.
* @param p2 the boid visible by p1.
* @param rep The repulsion to compute.
*/
protected
void
actionWithNeighboor
(
BoidParticle
p1
,
BoidParticle
p2
,
Vector3
rep
)
{
Point3
pp
=
p2
.
getPosition
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment