2020-03-19 06:22:59 +01:00
|
|
|
#include "Map.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <cstdlib>
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
bool Map::QTInsert(QuadTree *tree, Polygon *polygon, int depth) {
|
|
|
|
// printf("Inserting %d point poly\n", polygon->numPoints);
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
//temp maxdepth for debugging
|
|
|
|
if(depth > 10) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
if (!(polygon->lat_min >= tree->lat_min &&
|
|
|
|
polygon->lat_max <= tree->lat_max &&
|
|
|
|
polygon->lon_min >= tree->lon_min &&
|
|
|
|
polygon->lon_max <= tree->lon_max)) {
|
|
|
|
// printf("doesnt fit: %f > %f, %f < %f, %f < %f,%f > %f \n",polygon->lat_min, tree->lat_min, polygon->lat_max, tree->lat_max, polygon->lon_min, tree->lon_min, polygon->lon_max,tree->lon_max);
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->nw == NULL) {
|
2020-03-19 06:22:59 +01:00
|
|
|
tree->nw = new QuadTree;
|
2020-03-02 07:40:11 +01:00
|
|
|
|
|
|
|
tree->nw->lat_min = tree->lat_min;
|
|
|
|
tree->nw->lat_max = tree->lat_min + 0.5 * (tree->lat_max - tree->lat_min);
|
|
|
|
tree->nw->lon_min = tree->lon_min;
|
|
|
|
tree->nw->lon_max = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min);
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
if (QTInsert(tree->nw,polygon, depth++)){
|
2020-03-02 07:40:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->sw == NULL) {
|
2020-03-19 06:22:59 +01:00
|
|
|
tree->sw = new QuadTree;
|
2020-03-02 07:40:11 +01:00
|
|
|
|
|
|
|
tree->sw->lat_min = tree->lat_min;
|
|
|
|
tree->sw->lat_max = tree->lat_min + 0.5 * (tree->lat_max - tree->lat_min);
|
|
|
|
tree->sw->lon_min = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min);
|
|
|
|
tree->sw->lon_max = tree->lon_max;
|
|
|
|
}
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
if (QTInsert(tree->sw,polygon, depth++)){
|
2020-03-02 07:40:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->ne == NULL) {
|
2020-03-19 06:22:59 +01:00
|
|
|
tree->ne = new QuadTree;
|
2020-03-02 07:40:11 +01:00
|
|
|
|
|
|
|
tree->ne->lat_min = tree->lat_min + 0.5 * (tree->lat_max - tree->lat_min);
|
|
|
|
tree->ne->lat_max = tree->lat_max;
|
|
|
|
tree->ne->lon_min = tree->lon_min;
|
|
|
|
tree->ne->lon_max = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min);
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
if (QTInsert(tree->ne,polygon, depth++)){
|
2020-03-02 07:40:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->se == NULL) {
|
2020-03-19 06:22:59 +01:00
|
|
|
tree->se = new QuadTree;
|
2020-03-02 07:40:11 +01:00
|
|
|
|
|
|
|
tree->se->lat_min = tree->lat_min + 0.5 * (tree->lat_max - tree->lat_min);
|
|
|
|
tree->se->lat_max = tree->lat_max;
|
|
|
|
tree->se->lon_min = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min);
|
|
|
|
tree->se->lon_max = tree->lon_max;
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
if (QTInsert(tree->se,polygon, depth++)){
|
2020-03-02 07:40:11 +01:00
|
|
|
return true;
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
tree->polygons.push_back(*polygon);
|
2020-03-02 07:40:11 +01:00
|
|
|
|
|
|
|
return true;
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
std::vector<Polygon> Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
|
|
|
|
std::vector<Polygon> retPolys;
|
2020-03-19 06:22:59 +01:00
|
|
|
|
|
|
|
if(tree == NULL) {
|
|
|
|
return retPolys;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->lat_min > screen_lat_max || screen_lat_min > tree->lat_max) {
|
|
|
|
return retPolys;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->lon_min > screen_lon_max || screen_lon_min > tree->lon_max) {
|
|
|
|
return retPolys;
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:28:52 +02:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
std::vector<Polygon> ret;
|
|
|
|
ret = getPolysRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retPolys.insert(retPolys.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
ret = getPolysRecursive(tree->sw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retPolys.insert(retPolys.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
ret = getPolysRecursive(tree->ne, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retPolys.insert(retPolys.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
ret = getPolysRecursive(tree->se, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retPolys.insert(retPolys.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
retPolys.insert(retPolys.end(), tree->polygons.begin(), tree->polygons.end());
|
|
|
|
|
|
|
|
return retPolys;
|
2020-03-19 06:22:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
std::vector<Polygon> Map::getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
|
2020-03-19 06:22:59 +01:00
|
|
|
return getPolysRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
};
|
|
|
|
|
|
|
|
Map::Map() {
|
2020-02-27 07:44:30 +01:00
|
|
|
FILE *fileptr;
|
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
if(!(fileptr = fopen("mapdata.bin", "rb"))) {
|
|
|
|
printf("Couldn't read mapdata.bin\nDid you run getmap.sh?\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
fseek(fileptr, 0, SEEK_END);
|
|
|
|
mapPoints_count = ftell(fileptr) / sizeof(float);
|
|
|
|
rewind(fileptr);
|
2020-03-02 06:17:12 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
mapPoints = (float *)malloc(mapPoints_count * sizeof(float));
|
|
|
|
if(!fread(mapPoints, sizeof(float), mapPoints_count, fileptr)){
|
|
|
|
printf("Read error\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fileptr);
|
2020-03-02 06:17:12 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
printf("Read %d map points.\n",mapPoints_count / 2);
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
// load quad tree
|
|
|
|
|
2020-02-27 07:44:30 +01:00
|
|
|
for(int i = 0; i < mapPoints_count; i+=2) {
|
|
|
|
if(mapPoints[i] == 0)
|
|
|
|
continue;
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
if(mapPoints[i] < root.lon_min) {
|
|
|
|
root.lon_min = mapPoints[i];
|
|
|
|
} else if(mapPoints[i] > root.lon_max) {
|
|
|
|
root.lon_max = mapPoints[i];
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
if(mapPoints[i+1] < root.lat_min) {
|
|
|
|
root.lat_min = mapPoints[i+1];
|
|
|
|
} else if(mapPoints[i+1] > root.lat_max) {
|
|
|
|
root.lat_max = mapPoints[i+1];
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
Polygon *currentPolygon = new Polygon;
|
2020-03-02 07:40:11 +01:00
|
|
|
for(int i = 0; i < mapPoints_count; i+=2) {
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
if(mapPoints[i] == 0) {
|
2020-06-11 09:11:52 +02:00
|
|
|
QTInsert(&root, currentPolygon, 0);
|
2020-03-19 06:22:59 +01:00
|
|
|
currentPolygon = new Polygon;
|
2020-03-02 07:40:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
currentPolygon->numPoints++;
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
Point *currentPoint = new Point;
|
2020-02-27 07:44:30 +01:00
|
|
|
|
|
|
|
if(mapPoints[i] < currentPolygon->lon_min) {
|
|
|
|
currentPolygon->lon_min = mapPoints[i];
|
2020-06-12 06:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mapPoints[i] > currentPolygon->lon_max) {
|
2020-02-27 07:44:30 +01:00
|
|
|
currentPolygon->lon_max = mapPoints[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mapPoints[i+1] < currentPolygon->lat_min) {
|
|
|
|
currentPolygon->lat_min = mapPoints[i+1];
|
2020-06-12 06:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mapPoints[i+1] > currentPolygon->lat_max) {
|
2020-02-27 07:44:30 +01:00
|
|
|
currentPolygon->lat_max = mapPoints[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPoint->lon = mapPoints[i];
|
|
|
|
currentPoint->lat = mapPoints[i+1];
|
|
|
|
|
2020-03-19 06:22:59 +01:00
|
|
|
currentPolygon->points.push_back(*currentPoint);
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
2020-06-11 09:11:52 +02:00
|
|
|
|
2020-06-12 06:55:04 +02:00
|
|
|
}
|