Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /tests/lamont-tests/boids.diderot
ViewVC logotype

View of /tests/lamont-tests/boids.diderot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5034 - (download) (annotate)
Tue Jul 11 23:24:17 2017 UTC (23 months, 1 week ago) by jhr
File size: 3290 byte(s)
  minor edits
#version 2

real[] initVelocities = load_sequence("data/velocities.nrrd");
real[] initPosns = load_sequence("data/posns.nrrd");
int numBoids = 100;
input real boidRadius = 5;
input real queryRadius = 50;
input real DESIRED_SEPARTION = 30;
int MAX_SPEED = 2;
real MAX_FORCE = 0.05;
real boundsFactor = 10.0;
input int width = 800;
input int height = 600;
const int NORTH = 0;
const int SOUTH  = 1;
const int WEST = 2;
const int EAST =  3;
int steps = 0;

strand Boid (int i,real vx, real vy, real px, real py) {
    vec2 pos = [px,py];
    vec2 vel = [vx,vy];
    int id = i;
    output vec4 boidInfo = [pos[0],pos[1],vel[0],vel[1]];

  //output real value = 0.0;
    update {
        vec2 cohereVec = [0.0,0.0];
        vec2 alignVec = [0.0,0.0];
	vec2 seperateVec = [0.0,0.0];
        vec4 wrapDimensions = [-boundsFactor,
                               height + boundsFactor,
                               -boundsFactor,
                               width + boundsFactor];
	int count = 0;
        int count2 = 0;
        foreach (Boid neighbor in sphere(queryRadius)) {
            real d = |pos - neighbor.pos|;
	    if (d > 0) {
		cohereVec += neighbor.pos;
		alignVec += neighbor.vel;
		count+=1;
		if (d < DESIRED_SEPARTION) {
		    seperateVec = seperateVec + (normalize(pos-neighbor.pos)/d);
		    count2+=1;
		}
	    }
        }

        if (count > 0) {
	    cohereVec /= real(count);
	    alignVec /= real(count);
        }
        if (count2 > 0) {
            seperateVec /= real(count2);
        }

        // limit the force
        if ( count != 0 && |alignVec| > MAX_FORCE) {
	    alignVec = normalize(alignVec);
	    alignVec *= MAX_FORCE;
	}

        real diff = |cohereVec - pos|;

        if (diff > 0 && count != 0) {

            cohereVec = normalize(cohereVec);

            if(diff < 100.0) {
                cohereVec *= (MAX_SPEED*(diff/100.0));
      	    } else {
                cohereVec *= MAX_SPEED;
            }

            cohereVec -= vel;

          // limit the force
	    if (|cohereVec| > MAX_FORCE) {
		cohereVec = normalize(cohereVec);
                cohereVec *= MAX_FORCE;
	    }
	}
	else{
     	    cohereVec = [0.0,0.0];
        }

        vec2 acceleration = cohereVec + alignVec + seperateVec;

	vel += acceleration;

	if (|vel| > MAX_SPEED) {
	    vel = normalize(vel);
	    vel *= MAX_SPEED;
	}

        pos += vel;

        //Wrapping Code, if a boid goes outside the window
        real posX = wrapDimensions[EAST] if pos[0] < wrapDimensions[WEST]
                                         else wrapDimensions[WEST] if pos[0] > wrapDimensions[EAST]
                                                                else pos[0];

        real posY = wrapDimensions[SOUTH] if pos[1] < wrapDimensions[NORTH]
                else wrapDimensions[NORTH] if pos[1] > wrapDimensions[SOUTH]
                                          else pos[1];

        pos = [posX,posY];

	boidInfo = [pos[0],pos[1], vel[0], vel[1]];


        if (steps > 500){
            //value = boidInfo[0] + boidInfo[1] + boidInfo[2]  + boidInfo[3];
            stabilize;
        }

    } // update

}

update {
  steps += 1;
}

create_collection {
    Boid(i, initVelocities[i*2], initVelocities[i*2+1], initPosns[i*2], initPosns[i*2+1])
  | i in 0 .. numBoids-1
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0