QuonPRDefs.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <QuonPRDefs.h>
00025 
00026 QPRBoundingBox::QPRBoundingBox() {}
00027 
00028 QPRBoundingBox::QPRBoundingBox(Vector2D tl, Vector2D br)
00029 {
00030     this->tl = tl;
00031     this->br = br;
00032     center = (tl + br) * 0.5;
00033 }
00034 
00035 QPRBoundingBox::QPRBoundingBox(double tlx, double tly, double brx, double bry)
00036 {
00037     tl.x = tlx;
00038     tl.y = tly;
00039     br.x = brx;
00040     br.y = bry;
00041     center = (tl + br) * 0.5;
00042 }
00043 
00044 QPRBoundingBox::QPRBoundingBox(Vector2D center, double width)
00045 {
00046     this->center = center;
00047     tl.x = center.x - width * 0.5;
00048     tl.y = center.y + width * 0.5;
00049     br.x = center.x + width * 0.5;
00050     br.y = center.y - width * 0.5;
00051 }
00052 
00053 void QPRBoundingBox::resize(double width)
00054 {
00055     width /= 2.0;
00056     tl.x = center.x - width;
00057     tl.y = center.y + width;
00058     br.x = center.x + width;
00059     br.y = center.y - width;
00060 }
00061 
00062 
00063 bool QPRBoundingBox::collide(const QPRBoundingBox box) const
00064 {
00065     if(tl.x > box.br.x) {
00066         return false;
00067     }
00068     if(tl.y < box.br.y) {
00069         return false;
00070     }
00071     if(br.x < box.tl.x) {
00072         return false;
00073     }
00074     if(br.y > box.tl.y) {
00075         return false;
00076     }
00077 
00078     return true;
00079 }
00080 
00081 bool QPRBoundingBox::collide(const Vector2D p) const
00082 {
00083     if(p.x > tl.x && p.x < br.x && p.y < tl.y && p.y > br.y) {
00084         return true;
00085     }
00086     else {
00087         return false;
00088     }
00089 }
00090 
00091 int QPRBoundingBox::getChildNum(const Vector2D p) const
00092 {
00093     int childnum;
00094     if(p.x > center.x) {
00095         if(p.y < center.y) {
00096             childnum = 2;
00097         }
00098         else {
00099             childnum = 1;
00100         }
00101     }
00102     else {
00103         if(p.y < center.y) {
00104             childnum = 3;
00105         }
00106         else {
00107             childnum = 0;
00108         }
00109     }
00110     return childnum;
00111 }
00112 
00113 QPRBoundingBox QPRBoundingBox::getQuadrant(int i) const
00114 {
00115     Vector2D quadTl = tl;
00116     Vector2D quadBr = center;
00117     double width = center.x - tl.x;
00118     if(i == 1 || i == 2) {
00119         quadTl.x += width;
00120         quadBr.x += width;
00121     }
00122     if(i == 2 || i == 3) {
00123         quadTl.y -= width;
00124         quadBr.y -= width;
00125     }
00126 
00127     return QPRBoundingBox(quadTl, quadBr);
00128 }
00129 
00130 Vector2D QPRBoundingBox::shiftCenter(PRDirection direction) const
00131 {
00132     Vector2D temp = center;
00133     double width = br.x - tl.x;
00134     switch(direction) {
00135         case NW:
00136             temp.x -= width;
00137             temp.y += width;
00138         break;
00139         case N:
00140             temp.y += width;
00141         break;
00142         case NE:
00143             temp.x += width;
00144             temp.y += width;
00145         break;
00146         case E:
00147             temp.x += width;
00148         break;
00149         case SE:
00150             temp.x += width;
00151             temp.y -= width;
00152         break;
00153         case S:
00154             temp.y -= width;
00155         break;
00156         case SW:
00157             temp.x -= width;
00158             temp.y -= width;
00159         break;
00160         case W:
00161             temp.x -= width;
00162         break;
00163     }
00164     return temp;
00165 }
00166 
00167 std::ostream& operator<<(std::ostream& Stream, const QPRBoundingBox& box)
00168 {
00169     return Stream << box.tl << " - " << box.br;
00170 }
00171 
00172 QPRSite::QPRSite()
00173 {
00174     PRNode = NULL;
00175     type = QUNDEFINED;
00176     dirty = false;
00177     alive = false;
00178     address = NodeHandle::UNSPECIFIED_NODE;
00179     AOIwidth = 0.0;
00180 }
00181 
00182 std::ostream& operator<<(std::ostream& Stream, const QPRSite& s)
00183 {
00184     Stream << s.address.ip << ":" << s.address.port << " Type: ";
00185     switch(s.type) {
00186         case QUNDEFINED:
00187             Stream << "\"Undefined\"";
00188         break;
00189         case QTHIS:
00190             Stream << "\"Self\"";
00191         break;
00192         case QNEIGHBOR:
00193             Stream << "\"Direct Neighbor\"";
00194         break;
00195         case QBINDING:
00196             Stream << "\"Binding Neighbor\"";
00197         break;
00198         case QSOFT:
00199             Stream << "\"Softstate Neighbor\"";
00200         break;
00201     }
00202     Stream << " Position: " << s.position;
00203     return Stream;
00204 }
00205 
00206 PRTreeNode::PRTreeNode(QPRBoundingBox region, PRTreeNode* parent)
00207 {
00208     this->region = region;
00209     this->parent = parent;
00210     contents = NULL;
00211     child[0] = NULL;
00212     child[1] = NULL;
00213     child[2] = NULL;
00214     child[3] = NULL;
00215 }
00216 
00217 void PRTreeNode::add(QPRSite* site)
00218 {
00219     int i = region.getChildNum(site->position);
00220     if(child[i] == NULL) {
00221         child[i] = new PRTreeNode(region.getQuadrant(i), this);
00222         child[i]->contents = site;
00223         site->PRNode = child[i];
00224 
00225         if(contents != NULL) {
00226             i = region.getChildNum(contents->position);
00227             if(child[i] == NULL) {
00228                 child[i] = new PRTreeNode(region.getQuadrant(i), this);
00229                 child[i]->contents = contents;
00230                 contents->PRNode = child[i];
00231             }
00232             else {
00233                 child[i]->add(contents);
00234             }
00235             contents = NULL;
00236         }
00237     }
00238     else {
00239         child[i]->add(site);
00240     }
00241 }
00242 
00243 void PRTreeNode::remove(const Vector2D p)
00244 {
00245     int i = region.getChildNum(p);
00246     if(child[i]->contents != NULL && child[i]->contents->position == p) {
00247         delete child[i];
00248         child[i] = NULL;
00249         collapse();
00250     }
00251     else {
00252         child[i]->remove(p);
00253     }
00254 }
00255 
00256 QPRSite* PRTreeNode::findBindingNeighbor(const Vector2D p, int direction, int maxdepth)
00257 {
00258     if(!region.collide(p)) {
00259         return NULL;
00260     }
00261     int depth = 0;
00262     PRTreeNode* node = this;
00263     while(depth < maxdepth && node->child[node->region.getChildNum(p)] != NULL) {
00264         node = node->child[node->region.getChildNum(p)];
00265         ++depth;
00266     }
00267 
00268     int reverseDirection = (direction + 4) % 8;
00269     int childNum = node->region.getChildNum(p);
00270     Vector2D pNear = node->region.shiftCenter(PRDirection(reverseDirection));
00271     if(node->contents != NULL) {
00272         return node->contents;
00273     }
00274     else {
00275         if((depth == maxdepth) ||
00276            (childNum == reverseDirection / 2) ||
00277            ((reverseDirection % 2 == 1) && (childNum == ((reverseDirection + 1) % 8) / 2))) {
00278             return node->findNearestNeighbor(pNear);
00279         }
00280         else {
00281             QPRBoundingBox box = node->region.getQuadrant(node->region.getChildNum(p));
00282             return findBindingNeighbor(box.shiftCenter(PRDirection(direction)), direction, getDepth(node) + 1);
00283         }
00284     }
00285 }
00286 
00287 QPRSite* PRTreeNode::findNearestNeighbor(const Vector2D p) const
00288 {
00289     if(contents != NULL) {
00290         return contents;
00291     }
00292     else {
00293         QPRSite* temp = NULL;
00294         QPRSite* best = NULL;
00295         double distance = QINFINITY;
00296         for(int i=0; i<4; i++) {
00297             if(child[i] != NULL) {
00298                 temp = child[i]->findNearestNeighbor(p);
00299                 if(temp->position.distanceSqr(p) < distance) {
00300                     distance = temp->position.distanceSqr(p);
00301                     best = temp;
00302                 }
00303             }
00304         }
00305         return best;
00306     }
00307 }
00308 
00309 int PRTreeNode::getDepth(const Vector2D p, int depth) const
00310 {
00311     if(contents != NULL && contents->position == p) {
00312         return depth;
00313     }
00314     else {
00315         return child[region.getChildNum(p)]->getDepth(p, depth + 1);
00316     }
00317 }
00318 
00319 int PRTreeNode::getDepth(const PRTreeNode* node, int depth) const
00320 {
00321     if(this == node) {
00322         return depth;
00323     }
00324     else {
00325         return child[region.getChildNum(node->region.center)]->getDepth(node, depth + 1);
00326     }
00327 }
00328 
00329 std::ostream& operator<<(std::ostream& Stream, const PRTreeNode& node)
00330 {
00331     return node.print(Stream);
00332 }
00333 
00334 void PRTreeNode::collapse()
00335 {
00336     int childNum = -1;
00337     for(int i=0; i<4; i++) {
00338         if(child[i] != NULL) {
00339             if(childNum < 0) {
00340                 childNum = i;
00341             }
00342             else {
00343                 childNum = -1;
00344                 break;
00345             }
00346         }
00347     }
00348     if(childNum >= 0 && child[childNum]->contents != NULL) {
00349         contents = child[childNum]->contents;
00350         delete child[childNum];
00351         child[childNum] = NULL;
00352         contents->PRNode = this;
00353         if(parent != NULL) {
00354             parent->collapse();
00355         }
00356     }
00357 }
00358 
00359 std::ostream& PRTreeNode::print(std::ostream& Stream, int depth) const
00360 {
00361     if(contents == NULL) {
00362         if(parent == NULL) {
00363             Stream << "#" << endl;
00364         }
00365         else {
00366             Stream << "|" << endl;
00367         }
00368         for(int i=0; i<4; i++) {
00369             for(int j=0; j<depth; j++) {
00370                 Stream << "   ";
00371             }
00372             Stream << "|==";
00373             if(child[i] != NULL) {
00374                 child[i]->print(Stream, depth + 1);
00375             }
00376             else {
00377                 Stream << "]" << endl;
00378             }
00379         }
00380     }
00381     else {
00382         if(parent == NULL) {
00383             Stream << "#" << endl << "|==";
00384         }
00385         Stream << "> " << *contents << endl;
00386     }
00387     return Stream;
00388 }

Generated on Wed Mar 25 16:32:49 2009 for OverSim by  doxygen 1.5.5