Skip to content

Commit

Permalink
Merge pull request #55 from mockersf/switch-to-geo-types
Browse files Browse the repository at this point in the history
depends on geo_types instead of geo
  • Loading branch information
lelongg authored Sep 1, 2023
2 parents c5e43a5 + 68640dc commit a2cf093
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["geo", "polygon", "offset", "margin", "padding"]
categories = ["algorithms"]

[dependencies]
geo = "0.25.0"
geo-types = "0.7"
itertools = "0.11.0"
geo-clipper = "0.7.0"
num-traits = "0.2.8"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ The following example shows how to compute an inflated line.
The [`offset`] method is provided by the [`Offset`] trait which is implemented for most [geo-types](https://docs.rs/geo-types/0.4.3/geo_types/).

```rust
use geo_types::{Coordinate, Line};
use geo_types::{Coord, Line};
use geo_offset::Offset;

let line = Line::new(
Coordinate { x: 0.0, y: 0.0 },
Coordinate { x: 1.0, y: 8.0 },
Coord { x: 0.0, y: 0.0 },
Coord { x: 1.0, y: 8.0 },
);

let line_with_offset = line.offset(2.0)?;
Expand Down
2 changes: 1 addition & 1 deletion src/edge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Point = geo::Coordinate<f64>;
type Point = geo_types::Coord<f64>;

/// This enumeration contains error cases for edges manipulation.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
//!
//! ```
//! # fn main() -> Result<(), geo_offset::OffsetError> {
//! use geo_types::{Coordinate, Line};
//! use geo_types::{Coord, Line};
//! use geo_offset::Offset;
//!
//! let line = Line::new(
//! Coordinate { x: 0.0, y: 0.0 },
//! Coordinate { x: 1.0, y: 8.0 },
//! Coord { x: 0.0, y: 0.0 },
//! Coord { x: 1.0, y: 8.0 },
//! );
//!
//! let line_with_offset = line.offset(2.0)?;
Expand Down
108 changes: 56 additions & 52 deletions src/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ pub enum OffsetError {
pub const DEFAULT_ARC_SEGMENTS: u32 = 5;

pub trait Offset {
fn offset(&self, distance: f64) -> Result<geo::MultiPolygon<f64>, OffsetError> {
fn offset(&self, distance: f64) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
self.offset_with_arc_segments(distance, DEFAULT_ARC_SEGMENTS)
}

fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError>;
) -> Result<geo_types::MultiPolygon<f64>, OffsetError>;
}

impl Offset for geo::GeometryCollection<f64> {
impl Offset for geo_types::GeometryCollection<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
let mut geometry_collection_with_offset = geo::MultiPolygon(Vec::new());
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
let mut geometry_collection_with_offset = geo_types::MultiPolygon(Vec::new());
for geometry in self.0.iter() {
let geometry_with_offset = geometry.offset_with_arc_segments(distance, arc_segments)?;
geometry_collection_with_offset =
Expand All @@ -39,50 +39,54 @@ impl Offset for geo::GeometryCollection<f64> {
}
}

impl Offset for geo::Geometry<f64> {
impl Offset for geo_types::Geometry<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
match self {
geo::Geometry::Point(point) => point.offset_with_arc_segments(distance, arc_segments),
geo::Geometry::Line(line) => line.offset_with_arc_segments(distance, arc_segments),
geo::Geometry::LineString(line_tring) => {
geo_types::Geometry::Point(point) => {
point.offset_with_arc_segments(distance, arc_segments)
}
geo_types::Geometry::Line(line) => {
line.offset_with_arc_segments(distance, arc_segments)
}
geo_types::Geometry::LineString(line_tring) => {
line_tring.offset_with_arc_segments(distance, arc_segments)
}
geo::Geometry::Triangle(triangle) => triangle
geo_types::Geometry::Triangle(triangle) => triangle
.to_polygon()
.offset_with_arc_segments(distance, arc_segments),
geo::Geometry::Rect(rect) => rect
geo_types::Geometry::Rect(rect) => rect
.to_polygon()
.offset_with_arc_segments(distance, arc_segments),
geo::Geometry::Polygon(polygon) => {
geo_types::Geometry::Polygon(polygon) => {
polygon.offset_with_arc_segments(distance, arc_segments)
}
geo::Geometry::MultiPoint(multi_point) => {
geo_types::Geometry::MultiPoint(multi_point) => {
multi_point.offset_with_arc_segments(distance, arc_segments)
}
geo::Geometry::MultiLineString(multi_line_string) => {
geo_types::Geometry::MultiLineString(multi_line_string) => {
multi_line_string.offset_with_arc_segments(distance, arc_segments)
}
geo::Geometry::MultiPolygon(multi_polygon) => {
geo_types::Geometry::MultiPolygon(multi_polygon) => {
multi_polygon.offset_with_arc_segments(distance, arc_segments)
}
geo::Geometry::GeometryCollection(geometry_collection) => {
geo_types::Geometry::GeometryCollection(geometry_collection) => {
geometry_collection.offset_with_arc_segments(distance, arc_segments)
}
}
}
}

impl Offset for geo::MultiPolygon<f64> {
impl Offset for geo_types::MultiPolygon<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
let mut polygons = geo::MultiPolygon(Vec::new());
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
let mut polygons = geo_types::MultiPolygon(Vec::new());
for polygon in self.0.iter() {
let polygon_with_offset = polygon.offset_with_arc_segments(distance, arc_segments)?;
polygons = polygons.union(&polygon_with_offset, 1000.0);
Expand All @@ -91,16 +95,16 @@ impl Offset for geo::MultiPolygon<f64> {
}
}

impl Offset for geo::Polygon<f64> {
impl Offset for geo_types::Polygon<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
let exterior_with_offset = self
.exterior()
.offset_with_arc_segments(distance.abs(), arc_segments)?;
let interiors_with_offset = geo::MultiLineString(self.interiors().to_vec())
let interiors_with_offset = geo_types::MultiLineString(self.interiors().to_vec())
.offset_with_arc_segments(distance.abs(), arc_segments)?;

Ok(if distance.is_sign_positive() {
Expand All @@ -113,17 +117,17 @@ impl Offset for geo::Polygon<f64> {
}
}

impl Offset for geo::MultiLineString<f64> {
impl Offset for geo_types::MultiLineString<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
if distance < 0.0 {
return Ok(geo::MultiPolygon(Vec::new()));
return Ok(geo_types::MultiPolygon(Vec::new()));
}

let mut multi_line_string_with_offset = geo::MultiPolygon(Vec::new());
let mut multi_line_string_with_offset = geo_types::MultiPolygon(Vec::new());
for line_string in self.0.iter() {
let line_string_with_offset =
line_string.offset_with_arc_segments(distance, arc_segments)?;
Expand All @@ -134,24 +138,24 @@ impl Offset for geo::MultiLineString<f64> {
}
}

impl Offset for geo::LineString<f64> {
impl Offset for geo_types::LineString<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
if distance < 0.0 {
return Ok(geo::MultiPolygon(Vec::new()));
return Ok(geo_types::MultiPolygon(Vec::new()));
}

let mut line_string_with_offset = geo::MultiPolygon(Vec::new());
let mut line_string_with_offset = geo_types::MultiPolygon(Vec::new());
for line in self.lines() {
let line_with_offset = line.offset_with_arc_segments(distance, arc_segments)?;
line_string_with_offset = line_string_with_offset.union(&line_with_offset, 1000.0);
}

let line_string_with_offset = line_string_with_offset.0.iter().skip(1).fold(
geo::MultiPolygon(
geo_types::MultiPolygon(
line_string_with_offset
.0
.get(0)
Expand All @@ -165,14 +169,14 @@ impl Offset for geo::LineString<f64> {
}
}

impl Offset for geo::Line<f64> {
impl Offset for geo_types::Line<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
if distance < 0.0 {
return Ok(geo::MultiPolygon(Vec::new()));
return Ok(geo_types::MultiPolygon(Vec::new()));
}

let v1 = &self.start;
Expand Down Expand Up @@ -202,27 +206,27 @@ impl Offset for geo::Line<f64> {
);
}

Ok(geo::MultiPolygon(vec![geo::Polygon::new(
geo::LineString(vertices),
Ok(geo_types::MultiPolygon(vec![geo_types::Polygon::new(
geo_types::LineString(vertices),
vec![],
)]))
} else {
geo::Point::from(self.start).offset_with_arc_segments(distance, arc_segments)
geo_types::Point::from(self.start).offset_with_arc_segments(distance, arc_segments)
}
}
}

impl Offset for geo::MultiPoint<f64> {
impl Offset for geo_types::MultiPoint<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
if distance < 0.0 {
return Ok(geo::MultiPolygon(Vec::new()));
return Ok(geo_types::MultiPolygon(Vec::new()));
}

let mut multi_point_with_offset = geo::MultiPolygon(Vec::new());
let mut multi_point_with_offset = geo_types::MultiPolygon(Vec::new());
for point in self.0.iter() {
let point_with_offset = point.offset_with_arc_segments(distance, arc_segments)?;
multi_point_with_offset = multi_point_with_offset.union(&point_with_offset, 1000.0);
Expand All @@ -231,14 +235,14 @@ impl Offset for geo::MultiPoint<f64> {
}
}

impl Offset for geo::Point<f64> {
impl Offset for geo_types::Point<f64> {
fn offset_with_arc_segments(
&self,
distance: f64,
arc_segments: u32,
) -> Result<geo::MultiPolygon<f64>, OffsetError> {
) -> Result<geo_types::MultiPolygon<f64>, OffsetError> {
if distance < 0.0 {
return Ok(geo::MultiPolygon(Vec::new()));
return Ok(geo_types::MultiPolygon(Vec::new()));
}

let mut angle = 0.0;
Expand All @@ -251,26 +255,26 @@ impl Offset for geo::Point<f64> {
let contour = (0..vertice_count)
.map(|_| {
angle += 2.0 * std::f64::consts::PI / f64::from(vertice_count); // counter-clockwise
geo::Coordinate::from((
geo_types::Coord::from((
self.x() + (distance * angle.cos()),
self.y() + (distance * angle.sin()),
))
})
.collect();

Ok(geo::MultiPolygon(vec![geo::Polygon::new(
Ok(geo_types::MultiPolygon(vec![geo_types::Polygon::new(
contour,
Vec::new(),
)]))
}
}

fn create_arc(
vertices: &mut Vec<geo::Coordinate<f64>>,
center: &geo::Coordinate<f64>,
vertices: &mut Vec<geo_types::Coord<f64>>,
center: &geo_types::Coord<f64>,
radius: f64,
start_vertex: &geo::Coordinate<f64>,
end_vertex: &geo::Coordinate<f64>,
start_vertex: &geo_types::Coord<f64>,
end_vertex: &geo_types::Coord<f64>,
segment_count: u32,
outwards: bool,
) {
Expand Down Expand Up @@ -307,7 +311,7 @@ fn create_arc(
vertices.push(*start_vertex);
for i in 1..segment_count {
let angle = start_angle + segment_angle * f64::from(i);
vertices.push(geo::Coordinate::from((
vertices.push(geo_types::Coord::from((
center.x + angle.cos() * radius,
center.y + angle.sin() * radius,
)));
Expand Down
Loading

0 comments on commit a2cf093

Please sign in to comment.