Skip to content

Commit

Permalink
Merge #783
Browse files Browse the repository at this point in the history
783: Use `Triangle::new()` instead of `Triangle()` r=frewsxcv a=nyurik

Make migration simpler by using static function that will still be available if `Triangle` becomes a type alias.

Similar to #777

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- ~[ ] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.~
---



Co-authored-by: Yuri Astrakhan <[email protected]>
  • Loading branch information
bors[bot] and nyurik authored Mar 28, 2022
2 parents c0c7944 + a5cf700 commit 17ba0fe
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<T: CoordNum> LineString<T> {
self.0.windows(3).map(|w| {
// slice::windows(N) is guaranteed to yield a slice with exactly N elements
unsafe {
Triangle(
Triangle::new(
*w.get_unchecked(0),
*w.get_unchecked(1),
*w.get_unchecked(2),
Expand Down
15 changes: 10 additions & 5 deletions geo-types/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use approx::{AbsDiffEq, RelativeEq};
pub struct Triangle<T: CoordNum>(pub Coordinate<T>, pub Coordinate<T>, pub Coordinate<T>);

impl<T: CoordNum> Triangle<T> {
/// Instantiate Self from the raw content value
pub fn new(v1: Coordinate<T>, v2: Coordinate<T>, v3: Coordinate<T>) -> Self {
Self(v1, v2, v3)
}

pub fn to_array(&self) -> [Coordinate<T>; 3] {
[self.0, self.1, self.2]
}
Expand All @@ -31,7 +36,7 @@ impl<T: CoordNum> Triangle<T> {
/// ```rust
/// use geo_types::{coord, Triangle, polygon};
///
/// let triangle = Triangle(
/// let triangle = Triangle::new(
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 20. },
/// coord! { x: 20., y: -10. },
Expand Down Expand Up @@ -75,8 +80,8 @@ where
/// ```
/// use geo_types::{point, Triangle};
///
/// let a = Triangle((0.0, 0.0).into(), (10.0, 10.0).into(), (0.0, 5.0).into());
/// let b = Triangle((0.0, 0.0).into(), (10.01, 10.0).into(), (0.0, 5.0).into());
/// let a = Triangle::new((0.0, 0.0).into(), (10.0, 10.0).into(), (0.0, 5.0).into());
/// let b = Triangle::new((0.0, 0.0).into(), (10.01, 10.0).into(), (0.0, 5.0).into());
///
/// approx::assert_relative_eq!(a, b, max_relative=0.1);
/// approx::assert_relative_ne!(a, b, max_relative=0.0001);
Expand Down Expand Up @@ -122,8 +127,8 @@ where
/// ```
/// use geo_types::{point, Triangle};
///
/// let a = Triangle((0.0, 0.0).into(), (10.0, 10.0).into(), (0.0, 5.0).into());
/// let b = Triangle((0.0, 0.0).into(), (10.01, 10.0).into(), (0.0, 5.0).into());
/// let a = Triangle::new((0.0, 0.0).into(), (10.0, 10.0).into(), (0.0, 5.0).into());
/// let b = Triangle::new((0.0, 0.0).into(), (10.01, 10.0).into(), (0.0, 5.0).into());
///
/// approx::abs_diff_eq!(a, b, epsilon=0.1);
/// approx::abs_diff_ne!(a, b, epsilon=0.001);
Expand Down
4 changes: 2 additions & 2 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,14 @@ mod test {

#[test]
fn area_triangle_test() {
let triangle = Triangle(
let triangle = Triangle::new(
coord! { x: 0.0, y: 0.0 },
coord! { x: 1.0, y: 0.0 },
coord! { x: 0.0, y: 1.0 },
);
assert_relative_eq!(triangle.signed_area(), 0.5);

let triangle = Triangle(
let triangle = Triangle::new(
coord! { x: 0.0, y: 0.0 },
coord! { x: 0.0, y: 1.0 },
coord! { x: 1.0, y: 0.0 },
Expand Down
12 changes: 6 additions & 6 deletions geo/src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,38 +813,38 @@ mod test {
fn triangles() {
// boring triangle
assert_eq!(
Triangle(c(0., 0.), c(3., 0.), c(1.5, 3.)).centroid(),
Triangle::new(c(0., 0.), c(3., 0.), c(1.5, 3.)).centroid(),
point!(x: 1.5, y: 1.0)
);

// flat triangle
assert_eq!(
Triangle(c(0., 0.), c(3., 0.), c(1., 0.)).centroid(),
Triangle::new(c(0., 0.), c(3., 0.), c(1., 0.)).centroid(),
point!(x: 1.5, y: 0.0)
);

// flat triangle that's not axis-aligned
assert_eq!(
Triangle(c(0., 0.), c(3., 3.), c(1., 1.)).centroid(),
Triangle::new(c(0., 0.), c(3., 3.), c(1., 1.)).centroid(),
point!(x: 1.5, y: 1.5)
);

// triangle with some repeated points
assert_eq!(
Triangle(c(0., 0.), c(0., 0.), c(1., 0.)).centroid(),
Triangle::new(c(0., 0.), c(0., 0.), c(1., 0.)).centroid(),
point!(x: 0.5, y: 0.0)
);

// triangle with all repeated points
assert_eq!(
Triangle(c(0., 0.5), c(0., 0.5), c(0., 0.5)).centroid(),
Triangle::new(c(0., 0.5), c(0., 0.5), c(0., 0.5)).centroid(),
point!(x: 0., y: 0.5)
)
}

#[test]
fn degenerate_triangle_like_ring() {
let triangle = Triangle(c(0., 0.), c(1., 1.), c(2., 2.));
let triangle = Triangle::new(c(0., 0.), c(1., 1.), c(2., 2.));
let poly: Polygon<_> = triangle.into();

let line = Line::new(c(0., 1.), c(1., 3.));
Expand Down
4 changes: 2 additions & 2 deletions geo/src/algorithm/contains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,13 @@ mod test {
// https://github.com/georust/geo/issues/473
fn triangle_contains_collinear_points() {
let origin: Coordinate<f64> = (0., 0.).into();
let tri = Triangle(origin, origin, origin);
let tri = Triangle::new(origin, origin, origin);
let pt: Point<f64> = (0., 1.23456).into();
assert!(!tri.contains(&pt));
let pt: Point<f64> = (0., 0.).into();
assert!(!tri.contains(&pt));
let origin: Coordinate<f64> = (0., 0.).into();
let tri = Triangle((1., 1.).into(), origin, origin);
let tri = Triangle::new((1., 1.).into(), origin, origin);
let pt: Point<f64> = (1., 1.).into();
assert!(!tri.contains(&pt));
let pt: Point<f64> = (0.5, 0.5).into();
Expand Down
4 changes: 2 additions & 2 deletions geo/src/algorithm/coordinate_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ mod test {

#[test]
fn test_triangle() {
let triangle = Triangle((0.0, 0.0).into(), (5.0, 10.0).into(), (10.0, 0.0).into());
let triangle = Triangle::new((0.0, 0.0).into(), (5.0, 10.0).into(), (10.0, 0.0).into());
assert_eq!(
triangle.coordinate_position(&coord! { x: 5.0, y: 5.0 }),
CoordPos::Inside
Expand All @@ -708,7 +708,7 @@ mod test {

#[test]
fn test_collection() {
let triangle = Triangle((0.0, 0.0).into(), (5.0, 10.0).into(), (10.0, 0.0).into());
let triangle = Triangle::new((0.0, 0.0).into(), (5.0, 10.0).into(), (10.0, 0.0).into());
let rect = Rect::new((0.0, 0.0), (10.0, 10.0));
let collection = GeometryCollection(vec![triangle.into(), rect.into()]);

Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/coords_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ mod test {

fn create_triangle() -> (Triangle<f64>, Vec<Coordinate<f64>>) {
(
Triangle(
Triangle::new(
coord! { x: 1., y: 2. },
coord! { x: 3., y: 4. },
coord! { x: 5., y: 6. },
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/intersects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ mod test {
let ls = line_string![(0., 0.).into(), (1., 1.).into()];
let poly = Polygon::new(LineString::from(vec![(0., 0.), (1., 1.), (1., 0.)]), vec![]);
let rect = Rect::new(coord! { x: 10., y: 20. }, coord! { x: 30., y: 10. });
let tri = Triangle(
let tri = Triangle::new(
coord! { x: 0., y: 0. },
coord! { x: 10., y: 20. },
coord! { x: 20., y: -10. },
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/lines_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ mod test {

#[test]
fn test_triangle() {
let triangle = Triangle(
let triangle = Triangle::new(
coord! { x: 0., y: 0. },
coord! { x: 1., y: 2. },
coord! { x: 2., y: 3. },
Expand Down
6 changes: 3 additions & 3 deletions geo/src/algorithm/map_coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ impl<T: CoordNum, NT: CoordNum> MapCoords<T, NT> for Triangle<T> {
let p2 = func(&self.1.x_y());
let p3 = func(&self.2.x_y());

Triangle(
Triangle::new(
coord! { x: p1.0, y: p1.1 },
coord! { x: p2.0, y: p2.1 },
coord! { x: p3.0, y: p3.1 },
Expand All @@ -559,7 +559,7 @@ impl<T: CoordNum, NT: CoordNum, E> TryMapCoords<T, NT, E> for Triangle<T> {
let p2 = func(&self.1.x_y())?;
let p3 = func(&self.2.x_y())?;

Ok(Triangle(
Ok(Triangle::new(
coord! { x: p1.0, y: p1.1 },
coord! { x: p2.0, y: p2.1 },
coord! { x: p3.0, y: p3.1 },
Expand All @@ -573,7 +573,7 @@ impl<T: CoordNum> MapCoordsInplace<T> for Triangle<T> {
let p2 = func(&self.1.x_y());
let p3 = func(&self.2.x_y());

let mut new_triangle = Triangle(
let mut new_triangle = Triangle::new(
coord! { x: p1.0, y: p1.1 },
coord! { x: p2.0, y: p2.1 },
coord! { x: p3.0, y: p3.1 },
Expand Down
6 changes: 3 additions & 3 deletions geo/src/algorithm/simplifyvw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
// Out of bounds, i.e. we're on one edge
continue;
}
let area = Triangle(
let area = Triangle::new(
orig.0[ai as usize],
orig.0[current_point as usize],
orig.0[bi as usize],
Expand Down Expand Up @@ -335,7 +335,7 @@ where
// Out of bounds, i.e. we're on one edge
continue;
}
let new = Triangle(
let new = Triangle::new(
orig.0[ai as usize],
orig.0[current_point as usize],
orig.0[bi as usize],
Expand Down Expand Up @@ -394,7 +394,7 @@ where
{
let point_a = orig[triangle.left];
let point_c = orig[triangle.right];
let bounding_rect = Triangle(
let bounding_rect = Triangle::new(
orig[triangle.left],
orig[triangle.current],
orig[triangle.right],
Expand Down

0 comments on commit 17ba0fe

Please sign in to comment.