-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.h
148 lines (125 loc) · 4.04 KB
/
server.h
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
#ifndef SERVER_H
#define SERVER_H
#include <set> // std::set
#include <list> // std::list
#include <vector> // std::vector
#include <map> // std::map
#include <algorithm> // std::sort
#include "point.h"
#include "client.h"
#include "ConvexHull.h"
#include "GrahamScanConvexHull.h"
#include "VoronoiDiagramGenerator.h"
// Defines
#define MAXCLIENTS 5
#define MINCLIENTS 1
#define WIDTH 800
#define VORO false
#define _DEBUG
/*****************************************************************************
* Server class contains methods used in both the distributed Voronoi
* and QuadTree implementations.
*
* Description:
* Params:
* - Point loc => The current location of the server within the virtual
* environment
* - Cell => A cell struct that contians the following for each implementation
* Voronoi:
* - int n => The number of Vertex's
* - Vertex* origin => Pointer to first Vertex defining a polygon
*
* QuadTree:
* - int n => The index of Rectangles owned by the current server
* - rect => a List of Rectangles
* - neighbor => stdlib set of neighbours of the current server
******************************************************************************/
class Vertex {
public:
Vertex(Point a){
loc = a;
next = NULL;
}
~Vertex() {
delete &loc;
}
Point loc;
Vertex* next; // Pointer to next vertex in polygon
Vertex* prev;
};
class Rectangle {
public:
Rectangle(Point tL, Point bR) {
topLeft = tL;
botRight = bR;
}
Point topLeft;
Point botRight;
};
struct Cell {
int n;
double rmax;
Vertex* origin; // pointer to origin of polygon Assume counter clockwise sequence
std::list<Rectangle*> rect; // A Set of point Rectangle objects each defining a rectangle
};
class Server
{
public:
Server();
~Server();
Server(double x, double y);
void printNeighbourLocs();
bool isLoaded();
bool underLoaded();
/**********************
* Distributed Voronoi
**********************/
// std::vector<Point> generateVoronoi();
void generateVoronoi();
void GrahamSort(std::vector<Point> points);
void GrahamScan(std::vector<Point> p);
void vertsToVector(std::vector<Point> *v);
void findIntersects(Line line, std::vector<Point> *ip);
void removeMe(Server *t, set<Server *> excludeNeighs);
bool isNeigh(Server* t);
bool refine(Server *t); // calculate the new cell after intercection of the halfspace between
// the current server and new point
Point getCenterofClients();
void returnThisSite();
// Polygon functions
void addVertex(Point a, bool ccw);
void deleteCell();
void deleteMyVertex(Vertex* v);
bool pointInPolygon(Point p);
std::vector<Point> *RemoveDup(std::vector<Point> v);
/**********************
* QuadTree
**********************/
Server(double x, double y, Point p1, Point p2);
void addRect(Point p1, Point p2);
void addRect(Rectangle *r);
bool devide(); // Devide current rectangle into four and move location to top left rect
bool merge();
bool transfer(Server* t); // Transfer one of the current server's most loaded rectangles to t
bool returnArea(); // Selects less loaded neighbour in same lvl, else on lvl up;
bool insideArea(Point* tp); // Determines if the test point tp is inside the area of current server
void addAdjacent(Server* t); // Determines if the Server t, and all its neighbours is adjacent to this, thus is a neighbour
void ownership(Client* c);
void checkOwnership();
// Params
Point loc;
int lvl;
Cell cell;
Server* parent;
int childCount;
std::set<Server*> neighbours;
std::set<Client*> myClients;
#ifdef _DEBUG
bool master;
#endif
};
bool ccw(Point p[], int n);
bool ccw(Point p1, Point p2, Point p3);
bool inRect(Point* tp, Rectangle r);
void insertIntoVec(std::vector<Point> *v, Point p);
#endif // SERVER_H