2026-04-02 23:36:53 +03:00
|
|
|
// AREA command — compute area and perimeter of a polygon picked point by point.
|
|
|
|
|
// Press Enter to close and calculate.
|
|
|
|
|
|
|
|
|
|
use glam::Vec3;
|
|
|
|
|
|
|
|
|
|
use crate::command::{CadCommand, CmdResult};
|
2026-06-16 11:33:46 +03:00
|
|
|
use crate::scene::model::wire_model::WireModel;
|
2026-04-02 23:36:53 +03:00
|
|
|
|
|
|
|
|
pub struct AreaCommand {
|
|
|
|
|
points: Vec<Vec3>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AreaCommand {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self { points: vec![] }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CadCommand for AreaCommand {
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
|
"AREA"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn prompt(&self) -> String {
|
|
|
|
|
if self.points.is_empty() {
|
|
|
|
|
"AREA Specify first corner point (Enter to cancel):".into()
|
|
|
|
|
} else {
|
|
|
|
|
format!(
|
|
|
|
|
"AREA Specify next point ({} picked, Enter to calculate):",
|
|
|
|
|
self.points.len()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
|
|
|
|
self.points.push(pt);
|
|
|
|
|
CmdResult::NeedPoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_enter(&mut self) -> CmdResult {
|
|
|
|
|
if self.points.len() < 3 {
|
|
|
|
|
return CmdResult::Cancel;
|
|
|
|
|
}
|
2026-06-04 10:32:38 +03:00
|
|
|
// Shoelace formula in the world XY plane.
|
2026-04-02 23:36:53 +03:00
|
|
|
let n = self.points.len();
|
|
|
|
|
let mut area_sum = 0.0f32;
|
|
|
|
|
let mut perimeter = 0.0f32;
|
|
|
|
|
for idx in 0..n {
|
|
|
|
|
let a = self.points[idx];
|
|
|
|
|
let b = self.points[(idx + 1) % n];
|
2026-06-04 10:32:38 +03:00
|
|
|
area_sum += a.x * b.y - b.x * a.y;
|
2026-04-02 23:36:53 +03:00
|
|
|
perimeter += (b - a).length();
|
|
|
|
|
}
|
|
|
|
|
let area = (area_sum * 0.5).abs();
|
|
|
|
|
let msg = format!("Area = {area:.4}, Perimeter = {perimeter:.4}");
|
|
|
|
|
CmdResult::Measurement(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
|
|
|
|
if self.points.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let mut pts: Vec<[f32; 3]> = self.points.iter().map(|p| [p.x, p.y, p.z]).collect();
|
|
|
|
|
pts.push([pt.x, pt.y, pt.z]);
|
|
|
|
|
pts.push([self.points[0].x, self.points[0].y, self.points[0].z]);
|
|
|
|
|
Some(WireModel {
|
|
|
|
|
name: "area_preview".into(),
|
|
|
|
|
points: pts,
|
|
|
|
|
color: WireModel::CYAN,
|
|
|
|
|
selected: false,
|
|
|
|
|
pattern_length: 0.0,
|
|
|
|
|
pattern: [0.0; 8],
|
|
|
|
|
line_weight_px: 1.0,
|
|
|
|
|
snap_pts: vec![],
|
|
|
|
|
tangent_geoms: vec![],
|
2026-04-06 23:19:08 +03:00
|
|
|
aci: 0,
|
2026-04-02 23:36:53 +03:00
|
|
|
key_vertices: vec![],
|
2026-04-26 12:21:51 +03:00
|
|
|
aabb: WireModel::UNBOUNDED_AABB,
|
2026-05-06 06:55:28 +03:00
|
|
|
plinegen: true,
|
2026-05-06 07:12:05 +03:00
|
|
|
vp_scissor: None,
|
2026-05-06 23:24:26 +03:00
|
|
|
fill_tris: vec![],
|
2026-04-02 23:36:53 +03:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-26 22:45:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// ── Autocomplete registry ─────────────────────────────────
|
|
|
|
|
inventory::submit!(crate::command::CommandRegistration { names: &["AREA"] }); // AreaCommand
|