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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use crateAbsDiffEq;
use crate;
use cratesat;
use cratesupport_map_support_map_compute_separation;
use crate;
use ComplexField;
/// Finds the best separating axis by testing all edge-edge combinations between a cuboid and a triangle (3D only).
///
/// In 3D collision detection, when a box and triangle intersect, the contact may occur along an
/// axis perpendicular to an edge from each shape. This function tests all 3×3 = 9 such axes
/// (cross products of cuboid edges with triangle edges) to find the one with maximum separation.
///
/// # Parameters
///
/// - `cube1`: The cuboid (in its local coordinate frame)
/// - `triangle2`: The triangle
/// - `pos12`: The position of the triangle relative to the cuboid
///
/// # Returns
///
/// A tuple containing:
/// - `Real`: The maximum separation found across all edge-edge axes
/// - **Positive**: Shapes are separated
/// - **Negative**: Shapes are overlapping (penetration depth)
/// - `Vector`: The axis direction that gives this separation
///
/// # The 9 Axes Tested
///
/// The function tests cross products between:
/// - 3 cuboid edge directions: X, Y, Z (aligned with cuboid axes)
/// - 3 triangle edge vectors: AB, BC, CA
///
/// This gives 3×3 = 9 potential separating axes. Each axis is normalized before testing,
/// and degenerate axes (near-zero length, indicating parallel edges) are skipped.
///
/// # Example
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::{Cuboid, Triangle};
/// use parry3d::query::sat::cuboid_triangle_find_local_separating_edge_twoway;
/// use parry3d::math::{Vector, Pose};
///
/// let cube = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
/// let triangle = Triangle::new(
/// Vector::ZERO,
/// Vector::new(1.0, 0.0, 0.0),
/// Vector::new(0.0, 1.0, 0.0)
/// );
///
/// // Position triangle near the cube
/// let pos12 = Pose::translation(2.0, 0.0, 0.0);
///
/// let (separation, axis) = cuboid_triangle_find_local_separating_edge_twoway(
/// &cube,
/// &triangle,
/// &pos12
/// );
///
/// println!("Edge-edge separation: {} along {}", separation, axis);
/// # }
/// ```
///
/// # Usage in Complete SAT
///
/// For a complete cuboid-triangle SAT test, you must also test:
/// 1. Cuboid face normals (X, Y, Z axes)
/// 2. Triangle face normal (perpendicular to the triangle plane)
/// 3. Edge-edge axes (this function)
/// Finds the best separating axis by testing the edge normals of a triangle against a support map shape (2D only).
///
/// In 2D, a triangle has three edges, each with an associated outward-pointing normal.
/// This function tests all three edge normals to find which gives the maximum separation
/// from the support map shape.
///
/// # Parameters
///
/// - `triangle1`: The triangle whose edge normals will be tested
/// - `shape2`: Any convex shape implementing [`SupportMap`]
/// - `pos12`: The position of `shape2` relative to `triangle1`
///
/// # Returns
///
/// A tuple containing:
/// - `Real`: The maximum separation found among the triangle's edge normals
/// - **Positive**: Shapes are separated
/// - **Negative**: Shapes are overlapping
/// - `Vector`: The edge normal direction that gives this separation
///
/// # 2D vs 3D
///
/// In 2D, triangles are true polygons with edges that have normals. In 3D, triangles are
/// planar surfaces with a face normal (see the 3D version of this function).
///
/// # Example
///
/// ```rust
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::shape::{Triangle, Ball};
/// use parry2d::query::sat::triangle_support_map_find_local_separating_normal_oneway;
/// use parry2d::math::{Vector, Pose};
///
/// let triangle = Triangle::new(
/// Vector::ZERO,
/// Vector::new(2.0, 0.0),
/// Vector::new(1.0, 2.0)
/// );
/// let sphere = Ball::new(0.5);
///
/// let pos12 = Pose::translation(3.0, 1.0);
///
/// let (separation, normal) = triangle_support_map_find_local_separating_normal_oneway(
/// &triangle,
/// &sphere,
/// &pos12
/// );
///
/// if separation > 0.0 {
/// println!("Separated by {} along edge normal {}", separation, normal);
/// }
/// # }
/// ```
/// Finds the best separating axis by testing a triangle's normals against a cuboid (2D only).
///
/// This is a specialized version of [`triangle_support_map_find_local_separating_normal_oneway`]
/// for the specific case of a triangle and cuboid. In 2D, it tests the three edge normals of
/// the triangle.
///
/// # Parameters
///
/// - `triangle1`: The triangle whose edge normals will be tested
/// - `shape2`: The cuboid
/// - `pos12`: The position of the cuboid relative to the triangle
///
/// # Returns
///
/// A tuple containing the maximum separation and the corresponding edge normal direction.
///
/// See [`triangle_support_map_find_local_separating_normal_oneway`] for more details and examples.
/// Finds the best separating axis by testing a triangle's face normal against a cuboid (3D only).
///
/// In 3D, a triangle is a planar surface with a single face normal (perpendicular to the plane).
/// This function tests both directions of this normal (+normal and -normal) to find the maximum
/// separation from the cuboid.
///
/// # How It Works
///
/// The function uses the triangle's face normal and one of its vertices (point A) to represent
/// the triangle as a point-with-normal. It then delegates to
/// [`point_cuboid_find_local_separating_normal_oneway`](super::point_cuboid_find_local_separating_normal_oneway)
/// which efficiently handles this case.
///
/// # Parameters
///
/// - `triangle1`: The triangle whose face normal will be tested
/// - `shape2`: The cuboid
/// - `pos12`: The position of the cuboid relative to the triangle
///
/// # Returns
///
/// A tuple containing:
/// - `Real`: The separation distance along the triangle's face normal
/// - **Positive**: Shapes are separated
/// - **Negative**: Shapes are overlapping
/// - `Vector`: The face normal direction (or its negation) that gives this separation
///
/// # Example
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::{Triangle, Cuboid};
/// use parry3d::query::sat::triangle_cuboid_find_local_separating_normal_oneway;
/// use parry3d::math::{Vector, Pose};
///
/// // Horizontal triangle in the XY plane
/// let triangle = Triangle::new(
/// Vector::ZERO,
/// Vector::new(2.0, 0.0, 0.0),
/// Vector::new(1.0, 2.0, 0.0)
/// );
/// let cube = Cuboid::new(Vector::new(1.0, 1.0, 1.0));
///
/// // Position cube above the triangle
/// let pos12 = Pose::translation(1.0, 1.0, 2.0);
///
/// let (separation, normal) = triangle_cuboid_find_local_separating_normal_oneway(
/// &triangle,
/// &cube,
/// &pos12
/// );
///
/// println!("Separation along triangle normal: {}", separation);
/// # }
/// ```
///
/// # 2D vs 3D
///
/// - **2D version**: Tests three edge normals (one per triangle edge)
/// - **3D version** (this function): Tests one face normal (perpendicular to triangle plane)