forked from ppikula/quadtri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadTree.h
More file actions
159 lines (124 loc) · 3.37 KB
/
QuadTree.h
File metadata and controls
159 lines (124 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef QUADTREE_H
#define QUADTREE_H
#include <QtGui>
#include <vector>
#include <utility>
// forward declarations
class DrawingArea;
class Polygon;
struct Edge;
class Point
{
public:
Point():next(0),prev(0),x(0),y(0){}
Point(double x,double y):next(0),prev(0),x(x),y(y){}
Edge *next,*prev;
double x,y;
};
class CornerPoint:public Point {
public:
CornerPoint():Point(),left(0),right(0),opposite(0){}
CornerPoint(double x,double y):Point(x,y),left(0),right(0),opposite(0){}
CornerPoint *left,*right,*opposite;
};
class BorderPoint:public Point {
public:
BorderPoint():Point(),left(0),right(0),oppositeLeft(0),oppositeRight(0){}
BorderPoint(double x,double y):Point(x,y),left(0),right(0),oppositeRight(0){}
Point *left,*right,*oppositeLeft,*oppositeRight;
};
//TODO: indices or pointers?
struct Edge
{
Edge():b(0),e(0){}
Edge(Point* b,Point* e):b(b),e(e){}
void setPointsPtr(){b->next=this;e->prev=this;}
Point *b,*e;
bool intersects(Edge* e);
bool intersects(Point *p,Point *p2);
Point intersectionPoint(Edge* e);
int getSide(Point *p);
};
struct Triangle
{
Triangle(){}
Triangle(Point *a,Point* b,Point*c):a(a),b(b),c(c){}
Point *a,*b,*c;
void draw(DrawingArea* area);
};
struct QuadTreeNode
{
enum EQuadrant{
EQ_NW,
EQ_NE,
EQ_SE,
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;
CornerPoint corners[4];
BorderPoint borderPoints[4];
Edge borders[4];
Edge **crossEdges;
QList<Triangle> triangles;
Point lu_corner;
uint level;
double size;
QuadTreeNode* parent;
QScopedPointer<QuadTreeNode> NW,NE,SE,SW;//subnodes
QuadTreeNode* N,*E,*S,*W;//neighbours
static void initialize(const Point &p,double size,CornerPoint *corners,BorderPoint *borderPoints,Edge *borders,Edge ***crossEdges);
QuadTreeNode(const Point&p,double size);
QuadTreeNode(const Point&p,double size,QuadTreeNode* parent,EQuadrant type);
void insert(Point* p);
void insert(Edge* e);
void subdivide();
bool isLeaf();
uint depth();
void draw(DrawingArea *area);
void upadteNeighbours();
private:
void extractNeighbours();
EQuadrant whichQuadrant(const Point& p) const;
bool contains(Edge* e) const;
Point* findItersectionPoints(Edge* e);
};
class QuadTree
{
QScopedPointer<QuadTreeNode> root;
std::vector<Point> points;
std::vector<Edge> edges;
std::vector<Triangle> tris;
public:
QuadTree(const Point& p, uint size);
void insert(const Point& p);
// points with constraints
void insertPolygon(Polygon* poly);
//maximum level
uint depth();
//should return triangulation result
void Triangulate();
void applyTemplateTriangulation(QuadTreeNode* n);
void applyPointTriangulation(QuadTreeNode* n);
void applyEdgeTriangulation(QuadTreeNode *n);
//std::list<QuadTreeNode> leaves;
void generatePointTree(Point** p,QuadTreeNode*n ,int type,int start);
void draw(DrawingArea *area);
private:
void calculateTrianglesForEdgeCase(Edge *e,Point *p,Point *sp,Point *op,BorderPoint **otherBP,int othersSize);
};
#endif // QUADTREE_H