MCPcopy Index your code
hub / github.com/amethyst/bracket-lib / Algorithm3D

Interface Algorithm3D

bracket-algorithm-traits/src/algorithm3d.rs:21–62  ·  view source on GitHub ↗

Implement these for handling conversion to/from 2D coordinates (they are separate, because you might want Dwarf Fortress style 3D!)

Source from the content-addressed store, hash-verified

19/// Implement these for handling conversion to/from 2D coordinates (they are separate, because you might
20/// want Dwarf Fortress style 3D!)
21pub trait Algorithm3D: BaseMap {
22 /// Convert a Point (x/y) to an array index. Defaults to a Z, Y, X striding.
23 #[allow(clippy::cast_sign_loss)]
24 fn point3d_to_index(&self, pt: Point3) -> usize {
25 let bounds = self.dimensions();
26 ((pt.z * (bounds.x * bounds.y)) + (pt.y * bounds.x) + pt.x) as usize
27 }
28
29 /// Convert an array index to a point.
30 #[allow(clippy::cast_possible_wrap)]
31 #[allow(clippy::cast_possible_truncation)]
32 fn index_to_point3d(&self, idx: usize) -> Point3 {
33 let mut my_idx = idx as i32;
34 let bounds = self.dimensions();
35 let z = my_idx / (bounds.x * bounds.y);
36 my_idx -= z * bounds.x * bounds.y;
37
38 let y = my_idx / bounds.x;
39 my_idx -= y * bounds.x;
40
41 let x = my_idx;
42 Point3::new(x, y, z)
43 }
44
45 /// Dimensions
46 fn dimensions(&self) -> Point3 {
47 panic!("You must either define the dimensions function (trait Algorithm3D) on your map, or define the various point3d_to_index and index_to_point3d functions.");
48 }
49
50 // Optional - check that an x/y/z coordinate is within the map bounds. If not provided,
51 // it falls back to using the map's dimensions from that trait implementation. Most of
52 // the time, that's what you want.
53 fn in_bounds(&self, pos: Point3) -> bool {
54 let bounds = self.dimensions();
55 pos.x >= 0
56 && pos.x < bounds.x
57 && pos.y >= 0
58 && pos.y < bounds.y
59 && pos.z >= 0
60 && pos.z < bounds.z
61 }
62}
63
64#[cfg(test)]
65mod tests {

Callers

nothing calls this directly

Implementers 7

dwarfmap.rsbracket-terminal/examples/dwarfmap.rs
bevy_dwarfmap.rsbracket-bevy/examples/bevy_dwarfmap.rs
test_unimplemented_dimensionsbracket-algorithm-traits/src/algorithm
test_in_boundsbracket-algorithm-traits/src/algorithm
test_point3d_to_indexbracket-algorithm-traits/src/algorithm
test_index_to_point3dbracket-algorithm-traits/src/algorithm
ex14-dwarfmap.rsrltk/examples-deprecated/ex14-dwarfmap

Calls

no outgoing calls

Tested by

no test coverage detected