1
1
use rand:: Rng ;
2
+ use rand:: seq:: SliceRandom ;
2
3
3
4
pub mod geo;
4
5
pub use geo:: Vec3 ;
@@ -11,6 +12,7 @@ use crate::octree::Octree;
11
12
pub struct Dla {
12
13
spawn_radius : i64 ,
13
14
attraction_radius : i64 ,
15
+ attraction_radius2 : i64 ,
14
16
15
17
cells : Octree ,
16
18
bbox : Bbox ,
@@ -41,6 +43,7 @@ impl Dla {
41
43
bbox,
42
44
spawn_radius : i64:: from ( spawn_radius) ,
43
45
attraction_radius : i64:: from ( attraction_radius) ,
46
+ attraction_radius2 : i64:: from ( attraction_radius) . pow ( 2 ) ,
44
47
} )
45
48
}
46
49
@@ -88,15 +91,14 @@ impl Dla {
88
91
break ;
89
92
}
90
93
None => {
91
- let d = match rng. gen_range ( 0 , 6 ) {
92
- 0 => Vec3 :: new ( -1 , 0 , 0 ) ,
93
- 1 => Vec3 :: new ( 1 , 0 , 0 ) ,
94
- 2 => Vec3 :: new ( 0 , -1 , 0 ) ,
95
- 3 => Vec3 :: new ( 0 , 1 , 0 ) ,
96
- 4 => Vec3 :: new ( 0 , 0 , -1 ) ,
97
- 5 => Vec3 :: new ( 0 , 0 , 1 ) ,
98
- _ => unreachable ! ( ) ,
99
- } ;
94
+ let d = Vec3 :: new (
95
+ rng. gen_range ( 1 , self . attraction_radius / 2 )
96
+ * * [ -1 , 1 ] . choose ( rng) . unwrap ( ) ,
97
+ rng. gen_range ( 1 , self . attraction_radius / 2 )
98
+ * * [ -1 , 1 ] . choose ( rng) . unwrap ( ) ,
99
+ rng. gen_range ( 1 , self . attraction_radius / 2 )
100
+ * * [ -1 , 1 ] . choose ( rng) . unwrap ( ) ,
101
+ ) ;
100
102
cell = cell + d * self . attraction_radius ;
101
103
102
104
if !spawn_bbox. contains ( cell) {
@@ -110,10 +112,9 @@ impl Dla {
110
112
}
111
113
112
114
pub fn stuck ( & self , p : Vec3 ) -> Option < Vec3 > {
113
- let ( n, _d2 ) = self . cells . nearest ( p) ?;
115
+ let ( n, d2 ) = self . cells . nearest ( p) ?;
114
116
115
- let d = n - p;
116
- if d. x . abs ( ) + d. y . abs ( ) + d. z . abs ( ) <= self . attraction_radius {
117
+ if d2 <= self . attraction_radius2 {
117
118
Some ( n)
118
119
} else {
119
120
None
0 commit comments