// // void sortByDistance(v) // Last modified: 29Nov2009 // // Sorts the neighborhood based upon neighbor distance // as determined by the parameterized difference vector. // // Returns: <none> // Parameters: // v in the difference vector // void Neighborhood::sortByDistance(const Vector v) { for (GLint i = 0; i < size() - 1; ++i) for (GLint j = i; j < size(); ++j) if ((getNbr(i)->relActual - v).magnitude() > (getNbr(j)->relActual - v).magnitude()) swapNbrs(i, j); } // sortByDistance(const Vector)
// // void sortByGradient(v) // Last modified: 29Nov2009 // // Sorts the neighborhood based upon neighbor gradient // as determined by the parameterized difference vector. // // Returns: <none> // Parameters: // v in the difference vector // void Neighborhood::sortByGradient(const Vector v) { for (GLint i = 0; i < size() - 1; ++i) for (GLint j = i; j < size(); ++j) if ((getNbr(i)->gradient - v).magnitude() > (getNbr(j)->gradient - v).magnitude()) swapNbrs(i, j); } // sortByGradient(const Vector)
// // void sortByAbsAngle(v) // Last modified: 29Nov2009 // // Sorts the neighborhood based upon the absolute value of the angle // of the neighbor-relative and the parameterized difference vector. // // Returns: <none> // Parameters: // v in the difference vector // void Neighborhood::sortByAbsAngle(const Vector v) { for (GLint i = 0; i < size() - 1; ++i) for (GLint j = i; j < size(); ++j) if (abs((getNbr(i)->relActual - v).angle()) > abs((getNbr(j)->relActual - v).angle())) swapNbrs(i, j); } // sortByAbsAngle(const Vector)
// Sorts the neighborhood based upon the angle of the // neighbor-relative and the parameterized difference vector. void Neighborhood::sortByAngle(const Vector v) { for (unsigned i = 0; i < size() - 1; ++i) for (unsigned j = i; j < size(); ++j) if ((getNbr(i)->relActual - v).angle() > (getNbr(j)->relActual - v).angle()) swapNbrs(i, j); }
// Sorts the neighborhood based upon neighbor frp // as determined by the parameterized difference vector. void Neighborhood::sortByFrp(const Vector v) { for (unsigned i = 0; i < size() - 1; ++i) for (unsigned j = i; j < size(); ++j) if ((getNbr(i)->frp - v).magnitude() > (getNbr(j)->frp - v).magnitude()) swapNbrs(i, j); }
// // bool sendStateToNbrs() // Last modified: 27Aug2006 // // Attempts to broadcast the state of the cell // to the neighborhood of the cell, returning // true if successful, false otherwise. // // Returns: true if successful, false otherwise // Parameters: <none> // bool Cell::sendStateToNbrs() { Neighbor curr; for (GLint i = 0; i < getNNbrs(); ++i) if (!sendState(getNbr(i)->ID)) return false; return true; } // sendStateToNbrs()
// // void updateState() // Last modified: 27Aug2006 // // Updates the state of the cell based upon the // current states of the neighbors of the cell. // // Returns: <none> // Parameters: <none> // void Cell::updateState() { Neighbor currNbr; for (GLint i = 0; i < getNNbrs(); ++i) { if (!getHead(currNbr)) break; // change formation if a neighbor has changed formation if (getNbr(0)->formation.getFormationID() > formation.getFormationID()) changeFormation(getNbr(0)->formation, *getNbr(0)); getNbr(0)->relActual = getRelationship(currNbr.ID); ++(*this); } rels = getRelationships(); if(rels.getSize()) { // reference the neighbor with the smallest gradient // to establish correct position in formation Neighbor *refNbr = nbrWithMinGradient(); Relationship *nbrRel = relWithID(refNbr->rels, ID); if ((formation.getSeedID() != ID) && (refNbr != NULL) && (nbrRel != NULL)) { // error (state) is based upon the accumulated error in the formation nbrRel->relDesired.rotateRelative(-refNbr->rotError); GLfloat theta = scaleDegrees(nbrRel->relActual.angle() - (-refNbr->relActual).angle()); rotError = scaleDegrees(theta + refNbr->rotError); transError = nbrRel->relDesired - nbrRel->relActual + refNbr->transError; transError.rotateRelative(-theta); if (transError.norm() > threshold()) moveArc(transError); else if (abs(rotError) > angThreshold()) moveArc(0.0, degreesToRadians(-rotError)); /*if (abs(scaleDegrees(refNbr->relActual.angle() - refNbr->relDesired.angle())) > angThreshold()) orientTo(refNbr->relActual, refNbr->relDesired.angle());*/ else moveStop(); } else moveStop(); } } // updateState()
// // void sortByID() // Last modified: 29Nov2009 // // Sorts the neighborhood based upon neighbor ID. // // Returns: <none> // Parameters: <none> // void Neighborhood::sortByID() { for (GLint i = 0; i < size() - 1; ++i) for (GLint j = i; j < size(); ++j) if (getNbr(i)->ID > getNbr(j)->ID) swapNbrs(i, j); } // sortByID()