Skip to content

Commit

Permalink
Switch a few places to using Tessellation instead of constructing an …
Browse files Browse the repository at this point in the history
…invalid Polygon only to render it. #951
  • Loading branch information
dabreegster committed Aug 17, 2022
1 parent 523eb8a commit a2e78f7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 31 deletions.
1 change: 0 additions & 1 deletion abstutil/src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cmp::Ord;
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;

use anyhow::Result;
use serde::de::DeserializeOwned;
Expand Down
19 changes: 3 additions & 16 deletions geom/src/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::TryFrom;
use std::fmt;

use anyhow::Result;
Expand Down Expand Up @@ -35,7 +34,7 @@ impl Polygon {
vertices.push(pt.x());
vertices.push(pt.y());
}
let indices = downsize(earcutr::earcut(&vertices, &Vec::new(), 2));
let indices = crate::tessellation::downsize(earcutr::earcut(&vertices, &Vec::new(), 2));

Polygon {
points: orig_pts,
Expand All @@ -56,7 +55,7 @@ impl Polygon {
})
.collect();
let (vertices, holes, dims) = earcutr::flatten(&geojson_style);
let indices = downsize(earcutr::earcut(&vertices, &holes, dims));
let indices = crate::tessellation::downsize(earcutr::earcut(&vertices, &holes, dims));

Polygon {
points: vertices
Expand Down Expand Up @@ -90,7 +89,7 @@ impl Polygon {
assert!(indices.len() % 3 == 0);
Polygon {
points,
indices: downsize(indices),
indices: crate::tessellation::downsize(indices),
rings: None,
}
}
Expand Down Expand Up @@ -671,15 +670,3 @@ fn from_multi(multi: geo::MultiPolygon) -> Vec<Polygon> {
})
.collect()
}

fn downsize(input: Vec<usize>) -> Vec<u16> {
let mut output = Vec::new();
for x in input {
if let Ok(x) = u16::try_from(x) {
output.push(x);
} else {
panic!("{} can't fit in u16, some polygon is too huge", x);
}
}
output
}
2 changes: 0 additions & 2 deletions geom/src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryFrom;

use serde::{Deserialize, Serialize};

use crate::{Distance, Duration};
Expand Down
39 changes: 36 additions & 3 deletions geom/src/tessellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,34 @@ pub struct Triangle {

impl From<Polygon> for Tessellation {
fn from(polygon: Polygon) -> Self {
Self::new(polygon.points, polygon.indices)
Self {
points: polygon.points,
indices: polygon.indices,
}
}
}

impl Tessellation {
pub fn new(points: Vec<Pt2D>, indices: Vec<u16>) -> Self {
Tessellation { points, indices }
pub fn new(points: Vec<Pt2D>, indices: Vec<usize>) -> Self {
Tessellation {
points,
indices: downsize(indices),
}
}

/// The `points` are not necessarily a `Ring`, which has strict requirements about no duplicate
/// points. We can render various types of invalid polygon.
pub fn from_ring(points: Vec<Pt2D>) -> Self {
assert!(points.len() >= 3);

let mut vertices = Vec::new();
for pt in &points {
vertices.push(pt.x());
vertices.push(pt.y());
}
let indices = downsize(earcutr::earcut(&vertices, &Vec::new(), 2));

Self { points, indices }
}

/// Returns (points, indices) for rendering
Expand Down Expand Up @@ -115,3 +136,15 @@ impl Tessellation {
}
}
}

pub fn downsize(input: Vec<usize>) -> Vec<u16> {
let mut output = Vec::new();
for x in input {
if let Ok(x) = u16::try_from(x) {
output.push(x);
} else {
panic!("{} can't fit in u16, some polygon is too huge", x);
}
}
output
}
1 change: 0 additions & 1 deletion map_model/src/objects/intersection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryFrom;
use std::fmt;

use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions widgetry/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lyon::tessellation;
use lyon::tessellation::geometry_builder::{simple_builder, VertexBuffers};

use abstutil::VecMap;
use geom::{Bounds, Polygon, Pt2D};
use geom::{Bounds, Pt2D, Tessellation};

use crate::{Color, Fill, GeomBatch, LinearGradient, Prerender};

Expand Down Expand Up @@ -99,7 +99,7 @@ pub(crate) fn add_svg_inner(
for (color, mesh) in mesh_per_color.consume() {
batch.push(
color,
Polygon::precomputed(
Tessellation::new(
mesh.vertices
.into_iter()
.map(|v| Pt2D::new(f64::from(v.x), f64::from(v.y)))
Expand Down
6 changes: 3 additions & 3 deletions widgetry/src/widgets/fan_chart.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::VecDeque;

use geom::{
Angle, Distance, Duration, HgramValue, Histogram, PolyLine, Polygon, Pt2D, Statistic, Time,
UnitFmt,
Angle, Distance, Duration, HgramValue, Histogram, PolyLine, Pt2D, Statistic, Tessellation,
Time, UnitFmt,
};

use crate::widgets::plots::{make_legend, thick_lineseries, Axis, PlotOptions};
Expand Down Expand Up @@ -133,7 +133,7 @@ impl FanChart {
p99.reverse();
band.extend(transform(p99));
band.push(band[0]);
batch.push(s.color.alpha(0.5), Polygon::buggy_new(band));
batch.push(s.color.alpha(0.5), Tessellation::from_ring(band));

batch.push(
s.color,
Expand Down
6 changes: 3 additions & 3 deletions widgetry/src/widgets/plots.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use abstutil::prettyprint_usize;
use geom::{Circle, Distance, Duration, Percent, Polygon, Pt2D, Time, UnitFmt};
use geom::{Circle, Distance, Duration, Percent, Pt2D, Tessellation, Time, UnitFmt};

use crate::{Color, EventCtx, GeomBatch, ScreenDims, TextExt, Toggle, Widget};

Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn make_legend<X: Axis<X>, Y: Axis<Y>>(
}

// TODO If this proves useful, lift to geom
pub fn thick_lineseries(pts: Vec<Pt2D>, width: Distance) -> Polygon {
pub fn thick_lineseries(pts: Vec<Pt2D>, width: Distance) -> Tessellation {
use lyon::math::{point, Point};
use lyon::path::Path;
use lyon::tessellation::geometry_builder::{BuffersBuilder, Positions, VertexBuffers};
Expand All @@ -215,7 +215,7 @@ pub fn thick_lineseries(pts: Vec<Pt2D>, width: Distance) -> Polygon {
&mut buffer,
)
.unwrap();
Polygon::precomputed(
Tessellation::new(
geom.vertices
.into_iter()
.map(|v| Pt2D::new(f64::from(v.x), f64::from(v.y)))
Expand Down

0 comments on commit a2e78f7

Please sign in to comment.