Skip to content

Commit 00512e2

Browse files
authored
AB#61706: Add bounding box margins for rendered label positions (#127)
1 parent 7b4e871 commit 00512e2

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/components/labelPlacement/optimizePositions.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getAlphaOverflowCost,
1414
shouldBeVisible,
1515
} from './costFunctions';
16+
import { head } from 'lodash';
1617

1718
const timeout = 7 * 24 * 60 * 60 * 1000;
1819
const iterationsPerFactor = 8;
@@ -21,11 +22,11 @@ const angles = [-6, -3, -1, 0, 1, 3, 6];
2122
const distances = [-4, -2, -1, 0, 1, 2, 4];
2223
const factors = [30, 15, 7, 3];
2324

24-
const diffsArray = factors.map(factor =>
25+
const diffsArray = factors.map((factor) =>
2526
angles.reduce(
2627
(prev, angle) => [
2728
...prev,
28-
...distances.map(distance => ({ angle: angle * factor, distance: distance * factor })),
29+
...distances.map((distance) => ({ angle: angle * factor, distance: distance * factor })),
2930
],
3031
[],
3132
),
@@ -43,9 +44,9 @@ function getCloseByPositions(positions, index, maxDistance) {
4344
...pos,
4445
index: i,
4546
}))
46-
.filter(pos => !pos.allowCollision)
47-
.filter(pos => pos.shouldBeVisible || !pos.allowHidden)
48-
.filter(pos => {
47+
.filter((pos) => !pos.allowCollision)
48+
.filter((pos) => pos.shouldBeVisible || !pos.allowHidden)
49+
.filter((pos) => {
4950
if (!pos.allowHidden) return true;
5051
if (getDistance(pos, positions[index]) < maxDistance * 3) {
5152
return true;
@@ -88,7 +89,7 @@ function getPlacements(placement, index, diffs, bbox, alphaByteArray, configurat
8889
const { positions, indexes } = placement;
8990

9091
return diffs
91-
.map(diff => {
92+
.map((diff) => {
9293
const updatedPosition = updatePosition(positions[index], diff);
9394

9495
if (updatedPosition) {
@@ -108,8 +109,8 @@ function getPlacements(placement, index, diffs, bbox, alphaByteArray, configurat
108109
}
109110
return positions.map((position, i) => (i === index ? updatedPosition : position));
110111
})
111-
.filter(updatedPositions => !!updatedPositions)
112-
.map(updatedPositions => ({ positions: updatedPositions, indexes: [...indexes, index] }));
112+
.filter((updatedPositions) => !!updatedPositions)
113+
.map((updatedPositions) => ({ positions: updatedPositions, indexes: [...indexes, index] }));
113114
}
114115

115116
function comparePlacements(placement, other, bbox, alphaByteArray, closeByPositions) {
@@ -147,11 +148,9 @@ function getNextPlacement(initialPlacement, index, diffs, bbox, alphaByteArray,
147148
];
148149
}, []);
149150

150-
const nextPlacement = [
151-
initialPlacement,
152-
...placements,
153-
...placementsOverlapping,
154-
].reduce((prev, cur) => comparePlacements(prev, cur, bbox, alphaByteArray, closeByPositions));
151+
const nextPlacement = [initialPlacement, ...placements, ...placementsOverlapping].reduce(
152+
(prev, cur) => comparePlacements(prev, cur, bbox, alphaByteArray, closeByPositions),
153+
);
155154

156155
return nextPlacement;
157156
}
@@ -186,18 +185,29 @@ function findMostSuitablePosition(initialPlacement, bbox, isOccupied, configurat
186185

187186
function optimizePositions(initialPositions, bbox, alphaByteArray, mapOptions, configuration) {
188187
const placement = {
189-
positions: initialPositions.map(position => updatePosition(position)),
188+
positions: initialPositions.map((position) => updatePosition(position)),
190189
indexes: [],
191190
};
192191

192+
const dpi = 300;
193+
const marginMm = 6;
194+
const bboxPrintMargin = Math.round((marginMm * dpi) / 25.4);
195+
196+
const bboxWithMargins = {
197+
left: bbox.left + bboxPrintMargin,
198+
top: bbox.top + bboxPrintMargin,
199+
width: bbox.width - bboxPrintMargin * 2,
200+
height: bbox.height - bboxPrintMargin * 2,
201+
};
202+
193203
const isOccupied = getIfOccupied(alphaByteArray, mapOptions);
194-
const positions = findMostSuitablePosition(placement, bbox, isOccupied, configuration);
204+
const positions = findMostSuitablePosition(placement, bboxWithMargins, isOccupied, configuration);
195205

196206
const newPlacements = [];
197-
positions.forEach(position => {
207+
positions.forEach((position) => {
198208
newPlacements.push({
199209
...position,
200-
visible: shouldBeVisible(position, isOccupied, bbox, configuration, true),
210+
visible: shouldBeVisible(position, isOccupied, bboxWithMargins, configuration, true),
201211
});
202212
});
203213
return newPlacements;

0 commit comments

Comments
 (0)