testing hard coded multipliers for latlon conversion and different quadtree filling logic

Former-commit-id: eee50502c57870a95890f85711286f3138da7ec7
This commit is contained in:
nathan 2020-06-13 20:54:21 -07:00
parent cd1ce278c1
commit 6caca48666
4 changed files with 141 additions and 63 deletions

74
Map.cpp
View file

@ -6,16 +6,42 @@ bool Map::QTInsert(QuadTree *tree, Line *line, int depth) {
// printf("Inserting %d point poly\n", line->numPoints); // printf("Inserting %d point poly\n", line->numPoints);
if (!(line->lat_min >= tree->lat_min && // if (!(line->lat_min >= tree->lat_min &&
line->lat_max <= tree->lat_max && // line->lat_max <= tree->lat_max &&
line->lon_min >= tree->lon_min && // line->lon_min >= tree->lon_min &&
line->lon_max <= tree->lon_max)) { // 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); // // 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);
return false; // return false;
// }
if (!(line->start.lat >= tree->lat_min &&
line->start.lat <= tree->lat_max &&
line->start.lon >= tree->lon_min &&
line->start.lon <= tree->lon_max) &&
!(line->end.lat >= tree->lat_min &&
line->end.lat <= tree->lat_max &&
line->end.lon >= tree->lon_min &&
line->end.lon <= tree->lon_max)
) {
return false;
} }
if ((line->start.lat >= tree->lat_min &&
line->start.lat <= tree->lat_max &&
line->start.lon >= tree->lon_min &&
line->start.lon <= tree->lon_max)!=
(line->end.lat >= tree->lat_min &&
line->end.lat <= tree->lat_max &&
line->end.lon >= tree->lon_min &&
line->end.lon <= tree->lon_max)
) {
tree->lines.push_back(&(*line));
return true;
}
// //temp maxdepth for debugging // //temp maxdepth for debugging
// if(depth > 20) { // if(depth > 20) {
// tree->lines.push_back(*line); // tree->lines.push_back(*line);
@ -74,13 +100,13 @@ bool Map::QTInsert(QuadTree *tree, Line *line, int depth) {
return true; return true;
} }
tree->lines.push_back(*line); tree->lines.push_back(&(*line));
return true; return true;
} }
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*> Map::getLinesRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) {
std::vector<Line> retLines; std::vector<Line*> retLines;
if(tree == NULL) { if(tree == NULL) {
return retLines; return retLines;
@ -94,7 +120,7 @@ std::vector<Line> Map::getLinesRecursive(QuadTree *tree, float screen_lat_min, f
return retLines; return retLines;
} }
std::vector<Line> ret; std::vector<Line*> ret;
ret = getLinesRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); ret = getLinesRecursive(tree->nw, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
retLines.insert(retLines.end(), ret.begin(), ret.end()); retLines.insert(retLines.end(), ret.begin(), ret.end());
@ -109,10 +135,30 @@ std::vector<Line> Map::getLinesRecursive(QuadTree *tree, float screen_lat_min, f
retLines.insert(retLines.end(), tree->lines.begin(), tree->lines.end()); retLines.insert(retLines.end(), tree->lines.begin(), tree->lines.end());
// Debug quadtree
// Point TL, TR, BL, BR;
// TL.lat = tree->lat_min;
// TL.lon = tree->lon_min;
// TR.lat = tree->lat_max;
// TR.lon = tree->lon_min;
// BL.lat = tree->lat_min;
// BL.lon = tree->lon_max;
// BR.lat = tree->lat_max;
// BR.lon = tree->lon_max;
// retLines.push_back(new Line(TL,TR));
// retLines.push_back(new Line(TR,BR));
// retLines.push_back(new Line(BL,BR));
// retLines.push_back(new Line(TL,BL));
return retLines; return retLines;
} }
std::vector<Line> Map::getLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max) { 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); return getLinesRecursive(&root, screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
}; };
@ -163,6 +209,12 @@ Map::Map() {
for(int i = 0; i < mapPoints_count - 2; i+=2) { for(int i = 0; i < mapPoints_count - 2; i+=2) {
if(mapPoints[i] == 0) if(mapPoints[i] == 0)
continue; continue;
if(mapPoints[i + 1] == 0)
continue;
if(mapPoints[i + 2] == 0)
continue;
if(mapPoints[i + 3] == 0)
continue;
currentPoint.lon = mapPoints[i]; currentPoint.lon = mapPoints[i];
currentPoint.lat = mapPoints[i + 1]; currentPoint.lat = mapPoints[i + 1];

6
Map.h
View file

@ -34,7 +34,7 @@ typedef struct QuadTree{
float lon_min; float lon_min;
float lon_max; float lon_max;
std::vector<Line> lines; std::vector<Line*> lines;
struct QuadTree *nw; struct QuadTree *nw;
struct QuadTree *sw; struct QuadTree *sw;
@ -78,8 +78,8 @@ public:
QuadTree root; QuadTree root;
bool QTInsert(QuadTree *tree, Line *line, int depth); bool QTInsert(QuadTree *tree, Line *line, int depth);
std::vector<Line> getLinesRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); std::vector<Line*> getLinesRecursive(QuadTree *tree, float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max);
std::vector<Line> getLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max); std::vector<Line*> getLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max);
Map(); Map();

116
View.cpp
View file

@ -154,7 +154,8 @@ SDL_Color hsv2SDLColor(float h, float s, float v)
int View::screenDist(float d) { int View::screenDist(float d) {
float scale_factor = (screen_width > screen_height) ? screen_width : screen_height; float scale_factor = (screen_width > screen_height) ? screen_width : screen_height;
return round(0.95 * scale_factor * 0.5 * fabs(d) / maxDist); // return round(0.95 * scale_factor * 0.5 * fabs(d) / maxDist);
return round(scale_factor * 0.5 * fabs(d) / maxDist);
} }
void View::pxFromLonLat(float *dx, float *dy, float lon, float lat) { void View::pxFromLonLat(float *dx, float *dy, float lon, float lat) {
@ -164,8 +165,9 @@ void View::pxFromLonLat(float *dx, float *dy, float lon, float lat) {
return; return;
} }
*dx = 6371.0 * (lon - centerLon) * M_PI / 180.0f * cos(((lat + centerLat)/2.0f) * M_PI / 180.0f); // for accurate reprojection use the extra cos term
*dy = 6371.0 * (lat - centerLat) * M_PI / 180.0f; *dx = LATLONMULT * (lon - centerLon) * cos(((lat + centerLat)/2.0f) * M_PI / 180.0f);
*dy = LATLONMULT * (lat - centerLat);
} }
void View::latLonFromScreenCoords(float *lat, float *lon, int x, int y) { void View::latLonFromScreenCoords(float *lat, float *lon, int x, int y) {
@ -639,9 +641,9 @@ void View::drawScaleBars()
} }
void View::drawLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max, int bailTime) { void View::drawLines(float screen_lat_min, float screen_lat_max, float screen_lon_min, float screen_lon_max, int bailTime) {
std::vector<Line> lineList = map.getLines(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max); std::vector<Line*> lineList = map.getLines(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max);
std::vector<Line>::iterator currentLine; std::vector<Line*>::iterator currentLine;
for (currentLine = lineList.begin(); currentLine != lineList.end(); ++currentLine) { for (currentLine = lineList.begin(); currentLine != lineList.end(); ++currentLine) {
int x1,y1,x2,y2; int x1,y1,x2,y2;
@ -653,16 +655,22 @@ void View::drawLines(float screen_lat_min, float screen_lat_max, float screen_lo
} }
} }
pxFromLonLat(&dx, &dy, currentLine->start.lon, currentLine->start.lat); pxFromLonLat(&dx, &dy, (*currentLine)->start.lon, (*currentLine)->start.lat);
screenCoords(&x1, &y1, dx, dy); screenCoords(&x1, &y1, dx, dy);
pxFromLonLat(&dx, &dy, currentLine->end.lon, currentLine->end.lat); pxFromLonLat(&dx, &dy, (*currentLine)->end.lon, (*currentLine)->end.lat);
screenCoords(&x2, &y2, dx, dy); screenCoords(&x2, &y2, dx, dy);
lineCount++;
if(outOfBounds(x1,y1) && outOfBounds(x2,y2)) { if(outOfBounds(x1,y1) && outOfBounds(x2,y2)) {
continue; continue;
} }
if(x1 == x2 && y1 == y2) {
continue;
}
lineRGBA(renderer, x1, y1, x2, y2, style.mapInnerColor.r, style.mapInnerColor.g, style.mapInnerColor.b, 255); lineRGBA(renderer, x1, y1, x2, y2, style.mapInnerColor.r, style.mapInnerColor.g, style.mapInnerColor.b, 255);
} }
@ -674,6 +682,12 @@ void View::drawGeography(int left, int top, int right, int bottom, int bailTime)
latLonFromScreenCoords(&screen_lat_min, &screen_lon_min, left, top); latLonFromScreenCoords(&screen_lat_min, &screen_lon_min, left, top);
latLonFromScreenCoords(&screen_lat_max, &screen_lon_max, right, bottom); latLonFromScreenCoords(&screen_lat_max, &screen_lon_max, right, bottom);
// screen_lat_min = 47.4;
// screen_lat_max = 47.8;
// screen_lon_min = -122.5;
// screen_lon_max = -121.5;
drawLines(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max, bailTime); drawLines(screen_lat_min, screen_lat_max, screen_lon_min, screen_lon_max, bailTime);
} }
@ -1290,7 +1304,11 @@ void View::draw() {
//updatePlanes(); //updatePlanes();
if(mapRedraw && !mapMoved) { lineCount = 0;
if(mapMoved) {
//if(mapRedraw && !mapMoved) {
SDL_SetRenderTarget(renderer, mapTexture); SDL_SetRenderTarget(renderer, mapTexture);
SDL_SetRenderDrawColor(renderer, style.backgroundColor.r, style.backgroundColor.g, style.backgroundColor.b, 255); SDL_SetRenderDrawColor(renderer, style.backgroundColor.r, style.backgroundColor.g, style.backgroundColor.b, 255);
@ -1317,58 +1335,58 @@ void View::draw() {
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
int shiftx = 0; // int shiftx = 0;
int shifty = 0; // int shifty = 0;
if(mapMoved) { // if(mapMoved) {
float dx, dy; // float dx, dy;
int x1,y1, x2, y2; // int x1,y1, x2, y2;
pxFromLonLat(&dx, &dy, currentLon, currentLat); // pxFromLonLat(&dx, &dy, currentLon, currentLat);
screenCoords(&x1, &y1, dx, dy); // screenCoords(&x1, &y1, dx, dy);
pxFromLonLat(&dx, &dy, centerLon, centerLat); // pxFromLonLat(&dx, &dy, centerLon, centerLat);
screenCoords(&x2, &y2, dx, dy); // screenCoords(&x2, &y2, dx, dy);
shiftx = x1-x2; // shiftx = x1-x2;
shifty = y1-y2; // shifty = y1-y2;
mapRedraw = 1; // mapRedraw = 1;
SDL_Rect dest; // SDL_Rect dest;
dest.x = shiftx + (screen_width / 2) * (1 - currentMaxDist / maxDist); // dest.x = shiftx + (screen_width / 2) * (1 - currentMaxDist / maxDist);
dest.y = shifty + (screen_height / 2) * (1 - currentMaxDist / maxDist); // dest.y = shifty + (screen_height / 2) * (1 - currentMaxDist / maxDist);
dest.w = screen_width * currentMaxDist / maxDist; // dest.w = screen_width * currentMaxDist / maxDist;
dest.h = screen_height * currentMaxDist / maxDist; // dest.h = screen_height * currentMaxDist / maxDist;
//left // //left
if(dest.x > 0) { // if(dest.x > 0) {
drawGeography(0, 0, dest.x, screen_height, FRAMETIME / 4); // drawGeography(0, 0, dest.x, screen_height, FRAMETIME / 4);
} // }
//top // //top
if(dest.y > 0) { // if(dest.y > 0) {
drawGeography(0, screen_height - dest.y, screen_width, screen_height, FRAMETIME / 4); // drawGeography(0, screen_height - dest.y, screen_width, screen_height, FRAMETIME / 4);
} // }
//right // //right
if(dest.x + dest.w < screen_width) { // if(dest.x + dest.w < screen_width) {
drawGeography(dest.x + dest.w, 0, screen_width, screen_height, FRAMETIME / 4); // drawGeography(dest.x + dest.w, 0, screen_width, screen_height, FRAMETIME / 4);
} // }
//bottom // //bottom
if(dest.y + dest.h < screen_height) { // if(dest.y + dest.h < screen_height) {
drawGeography(0, 0, screen_width, screen_height - dest.y - dest.h, FRAMETIME / 4); // drawGeography(0, 0, screen_width, screen_height - dest.y - dest.h, FRAMETIME / 4);
} // }
//attempt rest before bailing // //attempt rest before bailing
//drawGeography(dest.x, screen_height - dest.y, dest.x + dest.w, screen_height - dest.y - dest.h, 1); // //drawGeography(dest.x, screen_height - dest.y, dest.x + dest.w, screen_height - dest.y - dest.h, 1);
SDL_RenderCopy(renderer, mapTexture, NULL, &dest); // SDL_RenderCopy(renderer, mapTexture, NULL, &dest);
mapMoved = 0; // mapMoved = 0;
} else { // } else {
SDL_RenderCopy(renderer, mapTexture, NULL, NULL); SDL_RenderCopy(renderer, mapTexture, NULL, NULL);
} //}
drawScaleBars(); drawScaleBars();
@ -1377,8 +1395,8 @@ void View::draw() {
drawMouse(); drawMouse();
drawClick(); drawClick();
char fps[13] = " "; char fps[40] = " ";
snprintf(fps,13," %.1ffps", 1000.0 / elapsed(lastFrameTime)); snprintf(fps,40," %d lines @ %.1ffps", lineCount, 1000.0 / elapsed(lastFrameTime));
drawStringBG(fps, 0,0, mapFont, grey, black); drawStringBG(fps, 0,0, mapFont, grey, black);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);

8
View.h
View file

@ -22,6 +22,9 @@
#define PAD 5 #define PAD 5
#define LATLONMULT 111.195 // 6371.0 * M_PI / 180.0
// //
@ -57,6 +60,11 @@ class View {
int clickx; int clickx;
int clicky; int clicky;
int lineCount;
float dx_mult;
float dy_mult;
TTF_Font* loadFont(char *name, int size); TTF_Font* loadFont(char *name, int size);
void closeFont(TTF_Font *font); void closeFont(TTF_Font *font);
void drawString(char * text, int x, int y, TTF_Font *font, SDL_Color color); void drawString(char * text, int x, int y, TTF_Font *font, SDL_Color color);