Skip to main content

kcl_lib/execution/
geometry.rs

1use std::f64::consts::TAU;
2use std::ops::Add;
3use std::ops::AddAssign;
4use std::ops::Mul;
5use std::ops::Sub;
6use std::ops::SubAssign;
7
8use anyhow::Result;
9use indexmap::IndexMap;
10use kcl_error::SourceRange;
11use kittycad_modeling_cmds::ModelingCmd;
12use kittycad_modeling_cmds::each_cmd as mcmd;
13use kittycad_modeling_cmds::length_unit::LengthUnit;
14use kittycad_modeling_cmds::units::UnitLength;
15use kittycad_modeling_cmds::websocket::ModelingCmdReq;
16use kittycad_modeling_cmds::{self as kcmc};
17use parse_display::Display;
18use parse_display::FromStr;
19use serde::Deserialize;
20use serde::Serialize;
21use uuid::Uuid;
22
23use crate::NodePath;
24use crate::engine::DEFAULT_PLANE_INFO;
25use crate::engine::PlaneName;
26use crate::errors::KclError;
27use crate::errors::KclErrorDetails;
28use crate::exec::KclValue;
29use crate::execution::ArtifactId;
30use crate::execution::ExecState;
31use crate::execution::ExecutorContext;
32use crate::execution::Metadata;
33use crate::execution::TagEngineInfo;
34use crate::execution::TagIdentifier;
35use crate::execution::normalize_to_solver_distance_unit;
36use crate::execution::types::NumericType;
37use crate::execution::types::adjust_length;
38use crate::front::ArcCtor;
39use crate::front::CircleCtor;
40use crate::front::Freedom;
41use crate::front::LineCtor;
42use crate::front::ObjectId;
43use crate::front::PointCtor;
44use crate::parsing::ast::types::Node;
45use crate::parsing::ast::types::NodeRef;
46use crate::parsing::ast::types::TagDeclarator;
47use crate::parsing::ast::types::TagNode;
48use crate::std::Args;
49use crate::std::args::TyF64;
50use crate::std::sketch::FaceTag;
51use crate::std::sketch::PlaneData;
52
53type Point3D = kcmc::shared::Point3d<f64>;
54
55/// A GD&T annotation.
56#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
57#[ts(export)]
58#[serde(tag = "type", rename_all = "camelCase")]
59pub struct GdtAnnotation {
60    /// The engine ID.
61    pub id: uuid::Uuid,
62    #[serde(skip)]
63    pub meta: Vec<Metadata>,
64}
65
66/// A geometry.
67#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
68#[ts(export)]
69#[serde(tag = "type")]
70#[allow(clippy::large_enum_variant)]
71pub enum Geometry {
72    Sketch(Sketch),
73    Solid(Solid),
74}
75
76impl Geometry {
77    pub fn id(&self) -> uuid::Uuid {
78        match self {
79            Geometry::Sketch(s) => s.id,
80            Geometry::Solid(e) => e.id,
81        }
82    }
83
84    /// If this geometry is the result of a pattern, then return the ID of
85    /// the original sketch which was patterned.
86    /// Equivalent to the `id()` method if this isn't a pattern.
87    pub fn original_id(&self) -> uuid::Uuid {
88        match self {
89            Geometry::Sketch(s) => s.original_id,
90            Geometry::Solid(e) => e.original_id(),
91        }
92    }
93}
94
95/// A geometry including an imported geometry.
96#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
97#[ts(export)]
98#[serde(tag = "type")]
99#[allow(clippy::large_enum_variant)]
100pub enum GeometryWithImportedGeometry {
101    Sketch(Sketch),
102    Solid(Solid),
103    ImportedGeometry(Box<ImportedGeometry>),
104}
105
106impl GeometryWithImportedGeometry {
107    pub async fn id(&mut self, ctx: &ExecutorContext) -> Result<uuid::Uuid, KclError> {
108        match self {
109            GeometryWithImportedGeometry::Sketch(s) => Ok(s.id),
110            GeometryWithImportedGeometry::Solid(e) => Ok(e.id),
111            GeometryWithImportedGeometry::ImportedGeometry(i) => {
112                let id = i.id(ctx).await?;
113                Ok(id)
114            }
115        }
116    }
117}
118
119/// A set of geometry.
120#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
121#[ts(export)]
122#[serde(tag = "type")]
123#[allow(clippy::vec_box)]
124pub enum Geometries {
125    Sketches(Vec<Sketch>),
126    Solids(Vec<Solid>),
127}
128
129impl From<Geometry> for Geometries {
130    fn from(value: Geometry) -> Self {
131        match value {
132            Geometry::Sketch(x) => Self::Sketches(vec![x]),
133            Geometry::Solid(x) => Self::Solids(vec![x]),
134        }
135    }
136}
137
138/// Data for an imported geometry.
139#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
140#[ts(export)]
141#[serde(rename_all = "camelCase")]
142pub struct ImportedGeometry {
143    /// The ID of the imported geometry.
144    pub id: uuid::Uuid,
145    /// The original file paths.
146    pub value: Vec<String>,
147    #[serde(skip)]
148    pub meta: Vec<Metadata>,
149    /// If the imported geometry has completed.
150    #[serde(skip)]
151    completed: bool,
152}
153
154impl ImportedGeometry {
155    pub fn new(id: uuid::Uuid, value: Vec<String>, meta: Vec<Metadata>) -> Self {
156        Self {
157            id,
158            value,
159            meta,
160            completed: false,
161        }
162    }
163
164    async fn wait_for_finish(&mut self, ctx: &ExecutorContext) -> Result<(), KclError> {
165        if self.completed {
166            return Ok(());
167        }
168
169        ctx.engine
170            .ensure_async_command_completed(self.id, self.meta.first().map(|m| m.source_range))
171            .await?;
172
173        self.completed = true;
174
175        Ok(())
176    }
177
178    pub async fn id(&mut self, ctx: &ExecutorContext) -> Result<uuid::Uuid, KclError> {
179        if !self.completed {
180            self.wait_for_finish(ctx).await?;
181        }
182
183        Ok(self.id)
184    }
185}
186
187/// Data for a solid, sketch, or an imported geometry.
188#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
189#[ts(export)]
190#[serde(tag = "type", rename_all = "camelCase")]
191#[allow(clippy::vec_box)]
192pub enum HideableGeometry {
193    ImportedGeometry(Box<ImportedGeometry>),
194    SolidSet(Vec<Solid>),
195    SketchSet(Vec<Sketch>),
196    HelixSet(Vec<Helix>),
197}
198
199impl From<HideableGeometry> for crate::execution::KclValue {
200    fn from(value: HideableGeometry) -> Self {
201        match value {
202            HideableGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
203            HideableGeometry::SolidSet(mut s) => {
204                if s.len() == 1
205                    && let Some(s) = s.pop()
206                {
207                    crate::execution::KclValue::Solid { value: Box::new(s) }
208                } else {
209                    crate::execution::KclValue::HomArray {
210                        value: s
211                            .into_iter()
212                            .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
213                            .collect(),
214                        ty: crate::execution::types::RuntimeType::solid(),
215                    }
216                }
217            }
218            HideableGeometry::SketchSet(mut s) => {
219                if s.len() == 1
220                    && let Some(s) = s.pop()
221                {
222                    crate::execution::KclValue::Sketch { value: Box::new(s) }
223                } else {
224                    crate::execution::KclValue::HomArray {
225                        value: s
226                            .into_iter()
227                            .map(|s| crate::execution::KclValue::Sketch { value: Box::new(s) })
228                            .collect(),
229                        ty: crate::execution::types::RuntimeType::sketch(),
230                    }
231                }
232            }
233            HideableGeometry::HelixSet(mut s) => {
234                if s.len() == 1
235                    && let Some(s) = s.pop()
236                {
237                    crate::execution::KclValue::Helix { value: Box::new(s) }
238                } else {
239                    crate::execution::KclValue::HomArray {
240                        value: s
241                            .into_iter()
242                            .map(|s| crate::execution::KclValue::Helix { value: Box::new(s) })
243                            .collect(),
244                        ty: crate::execution::types::RuntimeType::helices(),
245                    }
246                }
247            }
248        }
249    }
250}
251
252impl HideableGeometry {
253    pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
254        match self {
255            HideableGeometry::ImportedGeometry(s) => {
256                let id = s.id(ctx).await?;
257
258                Ok(vec![id])
259            }
260            HideableGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
261            HideableGeometry::SketchSet(s) => Ok(s.iter().map(|s| s.id).collect()),
262            HideableGeometry::HelixSet(s) => Ok(s.iter().map(|s| s.value).collect()),
263        }
264    }
265}
266
267/// Data for a solid, sketch, or an imported geometry.
268#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
269#[ts(export)]
270#[serde(tag = "type", rename_all = "camelCase")]
271#[allow(clippy::vec_box)]
272pub enum SolidOrSketchOrImportedGeometry {
273    ImportedGeometry(Box<ImportedGeometry>),
274    SolidSet(Vec<Solid>),
275    SketchSet(Vec<Sketch>),
276}
277
278impl From<SolidOrSketchOrImportedGeometry> for crate::execution::KclValue {
279    fn from(value: SolidOrSketchOrImportedGeometry) -> Self {
280        match value {
281            SolidOrSketchOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
282            SolidOrSketchOrImportedGeometry::SolidSet(mut s) => {
283                if s.len() == 1
284                    && let Some(s) = s.pop()
285                {
286                    crate::execution::KclValue::Solid { value: Box::new(s) }
287                } else {
288                    crate::execution::KclValue::HomArray {
289                        value: s
290                            .into_iter()
291                            .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
292                            .collect(),
293                        ty: crate::execution::types::RuntimeType::solid(),
294                    }
295                }
296            }
297            SolidOrSketchOrImportedGeometry::SketchSet(mut s) => {
298                if s.len() == 1
299                    && let Some(s) = s.pop()
300                {
301                    crate::execution::KclValue::Sketch { value: Box::new(s) }
302                } else {
303                    crate::execution::KclValue::HomArray {
304                        value: s
305                            .into_iter()
306                            .map(|s| crate::execution::KclValue::Sketch { value: Box::new(s) })
307                            .collect(),
308                        ty: crate::execution::types::RuntimeType::sketch(),
309                    }
310                }
311            }
312        }
313    }
314}
315
316impl SolidOrSketchOrImportedGeometry {
317    pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
318        match self {
319            SolidOrSketchOrImportedGeometry::ImportedGeometry(s) => {
320                let id = s.id(ctx).await?;
321
322                Ok(vec![id])
323            }
324            SolidOrSketchOrImportedGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
325            SolidOrSketchOrImportedGeometry::SketchSet(s) => Ok(s.iter().map(|s| s.id).collect()),
326        }
327    }
328}
329
330/// Data for a solid or an imported geometry.
331#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
332#[ts(export)]
333#[serde(tag = "type", rename_all = "camelCase")]
334#[allow(clippy::vec_box)]
335pub enum SolidOrImportedGeometry {
336    ImportedGeometry(Box<ImportedGeometry>),
337    SolidSet(Vec<Solid>),
338}
339
340impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
341    fn from(value: SolidOrImportedGeometry) -> Self {
342        match value {
343            SolidOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
344            SolidOrImportedGeometry::SolidSet(mut s) => {
345                if s.len() == 1
346                    && let Some(s) = s.pop()
347                {
348                    crate::execution::KclValue::Solid { value: Box::new(s) }
349                } else {
350                    crate::execution::KclValue::HomArray {
351                        value: s
352                            .into_iter()
353                            .map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
354                            .collect(),
355                        ty: crate::execution::types::RuntimeType::solid(),
356                    }
357                }
358            }
359        }
360    }
361}
362
363impl SolidOrImportedGeometry {
364    pub(crate) async fn ids(&mut self, ctx: &ExecutorContext) -> Result<Vec<uuid::Uuid>, KclError> {
365        match self {
366            SolidOrImportedGeometry::ImportedGeometry(s) => {
367                let id = s.id(ctx).await?;
368
369                Ok(vec![id])
370            }
371            SolidOrImportedGeometry::SolidSet(s) => Ok(s.iter().map(|s| s.id).collect()),
372        }
373    }
374}
375
376/// A helix.
377#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
378#[ts(export)]
379#[serde(rename_all = "camelCase")]
380pub struct Helix {
381    /// The id of the helix.
382    pub value: uuid::Uuid,
383    /// The artifact ID.
384    pub artifact_id: ArtifactId,
385    /// Number of revolutions.
386    pub revolutions: f64,
387    /// Start angle (in degrees).
388    pub angle_start: f64,
389    /// Is the helix rotation counter clockwise?
390    pub ccw: bool,
391    /// The cylinder the helix was created on.
392    pub cylinder_id: Option<uuid::Uuid>,
393    pub units: UnitLength,
394    #[serde(skip)]
395    pub meta: Vec<Metadata>,
396}
397
398#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
399#[ts(export)]
400#[serde(rename_all = "camelCase")]
401pub struct Plane {
402    /// The id of the plane.
403    pub id: uuid::Uuid,
404    /// The artifact ID.
405    pub artifact_id: ArtifactId,
406    /// The scene object ID. If this is None, then the plane has not been
407    /// sent to the engine yet. It must be sent before it is used.
408    #[serde(skip_serializing_if = "Option::is_none")]
409    pub object_id: Option<ObjectId>,
410    /// The kind of plane or custom.
411    pub kind: PlaneKind,
412    /// The information for the plane.
413    #[serde(flatten)]
414    pub info: PlaneInfo,
415    #[serde(skip)]
416    pub meta: Vec<Metadata>,
417}
418
419#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ts_rs::TS)]
420#[ts(export)]
421#[serde(rename_all = "camelCase")]
422pub struct PlaneInfo {
423    /// Origin of the plane.
424    pub origin: Point3d,
425    /// What should the plane's X axis be?
426    pub x_axis: Point3d,
427    /// What should the plane's Y axis be?
428    pub y_axis: Point3d,
429    /// What should the plane's Z axis be?
430    pub z_axis: Point3d,
431}
432
433impl PlaneInfo {
434    pub(crate) fn into_plane_data(self) -> PlaneData {
435        if self.origin.is_zero() {
436            match self {
437                Self {
438                    origin:
439                        Point3d {
440                            x: 0.0,
441                            y: 0.0,
442                            z: 0.0,
443                            units: Some(UnitLength::Millimeters),
444                        },
445                    x_axis:
446                        Point3d {
447                            x: 1.0,
448                            y: 0.0,
449                            z: 0.0,
450                            units: _,
451                        },
452                    y_axis:
453                        Point3d {
454                            x: 0.0,
455                            y: 1.0,
456                            z: 0.0,
457                            units: _,
458                        },
459                    z_axis: _,
460                } => return PlaneData::XY,
461                Self {
462                    origin:
463                        Point3d {
464                            x: 0.0,
465                            y: 0.0,
466                            z: 0.0,
467                            units: Some(UnitLength::Millimeters),
468                        },
469                    x_axis:
470                        Point3d {
471                            x: -1.0,
472                            y: 0.0,
473                            z: 0.0,
474                            units: _,
475                        },
476                    y_axis:
477                        Point3d {
478                            x: 0.0,
479                            y: 1.0,
480                            z: 0.0,
481                            units: _,
482                        },
483                    z_axis: _,
484                } => return PlaneData::NegXY,
485                Self {
486                    origin:
487                        Point3d {
488                            x: 0.0,
489                            y: 0.0,
490                            z: 0.0,
491                            units: Some(UnitLength::Millimeters),
492                        },
493                    x_axis:
494                        Point3d {
495                            x: 1.0,
496                            y: 0.0,
497                            z: 0.0,
498                            units: _,
499                        },
500                    y_axis:
501                        Point3d {
502                            x: 0.0,
503                            y: 0.0,
504                            z: 1.0,
505                            units: _,
506                        },
507                    z_axis: _,
508                } => return PlaneData::XZ,
509                Self {
510                    origin:
511                        Point3d {
512                            x: 0.0,
513                            y: 0.0,
514                            z: 0.0,
515                            units: Some(UnitLength::Millimeters),
516                        },
517                    x_axis:
518                        Point3d {
519                            x: -1.0,
520                            y: 0.0,
521                            z: 0.0,
522                            units: _,
523                        },
524                    y_axis:
525                        Point3d {
526                            x: 0.0,
527                            y: 0.0,
528                            z: 1.0,
529                            units: _,
530                        },
531                    z_axis: _,
532                } => return PlaneData::NegXZ,
533                Self {
534                    origin:
535                        Point3d {
536                            x: 0.0,
537                            y: 0.0,
538                            z: 0.0,
539                            units: Some(UnitLength::Millimeters),
540                        },
541                    x_axis:
542                        Point3d {
543                            x: 0.0,
544                            y: 1.0,
545                            z: 0.0,
546                            units: _,
547                        },
548                    y_axis:
549                        Point3d {
550                            x: 0.0,
551                            y: 0.0,
552                            z: 1.0,
553                            units: _,
554                        },
555                    z_axis: _,
556                } => return PlaneData::YZ,
557                Self {
558                    origin:
559                        Point3d {
560                            x: 0.0,
561                            y: 0.0,
562                            z: 0.0,
563                            units: Some(UnitLength::Millimeters),
564                        },
565                    x_axis:
566                        Point3d {
567                            x: 0.0,
568                            y: -1.0,
569                            z: 0.0,
570                            units: _,
571                        },
572                    y_axis:
573                        Point3d {
574                            x: 0.0,
575                            y: 0.0,
576                            z: 1.0,
577                            units: _,
578                        },
579                    z_axis: _,
580                } => return PlaneData::NegYZ,
581                _ => {}
582            }
583        }
584
585        PlaneData::Plane(Self {
586            origin: self.origin,
587            x_axis: self.x_axis,
588            y_axis: self.y_axis,
589            z_axis: self.z_axis,
590        })
591    }
592
593    pub(crate) fn is_right_handed(&self) -> bool {
594        // Katie's formula:
595        // dot(cross(x, y), z) ~= sqrt(dot(x, x) * dot(y, y) * dot(z, z))
596        let lhs = self
597            .x_axis
598            .axes_cross_product(&self.y_axis)
599            .axes_dot_product(&self.z_axis);
600        let rhs_x = self.x_axis.axes_dot_product(&self.x_axis);
601        let rhs_y = self.y_axis.axes_dot_product(&self.y_axis);
602        let rhs_z = self.z_axis.axes_dot_product(&self.z_axis);
603        let rhs = (rhs_x * rhs_y * rhs_z).sqrt();
604        // Check LHS ~= RHS
605        (lhs - rhs).abs() <= 0.0001
606    }
607
608    #[cfg(test)]
609    pub(crate) fn is_left_handed(&self) -> bool {
610        !self.is_right_handed()
611    }
612
613    pub(crate) fn make_right_handed(self) -> Self {
614        if self.is_right_handed() {
615            return self;
616        }
617        // To make it right-handed, negate X, i.e. rotate the plane 180 degrees.
618        Self {
619            origin: self.origin,
620            x_axis: self.x_axis.negated(),
621            y_axis: self.y_axis,
622            z_axis: self.z_axis,
623        }
624    }
625}
626
627impl TryFrom<PlaneData> for PlaneInfo {
628    type Error = KclError;
629
630    fn try_from(value: PlaneData) -> Result<Self, Self::Error> {
631        let name = match value {
632            PlaneData::XY => PlaneName::Xy,
633            PlaneData::NegXY => PlaneName::NegXy,
634            PlaneData::XZ => PlaneName::Xz,
635            PlaneData::NegXZ => PlaneName::NegXz,
636            PlaneData::YZ => PlaneName::Yz,
637            PlaneData::NegYZ => PlaneName::NegYz,
638            PlaneData::Plane(info) => {
639                return Ok(info);
640            }
641        };
642
643        let info = DEFAULT_PLANE_INFO.get(&name).ok_or_else(|| {
644            KclError::new_internal(KclErrorDetails::new(
645                format!("Plane {name} not found"),
646                Default::default(),
647            ))
648        })?;
649
650        Ok(info.clone())
651    }
652}
653
654impl From<&PlaneData> for PlaneKind {
655    fn from(value: &PlaneData) -> Self {
656        match value {
657            PlaneData::XY => PlaneKind::XY,
658            PlaneData::NegXY => PlaneKind::XY,
659            PlaneData::XZ => PlaneKind::XZ,
660            PlaneData::NegXZ => PlaneKind::XZ,
661            PlaneData::YZ => PlaneKind::YZ,
662            PlaneData::NegYZ => PlaneKind::YZ,
663            PlaneData::Plane(_) => PlaneKind::Custom,
664        }
665    }
666}
667
668impl From<&PlaneInfo> for PlaneKind {
669    fn from(value: &PlaneInfo) -> Self {
670        let data = PlaneData::Plane(value.clone());
671        PlaneKind::from(&data)
672    }
673}
674
675impl From<PlaneInfo> for PlaneKind {
676    fn from(value: PlaneInfo) -> Self {
677        let data = PlaneData::Plane(value);
678        PlaneKind::from(&data)
679    }
680}
681
682impl Plane {
683    #[cfg(test)]
684    pub(crate) fn from_plane_data_skipping_engine(
685        value: PlaneData,
686        exec_state: &mut ExecState,
687    ) -> Result<Self, KclError> {
688        let id = exec_state.next_uuid();
689        let kind = PlaneKind::from(&value);
690        Ok(Plane {
691            id,
692            artifact_id: id.into(),
693            info: PlaneInfo::try_from(value)?,
694            object_id: None,
695            kind,
696            meta: vec![],
697        })
698    }
699
700    /// Returns true if the plane has been sent to the engine.
701    pub fn is_initialized(&self) -> bool {
702        self.object_id.is_some()
703    }
704
705    /// Returns true if the plane has not been sent to the engine yet.
706    pub fn is_uninitialized(&self) -> bool {
707        !self.is_initialized()
708    }
709
710    /// The standard planes are XY, YZ and XZ (in both positive and negative)
711    pub fn is_standard(&self) -> bool {
712        match &self.kind {
713            PlaneKind::XY | PlaneKind::YZ | PlaneKind::XZ => true,
714            PlaneKind::Custom => false,
715        }
716    }
717
718    /// Project a point onto a plane by calculating how far away it is and moving it along the
719    /// normal of the plane so that it now lies on the plane.
720    pub fn project(&self, point: Point3d) -> Point3d {
721        let v = point - self.info.origin;
722        let dot = v.axes_dot_product(&self.info.z_axis);
723
724        point - self.info.z_axis * dot
725    }
726}
727
728/// A face.
729#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
730#[ts(export)]
731#[serde(rename_all = "camelCase")]
732pub struct Face {
733    /// The id of the face.
734    pub id: uuid::Uuid,
735    /// The artifact ID.
736    pub artifact_id: ArtifactId,
737    /// The scene object ID.
738    pub object_id: ObjectId,
739    /// The tag of the face.
740    pub value: String,
741    /// What should the face's X axis be?
742    pub x_axis: Point3d,
743    /// What should the face's Y axis be?
744    pub y_axis: Point3d,
745    /// The solid the face is on.
746    pub solid: Box<Solid>,
747    pub units: UnitLength,
748    #[serde(skip)]
749    pub meta: Vec<Metadata>,
750}
751
752/// A bounded edge.
753#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
754#[ts(export)]
755#[serde(rename_all = "camelCase")]
756pub struct BoundedEdge {
757    /// The id of the face this edge belongs to.
758    pub face_id: uuid::Uuid,
759    /// The id of the edge.
760    pub edge_id: uuid::Uuid,
761    /// A percentage bound of the edge, used to restrict what portion of the edge will be used.
762    /// Range (0, 1)
763    pub lower_bound: f32,
764    /// A percentage bound of the edge, used to restrict what portion of the edge will be used.
765    /// Range (0, 1)
766    pub upper_bound: f32,
767}
768
769/// Kind of plane.
770#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, ts_rs::TS, FromStr, Display)]
771#[ts(export)]
772#[display(style = "camelCase")]
773pub enum PlaneKind {
774    #[serde(rename = "XY", alias = "xy")]
775    #[display("XY")]
776    XY,
777    #[serde(rename = "XZ", alias = "xz")]
778    #[display("XZ")]
779    XZ,
780    #[serde(rename = "YZ", alias = "yz")]
781    #[display("YZ")]
782    YZ,
783    /// A custom plane.
784    #[display("Custom")]
785    Custom,
786}
787
788#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
789#[ts(export)]
790#[serde(tag = "type", rename_all = "camelCase")]
791pub struct Sketch {
792    /// The id of the sketch (this will change when the engine's reference to it changes).
793    pub id: uuid::Uuid,
794    /// The paths in the sketch.
795    /// Only paths on the "outside" i.e. the perimeter.
796    /// Does not include paths "inside" the profile (for example, edges made by subtracting a profile)
797    pub paths: Vec<Path>,
798    /// Inner paths, resulting from subtract2d to carve profiles out of the sketch.
799    #[serde(default, skip_serializing_if = "Vec::is_empty")]
800    pub inner_paths: Vec<Path>,
801    /// What the sketch is on (can be a plane or a face).
802    pub on: SketchSurface,
803    /// The starting path.
804    pub start: BasePath,
805    /// Tag identifiers that have been declared in this sketch.
806    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
807    pub tags: IndexMap<String, TagIdentifier>,
808    /// The original id of the sketch. This stays the same even if the sketch is
809    /// is sketched on face etc.
810    pub artifact_id: ArtifactId,
811    #[ts(skip)]
812    pub original_id: uuid::Uuid,
813    /// If the sketch includes a mirror.
814    #[serde(skip)]
815    pub mirror: Option<uuid::Uuid>,
816    /// If the sketch is a clone of another sketch.
817    #[serde(skip)]
818    pub clone: Option<uuid::Uuid>,
819    /// Synthetic pen-jump paths inserted to replay disconnected segment selections.
820    #[serde(skip)]
821    #[ts(skip)]
822    pub synthetic_jump_path_ids: Vec<uuid::Uuid>,
823    pub units: UnitLength,
824    /// Metadata.
825    #[serde(skip)]
826    pub meta: Vec<Metadata>,
827    /// Has the profile been closed?
828    /// If not given, defaults to yes, closed explicitly.
829    #[serde(
830        default = "ProfileClosed::explicitly",
831        skip_serializing_if = "ProfileClosed::is_explicitly"
832    )]
833    pub is_closed: ProfileClosed,
834}
835
836impl ProfileClosed {
837    #[expect(dead_code, reason = "it's not actually dead, it's called by serde")]
838    fn explicitly() -> Self {
839        Self::Explicitly
840    }
841
842    fn is_explicitly(&self) -> bool {
843        matches!(self, ProfileClosed::Explicitly)
844    }
845}
846
847/// Has the profile been closed?
848#[derive(Debug, Serialize, Eq, PartialEq, Clone, Copy, Hash, Ord, PartialOrd, ts_rs::TS)]
849#[serde(rename_all = "camelCase")]
850pub enum ProfileClosed {
851    /// It's definitely open.
852    No,
853    /// Unknown.
854    Maybe,
855    /// Yes, by adding a segment which loops back to the start.
856    Implicitly,
857    /// Yes, by calling `close()` or by making a closed shape (e.g. circle).
858    Explicitly,
859}
860
861impl Sketch {
862    // Tell the engine to enter sketch mode on the sketch.
863    // Run a specific command, then exit sketch mode.
864    pub(crate) fn build_sketch_mode_cmds(
865        &self,
866        exec_state: &mut ExecState,
867        inner_cmd: ModelingCmdReq,
868    ) -> Vec<ModelingCmdReq> {
869        vec![
870            // Before we extrude, we need to enable the sketch mode.
871            // We do this here in case extrude is called out of order.
872            ModelingCmdReq {
873                cmd: ModelingCmd::from(
874                    mcmd::EnableSketchMode::builder()
875                        .animated(false)
876                        .ortho(false)
877                        .entity_id(self.on.id())
878                        .adjust_camera(false)
879                        .maybe_planar_normal(if let SketchSurface::Plane(plane) = &self.on {
880                            // We pass in the normal for the plane here.
881                            let normal = plane.info.x_axis.axes_cross_product(&plane.info.y_axis);
882                            Some(normal.into())
883                        } else {
884                            None
885                        })
886                        .build(),
887                ),
888                cmd_id: exec_state.next_uuid().into(),
889            },
890            inner_cmd,
891            ModelingCmdReq {
892                cmd: ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::builder().build()),
893                cmd_id: exec_state.next_uuid().into(),
894            },
895        ]
896    }
897}
898
899/// A sketch type.
900#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
901#[ts(export)]
902#[serde(tag = "type", rename_all = "camelCase")]
903pub enum SketchSurface {
904    Plane(Box<Plane>),
905    Face(Box<Face>),
906}
907
908impl SketchSurface {
909    pub(crate) fn id(&self) -> uuid::Uuid {
910        match self {
911            SketchSurface::Plane(plane) => plane.id,
912            SketchSurface::Face(face) => face.id,
913        }
914    }
915    pub(crate) fn x_axis(&self) -> Point3d {
916        match self {
917            SketchSurface::Plane(plane) => plane.info.x_axis,
918            SketchSurface::Face(face) => face.x_axis,
919        }
920    }
921    pub(crate) fn y_axis(&self) -> Point3d {
922        match self {
923            SketchSurface::Plane(plane) => plane.info.y_axis,
924            SketchSurface::Face(face) => face.y_axis,
925        }
926    }
927
928    pub(crate) fn object_id(&self) -> Option<ObjectId> {
929        match self {
930            SketchSurface::Plane(plane) => plane.object_id,
931            SketchSurface::Face(face) => Some(face.object_id),
932        }
933    }
934
935    pub(crate) fn set_object_id(&mut self, object_id: ObjectId) {
936        match self {
937            SketchSurface::Plane(plane) => plane.object_id = Some(object_id),
938            SketchSurface::Face(face) => face.object_id = object_id,
939        }
940    }
941}
942
943/// A Sketch, Face, or TaggedFace.
944#[derive(Debug, Clone, PartialEq)]
945pub enum Extrudable {
946    /// Sketch.
947    Sketch(Box<Sketch>),
948    /// Face.
949    Face(FaceTag),
950}
951
952impl Extrudable {
953    /// Get the relevant id.
954    pub async fn id_to_extrude(
955        &self,
956        exec_state: &mut ExecState,
957        args: &Args,
958        must_be_planar: bool,
959    ) -> Result<uuid::Uuid, KclError> {
960        match self {
961            Extrudable::Sketch(sketch) => Ok(sketch.id),
962            Extrudable::Face(face_tag) => face_tag.get_face_id_from_tag(exec_state, args, must_be_planar).await,
963        }
964    }
965
966    pub fn as_sketch(&self) -> Option<Sketch> {
967        match self {
968            Extrudable::Sketch(sketch) => Some((**sketch).clone()),
969            Extrudable::Face(face_tag) => match face_tag.geometry() {
970                Some(Geometry::Sketch(sketch)) => Some(sketch),
971                Some(Geometry::Solid(solid)) => solid.sketch().cloned(),
972                None => None,
973            },
974        }
975    }
976
977    pub fn is_closed(&self) -> ProfileClosed {
978        match self {
979            Extrudable::Sketch(sketch) => sketch.is_closed,
980            Extrudable::Face(face_tag) => match face_tag.geometry() {
981                Some(Geometry::Sketch(sketch)) => sketch.is_closed,
982                Some(Geometry::Solid(solid)) => solid
983                    .sketch()
984                    .map(|sketch| sketch.is_closed)
985                    .unwrap_or(ProfileClosed::Maybe),
986                _ => ProfileClosed::Maybe,
987            },
988        }
989    }
990}
991
992impl From<Sketch> for Extrudable {
993    fn from(value: Sketch) -> Self {
994        Extrudable::Sketch(Box::new(value))
995    }
996}
997
998#[derive(Debug, Clone)]
999pub(crate) enum GetTangentialInfoFromPathsResult {
1000    PreviousPoint([f64; 2]),
1001    Arc {
1002        center: [f64; 2],
1003        ccw: bool,
1004    },
1005    Circle {
1006        center: [f64; 2],
1007        ccw: bool,
1008        radius: f64,
1009    },
1010    Ellipse {
1011        center: [f64; 2],
1012        ccw: bool,
1013        major_axis: [f64; 2],
1014        _minor_radius: f64,
1015    },
1016}
1017
1018impl GetTangentialInfoFromPathsResult {
1019    pub(crate) fn tan_previous_point(&self, last_arc_end: [f64; 2]) -> [f64; 2] {
1020        match self {
1021            GetTangentialInfoFromPathsResult::PreviousPoint(p) => *p,
1022            GetTangentialInfoFromPathsResult::Arc { center, ccw } => {
1023                crate::std::utils::get_tangent_point_from_previous_arc(*center, *ccw, last_arc_end)
1024            }
1025            // The circle always starts at 0 degrees, so a suitable tangent
1026            // point is either directly above or below.
1027            GetTangentialInfoFromPathsResult::Circle {
1028                center, radius, ccw, ..
1029            } => [center[0] + radius, center[1] + if *ccw { -1.0 } else { 1.0 }],
1030            GetTangentialInfoFromPathsResult::Ellipse {
1031                center,
1032                major_axis,
1033                ccw,
1034                ..
1035            } => [center[0] + major_axis[0], center[1] + if *ccw { -1.0 } else { 1.0 }],
1036        }
1037    }
1038}
1039
1040impl Sketch {
1041    pub(crate) fn add_tag(
1042        &mut self,
1043        tag: NodeRef<'_, TagDeclarator>,
1044        current_path: &Path,
1045        exec_state: &ExecState,
1046        surface: Option<&ExtrudeSurface>,
1047    ) {
1048        let mut tag_identifier: TagIdentifier = tag.into();
1049        let base = current_path.get_base();
1050        let mut sketch_copy = self.clone();
1051        sketch_copy.tags.clear();
1052        tag_identifier.info.push((
1053            exec_state.stack().current_epoch(),
1054            TagEngineInfo {
1055                id: base.geo_meta.id,
1056                geometry: Geometry::Sketch(sketch_copy),
1057                path: Some(current_path.clone()),
1058                surface: surface.cloned(),
1059            },
1060        ));
1061
1062        self.tags.insert(tag.name.to_string(), tag_identifier);
1063    }
1064
1065    pub(crate) fn merge_tags<'a>(&mut self, tags: impl Iterator<Item = &'a TagIdentifier>) {
1066        for t in tags {
1067            match self.tags.get_mut(&t.value) {
1068                Some(id) => {
1069                    id.merge_info(t);
1070                }
1071                None => {
1072                    self.tags.insert(t.value.clone(), t.clone());
1073                }
1074            }
1075        }
1076    }
1077
1078    /// Get the path most recently sketched.
1079    pub(crate) fn latest_path(&self) -> Option<&Path> {
1080        self.paths.last()
1081    }
1082
1083    /// The "pen" is an imaginary pen drawing the path.
1084    /// This gets the current point the pen is hovering over, i.e. the point
1085    /// where the last path segment ends, and the next path segment will begin.
1086    pub(crate) fn current_pen_position(&self) -> Result<Point2d, KclError> {
1087        let Some(path) = self.latest_path() else {
1088            return Ok(Point2d::new(self.start.to[0], self.start.to[1], self.start.units));
1089        };
1090
1091        let to = path.get_base().to;
1092        Ok(Point2d::new(to[0], to[1], path.get_base().units))
1093    }
1094
1095    pub(crate) fn get_tangential_info_from_paths(&self) -> GetTangentialInfoFromPathsResult {
1096        let Some(path) = self.latest_path() else {
1097            return GetTangentialInfoFromPathsResult::PreviousPoint(self.start.to);
1098        };
1099        path.get_tangential_info()
1100    }
1101}
1102
1103#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1104#[ts(export)]
1105#[serde(tag = "type", rename_all = "camelCase")]
1106pub struct Solid {
1107    /// The id of the solid.
1108    pub id: uuid::Uuid,
1109    /// The artifact ID of the solid.  Unlike `id`, this doesn't change.
1110    pub artifact_id: ArtifactId,
1111    /// The extrude surfaces.
1112    pub value: Vec<ExtrudeSurface>,
1113    /// How this solid was created.
1114    #[serde(rename = "sketch")]
1115    pub creator: SolidCreator,
1116    /// The id of the extrusion start cap
1117    pub start_cap_id: Option<uuid::Uuid>,
1118    /// The id of the extrusion end cap
1119    pub end_cap_id: Option<uuid::Uuid>,
1120    /// Chamfers or fillets on this solid.
1121    #[serde(default, skip_serializing_if = "Vec::is_empty")]
1122    pub edge_cuts: Vec<EdgeCut>,
1123    /// The units of the solid.
1124    pub units: UnitLength,
1125    /// Is this a sectional solid?
1126    pub sectional: bool,
1127    /// Metadata.
1128    #[serde(skip)]
1129    pub meta: Vec<Metadata>,
1130}
1131
1132#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1133#[ts(export)]
1134pub struct CreatorFace {
1135    /// The face id that served as the base.
1136    pub face_id: uuid::Uuid,
1137    /// The solid id that owned the face.
1138    pub solid_id: uuid::Uuid,
1139    /// The sketch used for the operation.
1140    pub sketch: Sketch,
1141}
1142
1143/// How a solid was created.
1144#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1145#[ts(export)]
1146#[serde(tag = "creatorType", rename_all = "camelCase")]
1147pub enum SolidCreator {
1148    /// Created from a sketch.
1149    Sketch(Sketch),
1150    /// Created by extruding or modifying a face.
1151    Face(CreatorFace),
1152    /// Created procedurally without a sketch.
1153    Procedural,
1154}
1155
1156impl Solid {
1157    pub fn sketch(&self) -> Option<&Sketch> {
1158        match &self.creator {
1159            SolidCreator::Sketch(sketch) => Some(sketch),
1160            SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1161            SolidCreator::Procedural => None,
1162        }
1163    }
1164
1165    pub fn sketch_mut(&mut self) -> Option<&mut Sketch> {
1166        match &mut self.creator {
1167            SolidCreator::Sketch(sketch) => Some(sketch),
1168            SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1169            SolidCreator::Procedural => None,
1170        }
1171    }
1172
1173    pub fn sketch_id(&self) -> Option<uuid::Uuid> {
1174        self.sketch().map(|sketch| sketch.id)
1175    }
1176
1177    pub fn original_id(&self) -> uuid::Uuid {
1178        self.sketch().map(|sketch| sketch.original_id).unwrap_or(self.id)
1179    }
1180
1181    pub(crate) fn get_all_edge_cut_ids(&self) -> impl Iterator<Item = uuid::Uuid> + '_ {
1182        self.edge_cuts.iter().map(|foc| foc.id())
1183    }
1184}
1185
1186/// A fillet or a chamfer.
1187#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1188#[ts(export)]
1189#[serde(tag = "type", rename_all = "camelCase")]
1190pub enum EdgeCut {
1191    /// A fillet.
1192    Fillet {
1193        /// The id of the engine command that called this fillet.
1194        id: uuid::Uuid,
1195        radius: TyF64,
1196        /// The engine id of the edge to fillet.
1197        #[serde(rename = "edgeId")]
1198        edge_id: uuid::Uuid,
1199        tag: Box<Option<TagNode>>,
1200    },
1201    /// A chamfer.
1202    Chamfer {
1203        /// The id of the engine command that called this chamfer.
1204        id: uuid::Uuid,
1205        length: TyF64,
1206        /// The engine id of the edge to chamfer.
1207        #[serde(rename = "edgeId")]
1208        edge_id: uuid::Uuid,
1209        tag: Box<Option<TagNode>>,
1210    },
1211}
1212
1213impl EdgeCut {
1214    pub fn id(&self) -> uuid::Uuid {
1215        match self {
1216            EdgeCut::Fillet { id, .. } => *id,
1217            EdgeCut::Chamfer { id, .. } => *id,
1218        }
1219    }
1220
1221    pub fn set_id(&mut self, id: uuid::Uuid) {
1222        match self {
1223            EdgeCut::Fillet { id: i, .. } => *i = id,
1224            EdgeCut::Chamfer { id: i, .. } => *i = id,
1225        }
1226    }
1227
1228    pub fn edge_id(&self) -> uuid::Uuid {
1229        match self {
1230            EdgeCut::Fillet { edge_id, .. } => *edge_id,
1231            EdgeCut::Chamfer { edge_id, .. } => *edge_id,
1232        }
1233    }
1234
1235    pub fn set_edge_id(&mut self, id: uuid::Uuid) {
1236        match self {
1237            EdgeCut::Fillet { edge_id: i, .. } => *i = id,
1238            EdgeCut::Chamfer { edge_id: i, .. } => *i = id,
1239        }
1240    }
1241
1242    pub fn tag(&self) -> Option<TagNode> {
1243        match self {
1244            EdgeCut::Fillet { tag, .. } => *tag.clone(),
1245            EdgeCut::Chamfer { tag, .. } => *tag.clone(),
1246        }
1247    }
1248}
1249
1250#[derive(Debug, Serialize, PartialEq, Clone, Copy, ts_rs::TS)]
1251#[ts(export)]
1252pub struct Point2d {
1253    pub x: f64,
1254    pub y: f64,
1255    pub units: UnitLength,
1256}
1257
1258impl Point2d {
1259    pub const ZERO: Self = Self {
1260        x: 0.0,
1261        y: 0.0,
1262        units: UnitLength::Millimeters,
1263    };
1264
1265    pub fn new(x: f64, y: f64, units: UnitLength) -> Self {
1266        Self { x, y, units }
1267    }
1268
1269    pub fn into_x(self) -> TyF64 {
1270        TyF64::new(self.x, self.units.into())
1271    }
1272
1273    pub fn into_y(self) -> TyF64 {
1274        TyF64::new(self.y, self.units.into())
1275    }
1276
1277    pub fn ignore_units(self) -> [f64; 2] {
1278        [self.x, self.y]
1279    }
1280}
1281
1282#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy, ts_rs::TS, Default)]
1283#[ts(export)]
1284pub struct Point3d {
1285    pub x: f64,
1286    pub y: f64,
1287    pub z: f64,
1288    pub units: Option<UnitLength>,
1289}
1290
1291impl Point3d {
1292    pub const ZERO: Self = Self {
1293        x: 0.0,
1294        y: 0.0,
1295        z: 0.0,
1296        units: Some(UnitLength::Millimeters),
1297    };
1298
1299    pub fn new(x: f64, y: f64, z: f64, units: Option<UnitLength>) -> Self {
1300        Self { x, y, z, units }
1301    }
1302
1303    pub const fn is_zero(&self) -> bool {
1304        self.x == 0.0 && self.y == 0.0 && self.z == 0.0
1305    }
1306
1307    /// Calculate the cross product of this vector with another.
1308    ///
1309    /// This should only be applied to axes or other vectors which represent only a direction (and
1310    /// no magnitude) since units are ignored.
1311    pub fn axes_cross_product(&self, other: &Self) -> Self {
1312        Self {
1313            x: self.y * other.z - self.z * other.y,
1314            y: self.z * other.x - self.x * other.z,
1315            z: self.x * other.y - self.y * other.x,
1316            units: None,
1317        }
1318    }
1319
1320    /// Calculate the dot product of this vector with another.
1321    ///
1322    /// This should only be applied to axes or other vectors which represent only a direction (and
1323    /// no magnitude) since units are ignored.
1324    pub fn axes_dot_product(&self, other: &Self) -> f64 {
1325        let x = self.x * other.x;
1326        let y = self.y * other.y;
1327        let z = self.z * other.z;
1328        x + y + z
1329    }
1330
1331    pub fn normalize(&self) -> Self {
1332        let len = f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
1333        Point3d {
1334            x: self.x / len,
1335            y: self.y / len,
1336            z: self.z / len,
1337            units: None,
1338        }
1339    }
1340
1341    pub fn as_3_dims(&self) -> ([f64; 3], Option<UnitLength>) {
1342        let p = [self.x, self.y, self.z];
1343        let u = self.units;
1344        (p, u)
1345    }
1346
1347    pub(crate) fn negated(self) -> Self {
1348        Self {
1349            x: -self.x,
1350            y: -self.y,
1351            z: -self.z,
1352            units: self.units,
1353        }
1354    }
1355}
1356
1357impl From<[TyF64; 3]> for Point3d {
1358    fn from(p: [TyF64; 3]) -> Self {
1359        Self {
1360            x: p[0].n,
1361            y: p[1].n,
1362            z: p[2].n,
1363            units: p[0].ty.as_length(),
1364        }
1365    }
1366}
1367
1368impl From<Point3d> for Point3D {
1369    fn from(p: Point3d) -> Self {
1370        Self { x: p.x, y: p.y, z: p.z }
1371    }
1372}
1373
1374impl From<Point3d> for kittycad_modeling_cmds::shared::Point3d<LengthUnit> {
1375    fn from(p: Point3d) -> Self {
1376        if let Some(units) = p.units {
1377            Self {
1378                x: LengthUnit(adjust_length(units, p.x, UnitLength::Millimeters).0),
1379                y: LengthUnit(adjust_length(units, p.y, UnitLength::Millimeters).0),
1380                z: LengthUnit(adjust_length(units, p.z, UnitLength::Millimeters).0),
1381            }
1382        } else {
1383            Self {
1384                x: LengthUnit(p.x),
1385                y: LengthUnit(p.y),
1386                z: LengthUnit(p.z),
1387            }
1388        }
1389    }
1390}
1391
1392impl Add for Point3d {
1393    type Output = Point3d;
1394
1395    fn add(self, rhs: Self) -> Self::Output {
1396        // TODO should assert that self and rhs the same units or coerce them
1397        Point3d {
1398            x: self.x + rhs.x,
1399            y: self.y + rhs.y,
1400            z: self.z + rhs.z,
1401            units: self.units,
1402        }
1403    }
1404}
1405
1406impl AddAssign for Point3d {
1407    fn add_assign(&mut self, rhs: Self) {
1408        *self = *self + rhs
1409    }
1410}
1411
1412impl Sub for Point3d {
1413    type Output = Point3d;
1414
1415    fn sub(self, rhs: Self) -> Self::Output {
1416        let (x, y, z) = if rhs.units != self.units
1417            && let Some(sunits) = self.units
1418            && let Some(runits) = rhs.units
1419        {
1420            (
1421                adjust_length(runits, rhs.x, sunits).0,
1422                adjust_length(runits, rhs.y, sunits).0,
1423                adjust_length(runits, rhs.z, sunits).0,
1424            )
1425        } else {
1426            (rhs.x, rhs.y, rhs.z)
1427        };
1428        Point3d {
1429            x: self.x - x,
1430            y: self.y - y,
1431            z: self.z - z,
1432            units: self.units,
1433        }
1434    }
1435}
1436
1437impl SubAssign for Point3d {
1438    fn sub_assign(&mut self, rhs: Self) {
1439        *self = *self - rhs
1440    }
1441}
1442
1443impl Mul<f64> for Point3d {
1444    type Output = Point3d;
1445
1446    fn mul(self, rhs: f64) -> Self::Output {
1447        Point3d {
1448            x: self.x * rhs,
1449            y: self.y * rhs,
1450            z: self.z * rhs,
1451            units: self.units,
1452        }
1453    }
1454}
1455
1456/// A base path.
1457#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1458#[ts(export)]
1459#[serde(rename_all = "camelCase")]
1460pub struct BasePath {
1461    /// The from point.
1462    #[ts(type = "[number, number]")]
1463    pub from: [f64; 2],
1464    /// The to point.
1465    #[ts(type = "[number, number]")]
1466    pub to: [f64; 2],
1467    pub units: UnitLength,
1468    /// The tag of the path.
1469    pub tag: Option<TagNode>,
1470    /// Metadata.
1471    #[serde(rename = "__geoMeta")]
1472    pub geo_meta: GeoMeta,
1473}
1474
1475impl BasePath {
1476    pub fn get_to(&self) -> [TyF64; 2] {
1477        let ty: NumericType = self.units.into();
1478        [TyF64::new(self.to[0], ty), TyF64::new(self.to[1], ty)]
1479    }
1480
1481    pub fn get_from(&self) -> [TyF64; 2] {
1482        let ty: NumericType = self.units.into();
1483        [TyF64::new(self.from[0], ty), TyF64::new(self.from[1], ty)]
1484    }
1485}
1486
1487/// Geometry metadata.
1488#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1489#[ts(export)]
1490#[serde(rename_all = "camelCase")]
1491pub struct GeoMeta {
1492    /// The id of the geometry.
1493    pub id: uuid::Uuid,
1494    /// Metadata.
1495    #[serde(flatten)]
1496    pub metadata: Metadata,
1497}
1498
1499/// A path.
1500#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1501#[ts(export)]
1502#[serde(tag = "type")]
1503pub enum Path {
1504    /// A straight line which ends at the given point.
1505    ToPoint {
1506        #[serde(flatten)]
1507        base: BasePath,
1508    },
1509    /// A arc that is tangential to the last path segment that goes to a point
1510    TangentialArcTo {
1511        #[serde(flatten)]
1512        base: BasePath,
1513        /// the arc's center
1514        #[ts(type = "[number, number]")]
1515        center: [f64; 2],
1516        /// arc's direction
1517        ccw: bool,
1518    },
1519    /// A arc that is tangential to the last path segment
1520    TangentialArc {
1521        #[serde(flatten)]
1522        base: BasePath,
1523        /// the arc's center
1524        #[ts(type = "[number, number]")]
1525        center: [f64; 2],
1526        /// arc's direction
1527        ccw: bool,
1528    },
1529    // TODO: consolidate segment enums, remove Circle. https://github.com/KittyCAD/modeling-app/issues/3940
1530    /// a complete arc
1531    Circle {
1532        #[serde(flatten)]
1533        base: BasePath,
1534        /// the arc's center
1535        #[ts(type = "[number, number]")]
1536        center: [f64; 2],
1537        /// the arc's radius
1538        radius: f64,
1539        /// arc's direction
1540        /// This is used to compute the tangential angle.
1541        ccw: bool,
1542    },
1543    CircleThreePoint {
1544        #[serde(flatten)]
1545        base: BasePath,
1546        /// Point 1 of the circle
1547        #[ts(type = "[number, number]")]
1548        p1: [f64; 2],
1549        /// Point 2 of the circle
1550        #[ts(type = "[number, number]")]
1551        p2: [f64; 2],
1552        /// Point 3 of the circle
1553        #[ts(type = "[number, number]")]
1554        p3: [f64; 2],
1555    },
1556    ArcThreePoint {
1557        #[serde(flatten)]
1558        base: BasePath,
1559        /// Point 1 of the arc (base on the end of previous segment)
1560        #[ts(type = "[number, number]")]
1561        p1: [f64; 2],
1562        /// Point 2 of the arc (interiorAbsolute kwarg)
1563        #[ts(type = "[number, number]")]
1564        p2: [f64; 2],
1565        /// Point 3 of the arc (endAbsolute kwarg)
1566        #[ts(type = "[number, number]")]
1567        p3: [f64; 2],
1568    },
1569    /// A path that is horizontal.
1570    Horizontal {
1571        #[serde(flatten)]
1572        base: BasePath,
1573        /// The x coordinate.
1574        x: f64,
1575    },
1576    /// An angled line to.
1577    AngledLineTo {
1578        #[serde(flatten)]
1579        base: BasePath,
1580        /// The x coordinate.
1581        x: Option<f64>,
1582        /// The y coordinate.
1583        y: Option<f64>,
1584    },
1585    /// A base path.
1586    Base {
1587        #[serde(flatten)]
1588        base: BasePath,
1589    },
1590    /// A circular arc, not necessarily tangential to the current point.
1591    Arc {
1592        #[serde(flatten)]
1593        base: BasePath,
1594        /// Center of the circle that this arc is drawn on.
1595        center: [f64; 2],
1596        /// Radius of the circle that this arc is drawn on.
1597        radius: f64,
1598        /// True if the arc is counterclockwise.
1599        ccw: bool,
1600    },
1601    Ellipse {
1602        #[serde(flatten)]
1603        base: BasePath,
1604        center: [f64; 2],
1605        major_axis: [f64; 2],
1606        minor_radius: f64,
1607        ccw: bool,
1608    },
1609    //TODO: (bc) figure this out
1610    Conic {
1611        #[serde(flatten)]
1612        base: BasePath,
1613    },
1614    /// A cubic Bezier curve.
1615    Bezier {
1616        #[serde(flatten)]
1617        base: BasePath,
1618        /// First control point (absolute coordinates).
1619        #[ts(type = "[number, number]")]
1620        control1: [f64; 2],
1621        /// Second control point (absolute coordinates).
1622        #[ts(type = "[number, number]")]
1623        control2: [f64; 2],
1624    },
1625}
1626
1627impl Path {
1628    pub fn get_id(&self) -> uuid::Uuid {
1629        match self {
1630            Path::ToPoint { base } => base.geo_meta.id,
1631            Path::Horizontal { base, .. } => base.geo_meta.id,
1632            Path::AngledLineTo { base, .. } => base.geo_meta.id,
1633            Path::Base { base } => base.geo_meta.id,
1634            Path::TangentialArcTo { base, .. } => base.geo_meta.id,
1635            Path::TangentialArc { base, .. } => base.geo_meta.id,
1636            Path::Circle { base, .. } => base.geo_meta.id,
1637            Path::CircleThreePoint { base, .. } => base.geo_meta.id,
1638            Path::Arc { base, .. } => base.geo_meta.id,
1639            Path::ArcThreePoint { base, .. } => base.geo_meta.id,
1640            Path::Ellipse { base, .. } => base.geo_meta.id,
1641            Path::Conic { base, .. } => base.geo_meta.id,
1642            Path::Bezier { base, .. } => base.geo_meta.id,
1643        }
1644    }
1645
1646    pub fn set_id(&mut self, id: uuid::Uuid) {
1647        match self {
1648            Path::ToPoint { base } => base.geo_meta.id = id,
1649            Path::Horizontal { base, .. } => base.geo_meta.id = id,
1650            Path::AngledLineTo { base, .. } => base.geo_meta.id = id,
1651            Path::Base { base } => base.geo_meta.id = id,
1652            Path::TangentialArcTo { base, .. } => base.geo_meta.id = id,
1653            Path::TangentialArc { base, .. } => base.geo_meta.id = id,
1654            Path::Circle { base, .. } => base.geo_meta.id = id,
1655            Path::CircleThreePoint { base, .. } => base.geo_meta.id = id,
1656            Path::Arc { base, .. } => base.geo_meta.id = id,
1657            Path::ArcThreePoint { base, .. } => base.geo_meta.id = id,
1658            Path::Ellipse { base, .. } => base.geo_meta.id = id,
1659            Path::Conic { base, .. } => base.geo_meta.id = id,
1660            Path::Bezier { base, .. } => base.geo_meta.id = id,
1661        }
1662    }
1663
1664    pub fn get_tag(&self) -> Option<TagNode> {
1665        match self {
1666            Path::ToPoint { base } => base.tag.clone(),
1667            Path::Horizontal { base, .. } => base.tag.clone(),
1668            Path::AngledLineTo { base, .. } => base.tag.clone(),
1669            Path::Base { base } => base.tag.clone(),
1670            Path::TangentialArcTo { base, .. } => base.tag.clone(),
1671            Path::TangentialArc { base, .. } => base.tag.clone(),
1672            Path::Circle { base, .. } => base.tag.clone(),
1673            Path::CircleThreePoint { base, .. } => base.tag.clone(),
1674            Path::Arc { base, .. } => base.tag.clone(),
1675            Path::ArcThreePoint { base, .. } => base.tag.clone(),
1676            Path::Ellipse { base, .. } => base.tag.clone(),
1677            Path::Conic { base, .. } => base.tag.clone(),
1678            Path::Bezier { base, .. } => base.tag.clone(),
1679        }
1680    }
1681
1682    pub fn get_base(&self) -> &BasePath {
1683        match self {
1684            Path::ToPoint { base } => base,
1685            Path::Horizontal { base, .. } => base,
1686            Path::AngledLineTo { base, .. } => base,
1687            Path::Base { base } => base,
1688            Path::TangentialArcTo { base, .. } => base,
1689            Path::TangentialArc { base, .. } => base,
1690            Path::Circle { base, .. } => base,
1691            Path::CircleThreePoint { base, .. } => base,
1692            Path::Arc { base, .. } => base,
1693            Path::ArcThreePoint { base, .. } => base,
1694            Path::Ellipse { base, .. } => base,
1695            Path::Conic { base, .. } => base,
1696            Path::Bezier { base, .. } => base,
1697        }
1698    }
1699
1700    /// Where does this path segment start?
1701    pub fn get_from(&self) -> [TyF64; 2] {
1702        let p = &self.get_base().from;
1703        let ty: NumericType = self.get_base().units.into();
1704        [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1705    }
1706
1707    /// Where does this path segment end?
1708    pub fn get_to(&self) -> [TyF64; 2] {
1709        let p = &self.get_base().to;
1710        let ty: NumericType = self.get_base().units.into();
1711        [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1712    }
1713
1714    /// The path segment start point and its type.
1715    pub fn start_point_components(&self) -> ([f64; 2], NumericType) {
1716        let p = &self.get_base().from;
1717        let ty: NumericType = self.get_base().units.into();
1718        (*p, ty)
1719    }
1720
1721    /// The path segment end point and its type.
1722    pub fn end_point_components(&self) -> ([f64; 2], NumericType) {
1723        let p = &self.get_base().to;
1724        let ty: NumericType = self.get_base().units.into();
1725        (*p, ty)
1726    }
1727
1728    /// Length of this path segment, in cartesian plane. Not all segment types
1729    /// are supported.
1730    pub fn length(&self) -> Option<TyF64> {
1731        let n = match self {
1732            Self::ToPoint { .. } | Self::Base { .. } | Self::Horizontal { .. } | Self::AngledLineTo { .. } => {
1733                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1734            }
1735            Self::TangentialArc {
1736                base: _,
1737                center,
1738                ccw: _,
1739            }
1740            | Self::TangentialArcTo {
1741                base: _,
1742                center,
1743                ccw: _,
1744            } => {
1745                // The radius can be calculated as the linear distance between `to` and `center`,
1746                // or between `from` and `center`. They should be the same.
1747                let radius = linear_distance(&self.get_base().from, center);
1748                debug_assert_eq!(radius, linear_distance(&self.get_base().to, center));
1749                // TODO: Call engine utils to figure this out.
1750                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1751            }
1752            Self::Circle { radius, .. } => Some(TAU * radius),
1753            Self::CircleThreePoint { .. } => {
1754                let circle_center = crate::std::utils::calculate_circle_from_3_points([
1755                    self.get_base().from,
1756                    self.get_base().to,
1757                    self.get_base().to,
1758                ]);
1759                let radius = linear_distance(
1760                    &[circle_center.center[0], circle_center.center[1]],
1761                    &self.get_base().from,
1762                );
1763                Some(TAU * radius)
1764            }
1765            Self::Arc { .. } => {
1766                // TODO: Call engine utils to figure this out.
1767                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1768            }
1769            Self::ArcThreePoint { .. } => {
1770                // TODO: Call engine utils to figure this out.
1771                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1772            }
1773            Self::Ellipse { .. } => {
1774                // Not supported.
1775                None
1776            }
1777            Self::Conic { .. } => {
1778                // Not supported.
1779                None
1780            }
1781            Self::Bezier { .. } => {
1782                // Not supported - Bezier curve length requires numerical integration.
1783                None
1784            }
1785        };
1786        n.map(|n| TyF64::new(n, self.get_base().units.into()))
1787    }
1788
1789    pub fn get_base_mut(&mut self) -> &mut BasePath {
1790        match self {
1791            Path::ToPoint { base } => base,
1792            Path::Horizontal { base, .. } => base,
1793            Path::AngledLineTo { base, .. } => base,
1794            Path::Base { base } => base,
1795            Path::TangentialArcTo { base, .. } => base,
1796            Path::TangentialArc { base, .. } => base,
1797            Path::Circle { base, .. } => base,
1798            Path::CircleThreePoint { base, .. } => base,
1799            Path::Arc { base, .. } => base,
1800            Path::ArcThreePoint { base, .. } => base,
1801            Path::Ellipse { base, .. } => base,
1802            Path::Conic { base, .. } => base,
1803            Path::Bezier { base, .. } => base,
1804        }
1805    }
1806
1807    pub(crate) fn get_tangential_info(&self) -> GetTangentialInfoFromPathsResult {
1808        match self {
1809            Path::TangentialArc { center, ccw, .. }
1810            | Path::TangentialArcTo { center, ccw, .. }
1811            | Path::Arc { center, ccw, .. } => GetTangentialInfoFromPathsResult::Arc {
1812                center: *center,
1813                ccw: *ccw,
1814            },
1815            Path::ArcThreePoint { p1, p2, p3, .. } => {
1816                let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1817                GetTangentialInfoFromPathsResult::Arc {
1818                    center: circle.center,
1819                    ccw: crate::std::utils::is_points_ccw(&[*p1, *p2, *p3]) > 0,
1820                }
1821            }
1822            Path::Circle {
1823                center, ccw, radius, ..
1824            } => GetTangentialInfoFromPathsResult::Circle {
1825                center: *center,
1826                ccw: *ccw,
1827                radius: *radius,
1828            },
1829            Path::CircleThreePoint { p1, p2, p3, .. } => {
1830                let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1831                let center_point = [circle.center[0], circle.center[1]];
1832                GetTangentialInfoFromPathsResult::Circle {
1833                    center: center_point,
1834                    // Note: a circle is always ccw regardless of the order of points
1835                    ccw: true,
1836                    radius: circle.radius,
1837                }
1838            }
1839            // TODO: (bc) fix me
1840            Path::Ellipse {
1841                center,
1842                major_axis,
1843                minor_radius,
1844                ccw,
1845                ..
1846            } => GetTangentialInfoFromPathsResult::Ellipse {
1847                center: *center,
1848                major_axis: *major_axis,
1849                _minor_radius: *minor_radius,
1850                ccw: *ccw,
1851            },
1852            Path::Conic { .. }
1853            | Path::ToPoint { .. }
1854            | Path::Horizontal { .. }
1855            | Path::AngledLineTo { .. }
1856            | Path::Base { .. }
1857            | Path::Bezier { .. } => {
1858                let base = self.get_base();
1859                GetTangentialInfoFromPathsResult::PreviousPoint(base.from)
1860            }
1861        }
1862    }
1863
1864    /// i.e. not a curve
1865    pub(crate) fn is_straight_line(&self) -> bool {
1866        matches!(self, Path::AngledLineTo { .. } | Path::ToPoint { .. })
1867    }
1868}
1869
1870/// Compute the straight-line distance between a pair of (2D) points.
1871#[rustfmt::skip]
1872fn linear_distance(
1873    [x0, y0]: &[f64; 2],
1874    [x1, y1]: &[f64; 2]
1875) -> f64 {
1876    let y_sq = (y1 - y0).powi(2);
1877    let x_sq = (x1 - x0).powi(2);
1878    (y_sq + x_sq).sqrt()
1879}
1880
1881/// An extrude surface.
1882#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1883#[ts(export)]
1884#[serde(tag = "type", rename_all = "camelCase")]
1885pub enum ExtrudeSurface {
1886    /// An extrude plane.
1887    ExtrudePlane(ExtrudePlane),
1888    ExtrudeArc(ExtrudeArc),
1889    Chamfer(ChamferSurface),
1890    Fillet(FilletSurface),
1891}
1892
1893// Chamfer surface.
1894#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1895#[ts(export)]
1896#[serde(rename_all = "camelCase")]
1897pub struct ChamferSurface {
1898    /// The id for the chamfer surface.
1899    pub face_id: uuid::Uuid,
1900    /// The tag.
1901    pub tag: Option<Node<TagDeclarator>>,
1902    /// Metadata.
1903    #[serde(flatten)]
1904    pub geo_meta: GeoMeta,
1905}
1906
1907// Fillet surface.
1908#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1909#[ts(export)]
1910#[serde(rename_all = "camelCase")]
1911pub struct FilletSurface {
1912    /// The id for the fillet surface.
1913    pub face_id: uuid::Uuid,
1914    /// The tag.
1915    pub tag: Option<Node<TagDeclarator>>,
1916    /// Metadata.
1917    #[serde(flatten)]
1918    pub geo_meta: GeoMeta,
1919}
1920
1921/// An extruded plane.
1922#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1923#[ts(export)]
1924#[serde(rename_all = "camelCase")]
1925pub struct ExtrudePlane {
1926    /// The face id for the extrude plane.
1927    pub face_id: uuid::Uuid,
1928    /// The tag.
1929    pub tag: Option<Node<TagDeclarator>>,
1930    /// Metadata.
1931    #[serde(flatten)]
1932    pub geo_meta: GeoMeta,
1933}
1934
1935/// An extruded arc.
1936#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1937#[ts(export)]
1938#[serde(rename_all = "camelCase")]
1939pub struct ExtrudeArc {
1940    /// The face id for the extrude plane.
1941    pub face_id: uuid::Uuid,
1942    /// The tag.
1943    pub tag: Option<Node<TagDeclarator>>,
1944    /// Metadata.
1945    #[serde(flatten)]
1946    pub geo_meta: GeoMeta,
1947}
1948
1949impl ExtrudeSurface {
1950    pub fn get_id(&self) -> uuid::Uuid {
1951        match self {
1952            ExtrudeSurface::ExtrudePlane(ep) => ep.geo_meta.id,
1953            ExtrudeSurface::ExtrudeArc(ea) => ea.geo_meta.id,
1954            ExtrudeSurface::Fillet(f) => f.geo_meta.id,
1955            ExtrudeSurface::Chamfer(c) => c.geo_meta.id,
1956        }
1957    }
1958
1959    pub fn face_id(&self) -> uuid::Uuid {
1960        match self {
1961            ExtrudeSurface::ExtrudePlane(ep) => ep.face_id,
1962            ExtrudeSurface::ExtrudeArc(ea) => ea.face_id,
1963            ExtrudeSurface::Fillet(f) => f.face_id,
1964            ExtrudeSurface::Chamfer(c) => c.face_id,
1965        }
1966    }
1967
1968    pub fn set_face_id(&mut self, face_id: uuid::Uuid) {
1969        match self {
1970            ExtrudeSurface::ExtrudePlane(ep) => ep.face_id = face_id,
1971            ExtrudeSurface::ExtrudeArc(ea) => ea.face_id = face_id,
1972            ExtrudeSurface::Fillet(f) => f.face_id = face_id,
1973            ExtrudeSurface::Chamfer(c) => c.face_id = face_id,
1974        }
1975    }
1976
1977    pub fn set_surface_tag(&mut self, tag: &TagNode) {
1978        match self {
1979            ExtrudeSurface::ExtrudePlane(extrude_plane) => extrude_plane.tag = Some(tag.clone()),
1980            ExtrudeSurface::ExtrudeArc(extrude_arc) => extrude_arc.tag = Some(tag.clone()),
1981            ExtrudeSurface::Chamfer(chamfer) => chamfer.tag = Some(tag.clone()),
1982            ExtrudeSurface::Fillet(fillet) => fillet.tag = Some(tag.clone()),
1983        }
1984    }
1985
1986    pub fn get_tag(&self) -> Option<Node<TagDeclarator>> {
1987        match self {
1988            ExtrudeSurface::ExtrudePlane(ep) => ep.tag.clone(),
1989            ExtrudeSurface::ExtrudeArc(ea) => ea.tag.clone(),
1990            ExtrudeSurface::Fillet(f) => f.tag.clone(),
1991            ExtrudeSurface::Chamfer(c) => c.tag.clone(),
1992        }
1993    }
1994}
1995
1996#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, ts_rs::TS)]
1997pub struct SketchVarId(pub usize);
1998
1999impl SketchVarId {
2000    pub fn to_constraint_id(self, range: SourceRange) -> Result<ezpz::Id, KclError> {
2001        self.0.try_into().map_err(|_| {
2002            KclError::new_type(KclErrorDetails::new(
2003                "Cannot convert to constraint ID since the sketch variable ID is too large".to_owned(),
2004                vec![range],
2005            ))
2006        })
2007    }
2008}
2009
2010#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2011#[ts(export_to = "Geometry.ts")]
2012#[serde(rename_all = "camelCase")]
2013pub struct SketchVar {
2014    pub id: SketchVarId,
2015    pub initial_value: f64,
2016    pub ty: NumericType,
2017    #[serde(skip)]
2018    pub meta: Vec<Metadata>,
2019}
2020
2021impl SketchVar {
2022    pub fn initial_value_to_solver_units(
2023        &self,
2024        exec_state: &mut ExecState,
2025        source_range: SourceRange,
2026        description: &str,
2027    ) -> Result<TyF64, KclError> {
2028        let x_initial_value = KclValue::Number {
2029            value: self.initial_value,
2030            ty: self.ty,
2031            meta: vec![source_range.into()],
2032        };
2033        let normalized_value =
2034            normalize_to_solver_distance_unit(&x_initial_value, source_range, exec_state, description)?;
2035        normalized_value.as_ty_f64().ok_or_else(|| {
2036            let message = format!(
2037                "Expected number after coercion, but found {}",
2038                normalized_value.human_friendly_type()
2039            );
2040            debug_assert!(false, "{}", &message);
2041            KclError::new_internal(KclErrorDetails::new(message, vec![source_range]))
2042        })
2043    }
2044}
2045
2046#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2047#[ts(export_to = "Geometry.ts")]
2048#[serde(tag = "type")]
2049pub enum UnsolvedExpr {
2050    Known(TyF64),
2051    Unknown(SketchVarId),
2052}
2053
2054impl UnsolvedExpr {
2055    pub fn var(&self) -> Option<SketchVarId> {
2056        match self {
2057            UnsolvedExpr::Known(_) => None,
2058            UnsolvedExpr::Unknown(id) => Some(*id),
2059        }
2060    }
2061}
2062
2063pub type UnsolvedPoint2dExpr = [UnsolvedExpr; 2];
2064
2065#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2066#[ts(export_to = "Geometry.ts")]
2067#[serde(rename_all = "camelCase")]
2068pub struct ConstrainablePoint2d {
2069    pub vars: crate::front::Point2d<SketchVarId>,
2070    pub object_id: ObjectId,
2071}
2072
2073#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2074#[ts(export_to = "Geometry.ts")]
2075pub enum ConstrainablePoint2dOrOrigin {
2076    Point(ConstrainablePoint2d),
2077    Origin,
2078}
2079
2080#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2081#[ts(export_to = "Geometry.ts")]
2082#[serde(rename_all = "camelCase")]
2083pub struct ConstrainableLine2d {
2084    pub vars: [crate::front::Point2d<SketchVarId>; 2],
2085    pub object_id: ObjectId,
2086}
2087
2088#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2089#[ts(export_to = "Geometry.ts")]
2090#[serde(rename_all = "camelCase")]
2091pub struct UnsolvedSegment {
2092    /// The engine ID.
2093    pub id: Uuid,
2094    pub object_id: ObjectId,
2095    pub kind: UnsolvedSegmentKind,
2096    #[serde(skip_serializing_if = "Option::is_none")]
2097    pub tag: Option<TagIdentifier>,
2098    #[serde(skip)]
2099    pub node_path: Option<NodePath>,
2100    #[serde(skip)]
2101    pub meta: Vec<Metadata>,
2102}
2103
2104#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2105#[ts(export_to = "Geometry.ts")]
2106#[serde(rename_all = "camelCase")]
2107pub enum UnsolvedSegmentKind {
2108    Point {
2109        position: UnsolvedPoint2dExpr,
2110        ctor: Box<PointCtor>,
2111    },
2112    Line {
2113        start: UnsolvedPoint2dExpr,
2114        end: UnsolvedPoint2dExpr,
2115        ctor: Box<LineCtor>,
2116        start_object_id: ObjectId,
2117        end_object_id: ObjectId,
2118        construction: bool,
2119    },
2120    Arc {
2121        start: UnsolvedPoint2dExpr,
2122        end: UnsolvedPoint2dExpr,
2123        center: UnsolvedPoint2dExpr,
2124        ctor: Box<ArcCtor>,
2125        start_object_id: ObjectId,
2126        end_object_id: ObjectId,
2127        center_object_id: ObjectId,
2128        construction: bool,
2129    },
2130    Circle {
2131        start: UnsolvedPoint2dExpr,
2132        center: UnsolvedPoint2dExpr,
2133        ctor: Box<CircleCtor>,
2134        start_object_id: ObjectId,
2135        center_object_id: ObjectId,
2136        construction: bool,
2137    },
2138}
2139
2140#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2141#[ts(export_to = "Geometry.ts")]
2142#[serde(rename_all = "camelCase")]
2143pub struct Segment {
2144    /// The engine ID.
2145    pub id: Uuid,
2146    pub object_id: ObjectId,
2147    pub kind: SegmentKind,
2148    pub surface: SketchSurface,
2149    /// The engine ID of the sketch that this is a part of.
2150    pub sketch_id: Uuid,
2151    #[serde(skip)]
2152    pub sketch: Option<Sketch>,
2153    #[serde(skip_serializing_if = "Option::is_none")]
2154    pub tag: Option<TagIdentifier>,
2155    #[serde(skip)]
2156    pub node_path: Option<NodePath>,
2157    #[serde(skip)]
2158    pub meta: Vec<Metadata>,
2159}
2160
2161impl Segment {
2162    pub fn is_construction(&self) -> bool {
2163        match &self.kind {
2164            SegmentKind::Point { .. } => true,
2165            SegmentKind::Line { construction, .. } => *construction,
2166            SegmentKind::Arc { construction, .. } => *construction,
2167            SegmentKind::Circle { construction, .. } => *construction,
2168        }
2169    }
2170}
2171
2172#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2173#[ts(export_to = "Geometry.ts")]
2174#[serde(rename_all = "camelCase")]
2175pub enum SegmentKind {
2176    Point {
2177        position: [TyF64; 2],
2178        ctor: Box<PointCtor>,
2179        #[serde(skip_serializing_if = "Option::is_none")]
2180        freedom: Option<Freedom>,
2181    },
2182    Line {
2183        start: [TyF64; 2],
2184        end: [TyF64; 2],
2185        ctor: Box<LineCtor>,
2186        start_object_id: ObjectId,
2187        end_object_id: ObjectId,
2188        #[serde(skip_serializing_if = "Option::is_none")]
2189        start_freedom: Option<Freedom>,
2190        #[serde(skip_serializing_if = "Option::is_none")]
2191        end_freedom: Option<Freedom>,
2192        construction: bool,
2193    },
2194    Arc {
2195        start: [TyF64; 2],
2196        end: [TyF64; 2],
2197        center: [TyF64; 2],
2198        ctor: Box<ArcCtor>,
2199        start_object_id: ObjectId,
2200        end_object_id: ObjectId,
2201        center_object_id: ObjectId,
2202        #[serde(skip_serializing_if = "Option::is_none")]
2203        start_freedom: Option<Freedom>,
2204        #[serde(skip_serializing_if = "Option::is_none")]
2205        end_freedom: Option<Freedom>,
2206        #[serde(skip_serializing_if = "Option::is_none")]
2207        center_freedom: Option<Freedom>,
2208        construction: bool,
2209    },
2210    Circle {
2211        start: [TyF64; 2],
2212        center: [TyF64; 2],
2213        ctor: Box<CircleCtor>,
2214        start_object_id: ObjectId,
2215        center_object_id: ObjectId,
2216        #[serde(skip_serializing_if = "Option::is_none")]
2217        start_freedom: Option<Freedom>,
2218        #[serde(skip_serializing_if = "Option::is_none")]
2219        center_freedom: Option<Freedom>,
2220        construction: bool,
2221    },
2222}
2223
2224#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2225#[ts(export_to = "Geometry.ts")]
2226#[serde(rename_all = "camelCase")]
2227pub struct AbstractSegment {
2228    pub repr: SegmentRepr,
2229    #[serde(skip)]
2230    pub meta: Vec<Metadata>,
2231}
2232
2233#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2234pub enum SegmentRepr {
2235    Unsolved { segment: Box<UnsolvedSegment> },
2236    Solved { segment: Box<Segment> },
2237}
2238
2239#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2240#[ts(export_to = "Geometry.ts")]
2241#[serde(rename_all = "camelCase")]
2242pub struct SketchConstraint {
2243    pub kind: SketchConstraintKind,
2244    #[serde(skip)]
2245    pub meta: Vec<Metadata>,
2246}
2247
2248#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2249#[ts(export_to = "Geometry.ts")]
2250#[serde(rename_all = "camelCase")]
2251pub enum SketchConstraintKind {
2252    Angle {
2253        line0: ConstrainableLine2d,
2254        line1: ConstrainableLine2d,
2255    },
2256    Distance {
2257        points: [ConstrainablePoint2dOrOrigin; 2],
2258    },
2259    Radius {
2260        points: [ConstrainablePoint2d; 2],
2261    },
2262    Diameter {
2263        points: [ConstrainablePoint2d; 2],
2264    },
2265    HorizontalDistance {
2266        points: [ConstrainablePoint2dOrOrigin; 2],
2267    },
2268    VerticalDistance {
2269        points: [ConstrainablePoint2dOrOrigin; 2],
2270    },
2271}
2272
2273impl SketchConstraintKind {
2274    pub fn name(&self) -> &'static str {
2275        match self {
2276            SketchConstraintKind::Angle { .. } => "angle",
2277            SketchConstraintKind::Distance { .. } => "distance",
2278            SketchConstraintKind::Radius { .. } => "radius",
2279            SketchConstraintKind::Diameter { .. } => "diameter",
2280            SketchConstraintKind::HorizontalDistance { .. } => "horizontalDistance",
2281            SketchConstraintKind::VerticalDistance { .. } => "verticalDistance",
2282        }
2283    }
2284}