Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use crate::geometry::{Point, Vector2, Line, Circle, Arc, Ellipse, Polyline, BSpline, NURBS, Curve};
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MeasurementResult {
    pub value: f64,
    pub unit: MeasurementUnit,
    pub display: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum MeasurementUnit {
    Millimeters,
    Centimeters,
    Meters,
    Inches,
    Feet,
    Degrees,
    Radians,
}

impl MeasurementUnit {
    pub fn abbreviation(&self) -> &str {
        match self {
            MeasurementUnit::Millimeters => "mm",
            MeasurementUnit::Centimeters => "cm",
            MeasurementUnit::Meters => "m",
            MeasurementUnit::Inches => "\"",
            MeasurementUnit::Feet => "'",
            MeasurementUnit::Degrees => "°",
            MeasurementUnit::Radians => "rad",
        }
    }
    
    pub fn conversion_factor(&self) -> f64 {
        match self {
            MeasurementUnit::Millimeters => 1.0,
            MeasurementUnit::Centimeters => 10.0,
            MeasurementUnit::Meters => 1000.0,
            MeasurementUnit::Inches => 25.4,
            MeasurementUnit::Feet => 304.8,
            MeasurementUnit::Degrees => 1.0,
            MeasurementUnit::Radians => 1.0,
        }
    }
}

pub trait MeasurementTool {
    fn measure(&self) -> MeasurementResult;
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult;
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct DistanceMeasurement {
    pub start_point: Point,
    pub end_point: Point,
    pub distance: f64,
    pub delta_x: f64,
    pub delta_y: f64,
    pub angle: f64,
    pub unit: MeasurementUnit,
}

impl DistanceMeasurement {
    pub fn new(start: Point, end: Point, unit: MeasurementUnit) -> Self {
        let delta = end - start;
        let distance = (delta.x.powi(2) + delta.y.powi(2)).sqrt();
        let angle = delta.to_vector2().angle();
        
        Self {
            start_point: start,
            end_point: end,
            distance,
            delta_x: delta.x,
            delta_y: delta.y,
            angle,
            unit,
        }
    }
    
    pub fn distance_in_unit(&self, unit: MeasurementUnit) -> f64 {
        let base_distance = self.distance * self.unit.conversion_factor();
        base_distance / unit.conversion_factor()
    }
    
    pub fn format(&self, decimals: u32) -> String {
        format!(
            "Distance: {:.3$} | Δx: {:.3$} | Δy: {:.3$} | Angle: {:.2$}°",
            self.distance,
            self.delta_x,
            self.delta_y,
            self.angle,
            decimals
        )
    }
}

impl MeasurementTool for DistanceMeasurement {
    fn measure(&self) -> MeasurementResult {
        MeasurementResult {
            value: self.distance,
            unit: self.unit,
            display: format!("{:.3} {}", self.distance, self.unit.abbreviation()),
        }
    }
    
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult {
        MeasurementResult {
            value: self.distance,
            unit: self.unit,
            display: format!("{:.1$} {}", self.distance, decimals, self.unit.abbreviation()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AngleMeasurement {
    pub vertex: Point,
    pub start_point: Point,
    pub end_point: Point,
    pub angle: f64,
    pub angle_rad: f64,
    pub is_reflex: bool,
    pub unit: MeasurementUnit,
}

impl AngleMeasurement {
    pub fn new(vertex: Point, p1: Point, p2: Point, unit: MeasurementUnit) -> Self {
        let v1 = (p1 - vertex).to_vector2();
        let v2 = (p2 - vertex).to_vector2();
        
        let dot = v1.x * v2.x + v1.y * v2.y;
        let cross = v1.x * v2.y - v1.y * v2.x;
        let angle_rad = cross.atan2(dot);
        
        let angle_deg = angle_rad.to_degrees();
        let normalized_angle = if angle_deg < 0.0 { angle_deg + 360.0 } else { angle_deg };
        let is_reflex = normalized_angle > 180.0;
        
        Self {
            vertex,
            start_point: p1,
            end_point: p2,
            angle: normalized_angle,
            angle_rad,
            is_reflex,
            unit,
        }
    }
    
    pub fn get_smallest_angle(&self) -> f64 {
        self.angle.min(360.0 - self.angle)
    }
    
    pub fn angle_in_unit(&self, unit: MeasurementUnit) -> f64 {
        if unit == MeasurementUnit::Radians {
            self.angle_rad.abs()
        } else {
            self.angle
        }
    }
    
    pub fn format(&self, decimals: u32) -> String {
        if self.is_reflex {
            format!(
                "Angle: {:.2$}° (reflex: {:.2$}°)",
                self.angle,
                360.0 - self.angle,
                decimals
            )
        } else {
            format!("Angle: {:.2$}°", self.angle, decimals)
        }
    }
}

impl MeasurementTool for AngleMeasurement {
    fn measure(&self) -> MeasurementResult {
        MeasurementResult {
            value: self.angle,
            unit: self.unit,
            display: format!("{:.2}°", self.angle),
        }
    }
    
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult {
        MeasurementResult {
            value: self.angle,
            unit: self.unit,
            display: format!("{:.1$}°", self.angle, decimals),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AreaMeasurement {
    pub area: f64,
    pub perimeter: f64,
    pub centroid: Point,
    pub unit: MeasurementUnit,
    pub points: Vec<Point>,
}

impl AreaMeasurement {
    pub fn new(points: Vec<Point>, unit: MeasurementUnit) -> Self {
        let area = Self::calculate_area(&points);
        let perimeter = Self::calculate_perimeter(&points);
        let centroid = Self::calculate_centroid(&points);
        
        Self {
            area,
            perimeter,
            centroid,
            unit,
            points,
        }
    }
    
    fn calculate_area(points: &[Point]) -> f64 {
        if points.len() < 3 {
            return 0.0;
        }
        
        let mut area = 0.0;
        let n = points.len();
        
        for i in 0..n {
            let j = (i + 1) % n;
            area += points[i].x * points[j].y;
            area -= points[j].x * points[i].y;
        }
        
        area.abs() / 2.0
    }
    
    fn calculate_perimeter(points: &[Point]) -> f64 {
        let mut perimeter = 0.0;
        
        for i in 0..points.len() {
            let next = (i + 1) % points.len();
            perimeter += points[i].distance_to(&points[next]);
        }
        
        perimeter
    }
    
    fn calculate_centroid(points: &[Point]) -> Point {
        if points.is_empty() {
            return Point::origin();
        }
        
        let cx = points.iter().map(|p| p.x).sum::<f64>() / points.len() as f64;
        let cy = points.iter().map(|p| p.y).sum::<f64>() / points.len() as f64;
        
        Point::new(cx, cy, 0.0)
    }
    
    pub fn area_in_unit(&self, unit: MeasurementUnit) -> f64 {
        let base_area = self.area * self.unit.conversion_factor().powi(2);
        base_area / unit.conversion_factor().powi(2)
    }
    
    pub fn perimeter_in_unit(&self, unit: MeasurementUnit) -> f64 {
        let base_perimeter = self.perimeter * self.unit.conversion_factor();
        base_perimeter / unit.conversion_factor()
    }
    
    pub fn format(&self, decimals: u32) -> String {
        format!(
            "Area: {:.3$} {}² | Perimeter: {:.3$} {}",
            self.area,
            self.unit.abbreviation(),
            self.perimeter,
            self.unit.abbreviation(),
            decimals
        )
    }
}

impl MeasurementTool for AreaMeasurement {
    fn measure(&self) -> MeasurementResult {
        MeasurementResult {
            value: self.area,
            unit: self.unit,
            display: format!("{:.3} {}^2", self.area, self.unit.abbreviation()),
        }
    }
    
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult {
        MeasurementResult {
            value: self.area,
            unit: self.unit,
            display: format!("{:.1$} {}^2", self.area, decimals, self.unit.abbreviation()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RadiusMeasurement {
    pub center: Point,
    pub point: Point,
    pub radius: f64,
    pub diameter: f64,
    pub unit: MeasurementUnit,
}

impl RadiusMeasurement {
    pub fn new(center: Point, point: Point, unit: MeasurementUnit) -> Self {
        let radius = center.distance_to(&point);
        
        Self {
            center,
            point,
            radius,
            diameter: radius * 2.0,
            unit,
        }
    }
    
    pub fn from_circle(circle: &Circle, unit: MeasurementUnit) -> Self {
        Self {
            center: circle.center,
            point: Point::new(
                circle.center.x + circle.radius,
                circle.center.y,
                0.0,
            ),
            radius: circle.radius,
            diameter: circle.radius * 2.0,
            unit,
        }
    }
    
    pub fn from_arc(arc: &Arc, unit: MeasurementUnit) -> Self {
        Self {
            center: arc.center,
            point: Point::new(
                arc.center.x + arc.radius,
                arc.center.y,
                0.0,
            ),
            radius: arc.radius,
            diameter: arc.radius * 2.0,
            unit,
        }
    }
    
    pub fn radius_in_unit(&self, unit: MeasurementUnit) -> f64 {
        let base_radius = self.radius * self.unit.conversion_factor();
        base_radius / unit.conversion_factor()
    }
    
    pub fn format(&self, decimals: u32) -> String {
        format!(
            "Radius: {:.3$} | Diameter: {:.3$}",
            self.radius,
            self.diameter,
            decimals
        )
    }
}

impl MeasurementTool for RadiusMeasurement {
    fn measure(&self) -> MeasurementResult {
        MeasurementResult {
            value: self.radius,
            unit: self.unit,
            display: format!("R {:.3} {}", self.radius, self.unit.abbreviation()),
        }
    }
    
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult {
        MeasurementResult {
            value: self.radius,
            unit: self.unit,
            display: format!("R {:.1$} {}", self.radius, decimals, self.unit.abbreviation()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ArcLengthMeasurement {
    pub arc: Arc,
    pub arc_length: f64,
    pub chord_length: f64,
    pub sweep_angle: f64,
    pub unit: MeasurementUnit,
}

impl ArcLengthMeasurement {
    pub fn new(arc: Arc, unit: MeasurementUnit) -> Self {
        let arc_length = arc.radius * (arc.end_angle - arc.start_angle).abs();
        
        let start = Point::new(
            arc.center.x + arc.radius * arc.start_angle.cos(),
            arc.center.y + arc.radius * arc.start_angle.sin(),
            0.0,
        );
        let end = Point::new(
            arc.center.x + arc.radius * arc.end_angle.cos(),
            arc.center.y + arc.radius * arc.end_angle.sin(),
            0.0,
        );
        let chord_length = start.distance_to(&end);
        
        let sweep_angle = (arc.end_angle - arc.start_angle).abs().to_degrees();
        
        Self {
            arc,
            arc_length,
            chord_length,
            sweep_angle,
            unit,
        }
    }
    
    pub fn format(&self, decimals: u32) -> String {
        format!(
            "Arc Length: {:.3$} | Chord: {:.3$} | Angle: {:.2$}°",
            self.arc_length,
            self.chord_length,
            self.sweep_angle,
            decimals
        )
    }
}

impl MeasurementTool for ArcLengthMeasurement {
    fn measure(&self) -> MeasurementResult {
        MeasurementResult {
            value: self.arc_length,
            unit: self.unit,
            display: format!("{:.3} {}", self.arc_length, self.unit.abbreviation()),
        }
    }
    
    fn measure_with_precision(&self, decimals: u32) -> MeasurementResult {
        MeasurementResult {
            value: self.arc_length,
            unit: self.unit,
            display: format!("{:.1$} {}", self.arc_length, decimals, self.unit.abbreviation()),
        }
    }
}

pub struct MeasurementCalculator;

impl MeasurementCalculator {
    pub fn calculate_distance(start: Point, end: Point, unit: MeasurementUnit) -> DistanceMeasurement {
        DistanceMeasurement::new(start, end, unit)
    }
    
    pub fn calculate_angle(vertex: Point, p1: Point, p2: Point, unit: MeasurementUnit) -> AngleMeasurement {
        AngleMeasurement::new(vertex, p1, p2, unit)
    }
    
    pub fn calculate_area_from_points(points: Vec<Point>, unit: MeasurementUnit) -> AreaMeasurement {
        AreaMeasurement::new(points, unit)
    }
    
    pub fn calculate_radius(center: Point, point: Point, unit: MeasurementUnit) -> RadiusMeasurement {
        RadiusMeasurement::new(center, point, unit)
    }
    
    pub fn calculate_circle_measurements(circle: &Circle, unit: MeasurementUnit) -> (RadiusMeasurement, AreaMeasurement) {
        let radius = RadiusMeasurement::from_circle(circle, unit);
        
        let circumference_points = vec![
            Point::new(circle.center.x + circle.radius, circle.center.y, 0.0),
            Point::new(circle.center.x, circle.center.y + circle.radius, 0.0),
            Point::new(circle.center.x - circle.radius, circle.center.y, 0.0),
            Point::new(circle.center.x, circle.center.y - circle.radius, 0.0),
        ];
        let area = AreaMeasurement::new(circumference_points, unit);
        
        (radius, area)
    }
    
    pub fn calculate_curve_length(curve: &Curve, tolerance: f64, unit: MeasurementUnit) -> f64 {
        match curve {
            Curve::Line(line) => line.length() * unit.conversion_factor(),
            Curve::Circle(circle) => 2.0 * std::f64::consts::PI * circle.radius * unit.conversion_factor(),
            Curve::Arc(arc) => arc.radius * (arc.end_angle - arc.start_angle).abs() * unit.conversion_factor(),
            _ => 0.0,
        }
    }
    
    pub fn convert_distance(value: f64, from: MeasurementUnit, to: MeasurementUnit) -> f64 {
        let base = value * from.conversion_factor();
        base / to.conversion_factor()
    }
    
    pub fn convert_area(value: f64, from: MeasurementUnit, to: MeasurementUnit) -> f64 {
        let base = value * from.conversion_factor().powi(2);
        base / to.conversion_factor().powi(2)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MeasurementSession {
    pub measurements: Vec<MeasurementResult>,
    pub session_start: std::time::SystemTime,
    pub unit: MeasurementUnit,
}

impl MeasurementSession {
    pub fn new(unit: MeasurementUnit) -> Self {
        Self {
            measurements: Vec::new(),
            session_start: std::time::SystemTime::now(),
            unit,
        }
    }
    
    pub fn add_measurement(&mut self, measurement: MeasurementResult) {
        self.measurements.push(measurement);
    }
    
    pub fn clear(&mut self) {
        self.measurements.clear();
    }
    
    pub fn get_statistics(&self) -> MeasurementStatistics {
        if self.measurements.is_empty() {
            return MeasurementStatistics::empty();
        }
        
        let values: Vec<f64> = self.measurements.iter().map(|m| m.value).collect();
        let sum: f64 = values.iter().sum();
        let count = values.len() as f64;
        let avg = sum / count;
        
        let min = *values.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
        let max = *values.iter().max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
        
        MeasurementStatistics {
            count: self.measurements.len(),
            sum,
            average: avg,
            minimum: min,
            maximum: max,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct MeasurementStatistics {
    pub count: usize,
    pub sum: f64,
    pub average: f64,
    pub minimum: f64,
    pub maximum: f64,
}

impl MeasurementStatistics {
    fn empty() -> Self {
        Self {
            count: 0,
            sum: 0.0,
            average: 0.0,
            minimum: 0.0,
            maximum: 0.0,
        }
    }
    
    pub fn format(&self, unit: MeasurementUnit) -> String {
        if self.count == 0 {
            "No measurements".to_string()
        } else {
            format!(
                "Count: {} | Sum: {:.3} | Avg: {:.3} | Min: {:.3} | Max: {:.3} {}",
                self.count,
                self.sum,
                self.average,
                self.minimum,
                self.maximum,
                unit.abbreviation()
            )
        }
    }
}