Skip to content

Commit

Permalink
Feature gate is_polygon_simple behind the alloc feature. (#16739)
Browse files Browse the repository at this point in the history
CI was failing because `bevy_math` no longer compiled with `libcore`.
This was due to PR #15981. This commit fixes the issue by moving the
applicable functionality behind `#[cfg(feature = "alloc")]`.
  • Loading branch information
pcwalton authored Dec 10, 2024
1 parent 7662b4f commit bb090e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use core::f32::consts::{FRAC_1_SQRT_2, FRAC_PI_2, FRAC_PI_3, PI};
use derive_more::derive::From;
use thiserror::Error;

use super::{polygon::is_polygon_simple, Measured2d, Primitive2d, WindingOrder};
use super::{Measured2d, Primitive2d, WindingOrder};
use crate::{
ops::{self, FloatPow},
Dir2, Vec2,
};

#[cfg(feature = "alloc")]
use super::polygon::is_polygon_simple;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
Expand Down Expand Up @@ -1634,6 +1637,7 @@ impl<const N: usize> Polygon<N> {
///
/// A polygon is simple if it is not self intersecting and not self tangent.
/// As such, no two edges of the polygon may cross each other and each vertex must not lie on another edge.
#[cfg(feature = "alloc")]
pub fn is_simple(&self) -> bool {
is_polygon_simple(&self.vertices)
}
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_math/src/primitives/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate alloc;
use alloc::collections::BTreeMap;
#[cfg(feature = "alloc")]
use alloc::{collections::BTreeMap, vec::Vec};

use core::cmp::Ordering;

use crate::Vec2;
Expand Down Expand Up @@ -58,10 +59,12 @@ fn xy_order(a: Vec2, b: Vec2) -> Ordering {
}

/// The event queue holds an ordered list of all events the [`SweepLine`] will encounter when checking the current polygon.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
struct EventQueue {
events: Vec<SweepLineEvent>,
}
#[cfg(feature = "alloc")]
impl EventQueue {
/// Initialize a new `EventQueue` with all events from the polygon represented by `vertices`.
///
Expand Down Expand Up @@ -143,11 +146,13 @@ struct SegmentOrder {
///
/// It can be thought of as a vertical line sweeping from -X to +X across the polygon that keeps track of the order of the segments
/// the sweep line is intersecting at any given moment.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)]
struct SweepLine<'a> {
vertices: &'a [Vec2],
tree: BTreeMap<Segment, SegmentOrder>,
}
#[cfg(feature = "alloc")]
impl<'a> SweepLine<'a> {
fn new(vertices: &'a [Vec2]) -> Self {
Self {
Expand Down Expand Up @@ -253,6 +258,7 @@ fn point_side(p1: Vec2, p2: Vec2, q: Vec2) -> f32 {
///
/// The algorithm used is the Shamos-Hoey algorithm, a version of the Bentley-Ottman algorithm adapted to only detect whether any intersections exist.
/// This function will run in O(n * log n)
#[cfg(feature = "alloc")]
pub fn is_polygon_simple(vertices: &[Vec2]) -> bool {
if vertices.len() < 3 {
return true;
Expand Down

0 comments on commit bb090e6

Please sign in to comment.