#include <NeighborsList.h>
Maintains a list of all neighbors and functions for building their voronoi diagram.
Public Member Functions | |
virtual | ~NeighborsList () |
virtual int | numInitStages () const |
virtual void | initialize (int stage) |
virtual void | handleMessage (cMessage *msg) |
void | initializeList (Vector2D p, NodeHandle owner, double AOI_size) |
void | setDebug (bool debugOutput) |
void | addNode (Vector2D p, NodeHandle node) |
void | updateTimeStamp (NodeHandle node) |
void | removeNode (NodeHandle node) |
void | changePosition (Vector2D p) |
bool | validatePosition (Vector2D p) |
void | buildVoronoi () |
void | buildVoronoi (Vector2D old_pos, Vector2D new_pos) |
void | removeNeighbors () |
int | getSize () |
void | clearList () |
Public Attributes | |
SiteMap | Sites |
Site | thisSite |
Protected Member Functions | |
void | updateDisplayString () |
Protected Attributes | |
PositionSet | Positions |
double | AOI_size |
bool | debugOutput |
Geometry | geom |
EdgeList | edgelist |
HeapPQ | heap |
NeighborsList::~NeighborsList | ( | ) | [virtual] |
virtual int NeighborsList::numInitStages | ( | void | ) | const [inline, virtual] |
void NeighborsList::initialize | ( | int | stage | ) | [virtual] |
00029 { 00030 // because of IPAddressResolver, we need to wait until interfaces are registered, 00031 // address auto-assignment takes place etc. 00032 if(stage != MIN_STAGE_OVERLAY) return; 00033 00034 WATCH(thisSite); 00035 WATCH_MAP(Sites); 00036 WATCH_SET(Positions); 00037 }
void NeighborsList::handleMessage | ( | cMessage * | msg | ) | [virtual] |
void NeighborsList::initializeList | ( | Vector2D | p, | |
NodeHandle | owner, | |||
double | AOI_size | |||
) |
00045 { 00046 Enter_Method_Silent(); 00047 // Initializes the neigbors list. 00048 thisSite.coord = p; 00049 thisSite.type = THIS; 00050 thisSite.addr = owner; 00051 00052 //Sites.insert(std::make_pair(temp_site.addr, temp_site)); 00053 //Positions.insert(temp_site.coord); 00054 this->AOI_size = AOI_size; 00055 updateDisplayString(); 00056 }
void NeighborsList::setDebug | ( | bool | debugOutput | ) |
00059 { 00060 Enter_Method_Silent(); 00061 this->debugOutput = debugOutput; 00062 geom.setDebug(debugOutput); 00063 }
void NeighborsList::addNode | ( | Vector2D | p, | |
NodeHandle | node | |||
) |
00066 { 00067 Enter_Method_Silent(); 00068 if(node != thisSite.addr) { 00069 if(Sites.find(node) == Sites.end()) { 00070 Site temp_site; 00071 temp_site.coord = p; 00072 temp_site.addr = node; 00073 00074 Sites.insert(std::make_pair(temp_site.addr, temp_site)); 00075 Positions.insert(temp_site.coord); 00076 00077 updateDisplayString(); 00078 } 00079 else { 00080 SiteMap::iterator itSites = Sites.find(node); 00081 Positions.erase(itSites->second.coord); 00082 itSites->second.coord = p; 00083 Positions.insert(itSites->second.coord); 00084 } 00085 } 00086 }
void NeighborsList::updateTimeStamp | ( | NodeHandle | node | ) |
void NeighborsList::removeNode | ( | NodeHandle | node | ) |
00096 { 00097 Enter_Method_Silent(); 00098 SiteMap::iterator itSites = Sites.find(node); 00099 if(itSites != Sites.end()) { 00100 Positions.erase(itSites->second.coord); 00101 Sites.erase(itSites); 00102 updateDisplayString(); 00103 } 00104 }
void NeighborsList::changePosition | ( | Vector2D | p | ) |
bool NeighborsList::validatePosition | ( | Vector2D | p | ) |
void NeighborsList::buildVoronoi | ( | ) |
00119 { 00120 Enter_Method_Silent(); 00121 int sqrt_nsites = 1; 00122 double xmin, xmax, ymin, ymax; 00123 double deltax, deltay; 00124 00125 SiteMap::iterator itTemp; 00126 std::map<Vector2D, Site*> sortedSites; 00127 std::map<Vector2D, Site*>::iterator itSortedSites; 00128 00129 // check wether there are any neighbors 00130 if(Sites.size() == 0) return; 00131 00132 xmin = xmax = thisSite.coord.x; 00133 ymin = ymax = thisSite.coord.y; 00134 sortedSites.insert(std::make_pair(thisSite.coord, &thisSite)); 00135 00136 for(itTemp = Sites.begin(); itTemp != Sites.end(); ++itTemp) { 00137 // determine min/max site coordinates 00138 if(itTemp->second.coord.x < xmin) xmin = itTemp->second.coord.x; 00139 if(itTemp->second.coord.x > xmax) xmax = itTemp->second.coord.x; 00140 if(itTemp->second.coord.y < ymin) ymin = itTemp->second.coord.y; 00141 if(itTemp->second.coord.y > ymax) ymax = itTemp->second.coord.y; 00142 // reset all sites to UNDEF 00143 itTemp->second.type = UNDEF; 00144 // fill sorted List 00145 sortedSites.insert(std::make_pair(itTemp->second.coord, &itTemp->second)); 00146 sqrt_nsites++; 00147 } 00148 00149 // needed to determine appropriate hashtable size 00150 deltax = xmax - xmin; 00151 deltay = ymax - ymin; 00152 sqrt_nsites = (int)sqrt((double)(sqrt_nsites+4)); 00153 00154 // start to calculate the voronoi 00155 Site *newsite, *bot, *top, *temp, *p, *v, *bottomsite; 00156 Vector2D newintstar; 00157 int pm; 00158 Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector; 00159 Edge *e; 00160 00161 newintstar.x = newintstar.y = 0.0; 00162 00163 itSortedSites = sortedSites.begin(); 00164 00165 geom.initialize(deltax, deltay, thisSite.coord, old_pos, new_pos, AOI_size); 00166 heap.PQinitialize(sqrt_nsites, ymin, deltay); 00167 bottomsite = itSortedSites->second; 00168 ++itSortedSites; 00169 edgelist.initialize(sqrt_nsites, xmin, deltax, bottomsite); 00170 00171 newsite = itSortedSites->second; 00172 ++itSortedSites; 00173 while(1) { 00174 if(!heap.PQempty()) newintstar = heap.PQ_min(); 00175 00176 if(newsite != NULL && (heap.PQempty() || 00177 newsite->coord.y < newintstar.y || 00178 (newsite->coord.y == newintstar.y && newsite->coord.x < newintstar.x))) { 00179 lbnd = edgelist.ELleftbnd(&(newsite->coord)); 00180 rbnd = edgelist.ELright(lbnd); 00181 bot = edgelist.rightreg(lbnd); 00182 e = geom.bisect(bot, newsite); 00183 bisector = edgelist.HEcreate(e, le); 00184 edgelist.ELinsert(lbnd, bisector); 00185 if ((p = geom.intersect(lbnd, bisector)) != NULL) { 00186 heap.PQdelete(lbnd); 00187 heap.PQinsert(lbnd, p, geom.dist(p, newsite)); 00188 } 00189 lbnd = bisector; 00190 bisector = edgelist.HEcreate(e, re); 00191 edgelist.ELinsert(lbnd, bisector); 00192 if ((p = geom.intersect(bisector, rbnd)) != NULL) heap.PQinsert(bisector, p, geom.dist(p, newsite)); 00193 if(itSortedSites != sortedSites.end()) { 00194 newsite = itSortedSites->second; 00195 ++itSortedSites; 00196 } 00197 else newsite = NULL; 00198 } 00199 else if (!heap.PQempty()) { 00200 lbnd = heap.PQextractmin(); 00201 llbnd = edgelist.ELleft(lbnd); 00202 rbnd = edgelist.ELright(lbnd); 00203 rrbnd = edgelist.ELright(rbnd); 00204 bot = edgelist.leftreg(lbnd); 00205 top = edgelist.rightreg(rbnd); 00206 v = lbnd->vertex; 00207 geom.endpoint(lbnd->ELedge, lbnd->ELpm, v); 00208 geom.endpoint(rbnd->ELedge, rbnd->ELpm, v); 00209 edgelist.ELdelete(lbnd); 00210 heap.PQdelete(rbnd); 00211 edgelist.ELdelete(rbnd); 00212 pm = le; 00213 if (bot->coord.y > top->coord.y) { 00214 temp = bot; 00215 bot = top; 00216 top = temp; 00217 pm = re; 00218 } 00219 e = geom.bisect(bot, top); 00220 bisector = edgelist.HEcreate(e, pm); 00221 edgelist.ELinsert(llbnd, bisector); 00222 geom.endpoint(e, re-pm, v); 00223 if((p = geom.intersect(llbnd, bisector)) != NULL) { 00224 heap.PQdelete(llbnd); 00225 heap.PQinsert(llbnd, p, geom.dist(p, bot)); 00226 } 00227 if ((p = geom.intersect(bisector, rrbnd)) != NULL) heap.PQinsert(bisector, p, geom.dist(p, bot)); 00228 } 00229 else break; 00230 } 00231 00232 // process the generated edgelist 00233 for(lbnd = edgelist.ELright(edgelist.ELleftend); lbnd != edgelist.ELrightend; lbnd = edgelist.ELright(lbnd)) { 00234 e = lbnd -> ELedge; 00235 geom.processEdge(e); 00236 } 00237 // process sites in order to determine our neighbors 00238 for(itTemp = Sites.begin(); itTemp != Sites.end(); ++itTemp) { 00239 if(itTemp->second.innerEdge[0]) { 00240 if(itTemp->second.outerEdge) { 00241 itTemp->second.type |= BOUNDARY; 00242 // Debug output 00243 if(debugOutput) 00244 ev << " VAST: Site at [" << itTemp->second.coord.x << ", " 00245 << itTemp->second.coord.y << "] is a boundary neighbor." << endl; 00246 } 00247 else { 00248 itTemp->second.type |= NEIGHBOR; 00249 // Debug output 00250 if(debugOutput) 00251 ev << " VAST: Site at [" << itTemp->second.coord.x << ", " 00252 << itTemp->second.coord.y << "] is a neighbor." << endl; 00253 } 00254 } 00255 if(!itTemp->second.innerEdge[1] && itTemp->second.innerEdge[2]) { 00256 itTemp->second.type |= NEW; 00257 // Debug output 00258 if(debugOutput) 00259 ev << " VAST: Site at [" << itTemp->second.coord.x << ", " 00260 << itTemp->second.coord.y << "] is a new neighbor for site at " << new_pos.x << ":" << new_pos.y << "." << endl; 00261 } 00262 // reset inner- and outeredge indicator 00263 itTemp->second.innerEdge[0] = false; 00264 itTemp->second.innerEdge[1] = false; 00265 itTemp->second.innerEdge[2] = false; 00266 itTemp->second.outerEdge = false; 00267 } 00268 // clean up 00269 edgelist.reset(); 00270 heap.PQreset(); 00271 geom.reset(); 00272 }
void NeighborsList::removeNeighbors | ( | ) |
00280 { 00281 Enter_Method_Silent(); 00282 SiteMap::iterator itSites; 00283 for(itSites = Sites.begin(); itSites != Sites.end();) { 00284 // if current site is no neighbor remove it else go on to next site 00285 if(itSites->second.type == UNDEF) { 00286 // Debug output 00287 if(debugOutput) ev << " VAST: Site at [" << itSites->second.coord.x << ", " << itSites->second.coord.y << "] has been removed from list." << endl; 00288 Positions.erase(itSites->second.coord); 00289 Sites.erase(itSites++); 00290 } 00291 else ++itSites; 00292 } 00293 updateDisplayString(); 00294 }
int NeighborsList::getSize | ( | ) |
void NeighborsList::clearList | ( | ) |
void NeighborsList::updateDisplayString | ( | ) | [protected] |
00297 { 00298 // Displays the current number of neighbors in the list. 00299 if(ev.isGUI()) { 00300 std::stringstream cur; 00301 if (Sites.size() == 1) { 00302 cur << "1 neighbor"; 00303 } 00304 else { 00305 cur << Sites.size() << " neigbors"; 00306 } 00307 displayString().setTagArg("t", 0, cur.str().c_str()); 00308 displayString().setTagArg("t", 2, "blue"); 00309 } 00310 }
PositionSet NeighborsList::Positions [protected] |
double NeighborsList::AOI_size [protected] |
bool NeighborsList::debugOutput [protected] |
Geometry NeighborsList::geom [protected] |
EdgeList NeighborsList::edgelist [protected] |
HeapPQ NeighborsList::heap [protected] |