Avoid using SVG code. Semantics of SVGs and vector tiles are different

This commit is contained in:
Maximilian Ammann 2022-01-23 22:05:12 +01:00
parent ddc11c20e5
commit e6f3b180f4

View File

@ -1,6 +1,7 @@
use std::ops::Add; use std::ops::Add;
use bytemuck::Pod; use bytemuck::Pod;
use lyon::geom::{point, vector};
use lyon::lyon_tessellation::VertexBuffers; use lyon::lyon_tessellation::VertexBuffers;
use lyon::tessellation::geometry_builder::MaxIndex; use lyon::tessellation::geometry_builder::MaxIndex;
@ -8,7 +9,7 @@ use lyon::tessellation::{
BuffersBuilder, FillOptions, FillTessellator, StrokeOptions, StrokeTessellator, BuffersBuilder, FillOptions, FillTessellator, StrokeOptions, StrokeTessellator,
}; };
use lyon_path::traits::SvgPathBuilder; use lyon_path::traits::SvgPathBuilder;
use lyon_path::Path; use lyon_path::{FillRule, Path};
use vector_tile::geometry::{Command, Geometry}; use vector_tile::geometry::{Command, Geometry};
use vector_tile::tile::Layer; use vector_tile::tile::Layer;
@ -25,51 +26,56 @@ impl<I: Add + From<lyon::lyon_tessellation::VertexId> + MaxIndex + Pod> Tesselat
for feature in self.features() { for feature in self.features() {
match feature.geometry() { match feature.geometry() {
Geometry::GeometryPolygon(polygon) => { Geometry::GeometryPolygon(polygon) => {
let mut tile_builder = Path::builder().with_svg(); let mut polygon_builder = Path::builder();
let mut cursor = point(0.0, 0.0);
for command in &polygon.commands { for command in &polygon.commands {
match command { match command {
Command::MoveTo(cmd) => { Command::MoveTo(cmd) => {
tile_builder.relative_move_to(lyon_path::math::vector( let delta = lyon_path::math::vector(cmd.x as f32, cmd.y as f32);
cmd.x as f32, cursor += delta;
cmd.y as f32, polygon_builder.begin(cursor);
));
} }
Command::LineTo(cmd) => { Command::LineTo(cmd) => {
tile_builder.relative_line_to(lyon_path::math::vector( let delta = lyon_path::math::vector(cmd.x as f32, cmd.y as f32);
cmd.x as f32, cursor += delta;
cmd.y as f32, polygon_builder.line_to(cursor);
));
} }
Command::Close => { Command::Close => {
tile_builder.close(); polygon_builder.close();
} }
}; };
} }
let mut tesselator = FillTessellator::new(); let mut fill_tesselator = FillTessellator::new();
tesselator fill_tesselator
.tessellate_path( .tessellate_path(
&tile_builder.build(), &polygon_builder.build(),
&FillOptions::tolerance(DEFAULT_TOLERANCE), &FillOptions::tolerance(DEFAULT_TOLERANCE)
.with_fill_rule(FillRule::NonZero),
&mut BuffersBuilder::new(&mut buffer, VertexConstructor {}), &mut BuffersBuilder::new(&mut buffer, VertexConstructor {}),
) )
.ok()?; .ok()?;
} }
Geometry::GeometryLineString(polygon) => { Geometry::GeometryLineString(line_string) => {
let mut tile_builder = Path::builder().with_svg(); let mut line_string_builder = Path::builder();
for command in &polygon.commands { let mut cursor = point(0.0, 0.0);
let mut subpath_open = false;
for command in &line_string.commands {
match command { match command {
Command::MoveTo(cmd) => { Command::MoveTo(cmd) => {
tile_builder.relative_move_to(lyon_path::math::vector( if subpath_open {
cmd.x as f32, line_string_builder.end(false);
cmd.y as f32, }
));
let delta = lyon_path::math::vector(cmd.x as f32, cmd.y as f32);
cursor += delta;
line_string_builder.begin(cursor);
subpath_open = true;
} }
Command::LineTo(cmd) => { Command::LineTo(cmd) => {
tile_builder.relative_line_to(lyon_path::math::vector( let delta = lyon_path::math::vector(cmd.x as f32, cmd.y as f32);
cmd.x as f32, cursor += delta;
cmd.y as f32, line_string_builder.line_to(cursor);
));
} }
Command::Close => { Command::Close => {
panic!("error") panic!("error")
@ -77,11 +83,15 @@ impl<I: Add + From<lyon::lyon_tessellation::VertexId> + MaxIndex + Pod> Tesselat
}; };
} }
let mut tesselator = StrokeTessellator::new(); if subpath_open {
line_string_builder.end(false);
}
tesselator let mut stroke_tesselator = StrokeTessellator::new();
stroke_tesselator
.tessellate_path( .tessellate_path(
&tile_builder.build(), &line_string_builder.build(),
&StrokeOptions::tolerance(DEFAULT_TOLERANCE), &StrokeOptions::tolerance(DEFAULT_TOLERANCE),
&mut BuffersBuilder::new(&mut buffer, VertexConstructor {}), &mut BuffersBuilder::new(&mut buffer, VertexConstructor {}),
) )