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

Make geo-types Rect fields private to force users to use constructor #374

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ mod test {

#[test]
fn test_rects() {
let r = Rect {
min: Coordinate { x: -1., y: -1. },
max: Coordinate { x: 1., y: 1. },
};
let r = Rect::new(
Coordinate { x: -1., y: -1. },
Coordinate { x: 1., y: 1. },
);
let p: Polygon<_> = r.into();
assert_eq!(
p,
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where

fn envelope(&self) -> Self::Envelope {
let bounding_rect = crate::private_utils::line_bounding_rect(*self);
::rstar::AABB::from_corners(bounding_rect.min.into(), bounding_rect.max.into())
::rstar::AABB::from_corners(bounding_rect.min().into(), bounding_rect.max().into())
}
}

Expand Down
4 changes: 2 additions & 2 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ where
Point::new(Bounded::max_value(), Bounded::max_value()),
),
Some(b) => ::rstar::AABB::from_corners(
Point::new(b.min.x, b.min.y),
Point::new(b.max.x, b.max.y),
Point::new(b.min().x, b.min().y),
Point::new(b.max().x, b.max().y),
),
}
}
Expand Down
10 changes: 5 additions & 5 deletions geo-types/src/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,11 @@ impl<T: CoordinateType> From<Rect<T>> for Polygon<T> {
fn from(r: Rect<T>) -> Polygon<T> {
Polygon::new(
vec![
(r.min.x, r.min.y),
(r.max.x, r.min.y),
(r.max.x, r.max.y),
(r.min.x, r.max.y),
(r.min.x, r.min.y),
(r.min().x, r.min().y),
(r.max().x, r.min().y),
(r.max().x, r.max().y),
(r.min().x, r.max().y),
(r.min().x, r.min().y),
]
.into(),
Vec::new(),
Expand Down
48 changes: 38 additions & 10 deletions geo-types/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct Rect<T>
where
T: CoordinateType,
{
pub min: Coordinate<T>,
pub max: Coordinate<T>,
min: Coordinate<T>,
max: Coordinate<T>,
}

impl<T: CoordinateType> Rect<T> {
Expand All @@ -30,29 +30,57 @@ impl<T: CoordinateType> Rect<T> {
/// Coordinate { x: 10., y: 20. },
/// );
///
/// assert_eq!(rect.min, Coordinate { x: 0., y: 0. });
/// assert_eq!(rect.max, Coordinate { x: 10., y: 20. });
/// assert_eq!(rect.min(), Coordinate { x: 0., y: 0. });
/// assert_eq!(rect.max(), Coordinate { x: 10., y: 20. });
/// ```
pub fn new<C>(min: C, max: C) -> Rect<T>
where
C: Into<Coordinate<T>>,
{
let (min, max) = (min.into(), max.into());

assert!(
min.x <= max.x && min.y <= max.y,
"Failed to create the Rect type: 'min' coordinate's x/y value must be smaller or equal to the 'max' x/y value"
);
Self::assert_valid_bounds(min, max);

Rect { min, max }
}

pub fn min(self) -> Coordinate<T> {
self.min
}

pub fn set_min<C>(&mut self, min: C)
where
C: Into<Coordinate<T>>,
{
self.min = min.into();
Self::assert_valid_bounds(self.min, self.max);
}

pub fn max(self) -> Coordinate<T> {
self.max
}

pub fn set_max<C>(&mut self, max: C)
where
C: Into<Coordinate<T>>,
{
self.max = max.into();
Self::assert_valid_bounds(self.min, self.max);
}

pub fn width(self) -> T {
self.max.x - self.min.x
self.max().x - self.min().x
}

pub fn height(self) -> T {
self.max.y - self.min.y
self.max().y - self.min().y
}

fn assert_valid_bounds(min: Coordinate<T>, max: Coordinate<T>) {
assert!(
min.x <= max.x && min.y <= max.y,
"Failed to create the Rect type: 'min' coordinate's x/y value must be smaller or equal to the 'max' x/y value"
);
}
}

Expand Down
18 changes: 9 additions & 9 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
T: CoordinateType,
{
fn area(&self) -> T {
(self.max.x - self.min.x) * (self.max.y - self.min.y)
self.width() * self.height()
}
}

Expand Down Expand Up @@ -126,16 +126,16 @@ mod test {
}
#[test]
fn rectangle_test() {
let rect1: Rect<f32> = Rect {
min: Coordinate { x: 10., y: 30. },
max: Coordinate { x: 20., y: 40. },
};
let rect1: Rect<f32> = Rect::new(
Coordinate { x: 10., y: 30. },
Coordinate { x: 20., y: 40. },
);
assert_relative_eq!(rect1.area(), 100.);

let rect2: Rect<i32> = Rect {
min: Coordinate { x: 10, y: 30 },
max: Coordinate { x: 20, y: 40 },
};
let rect2: Rect<i32> = Rect::new(
Coordinate { x: 10, y: 30 },
Coordinate { x: 20, y: 40 },
);
assert_eq!(rect2.area(), 100);
}
#[test]
Expand Down
72 changes: 36 additions & 36 deletions geo/src/algorithm/bounding_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub trait BoundingRect<T: CoordinateType> {
/// let linestring = LineString::from(vec);
/// let bounding_rect = linestring.bounding_rect().unwrap();
///
/// assert_eq!(40.02f64, bounding_rect.min.x);
/// assert_eq!(42.02f64, bounding_rect.max.x);
/// assert_eq!(116.34, bounding_rect.min.y);
/// assert_eq!(118.34, bounding_rect.max.y);
/// assert_eq!(40.02f64, bounding_rect.min().x);
/// assert_eq!(42.02f64, bounding_rect.max().x);
/// assert_eq!(116.34, bounding_rect.min().y);
/// assert_eq!(118.34, bounding_rect.max().y);
/// ```
///
fn bounding_rect(&self) -> Self::Output;
Expand Down Expand Up @@ -57,10 +57,10 @@ where
let b = self.end;
let (xmin, xmax) = if a.x <= b.x { (a.x, b.x) } else { (b.x, a.x) };
let (ymin, ymax) = if a.y <= b.y { (a.y, b.y) } else { (b.y, a.y) };
Rect {
min: Coordinate { x: xmin, y: ymin },
max: Coordinate { x: xmax, y: ymax },
}
Rect::new(
Coordinate { x: xmin, y: ymin },
Coordinate { x: xmax, y: ymax },
)
}
}

Expand Down Expand Up @@ -165,16 +165,16 @@ mod test {
#[test]
fn linestring_one_point_test() {
let linestring = line_string![(x: 40.02f64, y: 116.34)];
let bounding_rect = Rect {
min: Coordinate {
let bounding_rect = Rect::new(
Coordinate {
x: 40.02f64,
y: 116.34,
},
max: Coordinate {
Coordinate {
x: 40.02,
y: 116.34,
},
};
);
assert_eq!(bounding_rect, linestring.bounding_rect().unwrap());
}
#[test]
Expand All @@ -185,10 +185,10 @@ mod test {
(x: -3., y: -3.),
(x: -4., y: 4.)
];
let bounding_rect = Rect {
min: Coordinate { x: -4., y: -3. },
max: Coordinate { x: 2., y: 4. },
};
let bounding_rect = Rect::new(
Coordinate { x: -4., y: -3. },
Coordinate { x: 2., y: 4. },
);
assert_eq!(bounding_rect, linestring.bounding_rect().unwrap());
}
#[test]
Expand All @@ -199,19 +199,19 @@ mod test {
line_string![(x: 1., y: 1.), (x: 1., y: -60.)],
line_string![(x: 1., y: 1.), (x: 1., y: 70.)],
]);
let bounding_rect = Rect {
min: Coordinate { x: -40., y: -60. },
max: Coordinate { x: 50., y: 70. },
};
let bounding_rect = Rect::new(
Coordinate { x: -40., y: -60. },
Coordinate { x: 50., y: 70. },
);
assert_eq!(bounding_rect, multiline.bounding_rect().unwrap());
}
#[test]
fn multipoint_test() {
let multipoint = MultiPoint::from(vec![(1., 1.), (2., -2.), (-3., -3.), (-4., 4.)]);
let bounding_rect = Rect {
min: Coordinate { x: -4., y: -3. },
max: Coordinate { x: 2., y: 4. },
};
let bounding_rect = Rect::new(
Coordinate { x: -4., y: -3. },
Coordinate { x: 2., y: 4. },
);
assert_eq!(bounding_rect, multipoint.bounding_rect().unwrap());
}
#[test]
Expand All @@ -234,10 +234,10 @@ mod test {
polygon![(x: 0., y: 0.), (x: 5., y: 0.), (x: 0., y: 80.), (x: 0., y: 0.)],
polygon![(x: 0., y: 0.), (x: -60., y: 0.), (x: 0., y: 6.), (x: 0., y: 0.)],
]);
let bounding_rect = Rect {
min: Coordinate { x: -60., y: -70. },
max: Coordinate { x: 50., y: 80. },
};
let bounding_rect = Rect::new(
Coordinate { x: -60., y: -70. },
Coordinate { x: 50., y: 80. },
);
assert_eq!(bounding_rect, mpoly.bounding_rect().unwrap());
}
#[test]
Expand All @@ -246,17 +246,17 @@ mod test {
let line2 = Line::new(Coordinate { x: 2., y: 3. }, Coordinate { x: 0., y: 1. });
assert_eq!(
line1.bounding_rect(),
Rect {
min: Coordinate { x: 0., y: 1. },
max: Coordinate { x: 2., y: 3. },
}
Rect::new(
Coordinate { x: 0., y: 1. },
Coordinate { x: 2., y: 3. },
)
);
assert_eq!(
line2.bounding_rect(),
Rect {
min: Coordinate { x: 0., y: 1. },
max: Coordinate { x: 2., y: 3. },
}
Rect::new(
Coordinate { x: 0., y: 1. },
Coordinate { x: 2., y: 3. },
)
);
}
}
12 changes: 6 additions & 6 deletions geo/src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ where
fn centroid(&self) -> Self::Output {
let two = T::one() + T::one();
Point::new(
(self.max.x + self.min.x) / two,
(self.max.y + self.min.y) / two,
(self.max().x + self.min().x) / two,
(self.max().y + self.min().y) / two,
)
}
}
Expand Down Expand Up @@ -533,10 +533,10 @@ mod test {
}
#[test]
fn bounding_rect_test() {
let bounding_rect = Rect {
min: Coordinate { x: 0., y: 50. },
max: Coordinate { x: 4., y: 100. },
};
let bounding_rect = Rect::new(
Coordinate { x: 0., y: 50. },
Coordinate { x: 4., y: 100. },
);
let point = Point(Coordinate { x: 2., y: 75. });
assert_eq!(point, bounding_rect.centroid());
}
Expand Down
42 changes: 21 additions & 21 deletions geo/src/algorithm/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ where
T: CoordinateType,
{
fn contains(&self, p: &Point<T>) -> bool {
p.x() >= self.min.x && p.x() <= self.max.x && p.y() >= self.min.y && p.y() <= self.max.y
p.x() >= self.min().x && p.x() <= self.max().x && p.y() >= self.min().y && p.y() <= self.max().y
}
}

Expand All @@ -243,10 +243,10 @@ where
{
fn contains(&self, bounding_rect: &Rect<T>) -> bool {
// All points of LineString must be in the polygon ?
self.min.x <= bounding_rect.min.x
&& self.max.x >= bounding_rect.max.x
&& self.min.y <= bounding_rect.min.y
&& self.max.y >= bounding_rect.max.y
self.min().x <= bounding_rect.min().x
&& self.max().x >= bounding_rect.max().x
&& self.min().y <= bounding_rect.min().y
&& self.max().y >= bounding_rect.max().y
}
}

Expand Down Expand Up @@ -498,14 +498,14 @@ mod test {
}
#[test]
fn bounding_rect_in_inner_bounding_rect_test() {
let bounding_rect_xl = Rect {
min: Coordinate { x: -100., y: -200. },
max: Coordinate { x: 100., y: 200. },
};
let bounding_rect_sm = Rect {
min: Coordinate { x: -10., y: -20. },
max: Coordinate { x: 10., y: 20. },
};
let bounding_rect_xl = Rect::new(
Coordinate { x: -100., y: -200. },
Coordinate { x: 100., y: 200. },
);
let bounding_rect_sm = Rect::new(
Coordinate { x: -10., y: -20. },
Coordinate { x: 10., y: 20. },
);
assert_eq!(true, bounding_rect_xl.contains(&bounding_rect_sm));
assert_eq!(false, bounding_rect_sm.contains(&bounding_rect_xl));
}
Expand Down Expand Up @@ -594,17 +594,17 @@ mod test {
#[test]
fn integer_bounding_rects() {
let p: Point<i32> = Point::new(10, 20);
let bounding_rect: Rect<i32> = Rect {
min: Coordinate { x: 0, y: 0 },
max: Coordinate { x: 100, y: 100 },
};
let bounding_rect: Rect<i32> = Rect::new(
Coordinate { x: 0, y: 0 },
Coordinate { x: 100, y: 100 },
);
assert!(bounding_rect.contains(&p));
assert!(!bounding_rect.contains(&Point::new(-10, -10)));

let smaller_bounding_rect: Rect<i32> = Rect {
min: Coordinate { x: 10, y: 10 },
max: Coordinate { x: 20, y: 20 },
};
let smaller_bounding_rect: Rect<i32> = Rect::new(
Coordinate { x: 10, y: 10 },
Coordinate { x: 20, y: 20 },
);
assert!(bounding_rect.contains(&smaller_bounding_rect));
}

Expand Down
Loading