Mirror von
https://github.com/Chaoscaot/schemsearch
synchronisiert 2024-11-13 23:40:11 +01:00
new Matcher
Dieser Commit ist enthalten in:
Ursprung
d20940f89b
Commit
511ce04f0b
@ -16,4 +16,5 @@ opt-level = "z"
|
||||
codegen-units = 1
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
lto = true
|
||||
opt-level = 3
|
4
Makefile
4
Makefile
@ -1,10 +1,10 @@
|
||||
default:
|
||||
@echo "Building (Release)...";
|
||||
cargo rustc --release --color=always -p schemsearch-cli -- -C target-feature=+avx2
|
||||
cargo rustc --release --color=always -p schemsearch-cli -- -C target-cpu=native
|
||||
|
||||
sql:
|
||||
@echo "Building (Release)...";
|
||||
cargo rustc --release --color=always -p schemsearch-cli --features sql -- -C target-feature=+avx2
|
||||
cargo rustc --release --color=always -p schemsearch-cli --features sql -- -C target-cpu=native
|
||||
|
||||
debug:
|
||||
@echo "Building (Debug)...";
|
||||
|
@ -11,3 +11,6 @@ serde = { version = "1.0.160", features = ["derive"] }
|
||||
schemsearch-files = { path = "../schemsearch-files" }
|
||||
named-binary-tag = "0.6"
|
||||
libmath = "0.2.1"
|
||||
|
||||
[build-dependencies]
|
||||
cc = { version = "1.0.83", features = [] }
|
||||
|
7
schemsearch-lib/build.rs
Normale Datei
7
schemsearch-lib/build.rs
Normale Datei
@ -0,0 +1,7 @@
|
||||
use cc;
|
||||
|
||||
fn main() {
|
||||
cc::Build::new()
|
||||
.file("src/compare.c")
|
||||
.compile("compare");
|
||||
}
|
69
schemsearch-lib/src/compare.c
Normale Datei
69
schemsearch-lib/src/compare.c
Normale Datei
@ -0,0 +1,69 @@
|
||||
#include <stdint.h>
|
||||
|
||||
int32_t isMatching(
|
||||
const int32_t *schem_data,
|
||||
const int32_t *pattern_data,
|
||||
size_t pattern_data_length,
|
||||
int32_t x,
|
||||
int32_t y,
|
||||
int32_t z,
|
||||
int32_t schem_width,
|
||||
int32_t schem_length,
|
||||
int32_t pattern_width,
|
||||
int32_t pattern_height,
|
||||
int32_t pattern_length,
|
||||
int32_t *w_ptr
|
||||
) {
|
||||
for (int j = 0; j < pattern_height; ++j) {
|
||||
for (int k = 0; k < pattern_length; ++k) {
|
||||
int pattern_index_pre = k * pattern_width + j * pattern_width * pattern_length;
|
||||
int schem_index_pre = x + (k + z) * schem_width + (j + y) * schem_width * schem_length;
|
||||
for (int i = 0; i < pattern_width; ++i) {
|
||||
int pattern_index = i + pattern_index_pre;
|
||||
int schem_index = i + schem_index_pre;
|
||||
w_ptr[pattern_index] = schem_data[schem_index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t matching = 0;
|
||||
for (int i = 0; i < pattern_data_length; ++i) {
|
||||
matching += w_ptr[i] == pattern_data[i];
|
||||
}
|
||||
|
||||
return matching;
|
||||
}
|
||||
|
||||
void is_matching_all(
|
||||
const int32_t *schem_data,
|
||||
const int32_t *pattern_data,
|
||||
size_t schem_width,
|
||||
size_t schem_height,
|
||||
size_t schem_length,
|
||||
size_t pattern_width,
|
||||
size_t pattern_height,
|
||||
size_t pattern_length,
|
||||
int32_t *result
|
||||
) {
|
||||
for (size_t pz = 0; pz < pattern_length; ++pz) {
|
||||
size_t maxZ = schem_length - pattern_length + pz + 1;
|
||||
for (size_t py = 0; py < pattern_height; ++py) {
|
||||
size_t maxY = schem_height - pattern_height + py + 1;
|
||||
for (size_t px = 0; px < pattern_width; ++px) {
|
||||
int32_t pv = pattern_data[px + py * pattern_width + pz * pattern_width * pattern_height];
|
||||
size_t maxX = schem_width - pattern_width + px + 1;
|
||||
for (size_t z = pz; z < maxZ; ++z) {
|
||||
size_t sourceOffsetZ = z * schem_width * schem_height;
|
||||
size_t resultOffsetZ = (z - pz) * schem_width * schem_height - py * schem_width;
|
||||
for (size_t y = py; y < maxY; ++y) {
|
||||
size_t sourceOffsetY = sourceOffsetZ + y * schem_width;
|
||||
size_t resultOffsetY = resultOffsetZ + y * schem_width - px;
|
||||
for (size_t x = px; x < maxX; ++x) {
|
||||
result[resultOffsetY + x] += schem_data[sourceOffsetY + x] == pv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
BIN
tests/Pattern.nbt
Normale Datei
BIN
tests/Pattern.nbt
Normale Datei
Binäre Datei nicht angezeigt.
BIN
tests/Random.nbt
Normale Datei
BIN
tests/Random.nbt
Normale Datei
Binäre Datei nicht angezeigt.
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren