QuonPDefs.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 <QuonPDefs.h>
00025 
00026 QPBoundingBox::QPBoundingBox() {}
00027 
00028 QPBoundingBox::QPBoundingBox(Vector2D tl, Vector2D br)
00029 {
00030     this->tl = tl;
00031     this->br = br;
00032 }
00033 
00034 QPBoundingBox::QPBoundingBox(double tlx, double tly, double brx, double bry)
00035 {
00036     tl.x = tlx;
00037     tl.y = tly;
00038     br.x = brx;
00039     br.y = bry;
00040 }
00041 
00042 QPBoundingBox::QPBoundingBox(Vector2D center, double width)
00043 {
00044     width /= 2.0;
00045     tl.x = center.x - width;
00046     tl.y = center.y + width;
00047     br.x = center.x + width;
00048     br.y = center.y - width;
00049 }
00050 
00051 void QPBoundingBox::resize(double width)
00052 {
00053     Vector2D center = (tl + br) / 2.0;
00054     width /= 2.0;
00055     tl.x = center.x - width;
00056     tl.y = center.y + width;
00057     br.x = center.x + width;
00058     br.y = center.y - width;
00059 }
00060 
00061 bool QPBoundingBox::collide(const Vector2D p) const
00062 {
00063     if(p.x > tl.x && p.x < br.x && p.y < tl.y && p.y > br.y) {
00064         return true;
00065     }
00066     return false;
00067 }
00068 
00069 std::ostream& operator<<(std::ostream& Stream, const QPBoundingBox& box)
00070 {
00071     return Stream << box.tl << " - " << box.br;
00072 }
00073 
00074 QPCircle::QPCircle()
00075 {
00076     radius = 0.0;
00077 }
00078 
00079 QPCircle::QPCircle(Vector2D center, double diameter)
00080 {
00081     this->center = center;
00082     radius = diameter / 2.0;
00083 }
00084 
00085 void QPCircle::resize(double diameter)
00086 {
00087     radius = diameter / 2.0;
00088 }
00089 
00090 bool QPCircle::collide(const Vector2D p) const
00091 {
00092     if(center.distanceSqr(p) < (radius*radius))
00093     {
00094         return true;
00095     }
00096     return false;
00097 }
00098 
00099 std::ostream& operator<<(std::ostream& Stream, const QPCircle& circle)
00100 {
00101     return Stream << circle.center << " - " << 2.0 * circle.radius;
00102 }
00103 
00104 QPSite::QPSite()
00105 {
00106     type = QUNDEFINED;
00107     dirty = false;
00108     alive = false;
00109     address = NodeHandle::UNSPECIFIED_NODE;
00110     AOIwidth = 0.0;
00111 }
00112 
00113 std::ostream& operator<<(std::ostream& Stream, const QPSite& s)
00114 {
00115     Stream << s.address.ip << ":" << s.address.port << " Type: ";
00116     switch(s.type) {
00117         case QUNDEFINED:
00118             Stream << "\"Undefined\"";
00119             break;
00120         case QTHIS:
00121             Stream << "\"Self\"";
00122             break;
00123         case QNEIGHBOR:
00124             Stream << "\"Direct Neighbor\"";
00125             break;
00126         case QBINDING:
00127             Stream << "\"Binding Neighbor\"";
00128             break;
00129         case QSOFT:
00130             Stream << "\"Softstate Neighbor\"";
00131             break;
00132     }
00133     Stream << " Position: " << s.position;
00134     return Stream;
00135 }
00136 
00137 PTreeNode::PTreeNode(PTreeNode* parent)
00138 {
00139     this->parent = parent;
00140     contents = NULL;
00141     child[0] = NULL;
00142     child[1] = NULL;
00143     child[2] = NULL;
00144     child[3] = NULL;
00145 }
00146 
00147 void PTreeNode::add(QPSite* site)
00148 {
00149     if(contents == NULL) {
00150         contents = site;
00151     }
00152     else {
00153         int i = getQuadrant(site->position);
00154         if(child[i] == NULL) {
00155             child[i] = new PTreeNode(this);
00156         }
00157         child[i]->add(site);
00158     }
00159 }
00160 
00161 void PTreeNode::reset()
00162 {
00163     for(int i=0; i<4; i++) {
00164         if(child[i] != NULL) {
00165             child[i]->reset();
00166             delete child[i];
00167             child[i] = NULL;
00168         }
00169     }
00170     contents = NULL;
00171 }
00172 
00173 QPSite* PTreeNode::findNearestNeighbor(const Vector2D p) const
00174 {
00175     QPSite* temp = NULL;
00176     QPSite* best = contents;
00177     double distance = contents->position.distanceSqr(p);
00178     for(int i=0; i<4; i++) {
00179         if(child[i] != NULL) {
00180             temp = child[i]->findNearestNeighbor(p);
00181             if(temp->position.distanceSqr(p) < distance) {
00182                 best = temp;
00183                 distance = temp->position.distanceSqr(p);
00184             }
00185         }
00186     }
00187     return best;
00188 }
00189 
00190 int PTreeNode::getQuadrant(const Vector2D p) const
00191 {
00192     int quadrant;
00193     if(p.x > contents->position.x) {
00194         if(p.y < contents->position.y) {
00195             quadrant = 2;
00196         }
00197         else {
00198             quadrant = 1;
00199         }
00200     }
00201     else {
00202         if(p.y < contents->position.y) {
00203             quadrant = 3;
00204         }
00205         else {
00206             quadrant = 0;
00207         }
00208     }
00209     return quadrant;
00210 }

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