#include "mapTiles.h" #include #include #include "config.h" #include "atcommon.h" void sMapTile::clear() { memset(this,0,sizeof(sMapTile)); } bool sMapTile::loadFromJson(const JsonVariant &json, uint32_t baseAddress) { if(json.containsKey("size")&&json.containsKey("bounds")&&json.containsKey("x")&&json.containsKey("y") &&json.containsKey("address")&&json.containsKey("image_stride")&&json.containsKey("image_size")&&json.containsKey("image_fmt")) { sizeX = json["size"][0]; sizeY = json["size"][1]; address = json["address"]; address += baseAddress; size = json["image_size"]; stride = json["image_stride"]; fmt = json["image_fmt"]; tileX = json["x"]; tileY = json["y"]; bound.x0 = json["bounds"][0].as(); bound.y0 = json["bounds"][1].as(); bound.x1 = json["bounds"][2].as(); bound.y1 = json["bounds"][3].as(); mapSizeX = bound.x1-bound.x0; mapSizeY = bound.y0-bound.y1; return true; } return false; } cMapZoomLevel::cMapZoomLevel() { clear(); } void cMapZoomLevel::clear() { memset(&m_zlevel,0,sizeof(m_zlevel)); m_images.clear(); } void cMapZoomLevel::loadFromBinary(std::ifstream &file) { uint16_t cnt = 0; file.read((char*)&m_zlevel,sizeof(m_zlevel)); file.read((char*)&cnt,2); m_images.resize(cnt); file.read((char*)m_images.data(),cnt*sizeof(sMapTile)); } bool cMapZoomLevel::loadFromJson(const JsonVariant &json, uint32_t baseAddress) { if(json.containsKey("pixel_size")&&json.containsKey("bounds")&&json.containsKey("images")&&json.containsKey("tile_size")&&json.containsKey("id")) { clear(); m_zlevel.id = json["id"]; m_zlevel.pixelSizeX = json["pixel_size"][0].as(); m_zlevel.pixelSizeY = json["pixel_size"][1].as(); m_zlevel.tileSizeX = json["tile_size"][0].as(); m_zlevel.tileSizeY = json["tile_size"][1].as(); m_zlevel.bound.x0 = json["bounds"][0].as(); m_zlevel.bound.y0 = json["bounds"][1].as(); m_zlevel.bound.x1 = json["bounds"][2].as(); m_zlevel.bound.y1 = json["bounds"][3].as(); const JsonVariant &images = json.getMember("images"); m_images.resize(images.size()); for(int i=0; i(); m_map.bound.x0 = json["bounds"][0].as(); m_map.bound.y0 = json["bounds"][1].as(); m_map.bound.x1 = json["bounds"][2].as(); m_map.bound.y1 = json["bounds"][3].as(); const JsonVariant &zls = json.getMember("tile_matrix"); for(int i=0; i=0000) { itr = m_maps.erase(itr); continue; } } itr++; } } cMapTilesList cMapProcessor::getTilesToRender(double x, double y, double radius) { cMapTilesList result; cMap *map = NULL; if(m_lastMap) { if(checkMap(x,y,*m_lastMap)) { map = m_lastMap; } else { m_lastMap = NULL; } } if(!map) { for(auto itr=m_maps.begin(); itr!=m_maps.end(); itr++) { if(checkMap(x,y,*itr)) { m_lastMap = &*itr; map = m_lastMap; } } // if(map) // clearUnusedMaps(x,y); } if(map) { if(!map->isLoaded()) { map->loadMapData(); if(m_callbacks) m_callbacks->onMapLoad(map); } double zoomDiff = 0; cMapZoomLevel *zoomLevel = NULL; cMapZoomLevels &zls = map->getMapZoomLevels(); for(auto itr=zls.begin(); itr!=zls.end(); itr++) { sMapZoomLevel &zl = itr->getZoomLevel(); double zoomSize = std::max(abs(zl.tileSizeX),abs(zl.tileSizeY))/2; if(!zoomLevel || abs(zoomSize-radius)getImages(); sBoundaries sb = getScreenBoundaries(x,y,radius); for(auto itr=tiles.begin(); itr !=tiles.end(); itr++) { if(checkIntersection(sb,itr->bound)) { result.push_back(*itr); } } } } return result; } bool cMapProcessor::checkMap(const double &x, const double &y,cMap &map) { return isPointInside(x,y,map.getMap().bound); } bool cMapProcessor::checkIntersection(const sBoundaries &b1, const sBoundaries &b2) const { return isPointInside(b1.x0,b1.y0,b2) || isPointInside(b1.x1,b1.y0,b2) || isPointInside(b1.x0,b1.y1,b2) || isPointInside(b1.x1,b1.y1,b2) || isPointInside(b2.x0,b2.y0,b1) || isPointInside(b2.x1,b2.y0,b1) || isPointInside(b2.x0,b2.y1,b1) || isPointInside(b2.x1,b2.y1,b1); } void cMapProcessor::calcTile(double x, double y, const sMapTile &tile, double radius, double heading, double &scaleX, double &scaleY, double &tx, double &ty) { scaleX = (tile.mapSizeX)/(radius*2); scaleY = (tile.mapSizeY)/(radius*2); tx = (tile.bound.x0-x)*tile.sizeX/tile.mapSizeX;//*(240/(240+3/scaleX)); ty = (y-tile.bound.y0)*tile.sizeY/tile.mapSizeY;//*(240/(240+3/scaleY)); } double cMapProcessor::calcDistanceC(double x, double y) { double lat1,lng1; double lat2,lng2; convertMeterToDegree(x,y,lng1,lat1); convertMeterToDegree(x,y+1000,lng2,lat2); double d = distanceBetween(lat1, lng1, lat2, lng2); if(d>0) return abs(1000/d); else return 1; }