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
use serde::{Serialize, Deserialize};
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum GridType {
    Rectangular,
    Isometric,
    Polar,
    None,
}

impl Default for GridType {
    fn default() -> Self {
        GridType::Rectangular
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum GridSnapStyle {
    Standard,
    Isometric,
}

impl Default for GridSnapStyle {
    fn default() -> Self {
        GridSnapStyle::Standard
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GridSettings {
    pub grid_on: bool,
    pub grid_snap: bool,
    pub grid_type: GridType,
    pub grid_spacing_x: f64,
    pub grid_spacing_y: f64,
    pub grid_spacing_z: f64,
    pub grid_lines: u32,
    pub grid_major_lines: u32,
    pub grid_bounds: (f64, f64, f64, f64),
    pub grid_color: (u8, u8, u8),
    pub grid_axis_color: (u8, u8, u8),
    pub grid_line_weight: i32,
    pub display_grid_beyond_limits: bool,
    pub follow_ucs: bool,
    pub grid_snap_style: GridSnapStyle,
    pub grid_snap_angle: f64,
    pub polar_snap_spacing: f64,
    pub display_polar_tracking_path: bool,
}

impl Default for GridSettings {
    fn default() -> Self {
        Self {
            grid_on: true,
            grid_snap: true,
            grid_type: GridType::Rectangular,
            grid_spacing_x: 1.0,
            grid_spacing_y: 1.0,
            grid_spacing_z: 1.0,
            grid_lines: 100,
            grid_major_lines: 10,
            grid_bounds: (-100.0, -100.0, 100.0, 100.0),
            grid_color: (128, 128, 128),
            grid_axis_color: (255, 0, 0),
            grid_line_weight: 0,
            display_grid_beyond_limits: false,
            follow_ucs: true,
            grid_snap_style: GridSnapStyle::Standard,
            grid_snap_angle: 0.0,
            polar_snap_spacing: 90.0,
            display_polar_tracking_path: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GridPoint {
    pub x: f64,
    pub y: f64,
    pub is_major: bool,
    pub is_axis: bool,
}

impl GridPoint {
    pub fn new(x: f64, y: f64) -> Self {
        Self {
            x,
            y,
            is_major: false,
            is_axis: false,
        }
    }

    pub fn as_major(mut self) -> Self {
        self.is_major = true;
        self
    }

    pub fn as_axis(mut self) -> Self {
        self.is_axis = true;
        self
    }

    pub fn is_major(mut self, value: bool) -> Self {
        self.is_major = value;
        self
    }

    pub fn is_axis(mut self, value: bool) -> Self {
        self.is_axis = value;
        self
    }
}

pub struct Grid {
    settings: GridSettings,
    grid_points: Vec<GridPoint>,
    visible_bounds: (f64, f64, f64, f64),
}

impl Default for Grid {
    fn default() -> Self {
        Self {
            settings: GridSettings::default(),
            grid_points: Vec::new(),
            visible_bounds: (-100.0, -100.0, 100.0, 100.0),
        }
    }
}

impl Grid {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn new_with_spacing(spacing_x: f64, spacing_y: f64) -> Self {
        let mut grid = Self::default();
        grid.settings.grid_spacing_x = spacing_x;
        grid.settings.grid_spacing_y = spacing_y;
        grid
    }

    pub fn enable(&mut self, enabled: bool) {
        self.settings.grid_on = enabled;
    }

    pub fn is_enabled(&self) -> bool {
        self.settings.grid_on
    }

    pub fn enable_snap(&mut self, enabled: bool) {
        self.settings.grid_snap = enabled;
    }

    pub fn is_snap_enabled(&self) -> bool {
        self.settings.grid_snap
    }

    pub fn set_spacing(&mut self, x: f64, y: f64) {
        self.settings.grid_spacing_x = x;
        self.settings.grid_spacing_y = y;
    }

    pub fn get_spacing(&self) -> (f64, f64) {
        (self.settings.grid_spacing_x, self.settings.grid_spacing_y)
    }

    pub fn set_major_spacing(&mut self, major: u32) {
        self.settings.grid_major_lines = major;
    }

    pub fn get_major_spacing(&self) -> u32 {
        self.settings.grid_major_lines
    }

    pub fn set_bounds(&mut self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) {
        self.settings.grid_bounds = (min_x, min_y, max_x, max_y);
        self.visible_bounds = (min_x, min_y, max_x, max_y);
    }

    pub fn set_visible_bounds(&mut self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) {
        self.visible_bounds = (min_x, min_y, max_x, max_y);
    }

    pub fn set_type(&mut self, grid_type: GridType) {
        self.settings.grid_type = grid_type;
    }

    pub fn get_type(&self) -> GridType {
        self.settings.grid_type
    }

    pub fn set_color(&mut self, color: (u8, u8, u8)) {
        self.settings.grid_color = color;
    }

    pub fn set_axis_color(&mut self, color: (u8, u8, u8)) {
        self.settings.grid_axis_color = color;
    }

    pub fn settings(&self) -> &GridSettings {
        &self.settings
    }

    pub fn settings_mut(&mut self) -> &mut GridSettings {
        &mut self.settings
    }

    pub fn generate_grid(&mut self) {
        self.grid_points.clear();

        let (min_x, min_y, max_x, max_y) = self.visible_bounds;
        let spacing_x = self.settings.grid_spacing_x;
        let spacing_y = self.settings.grid_spacing_y;
        let major_spacing = self.settings.grid_major_lines;

        let start_x = (min_x / spacing_x).floor() * spacing_x;
        let start_y = (min_y / spacing_y).floor() * spacing_y;
        let end_x = (max_x / spacing_x).ceil() * spacing_x;
        let end_y = (max_y / spacing_y).ceil() * spacing_y;

        let mut x = start_x;
        let mut x_index = 0;
        while x <= end_x {
            let is_major = x_index % major_spacing == 0;
            let is_axis = x == 0.0;

            let mut y = start_y;
            let mut y_index = 0;
            while y <= end_y {
                let point = GridPoint::new(x, y)
                    .as_major()
                    .is_major(is_major && y_index % major_spacing == 0)
                    .is_axis(is_axis || y == 0.0);

                self.grid_points.push(point);

                y += spacing_y;
                y_index += 1;
            }

            x += spacing_x;
            x_index += 1;
        }
    }

    pub fn get_grid_points(&self) -> &[GridPoint] {
        &self.grid_points
    }

    pub fn snap_to_grid(&self, point: crate::geometry::Point) -> crate::geometry::Point {
        if !self.settings.grid_snap {
            return point;
        }

        let spacing_x = self.settings.grid_spacing_x;
        let spacing_y = self.settings.grid_spacing_y;

        let snapped_x = (point.x / spacing_x).round() * spacing_x;
        let snapped_y = (point.y / spacing_y).round() * spacing_y;

        crate::geometry::Point::new(snapped_x, snapped_y, point.z)
    }

    pub fn get_nearest_grid_point(&self, point: crate::geometry::Point) -> Option<GridPoint> {
        let mut nearest: Option<GridPoint> = None;
        let mut min_distance = f64::MAX;

        for grid_point in &self.grid_points {
            let distance = ((grid_point.x - point.x).powi(2) + (grid_point.y - point.y).powi(2)).sqrt();
            if distance < min_distance {
                min_distance = distance;
                nearest = Some(grid_point.clone());
            }
        }

        if min_distance < self.settings.grid_spacing_x.max(self.settings.grid_spacing_y) {
            nearest
        } else {
            None
        }
    }

    pub fn toggle(&mut self) -> bool {
        self.settings.grid_on = !self.settings.grid_on;
        self.settings.grid_on
    }

    pub fn toggle_snap(&mut self) -> bool {
        self.settings.grid_snap = !self.settings.grid_snap;
        self.settings.grid_snap
    }
}

impl fmt::Display for Grid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Grid(enabled={}, points={}, spacing=({}, {}))",
            self.settings.grid_on,
            self.grid_points.len(),
            self.settings.grid_spacing_x,
            self.settings.grid_spacing_y
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum OrthoMode {
    Off,
    Horizontal,
    Vertical,
    Both,
}

impl Default for OrthoMode {
    fn default() -> Self {
        OrthoMode::Off
    }
}

pub struct OrthoSettings {
    pub mode: OrthoMode,
    pub ortho_angle: f64,
    pub ortho_top_direction: f64,
    pub display_tool_tip: bool,
}

impl Default for OrthoSettings {
    fn default() -> Self {
        Self {
            mode: OrthoMode::Off,
            ortho_angle: 90.0,
            ortho_top_direction: 90.0,
            display_tool_tip: true,
        }
    }
}

pub struct Ortho {
    settings: OrthoSettings,
}

impl Default for Ortho {
    fn default() -> Self {
        Self {
            settings: OrthoSettings::default(),
        }
    }
}

impl Ortho {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn enable(&mut self, enabled: bool) {
        self.settings.mode = if enabled { OrthoMode::Both } else { OrthoMode::Off };
    }

    pub fn disable(&mut self) {
        self.settings.mode = OrthoMode::Off;
    }

    pub fn is_enabled(&self) -> bool {
        self.settings.mode != OrthoMode::Off
    }

    pub fn set_mode(&mut self, mode: OrthoMode) {
        self.settings.mode = mode;
    }

    pub fn get_mode(&self) -> OrthoMode {
        self.settings.mode
    }

    pub fn toggle(&mut self) -> bool {
        if self.settings.mode == OrthoMode::Off {
            self.settings.mode = OrthoMode::Both;
            true
        } else {
            self.settings.mode = OrthoMode::Off;
            false
        }
    }

    pub fn constrain(
        &self,
        from_point: crate::geometry::Point,
        to_point: crate::geometry::Point,
    ) -> crate::geometry::Point {
        if self.settings.mode == OrthoMode::Off {
            return to_point;
        }

        let dx = to_point.x - from_point.x;
        let dy = to_point.y - from_point.y;

        match self.settings.mode {
            OrthoMode::Horizontal => {
                crate::geometry::Point::new(to_point.x, from_point.y, to_point.z)
            }
            OrthoMode::Vertical => {
                crate::geometry::Point::new(from_point.x, to_point.y, to_point.z)
            }
            OrthoMode::Both => {
                if dx.abs() >= dy.abs() {
                    crate::geometry::Point::new(to_point.x, from_point.y, to_point.z)
                } else {
                    crate::geometry::Point::new(from_point.x, to_point.y, to_point.z)
                }
            }
            OrthoMode::Off => to_point,
        }
    }

    pub fn get_directions(&self) -> Vec<f64> {
        match self.settings.mode {
            OrthoMode::Horizontal => vec![0.0, 180.0],
            OrthoMode::Vertical => vec![90.0, 270.0],
            OrthoMode::Both => vec![0.0, 90.0, 180.0, 270.0],
            OrthoMode::Off => vec![],
        }
    }
}

impl fmt::Display for Ortho {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Ortho(mode={:?})", self.settings.mode)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_grid_creation() {
        let grid = Grid::new_with_spacing(1.0, 1.0);
        assert!(grid.is_enabled());
        assert!(grid.is_snap_enabled());
    }

    #[test]
    fn test_grid_snap() {
        let grid = Grid::new_with_spacing(1.0, 1.0);
        let point = crate::geometry::Point::new(1.3, 2.7, 0.0);
        let snapped = grid.snap_to_grid(point);
        assert_eq!(snapped.x, 1.0);
        assert_eq!(snapped.y, 3.0);
    }

    #[test]
    fn test_ortho_constrain() {
        let ortho = Ortho::new();
        let from = crate::geometry::Point::new(0.0, 0.0, 0.0);
        let to = crate::geometry::Point::new(1.0, 1.0, 0.0);

        let constrained = ortho.constrain(from, to);
        assert_eq!(constrained.x, 1.0);
        assert_eq!(constrained.y, 0.0);
    }

    #[test]
    fn test_ortho_toggle() {
        let mut ortho = Ortho::new();
        assert!(!ortho.is_enabled());

        ortho.enable(true);
        assert!(ortho.is_enabled());

        ortho.disable();
        assert!(!ortho.is_enabled());

        assert!(ortho.toggle());
        assert!(ortho.is_enabled());

        assert!(!ortho.toggle());
        assert!(!ortho.is_enabled());
    }
}