Skip to content

Commit

Permalink
Issue Framstag#1045 - Cleanup of MapPainter
Browse files Browse the repository at this point in the history
- Made more use of Vertex2D
- Simplify APIs by using Vertex2D instead of x,y
- Added Way.IsValid()
- Reduce Sonar warnings
  • Loading branch information
Framstag authored and DerKleinePunk committed Jul 16, 2023
1 parent 6727f6b commit 95b09be
Show file tree
Hide file tree
Showing 34 changed files with 550 additions and 454 deletions.
46 changes: 32 additions & 14 deletions Demos/src/RoutingAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ void drawDot(QPainter &painter,
const osmscout::MercatorProjection &projection,
const osmscout::GeoCoord &coord){

double x,y;
projection.GeoToPixel(coord,x,y);
osmscout::Vertex2D pos;
projection.GeoToPixel(coord,
pos);
painter.setPen(Qt::NoPen);
painter.drawEllipse(x,y,10,10);
painter.drawEllipse(pos.GetX(),pos.GetY(),10,10);
}

class RoutingServiceAnimation: public osmscout::SimpleRoutingService{
Expand Down Expand Up @@ -167,8 +168,6 @@ class RoutingServiceAnimation: public osmscout::SimpleRoutingService{
}
stepCounter++;

double x1,y1;
double x2,y2;
osmscout::RouteNodeRef n1;
osmscout::RouteNodeRef n2;

Expand Down Expand Up @@ -208,10 +207,16 @@ class RoutingServiceAnimation: public osmscout::SimpleRoutingService{
pen.setColor(red);
}

projection.GeoToPixel(n1->GetCoord(),x1,y1);
projection.GeoToPixel(n2->GetCoord(),x2,y2);
osmscout::Vertex2D pos1;
osmscout::Vertex2D pos2;

projection.GeoToPixel(n1->GetCoord(),
pos1);
projection.GeoToPixel(n2->GetCoord(),
pos2);
painter.setPen(pen);
painter.drawLine(x1,y1,x2,y2);
painter.drawLine(pos1.GetX(),pos1.GetY(),
pos2.GetX(),pos2.GetY());
}

// draw nodes in open list
Expand All @@ -224,10 +229,16 @@ class RoutingServiceAnimation: public osmscout::SimpleRoutingService{
if (!GetRouteNode(open->prev,n1)){
return false;
}
projection.GeoToPixel(n1->GetCoord(),x1,y1);
projection.GeoToPixel(open->node->GetCoord(),x2,y2);
osmscout::Vertex2D pos1;
osmscout::Vertex2D pos2;

projection.GeoToPixel(n1->GetCoord(),
pos1);
projection.GeoToPixel(open->node->GetCoord(),
pos2);
painter.setPen(pen);
painter.drawLine(x1,y1,x2,y2);
painter.drawLine(pos1.GetX(),pos1.GetY(),
pos2.GetX(),pos2.GetY());
}
}

Expand All @@ -239,10 +250,17 @@ class RoutingServiceAnimation: public osmscout::SimpleRoutingService{
if (!GetRouteNode(current->prev,n1)){
return false;
}
projection.GeoToPixel(n1->GetCoord(),x1,y1);
projection.GeoToPixel(current->node->GetCoord(),x2,y2);

osmscout::Vertex2D pos1;
osmscout::Vertex2D pos2;

projection.GeoToPixel(n1->GetCoord(),
pos1);
projection.GeoToPixel(current->node->GetCoord(),
pos2);
painter.setPen(pen);
painter.drawLine(x1,y1,x2,y2);
painter.drawLine(pos1.GetX(),pos1.GetY(),
pos2.GetX(),pos2.GetY());
}

painter.end();
Expand Down
9 changes: 5 additions & 4 deletions libosmscout-client-qt/src/osmscoutclientqt/IconAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ void IconAnimation::deactivateAll()
void IconAnimation::paint(QPainter *painter, const MercatorProjection &projection)
{
for (Animation &animation: icons) {
double x,y;
projection.GeoToPixel(animation.icon.coord, x, y);
osmscout::Vertex2D screenPos;
projection.GeoToPixel(animation.icon.coord,
screenPos);
double w = animation.size;
double h = animation.icon.image.height() * (w / animation.icon.image.width());

// draw semitransparent black circle as background
double r = std::sqrt(std::pow(w, 2) + std::pow(h, 2)) / 2;
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor::fromRgbF(0, 0, 0, 0.1)));
painter->drawEllipse(QPointF(x, y) ,r ,r);
painter->drawEllipse(QPointF(screenPos.GetX(), screenPos.GetY()) ,r ,r);

painter->drawImage(QRectF(x-w/2, y-h/2, w, h), animation.icon.image);
painter->drawImage(QRectF(screenPos.GetX()-w/2, screenPos.GetY()-h/2, w, h), animation.icon.image);
}
}

Expand Down
20 changes: 14 additions & 6 deletions libosmscout-client-qt/src/osmscoutclientqt/IconLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,29 @@ void IconLookup::lookupIcons(const QString &databasePath,
const TypeInfoRef& type,
const FeatureValueBuffer& featureBuffer) {
if (iconStyle && iconStyle->IsVisible() && !iconStyle->IsOverlay()) {
double x, y;
projection.GeoToPixel(coord, x, y);
osmscout::Vertex2D screenPos;
projection.GeoToPixel(coord,
screenPos);
QRectF iconRect;

if (!iconStyle->GetIconName().empty()) {
iconRect=QRectF(x - iconSize/2, y-iconSize/2, iconSize, iconSize);
iconRect=QRectF(screenPos.GetX() - iconSize/2,
screenPos.GetY()-iconSize/2,
iconSize,
iconSize);
} else {
auto symbol=iconStyle->GetSymbol();
assert(symbol);
double w=symbol->GetWidth(projection);
double h=symbol->GetHeight(projection);
iconRect=QRectF(x - w/2, y-h/2, w, h);
iconRect=QRectF(screenPos.GetX() - w/2,
screenPos.GetY()-h/2,
w,
h);
}
if (iconRect.intersects(tapRectangle)) {
double distanceSquare=iconRect.contains(lookupCoord) ? 0 :
std::pow(lookupCoord.x()-x,2)+std::pow(lookupCoord.y()-y,2);
std::pow(lookupCoord.x()-screenPos.GetX(),2)+std::pow(lookupCoord.y()-screenPos.GetY(),2);

QString name;
QString altName;
Expand Down Expand Up @@ -119,7 +126,8 @@ void IconLookup::lookupIcons(const QString &databasePath,
website=QString::fromStdString(websiteValue->GetWebsite());
}

findIcons.push_back(MapIcon{QPoint(x,y), iconRect, coord, distanceSquare, iconStyle,
findIcons.push_back(MapIcon{QPoint(screenPos.GetX(),screenPos.GetY()),
iconRect, coord, distanceSquare, iconStyle,
databasePath, objectRef, poiId, QString::fromStdString(type->GetName()),
name, altName, ref, operatorName, phone, website, QImage()});
}
Expand Down
10 changes: 5 additions & 5 deletions libosmscout-client-qt/src/osmscoutclientqt/InputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,11 @@ bool LockHandler::currentPosition(bool locationValid, osmscout::GeoCoord current
return false;
}

double x;
double y;
projection.GeoToPixel(currentPosition, x, y);
double distanceFromCenter = sqrt(pow(std::abs(projection.GetWidth()/2 - x), 2) +
pow(std::abs(projection.GetHeight()/2 - y), 2));
Vertex2D screenPos;
projection.GeoToPixel(currentPosition,
screenPos);
double distanceFromCenter = sqrt(pow(std::abs(projection.GetWidth()/2 - screenPos.GetX()), 2) +
pow(std::abs(projection.GetHeight()/2 - screenPos.GetY()), 2));

double moveTolerance = std::min(window.width(), window.height()) / 4;
if (distanceFromCenter > moveTolerance){
Expand Down
33 changes: 20 additions & 13 deletions libosmscout-client-qt/src/osmscoutclientqt/LookupModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ void LookupModule::filterObjectInView(const osmscout::MapData &mapData,
std::map<osmscout::FileOffset,osmscout::AdminRegionRef> regionMap;
DBInstanceRef db;

double x;
double y;
double x2;
double y2;

//std::cout << "nodes: " << d.nodes.size() << std::endl;
for (auto const &n:mapData.nodes){
projection.GeoToPixel(n->GetCoords(),x,y);
if (filterRectangle.contains(x,y)){
osmscout::Vertex2D screenPos;
projection.GeoToPixel(n->GetCoords(),
screenPos);
if (filterRectangle.contains(screenPos.GetX(),screenPos.GetY())){
addObjectInfo(objectList, n, reverseLookupMap, db, regionMap);
}
}
Expand All @@ -241,9 +238,14 @@ void LookupModule::filterObjectInView(const osmscout::MapData &mapData,
for (auto const &w:mapData.ways){
// TODO: better detection
osmscout::GeoBox bbox=w->GetBoundingBox();
projection.GeoToPixel(bbox.GetMinCoord(),x,y);
projection.GeoToPixel(bbox.GetMaxCoord(),x2,y2);
if (filterRectangle.intersects(QRectF(QPointF(x,y),QPointF(x2,y2)))){
osmscout::Vertex2D minScreenPos;
osmscout::Vertex2D maxScreenPos;
projection.GeoToPixel(bbox.GetMinCoord(),
minScreenPos);
projection.GeoToPixel(bbox.GetMaxCoord(),
maxScreenPos);
if (filterRectangle.intersects(QRectF(QPointF(minScreenPos.GetX(),minScreenPos.GetY()),
QPointF(maxScreenPos.GetX(),maxScreenPos.GetY())))){
addObjectInfo(objectList, w, reverseLookupMap, db, regionMap);
}
}
Expand All @@ -252,9 +254,14 @@ void LookupModule::filterObjectInView(const osmscout::MapData &mapData,
for (auto const &a:mapData.areas){
// TODO: better detection
osmscout::GeoBox bbox=a->GetBoundingBox();
projection.GeoToPixel(bbox.GetMinCoord(),x,y);
projection.GeoToPixel(bbox.GetMaxCoord(),x2,y2);
if (filterRectangle.intersects(QRectF(QPointF(x,y),QPointF(x2,y2)))){
osmscout::Vertex2D minScreenPos;
osmscout::Vertex2D maxScreenPos;
projection.GeoToPixel(bbox.GetMinCoord(),
minScreenPos);
projection.GeoToPixel(bbox.GetMaxCoord(),
maxScreenPos);
if (filterRectangle.intersects(QRectF(QPointF(minScreenPos.GetX(),minScreenPos.GetY()),
QPointF(maxScreenPos.GetY(),maxScreenPos.GetY())))){
addObjectInfo(objectList, a, reverseLookupMap, db, regionMap);
}
}
Expand Down
4 changes: 2 additions & 2 deletions libosmscout-client-qt/src/osmscoutclientqt/MapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void DBRenderJob::Run(const osmscout::BasemapDatabaseRef& basemapDatabase,

auto *painter = db->GetPainter();
if (painter != nullptr) {
batch.addData(data, painter);
batch.AddData(data, painter);
} else {
osmscout::log.Warn() << "Painter is not available for database: " << db->path.toStdString();
success = false;
Expand All @@ -344,7 +344,7 @@ void DBRenderJob::Run(const osmscout::BasemapDatabaseRef& basemapDatabase,
addOverlayObjectData(data, emptyStyleConfig->GetTypeConfig());
painter=std::make_unique<osmscout::MapPainterQt>(emptyStyleConfig);
MapPainterQt *p = painter.get();
batch.addData(data, p);
batch.AddData(data, p);
}

// draw databases
Expand Down
51 changes: 31 additions & 20 deletions libosmscout-client-qt/src/osmscoutclientqt/MapWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ void MapWidget::paint(QPainter *painter)
// render vehicle
if (vehicle.position && !vehicle.getIcon().isNull()){
QImage vehicleIcon=vehicle.getIcon();
double x;
double y;
projection.GeoToPixel(vehicle.position->getCoord(), x, y);
osmscout::Vertex2D screenPos;
projection.GeoToPixel(vehicle.position->getCoord(),
screenPos);

Bearing iconAngle;
if (vehicle.position->getBearing()) {
Expand All @@ -321,26 +321,32 @@ void MapWidget::paint(QPainter *painter)
}

painter->save();
painter->translate(x,y);
painter->translate(screenPos.GetX(),
screenPos.GetY());
painter->rotate(iconAngle.AsDegrees());
// draw vehicleIcon center on coordinate 0x0
painter->drawImage(QPointF(vehicleIcon.width()/-2, vehicleIcon.height()/-2), vehicleIcon);
painter->drawImage(QPointF(vehicleIcon.width()/-2,
vehicleIcon.height()/-2),
vehicleIcon);
painter->restore();
}

// render current position spot
if (showCurrentPosition && currentPosition.valid){
double x;
double y;
projection.GeoToPixel(currentPosition.coord, x, y);
if (boundingBox.contains(x, y)){
osmscout::Vertex2D screenPos;
projection.GeoToPixel(currentPosition.coord,
screenPos);
if (boundingBox.contains(screenPos.GetX(), screenPos.GetY())){

if (currentPosition.horizontalAccuracyValid){
double diameter = currentPosition.horizontalAccuracy * projection.GetMeterInPixel();
if (diameter > 25.0 && diameter < std::max(request.width, request.height)){
painter->setBrush(QBrush(QColor::fromRgbF(1.0, 1.0, 1.0, 0.4)));
painter->setPen(QColor::fromRgbF(1.0, 1.0, 1.0, 0.7));
painter->drawEllipse(x - (diameter /2.0), y - (diameter /2.0), diameter, diameter);
painter->drawEllipse(screenPos.GetX() - (diameter /2.0),
screenPos.GetY() - (diameter /2.0),
diameter,
diameter);
}
}

Expand All @@ -353,25 +359,30 @@ void MapWidget::paint(QPainter *painter)
}
painter->setPen(QColor::fromRgbF(0.0, 0.5, 0.0, 0.9));
double dimension = projection.ConvertWidthToPixel(2.8);
painter->drawEllipse(x - dimension/2, y - dimension/2, dimension, dimension);
painter->drawEllipse(screenPos.GetX() - dimension/2,
screenPos.GetY() - dimension/2,
dimension,
dimension);
}
}

// render marks
if (!marks.isEmpty()){
double x;
double y;
painter->setBrush(QBrush());
QPen pen;
pen.setColor(QColor::fromRgbF(0.8, 0.0, 0.0, 0.9));
pen.setWidth(6);
painter->setPen(pen);

for (auto &entry: marks){
projection.GeoToPixel(osmscout::GeoCoord(entry.GetLat(), entry.GetLon()), x, y);
if (boundingBox.contains(x, y)){
osmscout::Vertex2D screenPos;
projection.GeoToPixel(osmscout::GeoCoord(entry.GetLat(), entry.GetLon()),
screenPos);
if (boundingBox.contains(screenPos.GetX(), screenPos.GetY())){
double dimension = projection.ConvertWidthToPixel(6);
painter->drawEllipse(x - dimension/2, y - dimension/2, dimension, dimension);
painter->drawEllipse(screenPos.GetX() - dimension/2,
screenPos.GetY() - dimension/2,
dimension, dimension);
}
}
}
Expand Down Expand Up @@ -410,10 +421,10 @@ bool MapWidget::isInDatabaseBoundingBox(double lat, double lon)

QPointF MapWidget::screenPosition(double lat, double lon)
{
double x;
double y;
getProjection().GeoToPixel(osmscout::GeoCoord(lat, lon), x, y);
return QPointF(x, y);
osmscout::Vertex2D screenPos;
getProjection().GeoToPixel(osmscout::GeoCoord(lat, lon),
screenPos);
return QPointF(screenPos.GetX(), screenPos.GetY());
}

void MapWidget::zoom(double zoomFactor)
Expand Down
24 changes: 12 additions & 12 deletions libosmscout-client-qt/src/osmscoutclientqt/PlaneMapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ bool PlaneMapRenderer::RenderMap(QPainter& painter,
finalImgProjection.GetWidth(),
finalImgProjection.GetHeight());

double targetCenterX;
double targetCenterY;
osmscout::Vertex2D targetCenter;

osmscout::GeoCoord srcImageCenterCoord;
finalImgProjection.PixelToGeo(finalImgProjection.GetWidth()/2,
finalImgProjection.GetHeight()/2,
srcImageCenterCoord);

requestProjection.GeoToPixel(srcImageCenterCoord,targetCenterX,targetCenterY);
double targetTopLeftX=targetCenterX - finalImgProjection.GetWidth()*scale*0.5;
double targetTopLeftY=targetCenterY - finalImgProjection.GetHeight()*scale*0.5;
requestProjection.GeoToPixel(srcImageCenterCoord,
targetCenter);
double targetTopLeftX=targetCenter.GetX() - finalImgProjection.GetWidth()*scale*0.5;
double targetTopLeftY=targetCenter.GetY() - finalImgProjection.GetHeight()*scale*0.5;

QRectF targetRectangle(targetTopLeftX,
targetTopLeftY,
Expand Down Expand Up @@ -281,14 +281,14 @@ double PlaneMapRenderer::computeScale(const osmscout::MercatorProjection &previo
currentProjection.PixelToGeo(currentProjection.GetWidth(),currentProjection.GetHeight(),
bottomRight);

double x1;
double y1;
previousProjection.GeoToPixel(topLeft,x1,y1);
double x2;
double y2;
previousProjection.GeoToPixel(bottomRight,x2,y2);
osmscout::Vertex2D pos1;
osmscout::Vertex2D pos2;
previousProjection.GeoToPixel(topLeft,
pos1);
previousProjection.GeoToPixel(bottomRight,
pos2);

double previousDiagonal=sqrt(pow(x2-x1,2) + pow(y2-y1,2));
double previousDiagonal=sqrt(pow(pos2.GetX()-pos1.GetX(),2) + pow(pos2.GetY()-pos1.GetY(),2));
return currentDiagonal / previousDiagonal;
}

Expand Down
Loading

0 comments on commit 95b09be

Please sign in to comment.