From f2cbeb465eff1b716b774aeff545797ce43fe98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Tue, 8 Nov 2016 18:32:10 +0100 Subject: [PATCH] :triangular_ruler: adding hole support to polygon triangulation --- src/Canvas.coffee | 17 ++++++----------- src/Renderer.coffee | 16 +++++++++++----- src/Tile.coffee | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Canvas.coffee b/src/Canvas.coffee index 780b088..f74c1cc 100644 --- a/src/Canvas.coffee +++ b/src/Canvas.coffee @@ -44,20 +44,15 @@ module.exports = class Canvas background: (x, y, color) -> @buffer.setBackground x, y, color - polygon: (polylines, color) -> + polygon: (rings, color) -> vertices = [] holes = [] - xs = {} - ys = {} - # - # for points in polylines - # if vertices.length - # continue - # holes.push vertices.length/2 - polylines.push polylines[0] - for point in polylines - vertices = vertices.concat point + for ring in rings + if vertices.length + holes.push vertices.length/2 + for point in ring + vertices = vertices.concat point try triangles = earcut vertices, holes diff --git a/src/Renderer.coffee b/src/Renderer.coffee index aa9a95b..4ccd148 100644 --- a/src/Renderer.coffee +++ b/src/Renderer.coffee @@ -14,6 +14,8 @@ Styler = require './Styler' Tile = require './Tile' utils = require './utils' +simplify = require 'simplify-js' + module.exports = class Renderer cache: {} config: @@ -210,16 +212,17 @@ module.exports = class Renderer if feature.style.minzoom and tile.zoom < feature.style.minzoom return false - points = @_scaleAndReduce tile, feature, feature.points switch feature.style.type when "line" width = feature.style.paint['line-width'] width = width.stops[0][1] if width instanceof Object + points = @_scaleAndReduce tile, feature, feature.points @canvas.polyline points, feature.color, width if points.length when "fill" + points = (@_scaleAndReduce tile, feature, p, false for p in feature.points) @canvas.polygon points, feature.color # if points.length is 3 # @canvas._filledTriangle points[0], points[1], points[2], feature.color @@ -281,7 +284,13 @@ module.exports = class Renderer outside = null scaled.push [lastX, lastY] - scaled.push [x, y] + scaled.push [x, y] #x: x, y: y + + if scaled.length < 2 + if feature.style.type isnt "symbol" + return [] + # else + # scaled = ([point.x, point.y] for point in simplify scaled, 2, false) if filter if scaled.length is 2 @@ -291,7 +300,4 @@ module.exports = class Renderer @_seen[ka] = @_seen[kb] = true - if scaled.length < 2 and feature.style.type isnt "symbol" - return [] - scaled diff --git a/src/Tile.coffee b/src/Tile.coffee index b02d558..2b7b2d9 100644 --- a/src/Tile.coffee +++ b/src/Tile.coffee @@ -74,15 +74,25 @@ class Tile # use feature.loadGeometry() again as soon as we got a 512 extent tileset geometries = feature.loadGeometry() #@_reduceGeometry feature, 8 - for points in (if style.type is "fill" then [geometries[0]] else geometries) - nodes.push @_addBoundaries + if style.type is "fill" + nodes.push @_addBoundaries true, id: feature.id layer: name style: style properties: feature.properties - points: points + points: geometries color: colorCode + else + for points in geometries + nodes.push @_addBoundaries false, + id: feature.id + layer: name + style: style + properties: feature.properties + points: points + color: colorCode + tree = rbush 18 tree.load nodes @@ -91,13 +101,13 @@ class Tile @layers = layers - _addBoundaries: (data) -> + _addBoundaries: (deep, data) -> minX = Infinity maxX = -Infinity minY = Infinity maxY = -Infinity - for p in data.points + for p in (if deep then data.points[0] else data.points) minX = p.x if p.x < minX maxX = p.x if p.x > maxX minY = p.y if p.y < minY