Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions resources/qss/mapmap.qss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ QPushButton::checked {
background-color: #272a36;
}

/* After the hover: a button that does nothing does not react to the mouse either. */
QPushButton:disabled {
color: #4C4F5B;
background-color: #323541;
}

QToolTip {
padding: 1px 2px;
border-radius: 2px;
Expand Down
336 changes: 336 additions & 0 deletions src/automap/ContourSimplifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
/*
* ContourSimplifier.cpp
*
* (c) 2026 MapMap contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "ContourSimplifier.h"

#include <QLineF>
#include <QtMath>

#include <algorithm>
#include <cmath>

namespace mmp {

namespace {

const int CORNERS = 4;

// The directions along the edges of the cells, turning right one after the other on the screen.
enum Direction { EAST = 0, SOUTH = 1, WEST = 2, NORTH = 3, DIRECTIONS = 4 };
const int STEP_X[DIRECTIONS] = { 1, 0, -1, 0 };
const int STEP_Y[DIRECTIONS] = { 0, 1, 0, -1 };

// Seen from a corner of the cells, going in a direction: the cell ahead on the left, then on the right.
const int LEFT_X[DIRECTIONS] = { 0, 0, -1, -1 };
const int LEFT_Y[DIRECTIONS] = { -1, 0, 0, -1 };
const int RIGHT_X[DIRECTIONS] = { 0, -1, -1, 0 };
const int RIGHT_Y[DIRECTIONS] = { 0, 0, -1, -1 };

// The part of each side, at each end, left out when fitting its line: there, the outline turns.
const qreal SIDE_END_SHARE = 0.15;
// The step of the points taken along a side to fit its line, in pixels.
const qreal SIDE_SAMPLE_STEP = 0.5;
// A corner where the lines cross farther than this from the corner of the outline is not moved:
// at least this many pixels, or this share of its shortest side.
const qreal MINIMUM_CORNER_SHIFT = 4;
const qreal CORNER_SHIFT_SHARE = 0.25;
const qreal PARALLEL = 1e-9;

qreal distanceToSegment(const QPointF& p, const QPointF& a, const QPointF& b)
{
const QPointF ab = b - a;
const qreal length2 = ab.x() * ab.x() + ab.y() * ab.y();
if (length2 <= 0)
return std::hypot(p.x() - a.x(), p.y() - a.y());
const qreal t = qBound(0.0, ((p.x() - a.x()) * ab.x() + (p.y() - a.y()) * ab.y()) / length2, 1.0);
const QPointF closest = a + t * ab;
return std::hypot(p.x() - closest.x(), p.y() - closest.y());
}

/// A straight line: a point on it and its direction, of length 1.
struct Line {
QPointF point;
QPointF direction;
};

/// Crosses @a a and @a b; false when they are parallel.
bool cross(const Line& a, const Line& b, QPointF* point)
{
const qreal det = a.direction.x() * b.direction.y() - a.direction.y() * b.direction.x();
if (std::fabs(det) < PARALLEL)
return false;
const QPointF d = b.point - a.point;
const qreal t = (d.x() * b.direction.y() - d.y() * b.direction.x()) / det;
*point = a.point + t * a.direction;
return true;
}

/// Fits a line on the points of @a outline from index @a from to index @a to, going forwards; false when too short.
bool fitSide(const QPolygonF& outline, int from, int to, Line* line)
{
const int n = outline.size();
QPolygonF side;
for (int i = from;; i = (i + 1) % n)
{
side.append(outline.at(i));
if (i == to)
break;
}
qreal length = 0;
for (int i = 1; i < side.size(); i++)
length += QLineF(side.at(i - 1), side.at(i)).length();
if (length <= 0)
return false;

// Points every half pixel along the side, away from its ends.
QVector<QPointF> samples;
qreal travelled = 0;
for (int i = 1; i < side.size(); i++)
{
const QLineF segment(side.at(i - 1), side.at(i));
const qreal segmentLength = segment.length();
for (qreal s = 0; s < segmentLength; s += SIDE_SAMPLE_STEP)
{
const qreal along = travelled + s;
if (along >= SIDE_END_SHARE * length && along <= (1 - SIDE_END_SHARE) * length)
samples.append(segment.pointAt(s / segmentLength));
}
travelled += segmentLength;
}
if (samples.size() < 2)
return false;

// The line through their middle, along their main direction.
QPointF mean;
for (const QPointF& p : samples)
mean += p;
mean /= samples.size();
qreal xx = 0, xy = 0, yy = 0;
for (const QPointF& p : samples)
{
const QPointF d = p - mean;
xx += d.x() * d.x();
xy += d.x() * d.y();
yy += d.y() * d.y();
}
const qreal angle = 0.5 * std::atan2(2 * xy, xx - yy);
line->point = mean;
line->direction = QPointF(std::cos(angle), std::sin(angle));
return true;
}

}

QPolygonF ContourSimplifier::outline(const PlaneRegion& region)
{
QPolygonF result;
const int width = region.gridSize.width();
int start = -1;
for (int i = 0; i < region.cells.size() && start < 0; i++)
if (region.cells.at(i))
start = i;
if (start < 0)
return result;

// Along the edges of the cells, with the face on the right, from the top left corner of its first cell.
const int startX = start % width;
const int startY = start / width;
const auto goes = [&region](int x, int y, int direction) {
return !region.contains(x + LEFT_X[direction], y + LEFT_Y[direction]) &&
region.contains(x + RIGHT_X[direction], y + RIGHT_Y[direction]);
};
int x = startX;
int y = startY;
int direction = EAST;
result.append(QPointF(x, y) * region.cellSize);
const int maximumSteps = 4 * (region.cells.size() + 1);
for (int step = 0; step < maximumSteps; step++)
{
x += STEP_X[direction];
y += STEP_Y[direction];
// Turn right when possible, else go straight, else turn left.
int next = direction;
const int choices[3] = { (direction + 1) % DIRECTIONS, direction, (direction + DIRECTIONS - 1) % DIRECTIONS };
for (int choice : choices)
{
if (goes(x, y, choice))
{
next = choice;
break;
}
}
if (x == startX && y == startY && next == EAST)
break;
if (next != direction)
result.append(QPointF(x, y) * region.cellSize);
direction = next;
}
return result;
}

QPolygonF ContourSimplifier::quad(const QPolygonF& outline)
{
const int n = outline.size();
if (n < CORNERS)
return QPolygonF();

// The two points farthest apart (nearly): the farthest from the middle, then the farthest from it.
QPointF middle;
for (const QPointF& p : outline)
middle += p;
middle /= n;
int first = 0;
for (int i = 1; i < n; i++)
if (QLineF(middle, outline.at(i)).length() > QLineF(middle, outline.at(first)).length())
first = i;
int second = first;
for (int i = 0; i < n; i++)
if (QLineF(outline.at(first), outline.at(i)).length() > QLineF(outline.at(first), outline.at(second)).length())
second = i;
if (second == first)
return QPolygonF();

// Then each time the point farthest from the outline kept so far.
QVector<int> kept = { qMin(first, second), qMax(first, second) };
while (kept.size() < CORNERS)
{
qreal farthest = -1;
int farthestIndex = -1;
int insertAt = -1;
for (int k = 0; k < kept.size(); k++)
{
const int from = kept.at(k);
const int to = kept.at((k + 1) % kept.size());
for (int i = (from + 1) % n; i != to; i = (i + 1) % n)
{
const qreal distance = distanceToSegment(outline.at(i), outline.at(from), outline.at(to));
if (distance > farthest)
{
farthest = distance;
farthestIndex = i;
insertAt = k + 1;
}
}
}
if (farthestIndex < 0)
return QPolygonF();
kept.insert(insertAt, farthestIndex);
}
// Kept in the order of the outline, starting anywhere.
std::sort(kept.begin(), kept.end());

QPolygonF corners;
for (int index : kept)
corners.append(outline.at(index));

// Each corner where the lines of its two sides cross, when they do near it.
Line lines[CORNERS];
bool fitted[CORNERS];
for (int k = 0; k < CORNERS; k++)
fitted[k] = fitSide(outline, kept.at(k), kept.at((k + 1) % CORNERS), &lines[k]);
qreal shortestSide = QLineF(corners.at(0), corners.at(1)).length();
for (int k = 1; k < CORNERS; k++)
shortestSide = qMin(shortestSide, QLineF(corners.at(k), corners.at((k + 1) % CORNERS)).length());
const qreal maximumShift = qMax(MINIMUM_CORNER_SHIFT, CORNER_SHIFT_SHARE * shortestSide);
QPolygonF refined = corners;
for (int k = 0; k < CORNERS; k++)
{
const int before = (k + CORNERS - 1) % CORNERS;
QPointF crossing;
if (fitted[before] && fitted[k] && cross(lines[before], lines[k], &crossing) &&
QLineF(crossing, corners.at(k)).length() <= maximumShift)
refined[k] = crossing;
}

// Clockwise on the screen, from the corner nearest to the top left.
if (signedArea(refined) < 0)
std::reverse(refined.begin(), refined.end());
int topLeft = 0;
for (int k = 1; k < CORNERS; k++)
if (refined.at(k).x() + refined.at(k).y() < refined.at(topLeft).x() + refined.at(topLeft).y())
topLeft = k;
QPolygonF result;
for (int k = 0; k < CORNERS; k++)
result.append(refined.at((topLeft + k) % CORNERS));
return result;
}

bool ContourSimplifier::isConvex(const QPolygonF& quad)
{
if (quad.size() != CORNERS)
return false;
int positive = 0;
int negative = 0;
for (int k = 0; k < CORNERS; k++)
{
const QPointF a = quad.at(k);
const QPointF b = quad.at((k + 1) % CORNERS);
const QPointF c = quad.at((k + 2) % CORNERS);
const qreal turn = (b.x() - a.x()) * (c.y() - b.y()) - (b.y() - a.y()) * (c.x() - b.x());
if (turn > 0)
positive++;
else if (turn < 0)
negative++;
}
return positive == CORNERS || negative == CORNERS;
}

qreal ContourSimplifier::signedArea(const QPolygonF& polygon)
{
qreal sum = 0;
for (int i = 0; i < polygon.size(); i++)
{
const QPointF& a = polygon.at(i);
const QPointF& b = polygon.at((i + 1) % polygon.size());
sum += a.x() * b.y() - b.x() * a.y();
}
return sum / 2;
}

QPolygonF ContourSimplifier::inset(const QPolygonF& quad, qreal distance)
{
if (quad.size() != CORNERS || distance == 0)
return quad;
// Inwards is on the right of each side when the quadrilateral goes clockwise on the screen.
const qreal side = signedArea(quad) >= 0 ? 1 : -1;
Line lines[CORNERS];
for (int k = 0; k < CORNERS; k++)
{
const QPointF a = quad.at(k);
const QPointF b = quad.at((k + 1) % CORNERS);
const qreal length = QLineF(a, b).length();
if (length <= 0)
return quad;
const QPointF direction = (b - a) / length;
const QPointF inwards(-direction.y() * side, direction.x() * side);
lines[k].point = a + inwards * distance;
lines[k].direction = direction;
}
QPolygonF result;
for (int k = 0; k < CORNERS; k++)
{
QPointF corner;
if (!cross(lines[(k + CORNERS - 1) % CORNERS], lines[k], &corner))
return quad;
result.append(corner);
}
return result;
}

}
Loading
Loading