Skip to content

Commit

Permalink
Possible fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Oct 9, 2023
1 parent 65199e3 commit 745ce09
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions crates/pathing/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ impl SegmentInterval {
let b = RayProjection::calculate(ray_b, target);

let side = which_side(ray_a.origin, ray_a.point_at(1.), ray_b.point_at(1.));
debug_assert!(side != Side::Straight || ray_a.dir.dot(&ray_b.dir) < 0.);
SegmentProjection::new(a, b, side)
// TODO: for some reason this assert fails due to the ray origin and
// self.segment lying on a line.
// debug_assert!(side != Side::Straight || ray_a.dir.dot(&ray_b.dir) < 0.);
SegmentProjection::new(a, b, side, target.length())
}

/// Returns a ray with a given origin and pointing towards the endpoint a
Expand Down Expand Up @@ -197,11 +199,19 @@ pub(crate) struct SegmentProjection {
a: RayProjection,
b: RayProjection,
ray_b_side: Side,
// TODO rename (everywhere to scale?)
size: f32,
}

impl SegmentProjection {
fn new(a: RayProjection, b: RayProjection, ray_b_side: Side) -> Self {
Self { a, b, ray_b_side }
// TODO docs
fn new(a: RayProjection, b: RayProjection, ray_b_side: Side, size: f32) -> Self {
Self {
a,
b,
ray_b_side,
size,
}
}

// TODO document
Expand All @@ -217,7 +227,7 @@ impl SegmentProjection {
0.
};

ParamPair::ordered(first, second)
ParamPair::ordered(first, second, self.size)
}

// TODO document
Expand All @@ -233,7 +243,7 @@ impl SegmentProjection {
0.
};

ParamPair::ordered(first, second)
ParamPair::ordered(first, second, self.size)
}

// TODO document
Expand All @@ -243,7 +253,7 @@ impl SegmentProjection {
}

match (self.a.parameter(), self.b.parameter()) {
(Some(a), Some(b)) => ParamPair::ordered(a, b),
(Some(a), Some(b)) => ParamPair::ordered(a, b, self.size),
(None, None) => {
if self.a.endpoint_a_side() == self.b.endpoint_a_side() {
None
Expand All @@ -257,7 +267,7 @@ impl SegmentProjection {
} else {
0.
};
ParamPair::ordered(first, second)
ParamPair::ordered(first, second, self.size)
}
}
}
Expand All @@ -267,24 +277,27 @@ impl SegmentProjection {
pub(crate) struct ParamPair(f32, f32);

impl ParamPair {
fn round(parameter: f32) -> f32 {
// TODO use constants

// TODO docs
fn round(parameter: f32, size: f32) -> f32 {
// Due to the nature of the algorithm, the ray and the segment
// frequently intersect near one of the endpoints. To avoid rounding issues,
if parameter < 0.0001 {

let scaled = parameter * size;

// TODO use constants (negligible distance)
if parameter < 0.01 {
0.
} else if parameter > 0.9999 {
} else if parameter > 0.99 {
1.
} else {
parameter
}
}

// TODO document
fn ordered(a: f32, b: f32) -> Option<Self> {
let a = Self::round(a);
let b = Self::round(b);
fn ordered(a: f32, b: f32, size: f32) -> Option<Self> {
let a = Self::round(a, size);
let b = Self::round(b, size);

if a < b {
Some(Self::new(a, b))
Expand Down

0 comments on commit 745ce09

Please sign in to comment.