Skip to content

Commit

Permalink
Simplify symbols and allow renderer work with generic geometries (Max…
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximkaaa authored Jan 16, 2024
1 parent a79ad53 commit df3cecd
Show file tree
Hide file tree
Showing 16 changed files with 645 additions and 737 deletions.
7 changes: 7 additions & 0 deletions galileo-types/src/cartesian/impls/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ pub struct Polygon<P> {
}

impl<P> Polygon<P> {
pub fn new(outer_contour: ClosedContour<P>, inner_contours: Vec<ClosedContour<P>>) -> Self {
Self {
outer_contour,
inner_contours,
}
}

pub fn iter_contours(&self) -> impl Iterator<Item = &ClosedContour<P>> {
std::iter::once(&self.outer_contour).chain(self.inner_contours.iter())
}
Expand Down
49 changes: 18 additions & 31 deletions galileo/examples/feature_layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use galileo::layer::feature_layer::symbol::polygon::SimplePolygonSymbol;
use galileo::layer::feature_layer::symbol::Symbol;
use galileo::layer::feature_layer::FeatureLayer;
use galileo::primitives::Color;
use galileo::render::{PointPaint, PrimitiveId, RenderBundle, UnpackedBundle};
use galileo_types::cartesian::impls::point::{Point2d, Point3d};
use galileo_types::cartesian::impls::polygon::Polygon;
use galileo::render::render_bundle::RenderBundle;
use galileo::render::{PointPaint, PrimitiveId, UnpackedBundle};
use galileo_types::cartesian::traits::cartesian_point::CartesianPoint3d;
use galileo_types::geo::crs::Crs;
use galileo_types::geometry::Geom;
use galileo_types::geometry_type::CartesianSpace2d;
use num_traits::AsPrimitive;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -124,7 +124,7 @@ pub async fn run(builder: MapBuilder) {
struct CountrySymbol {}

impl CountrySymbol {
fn get_polygon_symbol(&self, feature: &Country) -> SimplePolygonSymbol<CartesianSpace2d> {
fn get_polygon_symbol(&self, feature: &Country) -> SimplePolygonSymbol {
let stroke_color = feature.color;
let fill_color = Color {
a: if feature.is_selected() { 255 } else { 150 },
Expand All @@ -137,27 +137,15 @@ impl CountrySymbol {
}
}

impl Symbol<Country, Geom<Point2d>> for CountrySymbol {
fn render(
impl Symbol<Country> for CountrySymbol {
fn render<N: AsPrimitive<f32>, P: CartesianPoint3d<Num = N>>(
&self,
feature: &Country,
geometry: &Geom<Point2d>,
bundle: &mut Box<dyn RenderBundle>,
geometry: &Geom<P>,
bundle: &mut RenderBundle,
) -> Vec<PrimitiveId> {
let mut ids = vec![];
let Geom::MultiPolygon(geometry) = geometry else {
return ids;
};

for polygon in geometry.parts() {
ids.append(
&mut self
.get_polygon_symbol(feature)
.render(&(), polygon, bundle),
)
}

ids
self.get_polygon_symbol(feature)
.render(&(), geometry, bundle)
}

fn update(
Expand All @@ -170,7 +158,7 @@ impl Symbol<Country, Geom<Point2d>> for CountrySymbol {
let mut next_index = 0;
for _ in feature.geometry.parts() {
let polygon_symbol = self.get_polygon_symbol(feature);
<SimplePolygonSymbol<CartesianSpace2d> as Symbol<(), Polygon<Point2d>>>::update(
<SimplePolygonSymbol as Symbol<()>>::update(
&polygon_symbol,
&(),
&render_ids[next_index..next_index + renders_by_feature],
Expand All @@ -184,12 +172,12 @@ impl Symbol<Country, Geom<Point2d>> for CountrySymbol {

struct CitySymbol {}

impl Symbol<City, Geom<Point2d>> for CitySymbol {
fn render(
impl Symbol<City> for CitySymbol {
fn render<N: AsPrimitive<f32>, P: CartesianPoint3d<Num = N>>(
&self,
feature: &City,
geometry: &Geom<Point2d>,
bundle: &mut Box<dyn RenderBundle>,
geometry: &Geom<P>,
bundle: &mut RenderBundle,
) -> Vec<PrimitiveId> {
let size = (feature.population / 1000.0).log2() / 2.0;
let color = match &feature.capital[..] {
Expand All @@ -204,19 +192,18 @@ impl Symbol<City, Geom<Point2d>> for CitySymbol {
let Geom::Point(point) = geometry else {
return ids;
};
let point = Point3d::new(point.x, point.y, 0.0);

if &feature.capital == "primary" {
ids.push(bundle.add_point(
&point,
point,
PointPaint {
color: Color::rgba(255, 255, 255, 255),
size: size + 4.0,
},
));
}

ids.push(bundle.add_point(&point, PointPaint { color, size }));
ids.push(bundle.add_point(point, PointPaint { color, size }));

ids
}
Expand Down
35 changes: 12 additions & 23 deletions galileo/examples/lambert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ use galileo::layer::feature_layer::symbol::polygon::SimplePolygonSymbol;
use galileo::layer::feature_layer::symbol::Symbol;
use galileo::layer::feature_layer::FeatureLayer;
use galileo::primitives::Color;
use galileo::render::{PrimitiveId, RenderBundle, UnpackedBundle};
use galileo::render::render_bundle::RenderBundle;
use galileo::render::{PrimitiveId, UnpackedBundle};
use galileo::view::MapView;
use galileo_types::cartesian::impls::point::Point2d;
use galileo_types::cartesian::impls::polygon::Polygon;
use galileo_types::cartesian::traits::cartesian_point::CartesianPoint3d;
use galileo_types::geo::crs::{Crs, ProjectionType};
use galileo_types::geo::datum::Datum;
use galileo_types::geo::impls::point::GeoPoint2d;
use galileo_types::geo::traits::point::NewGeoPoint;
use galileo_types::geo::traits::projection::{ChainProjection, InvertedProjection, Projection};
use galileo_types::geometry::Geom;
use galileo_types::geometry_type::CartesianSpace2d;
use num_traits::AsPrimitive;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -107,7 +108,7 @@ pub async fn run(builder: MapBuilder) {
struct CountrySymbol {}

impl CountrySymbol {
fn get_polygon_symbol(&self, feature: &Country) -> SimplePolygonSymbol<CartesianSpace2d> {
fn get_polygon_symbol(&self, feature: &Country) -> SimplePolygonSymbol {
let stroke_color = feature.color;
let fill_color = Color {
a: if feature.is_selected() { 255 } else { 150 },
Expand All @@ -120,27 +121,15 @@ impl CountrySymbol {
}
}

impl Symbol<Country, Geom<Point2d>> for CountrySymbol {
fn render(
impl Symbol<Country> for CountrySymbol {
fn render<N: AsPrimitive<f32>, P: CartesianPoint3d<Num = N>>(
&self,
feature: &Country,
geometry: &Geom<Point2d>,
bundle: &mut Box<dyn RenderBundle>,
geometry: &Geom<P>,
bundle: &mut RenderBundle,
) -> Vec<PrimitiveId> {
let mut ids = vec![];
let Geom::MultiPolygon(geometry) = geometry else {
return ids;
};

for polygon in geometry.parts() {
ids.append(
&mut self
.get_polygon_symbol(feature)
.render(&(), polygon, bundle),
)
}

ids
self.get_polygon_symbol(feature)
.render(&(), geometry, bundle)
}

fn update(
Expand All @@ -153,7 +142,7 @@ impl Symbol<Country, Geom<Point2d>> for CountrySymbol {
let mut next_index = 0;
for _ in feature.geometry.parts() {
let polygon_symbol = self.get_polygon_symbol(feature);
<SimplePolygonSymbol<CartesianSpace2d> as Symbol<(), Polygon<Point2d>>>::update(
<SimplePolygonSymbol as Symbol<()>>::update(
&polygon_symbol,
&(),
&render_ids[next_index..next_index + renders_by_feature],
Expand Down
32 changes: 20 additions & 12 deletions galileo/examples/many_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ use galileo::layer::feature_layer::feature::Feature;
use galileo::layer::feature_layer::symbol::Symbol;
use galileo::layer::feature_layer::FeatureLayer;
use galileo::primitives::Color;
use galileo::render::{PointPaint, PrimitiveId, RenderBundle};
use galileo::render::render_bundle::RenderBundle;
use galileo::render::{PointPaint, PrimitiveId};
use galileo::tile_scheme::TileScheme;
use galileo_types::cartesian::impls::point::Point3d;
use galileo_types::cartesian::traits::cartesian_point::CartesianPoint3d;
use galileo_types::geo::crs::Crs;
use galileo_types::geometry::Geom;
use num_traits::AsPrimitive;

#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
Expand All @@ -29,20 +33,24 @@ impl Feature for ColoredPoint {
}

struct ColoredPointSymbol {}
impl Symbol<ColoredPoint, Point3d> for ColoredPointSymbol {
fn render(
impl Symbol<ColoredPoint> for ColoredPointSymbol {
fn render<N: AsPrimitive<f32>, P: CartesianPoint3d<Num = N>>(
&self,
feature: &ColoredPoint,
geometry: &Point3d,
bundle: &mut Box<dyn RenderBundle>,
geometry: &Geom<P>,
bundle: &mut RenderBundle,
) -> Vec<PrimitiveId> {
vec![bundle.add_point(
geometry,
PointPaint {
color: feature.color,
size: 3.0,
},
)]
if let Geom::Point(point) = geometry {
vec![bundle.add_point(
point,
PointPaint {
color: feature.color,
size: 3.0,
},
)]
} else {
vec![]
}
}
}

Expand Down
Loading

0 comments on commit df3cecd

Please sign in to comment.