bevy/crates
Matty d2ef88f5e8
Add Distribution access methods for ShapeSample trait (#13315)
Stolen from #12835. 

# Objective

Sometimes you want to sample a whole bunch of points from a shape
instead of just one. You can write your own loop to do this, but it's
really more idiomatic to use a `rand`
[`Distribution`](https://docs.rs/rand/latest/rand/distributions/trait.Distribution.html)
with the `sample_iter` method. Distributions also support other useful
things like mapping, and they are suitable as generic items for
consumption by other APIs.

## Solution

`ShapeSample` has been given two new automatic trait methods,
`interior_dist` and `boundary_dist`. They both have similar signatures
(recall that `Output` is the output type for `ShapeSample`):
```rust
fn interior_dist(self) -> impl Distribution<Self::Output>
where Self: Sized { //... }
```

These have default implementations which are powered by wrapper structs
`InteriorOf` and `BoundaryOf` that actually implement `Distribution` —
the implementations effectively just call `ShapeSample::sample_interior`
and `ShapeSample::sample_boundary` on the contained type.

The upshot is that this allows iteration as follows:
```rust
// Get an iterator over boundary points of a rectangle:
let rectangle = Rectangle::new(1.0, 2.0);
let boundary_iter = rectangle.boundary_dist().sample_iter(rng);
// Collect a bunch of boundary points at once:
let boundary_pts: Vec<Vec2> = boundary_iter.take(1000).collect();
```

Alternatively, you can use `InteriorOf`/`BoundaryOf` explicitly to
similar effect:
```rust
let boundary_pts: Vec<Vec2> = BoundaryOf(rectangle).sample_iter(rng).take(1000).collect();
```

---

## Changelog

- Added `InteriorOf` and `BoundaryOf` distribution wrapper structs in
`bevy_math::sampling::shape_sampling`.
- Added `interior_dist` and `boundary_dist` automatic trait methods to
`ShapeSample`.
- Made `shape_sampling` module public with explanatory documentation.

---

## Discussion

### Design choices

The main point of interest here is just the choice of `impl
Distribution` instead of explicitly using `InteriorOf`/`BoundaryOf`
return types for `interior_dist` and `boundary_dist`. The reason for
this choice is that it allows future optimizations for repeated sampling
— for example, instead of just wrapping the base type,
`interior_dist`/`boundary_dist` could construct auxiliary data that is
held over between sampling operations.
2024-05-22 12:38:08 +00:00
..
bevy_a11y Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_animation Remove ClampColor (#13307) 2024-05-10 13:15:56 +00:00
bevy_app Deprecate dynamic plugins (#13080) 2024-05-20 20:01:28 +00:00
bevy_asset Add more load_direct implementations (#13415) 2024-05-21 18:32:00 +00:00
bevy_audio Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_color Implement Color Operations for Color (#13285) 2024-05-14 10:15:47 +00:00
bevy_core Determine msrv for every standalone bevy_* crate. (#13211) 2024-05-13 18:26:41 +00:00
bevy_core_pipeline Make render phases render world resources instead of components. (#13277) 2024-05-21 18:23:04 +00:00
bevy_derive Deprecate dynamic plugins (#13080) 2024-05-20 20:01:28 +00:00
bevy_dev_tools #12502 Remove limit on RenderLayers. (#13317) 2024-05-16 16:15:47 +00:00
bevy_diagnostic Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_dylib Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_dynamic_plugin Deprecate dynamic plugins (#13080) 2024-05-20 20:01:28 +00:00
bevy_ecs Implement a SystemBuilder for building SystemParams (#13123) 2024-05-22 00:58:37 +00:00
bevy_encase_derive Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_gilrs Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_gizmos Inconsistent segments/resolution naming (#13438) 2024-05-21 18:42:59 +00:00
bevy_gltf fix normals computation for gltf (#13396) 2024-05-18 12:07:27 +00:00
bevy_hierarchy Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_input Separate state crate (#13216) 2024-05-09 18:06:05 +00:00
bevy_internal Separate state crate (#13216) 2024-05-09 18:06:05 +00:00
bevy_log Improve tracing layer customization (#13159) 2024-05-12 21:16:56 +00:00
bevy_macro_utils Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_math Add Distribution access methods for ShapeSample trait (#13315) 2024-05-22 12:38:08 +00:00
bevy_mikktspace Determine msrv for every standalone bevy_* crate. (#13211) 2024-05-13 18:26:41 +00:00
bevy_pbr Make render phases render world resources instead of components. (#13277) 2024-05-21 18:23:04 +00:00
bevy_ptr Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_reflect bevy_reflect: Custom attributes (#11659) 2024-05-20 19:30:21 +00:00
bevy_render Tetrahedron mesh (#13463) 2024-05-22 12:22:11 +00:00
bevy_scene Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_sprite Make render phases render world resources instead of components. (#13277) 2024-05-21 18:23:04 +00:00
bevy_state Revert "Add on_unimplemented Diagnostics to Most Public Traits" (#13413) 2024-05-17 17:00:43 +00:00
bevy_tasks multi_threaded feature rename (#12997) 2024-05-06 20:49:32 +00:00
bevy_text Add doc comments explaining the different behaviours of alignment and Anchor with text_2d (#8022) 2024-05-12 21:42:04 +00:00
bevy_time Adds doc note that Timer and Stopwatch must be progressed manually (#13441) 2024-05-20 19:46:25 +00:00
bevy_transform Use Dir3 for local axis methods in GlobalTransform (#13264) 2024-05-06 20:52:05 +00:00
bevy_ui Fix UI elements randomly not appearing after #13277. (#13462) 2024-05-21 22:06:25 +00:00
bevy_utils Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_window Ensure clean exit (#13236) 2024-05-12 15:56:01 +00:00
bevy_winit Ensure clean exit (#13236) 2024-05-12 15:56:01 +00:00