//
// Cell* getCell() const
// Last modified: 27Aug2006
//
// Returns the cell at the parameterized position.
//
// Returns:     the cell at the parameterized position
// Parameters:  <none>
//
Cell* Environment::getCell(GLint pos) const
{
    Cell *head = NULL;
    if ((pos < 0) || (pos > getNCells()) || (!cells.getHead(head)))
        return NULL;
    pos -= head->getID();
    return cells[(pos < 0) ? getNCells() + pos : pos];
}   // getCell(GLint) const
//
// bool initNbrs(nNbrs)
// Last modified: 03Sep2006
//
// Initializes the neighborhood of each cell,
// returning true if successful, false otherwise.
//
// Returns:     true if successful, false otherwise
// Parameters:
//      nNbrs       in      the initial number of neighbors
//
bool Environment::initNbrs(const GLint nNbrs)
{
    Cell *c = NULL;
    for (GLint i = 0; i < getNCells(); ++i)
    {
        if (!cells.getHead(c))                            return false;
        c->clearNbrs();
        if ((i > 0)               && (!c->addNbr(i - 1))) return false;
        else c->leftNbr  = c->nbrWithID(i - 1);
        if ((i < getNCells() - 1) && (!c->addNbr(i + 1))) return false;
        else c->rightNbr = c->nbrWithID(i + 1);
        ++cells;
    }
    return true;
}   // initNbrs(const GLint)
Beispiel #3
0
float *Dataslc::compGradient(int &len, float **funx)
{
   float *val = (float *)malloc(sizeof(float)*FSAMPLES);
   float *fx = (float *)malloc(sizeof(float)*FSAMPLES);
   int c;
   u_int *v;
   float cellgrad[3], scaling;
//   u_int cv[3];

   len = FSAMPLES;
   memset(val, 0, sizeof(float)*len);

   *funx = fx;
   for (c=0; c<len; c++)
      fx[c] = getMin() + (c/(len-1.0)) * (getMax()-getMin());

   for (c=0; c<getNCells(); c++) {
      v = getCellVerts(c);
      getCellGrad3(c, cellgrad);
      scaling = sqrt(cellgrad[0]*cellgrad[0] + cellgrad[1]*cellgrad[1]) / (cellgrad[2]);
//      scaling = (cellgrad[0]*cellgrad[0] + cellgrad[1]*cellgrad[1]) / sqr(cellgrad[2]);
//      cv[0] = v[0]; cv[1]=v[1]; cv[2]=v[2];

      triSurfIntegral(getVert(v[0]), getVert(v[1]), getVert(v[2]),
                      getValue(v[0]), getValue(v[1]), getValue(v[2]), fx, val, len,
                      getMin(), getMax(), fabs(scaling));
   }

   return(val);
}
Beispiel #4
0
float *Dataslc::compLength(int &len, float **funx)
{
   float *val = (float *)malloc(sizeof(float)*FSAMPLES);
   float *fx = (float *)malloc(sizeof(float)*FSAMPLES);
   int c;
   u_int *v;

   len = FSAMPLES;
   memset(val, 0, sizeof(float)*len);

   *funx = fx;
   for (c=0; c<len; c++)
      fx[c] = getMin() + (c/(len-1.0)) * (getMax()-getMin());

   for (c=0; c<getNCells(); c++) {
      v = getCellVerts(c);

      triSurfIntegral(getVert(v[0]), getVert(v[1]), getVert(v[2]),
                      getValue(v[0]), getValue(v[1]), getValue(v[2]), fx, val, len,
                      getMin(), getMax(), 1.0);

   }

   return(val);
}
//
// void draw()
// Last modified: 27Aug2006
//
// Renders the environment.
//
// Returns:     <none>
// Parameters:  <none>
//
void Environment::draw()
{
    Cell *currCell = NULL;
    for (GLint i = 0; i < getNCells(); ++i)
        if (cells.getHead(currCell))
        {
            currCell->draw();
            ++cells;
        }
}   // draw()
//
// bool showHeading(show)
// Last modified: 28Aug2006
//
// Attempts to display the heading vector of each cell,
// returning true if successful, false otherwise.
//
// Returns:     true if successful, false otherwise
// Parameters:
//      show    in      display flag for showing the heading vector
//
bool Environment::showHeading(const bool show)
{
    Cell *currCell;
    for (GLint i = 0; i < getNCells(); ++i)
        if (cells.getHead(currCell))
        {
            currCell->showHeading      = show;
            ++cells;
        }
        else return false;
    return true;
}   // showHeading(const bool)
//
// void step()
// Last modified: 27Aug2006
//
// Executes the next step in each cell in the environment
// and forwards all sent packets to their destinations.
//
// Returns:     <none>
// Parameters:  <none>
//
bool Environment::step()
{
    Cell *currCell = NULL;
    for (GLint i = 0; i < getNCells(); ++i)
    {
        if (!cells.getHead(currCell)) return false;
        currCell->step();
        ++cells;
    }

    // forwards all messages sent via robot cell communication
    return forwardPackets();
}   // step()
//
// bool initCells(n, f)
// Last modified: 28Aug2006
//
// Initializes each cell to the parameterized values,
// returning true if successful, false otherwise.
//
// Returns:     true if successful, false otherwise
// Parameters:
//      n           in      the initial number of cells
//      f           in      the initial formation
//
bool Environment::initCells(const GLint n, const Formation f)
{
    srand(time(NULL));
    for (GLint i = 0; i < n; ++i) if (!addCell()) return false;

    // initialize each robot's neighborhood
    if (!initNbrs()) return false;

    // organizes the cells into an initial formation (default line)
    Cell *c  = NULL;
    for (GLint i = 0; i < getNCells(); ++i)
    {
        if (!cells.getHead(c)) return false;
        c->x = f.getRadius() *
               ((GLfloat)i - (GLfloat)(getNCells() - 1) / 2.0f);
        c->y = 0.0f;
        c->setColor(DEFAULT_ROBOT_COLOR);
        c->setHeading(f.getHeading());
        ++cells;
    }
    return (getNCells() == n) &&
           sendMsg(new Formation(f), f.getSeedID(),
                   ID_OPERATOR,      CHANGE_FORMATION);
}   // initCells(const GLint, const Formation f)
bool Environment::initRobots(const Formation f)
{
	for (GLint i = 0; i < N_CELLS; ++i)
		if (!addCell(gXPos[i], gYPos[i], gHeading[i]))
			return false;

    // initialize each robot's neighborhood
    if (!initNbrs()) return false;

	setColor(BLACK);

    return (getNCells() == N_CELLS) &&
           sendMsg(new Formation(f), f.getSeedID(),
                   ID_OPERATOR,      CHANGE_FORMATION);
}
Beispiel #10
0
SOIL* GRID::getNbrGPtr (int C, int R, float direction, int distance)
{
    int maxLength = 0;
    maxLength=getNCells(3);
    distance %= maxLength; // restrict distance to length of the longest edge

		if (distance == 0)
			return & theGrid [C] [R];
		else
		{
			int hexDirection, onHexEdge;
			float sectorAngle = 360.0/float(NCellsOnPerimeter(distance));
			
			while (direction>=360.0) { direction -= 360.0; } // restrict direction
	  
			 // centre on hexagon
			hexDirection = MK_round(direction/sectorAngle); // number of cells on perimeter
			onHexEdge = hexDirection % distance; // number of cells on edge
	  
      switch (int(hexDirection/distance))
			{
        case 0: R -= distance;
                C += onHexEdge; break;
        case 1: C += distance;
                R -= distance; 
                R += onHexEdge; break;
        case 2: C += distance; 
                C -= onHexEdge;
                R += onHexEdge; break;
        case 3: R += distance; 
                C -= onHexEdge; break;
        case 4: C -= distance;
                R += distance;
                R -= onHexEdge; break;
        case 5:	C -= distance;
                C += onHexEdge;
                R -= onHexEdge; break;
	  }
		
		// wrap around torus
    while (C < 0) C += theGridLengthC;	// necessary for rectangles with unequal edge length
			C %= theGridLengthC;							// because 'distance' is restricted to the longest
    while (R < 0) R += theGridLengthR;	// edge and not the shortest.
      R %= theGridLengthR;
      
    return & theGrid [C] [R];
  }
}
Beispiel #11
0
float *Dataslc::compArea(int &len, float **funx)
{
   float *val = (float *)malloc(sizeof(float)*FSAMPLES);
   float *cum = (float *)malloc(sizeof(float)*FSAMPLES);
   float *fx = (float *)malloc(sizeof(float)*FSAMPLES);
   int c;
   u_int *v;
   float sum;
   int b;

   len = FSAMPLES;
   memset(val, 0, sizeof(float)*len);
   memset(cum, 0, sizeof(float)*len);

   *funx = fx;
   for (c=0; c<len; c++)
      fx[c] = getMin() + (c/(len-1.0)) * (getMax()-getMin());

   for (c=0; c<getNCells(); c++) {
      v = getCellVerts(c);

      triVolIntegral(getVert(v[0]), getVert(v[1]), getVert(v[2]),
                     getValue(v[0]), getValue(v[1]), getValue(v[2]), fx, val, cum,
                     len, getMin(), getMax(), 1.0);

   }

   // now we need to sum from the first to last
   sum = 0.0;
   for (b=0; b<len; b++) {
      val[b]+=sum;
      sum+=cum[b];
   }

   free(cum);

   return(val);
}
Beispiel #12
0
//------------------------------------------------------------------------
//
// commonConstructor() - called by the constructors to initialize a
//                       volume
//
//------------------------------------------------------------------------
void
Dataslc::commonConstructor(Data::DataType t, int ndata, char *fn)
{
   int i;
   float grad[3];
   float len;

   verts = (double (*)[2])malloc(sizeof(double[2])*getNVerts());
   vgrad = (float (*)[3])malloc(sizeof(float[3])*getNVerts());
   cells = (u_int (*)[3])malloc(sizeof(u_int[3])*getNCells());
   celladj = (int (*)[3])malloc(sizeof(int[3])*getNCells());

printf("reading verts\n");
   fread(verts, sizeof(double[2]), getNVerts(), fp);
printf("reading cells\n");
   for (i=0; i<getNCells(); i++) {
      fread(cells[i], sizeof(u_int[3]), 1, fp);
      fread(celladj[i], sizeof(int[3]), 1, fp);
   }

   for (i=0; i<getNCells(); i++) {
      for (int j=0; j<getNCellFaces(); j++) {
         int adj = celladj[i][j];
         int same = 0;
         if (adj != -1) {
         for (int k=0; k<3; k++)
            for (int l=0; l<3; l++)
               if (cells[i][k] == cells[adj][l])
                  same++;
         if (same != 2)
            printf("cell %d (%d %d %d) not adj to %d (%d %d %d)\n",
                   i, cells[i][0], cells[i][1], cells[i][2],
                 adj, cells[adj][0], cells[adj][1], cells[adj][2]);
        }
      }
   }

   readData();

   for (i=0; i<getNCells(); i++) {
      getCellGrad(i, grad);
      vgrad[getCellVert(i,0)][0] += grad[0];
      vgrad[getCellVert(i,0)][1] += grad[1];
      vgrad[getCellVert(i,0)][2] += grad[2];
      vgrad[getCellVert(i,1)][0] += grad[0];
      vgrad[getCellVert(i,1)][1] += grad[1];
      vgrad[getCellVert(i,1)][2] += grad[2];
      vgrad[getCellVert(i,2)][0] += grad[0];
      vgrad[getCellVert(i,2)][1] += grad[1];
      vgrad[getCellVert(i,2)][2] += grad[2];
   }

   for (i=0; i<getNVerts(); i++) {
//printf("scaling vgrad %d\n", i);
      len = sqrt(vgrad[i][0]*vgrad[i][0] + vgrad[i][1]*vgrad[i][1] +
                 vgrad[i][2]*vgrad[i][2]);
      if (len != 0.0) {
         vgrad[i][0] /= len;
         vgrad[i][1] /= len;
         vgrad[i][2] /= len;
      }
   }
}
Beispiel #13
0
Dataslc::Dataslc(Data::DataType t, int ndata, int nverts, int ncells,
		 double *_verts, u_int *_cells, int *_celladj, u_char *data)
		 : Data(t, ndata, data) 
{
   int i;
   float grad[3];
   float len;

   Dataslc::nverts = nverts;			// initializations
   Dataslc::ncells = ncells;

   verts   = (double (*)[2])_verts;
   cells   = (u_int (*)[3])_cells;
   celladj = (int (*)[3])_celladj;

   printf("computing extent\n");		// compute data extents

   minext[0] = minext[1] =
   maxext[0] = maxext[1] =
   minext[2] = maxext[2] = 0.0;

   for (i = 0; i < nverts; i++)
        {
        if (verts[i][0] < minext[0])
            minext[0] = verts[i][0];
        if (verts[i][0] > maxext[0])
            maxext[0] = verts[i][0];
        if (verts[i][1] < minext[1])
            minext[1] = verts[i][1];
        if (verts[i][1] > maxext[1])
            maxext[1] = verts[i][1];
	} 

   //fread(minext, sizeof(float[3]), 1, fp);
   //fread(maxext, sizeof(float[3]), 1, fp);

   printf("  min = %f %f %f  max = %f %f %f\n",
	   minext[0], minext[1], minext[2],
	   maxext[0], maxext[1], maxext[2]);

   //fread(&nverts, sizeof(int), 1, fp);
   //fread(&ncells, sizeof(int), 1, fp);

   printf("%d verts, %d cells\n", Dataslc::nverts, Dataslc::ncells);

						// compute gradients

   vgrad = (float (*)[3])malloc(sizeof(float[3])*getNVerts());

   printf("processing cells\n");

   for (i=0; i<getNCells(); i++) {
      for (int j=0; j<getNCellFaces(); j++) {
         int adj = celladj[i][j];
         int same = 0;
         if (adj != -1) {
         for (int k=0; k<3; k++)
            for (int l=0; l<3; l++)
               if (cells[i][k] == cells[adj][l])
                  same++;
         if (same != 2)
            printf("cell %d (%d %d %d) not adj to %d (%d %d %d)\n",
                   i, cells[i][0], cells[i][1], cells[i][2],
                 adj, cells[adj][0], cells[adj][1], cells[adj][2]);
        }
      }
   }

   preprocessData(data);

   for (i=0; i<getNCells(); i++) {
      getCellGrad(i, grad);
      vgrad[getCellVert(i,0)][0] += grad[0];
      vgrad[getCellVert(i,0)][1] += grad[1];
      vgrad[getCellVert(i,0)][2] += grad[2];
      vgrad[getCellVert(i,1)][0] += grad[0];
      vgrad[getCellVert(i,1)][1] += grad[1];
      vgrad[getCellVert(i,1)][2] += grad[2];
      vgrad[getCellVert(i,2)][0] += grad[0];
      vgrad[getCellVert(i,2)][1] += grad[1];
      vgrad[getCellVert(i,2)][2] += grad[2];
   }

   for (i=0; i<getNVerts(); i++) {
//printf("scaling vgrad %d\n", i);
      len = sqrt(vgrad[i][0]*vgrad[i][0] + vgrad[i][1]*vgrad[i][1] +
                 vgrad[i][2]*vgrad[i][2]);
      if (len != 0.0) {
         vgrad[i][0] /= len;
         vgrad[i][1] /= len;
         vgrad[i][2] /= len;
      }
   }
}