From bc88c0ba5d2fd1bf184650448384c6af8e3595b2 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 11 Jun 2020 00:11:52 -0700 Subject: [PATCH] std::list -> vector for performance. Working on mapconverter precision to merge points. Broken with lower precision levels, something wrong with QTInsert Former-commit-id: c40d67cd1346a723dd8a3440b7eb845575c05f0b --- Aircraft.h | 4 ++-- Map.cpp | 59 +++++++++++++++++++++++++++---------------------- Map.h | 12 +++++----- README.md | 2 +- View.cpp | 35 ++++++++++++----------------- mapconverter.py | 37 +++++++++++++++++++++++++++---- viz1090.cpp | 2 +- 7 files changed, 89 insertions(+), 62 deletions(-) diff --git a/Aircraft.h b/Aircraft.h index 83d4660..f25fad9 100644 --- a/Aircraft.h +++ b/Aircraft.h @@ -1,7 +1,7 @@ #include #include -#include +#include class Aircraft { public: @@ -20,7 +20,7 @@ public: //history - std::list lonHistory, latHistory, headingHistory, timestampHistory; + std::vector lonHistory, latHistory, headingHistory, timestampHistory; // float oldLon[TRAIL_LENGTH]; // float oldLat[TRAIL_LENGTH]; diff --git a/Map.cpp b/Map.cpp index 229520e..05be4fd 100644 --- a/Map.cpp +++ b/Map.cpp @@ -2,9 +2,14 @@ #include #include -bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { - // printf("Inserting %d point poly\n", polygon->numPoints); +bool Map::QTInsert(QuadTree *tree, Polygon *polygon, int depth) { + // printf("Inserting %d point poly\n", polygon->numPoints); + //temp maxdepth for debugging + if(depth > 10) { + return false; + } + if (!(polygon->lat_min >= tree->lat_min && polygon->lat_max <= tree->lat_max && polygon->lon_min >= tree->lon_min && @@ -23,7 +28,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { tree->nw->lon_max = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min); } - if (QTInsert(tree->nw,polygon)){ + if (QTInsert(tree->nw,polygon, depth++)){ return true; } @@ -36,7 +41,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { tree->sw->lon_max = tree->lon_max; } - if (QTInsert(tree->sw,polygon)){ + if (QTInsert(tree->sw,polygon, depth++)){ return true; } @@ -49,7 +54,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { tree->ne->lon_max = tree->lon_min + 0.5 * (tree->lon_max - tree->lon_min); } - if (QTInsert(tree->ne,polygon)){ + if (QTInsert(tree->ne,polygon, depth++)){ return true; } @@ -62,7 +67,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { tree->se->lon_max = tree->lon_max; } - if (QTInsert(tree->se,polygon)){ + if (QTInsert(tree->se,polygon, depth++)){ return true; } @@ -72,8 +77,8 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { } -std::list Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { - std::list retPolys; +std::vector Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { + std::vector retPolys; if(tree == NULL) { return retPolys; @@ -88,26 +93,25 @@ std::list Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, } - //for some reason os x clang doesn't like this - retPolys.splice(retPolys.end(),getPolysRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max)); - retPolys.splice(retPolys.end(),getPolysRecursive(tree->sw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max)); - retPolys.splice(retPolys.end(),getPolysRecursive(tree->ne, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max)); - retPolys.splice(retPolys.end(),getPolysRecursive(tree->se, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max)); + std::vector 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()); - float dx, dy; + ret = getPolysRecursive(tree->sw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); + retPolys.insert(retPolys.end(), ret.begin(), ret.end()); - std::list::iterator currentPolygon; + ret = getPolysRecursive(tree->ne, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); + retPolys.insert(retPolys.end(), ret.begin(), ret.end()); - for (currentPolygon = tree->polygons.begin(); currentPolygon != tree->polygons.end(); ++currentPolygon) { - if(currentPolygon->points.empty()) { - continue; - } + ret = getPolysRecursive(tree->se, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); + retPolys.insert(retPolys.end(), ret.begin(), ret.end()); - retPolys.push_back(*currentPolygon); - } + retPolys.insert(retPolys.end(), tree->polygons.begin(), tree->polygons.end()); + + return retPolys; } -std::list Map::getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { +std::vector Map::getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { return getPolysRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); }; @@ -131,7 +135,7 @@ Map::Map() { fclose(fileptr); - printf("Read %d map points.\n",mapPoints_count); + printf("Read %d map points.\n",mapPoints_count / 2); // load quad tree @@ -153,15 +157,14 @@ Map::Map() { } Polygon *currentPolygon = new Polygon; - +printf("A\n"); for(int i = 0; i < mapPoints_count; i+=2) { if(mapPoints[i] == 0) { - QTInsert(&root, currentPolygon); + QTInsert(&root, currentPolygon, 0); currentPolygon = new Polygon; continue; } - currentPolygon->numPoints++; Point *currentPoint = new Point; @@ -183,4 +186,6 @@ Map::Map() { currentPolygon->points.push_back(*currentPoint); } -} +printf("B\n"); + +} \ No newline at end of file diff --git a/Map.h b/Map.h index 371278d..6c42333 100644 --- a/Map.h +++ b/Map.h @@ -1,7 +1,7 @@ #ifndef MAP_H #define MAP_H -#include +#include typedef struct Point{ float lat; @@ -14,7 +14,7 @@ typedef struct Polygon{ float lon_min; float lon_max; - std::list points; + std::vector points; int numPoints; Polygon() { @@ -32,7 +32,7 @@ typedef struct QuadTree{ float lon_min; float lon_max; - std::list polygons; + std::vector polygons; struct QuadTree *nw; struct QuadTree *sw; @@ -75,9 +75,9 @@ class Map { public: QuadTree root; - bool QTInsert(QuadTree *tree, Polygon *polygon); - std::list getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); - std::list getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); + bool QTInsert(QuadTree *tree, Polygon *polygon, int depth); + std::vector getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); + std::vector getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); Map(); diff --git a/README.md b/README.md index b4d315d..b5924b9 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ The getmap.sh pulls the svg file for the contiguous 48 US states and produces a ``` sudo apt install python3 python3-pip -pip3 install lxml numpy +pip3 install lxml numpy tqdm ./getmap.sh ``` diff --git a/View.cpp b/View.cpp index 3920c81..bf5856a 100644 --- a/View.cpp +++ b/View.cpp @@ -238,7 +238,7 @@ void View::SDL_init() { screen_height= DM.h; } - window = SDL_CreateWindow("map1090", SDL_WINDOWPOS_CENTERED_DISPLAY(screen_index), SDL_WINDOWPOS_CENTERED_DISPLAY(screen_index), screen_width, screen_height, flags); + window = SDL_CreateWindow("viz1090", SDL_WINDOWPOS_CENTERED_DISPLAY(screen_index), SDL_WINDOWPOS_CENTERED_DISPLAY(screen_index), screen_width, screen_height, flags); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, @@ -522,23 +522,24 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor) } void View::drawTrail(Aircraft *p) { + return; int currentX, currentY, prevX, prevY; - std::list::iterator lon_idx = std::next(p->lonHistory.begin()); - std::list::iterator lat_idx = std::next(p->latHistory.begin()); - std::list::iterator heading_idx = std::next(p->headingHistory.begin()); + std::vector::iterator lon_idx = p->lonHistory.begin(); + std::vector::iterator lat_idx = p->latHistory.begin(); + std::vector::iterator heading_idx = p->headingHistory.begin(); int idx = p->lonHistory.size(); - for(; lon_idx != p->lonHistory.end(); ++lon_idx, ++lat_idx, ++heading_idx) { + for(; std::next(lon_idx) != p->lonHistory.end(); ++lon_idx, ++lat_idx, ++heading_idx) { float dx, dy; - pxFromLonLat(&dx, &dy, *lon_idx, *lat_idx); + pxFromLonLat(&dx, &dy, *std::next(lon_idx), *std::next(lat_idx)); screenCoords(¤tX, ¤tY, dx, dy); - pxFromLonLat(&dx, &dy, *(std::prev(lon_idx)), *(std::prev(lat_idx))); + pxFromLonLat(&dx, &dy, *lon_idx, *lat_idx); screenCoords(&prevX, &prevY, dx, dy); @@ -627,20 +628,19 @@ void View::drawScaleBars() } void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { - std::list polyList = map.getPolys(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); + std::vector polyList = map.getPolys(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); - std::list::iterator currentPolygon; + std::vector::iterator currentPolygon; for (currentPolygon = polyList.begin(); currentPolygon != polyList.end(); ++currentPolygon) { int x1,y1,x2,y2; float dx,dy; - std::list::iterator currentPoint; - std::list::iterator prevPoint; - - for (currentPoint = std::next(currentPolygon->points.begin()); currentPoint != currentPolygon->points.end(); ++currentPoint) { - prevPoint = std::prev(currentPoint); + std::vector::iterator currentPoint; + std::vector::iterator prevPoint = currentPolygon->points.begin(); + for (currentPoint = std::next(currentPolygon->points.begin()); currentPoint != currentPolygon->points.end(); ++currentPoint, ++prevPoint) { + pxFromLonLat(&dx, &dy, prevPoint->lon, prevPoint->lat); screenCoords(&x1, &y1, dx, dy); @@ -657,13 +657,6 @@ void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lo continue; } - //this needs a differnt approach for the std list setup - - // if((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) < MIN_MAP_FEATURE){ - // currentPoint = currentPoint->next; - // continue; - // } - float d2 = dx* dx + dy * dy; float factor = 1.0 - (d1+d2) / (3* maxDist * maxDist); diff --git a/mapconverter.py b/mapconverter.py index d18cf06..fc0d9f3 100644 --- a/mapconverter.py +++ b/mapconverter.py @@ -1,6 +1,7 @@ from lxml import etree as ElementTree import numpy as np import sys +from tqdm import tqdm filename = sys.argv[1] @@ -12,16 +13,44 @@ parser = ElementTree.XMLParser(recover=True) tree = ElementTree.parse(filename, parser) polys = tree.xpath('//polygon') -bin_file = open("mapdata.bin", "wt") +bin_file = open("mapdata.bin", "wb") outlist = [] -for p in polys: + +precision = 2 + +print("Reading points") +#for i in tqdm(range(len(polys))): +for i in range(40): + p = polys[i] currentPoints = (p.attrib['points']).replace(","," ").split() - if(len(currentPoints) > 12): #remove little circles in the McCurley maps - outlist.extend(currentPoints) + if(len(currentPoints) == 14): #remove little circles in the McCurley maps + continue + + prevx = 0 + prevy = 0 + + temp = [] + + for i in range(int(len(currentPoints)/2)): + currentPoints[2 * i + 0] = "%.*f" % (precision, float(currentPoints[2 * i + 0])) + currentPoints[2 * i + 1] = "%.*f" % (precision, float(currentPoints[2 * i + 1])) + + if(currentPoints[2 * i + 0] != prevx and currentPoints[2 * i + 1] != prevy): + temp.extend([currentPoints[2 * i + 0],currentPoints[2 * i + 1]]) + + prevx = currentPoints[2 * i + 0] + prevy = currentPoints[2 * i + 1] + + + if(len(currentPoints) > 14): + outlist.extend(temp) outlist.extend(["0","0"]) + np.asarray(outlist).astype(np.single).tofile(bin_file) bin_file.close() + +print("Wrote %d points" % (len(outlist) / 2)) \ No newline at end of file diff --git a/viz1090.cpp b/viz1090.cpp index 7118260..6b25990 100644 --- a/viz1090.cpp +++ b/viz1090.cpp @@ -58,7 +58,7 @@ Style style; void showHelp(void) { printf( "-----------------------------------------------------------------------------\n" -"| viz1090 ADSB Viewer Ver : "0.1" |\n" +"| viz1090 ADSB Viewer Ver : 0.1 |\n" "-----------------------------------------------------------------------------\n" "--server TCP Beast output listen IPv4 (default: 127.0.0.1)\n" "--port TCP Beast output listen port (default: 30005)\n"