fix(properties): measure bulged polylines
Merge PR #808 and use kernel area, length, and centroid measurements.
This commit is contained in:
commit
afd4c96a78
3 changed files with 16 additions and 28 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -878,7 +878,7 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cadkernel"
|
name = "cadkernel"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/HakanSeven12/cadkernel.git?rev=efc77f5#efc77f5d18375467c3cc2c256a22759bb5f9cb54"
|
source = "git+https://github.com/HakanSeven12/cadkernel.git?rev=b8ec104#b8ec104e86060fe7d5c698870915b5388569649b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"acadrust",
|
"acadrust",
|
||||||
"cavalier_contours",
|
"cavalier_contours",
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ rfd = "0.17"
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
acadrust = { git = "https://github.com/HakanSeven12/cadcodec.git", rev = "931c4ab", features = ["serde"] }
|
acadrust = { git = "https://github.com/HakanSeven12/cadcodec.git", rev = "931c4ab", features = ["serde"] }
|
||||||
cadkernel = { git = "https://github.com/HakanSeven12/cadkernel.git", rev = "efc77f5", features = ["acis", "offset"] }
|
cadkernel = { git = "https://github.com/HakanSeven12/cadkernel.git", rev = "b8ec104", features = ["acis", "offset"] }
|
||||||
dwg-thumbnailer = { path = "crates/dwg-thumbnailer" }
|
dwg-thumbnailer = { path = "crates/dwg-thumbnailer" }
|
||||||
flate2 = "1"
|
flate2 = "1"
|
||||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "tiff"] }
|
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "tiff"] }
|
||||||
|
|
|
||||||
|
|
@ -1053,32 +1053,20 @@ impl crate::entities::traits::MassPropsCalc for acadrust::entities::LwPolyline {
|
||||||
cy: 0.0,
|
cy: 0.0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Shoelace area + perimeter
|
// Kernel measurement includes bulges and closes open curves only for area.
|
||||||
let mut area_sum = 0.0f64;
|
let curve = crate::entities::curve::lwpolyline_curve(p)
|
||||||
let mut perimeter = 0.0f64;
|
.expect("an LwPolyline with at least two vertices has a planar curve");
|
||||||
let mut cx_sum = 0.0f64;
|
let area = curve.curve.enclosed_area().abs();
|
||||||
let mut cy_sum = 0.0f64;
|
let perimeter = curve.length();
|
||||||
let n_segs = if p.is_closed { n } else { n - 1 };
|
let (cx, cy) = curve
|
||||||
for idx in 0..n_segs {
|
.curve
|
||||||
let v0 = &p.vertices[idx];
|
.enclosed_centroid()
|
||||||
let v1 = &p.vertices[(idx + 1) % n];
|
.map(|point| (point[0], point[1]))
|
||||||
let x0 = v0.location.x;
|
.unwrap_or_else(|| {
|
||||||
let y0 = v0.location.y;
|
let x = p.vertices.iter().map(|v| v.location.x).sum::<f64>() / n as f64;
|
||||||
let x1 = v1.location.x;
|
let y = p.vertices.iter().map(|v| v.location.y).sum::<f64>() / n as f64;
|
||||||
let y1 = v1.location.y;
|
(x, y)
|
||||||
area_sum += x0 * y1 - x1 * y0;
|
});
|
||||||
perimeter += ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt();
|
|
||||||
cx_sum += (x0 + x1) * (x0 * y1 - x1 * y0);
|
|
||||||
cy_sum += (y0 + y1) * (x0 * y1 - x1 * y0);
|
|
||||||
}
|
|
||||||
let area = (area_sum / 2.0).abs();
|
|
||||||
let (cx, cy) = if area > 1e-12 {
|
|
||||||
(cx_sum / (6.0 * area), cy_sum / (6.0 * area))
|
|
||||||
} else {
|
|
||||||
let sx: f64 = p.vertices.iter().map(|v| v.location.x).sum::<f64>() / n as f64;
|
|
||||||
let sy: f64 = p.vertices.iter().map(|v| v.location.y).sum::<f64>() / n as f64;
|
|
||||||
(sx, sy)
|
|
||||||
};
|
|
||||||
crate::entities::traits::MassProps {
|
crate::entities::traits::MassProps {
|
||||||
area,
|
area,
|
||||||
perimeter,
|
perimeter,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue