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
This commit is contained in:
nathan 2020-06-11 00:11:52 -07:00
parent c45c788cae
commit bc88c0ba5d
7 changed files with 89 additions and 62 deletions

View file

@ -1,7 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <ctime> #include <ctime>
#include <list> #include <vector>
class Aircraft { class Aircraft {
public: public:
@ -20,7 +20,7 @@ public:
//history //history
std::list <float> lonHistory, latHistory, headingHistory, timestampHistory; std::vector <float> lonHistory, latHistory, headingHistory, timestampHistory;
// float oldLon[TRAIL_LENGTH]; // float oldLon[TRAIL_LENGTH];
// float oldLat[TRAIL_LENGTH]; // float oldLat[TRAIL_LENGTH];

57
Map.cpp
View file

@ -2,8 +2,13 @@
#include <stdio.h> #include <stdio.h>
#include <cstdlib> #include <cstdlib>
bool Map::QTInsert(QuadTree *tree, Polygon *polygon) { bool Map::QTInsert(QuadTree *tree, Polygon *polygon, int depth) {
// printf("Inserting %d point poly\n", polygon->numPoints); // printf("Inserting %d point poly\n", polygon->numPoints);
//temp maxdepth for debugging
if(depth > 10) {
return false;
}
if (!(polygon->lat_min >= tree->lat_min && if (!(polygon->lat_min >= tree->lat_min &&
polygon->lat_max <= tree->lat_max && polygon->lat_max <= tree->lat_max &&
@ -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); 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; return true;
} }
@ -36,7 +41,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) {
tree->sw->lon_max = tree->lon_max; tree->sw->lon_max = tree->lon_max;
} }
if (QTInsert(tree->sw,polygon)){ if (QTInsert(tree->sw,polygon, depth++)){
return true; 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); 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; return true;
} }
@ -62,7 +67,7 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) {
tree->se->lon_max = tree->lon_max; tree->se->lon_max = tree->lon_max;
} }
if (QTInsert(tree->se,polygon)){ if (QTInsert(tree->se,polygon, depth++)){
return true; return true;
} }
@ -72,8 +77,8 @@ bool Map::QTInsert(QuadTree *tree, Polygon *polygon) {
} }
std::list<Polygon> Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { std::vector<Polygon> Map::getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
std::list<Polygon> retPolys; std::vector<Polygon> retPolys;
if(tree == NULL) { if(tree == NULL) {
return retPolys; return retPolys;
@ -88,26 +93,25 @@ std::list<Polygon> Map::getPolysRecursive(QuadTree *tree, float screen_lat_min,
} }
//for some reason os x clang doesn't like this std::vector<Polygon> ret;
retPolys.splice(retPolys.end(),getPolysRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max)); ret = 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.insert(retPolys.end(), ret.begin(), ret.end());
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));
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<Polygon>::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) { ret = getPolysRecursive(tree->se, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
if(currentPolygon->points.empty()) { retPolys.insert(retPolys.end(), ret.begin(), ret.end());
continue;
}
retPolys.push_back(*currentPolygon); retPolys.insert(retPolys.end(), tree->polygons.begin(), tree->polygons.end());
}
return retPolys;
} }
std::list<Polygon> Map::getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { std::vector<Polygon> 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); return getPolysRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
}; };
@ -131,7 +135,7 @@ Map::Map() {
fclose(fileptr); fclose(fileptr);
printf("Read %d map points.\n",mapPoints_count); printf("Read %d map points.\n",mapPoints_count / 2);
// load quad tree // load quad tree
@ -153,15 +157,14 @@ Map::Map() {
} }
Polygon *currentPolygon = new Polygon; Polygon *currentPolygon = new Polygon;
printf("A\n");
for(int i = 0; i < mapPoints_count; i+=2) { for(int i = 0; i < mapPoints_count; i+=2) {
if(mapPoints[i] == 0) { if(mapPoints[i] == 0) {
QTInsert(&root, currentPolygon); QTInsert(&root, currentPolygon, 0);
currentPolygon = new Polygon; currentPolygon = new Polygon;
continue; continue;
} }
currentPolygon->numPoints++; currentPolygon->numPoints++;
Point *currentPoint = new Point; Point *currentPoint = new Point;
@ -183,4 +186,6 @@ Map::Map() {
currentPolygon->points.push_back(*currentPoint); currentPolygon->points.push_back(*currentPoint);
} }
printf("B\n");
} }

12
Map.h
View file

@ -1,7 +1,7 @@
#ifndef MAP_H #ifndef MAP_H
#define MAP_H #define MAP_H
#include <list> #include <vector>
typedef struct Point{ typedef struct Point{
float lat; float lat;
@ -14,7 +14,7 @@ typedef struct Polygon{
float lon_min; float lon_min;
float lon_max; float lon_max;
std::list<Point> points; std::vector<Point> points;
int numPoints; int numPoints;
Polygon() { Polygon() {
@ -32,7 +32,7 @@ typedef struct QuadTree{
float lon_min; float lon_min;
float lon_max; float lon_max;
std::list<Polygon> polygons; std::vector<Polygon> polygons;
struct QuadTree *nw; struct QuadTree *nw;
struct QuadTree *sw; struct QuadTree *sw;
@ -75,9 +75,9 @@ class Map {
public: public:
QuadTree root; QuadTree root;
bool QTInsert(QuadTree *tree, Polygon *polygon); bool QTInsert(QuadTree *tree, Polygon *polygon, int depth);
std::list<Polygon> getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); std::vector<Polygon> getPolysRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max);
std::list<Polygon> getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); std::vector<Polygon> getPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max);
Map(); Map();

View file

@ -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 sudo apt install python3 python3-pip
pip3 install lxml numpy pip3 install lxml numpy tqdm
./getmap.sh ./getmap.sh
``` ```

View file

@ -238,7 +238,7 @@ void View::SDL_init() {
screen_height= DM.h; 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); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
mapTexture = SDL_CreateTexture(renderer, mapTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_ARGB8888,
@ -522,23 +522,24 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor)
} }
void View::drawTrail(Aircraft *p) { void View::drawTrail(Aircraft *p) {
return;
int currentX, currentY, prevX, prevY; int currentX, currentY, prevX, prevY;
std::list<float>::iterator lon_idx = std::next(p->lonHistory.begin()); std::vector<float>::iterator lon_idx = p->lonHistory.begin();
std::list<float>::iterator lat_idx = std::next(p->latHistory.begin()); std::vector<float>::iterator lat_idx = p->latHistory.begin();
std::list<float>::iterator heading_idx = std::next(p->headingHistory.begin()); std::vector<float>::iterator heading_idx = p->headingHistory.begin();
int idx = p->lonHistory.size(); 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; float dx, dy;
pxFromLonLat(&dx, &dy, *lon_idx, *lat_idx); pxFromLonLat(&dx, &dy, *std::next(lon_idx), *std::next(lat_idx));
screenCoords(&currentX, &currentY, dx, dy); screenCoords(&currentX, &currentY, 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); screenCoords(&prevX, &prevY, dx, dy);
@ -627,19 +628,18 @@ void View::drawScaleBars()
} }
void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
std::list<Polygon> polyList = map.getPolys(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); std::vector<Polygon> polyList = map.getPolys(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
std::list<Polygon>::iterator currentPolygon; std::vector<Polygon>::iterator currentPolygon;
for (currentPolygon = polyList.begin(); currentPolygon != polyList.end(); ++currentPolygon) { for (currentPolygon = polyList.begin(); currentPolygon != polyList.end(); ++currentPolygon) {
int x1,y1,x2,y2; int x1,y1,x2,y2;
float dx,dy; float dx,dy;
std::list<Point>::iterator currentPoint; std::vector<Point>::iterator currentPoint;
std::list<Point>::iterator prevPoint; std::vector<Point>::iterator prevPoint = currentPolygon->points.begin();
for (currentPoint = std::next(currentPolygon->points.begin()); currentPoint != currentPolygon->points.end(); ++currentPoint) { for (currentPoint = std::next(currentPolygon->points.begin()); currentPoint != currentPolygon->points.end(); ++currentPoint, ++prevPoint) {
prevPoint = std::prev(currentPoint);
pxFromLonLat(&dx, &dy, prevPoint->lon, prevPoint->lat); pxFromLonLat(&dx, &dy, prevPoint->lon, prevPoint->lat);
screenCoords(&x1, &y1, dx, dy); screenCoords(&x1, &y1, dx, dy);
@ -657,13 +657,6 @@ void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lo
continue; 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 d2 = dx* dx + dy * dy;
float factor = 1.0 - (d1+d2) / (3* maxDist * maxDist); float factor = 1.0 - (d1+d2) / (3* maxDist * maxDist);

View file

@ -1,6 +1,7 @@
from lxml import etree as ElementTree from lxml import etree as ElementTree
import numpy as np import numpy as np
import sys import sys
from tqdm import tqdm
filename = sys.argv[1] filename = sys.argv[1]
@ -12,16 +13,44 @@ parser = ElementTree.XMLParser(recover=True)
tree = ElementTree.parse(filename, parser) tree = ElementTree.parse(filename, parser)
polys = tree.xpath('//polygon') polys = tree.xpath('//polygon')
bin_file = open("mapdata.bin", "wt") bin_file = open("mapdata.bin", "wb")
outlist = [] 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() currentPoints = (p.attrib['points']).replace(","," ").split()
if(len(currentPoints) > 12): #remove little circles in the McCurley maps if(len(currentPoints) == 14): #remove little circles in the McCurley maps
outlist.extend(currentPoints) 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"]) outlist.extend(["0","0"])
np.asarray(outlist).astype(np.single).tofile(bin_file) np.asarray(outlist).astype(np.single).tofile(bin_file)
bin_file.close() bin_file.close()
print("Wrote %d points" % (len(outlist) / 2))

View file

@ -58,7 +58,7 @@ Style style;
void showHelp(void) { void showHelp(void) {
printf( printf(
"-----------------------------------------------------------------------------\n" "-----------------------------------------------------------------------------\n"
"| viz1090 ADSB Viewer Ver : "0.1" |\n" "| viz1090 ADSB Viewer Ver : 0.1 |\n"
"-----------------------------------------------------------------------------\n" "-----------------------------------------------------------------------------\n"
"--server <IPv4/hosname> TCP Beast output listen IPv4 (default: 127.0.0.1)\n" "--server <IPv4/hosname> TCP Beast output listen IPv4 (default: 127.0.0.1)\n"
"--port <port> TCP Beast output listen port (default: 30005)\n" "--port <port> TCP Beast output listen port (default: 30005)\n"