Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare a mesh for a given geometry. #9

Merged
merged 13 commits into from
Jul 1, 2023
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "geo-bevy"
description = "Generate Bevy meshes from `geo` types"
version = "0.3.0"
version = "0.4.0"
authors = ["Corey Farwell <coreyf@rwell.org>"]
edition = "2021"
repository = "https://github.com/frewsxcv/geo-bevy"
Expand All @@ -11,3 +11,8 @@ license = "MIT OR Apache-2.0"
bevy-earcutr = "0.9"
bevy = { version = "0.10", default-features = false, features = ["bevy_render"] }
geo = "0.25"
geo-types = "0.7"

[patch.crates-io]
# TEMP: https://github.com/georust/geo/pull/1020 is yet to included in a release.
gibbz00 marked this conversation as resolved.
Show resolved Hide resolved
geo-types = { git = "https://github.com/georust/geo" }
143 changes: 143 additions & 0 deletions src/build_mesh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use geo_types::*;
use std::num::TryFromIntError;

pub trait BuildMesh {
fn build(self) -> Option<crate::GeometryMesh>;
}

#[derive(Default)]
pub struct BuildBevyMeshesContext {
pub point_mesh_builder: crate::point::PointMeshBuilder,
pub line_string_mesh_builder: crate::line_string::LineStringMeshBuilder,
pub polygon_mesh_builder: crate::polygon::PolygonMeshBuilder,
}

pub trait BuildBevyMeshes {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError>;
}

impl BuildBevyMeshes for Point {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
ctx.point_mesh_builder.add_point(self)
}
}

impl BuildBevyMeshes for LineString {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
ctx.line_string_mesh_builder.add_line_string(self)
}
}

impl BuildBevyMeshes for Polygon {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
ctx.polygon_mesh_builder.add_polygon(self)
}
}

impl BuildBevyMeshes for MultiPoint {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
for point in &self.0 {
point.populate_mesh_builders(ctx)?;
}
Ok(())
}
}

impl BuildBevyMeshes for MultiLineString {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
for line_string in &self.0 {
line_string.populate_mesh_builders(ctx)?;
}
Ok(())
}
}

impl BuildBevyMeshes for MultiPolygon {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
for polygon in &self.0 {
polygon.populate_mesh_builders(ctx)?;
}
Ok(())
}
}

impl BuildBevyMeshes for Line {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
LineString::from(self).populate_mesh_builders(ctx)
}
}

impl BuildBevyMeshes for Triangle {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
self.to_polygon().populate_mesh_builders(ctx)
}
}

impl BuildBevyMeshes for Rect {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
self.to_polygon().populate_mesh_builders(ctx)
}
}

impl BuildBevyMeshes for Geometry {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
match self {
Geometry::Point(g) => g.populate_mesh_builders(ctx)?,
Geometry::Line(g) => g.populate_mesh_builders(ctx)?,
Geometry::LineString(g) => g.populate_mesh_builders(ctx)?,
Geometry::Polygon(g) => g.populate_mesh_builders(ctx)?,
Geometry::MultiPoint(g) => g.populate_mesh_builders(ctx)?,
Geometry::MultiLineString(g) => g.populate_mesh_builders(ctx)?,
Geometry::MultiPolygon(g) => g.populate_mesh_builders(ctx)?,
Geometry::GeometryCollection(g) => g.populate_mesh_builders(ctx)?,
Geometry::Triangle(g) => g.populate_mesh_builders(ctx)?,
Geometry::Rect(g) => g.populate_mesh_builders(ctx)?,
};
Ok(())
}
}

impl BuildBevyMeshes for GeometryCollection {
fn populate_mesh_builders(
&self,
ctx: &mut BuildBevyMeshesContext,
) -> Result<(), TryFromIntError> {
for g in self {
g.populate_mesh_builders(ctx)?;
}
Ok(())
}
}
Loading