mirror of
https://github.com/Chaoscaot/schemsearch.git
synced 2025-12-05 08:47:06 +01:00
new Matcher
This commit is contained in:
@@ -33,6 +33,36 @@ pub struct SearchBehavior {
|
||||
pub threshold: f32,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
pub fn isMatching(
|
||||
schem_data: *const i32,
|
||||
pattern_data: *const i32,
|
||||
pattern_data_length: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
z: usize,
|
||||
schem_width: usize,
|
||||
schem_length: usize,
|
||||
pattern_width: usize,
|
||||
pattern_height: usize,
|
||||
pattern_length: usize,
|
||||
w_ptr: *mut i32,
|
||||
) -> i32;
|
||||
|
||||
|
||||
pub fn is_matching_all(
|
||||
schem_data: *const i32,
|
||||
pattern_data: *const i32,
|
||||
schem_width: usize,
|
||||
schem_height: usize,
|
||||
schem_length: usize,
|
||||
pattern_width: usize,
|
||||
pattern_height: usize,
|
||||
pattern_length: usize,
|
||||
result: *mut i32
|
||||
);
|
||||
}
|
||||
|
||||
pub fn search(
|
||||
schem: SpongeSchematic,
|
||||
pattern_schem: &SpongeSchematic,
|
||||
@@ -62,7 +92,8 @@ pub fn search(
|
||||
|
||||
let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.palette.get("minecraft:air").unwrap_or(&-1) } else { &-1};
|
||||
|
||||
let pattern_blocks = pattern_schem.block_data.len() as f32;
|
||||
let pattern_blocks_usize = pattern_schem.block_data.len();
|
||||
let pattern_blocks = pattern_blocks_usize as f32;
|
||||
let i_pattern_blocks = pattern_blocks as i32;
|
||||
|
||||
let pattern_width = pattern_schem.width as usize;
|
||||
@@ -75,44 +106,75 @@ pub fn search(
|
||||
|
||||
let skip_amount = ceil((pattern_blocks * (1.0 - search_behavior.threshold)) as f64, 0) as i32;
|
||||
|
||||
for y in 0..=schem_height - pattern_height {
|
||||
/*for y in 0..=schem_height - pattern_height {
|
||||
for z in 0..=schem_length - pattern_length {
|
||||
for x in 0..=schem_width - pattern_width {
|
||||
let mut not_matching = 0;
|
||||
'outer:
|
||||
for j in 0..pattern_height {
|
||||
for k in 0..pattern_length {
|
||||
'inner:
|
||||
for i in 0..pattern_width {
|
||||
let index = (x + i) + schem_width * ((z + k) + (y + j) * schem_length);
|
||||
let pattern_index = i + pattern_width * (k + j * pattern_length);
|
||||
let data = unsafe { *schem_data.add(index) };
|
||||
let pattern_data = unsafe { *pattern_data.add(pattern_index) };
|
||||
if (search_behavior.ignore_air && data != *air_id) || (search_behavior.air_as_any && pattern_data != *air_id) {
|
||||
continue 'inner;
|
||||
}
|
||||
if data != pattern_data {
|
||||
not_matching += 1;
|
||||
if not_matching >= skip_amount {
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let matching_count;
|
||||
unsafe {
|
||||
matching_count = isMatching(
|
||||
schem_data,
|
||||
pattern_data,
|
||||
pattern_blocks_usize,
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
schem_width,
|
||||
schem_length,
|
||||
pattern_width,
|
||||
pattern_height,
|
||||
pattern_length,
|
||||
w_ptr,
|
||||
);
|
||||
};
|
||||
|
||||
if not_matching < skip_amount {
|
||||
matches.push(Match {
|
||||
x: x as u16,
|
||||
y: y as u16,
|
||||
z: z as u16,
|
||||
percent: (i_pattern_blocks - not_matching) as f32 / pattern_blocks,
|
||||
});
|
||||
if matching_count >= i_pattern_blocks - skip_amount {
|
||||
let percent = matching_count as f32 / pattern_blocks;
|
||||
if percent >= search_behavior.threshold {
|
||||
matches.push(Match {
|
||||
x: x as u16,
|
||||
y: y as u16,
|
||||
z: z as u16,
|
||||
percent,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
let mut result = Vec::<i32>::with_capacity(schem_width * schem_height * schem_length);
|
||||
result.resize(schem_width * schem_height * schem_length, 0);
|
||||
|
||||
unsafe {
|
||||
is_matching_all(
|
||||
schem_data,
|
||||
pattern_data,
|
||||
schem_width,
|
||||
schem_height,
|
||||
schem_length,
|
||||
pattern_width,
|
||||
pattern_height,
|
||||
pattern_length,
|
||||
result.as_mut_ptr()
|
||||
);
|
||||
}
|
||||
|
||||
dbg!(result.clone());
|
||||
|
||||
result.into_iter().enumerate().filter(|(_, matching_count)| *matching_count >= i_pattern_blocks - skip_amount).for_each(|(i, matching_count)| {
|
||||
let percent = matching_count as f32 / pattern_blocks;
|
||||
let x = i % schem_width;
|
||||
let y = (i / schem_width) % schem_height;
|
||||
let z = i / (schem_width * schem_height);
|
||||
matches.push(Match {
|
||||
x: x as u16,
|
||||
y: y as u16,
|
||||
z: z as u16,
|
||||
percent,
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user