use std::f64::consts::PI;
use std::sync::Arc;
use geometry::Point;
use mln_style::PropertyEvaluationParameters;
use mln_style::color::Color;
use mln_style::constants::EXTENT;
use mln_style::expression::{Image, Value};
use mln_style::feature::{GeometryCollection, GeometryCoordinates, GeometryTileFeature, classify_rings};
use mln_style::layers::FillExtrusion;
use mln_style::layers::fill_extrusion_layer_properties::{FillExtrusionLayoutProperties, FillExtrusionPaintProperties};
use mln_style::property::evaluated::Evaluated;
use mln_style::subdivision_granularity::SubdivisionGranularitySetting;
use mln_style::tile_id::CanonicalTileID;
use mln_style::types::LightAnchorType;
use mln_vec_map::VecMap;
use crate::camera::View;
use crate::draw::mesh::Indexed;
use crate::draw::segment::{MAX_SEGMENT_VERTICES, SegmentBase};
use crate::geo::constants::{EARTH_RADIUS_M, M2PI};
use crate::layer::bucket::{Bucket, BucketBase, UploadNeeds};
use crate::layer::bucket_geometry::{GeometryUpload, GeometryVec};
use crate::layer::layer_type::EvaluatedLayer;
use crate::layer::paint_property_binder::{CrossFadedPaintPropertyBinder, PaintPropertyBinder};
use crate::layout::pattern_layout::{PatternLayerMap, PatternLayoutSpec};
use crate::render::globe::subdivision::{SubdividedPolygon, subdivide_polygon, subdivide_vertex_line};
use crate::render::static_data::RenderStaticData;
use crate::text::atlas::ImagePositions;
use crate::tile::geometry_tile_data::{is_boundary_edge, is_entirely_outside, limit_holes, round_polygon_corners};
use crate::util::tessellator::Tessellator;
crate::paint_property_binders!(
pub struct FillExtrusionBinders for FillExtrusionPaintProperties::PossiblyEvaluated {
fill_extrusion_base: PaintPropertyBinder<f32> => ["base"],
fill_extrusion_color: PaintPropertyBinder<Color> => ["color"],
fill_extrusion_height: PaintPropertyBinder<f32> => ["height"],
fill_extrusion_pattern: CrossFadedPaintPropertyBinder<Image> => ["pattern_from", "pattern_to"],
}
);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
pub(crate) struct FillExtrusionLayoutVertex {
pub(crate) a1: [i16; 2],
pub(crate) a2: [u16; 2],
}
pub(crate) fn polygon_centroid<R: AsRef<[Point<i16>]>>(polygon: &[R]) -> [i16; 2] {
let (mut x, mut y, mut count) = (0i64, 0i64, 0i64);
for ring in polygon {
let ring = ring.as_ref();
let closed = ring.len() > 1 && ring.first() == ring.last();
for p in &ring[..ring.len() - usize::from(closed)] {
x += i64::from(p.x);
y += i64::from(p.y);
count += 1;
}
}
let count = count.max(1);
[x.div_euclid(count) as i16, y.div_euclid(count) as i16]
}
pub(crate) type FillExtrusionStaticVertex = [i16; 2];
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
pub(crate) struct FillExtrusionWallVertex {
pub(crate) corner: FillExtrusionStaticVertex,
pub(crate) vertex: FillExtrusionLayoutVertex,
}
pub(crate) use crate::render::light::EvaluatedLight;
pub(crate) fn wall_quad_vertices(vertices: &[FillExtrusionLayoutVertex]) -> Vec<FillExtrusionWallVertex> {
let corners = RenderStaticData::fill_extrusion_vertices();
let quads: Vec<FillExtrusionWallVertex> = vertices
.iter()
.flat_map(|&vertex| corners.iter().map(move |&corner| FillExtrusionWallVertex { corner, vertex }))
.collect();
quads
}
pub(crate) fn wall_quad_paint(paint: &[u8], stride: usize, walls: usize) -> Vec<u8> {
let mut values = Vec::with_capacity(4 * stride * walls);
if stride > 0 {
for vertex in paint.chunks_exact(stride).take(walls) {
for _ in 0..4 {
values.extend_from_slice(vertex);
}
}
}
values
}
#[derive(Clone, mln_wire::Wire)]
pub(crate) struct FillExtrusionBucket {
pub(crate) base: BucketBase,
pub(crate) layout: FillExtrusionLayoutProperties::PossiblyEvaluated,
pub(crate) vertices: GeometryVec<FillExtrusionLayoutVertex>,
pub(crate) centroids: Vec<([i16; 2], u32)>,
pub(crate) triangles: GeometryVec<u16>,
pub(crate) triangle_segments: Vec<SegmentBase>,
pub(crate) subdivision_granularity: SubdivisionGranularitySetting,
pub(crate) paint_property_binders: VecMap<String, FillExtrusionBinders>,
}
static EMPTY_FORMATTED_SECTION: Value = Value::Null;
pub(crate) fn tile_units_per_meter(tile: &CanonicalTileID) -> f64 {
let tiles = 2f64.powi(i32::from(tile.z));
let latitude = (PI * (1.0 - 2.0 * (f64::from(tile.y) + 0.5) / tiles)).sinh().atan();
f64::from(EXTENT) * tiles / (latitude.cos() * M2PI * EARTH_RADIUS_M)
}
fn shared_wall_normals(
ring: &[Point<i16>],
has_wall: impl Fn(Point<i16>, Point<i16>) -> bool,
) -> Vec<Option<Point<f64>>> {
let n = ring.len();
let perps: Vec<Option<Point<f64>>> = ring
.windows(2)
.map(|edge| {
has_wall(edge[0], edge[1]).then(|| {
Point::new(-(f64::from(edge[1].y) - f64::from(edge[0].y)), f64::from(edge[1].x) - f64::from(edge[0].x))
})
})
.collect();
let closed = n > 2 && ring[0] == ring[n - 1];
(0..n)
.map(|i| {
let incoming = if i > 0 {
perps[i - 1]
} else if closed {
perps[n - 2]
} else {
None
};
let outgoing = if i + 1 < n {
perps[i]
} else if closed {
perps[0]
} else {
None
};
let (a, b) = (incoming?, outgoing?);
let (len_a, len_b) = (a.x.hypot(a.y), b.x.hypot(b.y));
if a.x * b.x + a.y * b.y < mln_style::math::deg2rad(35.0).cos() * len_a * len_b {
None
} else if len_a >= 2.0 * len_b {
Some(a)
} else if len_b >= 2.0 * len_a {
Some(b)
} else {
Some(Point::new(a.x + b.x, a.y + b.y))
}
})
.collect()
}
fn dist_u16(a: Point<f64>, b: Point<f64>) -> u16 {
let dx = b.x - a.x;
let dy = b.y - a.y;
((dx * dx + dy * dy).sqrt() as i64) as u16
}
impl FillExtrusionBucket {
#[must_use]
pub(crate) fn new(
layout: FillExtrusionLayoutProperties::PossiblyEvaluated,
layer_paint_properties: &VecMap<String, Arc<EvaluatedLayer<FillExtrusion>>>,
zoom: f32,
_overscaling: u32,
) -> Self {
let mut paint_property_binders = VecMap::new();
for (id, properties) in layer_paint_properties {
paint_property_binders
.entry(id.clone())
.or_insert_with(|| FillExtrusionBinders::new(&properties.paint, zoom));
}
FillExtrusionBucket {
base: BucketBase::default(),
layout,
vertices: GeometryVec::default(),
centroids: Vec::new(),
triangles: GeometryVec::default(),
triangle_segments: Vec::new(),
subdivision_granularity: SubdivisionGranularitySetting::none(),
paint_property_binders,
}
}
pub(crate) fn set_subdivision_granularity(&mut self, value: &SubdivisionGranularitySetting) {
self.subdivision_granularity = *value;
}
#[must_use]
pub(crate) fn layout_vertex(
p: Point<i16>,
edge_distance: u16,
is_discarded: bool,
shared_normal: Option<Point<f64>>,
) -> FillExtrusionLayoutVertex {
let normal = shared_normal.map_or(0, |n| {
let turn = n.y.atan2(n.x) / M2PI + 0.5;
(((turn * 16384.0).round() as u16 % 16384) << 2) | 2
});
FillExtrusionLayoutVertex {
a1: [p.x, p.y],
a2: [normal | u16::from(is_discarded), edge_distance],
}
}
fn add_outline<R: AsRef<[Point<i16>]>>(&mut self, polygon: &[R]) {
let rounded = *self.layout.fill_extrusion_rounded_corner_distance.uniform() > 0.0;
let vertices = self.vertices.get_mut();
for ring in polygon {
let ring = ring.as_ref();
let outside = is_entirely_outside(ring);
let normals = rounded.then(|| shared_wall_normals(ring, |a, b| a != b && !is_boundary_edge(a, b)));
let mut edge_distance: usize = 0;
for (i, &p) in ring.iter().enumerate() {
let next = ring.get(i + 1).copied();
let is_discarded = outside || next.is_none_or(|next| is_boundary_edge(p, next));
let p1 = Point::new(f64::from(p.x), f64::from(p.y));
let p2 = next.map(|p| Point::new(f64::from(p.x), f64::from(p.y)));
let shared_normal = normals.as_ref().and_then(|normals| normals[i]);
vertices.push(Self::layout_vertex(p, edge_distance as u16, is_discarded, shared_normal));
if let Some(p2) = p2.filter(|_| !is_discarded) {
let dist = usize::from(dist_u16(p1, p2));
if edge_distance + dist > usize::from(u16::MAX) {
edge_distance = 0;
}
edge_distance += dist;
}
}
}
}
fn add_polygon<R: AsRef<[Point<i16>]>>(
&mut self,
polygon: &[R],
roof: Option<&SubdividedPolygon>,
tessellator: &mut Tessellator,
) {
let first = self.vertices.len();
self.add_outline(polygon);
let vertices = self.vertices.get_mut();
let mut triangles = Indexed { indexes: self.triangles.get_mut(), segments: &mut self.triangle_segments };
let roof_vertex = |p: Point<i16>| Self::layout_vertex(p, 0, true, None);
if let Some(roof) = roof {
let roof_first = vertices.len();
vertices.extend(roof.vertices.iter().map(|&p| roof_vertex(p)));
triangles.add(vertices, roof_first, roof.triangles.iter().copied());
} else {
let roof_first = if vertices.len() - first <= MAX_SEGMENT_VERTICES {
first
} else {
let roof_first = vertices.len();
vertices.extend(polygon.iter().flat_map(|ring| ring.as_ref().iter().map(|&p| roof_vertex(p))));
roof_first
};
let roof = tessellator.triangulate(polygon).iter().map(|&[a, b, c]| [a, c, b]);
triangles.add(vertices, roof_first, roof);
}
}
#[must_use]
pub(crate) fn light_color(light: &EvaluatedLight) -> [f32; 3] {
let color = light.light_color.uniform();
[color.r, color.g, color.b]
}
pub(crate) fn light_position(light: &EvaluatedLight, state: &View) -> [f32; 3] {
let light_pos = glam::Vec3::from_array(light.light_position.uniform().get_cartesian());
if *light.light_anchor.uniform() == LightAnchorType::Viewport {
(glam::Mat3::from_rotation_z(-state.angle() as f32) * light_pos).to_array()
} else {
light_pos.to_array()
}
}
#[must_use]
pub(crate) fn light_intensity(light: &EvaluatedLight) -> f32 {
*light.light_intensity.uniform()
}
}
impl Bucket for FillExtrusionBucket {
fn base(&self) -> &BucketBase {
&self.base
}
fn add_feature(
&mut self,
feature: &dyn GeometryTileFeature,
geometry: &GeometryCollection,
pattern_positions: &ImagePositions,
pattern_dependencies: &PatternLayerMap,
index: usize,
canonical: &CanonicalTileID,
tessellator: &mut Tessellator,
) {
for mut polygon in classify_rings(geometry) {
limit_holes(&mut polygon, 500);
let centroid = polygon_centroid(&polygon);
let rounded_corner_distance = f64::from(*self.layout.fill_extrusion_rounded_corner_distance.uniform());
if rounded_corner_distance > 0.0 {
polygon = round_polygon_corners(&polygon, rounded_corner_distance * tile_units_per_meter(canonical));
}
if polygon.first().is_none_or(|outer| is_entirely_outside(outer)) {
continue;
}
let granularity = self.subdivision_granularity.fill.granularity_for_zoom_level(canonical.z);
let first = self.vertices.len();
if granularity >= 2 {
let subdivided_rings: Vec<GeometryCoordinates> =
polygon.iter().map(|ring| subdivide_vertex_line(ring, granularity, true)).collect();
let roof = subdivide_polygon(&polygon, canonical, granularity, false, tessellator);
self.add_polygon(&subdivided_rings, Some(&roof), tessellator);
} else {
self.add_polygon(&polygon[..], None, tessellator);
}
self.centroids.push((centroid, (self.vertices.len() - first) as u32));
}
let length = self.vertices.len();
for (id, binders) in &mut self.paint_property_binders {
binders.populate_vertex_vectors(
feature,
length,
index,
pattern_positions,
pattern_dependencies.get(id),
canonical,
&EMPTY_FORMATTED_SECTION,
);
}
}
fn upload(&mut self, geometry: &mut GeometryUpload<'_>, needs: UploadNeeds) {
let walls = self.vertices.len().saturating_sub(1);
if needs.wall_quads {
geometry.derive_vertices(&self.vertices, wall_quad_vertices);
}
geometry.move_vertices(&mut self.vertices);
geometry.move_indexes(&mut self.triangles);
for binders in self.paint_property_binders.values_mut() {
let depends_on_feature_state = binders.depends_on_feature_state();
let buffer = &mut binders.interleaved_vertex_buffer;
if needs.wall_quads && !depends_on_feature_state {
let stride = buffer.stride;
geometry.derive_vertices(&buffer.shared_vertex_vector, |paint| wall_quad_paint(paint, stride, walls));
}
geometry.move_paint(buffer, depends_on_feature_state);
}
self.base.uploaded = true;
}
fn has_data(&self) -> bool {
!self.triangle_segments.is_empty()
}
fn write_wire(&self, w: &mut mln_wire::Writer) {
mln_wire::Wire::write(self, w);
}
fn update(
&mut self,
states: &crate::layer::bucket::FeatureStates,
layer: &crate::tile::TileLayer<'_>,
layer_id: &str,
image_positions: &ImagePositions,
) -> bool {
let Some(binders) = self.paint_property_binders.get_mut(layer_id) else {
return false;
};
binders.update_vertex_vectors(states, layer, image_positions);
self.base.uploaded = false;
true
}
}
pub(crate) struct FillExtrusionLayoutSpec;
impl PatternLayoutSpec for FillExtrusionLayoutSpec {
type Type = FillExtrusion;
type Bucket = FillExtrusionBucket;
type Layout = FillExtrusionLayoutProperties::PossiblyEvaluated;
fn evaluate_layout(
layer: &EvaluatedLayer<FillExtrusion>,
parameters: &PropertyEvaluationParameters,
) -> Self::Layout {
layer.style.layout.evaluate(parameters)
}
fn pattern_property(layer: &EvaluatedLayer<FillExtrusion>) -> &Evaluated<Image> {
&layer.paint.fill_extrusion_pattern
}
fn sorted(_leader: &EvaluatedLayer<FillExtrusion>) -> bool {
false
}
fn sort_key(
_layout: &Self::Layout,
_feature: &dyn GeometryTileFeature,
_zoom: f32,
_canonical: &CanonicalTileID,
) -> f32 {
0.0
}
fn constant_sort_key(_layout: &Self::Layout) -> Option<f32> {
None
}
fn create_bucket(
layout: &Self::Layout,
layer_properties_map: &VecMap<String, Arc<EvaluatedLayer<FillExtrusion>>>,
zoom: f32,
overscaling: u32,
) -> FillExtrusionBucket {
FillExtrusionBucket::new(layout.clone(), layer_properties_map, zoom, overscaling)
}
fn set_subdivision_granularity(bucket: &mut FillExtrusionBucket, value: &SubdivisionGranularitySetting) {
bucket.set_subdivision_granularity(value);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn layout_vertex_packs_the_position_and_wall_data() {
let v = FillExtrusionBucket::layout_vertex(Point::new(10, 20), 7, true, None);
assert_eq!(v.a1, [10, 20]);
assert_eq!(v.a2, [1, 7]);
let v = FillExtrusionBucket::layout_vertex(Point::new(-2, 2), 9, false, Some(Point::new(0.0, 1.0)));
assert_eq!(v.a1, [-2, 2]);
assert_eq!(v.a2, [(12288 << 2) | 2, 9]);
let v = FillExtrusionBucket::layout_vertex(Point::new(0, 0), 0, false, Some(Point::new(-1.0, 0.0)));
assert_eq!(v.a2[0], 2);
}
#[test]
fn a_polygon_stands_on_the_mean_of_its_vertices() {
let ring = |points: &[(i16, i16)]| points.iter().map(|&(x, y)| Point::new(x, y)).collect::<Vec<_>>();
let square = ring(&[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]);
assert_eq!([5, 5], polygon_centroid(std::slice::from_ref(&square)));
let hole = ring(&[(1, 1), (2, 1), (2, 2), (1, 1)]);
assert_eq!([3, 3], polygon_centroid(&[square, hole]));
assert_eq!([-2, -1], polygon_centroid(&[ring(&[(-3, -1), (0, 0)])]));
}
fn normals(ring: &[(i16, i16)]) -> Vec<Option<(f64, f64)>> {
let ring: Vec<Point<i16>> = ring.iter().map(|&(x, y)| Point::new(x, y)).collect();
shared_wall_normals(&ring, |a, b| a != b && !is_boundary_edge(a, b))
.into_iter()
.map(|n| {
n.map(|n| {
let len = n.x.hypot(n.y);
((n.x / len * 1000.0).round() / 1000.0, (n.y / len * 1000.0).round() / 1000.0)
})
})
.collect()
}
#[test]
fn walls_share_normals_at_shallow_turns() {
let (x, y) = (100.0 * 20f64.to_radians().cos(), 100.0 * 20f64.to_radians().sin());
let ring = [(0, 0), (100, 0), (100 + x as i16, y as i16)];
let mid = (-10f64.to_radians().sin(), 10f64.to_radians().cos());
let half = normals(&ring);
assert_eq!(half[0], None);
let (nx, ny) = half[1].unwrap();
assert!((nx - mid.0).abs() < 0.01 && (ny - mid.1).abs() < 0.01, "{half:?}");
assert_eq!(half[2], None);
let long = normals(&[(0, 0), (100, 0), (109, 4)]);
assert_eq!(long[1], Some((0.0, 1.0)));
assert_eq!(normals(&[(0, 0), (100, 0), (100, 100)])[1], None);
}
#[test]
fn closed_rings_share_normals_across_their_first_vertex() {
let ring: Vec<(i16, i16)> = (0..=12)
.map(|k| {
let a = f64::from(k % 12) * 30f64.to_radians();
((100.0 * a.cos()).round() as i16, (100.0 * a.sin()).round() as i16)
})
.collect();
let shared = normals(&ring);
assert!(shared.iter().all(Option::is_some), "{shared:?}");
assert_eq!(shared[0], shared[12]);
let (nx, ny) = shared[0].unwrap();
assert!(nx.abs() > 0.99 && ny.abs() < 0.01, "{shared:?}");
}
#[test]
fn walls_by_clip_lines_keep_their_own_normals() {
let shared = normals(&[(-64, 100), (-64, 0), (-60, -100), (-56, -200)]);
assert_eq!(shared[1], None);
assert!(shared[2].is_some());
}
}