From def7ee20e154fd4d95b05d0679bbcef9f1c0aff2 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 11 Jun 2020 21:55:04 -0700 Subject: [PATCH] map convert precision change. fixed map load issue. changed time functions to std::chrono Former-commit-id: a774a5bdbcf9e1c643e56277dbeedcedacb54905 --- AircraftList.cpp | 18 +++----- AppData.cpp | 36 +++++++-------- Input.cpp | 32 +++++++------ Makefile | 2 +- Map.cpp | 12 +++-- View.cpp | 99 +++++++++++++++++++++------------------- View.h | 4 +- drawPolys.REMOVED.git-id | 1 + mapconverter.py | 32 +++++++------ out.prof.REMOVED.git-id | 1 + viz1090.cpp | 12 ----- 11 files changed, 123 insertions(+), 126 deletions(-) create mode 100644 drawPolys.REMOVED.git-id create mode 100644 out.prof.REMOVED.git-id diff --git a/AircraftList.cpp b/AircraftList.cpp index 547dba0..5f5ef3d 100644 --- a/AircraftList.cpp +++ b/AircraftList.cpp @@ -1,15 +1,12 @@ #include "AircraftList.h" -static uint64_t mstime(void) { - struct timeval tv; - uint64_t mst; +#include - gettimeofday(&tv, nullptr); - mst = ((uint64_t)tv.tv_sec)*1000; - mst += tv.tv_usec/1000; - return mst; +static uint64_t now() { + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); } + Aircraft *AircraftList::find(uint32_t addr) { Aircraft *p = head; @@ -35,7 +32,6 @@ void AircraftList::update(Modes *modes) { p = find(a->addr); if (!p) { - //p = createPlaneObj(a); p = new Aircraft(a->addr); p->next = head; head = p; @@ -51,7 +47,7 @@ void AircraftList::update(Modes *modes) { } p->seen = a->seen; - p->msSeen = mstime(); + p->msSeen = now(); if((p->seen - p->prev_seen) > 0) { p->messageRate = 1.0 / (double)(p->seen - p->prev_seen); @@ -68,7 +64,7 @@ void AircraftList::update(Modes *modes) { p->lat = a->lat; if(p->seenLatLon < a->seenLatLon) { - p->msSeenLatLon = mstime(); + p->msSeenLatLon = now(); // p->oldIdx = (p->oldIdx+1) % 32; @@ -120,4 +116,4 @@ AircraftList::~AircraftList() { head = head->next; delete(temp); } -} \ No newline at end of file +} diff --git a/AppData.cpp b/AppData.cpp index 577968c..61eb889 100644 --- a/AppData.cpp +++ b/AppData.cpp @@ -81,31 +81,31 @@ void AppData::updateStatus() { msgRateAccumulate = 0.0; - // PlaneObj *p = appData.planes; + Aircraft *p = aircraftList.head; - // while(p) { - // unsigned char * pSig = p->signalLevel; - // unsigned int signalAverage = (pSig[0] + pSig[1] + pSig[2] + pSig[3] + - // pSig[4] + pSig[5] + pSig[6] + pSig[7]); + while(p) { + unsigned char * pSig = p->signalLevel; + unsigned int signalAverage = (pSig[0] + pSig[1] + pSig[2] + pSig[3] + + pSig[4] + pSig[5] + pSig[6] + pSig[7]); - // sigAccumulate += signalAverage; + sigAccumulate += signalAverage; - // if (p->lon && p->lat) { - // numVisiblePlanes++; - // } + if (p->lon && p->lat) { + numVisiblePlanes++; + } - // totalCount++; + totalCount++; - // msgRateAccumulate += p->messageRate; + msgRateAccumulate += p->messageRate; - // p = p->next; - // } + p = p->next; + } - // Status.msgRate = msgRateAccumulate; - // Status.avgSig = sigAccumulate / (double) totalCount; - // Status.numPlanes = totalCount; - // Status.numVisiblePlanes = numVisiblePlanes; - // Status.maxDist = maxDist; + msgRate = msgRateAccumulate; + avgSig = sigAccumulate / (double) totalCount; + numPlanes = totalCount; + numVisiblePlanes = numVisiblePlanes; + maxDist = maxDist; } diff --git a/Input.cpp b/Input.cpp index f5496d6..fdab925 100644 --- a/Input.cpp +++ b/Input.cpp @@ -1,13 +1,13 @@ #include "Input.h" -static uint64_t mstime(void) { - struct timeval tv; - uint64_t mst; +#include - gettimeofday(&tv, NULL); - mst = ((uint64_t)tv.tv_sec)*1000; - mst += tv.tv_usec/1000; - return mst; +static uint64_t now() { + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); +} + +static uint64_t elapsed(uint64_t ref) { + return now() - ref; } template int sgn(T val) { @@ -54,13 +54,13 @@ void Input::getInput() view->mapTargetMaxDist = 0; view->mapMoved = 1; - if(mstime() - touchDownTime > 100) { + if(elapsed(touchDownTime) > 100) { touchDownTime = 0; } break; case SDL_FINGERMOTION:; - if(mstime() - touchDownTime > 150) { + if(elapsed(touchDownTime) > 150) { tapCount = 0; touchDownTime = 0; } @@ -68,17 +68,19 @@ void Input::getInput() break; case SDL_FINGERDOWN: - if(mstime() - touchDownTime > 500) { + if(elapsed(touchDownTime) > 500) { tapCount = 0; } - if(SDL_GetNumTouchFingers(event.tfinger.touchId) == 0) { - touchDownTime = mstime(); + + //this finger number is always 1 for down and 0 for up an rpi+hyperpixel?? + if(SDL_GetNumTouchFingers(event.tfinger.touchId) == 1) { + touchDownTime = now(); } break; case SDL_FINGERUP: - if(mstime() - touchDownTime < 150 && SDL_GetNumTouchFingers(event.tfinger.touchId) == 0) { + if(elapsed(touchDownTime) < 150 && SDL_GetNumTouchFingers(event.tfinger.touchId) == 0) { touchx = view->screen_width * event.tfinger.x; touchy = view->screen_height * event.tfinger.y; tapCount++; @@ -93,10 +95,10 @@ void Input::getInput() case SDL_MOUSEBUTTONDOWN: if(event.button.which != SDL_TOUCH_MOUSEID) { - if(mstime() - touchDownTime > 500) { + if(elapsed(touchDownTime) > 500) { tapCount = 0; } - touchDownTime = mstime(); + touchDownTime = now(); } break; diff --git a/Makefile b/Makefile index fb6dc98..5eae658 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # sure that the variable PREFIX is defined, e.g. make PREFIX=/usr/local # -CFLAGS=-O2 -g -Wno-write-strings +CFLAGS=-O3 -Wno-write-strings LIBS=-lm -lSDL2 -lSDL2_ttf -lSDL2_gfx CC=g++ diff --git a/Map.cpp b/Map.cpp index 05be4fd..2baa43a 100644 --- a/Map.cpp +++ b/Map.cpp @@ -157,7 +157,6 @@ Map::Map() { } Polygon *currentPolygon = new Polygon; -printf("A\n"); for(int i = 0; i < mapPoints_count; i+=2) { if(mapPoints[i] == 0) { @@ -171,13 +170,17 @@ printf("A\n"); if(mapPoints[i] < currentPolygon->lon_min) { currentPolygon->lon_min = mapPoints[i]; - } else if(mapPoints[i] > currentPolygon->lon_max) { + } + + if(mapPoints[i] > currentPolygon->lon_max) { currentPolygon->lon_max = mapPoints[i]; } if(mapPoints[i+1] < currentPolygon->lat_min) { currentPolygon->lat_min = mapPoints[i+1]; - } else if(mapPoints[i+1] > currentPolygon->lat_max) { + } + + if(mapPoints[i+1] > currentPolygon->lat_max) { currentPolygon->lat_max = mapPoints[i+1]; } @@ -186,6 +189,5 @@ printf("A\n"); currentPolygon->points.push_back(*currentPoint); } -printf("B\n"); -} \ No newline at end of file +} diff --git a/View.cpp b/View.cpp index bf5856a..d158029 100644 --- a/View.cpp +++ b/View.cpp @@ -7,14 +7,24 @@ #include "View.h" -static uint64_t mstime(void) { - struct timeval tv; - uint64_t mst; +#include - gettimeofday(&tv, NULL); - mst = ((uint64_t)tv.tv_sec)*1000; - mst += tv.tv_usec/1000; - return mst; +#include + +static uint64_t now() { + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); +} + +static time_t now_s() { + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); +} + +static uint64_t elapsed(uint64_t ref) { + return now() - ref; +} + +static time_t elapsed_s(time_t ref) { + return now_s() - ref; } static float sign(float x) { @@ -239,7 +249,7 @@ void View::SDL_init() { } 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_SOFTWARE); mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, @@ -278,11 +288,11 @@ void View::font_init() { // todo separate style stuff // - SDL_Color bgcolor = {10,20,30,255}; + SDL_Color bgcolor = {0,0,0,255}; SDL_Color greenblue = {236,192,68,255}; SDL_Color lightblue = {211,208,203,255}; SDL_Color mediumblue ={110,136,152,255}; - SDL_Color darkblue = {46,82,102,255}; + SDL_Color darkblue = {23,41,51,255}; style.backgroundColor = bgcolor; style.selectedColor = pink; @@ -522,9 +532,12 @@ void View::drawPlaneIcon(int x, int y, float heading, SDL_Color planeColor) } void View::drawTrail(Aircraft *p) { - return; int currentX, currentY, prevX, prevY; + if(p->lonHistory.empty()) { + return; + } + std::vector::iterator lon_idx = p->lonHistory.begin(); std::vector::iterator lat_idx = p->latHistory.begin(); std::vector::iterator heading_idx = p->headingHistory.begin(); @@ -534,15 +547,12 @@ void View::drawTrail(Aircraft *p) { for(; std::next(lon_idx) != p->lonHistory.end(); ++lon_idx, ++lat_idx, ++heading_idx) { float dx, dy; - - pxFromLonLat(&dx, &dy, *std::next(lon_idx), *std::next(lat_idx)); - + pxFromLonLat(&dx, &dy, *(std::next(lon_idx)), *(std::next(lat_idx))); screenCoords(¤tX, ¤tY, dx, dy); pxFromLonLat(&dx, &dy, *lon_idx, *lat_idx); screenCoords(&prevX, &prevY, dx, dy); - if(outOfBounds(currentX,currentY)) { continue; } @@ -631,11 +641,11 @@ void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lo std::vector polyList = map.getPolys(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); std::vector::iterator currentPolygon; - + for (currentPolygon = polyList.begin(); currentPolygon != polyList.end(); ++currentPolygon) { int x1,y1,x2,y2; float dx,dy; - + std::vector::iterator currentPoint; std::vector::iterator prevPoint = currentPolygon->points.begin(); @@ -644,16 +654,12 @@ void View::drawPolys(float screen_lat_min, float screen_lat_max, float screen_lo pxFromLonLat(&dx, &dy, prevPoint->lon, prevPoint->lat); screenCoords(&x1, &y1, dx, dy); - if(outOfBounds(x1,y1)) { - continue; - } - float d1 = dx* dx + dy * dy; pxFromLonLat(&dx, &dy, currentPoint->lon, currentPoint->lat); screenCoords(&x2, &y2, dx, dy); - if(outOfBounds(x2,y2)) { + if(outOfBounds(x1,y1) && outOfBounds(x2,y2)) { continue; } @@ -703,11 +709,11 @@ void View::drawSignalMarks(Aircraft *p, int x, int y) { SDL_Color barColor = signalToColor(signalAverage); - Uint8 seenFade = (Uint8) (255.0 - (mstime() - p->msSeen) / 4.0); + Uint8 seenFade = (Uint8) (255.0 - elapsed(p->msSeen) / 4.0); circleRGBA(renderer, x + mapFontWidth, y - 5, 2 * screen_uiscale, barColor.r, barColor.g, barColor.b, seenFade); - seenFade = (Uint8) (255.0 - (mstime() - p->msSeenLatLon) / 4.0); + seenFade = (Uint8) (255.0 - elapsed(p->msSeenLatLon) / 4.0); hlineRGBA(renderer, x + mapFontWidth + 5 * screen_uiscale, x + mapFontWidth + 9 * screen_uiscale, y - 5, barColor.r, barColor.g, barColor.b, seenFade); vlineRGBA(renderer, x + mapFontWidth + 7 * screen_uiscale, y - 2 * screen_uiscale - 5, y + 2 * screen_uiscale - 5, barColor.r, barColor.g, barColor.b, seenFade); @@ -1037,7 +1043,6 @@ void View::resolveLabelConflicts() { void View::drawPlanes() { Aircraft *p = appData->aircraftList.head; - time_t now = time(NULL); SDL_Color planeColor; // draw all trails first so they don't cover up planes and text @@ -1063,10 +1068,10 @@ void View::drawPlanes() { screenCoords(&x, &y, dx, dy); if(p->created == 0) { - p->created = mstime(); + p->created = now(); } - float age_ms = (float)(mstime() - p->created); + float age_ms = (float)elapsed(p->created); if(age_ms < 500) { circleRGBA(renderer, x, y, 500 - age_ms, 255,255, 255, (uint8_t)(255.0 * age_ms / 500.0)); } else { @@ -1083,11 +1088,11 @@ void View::drawPlanes() { float velx = (x - oldx) / (1000.0 * (p->seenLatLon - p->timestampHistory.back())); float vely = (y - oldy) / (1000.0 * (p->seenLatLon - p->timestampHistory.back())); - usex = x + (mstime() - p->msSeenLatLon) * velx; - usey = y + (mstime() - p->msSeenLatLon) * vely; + usex = x + elapsed(p->msSeenLatLon) * velx; + usey = y + elapsed(p->msSeenLatLon) * vely; } - planeColor = lerpColor(style.planeColor, style.planeGoneColor, (now - p->seen) / (float) DISPLAY_ACTIVE); + planeColor = lerpColor(style.planeColor, style.planeGoneColor, float(elapsed_s(p->seen)) / (float) DISPLAY_ACTIVE); if(p == selectedAircraft) { planeColor = style.selectedColor; @@ -1165,8 +1170,8 @@ void View::moveCenterRelative(float dx, float dy) { float scale_factor = (screen_width > screen_height) ? screen_width : screen_height; - dx = -1.0 * (0.75*(double)screen_width / (double)screen_height) * dx * maxDist / (0.95 * scale_factor * 0.5); - dy = 1.0 * dy * maxDist / (0.95 * scale_factor * 0.5); + dx = -1.0 * dx * maxDist / (0.95 * scale_factor * 0.5); + dy = 1.0 * dy * maxDist / (0.95 * scale_factor * 0.5); float outLat = dy * (1.0/6371.0) * (180.0f / M_PI); @@ -1209,12 +1214,12 @@ void View::moveMapToTarget() { } void View::drawMouse() { - if(mouseMovedTime == 0 || (mstime() - mouseMovedTime) > 1000) { + if(mouseMovedTime == 0 || elapsed(mouseMovedTime) > 1000) { mouseMovedTime = 0; return; } - int alpha = (int)(255.0f - 255.0f * (float)(mstime() - mouseMovedTime) / 1000.0f); + int alpha = (int)(255.0f - 255.0f * (float)elapsed(mouseMovedTime) / 1000.0f); lineRGBA(renderer, mousex - 10 * screen_uiscale, mousey, mousex + 10 * screen_uiscale, mousey, white.r, white.g, white.b, alpha); lineRGBA(renderer, mousex, mousey - 10 * screen_uiscale, mousex, mousey + 10 * screen_uiscale, white.r, white.g, white.b, alpha); @@ -1223,8 +1228,8 @@ void View::drawMouse() { void View::drawClick() { if(clickx && clicky) { - int radius = .25 * (mstime() - clickTime); - int alpha = 128 - (int)(0.5 * (mstime() - clickTime)); + int radius = .25 * elapsed(clickTime); + int alpha = 128 - (int)(0.5 * elapsed(clickTime)); if(alpha < 0 ) { alpha = 0; clickx = 0; @@ -1237,11 +1242,10 @@ void View::drawClick() { if(selectedAircraft) { // this logic should be in input, register a callback for click? - float elapsed = mstime() - clickTime; int boxSize; - if(elapsed < 300) { - boxSize = (int)(20.0 * (1.0 - (1.0 - elapsed / 300.0) * cos(sqrt(elapsed)))); + if(elapsed(clickTime) < 300) { + boxSize = (int)(20.0 * (1.0 - (1.0 - float(elapsed(clickTime)) / 300.0) * cos(sqrt(float(elapsed(clickTime)))))); } else { boxSize = 20; } @@ -1290,11 +1294,11 @@ void View::registerClick(int tapcount, int x, int y) { clickx = x; clicky = y; - clickTime = mstime(); + clickTime = now(); } void View::registerMouseMove(int x, int y) { - mouseMovedTime = mstime(); + mouseMovedTime = now(); this->mousex = x; this->mousey = y; } @@ -1304,7 +1308,7 @@ void View::registerMouseMove(int x, int y) { // void View::draw() { - uint64_t drawStartTime = mstime(); + uint64_t drawStartTime = now(); moveMapToTarget(); zoomMapToTarget(); @@ -1344,16 +1348,17 @@ void View::draw() { drawClick(); char fps[13] = " "; - snprintf(fps,13," %.1ffps", 1000.0 / (mstime() - lastFrameTime)); + snprintf(fps,13," %.1ffps", 1000.0 / elapsed(lastFrameTime)); drawStringBG(fps, 0,0, mapFont, grey, black); SDL_RenderPresent(renderer); - lastFrameTime = mstime(); + lastFrameTime = now(); - if ((mstime() - drawStartTime) < FRAMETIME) { - usleep(1000 * (FRAMETIME - (mstime() - drawStartTime))); + if (elapsed(drawStartTime) < FRAMETIME) { + usleep(1000 * (FRAMETIME - elapsed(drawStartTime))); } + } View::View(AppData *appData){ @@ -1381,4 +1386,4 @@ View::~View() { TTF_Quit(); SDL_Quit(); -} \ No newline at end of file +} diff --git a/View.h b/View.h index d8a12ba..9991b71 100644 --- a/View.h +++ b/View.h @@ -9,7 +9,7 @@ //defs - should all move to config file setup #define ROUND_RADIUS 3 //radius of text box corners -#define CENTEROFFSET .375 //vertical offset for middle of screen +#define CENTEROFFSET .5 //vertical offset for middle of screen #define TRAIL_LENGTH 120 #define TRAIL_TTL 240.0 @@ -147,4 +147,4 @@ class View { int messageFontHeight; }; -#endif \ No newline at end of file +#endif diff --git a/drawPolys.REMOVED.git-id b/drawPolys.REMOVED.git-id new file mode 100644 index 0000000..f60973a --- /dev/null +++ b/drawPolys.REMOVED.git-id @@ -0,0 +1 @@ +85c850fc7f07d0cf30352d3385c7a2aeb57fecf3 \ No newline at end of file diff --git a/mapconverter.py b/mapconverter.py index fc0d9f3..23c3fe4 100644 --- a/mapconverter.py +++ b/mapconverter.py @@ -18,11 +18,10 @@ bin_file = open("mapdata.bin", "wb") outlist = [] -precision = 2 - +resolution = 250 print("Reading points") -#for i in tqdm(range(len(polys))): -for i in range(40): +for i in tqdm(range(len(polys))): +#for i in range(40): p = polys[i] currentPoints = (p.attrib['points']).replace(","," ").split() @@ -35,22 +34,25 @@ for i in range(40): 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])) + #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] + currentPoints[2 * i + 0] = float(int(resolution * float(currentPoints[2 * i + 0]))) / resolution + currentPoints[2 * i + 1] = float(int(resolution * float(currentPoints[2 * i + 1]))) / resolution + if(currentPoints[2 * i + 0] != prevx and currentPoints[2 * i + 1] != prevy): + temp.extend([currentPoints[2 * i + 0],currentPoints[2 * i + 1]]) - if(len(currentPoints) > 14): - outlist.extend(temp) - outlist.extend(["0","0"]) + prevx = currentPoints[2 * i + 0] + prevy = currentPoints[2 * i + 1] + + if(len(currentPoints) > 6): #must be at least a triangle + outlist.extend(temp) + outlist.extend([temp[0],temp[1]]) + 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 +print("Wrote %d points" % (len(outlist) / 2)) diff --git a/out.prof.REMOVED.git-id b/out.prof.REMOVED.git-id new file mode 100644 index 0000000..5f8f31c --- /dev/null +++ b/out.prof.REMOVED.git-id @@ -0,0 +1 @@ +34c8403286643597ad193fa68d13266d3a5f07a6 \ No newline at end of file diff --git a/viz1090.cpp b/viz1090.cpp index 6b25990..577f95b 100644 --- a/viz1090.cpp +++ b/viz1090.cpp @@ -34,18 +34,6 @@ #include "Input.h" #include -//time utility, might change to std::chrono -uint64_t mstime(void) { - struct timeval tv; - uint64_t mst; - - gettimeofday(&tv, NULL); - mst = ((uint64_t)tv.tv_sec)*1000; - mst += tv.tv_usec/1000; - return mst; -} - - int go = 1;