9 |
* All rights reserved. |
* All rights reserved. |
10 |
*/ |
*/ |
11 |
real{} initVelocities = load("velocities.nrrd"); |
real{} initVelocities = load("velocities.nrrd"); |
12 |
|
real{} initPosns = load("posns.nrrd"); |
13 |
int numBoids = length(initVelocities)/2; |
int numBoids = length(initVelocities)/2; |
14 |
int strandSize = 2; |
real boidRadius = 2; |
15 |
real query_radius = 50; |
real queryRadius = 50; |
16 |
|
int DESIRED_SEPARTION = 30; |
17 |
int MAX_SPEED = 2; |
int MAX_SPEED = 2; |
18 |
real MAX_FORCE = 0.05; |
real MAX_FORCE = 0.05; |
|
int stepsMax = 10000; |
|
19 |
real boundsFactor = 10.0; |
real boundsFactor = 10.0; |
20 |
input int width; |
input int width; |
21 |
input int height; |
input int height; |
22 |
|
real cellSize=queryRadius; |
23 |
int NORTH = 0; |
int NORTH = 0; |
24 |
int SOUTH = 1; |
int SOUTH = 1; |
25 |
int WEST = 2; |
int WEST = 2; |
26 |
int EAST = 3; |
int EAST = 3; |
27 |
|
real threshold = 0.02; |
28 |
|
|
29 |
strand Boid(int i,real vx, real vy) { |
vec2 xDom = [0,width]; |
30 |
vec2 pos = [real(width)/2.0,real(height)/2.0]; |
vec2 yDom = [height,0]; |
31 |
|
|
32 |
|
real xSamples = floor((xDom[1] - xDom[0])/cellSize); |
33 |
|
real ySamples = floor((yDom[0] - yDom[1])/cellSize); |
34 |
|
|
35 |
|
vec4 qWinDim = [xDom[0],xDom[1],yDom[0],yDom[1]]; |
36 |
|
vec2 qGridDim = [xSamples ,ySamples]; |
37 |
|
vec2 qCellDim = [cellSize,cellSize]; |
38 |
|
|
39 |
|
|
40 |
|
strand Boid(int i,real vx, real vy, real px, real py) { |
41 |
|
vec2 pos = [px,py]; |
42 |
vec2 vel = [vx,vy]; |
vec2 vel = [vx,vy]; |
|
int steps = 0; |
|
43 |
int id = i; |
int id = i; |
44 |
output vec4 boidInfo = [pos[0],pos[1],vel[0],vel[1]]; |
output vec4 boidInfo = [pos[0],pos[1],vel[0],vel[1]]; |
45 |
update { |
update { |
46 |
|
|
47 |
vec2 cohereVec = [0.0,0.0]; |
vec2 cohereVec = [0.0,0.0]; |
48 |
vec2 alignVec = [0.0,0.0]; |
vec2 alignVec = [0.0,0.0]; |
49 |
vec2 seperateVec = [0.0,0.0]; |
vec2 seperateVec = [0.0,0.0]; |
52 |
-boundsFactor, |
-boundsFactor, |
53 |
width + boundsFactor]; |
width + boundsFactor]; |
54 |
int count = 0; |
int count = 0; |
55 |
|
int count2 = 0; |
56 |
foreach(Boid neighbor in sphere(query_radius)){ |
foreach(Boid neighbor in sphere(queryRadius)){ |
57 |
real d = dist(pos,neighbor.pos); |
real d = |pos - neighbor.pos|; |
|
|
|
|
if(d > 0) { |
|
58 |
cohereVec += neighbor.pos; |
cohereVec += neighbor.pos; |
59 |
alignVec += neighbor.vel; |
alignVec += neighbor.vel; |
|
seperateVec = seperateVec + (normalize(pos-neighbor.pos)/d); |
|
60 |
count+=1; |
count+=1; |
61 |
|
|
62 |
|
if(d < DESIRED_SEPARTION) { |
63 |
|
seperateVec = seperateVec + (normalize(pos-neighbor.pos)/d); |
64 |
|
count2+=1; |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
if(count > 0) { |
if(count > 0) { |
69 |
cohereVec /= real(count); |
cohereVec /= real(count); |
70 |
alignVec /= real(count); |
alignVec /= real(count); |
71 |
seperateVec /= real(count); |
} |
72 |
|
if(count2 > 0) { |
73 |
|
seperateVec /= real(count2); |
74 |
} |
} |
75 |
|
|
76 |
real isZero = alignVec • alignVec; |
real isZero = alignVec • alignVec; |
118 |
|
|
119 |
pos += vel; |
pos += vel; |
120 |
|
|
121 |
/* Wrapping Code, if a boid goes outside the window */ |
//Wrapping Code, if a boid goes outside the window |
122 |
real posX = wrapDimensions[EAST] if pos[0] < wrapDimensions[WEST] |
real posX = wrapDimensions[EAST] if pos[0] < wrapDimensions[WEST] |
123 |
else wrapDimensions[WEST] if pos[0] > wrapDimensions[EAST] |
else wrapDimensions[WEST] if pos[0] > wrapDimensions[EAST] |
124 |
else pos[0]; |
else pos[0]; |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
initially {Boid(i,initVelocities{i*2},initVelocities{i*2+1}) | i in 0 .. numBoids-1 }; |
initially {Boid(i,initVelocities{i*2},initVelocities{i*2+1},initPosns{i*2}, |
137 |
|
initPosns{i*2+1}) | i in 0 .. numBoids-1 }; |