From 376fa175d6cd6a78ba4f21d65c216bfbf56b32bd Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 22 Jun 2023 22:24:30 +0200 Subject: [PATCH 1/8] Ability to build mesh for a specified geometry. --- Cargo.toml | 5 ++ src/lib.rs | 156 ++++++++++++++++++++++++++++++++------------- src/line_string.rs | 14 ++-- src/polygon.rs | 38 +++++++---- 4 files changed, 150 insertions(+), 63 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 29cec50..c81fc98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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. +geo-types = { git = "https://github.com/georust/geo" } diff --git a/src/lib.rs b/src/lib.rs index cb2f676..90af78d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,39 +1,72 @@ -use bevy::prelude::*; +use crate::polygon::PolygonMesh; +use bevy::prelude::{info_span, Mesh}; +use geo_types::geometry::*; +use line_string::LineStringMeshBuilder; +use polygon::PolygonMeshBuilder; use std::num::TryFromIntError; mod line_string; mod point; mod polygon; -pub enum PreparedMesh { - Point(Vec), - LineString { - mesh: Mesh, - }, - Polygon { - polygon_mesh: Mesh, - exterior_mesh: Mesh, - interior_meshes: Vec, - }, +pub fn line_to_mesh(line: &Line) -> Result, TryFromIntError> { + line_string_to_mesh(&line.into()) } -trait BuildMesh { - fn build(self) -> Option; +pub fn line_string_to_mesh(line_string: &LineString) -> Result, TryFromIntError> { + let mut mesh_builder = LineStringMeshBuilder::default(); + mesh_builder.add_line_string(line_string)?; + Ok(mesh_builder.into()) } -#[derive(Default)] -pub struct BuildBevyMeshesContext { - point_mesh_builder: point::PointMeshBuilder, - line_string_mesh_builder: line_string::LineStringMeshBuilder, - polygon_mesh_builder: polygon::PolygonMeshBuilder, +pub fn multi_line_string_to_mesh( + multi_line_string: &MultiLineString, +) -> Result, TryFromIntError> { + let line_strings = &multi_line_string.0; + let mut line_string_meshes = Vec::with_capacity(line_strings.len()); + + for line_string in line_strings { + if let Some(line_string_mesh) = line_string_to_mesh(line_string)? { + line_string_meshes.push(line_string_mesh); + } + } + + Ok(line_string_meshes) +} + +pub fn polygon_to_mesh(polygon: &Polygon) -> Result, TryFromIntError> { + let mut mesh_builder = PolygonMeshBuilder::default(); + mesh_builder.add_polygon(polygon)?; + Ok(mesh_builder.into()) +} + +pub fn multi_polygon_to_mesh( + multi_polygon: &MultiPolygon, +) -> Result, TryFromIntError> { + let polygons = &multi_polygon.0; + let mut polygon_meshes = Vec::with_capacity(polygons.len()); + for polygon in polygons { + if let Some(polygon_mesh) = polygon_to_mesh(polygon)? { + polygon_meshes.push(polygon_mesh); + } + } + + Ok(polygon_meshes) +} + +pub fn rect_to_mesh(rect: &Rect) -> Result, TryFromIntError> { + polygon_to_mesh(&rect.to_polygon()) } -pub fn build_bevy_meshes( - geo: &G, -) -> Result, TryFromIntError> { +pub fn triangle_to_mesh(triangle: &Triangle) -> Result, TryFromIntError> { + polygon_to_mesh(&triangle.to_polygon()) +} + +pub fn geometry_to_mesh(geometry: &Geometry) -> Result, TryFromIntError> { let mut ctx = BuildBevyMeshesContext::default(); - info_span!("Populating Bevy mesh builder").in_scope(|| geo.populate_mesh_builders(&mut ctx))?; + info_span!("Populating Bevy mesh builder") + .in_scope(|| geometry.populate_mesh_builders(&mut ctx))?; info_span!("Building Bevy meshes").in_scope(|| { Ok([ @@ -42,10 +75,41 @@ pub fn build_bevy_meshes( ctx.polygon_mesh_builder.build(), ] .into_iter() - .flatten()) + .find(|prepared_mesh| prepared_mesh.is_some()) + .unwrap_or_default()) }) } +pub fn geometry_collection_to_mesh( + geometry_collection: &GeometryCollection, +) -> Result, TryFromIntError> { + let mut geometry_meshes = Vec::with_capacity(geometry_collection.len()); + for geometry in geometry_collection { + if let Some(geometry_mesh) = geometry_to_mesh(geometry)? { + geometry_meshes.push(geometry_mesh); + } + } + + Ok(geometry_meshes) +} + +pub enum PreparedMesh { + Point(Vec), + LineString { mesh: Mesh }, + Polygon(polygon::PolygonMesh), +} + +trait BuildMesh { + fn build(self) -> Option; +} + +#[derive(Default)] +pub struct BuildBevyMeshesContext { + point_mesh_builder: point::PointMeshBuilder, + line_string_mesh_builder: LineStringMeshBuilder, + polygon_mesh_builder: polygon::PolygonMeshBuilder, +} + pub trait BuildBevyMeshes { fn populate_mesh_builders( &self, @@ -53,7 +117,7 @@ pub trait BuildBevyMeshes { ) -> Result<(), TryFromIntError>; } -impl BuildBevyMeshes for geo::Point { +impl BuildBevyMeshes for Point { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -62,7 +126,7 @@ impl BuildBevyMeshes for geo::Point { } } -impl BuildBevyMeshes for geo::LineString { +impl BuildBevyMeshes for LineString { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -71,16 +135,16 @@ impl BuildBevyMeshes for geo::LineString { } } -impl BuildBevyMeshes for geo::Polygon { +impl BuildBevyMeshes for Polygon { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, ) -> Result<(), TryFromIntError> { - ctx.polygon_mesh_builder.add_polygon_components(self) + ctx.polygon_mesh_builder.add_polygon(self) } } -impl BuildBevyMeshes for geo::MultiPoint { +impl BuildBevyMeshes for MultiPoint { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -92,7 +156,7 @@ impl BuildBevyMeshes for geo::MultiPoint { } } -impl BuildBevyMeshes for geo::MultiLineString { +impl BuildBevyMeshes for MultiLineString { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -104,7 +168,7 @@ impl BuildBevyMeshes for geo::MultiLineString { } } -impl BuildBevyMeshes for geo::MultiPolygon { +impl BuildBevyMeshes for MultiPolygon { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -116,16 +180,16 @@ impl BuildBevyMeshes for geo::MultiPolygon { } } -impl BuildBevyMeshes for geo::Line { +impl BuildBevyMeshes for Line { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, ) -> Result<(), TryFromIntError> { - geo::LineString::new(vec![self.start, self.end]).populate_mesh_builders(ctx) + LineString::from(self).populate_mesh_builders(ctx) } } -impl BuildBevyMeshes for geo::Triangle { +impl BuildBevyMeshes for Triangle { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -134,7 +198,7 @@ impl BuildBevyMeshes for geo::Triangle { } } -impl BuildBevyMeshes for geo::Rect { +impl BuildBevyMeshes for Rect { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, @@ -143,28 +207,28 @@ impl BuildBevyMeshes for geo::Rect { } } -impl BuildBevyMeshes for geo::Geometry { +impl BuildBevyMeshes for Geometry { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, ) -> Result<(), TryFromIntError> { match self { - geo::Geometry::Point(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::Line(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::LineString(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::Polygon(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::MultiPoint(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::MultiLineString(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::MultiPolygon(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::GeometryCollection(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::Triangle(g) => g.populate_mesh_builders(ctx)?, - geo::Geometry::Rect(g) => g.populate_mesh_builders(ctx)?, + 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 geo::GeometryCollection { +impl BuildBevyMeshes for GeometryCollection { fn populate_mesh_builders( &self, ctx: &mut BuildBevyMeshesContext, diff --git a/src/line_string.rs b/src/line_string.rs index b6e8fac..634d31d 100644 --- a/src/line_string.rs +++ b/src/line_string.rs @@ -50,12 +50,18 @@ impl From for Mesh { } } -impl crate::BuildMesh for LineStringMeshBuilder { - fn build(self) -> Option { - if self.vertices.is_empty() { +impl From for Option { + fn from(line_string_mesh_builder: LineStringMeshBuilder) -> Self { + if line_string_mesh_builder.vertices.is_empty() { None } else { - Some(crate::PreparedMesh::LineString { mesh: self.into() }) + Some(line_string_mesh_builder.into()) } } } + +impl crate::BuildMesh for LineStringMeshBuilder { + fn build(self) -> Option { + Option::::from(self).map(|mesh| crate::PreparedMesh::LineString { mesh }) + } +} diff --git a/src/polygon.rs b/src/polygon.rs index 0d5003a..587ad45 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -1,5 +1,13 @@ use crate::{line_string::LineStringMeshBuilder, PreparedMesh}; -use geo::algorithm::coords_iter::CoordsIter; +use bevy::prelude::Mesh; +use geo::CoordsIter; +use geo_types::{LineString, Polygon}; + +pub struct PolygonMesh { + pub polygon_mesh: Mesh, + pub exterior_mesh: Mesh, + pub interior_meshes: Vec, +} #[derive(Default)] pub struct PolygonMeshBuilder { @@ -9,10 +17,7 @@ pub struct PolygonMeshBuilder { } impl PolygonMeshBuilder { - pub fn add_polygon_components( - &mut self, - polygon: &geo::Polygon, - ) -> Result<(), std::num::TryFromIntError> { + pub fn add_polygon(&mut self, polygon: &Polygon) -> Result<(), std::num::TryFromIntError> { self.polygon .add_earcutr_input(Self::polygon_to_earcutr_input(polygon)); self.exterior.add_line_string(polygon.exterior())?; @@ -25,7 +30,7 @@ impl PolygonMeshBuilder { Ok(()) } - fn polygon_to_earcutr_input(polygon: &geo::Polygon) -> bevy_earcutr::EarcutrInput { + fn polygon_to_earcutr_input(polygon: &Polygon) -> bevy_earcutr::EarcutrInput { let mut vertices = Vec::with_capacity(polygon.coords_count() * 2); let mut interior_indices = Vec::with_capacity(polygon.interiors().len()); debug_assert!(polygon.exterior().0.len() >= 4); @@ -44,7 +49,7 @@ impl PolygonMeshBuilder { } } - fn flat_line_string_coords_2(line_string: &geo::LineString, vertices: &mut Vec) { + fn flat_line_string_coords_2(line_string: &LineString, vertices: &mut Vec) { for coord in &line_string.0 { vertices.push(coord.x); vertices.push(coord.y); @@ -52,14 +57,15 @@ impl PolygonMeshBuilder { } } -impl crate::BuildMesh for PolygonMeshBuilder { - fn build(self) -> Option { - self.polygon +impl From for Option { + fn from(polygon_mesh_builder: PolygonMeshBuilder) -> Self { + polygon_mesh_builder + .polygon .build() - .map(|polygon_mesh| PreparedMesh::Polygon { + .map(|polygon_mesh| PolygonMesh { polygon_mesh, - exterior_mesh: self.exterior.into(), - interior_meshes: self + exterior_mesh: polygon_mesh_builder.exterior.into(), + interior_meshes: polygon_mesh_builder .interiors .into_iter() .map(|interior_builder| interior_builder.into()) @@ -67,3 +73,9 @@ impl crate::BuildMesh for PolygonMeshBuilder { }) } } + +impl crate::BuildMesh for PolygonMeshBuilder { + fn build(self) -> Option { + Option::::from(self).map(PreparedMesh::Polygon) + } +} From e8947a3c8d9a956e1bdd40e99b83dbc954ad7e75 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 22 Jun 2023 22:26:25 +0200 Subject: [PATCH 2/8] PreparedMesh -> GeometryMesh --- src/lib.rs | 8 ++++---- src/line_string.rs | 4 ++-- src/point.rs | 4 ++-- src/polygon.rs | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 90af78d..2740357 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub fn triangle_to_mesh(triangle: &Triangle) -> Result, TryF polygon_to_mesh(&triangle.to_polygon()) } -pub fn geometry_to_mesh(geometry: &Geometry) -> Result, TryFromIntError> { +pub fn geometry_to_mesh(geometry: &Geometry) -> Result, TryFromIntError> { let mut ctx = BuildBevyMeshesContext::default(); info_span!("Populating Bevy mesh builder") @@ -82,7 +82,7 @@ pub fn geometry_to_mesh(geometry: &Geometry) -> Result, Try pub fn geometry_collection_to_mesh( geometry_collection: &GeometryCollection, -) -> Result, TryFromIntError> { +) -> Result, TryFromIntError> { let mut geometry_meshes = Vec::with_capacity(geometry_collection.len()); for geometry in geometry_collection { if let Some(geometry_mesh) = geometry_to_mesh(geometry)? { @@ -93,14 +93,14 @@ pub fn geometry_collection_to_mesh( Ok(geometry_meshes) } -pub enum PreparedMesh { +pub enum GeometryMesh { Point(Vec), LineString { mesh: Mesh }, Polygon(polygon::PolygonMesh), } trait BuildMesh { - fn build(self) -> Option; + fn build(self) -> Option; } #[derive(Default)] diff --git a/src/line_string.rs b/src/line_string.rs index 634d31d..add6da8 100644 --- a/src/line_string.rs +++ b/src/line_string.rs @@ -61,7 +61,7 @@ impl From for Option { } impl crate::BuildMesh for LineStringMeshBuilder { - fn build(self) -> Option { - Option::::from(self).map(|mesh| crate::PreparedMesh::LineString { mesh }) + fn build(self) -> Option { + Option::::from(self).map(|mesh| crate::GeometryMesh::LineString { mesh }) } } diff --git a/src/point.rs b/src/point.rs index bb622b8..7619c12 100644 --- a/src/point.rs +++ b/src/point.rs @@ -14,11 +14,11 @@ impl PointMeshBuilder { } impl crate::BuildMesh for PointMeshBuilder { - fn build(self) -> Option { + fn build(self) -> Option { if self.points.is_empty() { None } else { - Some(crate::PreparedMesh::Point(self.points)) + Some(crate::GeometryMesh::Point(self.points)) } } } diff --git a/src/polygon.rs b/src/polygon.rs index 587ad45..063cf22 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -1,4 +1,4 @@ -use crate::{line_string::LineStringMeshBuilder, PreparedMesh}; +use crate::{line_string::LineStringMeshBuilder, GeometryMesh}; use bevy::prelude::Mesh; use geo::CoordsIter; use geo_types::{LineString, Polygon}; @@ -75,7 +75,7 @@ impl From for Option { } impl crate::BuildMesh for PolygonMeshBuilder { - fn build(self) -> Option { - Option::::from(self).map(PreparedMesh::Polygon) + fn build(self) -> Option { + Option::::from(self).map(GeometryMesh::Polygon) } } From 903878d73d7410ba0885057219eb794051e0e8f2 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 22 Jun 2023 22:31:25 +0200 Subject: [PATCH 3/8] Place mesh building traits and implementations in a separate file. --- src/build_mesh.rs | 143 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 148 ++------------------------------------------- src/line_string.rs | 2 +- src/point.rs | 2 +- src/polygon.rs | 2 +- 5 files changed, 151 insertions(+), 146 deletions(-) create mode 100644 src/build_mesh.rs diff --git a/src/build_mesh.rs b/src/build_mesh.rs new file mode 100644 index 0000000..64c2ba0 --- /dev/null +++ b/src/build_mesh.rs @@ -0,0 +1,143 @@ +use geo_types::*; +use std::num::TryFromIntError; + +pub trait BuildMesh { + fn build(self) -> Option; +} + +#[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(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 2740357..54c045f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ -use crate::polygon::PolygonMesh; use bevy::prelude::{info_span, Mesh}; +use build_mesh::BuildBevyMeshes; +use build_mesh::BuildMesh; use geo_types::geometry::*; use line_string::LineStringMeshBuilder; +use polygon::PolygonMesh; use polygon::PolygonMeshBuilder; use std::num::TryFromIntError; +mod build_mesh; mod line_string; mod point; mod polygon; @@ -63,7 +66,7 @@ pub fn triangle_to_mesh(triangle: &Triangle) -> Result, TryF } pub fn geometry_to_mesh(geometry: &Geometry) -> Result, TryFromIntError> { - let mut ctx = BuildBevyMeshesContext::default(); + let mut ctx = build_mesh::BuildBevyMeshesContext::default(); info_span!("Populating Bevy mesh builder") .in_scope(|| geometry.populate_mesh_builders(&mut ctx))?; @@ -98,144 +101,3 @@ pub enum GeometryMesh { LineString { mesh: Mesh }, Polygon(polygon::PolygonMesh), } - -trait BuildMesh { - fn build(self) -> Option; -} - -#[derive(Default)] -pub struct BuildBevyMeshesContext { - point_mesh_builder: point::PointMeshBuilder, - line_string_mesh_builder: LineStringMeshBuilder, - polygon_mesh_builder: 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(()) - } -} diff --git a/src/line_string.rs b/src/line_string.rs index add6da8..f80da36 100644 --- a/src/line_string.rs +++ b/src/line_string.rs @@ -60,7 +60,7 @@ impl From for Option { } } -impl crate::BuildMesh for LineStringMeshBuilder { +impl crate::build_mesh::BuildMesh for LineStringMeshBuilder { fn build(self) -> Option { Option::::from(self).map(|mesh| crate::GeometryMesh::LineString { mesh }) } diff --git a/src/point.rs b/src/point.rs index 7619c12..6ca5491 100644 --- a/src/point.rs +++ b/src/point.rs @@ -13,7 +13,7 @@ impl PointMeshBuilder { } } -impl crate::BuildMesh for PointMeshBuilder { +impl crate::build_mesh::BuildMesh for PointMeshBuilder { fn build(self) -> Option { if self.points.is_empty() { None diff --git a/src/polygon.rs b/src/polygon.rs index 063cf22..2aed068 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -74,7 +74,7 @@ impl From for Option { } } -impl crate::BuildMesh for PolygonMeshBuilder { +impl crate::build_mesh::BuildMesh for PolygonMeshBuilder { fn build(self) -> Option { Option::::from(self).map(GeometryMesh::Polygon) } From 30116fe830dd0861f54af8ea2c3bf44782371a10 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 22 Jun 2023 22:34:08 +0200 Subject: [PATCH 4/8] Make `GeometryMesh::LineString` a newtype struct. --- src/lib.rs | 2 +- src/line_string.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 54c045f..44c5765 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,6 +98,6 @@ pub fn geometry_collection_to_mesh( pub enum GeometryMesh { Point(Vec), - LineString { mesh: Mesh }, + LineString(Mesh), Polygon(polygon::PolygonMesh), } diff --git a/src/line_string.rs b/src/line_string.rs index f80da36..8eee34e 100644 --- a/src/line_string.rs +++ b/src/line_string.rs @@ -62,6 +62,6 @@ impl From for Option { impl crate::build_mesh::BuildMesh for LineStringMeshBuilder { fn build(self) -> Option { - Option::::from(self).map(|mesh| crate::GeometryMesh::LineString { mesh }) + Option::::from(self).map(|mesh| crate::GeometryMesh::LineString(mesh)) } } From 94cd24188ae9d5b3ab98d943bb3a2cd54183a5e1 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 23 Jun 2023 23:00:28 +0200 Subject: [PATCH 5/8] Remove redundant closure. --- src/line_string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/line_string.rs b/src/line_string.rs index 8eee34e..ccd93d5 100644 --- a/src/line_string.rs +++ b/src/line_string.rs @@ -62,6 +62,6 @@ impl From for Option { impl crate::build_mesh::BuildMesh for LineStringMeshBuilder { fn build(self) -> Option { - Option::::from(self).map(|mesh| crate::GeometryMesh::LineString(mesh)) + Option::::from(self).map(crate::GeometryMesh::LineString) } } From dbc21443debcfa4e652d16ddac917f9c2b0ffad5 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 23 Jun 2023 23:05:19 +0200 Subject: [PATCH 6/8] `PolygonMesh.polygon_mesh` -> `PolygonMesh.mesh` --- src/polygon.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/polygon.rs b/src/polygon.rs index 2aed068..6bfa576 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -4,7 +4,7 @@ use geo::CoordsIter; use geo_types::{LineString, Polygon}; pub struct PolygonMesh { - pub polygon_mesh: Mesh, + pub mesh: Mesh, pub exterior_mesh: Mesh, pub interior_meshes: Vec, } @@ -63,7 +63,7 @@ impl From for Option { .polygon .build() .map(|polygon_mesh| PolygonMesh { - polygon_mesh, + mesh: polygon_mesh, exterior_mesh: polygon_mesh_builder.exterior.into(), interior_meshes: polygon_mesh_builder .interiors From cc92339ed7cb122c177cfb9fd623af22bf95c677 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 23 Jun 2023 23:09:16 +0200 Subject: [PATCH 7/8] Make `PolygonMesh` public. --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 44c5765..039b38d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,12 @@ use bevy::prelude::{info_span, Mesh}; -use build_mesh::BuildBevyMeshes; -use build_mesh::BuildMesh; +use build_mesh::{BuildBevyMeshes, BuildMesh}; use geo_types::geometry::*; use line_string::LineStringMeshBuilder; -use polygon::PolygonMesh; use polygon::PolygonMeshBuilder; use std::num::TryFromIntError; +pub use polygon::PolygonMesh; + mod build_mesh; mod line_string; mod point; From fa1f58a5c51c40ce461fab3a49d63bc2e9605e90 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Sat, 1 Jul 2023 17:35:06 +0200 Subject: [PATCH 8/8] Remove temporary `geo-types` patch. --- Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c81fc98..366a800 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,3 @@ 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. -geo-types = { git = "https://github.com/georust/geo" }