1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/// The result of a plane-splitting operation.
///
/// This enum represents the three possible outcomes when splitting a geometric shape with a plane.
/// It efficiently handles all cases without unnecessary allocations when the shape doesn't need
/// to be split.
///
/// # Type Parameter
///
/// The generic type `T` represents the shape being split. Common types include:
/// - [`Aabb`](crate::bounding_volume::Aabb) - Axis-aligned bounding box
/// - [`Segment`](crate::shape::Segment) - Line segment
/// - [`TriMesh`](crate::shape::TriMesh) - Triangle mesh
///
/// # Half-Space Definition
///
/// Given a plane defined by a normal vector `n` and bias `b`, a point `p` lies in:
/// - **Negative half-space** if `n · p < b` (behind the plane)
/// - **Positive half-space** if `n · p > b` (in front of the plane)
/// - **On the plane** if `n · p ≈ b` (within epsilon tolerance)
///
/// # Examples
///
/// ## Splitting an AABB
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::bounding_volume::Aabb;
/// use parry3d::math::Vector;
/// use parry3d::query::SplitResult;
///
/// let aabb = Aabb::new(Vector::new(0.0, 0.0, 0.0), Vector::new(10.0, 10.0, 10.0));
///
/// // Split along X-axis at x = 5.0
/// match aabb.canonical_split(0, 5.0, 1e-6) {
/// SplitResult::Pair(left, right) => {
/// println!("AABB split into two pieces");
/// println!("Left AABB: {:?}", left);
/// println!("Right AABB: {:?}", right);
/// }
/// SplitResult::Negative => {
/// println!("AABB is entirely on the negative side (x < 5.0)");
/// }
/// SplitResult::Positive => {
/// println!("AABB is entirely on the positive side (x > 5.0)");
/// }
/// }
/// # }
/// ```
///
/// ## Splitting a Segment
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::Segment;
/// use parry3d::math::Vector;
/// use parry3d::query::SplitResult;
///
/// let segment = Segment::new(Vector::new(0.0, 0.0, 0.0), Vector::new(10.0, 0.0, 0.0));
///
/// // Split along X-axis at x = 3.0
/// match segment.canonical_split(0, 3.0, 1e-6) {
/// SplitResult::Pair(seg1, seg2) => {
/// println!("Segment split at x = 3.0");
/// println!("First segment: {:?} to {:?}", seg1.a, seg1.b);
/// println!("Second segment: {:?} to {:?}", seg2.a, seg2.b);
/// }
/// SplitResult::Negative => {
/// println!("Entire segment is on the negative side");
/// }
/// SplitResult::Positive => {
/// println!("Entire segment is on the positive side");
/// }
/// }
/// # }
/// ```
/// The result of a plane-intersection operation.
///
/// This enum represents the outcome when computing the intersection between a geometric shape
/// and a plane. Unlike [`SplitResult`] which produces pieces of the original shape, this produces
/// the geometry that lies exactly on the plane (within epsilon tolerance).
///
/// # Type Parameter
///
/// The generic type `T` represents the result of the intersection. Common types include:
/// - [`Polyline`](crate::shape::Polyline) - For mesh-plane intersections, representing the
/// cross-section outline
///
/// # Use Cases
///
/// Plane-intersection operations are useful for:
/// - **Cross-sectional analysis**: Computing 2D slices of 3D geometry
/// - **Contour generation**: Finding outlines at specific heights
/// - **Visualization**: Displaying cutting planes through complex geometry
/// - **CAD/CAM**: Generating toolpaths or analyzing part geometry
///
/// # Examples
///
/// ## Computing a Mesh Cross-Section
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32", feature = "spade"))] {
/// use parry3d::shape::TriMesh;
/// use parry3d::math::Vector;
/// use parry3d::query::IntersectResult;
///
/// // Create a simple tetrahedron mesh
/// let vertices = vec![
/// Vector::new(0.0, 0.0, 0.0),
/// Vector::new(1.0, 0.0, 0.0),
/// Vector::new(0.5, 1.0, 0.0),
/// Vector::new(0.5, 0.5, 1.0),
/// ];
/// let indices = vec![
/// [0u32, 1, 2], // Bottom face
/// [0, 1, 3], // Front face
/// [1, 2, 3], // Right face
/// [2, 0, 3], // Left face
/// ];
/// let mesh = TriMesh::new(vertices, indices).unwrap();
///
/// // Compute cross-section at z = 0.5
/// match mesh.canonical_intersection_with_plane(2, 0.5, 1e-6) {
/// IntersectResult::Intersect(polyline) => {
/// println!("Cross-section computed!");
/// println!("Number of vertices: {}", polyline.vertices().len());
/// // The polyline represents the outline of the mesh at z = 0.5
/// // It may consist of multiple disconnected loops if the mesh
/// // has multiple separate pieces at this height
/// }
/// IntersectResult::Negative => {
/// println!("Mesh is entirely below z = 0.5");
/// }
/// IntersectResult::Positive => {
/// println!("Mesh is entirely above z = 0.5");
/// }
/// }
/// # }
/// ```
///
/// ## Handling Multiple Connected Components
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32", feature = "spade"))] {
/// use parry3d::shape::TriMesh;
/// use parry3d::query::IntersectResult;
///
/// # let vertices = vec![
/// # parry3d::math::Vector::ZERO,
/// # parry3d::math::Vector::new(1.0, 0.0, 0.0),
/// # parry3d::math::Vector::new(0.5, 1.0, 0.5),
/// # ];
/// # let indices = vec![[0u32, 1, 2]];
/// # let mesh = TriMesh::new(vertices, indices).unwrap();
/// // When intersecting a mesh with holes or multiple separate parts,
/// // the resulting polyline may have multiple connected components
/// match mesh.canonical_intersection_with_plane(2, 0.5, 1e-6) {
/// IntersectResult::Intersect(polyline) => {
/// // The polyline contains all intersection curves
/// // You can identify separate components by analyzing connectivity
/// // through the polyline's segment indices
/// let indices = polyline.indices();
/// println!("Number of edges in cross-section: {}", indices.len());
/// }
/// _ => println!("No intersection"),
/// }
/// # }
/// ```