Skip to content

Commit

Permalink
Refactoring - QuadTreeNode extended to contain corner points, edges a…
Browse files Browse the repository at this point in the history
…nd and borders defined
  • Loading branch information
mniec committed Dec 7, 2011
1 parent 6c1979f commit 4d36a97
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
51 changes: 43 additions & 8 deletions QuadTree.cpp
Expand Up @@ -8,17 +8,17 @@

uint QuadTreeNode::MAX_LEVEL = 1000;

static int direction(const Point& a, const Point& b,const Point& c){
double res = (a.x-c.x)*(b.y-c.y) -(b.x-c.x)*(a.y-c.y);
int Edge::getSide(Point *c){
double res = (b->x-c->x)*(e->y-c->y) -(e->x-c->x)*(b->y-c->y);
if( res< 0 ) return -1;
else if (res > 0 ) return 1;
return 0;
}

bool Edge::intersects(Edge* e)
{
if( direction(*b,*(this->e),*(e->b)) == direction(*b,*(this->e),*(e->e) ))return false;
if( direction(*(e->b),*(e->e),*(this->b)) == direction(*(e->b),*(e->e),*(this->e))) return false;
if( getSide(e->b) == getSide(e->e))return false;
if( e->getSide(this->b)== e->getSide(this->e)) return false;
return true;
}

Expand Down Expand Up @@ -75,6 +75,20 @@ QuadTreeNode::QuadTreeNode(const Point& p, double size):lu_corner(p),level(0),si
S=0;
W=0;
E=0;

corners[P_NW] = Point(p);
corners[P_NE] = Point(p.x+size,p.y);
corners[P_SE] = Point(p.x+size,p.y+size);
corners[P_SW] = Point(p.x,p.y+size);

borders[P_N] = Edge(&corners[P_NW],&corners[P_NE]);
borders[P_E] = Edge(&corners[P_NE],&corners[P_SE]);
borders[P_S] = Edge(&corners[P_SE],&corners[P_SW]);
borders[P_W] = Edge(&corners[P_SW],&corners[P_NW]);

crossEdges = new Edge*[2];
crossEdges[0] = NULL;
crossEdges[1] = NULL;
}

QuadTreeNode::QuadTreeNode(const Point& p, double size,QuadTreeNode* parent,EQuadrant type):lu_corner(p),size(size),parent(parent)
Expand All @@ -88,6 +102,20 @@ QuadTreeNode::QuadTreeNode(const Point& p, double size,QuadTreeNode* parent,EQua
S=0;
W=0;
E=0;

corners[P_NW] = Point(p);
corners[P_NE] = Point(p.x+size,p.y);
corners[P_SE] = Point(p.x+size,p.y+size);
corners[P_SW] = Point(p.x,p.y+size);

borders[P_N] = Edge(&corners[P_NW],&corners[P_NE]);
borders[P_E] = Edge(&corners[P_NE],&corners[P_SE]);
borders[P_S] = Edge(&corners[P_SE],&corners[P_SW]);
borders[P_W] = Edge(&corners[P_SW],&corners[P_NW]);

crossEdges = new Edge*[2];
crossEdges[0] = NULL;
crossEdges[1] = NULL;
this->type=type;
}

Expand Down Expand Up @@ -133,7 +161,6 @@ void QuadTreeNode::subdivide()
if(type==EQ_NW)W=parent->W->NE.data();
if(type==EQ_SW)W=parent->W->SE.data();
}

}

//filling obvious neighbours
Expand Down Expand Up @@ -263,6 +290,8 @@ void QuadTreeNode::insert(Edge* e)
{
if( isLeaf() && !insertedEdge && !insertedPoint ){
insertedEdge=e;
Point *points = findItersectionPoints(e);
crossEdges[0] = new Edge(&points[0],&points[1]);
}else{
if(!insertedPoint)//if in leaf does not exist point
{
Expand Down Expand Up @@ -297,10 +326,16 @@ bool QuadTreeNode::contains(Edge* e) const
return false;
}

Point QuadTreeNode::itersectionP(Edge* e)
Point * QuadTreeNode::findItersectionPoints(Edge* e)
{
Q_UNUSED(e);

Point *points = new Point[2];
int j=0;
for(int i=0;i<4;i++){
if(j==2) break;
if(borders[i].intersects(e))
points[j++] = borders[i].intersectionPoint(e);
}
return points;
}

void QuadTreeNode::extractNeighbours()
Expand Down
31 changes: 26 additions & 5 deletions QuadTree.h
@@ -1,6 +1,6 @@
#ifndef QUADTREE_H
#define QUADTREE_H
#include <QScopedPointer>
#include <QtGui>
#include <vector>
#include <utility>

Expand Down Expand Up @@ -28,6 +28,8 @@ struct Edge
Point *b,*e;
bool intersects(Edge* e);
Point intersectionPoint(Edge* e);
private:
int getSide(Point *p);
};


Expand All @@ -50,12 +52,33 @@ struct QuadTreeNode
EQ_SW
};

enum Corner {
P_NW,
P_NE,
P_SE,
P_SW
};

enum Directions{
P_N,
P_E,
P_S,
P_W
};


static uint MAX_LEVEL;//maximum tree level

EQuadrant type;
Point* insertedPoint;
Edge* insertedEdge;

Point corners[4];
Edge borders[4];

Edge **crossEdges;
QList<Triangle> triangles;

Point lu_corner;
uint level;
double size;
Expand All @@ -68,7 +91,6 @@ struct QuadTreeNode
QuadTreeNode(const Point&p,double size,QuadTreeNode* parent,EQuadrant type);

void insert(Point* p);
//FIXME: criterion for subdivision apply when there is no point in quadrant
void insert(Edge* e);
void subdivide();
bool isLeaf();
Expand All @@ -82,10 +104,9 @@ struct QuadTreeNode
void extractNeighbours();
EQuadrant whichQuadrant(const Point& p) const;
bool contains(Edge* e) const;
Point itersectionP(Edge* e);
};

Point* findItersectionPoints(Edge* e);

};

class QuadTree
{
Expand Down

0 comments on commit 4d36a97

Please sign in to comment.