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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2023 lacklustr@protonmail.com https://github.com/eadf
// This file is part of the hronn crate.
mod adaptivesamplebuffer;
pub(crate) mod meanderpath;
pub mod meanderpattern;
pub mod triangulatepattern;
use super::{
meshanalyzer::MeshAnalyzer,
probe::{Probe, QueryParameters},
};
use crate::{HronnError, meshanalyzer::SearchResult, prelude::*};
use krakel::PointTrait;
use std::{cell::RefCell, collections::HashSet};
use vector_traits::{
num_traits::Float,
prelude::{GenericScalar, GenericVector3, HasXYZ},
};
#[derive(Clone, Copy, Debug)]
pub struct AdaptiveSearchConfig<T> {
pub xy_sample_dist: T,
pub z_jump_threshold: T,
pub buffer_size: usize,
pub reduce: bool,
}
impl<T: Float> AdaptiveSearchConfig<T> {
pub fn new(xy_sample_dist: T, z_jump_threshold: T, reduce: bool) -> Self {
Self {
xy_sample_dist,
z_jump_threshold,
buffer_size: 5,
reduce,
}
}
}
pub struct SearchPatternConfig<'a, T: GenericVector3, MESH: HasXYZ + ConvertTo<T>> {
pub probe: &'a dyn Probe<T, MESH>,
pub minimum_z: T::Scalar,
pub adaptive_mesh_config: Option<AdaptiveSearchConfig<T::Scalar>>,
}
impl<'a, T: GenericVector3, MESH: HasXYZ + ConvertTo<T>> SearchPatternConfig<'a, T, MESH> {
pub fn new(probe: &'a dyn Probe<T, MESH>, minimum_z: T::Scalar) -> Self {
Self {
probe,
minimum_z,
adaptive_mesh_config: None,
}
}
pub fn with_adaptive_config(mut self, adaptive: AdaptiveSearchConfig<T::Scalar>) -> Self {
self.adaptive_mesh_config = Some(adaptive);
self
}
}
/// A Strategy interface
pub trait SearchPattern<T: GenericVector3, MESH: HasXYZ + ConvertTo<T>> {
/// This method is supposed to only use the dynamic dispatch of the &dyn Probe a hand full of
/// times during the entire search() operation.
fn search(
&self,
context: &MeshAnalyzer<'_, T, MESH>,
config: &SearchPatternConfig<'_, T, MESH>,
) -> Result<StrategyResult<MESH>, HronnError>;
}
pub(crate) trait SearchPatternImpl<T: GenericVector3, MESH: HasXYZ>
where
T::Vector2: PointTrait<PScalar = T::Scalar>,
MESH: ConvertTo<T>,
{
/// This method is supposed to only use the dynamic dispatch of the &dyn Probe a hand full of
/// times during the entire search() operation.
fn search_impl(
&self,
context: &MeshAnalyzer<'_, T, MESH>,
config: &SearchPatternConfig<'_, T, MESH>,
) -> Result<StrategyResult<MESH>, HronnError>;
/// Performs a query using the original (non-split) mesh.
///
/// This function serves as the "inner loop" for collision computations.
/// To achieve optimal performance, this function does not use any dynamic dispatch.
///
/// # Parameters
///
/// * `center`: The center point of the probe.
/// * `search_radius`: The radius around the `center` in which the search is performed.
/// * `probe`: The probe object responsible for computing collisions.
///
/// # Returns
///
/// * An `Option<T::Scalar>` that represents the maximum value computed by the
/// `probe.compute_collision()` function. If no collisions are detected,
/// it returns `None`.
#[allow(clippy::type_complexity)]
fn query_nonsplit_point(
&self,
ma: &MeshAnalyzer<'_, T, MESH>,
center: T::Vector2,
qp: &QueryParameters<'_, T, MESH>,
collision_fn: fn(
query_parameters: &QueryParameters<'_, T, MESH>,
site_index: u32,
center: T::Vector2,
mt: &mut MaximumTracker<SearchResult<T>>,
),
) -> Option<SearchResult<T>> {
let mut mt = MaximumTracker::<SearchResult<T>>::default();
ma.kd_tree
.closure_range_query(¢er, qp.search_radius, |site| {
collision_fn(qp, site.index, center, &mut mt)
});
mt.get_max()
}
/// Performs a query using the split mesh.
///
/// This function serves as the "inner loop" for collision computations.
/// To achieve optimal performance, this function does not use any dynamic dispatch.
///
/// # Parameters
///
/// * `center`: The center point of the probe.
/// * `search_radius`: The radius around the `center` in which the search is performed.
/// * `probe`: The probe object responsible for computing collisions.
///
/// # Returns
///
/// * An `Option<T::Scalar>` that represents the maximum value computed by the
/// `probe.compute_collision()` function. If no collisions are detected,
/// it returns `None`.
#[allow(clippy::type_complexity)]
fn query_split_point(
&self,
ma: &MeshAnalyzer<'_, T, MESH>,
center: T::Vector2,
qp: &QueryParameters<'_, T, MESH>,
collision_fn: fn(
query_parameters: &QueryParameters<'_, T, MESH>,
site_index: u32,
center: T::Vector2,
mt: &mut MaximumTracker<SearchResult<T>>,
),
) -> Option<SearchResult<T>> {
let mut mt = MaximumTracker::<SearchResult<T>>::default();
// The original triangles are split, only report original triangles
THREAD_LOCAL_SET.with(|set| {
let mut already_tested = set.borrow_mut();
already_tested.clear();
ma.kd_tree
.closure_range_query(¢er, qp.search_radius, |site| {
let site_index = site.index;
if already_tested.insert(site_index) {
collision_fn(qp, site_index, center, &mut mt)
//probe.compute_collision(vertices, indices, site_index, &mut mt, center);
}
});
already_tested.clear();
});
mt.get_max()
}
/// a search query that bypasses the kd-tree and simply tests each and every triangle
#[allow(clippy::type_complexity)]
fn brute_force_query_point(
&self,
_: &MeshAnalyzer<'_, T, MESH>,
center: T::Vector2,
qp: &QueryParameters<'_, T, MESH>,
collision_fn: fn(
query_parameters: &QueryParameters<'_, T, MESH>,
site_index: u32,
center: T::Vector2,
mt: &mut MaximumTracker<SearchResult<T>>,
),
) -> Option<SearchResult<T>> {
let mut mt = MaximumTracker::<SearchResult<T>>::default();
let site_index_max = qp.indices.len() / 3;
for site_index in 0..site_index_max as u32 {
// Brute force vertex collision processing
collision_fn(qp, site_index, center, &mut mt);
}
mt.get_max()
}
/// The distance we use when searching the kd-tree
/// We can search by a distance of r+l/2, i.e: 1.5*r if l=r
#[inline(always)]
fn calculate_search_radius(
probe_radius: T::Scalar,
triangle_side_length: T::Scalar,
) -> T::Scalar {
let tree: T::Scalar = 3.0.into();
probe_radius + tree.sqrt() * triangle_side_length / T::Scalar::TWO
}
}
// a thread local HashSet used by the query_split_point() method
// we use a thread local container so that we don't have to allocate it again and again.
// Remember to clear & shrink this after each search pattern is executed
// TODO: use a vob instead?
thread_local! {
static THREAD_LOCAL_SET: RefCell<HashSet<u32>> = RefCell::new(HashSet::new());
}