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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Traits for support mapping based shapes.
//!
//! # What is a Support Map?
//!
//! A **support map** (or support function) is a fundamental concept in computational geometry that
//! describes a convex shape by answering a simple question: "What point on the shape is furthest
//! in a given direction?"
//!
//! More formally, for a convex shape `S` and a direction vector `d`, the support function returns:
//!
//! ```text
//! support(S, d) = argmax { p · d : p ∈ S }
//! ```
//!
//! Where `p · d` is the dot product between a point `p` on the shape and the direction `d`.
//!
//! ## Visual Intuition
//!
//! Imagine shining a light from infinity in direction `d` onto your shape. The support point
//! is where the "shadow" boundary would be - the point that sticks out furthest in that direction.
//!
//! For example, for a circle centered at the origin with radius `r`:
//! - If `d = (1, 0)` (pointing right), the support point is `(r, 0)` (rightmost point)
//! - If `d = (0, 1)` (pointing up), the support point is `(0, r)` (topmost point)
//! - If `d = (1, 1)` (diagonal), the support point is `(r/√2, r/√2)` (northeast point)
//!
//! ## Why Support Maps Matter for Collision Detection
//!
//! Support maps are the foundation of two powerful collision detection algorithms:
//!
//! ### 1. GJK Algorithm (Gilbert-Johnson-Keerthi)
//!
//! GJK is an iterative algorithm that determines:
//! - Whether two convex shapes intersect
//! - The distance between two separated shapes
//! - The closest points between two shapes
//!
//! GJK works by computing the **Minkowski difference** of two shapes using only their support
//! functions. It builds a simplex (a simple polytope) that converges toward the origin, allowing
//! it to answer collision queries without ever explicitly computing the shapes' geometry.
//!
//! **Key advantage**: GJK only needs the support function - it never needs to know the actual
//! vertices, faces, or internal structure of the shapes. This makes it incredibly flexible and
//! efficient.
//!
//! ### 2. EPA Algorithm (Expanding Polytope Algorithm)
//!
//! EPA is used when two shapes are penetrating (overlapping). It computes:
//! - The penetration depth (how much they overlap)
//! - The penetration normal (the direction to separate them)
//! - Contact points for physics simulation
//!
//! EPA starts with the final simplex from GJK and expands it into a polytope that approximates
//! the Minkowski difference, converging toward the shallowest penetration.
//!
//! ## Why Support Maps are Efficient
//!
//! 1. **Simple to implement**: For most convex shapes, the support function is straightforward
//! 2. **No geometry storage**: Implicit shapes (like spheres, capsules) don't need vertex data
//! 3. **Transform-friendly**: Easy to handle rotations and translations
//! 4. **Composable**: Can combine support functions for compound shapes
//! 5. **Fast queries**: Often just a few dot products and comparisons
//!
//! ## Examples of Support Functions
//!
//! Here are some common shapes and their support functions:
//!
//! ### Sphere/Ball
//! ```text
//! support(sphere, d) = center + normalize(d) * radius
//! ```
//!
//! ### Cuboid (Box)
//! ```text
//! support(box, d) = (sign(d.x) * half_width,
//! sign(d.y) * half_height,
//! sign(d.z) * half_depth)
//! ```
//!
//! ### Convex Polygon/Polyhedron
//! ```text
//! support(poly, d) = vertex with maximum dot product with d
//! ```
//!
//! ## Limitations
//!
//! Support maps only work for **convex** shapes. Concave shapes must be decomposed into
//! convex parts or handled with different algorithms. This is why Parry provides composite
//! shapes and specialized algorithms for triangle meshes.
use crate;
/// Trait for convex shapes representable by a support mapping function.
///
/// A support map is a function that returns the point on a shape that is furthest in a given
/// direction. This is the fundamental building block for collision detection algorithms like
/// GJK (Gilbert-Johnson-Keerthi) and EPA (Expanding Polytope Algorithm).
///
/// # What You Need to Know
///
/// If you're implementing this trait for a custom shape, you only need to implement
/// [`local_support_point`](SupportMap::local_support_point). The other methods have default
/// implementations that handle transformations and normalized directions.
///
/// # Requirements
///
/// - The shape **must be convex**. Non-convex shapes will produce incorrect results.
/// - The support function should return a point on the surface of the shape (or inside it,
/// but surface points are preferred for better accuracy).
/// - For a given direction `d`, the returned point `p` should maximize `p · d` (dot product).
///
/// # Examples
///
/// ## Using Support Maps for Distance Queries
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::{Ball, Cuboid, SupportMap};
/// use parry3d::math::{Vector, Real};
///
/// // Create a ball (sphere) with radius 1.0
/// let ball = Ball::new(1.0);
///
/// // Get the support point in the direction (1, 0, 0) - pointing right
/// let dir = Vector::new(1.0, 0.0, 0.0);
/// let support_point = ball.local_support_point(dir);
///
/// // For a ball centered at origin, this should be approximately (1, 0, 0)
/// assert!((support_point.x - 1.0).abs() < 1e-6);
/// assert!(support_point.y.abs() < 1e-6);
/// assert!(support_point.z.abs() < 1e-6);
///
/// // Try another direction - diagonal up and right
/// let dir2 = Vector::new(1.0, 1.0, 0.0);
/// let support_point2 = ball.local_support_point(dir2);
///
/// // The point should be on the surface of the ball (distance = radius)
/// let distance = (support_point2.length() - 1.0).abs();
/// assert!(distance < 1e-6);
/// # }
/// ```
///
/// ## Support Vectors on a Cuboid
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::{Cuboid, SupportMap};
/// use parry3d::math::Vector;
///
/// // Create a cuboid (box) with half-extents 2x3x4
/// let cuboid = Cuboid::new(Vector::new(2.0, 3.0, 4.0));
///
/// // Support point in positive X direction should be at the right face
/// let dir_x = Vector::new(1.0, 0.0, 0.0);
/// let support_x = cuboid.local_support_point(dir_x);
/// assert!((support_x.x - 2.0).abs() < 1e-6);
///
/// // Support point in negative Y direction should be at the bottom face
/// let dir_neg_y = Vector::new(0.0, -1.0, 0.0);
/// let support_neg_y = cuboid.local_support_point(dir_neg_y);
/// assert!((support_neg_y.y + 3.0).abs() < 1e-6);
///
/// // Support point in diagonal direction should be at a corner
/// let dir_diag = Vector::new(1.0, 1.0, 1.0);
/// let support_diag = cuboid.local_support_point(dir_diag);
/// assert!((support_diag.x - 2.0).abs() < 1e-6);
/// assert!((support_diag.y - 3.0).abs() < 1e-6);
/// assert!((support_diag.z - 4.0).abs() < 1e-6);
/// # }
/// ```
///
/// ## Implementing SupportMap for a Custom Shape
///
/// Here's how you might implement `SupportMap` for a simple custom shape:
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # // Note: This example shows the concept but won't actually compile in doc tests
/// # // since we can't implement traits for external types in doc tests.
/// # // It's here for educational purposes.
/// use parry3d::shape::SupportMap;
/// use parry3d::math::{Vector, Real};
///
/// // A simple pill-shaped object aligned with the X axis
/// struct SimplePill {
/// half_length: Real, // Half the length of the cylindrical part
/// radius: Real, // Radius of the spherical ends
/// }
///
/// impl SupportMap for SimplePill {
/// fn local_support_point(&self, dir: Vector) -> Vector {
/// // Support point is on one of the spherical ends
/// // Choose the end that's in the direction of dir.x
/// let center_x = if dir.x >= 0.0 { self.half_length } else { -self.half_length };
///
/// // From that center, extend by radius in the direction of dir
/// let dir_normalized = dir.normalize();
/// Vector::new(
/// center_x + dir_normalized.x * self.radius,
/// dir_normalized.y * self.radius,
/// dir_normalized.z * self.radius,
/// )
/// }
/// }
/// # }
/// ```