Skip to content

Commit

Permalink
Lower trait bounds on Ball and Enclosing.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3vu0r committed Mar 22, 2024
1 parent ac1f0ec commit c89b6d4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "miniball"
description = "Minimum enclosing ball"
authors = ["Rouven Spreckels <[email protected]>"]
version = "0.3.0"
version = "0.3.1"
rust-version = "1.61.0"
edition = "2021"
documentation = "https://docs.rs/miniball"
Expand Down
6 changes: 5 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Version 0.3.1 (2024-03-22)

* Lower trait bounds on `Ball` and `Enclosing`.

# Version 0.3.0 (2024-03-17)

* Replace `Point` with `OPoint` supporting static as well as dynamic dimensions.
* Replace `Point` with `OPoint` supporting arithmetic at compile-time.
* Replace `ArrayVec<T, { D + 1 }>` with `OVec<T, DimNameSum<D, U1>>` supporting stabe Rust.

# Version 0.2.0 (2023-04-01)
Expand Down
18 changes: 9 additions & 9 deletions src/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use super::Enclosing;
use nalgebra::{
base::allocator::Allocator, DefaultAllocator, DimNameAdd, DimNameSum, OMatrix, OPoint, OVector,
RealField, U1,
base::allocator::Allocator, DefaultAllocator, DimName, OMatrix, OPoint, OVector, RealField,
};

/// Ball over real field `R` of dimension `D` with center and radius squared.
#[derive(Debug, Clone, PartialEq)]
pub struct Ball<R: RealField, D: DimNameAdd<U1>>
pub struct Ball<R: RealField, D: DimName>
where
DefaultAllocator: Allocator<R, D>,
{
Expand All @@ -22,24 +21,25 @@ where
pub radius_squared: R,
}

impl<R: RealField + Copy, D: DimNameAdd<U1>> Copy for Ball<R, D>
impl<R: RealField + Copy, D: DimName> Copy for Ball<R, D>
where
OPoint<R, D>: Copy,
DefaultAllocator: Allocator<R, D>,
{
}

impl<R: RealField, D: DimNameAdd<U1>> Enclosing<R, D> for Ball<R, D>
impl<R: RealField, D: DimName> Enclosing<R, D> for Ball<R, D>
where
DefaultAllocator:
Allocator<R, D> + Allocator<R, D, D> + Allocator<OPoint<R, D>, DimNameSum<D, U1>>,
<DefaultAllocator as Allocator<OPoint<R, D>, DimNameSum<D, U1>>>::Buffer: Default,
DefaultAllocator: Allocator<R, D>,
{
#[inline]
fn contains(&self, point: &OPoint<R, D>) -> bool {
(point - &self.center).norm_squared() <= self.radius_squared
}
fn with_bounds(bounds: &[OPoint<R, D>]) -> Option<Self> {
fn with_bounds(bounds: &[OPoint<R, D>]) -> Option<Self>
where
DefaultAllocator: Allocator<R, D, D>,
{
let length = bounds.len().checked_sub(1).filter(|&len| len <= D::USIZE)?;
let points = OMatrix::<R, D, D>::from_fn(|row, column| {
if column < length {
Expand Down
26 changes: 19 additions & 7 deletions src/enclosing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use super::{Deque, OVec};
use nalgebra::{
base::allocator::Allocator, DefaultAllocator, DimNameAdd, DimNameSum, OPoint, RealField, U1,
base::allocator::Allocator, DefaultAllocator, DimName, DimNameAdd, DimNameSum, OPoint,
RealField, U1,
};
use stacker::maybe_grow;
use std::mem::size_of;

/// Minimum enclosing ball.
pub trait Enclosing<R: RealField, D: DimNameAdd<U1>>
pub trait Enclosing<R: RealField, D: DimName>
where
Self: Clone,
DefaultAllocator: Allocator<R, D> + Allocator<OPoint<R, D>, DimNameSum<D, U1>>,
<DefaultAllocator as Allocator<OPoint<R, D>, DimNameSum<D, U1>>>::Buffer: Default,
DefaultAllocator: Allocator<R, D>,
{
#[doc(hidden)]
/// Guaranteed stack size per recursion step.
Expand Down Expand Up @@ -59,7 +59,9 @@ where
/// assert_eq!(radius_squared, 3.0);
/// ```
#[must_use]
fn with_bounds(bounds: &[OPoint<R, D>]) -> Option<Self>;
fn with_bounds(bounds: &[OPoint<R, D>]) -> Option<Self>
where
DefaultAllocator: Allocator<R, D, D>;

/// Returns minimum ball enclosing `points`.
///
Expand Down Expand Up @@ -130,7 +132,12 @@ where
/// ```
#[must_use]
#[inline]
fn enclosing_points(points: &mut impl Deque<OPoint<R, D>>) -> Self {
fn enclosing_points(points: &mut impl Deque<OPoint<R, D>>) -> Self
where
D: DimNameAdd<U1>,
DefaultAllocator: Allocator<R, D, D> + Allocator<OPoint<R, D>, DimNameSum<D, U1>>,
<DefaultAllocator as Allocator<OPoint<R, D>, DimNameSum<D, U1>>>::Buffer: Default,
{
maybe_grow(Self::RED_ZONE, Self::STACK_SIZE, || {
Self::enclosing_points_with_bounds(
points,
Expand All @@ -147,7 +154,12 @@ where
fn enclosing_points_with_bounds(
points: &mut impl Deque<OPoint<R, D>>,
bounds: &mut OVec<OPoint<R, D>, DimNameSum<D, U1>>,
) -> Option<Self> {
) -> Option<Self>
where
D: DimNameAdd<U1>,
DefaultAllocator: Allocator<R, D, D> + Allocator<OPoint<R, D>, DimNameSum<D, U1>>,
<DefaultAllocator as Allocator<OPoint<R, D>, DimNameSum<D, U1>>>::Buffer: Default,
{
// Take point from back.
if let Some(point) = points.pop_back().filter(|_| !bounds.is_full()) {
let ball = maybe_grow(Self::RED_ZONE, Self::STACK_SIZE, || {
Expand Down
1 change: 0 additions & 1 deletion src/ovec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ where
/// Panics if [`Self::is_full()`].
#[inline]
pub fn push(&mut self, item: T) {
assert!(!self.is_full());
self.data[self.size] = item;
self.size += 1;
}
Expand Down

0 comments on commit c89b6d4

Please sign in to comment.