From fabecf7aa16318dfc4485ca04481d6d51f6f0480 Mon Sep 17 00:00:00 2001 From: Benjamin MICHEL Date: Tue, 13 Nov 2018 17:49:25 +0100 Subject: [PATCH 1/2] Prioritize centroid cells (JS) Ported from CPP version (https://github.com/mapycz/polylabel/commit/a51fe7acb5f13cda92dff01290faf79efcb38378#diff-edf00b5814128edde2ae52e5be0f4c45R154) --- polylabel.js | 58 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/polylabel.js b/polylabel.js index 5819a44..4319491 100644 --- a/polylabel.js +++ b/polylabel.js @@ -5,6 +5,25 @@ var Queue = require('tinyqueue'); module.exports = polylabel; module.exports.default = polylabel; +function getFitnessFunc(centroid, polygonSize) { + var maxSize = Math.max(polygonSize[0], polygonSize[1]); + + return (function (cellCenter, distancePolygon) { + if (distancePolygon <= 0) { + return distancePolygon; + } + + var d = [ + cellCenter[0] - centroid[0], + cellCenter[1] - centroid[1] + ]; + + var distanceCentroid = Math.sqrt((d[0] * d[0]) + (d[1] * d[1])); + + return distancePolygon * (1 - distanceCentroid / maxSize); + }); +} + function polylabel(polygon, precision, debug) { precision = precision || 1.0; @@ -23,24 +42,23 @@ function polylabel(polygon, precision, debug) { var cellSize = Math.min(width, height); var h = cellSize / 2; - if (cellSize === 0) return [minX, minY]; - // a priority queue of cells in order of their "potential" (max distance to polygon) var cellQueue = new Queue(null, compareMax); + if (cellSize === 0) return [minX, minY]; + + var centroid = getCentroid(polygon); + var fitnessFunc = getFitnessFunc(centroid, [width, height]); + // cover polygon with initial cells for (var x = minX; x < maxX; x += cellSize) { for (var y = minY; y < maxY; y += cellSize) { - cellQueue.push(new Cell(x + h, y + h, h, polygon)); + cellQueue.push(new Cell(x + h, y + h, h, polygon, fitnessFunc)); } } // take centroid as the first best guess - var bestCell = getCentroidCell(polygon); - - // special case for rectangular polygons - var bboxCell = new Cell(minX + width / 2, minY + height / 2, 0, polygon); - if (bboxCell.d > bestCell.d) bestCell = bboxCell; + var bestCell = new Cell(centroid[0], centroid[1], 0, polygon, fitnessFunc); var numProbes = cellQueue.length; @@ -49,20 +67,20 @@ function polylabel(polygon, precision, debug) { var cell = cellQueue.pop(); // update the best cell if we found a better one - if (cell.d > bestCell.d) { + if (cell.fitness > bestCell.fitness) { bestCell = cell; if (debug) console.log('found best %d after %d probes', Math.round(1e4 * cell.d) / 1e4, numProbes); } // do not drill down further if there's no chance of a better solution - if (cell.max - bestCell.d <= precision) continue; + if (cell.maxFitness - bestCell.fitness <= precision) continue; // split the cell into four cells h = cell.h / 2; - cellQueue.push(new Cell(cell.x - h, cell.y - h, h, polygon)); - cellQueue.push(new Cell(cell.x + h, cell.y - h, h, polygon)); - cellQueue.push(new Cell(cell.x - h, cell.y + h, h, polygon)); - cellQueue.push(new Cell(cell.x + h, cell.y + h, h, polygon)); + cellQueue.push(new Cell(cell.x - h, cell.y - h, h, polygon, fitnessFunc)); + cellQueue.push(new Cell(cell.x + h, cell.y - h, h, polygon, fitnessFunc)); + cellQueue.push(new Cell(cell.x - h, cell.y + h, h, polygon, fitnessFunc)); + cellQueue.push(new Cell(cell.x + h, cell.y + h, h, polygon, fitnessFunc)); numProbes += 4; } @@ -75,15 +93,16 @@ function polylabel(polygon, precision, debug) { } function compareMax(a, b) { - return b.max - a.max; + return b.maxFitness - a.maxFitness; } -function Cell(x, y, h, polygon) { +function Cell(x, y, h, polygon, fitnessFunc) { this.x = x; // cell center x this.y = y; // cell center y this.h = h; // half the cell size this.d = pointToPolygonDist(x, y, polygon); // distance from cell center to polygon - this.max = this.d + this.h * Math.SQRT2; // max distance to polygon within a cell + this.fitness = fitnessFunc([x, y], this.d); + this.maxFitness = fitnessFunc([x, y], this.d + this.h * Math.SQRT2); // max distance to polygon within a cell } // signed distance from point to polygon outline (negative if point is outside) @@ -109,7 +128,7 @@ function pointToPolygonDist(x, y, polygon) { } // get polygon centroid -function getCentroidCell(polygon) { +function getCentroid(polygon) { var area = 0; var x = 0; var y = 0; @@ -123,8 +142,7 @@ function getCentroidCell(polygon) { y += (a[1] + b[1]) * f; area += f * 3; } - if (area === 0) return new Cell(points[0][0], points[0][1], 0, polygon); - return new Cell(x / area, y / area, 0, polygon); + return (area === 0) ? points[0] : [x / area, y / area]; } // get squared distance from a point to a segment From 1c14b3988961bd54588fc7ea2b3ab9d2e5fcf8c8 Mon Sep 17 00:00:00 2001 From: Benjamin MICHEL Date: Tue, 13 Nov 2018 17:52:18 +0100 Subject: [PATCH 2/2] Backport of a fix --- polylabel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/polylabel.js b/polylabel.js index 4319491..1a3acf5 100644 --- a/polylabel.js +++ b/polylabel.js @@ -42,11 +42,11 @@ function polylabel(polygon, precision, debug) { var cellSize = Math.min(width, height); var h = cellSize / 2; + if (cellSize === 0) return [minX, minY]; + // a priority queue of cells in order of their "potential" (max distance to polygon) var cellQueue = new Queue(null, compareMax); - if (cellSize === 0) return [minX, minY]; - var centroid = getCentroid(polygon); var fitnessFunc = getFitnessFunc(centroid, [width, height]);