2
2
use {
3
3
super :: { Measured2d , Triangle2d } ,
4
4
alloc:: { collections:: BTreeMap , vec:: Vec } ,
5
+ core:: cmp:: Ordering ,
5
6
} ;
6
7
7
- use core:: cmp:: Ordering ;
8
-
9
8
use crate :: Vec2 ;
10
9
11
- #[ cfg_attr(
12
- not( feature = "alloc" ) ,
13
- expect( dead_code, reason = "this type is only used with the alloc feature" )
14
- ) ]
15
10
#[ derive( Debug , Clone , Copy ) ]
11
+ #[ cfg( feature = "alloc" ) ]
16
12
enum Endpoint {
17
13
Left ,
18
14
Right ,
@@ -24,22 +20,16 @@ enum Endpoint {
24
20
/// If `e1.position().x == e2.position().x` the events are ordered from bottom to top.
25
21
///
26
22
/// This is the order expected by the [`SweepLine`].
23
+ #[ cfg( feature = "alloc" ) ]
27
24
#[ derive( Debug , Clone , Copy ) ]
28
- #[ cfg_attr(
29
- not( feature = "alloc" ) ,
30
- allow( dead_code, reason = "this type is only used with the alloc feature" )
31
- ) ]
32
25
struct SweepLineEvent {
33
26
segment : Segment ,
34
27
/// Type of the vertex (left or right)
35
28
endpoint : Endpoint ,
36
29
}
37
30
31
+ #[ cfg( feature = "alloc" ) ]
38
32
impl SweepLineEvent {
39
- #[ cfg_attr(
40
- not( feature = "alloc" ) ,
41
- allow( dead_code, reason = "this type is only used with the alloc feature" )
42
- ) ]
43
33
fn position ( & self ) -> Vec2 {
44
34
match self . endpoint {
45
35
Endpoint :: Left => self . segment . left ,
@@ -48,31 +38,32 @@ impl SweepLineEvent {
48
38
}
49
39
}
50
40
41
+ #[ cfg( feature = "alloc" ) ]
51
42
impl PartialEq for SweepLineEvent {
52
43
fn eq ( & self , other : & Self ) -> bool {
53
44
self . position ( ) == other. position ( )
54
45
}
55
46
}
56
47
48
+ #[ cfg( feature = "alloc" ) ]
57
49
impl Eq for SweepLineEvent { }
58
50
51
+ #[ cfg( feature = "alloc" ) ]
59
52
impl PartialOrd for SweepLineEvent {
60
53
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
61
54
Some ( self . cmp ( other) )
62
55
}
63
56
}
64
57
58
+ #[ cfg( feature = "alloc" ) ]
65
59
impl Ord for SweepLineEvent {
66
60
fn cmp ( & self , other : & Self ) -> Ordering {
67
61
xy_order ( self . position ( ) , other. position ( ) )
68
62
}
69
63
}
70
64
71
65
/// Orders 2D points according to the order expected by the sweep line and event queue from -X to +X and then -Y to Y.
72
- #[ cfg_attr(
73
- not( feature = "alloc" ) ,
74
- allow( dead_code, reason = "this type is only used with the alloc feature" )
75
- ) ]
66
+ #[ cfg( feature = "alloc" ) ]
76
67
fn xy_order ( a : Vec2 , b : Vec2 ) -> Ordering {
77
68
a. x . total_cmp ( & b. x ) . then_with ( || a. y . total_cmp ( & b. y ) )
78
69
}
@@ -129,26 +120,31 @@ impl EventQueue {
129
120
/// Segments are ordered from bottom to top based on their left vertices if possible.
130
121
/// If their y values are identical, the segments are ordered based on the y values of their right vertices.
131
122
#[ derive( Debug , Clone , Copy ) ]
123
+ #[ cfg( feature = "alloc" ) ]
132
124
struct Segment {
133
125
edge_index : usize ,
134
126
left : Vec2 ,
135
127
right : Vec2 ,
136
128
}
137
129
130
+ #[ cfg( feature = "alloc" ) ]
138
131
impl PartialEq for Segment {
139
132
fn eq ( & self , other : & Self ) -> bool {
140
133
self . edge_index == other. edge_index
141
134
}
142
135
}
143
136
137
+ #[ cfg( feature = "alloc" ) ]
144
138
impl Eq for Segment { }
145
139
140
+ #[ cfg( feature = "alloc" ) ]
146
141
impl PartialOrd for Segment {
147
142
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
148
143
Some ( self . cmp ( other) )
149
144
}
150
145
}
151
146
147
+ #[ cfg( feature = "alloc" ) ]
152
148
impl Ord for Segment {
153
149
fn cmp ( & self , other : & Self ) -> Ordering {
154
150
self . left
@@ -159,10 +155,7 @@ impl Ord for Segment {
159
155
}
160
156
161
157
/// Holds information about which segment is above and which is below a given [`Segment`]
162
- #[ cfg_attr(
163
- not( feature = "alloc" ) ,
164
- expect( dead_code, reason = "this type is only used with the alloc feature" )
165
- ) ]
158
+ #[ cfg( feature = "alloc" ) ]
166
159
#[ derive( Debug , Clone , Copy ) ]
167
160
struct SegmentOrder {
168
161
above : Option < usize > ,
@@ -173,8 +166,8 @@ struct SegmentOrder {
173
166
///
174
167
/// 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
175
168
/// the sweep line is intersecting at any given moment.
176
- #[ cfg( feature = "alloc" ) ]
177
169
#[ derive( Debug , Clone ) ]
170
+ #[ cfg( feature = "alloc" ) ]
178
171
struct SweepLine < ' a > {
179
172
vertices : & ' a [ Vec2 ] ,
180
173
tree : BTreeMap < Segment , SegmentOrder > ,
0 commit comments