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-13 05:32:42 +02:00
|
|
|
bool Map::QTInsert(QuadTree *tree, Line *line, int depth) {
|
|
|
|
// printf("Inserting %d point poly\n", line->numPoints);
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-11 09:11:52 +02:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
if (!(line->lat_min >= tree->lat_min &&
|
|
|
|
line->lat_max <= tree->lat_max &&
|
|
|
|
line->lon_min >= tree->lon_min &&
|
|
|
|
line->lon_max <= tree->lon_max)) {
|
|
|
|
// printf("doesnt fit: %f > %f, %f < %f, %f < %f,%f > %f \n",line->lat_min, tree->lat_min, line->lat_max, tree->lat_max, line->lon_min, tree->lon_min, line->lon_max,tree->lon_max);
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
|
|
|
|
// //temp maxdepth for debugging
|
|
|
|
// if(depth > 20) {
|
|
|
|
// tree->lines.push_back(*line);
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
2020-03-02 07:40:11 +01:00
|
|
|
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-13 05:32:42 +02:00
|
|
|
if (QTInsert(tree->nw, line, 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-13 05:32:42 +02:00
|
|
|
if (QTInsert(tree->sw, line, 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-13 05:32:42 +02:00
|
|
|
if (QTInsert(tree->ne, line, 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-13 05:32:42 +02:00
|
|
|
if (QTInsert(tree->se, line, depth++)){
|
2020-03-02 07:40:11 +01:00
|
|
|
return true;
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
tree->lines.push_back(*line);
|
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-13 05:32:42 +02:00
|
|
|
std::vector<Line> Map::getLinesRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
|
|
|
|
std::vector<Line> retLines;
|
2020-03-19 06:22:59 +01:00
|
|
|
|
|
|
|
if(tree == NULL) {
|
2020-06-13 05:32:42 +02:00
|
|
|
return retLines;
|
2020-03-19 06:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->lat_min > screen_lat_max || screen_lat_min > tree->lat_max) {
|
2020-06-13 05:32:42 +02:00
|
|
|
return retLines;
|
2020-03-19 06:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tree->lon_min > screen_lon_max || screen_lon_min > tree->lon_max) {
|
2020-06-13 05:32:42 +02:00
|
|
|
return retLines;
|
2020-03-19 06:22:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
std::vector<Line> ret;
|
|
|
|
ret = getLinesRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retLines.insert(retLines.end(), ret.begin(), ret.end());
|
2020-06-10 20:28:52 +02:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
ret = getLinesRecursive(tree->sw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retLines.insert(retLines.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
ret = getLinesRecursive(tree->ne, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retLines.insert(retLines.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
ret = getLinesRecursive(tree->se, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
|
|
|
retLines.insert(retLines.end(), ret.begin(), ret.end());
|
2020-03-19 06:22:59 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
retLines.insert(retLines.end(), tree->lines.begin(), tree->lines.end());
|
2020-06-11 09:11:52 +02:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
return retLines;
|
2020-03-19 06:22:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
std::vector<Line> Map::getLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
|
|
|
|
return getLinesRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
|
2020-03-19 06:22:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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-06-13 05:32:42 +02:00
|
|
|
Point currentPoint;
|
|
|
|
Point nextPoint;
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
for(int i = 0; i < mapPoints_count - 2; i+=2) {
|
|
|
|
if(mapPoints[i] == 0)
|
|
|
|
continue;
|
|
|
|
currentPoint.lon = mapPoints[i];
|
|
|
|
currentPoint.lat = mapPoints[i + 1];
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
nextPoint.lon = mapPoints[i + 2];
|
|
|
|
nextPoint.lat = mapPoints[i + 3];
|
2020-02-27 07:44:30 +01:00
|
|
|
|
2020-06-13 05:32:42 +02:00
|
|
|
QTInsert(&root, new Line(currentPoint, nextPoint), 0);
|
2020-02-27 07:44:30 +01:00
|
|
|
}
|
2020-06-12 06:55:04 +02:00
|
|
|
}
|
2020-06-14 05:03:06 +02:00
|
|
|
|
|
|
|
Map::~Map() {
|
|
|
|
delete mapPoints;
|
|
|
|
}
|