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 this sketch represents a region created from `region()`, the origin
814    /// sketch ID is the ID of the sketch block it was created from. None,
815    /// otherwise. This field corresponds to the `origin_path_id` of the `Path`
816    /// artifact.
817    #[serde(skip_serializing_if = "Option::is_none")]
818    #[ts(skip)]
819    pub origin_sketch_id: Option<uuid::Uuid>,
820    /// If the sketch includes a mirror.
821    #[serde(skip)]
822    pub mirror: Option<uuid::Uuid>,
823    /// If the sketch is a clone of another sketch.
824    #[serde(skip)]
825    pub clone: Option<uuid::Uuid>,
826    /// Synthetic pen-jump paths inserted to replay disconnected segment selections.
827    #[serde(skip)]
828    #[ts(skip)]
829    pub synthetic_jump_path_ids: Vec<uuid::Uuid>,
830    pub units: UnitLength,
831    /// Metadata.
832    #[serde(skip)]
833    pub meta: Vec<Metadata>,
834    /// Has the profile been closed?
835    /// If not given, defaults to yes, closed explicitly.
836    #[serde(
837        default = "ProfileClosed::explicitly",
838        skip_serializing_if = "ProfileClosed::is_explicitly"
839    )]
840    pub is_closed: ProfileClosed,
841}
842
843impl ProfileClosed {
844    #[expect(dead_code, reason = "it's not actually dead, it's called by serde")]
845    fn explicitly() -> Self {
846        Self::Explicitly
847    }
848
849    fn is_explicitly(&self) -> bool {
850        matches!(self, ProfileClosed::Explicitly)
851    }
852}
853
854/// Has the profile been closed?
855#[derive(Debug, Serialize, Eq, PartialEq, Clone, Copy, Hash, Ord, PartialOrd, ts_rs::TS)]
856#[serde(rename_all = "camelCase")]
857pub enum ProfileClosed {
858    /// It's definitely open.
859    No,
860    /// Unknown.
861    Maybe,
862    /// Yes, by adding a segment which loops back to the start.
863    Implicitly,
864    /// Yes, by calling `close()` or by making a closed shape (e.g. circle).
865    Explicitly,
866}
867
868impl Sketch {
869    // Tell the engine to enter sketch mode on the sketch.
870    // Run a specific command, then exit sketch mode.
871    pub(crate) fn build_sketch_mode_cmds(
872        &self,
873        exec_state: &mut ExecState,
874        inner_cmd: ModelingCmdReq,
875    ) -> Vec<ModelingCmdReq> {
876        vec![
877            // Before we extrude, we need to enable the sketch mode.
878            // We do this here in case extrude is called out of order.
879            ModelingCmdReq {
880                cmd: ModelingCmd::from(
881                    mcmd::EnableSketchMode::builder()
882                        .animated(false)
883                        .ortho(false)
884                        .entity_id(self.on.id())
885                        .adjust_camera(false)
886                        .maybe_planar_normal(if let SketchSurface::Plane(plane) = &self.on {
887                            // We pass in the normal for the plane here.
888                            let normal = plane.info.x_axis.axes_cross_product(&plane.info.y_axis);
889                            Some(normal.into())
890                        } else {
891                            None
892                        })
893                        .build(),
894                ),
895                cmd_id: exec_state.next_uuid().into(),
896            },
897            inner_cmd,
898            ModelingCmdReq {
899                cmd: ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::builder().build()),
900                cmd_id: exec_state.next_uuid().into(),
901            },
902        ]
903    }
904}
905
906/// A sketch type.
907#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
908#[ts(export)]
909#[serde(tag = "type", rename_all = "camelCase")]
910pub enum SketchSurface {
911    Plane(Box<Plane>),
912    Face(Box<Face>),
913}
914
915impl SketchSurface {
916    pub(crate) fn id(&self) -> uuid::Uuid {
917        match self {
918            SketchSurface::Plane(plane) => plane.id,
919            SketchSurface::Face(face) => face.id,
920        }
921    }
922    pub(crate) fn x_axis(&self) -> Point3d {
923        match self {
924            SketchSurface::Plane(plane) => plane.info.x_axis,
925            SketchSurface::Face(face) => face.x_axis,
926        }
927    }
928    pub(crate) fn y_axis(&self) -> Point3d {
929        match self {
930            SketchSurface::Plane(plane) => plane.info.y_axis,
931            SketchSurface::Face(face) => face.y_axis,
932        }
933    }
934
935    pub(crate) fn object_id(&self) -> Option<ObjectId> {
936        match self {
937            SketchSurface::Plane(plane) => plane.object_id,
938            SketchSurface::Face(face) => Some(face.object_id),
939        }
940    }
941
942    pub(crate) fn set_object_id(&mut self, object_id: ObjectId) {
943        match self {
944            SketchSurface::Plane(plane) => plane.object_id = Some(object_id),
945            SketchSurface::Face(face) => face.object_id = object_id,
946        }
947    }
948}
949
950/// A Sketch, Face, or TaggedFace.
951#[derive(Debug, Clone, PartialEq)]
952pub enum Extrudable {
953    /// Sketch.
954    Sketch(Box<Sketch>),
955    /// Face.
956    Face(FaceTag),
957}
958
959impl Extrudable {
960    /// Get the relevant id.
961    pub async fn id_to_extrude(
962        &self,
963        exec_state: &mut ExecState,
964        args: &Args,
965        must_be_planar: bool,
966    ) -> Result<uuid::Uuid, KclError> {
967        match self {
968            Extrudable::Sketch(sketch) => Ok(sketch.id),
969            Extrudable::Face(face_tag) => face_tag.get_face_id_from_tag(exec_state, args, must_be_planar).await,
970        }
971    }
972
973    pub fn as_sketch(&self) -> Option<Sketch> {
974        match self {
975            Extrudable::Sketch(sketch) => Some((**sketch).clone()),
976            Extrudable::Face(face_tag) => match face_tag.geometry() {
977                Some(Geometry::Sketch(sketch)) => Some(sketch),
978                Some(Geometry::Solid(solid)) => solid.sketch().cloned(),
979                None => None,
980            },
981        }
982    }
983
984    pub fn is_closed(&self) -> ProfileClosed {
985        match self {
986            Extrudable::Sketch(sketch) => sketch.is_closed,
987            Extrudable::Face(face_tag) => match face_tag.geometry() {
988                Some(Geometry::Sketch(sketch)) => sketch.is_closed,
989                Some(Geometry::Solid(solid)) => solid
990                    .sketch()
991                    .map(|sketch| sketch.is_closed)
992                    .unwrap_or(ProfileClosed::Maybe),
993                _ => ProfileClosed::Maybe,
994            },
995        }
996    }
997}
998
999impl From<Sketch> for Extrudable {
1000    fn from(value: Sketch) -> Self {
1001        Extrudable::Sketch(Box::new(value))
1002    }
1003}
1004
1005#[derive(Debug, Clone)]
1006pub(crate) enum GetTangentialInfoFromPathsResult {
1007    PreviousPoint([f64; 2]),
1008    Arc {
1009        center: [f64; 2],
1010        ccw: bool,
1011    },
1012    Circle {
1013        center: [f64; 2],
1014        ccw: bool,
1015        radius: f64,
1016    },
1017    Ellipse {
1018        center: [f64; 2],
1019        ccw: bool,
1020        major_axis: [f64; 2],
1021        _minor_radius: f64,
1022    },
1023}
1024
1025impl GetTangentialInfoFromPathsResult {
1026    pub(crate) fn tan_previous_point(&self, last_arc_end: [f64; 2]) -> [f64; 2] {
1027        match self {
1028            GetTangentialInfoFromPathsResult::PreviousPoint(p) => *p,
1029            GetTangentialInfoFromPathsResult::Arc { center, ccw } => {
1030                crate::std::utils::get_tangent_point_from_previous_arc(*center, *ccw, last_arc_end)
1031            }
1032            // The circle always starts at 0 degrees, so a suitable tangent
1033            // point is either directly above or below.
1034            GetTangentialInfoFromPathsResult::Circle {
1035                center, radius, ccw, ..
1036            } => [center[0] + radius, center[1] + if *ccw { -1.0 } else { 1.0 }],
1037            GetTangentialInfoFromPathsResult::Ellipse {
1038                center,
1039                major_axis,
1040                ccw,
1041                ..
1042            } => [center[0] + major_axis[0], center[1] + if *ccw { -1.0 } else { 1.0 }],
1043        }
1044    }
1045}
1046
1047impl Sketch {
1048    pub(crate) fn add_tag(
1049        &mut self,
1050        tag: NodeRef<'_, TagDeclarator>,
1051        current_path: &Path,
1052        exec_state: &ExecState,
1053        surface: Option<&ExtrudeSurface>,
1054    ) {
1055        let mut tag_identifier: TagIdentifier = tag.into();
1056        let base = current_path.get_base();
1057        let mut sketch_copy = self.clone();
1058        sketch_copy.tags.clear();
1059        tag_identifier.info.push((
1060            exec_state.stack().current_epoch(),
1061            TagEngineInfo {
1062                id: base.geo_meta.id,
1063                geometry: Geometry::Sketch(sketch_copy),
1064                path: Some(current_path.clone()),
1065                surface: surface.cloned(),
1066            },
1067        ));
1068
1069        self.tags.insert(tag.name.to_string(), tag_identifier);
1070    }
1071
1072    pub(crate) fn merge_tags<'a>(&mut self, tags: impl Iterator<Item = &'a TagIdentifier>) {
1073        for t in tags {
1074            match self.tags.get_mut(&t.value) {
1075                Some(id) => {
1076                    id.merge_info(t);
1077                }
1078                None => {
1079                    self.tags.insert(t.value.clone(), t.clone());
1080                }
1081            }
1082        }
1083    }
1084
1085    /// Get the path most recently sketched.
1086    pub(crate) fn latest_path(&self) -> Option<&Path> {
1087        self.paths.last()
1088    }
1089
1090    /// The "pen" is an imaginary pen drawing the path.
1091    /// This gets the current point the pen is hovering over, i.e. the point
1092    /// where the last path segment ends, and the next path segment will begin.
1093    pub(crate) fn current_pen_position(&self) -> Result<Point2d, KclError> {
1094        let Some(path) = self.latest_path() else {
1095            return Ok(Point2d::new(self.start.to[0], self.start.to[1], self.start.units));
1096        };
1097
1098        let to = path.get_base().to;
1099        Ok(Point2d::new(to[0], to[1], path.get_base().units))
1100    }
1101
1102    pub(crate) fn get_tangential_info_from_paths(&self) -> GetTangentialInfoFromPathsResult {
1103        let Some(path) = self.latest_path() else {
1104            return GetTangentialInfoFromPathsResult::PreviousPoint(self.start.to);
1105        };
1106        path.get_tangential_info()
1107    }
1108}
1109
1110#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1111#[ts(export)]
1112#[serde(tag = "type", rename_all = "camelCase")]
1113pub struct Solid {
1114    /// The id of the solid.
1115    pub id: uuid::Uuid,
1116    /// The artifact ID of the solid.  Unlike `id`, this doesn't change.
1117    pub artifact_id: ArtifactId,
1118    /// The extrude surfaces.
1119    pub value: Vec<ExtrudeSurface>,
1120    /// How this solid was created.
1121    #[serde(rename = "sketch")]
1122    pub creator: SolidCreator,
1123    /// The id of the extrusion start cap
1124    pub start_cap_id: Option<uuid::Uuid>,
1125    /// The id of the extrusion end cap
1126    pub end_cap_id: Option<uuid::Uuid>,
1127    /// Chamfers or fillets on this solid.
1128    #[serde(default, skip_serializing_if = "Vec::is_empty")]
1129    pub edge_cuts: Vec<EdgeCut>,
1130    /// The units of the solid.
1131    pub units: UnitLength,
1132    /// Is this a sectional solid?
1133    pub sectional: bool,
1134    /// Metadata.
1135    #[serde(skip)]
1136    pub meta: Vec<Metadata>,
1137}
1138
1139#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1140#[ts(export)]
1141pub struct CreatorFace {
1142    /// The face id that served as the base.
1143    pub face_id: uuid::Uuid,
1144    /// The solid id that owned the face.
1145    pub solid_id: uuid::Uuid,
1146    /// The sketch used for the operation.
1147    pub sketch: Sketch,
1148}
1149
1150/// How a solid was created.
1151#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1152#[ts(export)]
1153#[serde(tag = "creatorType", rename_all = "camelCase")]
1154pub enum SolidCreator {
1155    /// Created from a sketch.
1156    Sketch(Sketch),
1157    /// Created by extruding or modifying a face.
1158    Face(CreatorFace),
1159    /// Created procedurally without a sketch.
1160    Procedural,
1161}
1162
1163impl Solid {
1164    pub fn sketch(&self) -> Option<&Sketch> {
1165        match &self.creator {
1166            SolidCreator::Sketch(sketch) => Some(sketch),
1167            SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1168            SolidCreator::Procedural => None,
1169        }
1170    }
1171
1172    pub fn sketch_mut(&mut self) -> Option<&mut Sketch> {
1173        match &mut self.creator {
1174            SolidCreator::Sketch(sketch) => Some(sketch),
1175            SolidCreator::Face(CreatorFace { sketch, .. }) => Some(sketch),
1176            SolidCreator::Procedural => None,
1177        }
1178    }
1179
1180    pub fn sketch_id(&self) -> Option<uuid::Uuid> {
1181        self.sketch().map(|sketch| sketch.id)
1182    }
1183
1184    pub fn original_id(&self) -> uuid::Uuid {
1185        self.sketch().map(|sketch| sketch.original_id).unwrap_or(self.id)
1186    }
1187
1188    pub(crate) fn get_all_edge_cut_ids(&self) -> impl Iterator<Item = uuid::Uuid> + '_ {
1189        self.edge_cuts.iter().map(|foc| foc.id())
1190    }
1191}
1192
1193/// A fillet or a chamfer.
1194#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1195#[ts(export)]
1196#[serde(tag = "type", rename_all = "camelCase")]
1197pub enum EdgeCut {
1198    /// A fillet.
1199    Fillet {
1200        /// The id of the engine command that called this fillet.
1201        id: uuid::Uuid,
1202        radius: TyF64,
1203        /// The engine id of the edge to fillet.
1204        #[serde(rename = "edgeId")]
1205        edge_id: uuid::Uuid,
1206        tag: Box<Option<TagNode>>,
1207    },
1208    /// A chamfer.
1209    Chamfer {
1210        /// The id of the engine command that called this chamfer.
1211        id: uuid::Uuid,
1212        length: TyF64,
1213        /// The engine id of the edge to chamfer.
1214        #[serde(rename = "edgeId")]
1215        edge_id: uuid::Uuid,
1216        tag: Box<Option<TagNode>>,
1217    },
1218}
1219
1220impl EdgeCut {
1221    pub fn id(&self) -> uuid::Uuid {
1222        match self {
1223            EdgeCut::Fillet { id, .. } => *id,
1224            EdgeCut::Chamfer { id, .. } => *id,
1225        }
1226    }
1227
1228    pub fn set_id(&mut self, id: uuid::Uuid) {
1229        match self {
1230            EdgeCut::Fillet { id: i, .. } => *i = id,
1231            EdgeCut::Chamfer { id: i, .. } => *i = id,
1232        }
1233    }
1234
1235    pub fn edge_id(&self) -> uuid::Uuid {
1236        match self {
1237            EdgeCut::Fillet { edge_id, .. } => *edge_id,
1238            EdgeCut::Chamfer { edge_id, .. } => *edge_id,
1239        }
1240    }
1241
1242    pub fn set_edge_id(&mut self, id: uuid::Uuid) {
1243        match self {
1244            EdgeCut::Fillet { edge_id: i, .. } => *i = id,
1245            EdgeCut::Chamfer { edge_id: i, .. } => *i = id,
1246        }
1247    }
1248
1249    pub fn tag(&self) -> Option<TagNode> {
1250        match self {
1251            EdgeCut::Fillet { tag, .. } => *tag.clone(),
1252            EdgeCut::Chamfer { tag, .. } => *tag.clone(),
1253        }
1254    }
1255}
1256
1257#[derive(Debug, Serialize, PartialEq, Clone, Copy, ts_rs::TS)]
1258#[ts(export)]
1259pub struct Point2d {
1260    pub x: f64,
1261    pub y: f64,
1262    pub units: UnitLength,
1263}
1264
1265impl Point2d {
1266    pub const ZERO: Self = Self {
1267        x: 0.0,
1268        y: 0.0,
1269        units: UnitLength::Millimeters,
1270    };
1271
1272    pub fn new(x: f64, y: f64, units: UnitLength) -> Self {
1273        Self { x, y, units }
1274    }
1275
1276    pub fn into_x(self) -> TyF64 {
1277        TyF64::new(self.x, self.units.into())
1278    }
1279
1280    pub fn into_y(self) -> TyF64 {
1281        TyF64::new(self.y, self.units.into())
1282    }
1283
1284    pub fn ignore_units(self) -> [f64; 2] {
1285        [self.x, self.y]
1286    }
1287}
1288
1289#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy, ts_rs::TS, Default)]
1290#[ts(export)]
1291pub struct Point3d {
1292    pub x: f64,
1293    pub y: f64,
1294    pub z: f64,
1295    pub units: Option<UnitLength>,
1296}
1297
1298impl Point3d {
1299    pub const ZERO: Self = Self {
1300        x: 0.0,
1301        y: 0.0,
1302        z: 0.0,
1303        units: Some(UnitLength::Millimeters),
1304    };
1305
1306    pub fn new(x: f64, y: f64, z: f64, units: Option<UnitLength>) -> Self {
1307        Self { x, y, z, units }
1308    }
1309
1310    pub const fn is_zero(&self) -> bool {
1311        self.x == 0.0 && self.y == 0.0 && self.z == 0.0
1312    }
1313
1314    /// Calculate the cross product of this vector with another.
1315    ///
1316    /// This should only be applied to axes or other vectors which represent only a direction (and
1317    /// no magnitude) since units are ignored.
1318    pub fn axes_cross_product(&self, other: &Self) -> Self {
1319        Self {
1320            x: self.y * other.z - self.z * other.y,
1321            y: self.z * other.x - self.x * other.z,
1322            z: self.x * other.y - self.y * other.x,
1323            units: None,
1324        }
1325    }
1326
1327    /// Calculate the dot product of this vector with another.
1328    ///
1329    /// This should only be applied to axes or other vectors which represent only a direction (and
1330    /// no magnitude) since units are ignored.
1331    pub fn axes_dot_product(&self, other: &Self) -> f64 {
1332        let x = self.x * other.x;
1333        let y = self.y * other.y;
1334        let z = self.z * other.z;
1335        x + y + z
1336    }
1337
1338    pub fn normalize(&self) -> Self {
1339        let len = f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
1340        Point3d {
1341            x: self.x / len,
1342            y: self.y / len,
1343            z: self.z / len,
1344            units: None,
1345        }
1346    }
1347
1348    pub fn as_3_dims(&self) -> ([f64; 3], Option<UnitLength>) {
1349        let p = [self.x, self.y, self.z];
1350        let u = self.units;
1351        (p, u)
1352    }
1353
1354    pub(crate) fn negated(self) -> Self {
1355        Self {
1356            x: -self.x,
1357            y: -self.y,
1358            z: -self.z,
1359            units: self.units,
1360        }
1361    }
1362}
1363
1364impl From<[TyF64; 3]> for Point3d {
1365    fn from(p: [TyF64; 3]) -> Self {
1366        Self {
1367            x: p[0].n,
1368            y: p[1].n,
1369            z: p[2].n,
1370            units: p[0].ty.as_length(),
1371        }
1372    }
1373}
1374
1375impl From<Point3d> for Point3D {
1376    fn from(p: Point3d) -> Self {
1377        Self { x: p.x, y: p.y, z: p.z }
1378    }
1379}
1380
1381impl From<Point3d> for kittycad_modeling_cmds::shared::Point3d<LengthUnit> {
1382    fn from(p: Point3d) -> Self {
1383        if let Some(units) = p.units {
1384            Self {
1385                x: LengthUnit(adjust_length(units, p.x, UnitLength::Millimeters).0),
1386                y: LengthUnit(adjust_length(units, p.y, UnitLength::Millimeters).0),
1387                z: LengthUnit(adjust_length(units, p.z, UnitLength::Millimeters).0),
1388            }
1389        } else {
1390            Self {
1391                x: LengthUnit(p.x),
1392                y: LengthUnit(p.y),
1393                z: LengthUnit(p.z),
1394            }
1395        }
1396    }
1397}
1398
1399impl Add for Point3d {
1400    type Output = Point3d;
1401
1402    fn add(self, rhs: Self) -> Self::Output {
1403        // TODO should assert that self and rhs the same units or coerce them
1404        Point3d {
1405            x: self.x + rhs.x,
1406            y: self.y + rhs.y,
1407            z: self.z + rhs.z,
1408            units: self.units,
1409        }
1410    }
1411}
1412
1413impl AddAssign for Point3d {
1414    fn add_assign(&mut self, rhs: Self) {
1415        *self = *self + rhs
1416    }
1417}
1418
1419impl Sub for Point3d {
1420    type Output = Point3d;
1421
1422    fn sub(self, rhs: Self) -> Self::Output {
1423        let (x, y, z) = if rhs.units != self.units
1424            && let Some(sunits) = self.units
1425            && let Some(runits) = rhs.units
1426        {
1427            (
1428                adjust_length(runits, rhs.x, sunits).0,
1429                adjust_length(runits, rhs.y, sunits).0,
1430                adjust_length(runits, rhs.z, sunits).0,
1431            )
1432        } else {
1433            (rhs.x, rhs.y, rhs.z)
1434        };
1435        Point3d {
1436            x: self.x - x,
1437            y: self.y - y,
1438            z: self.z - z,
1439            units: self.units,
1440        }
1441    }
1442}
1443
1444impl SubAssign for Point3d {
1445    fn sub_assign(&mut self, rhs: Self) {
1446        *self = *self - rhs
1447    }
1448}
1449
1450impl Mul<f64> for Point3d {
1451    type Output = Point3d;
1452
1453    fn mul(self, rhs: f64) -> Self::Output {
1454        Point3d {
1455            x: self.x * rhs,
1456            y: self.y * rhs,
1457            z: self.z * rhs,
1458            units: self.units,
1459        }
1460    }
1461}
1462
1463/// A base path.
1464#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1465#[ts(export)]
1466#[serde(rename_all = "camelCase")]
1467pub struct BasePath {
1468    /// The from point.
1469    #[ts(type = "[number, number]")]
1470    pub from: [f64; 2],
1471    /// The to point.
1472    #[ts(type = "[number, number]")]
1473    pub to: [f64; 2],
1474    pub units: UnitLength,
1475    /// The tag of the path.
1476    pub tag: Option<TagNode>,
1477    /// Metadata.
1478    #[serde(rename = "__geoMeta")]
1479    pub geo_meta: GeoMeta,
1480}
1481
1482impl BasePath {
1483    pub fn get_to(&self) -> [TyF64; 2] {
1484        let ty: NumericType = self.units.into();
1485        [TyF64::new(self.to[0], ty), TyF64::new(self.to[1], ty)]
1486    }
1487
1488    pub fn get_from(&self) -> [TyF64; 2] {
1489        let ty: NumericType = self.units.into();
1490        [TyF64::new(self.from[0], ty), TyF64::new(self.from[1], ty)]
1491    }
1492}
1493
1494/// Geometry metadata.
1495#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1496#[ts(export)]
1497#[serde(rename_all = "camelCase")]
1498pub struct GeoMeta {
1499    /// The id of the geometry.
1500    pub id: uuid::Uuid,
1501    /// Metadata.
1502    #[serde(flatten)]
1503    pub metadata: Metadata,
1504}
1505
1506/// A path.
1507#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1508#[ts(export)]
1509#[serde(tag = "type")]
1510pub enum Path {
1511    /// A straight line which ends at the given point.
1512    ToPoint {
1513        #[serde(flatten)]
1514        base: BasePath,
1515    },
1516    /// A arc that is tangential to the last path segment that goes to a point
1517    TangentialArcTo {
1518        #[serde(flatten)]
1519        base: BasePath,
1520        /// the arc's center
1521        #[ts(type = "[number, number]")]
1522        center: [f64; 2],
1523        /// arc's direction
1524        ccw: bool,
1525    },
1526    /// A arc that is tangential to the last path segment
1527    TangentialArc {
1528        #[serde(flatten)]
1529        base: BasePath,
1530        /// the arc's center
1531        #[ts(type = "[number, number]")]
1532        center: [f64; 2],
1533        /// arc's direction
1534        ccw: bool,
1535    },
1536    // TODO: consolidate segment enums, remove Circle. https://github.com/KittyCAD/modeling-app/issues/3940
1537    /// a complete arc
1538    Circle {
1539        #[serde(flatten)]
1540        base: BasePath,
1541        /// the arc's center
1542        #[ts(type = "[number, number]")]
1543        center: [f64; 2],
1544        /// the arc's radius
1545        radius: f64,
1546        /// arc's direction
1547        /// This is used to compute the tangential angle.
1548        ccw: bool,
1549    },
1550    CircleThreePoint {
1551        #[serde(flatten)]
1552        base: BasePath,
1553        /// Point 1 of the circle
1554        #[ts(type = "[number, number]")]
1555        p1: [f64; 2],
1556        /// Point 2 of the circle
1557        #[ts(type = "[number, number]")]
1558        p2: [f64; 2],
1559        /// Point 3 of the circle
1560        #[ts(type = "[number, number]")]
1561        p3: [f64; 2],
1562    },
1563    ArcThreePoint {
1564        #[serde(flatten)]
1565        base: BasePath,
1566        /// Point 1 of the arc (base on the end of previous segment)
1567        #[ts(type = "[number, number]")]
1568        p1: [f64; 2],
1569        /// Point 2 of the arc (interiorAbsolute kwarg)
1570        #[ts(type = "[number, number]")]
1571        p2: [f64; 2],
1572        /// Point 3 of the arc (endAbsolute kwarg)
1573        #[ts(type = "[number, number]")]
1574        p3: [f64; 2],
1575    },
1576    /// A path that is horizontal.
1577    Horizontal {
1578        #[serde(flatten)]
1579        base: BasePath,
1580        /// The x coordinate.
1581        x: f64,
1582    },
1583    /// An angled line to.
1584    AngledLineTo {
1585        #[serde(flatten)]
1586        base: BasePath,
1587        /// The x coordinate.
1588        x: Option<f64>,
1589        /// The y coordinate.
1590        y: Option<f64>,
1591    },
1592    /// A base path.
1593    Base {
1594        #[serde(flatten)]
1595        base: BasePath,
1596    },
1597    /// A circular arc, not necessarily tangential to the current point.
1598    Arc {
1599        #[serde(flatten)]
1600        base: BasePath,
1601        /// Center of the circle that this arc is drawn on.
1602        center: [f64; 2],
1603        /// Radius of the circle that this arc is drawn on.
1604        radius: f64,
1605        /// True if the arc is counterclockwise.
1606        ccw: bool,
1607    },
1608    Ellipse {
1609        #[serde(flatten)]
1610        base: BasePath,
1611        center: [f64; 2],
1612        major_axis: [f64; 2],
1613        minor_radius: f64,
1614        ccw: bool,
1615    },
1616    //TODO: (bc) figure this out
1617    Conic {
1618        #[serde(flatten)]
1619        base: BasePath,
1620    },
1621    /// A cubic Bezier curve.
1622    Bezier {
1623        #[serde(flatten)]
1624        base: BasePath,
1625        /// First control point (absolute coordinates).
1626        #[ts(type = "[number, number]")]
1627        control1: [f64; 2],
1628        /// Second control point (absolute coordinates).
1629        #[ts(type = "[number, number]")]
1630        control2: [f64; 2],
1631    },
1632}
1633
1634impl Path {
1635    pub fn get_id(&self) -> uuid::Uuid {
1636        match self {
1637            Path::ToPoint { base } => base.geo_meta.id,
1638            Path::Horizontal { base, .. } => base.geo_meta.id,
1639            Path::AngledLineTo { base, .. } => base.geo_meta.id,
1640            Path::Base { base } => base.geo_meta.id,
1641            Path::TangentialArcTo { base, .. } => base.geo_meta.id,
1642            Path::TangentialArc { base, .. } => base.geo_meta.id,
1643            Path::Circle { base, .. } => base.geo_meta.id,
1644            Path::CircleThreePoint { base, .. } => base.geo_meta.id,
1645            Path::Arc { base, .. } => base.geo_meta.id,
1646            Path::ArcThreePoint { base, .. } => base.geo_meta.id,
1647            Path::Ellipse { base, .. } => base.geo_meta.id,
1648            Path::Conic { base, .. } => base.geo_meta.id,
1649            Path::Bezier { base, .. } => base.geo_meta.id,
1650        }
1651    }
1652
1653    pub fn set_id(&mut self, id: uuid::Uuid) {
1654        match self {
1655            Path::ToPoint { base } => base.geo_meta.id = id,
1656            Path::Horizontal { base, .. } => base.geo_meta.id = id,
1657            Path::AngledLineTo { base, .. } => base.geo_meta.id = id,
1658            Path::Base { base } => base.geo_meta.id = id,
1659            Path::TangentialArcTo { base, .. } => base.geo_meta.id = id,
1660            Path::TangentialArc { base, .. } => base.geo_meta.id = id,
1661            Path::Circle { base, .. } => base.geo_meta.id = id,
1662            Path::CircleThreePoint { base, .. } => base.geo_meta.id = id,
1663            Path::Arc { base, .. } => base.geo_meta.id = id,
1664            Path::ArcThreePoint { base, .. } => base.geo_meta.id = id,
1665            Path::Ellipse { base, .. } => base.geo_meta.id = id,
1666            Path::Conic { base, .. } => base.geo_meta.id = id,
1667            Path::Bezier { base, .. } => base.geo_meta.id = id,
1668        }
1669    }
1670
1671    pub fn get_tag(&self) -> Option<TagNode> {
1672        match self {
1673            Path::ToPoint { base } => base.tag.clone(),
1674            Path::Horizontal { base, .. } => base.tag.clone(),
1675            Path::AngledLineTo { base, .. } => base.tag.clone(),
1676            Path::Base { base } => base.tag.clone(),
1677            Path::TangentialArcTo { base, .. } => base.tag.clone(),
1678            Path::TangentialArc { base, .. } => base.tag.clone(),
1679            Path::Circle { base, .. } => base.tag.clone(),
1680            Path::CircleThreePoint { base, .. } => base.tag.clone(),
1681            Path::Arc { base, .. } => base.tag.clone(),
1682            Path::ArcThreePoint { base, .. } => base.tag.clone(),
1683            Path::Ellipse { base, .. } => base.tag.clone(),
1684            Path::Conic { base, .. } => base.tag.clone(),
1685            Path::Bezier { base, .. } => base.tag.clone(),
1686        }
1687    }
1688
1689    pub fn get_base(&self) -> &BasePath {
1690        match self {
1691            Path::ToPoint { base } => base,
1692            Path::Horizontal { base, .. } => base,
1693            Path::AngledLineTo { base, .. } => base,
1694            Path::Base { base } => base,
1695            Path::TangentialArcTo { base, .. } => base,
1696            Path::TangentialArc { base, .. } => base,
1697            Path::Circle { base, .. } => base,
1698            Path::CircleThreePoint { base, .. } => base,
1699            Path::Arc { base, .. } => base,
1700            Path::ArcThreePoint { base, .. } => base,
1701            Path::Ellipse { base, .. } => base,
1702            Path::Conic { base, .. } => base,
1703            Path::Bezier { base, .. } => base,
1704        }
1705    }
1706
1707    /// Where does this path segment start?
1708    pub fn get_from(&self) -> [TyF64; 2] {
1709        let p = &self.get_base().from;
1710        let ty: NumericType = self.get_base().units.into();
1711        [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1712    }
1713
1714    /// Where does this path segment end?
1715    pub fn get_to(&self) -> [TyF64; 2] {
1716        let p = &self.get_base().to;
1717        let ty: NumericType = self.get_base().units.into();
1718        [TyF64::new(p[0], ty), TyF64::new(p[1], ty)]
1719    }
1720
1721    /// The path segment start point and its type.
1722    pub fn start_point_components(&self) -> ([f64; 2], NumericType) {
1723        let p = &self.get_base().from;
1724        let ty: NumericType = self.get_base().units.into();
1725        (*p, ty)
1726    }
1727
1728    /// The path segment end point and its type.
1729    pub fn end_point_components(&self) -> ([f64; 2], NumericType) {
1730        let p = &self.get_base().to;
1731        let ty: NumericType = self.get_base().units.into();
1732        (*p, ty)
1733    }
1734
1735    /// Length of this path segment, in cartesian plane. Not all segment types
1736    /// are supported.
1737    pub fn length(&self) -> Option<TyF64> {
1738        let n = match self {
1739            Self::ToPoint { .. } | Self::Base { .. } | Self::Horizontal { .. } | Self::AngledLineTo { .. } => {
1740                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1741            }
1742            Self::TangentialArc {
1743                base: _,
1744                center,
1745                ccw: _,
1746            }
1747            | Self::TangentialArcTo {
1748                base: _,
1749                center,
1750                ccw: _,
1751            } => {
1752                // The radius can be calculated as the linear distance between `to` and `center`,
1753                // or between `from` and `center`. They should be the same.
1754                let radius = linear_distance(&self.get_base().from, center);
1755                debug_assert_eq!(radius, linear_distance(&self.get_base().to, center));
1756                // TODO: Call engine utils to figure this out.
1757                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1758            }
1759            Self::Circle { radius, .. } => Some(TAU * radius),
1760            Self::CircleThreePoint { .. } => {
1761                let circle_center = crate::std::utils::calculate_circle_from_3_points([
1762                    self.get_base().from,
1763                    self.get_base().to,
1764                    self.get_base().to,
1765                ]);
1766                let radius = linear_distance(
1767                    &[circle_center.center[0], circle_center.center[1]],
1768                    &self.get_base().from,
1769                );
1770                Some(TAU * radius)
1771            }
1772            Self::Arc { .. } => {
1773                // TODO: Call engine utils to figure this out.
1774                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1775            }
1776            Self::ArcThreePoint { .. } => {
1777                // TODO: Call engine utils to figure this out.
1778                Some(linear_distance(&self.get_base().from, &self.get_base().to))
1779            }
1780            Self::Ellipse { .. } => {
1781                // Not supported.
1782                None
1783            }
1784            Self::Conic { .. } => {
1785                // Not supported.
1786                None
1787            }
1788            Self::Bezier { .. } => {
1789                // Not supported - Bezier curve length requires numerical integration.
1790                None
1791            }
1792        };
1793        n.map(|n| TyF64::new(n, self.get_base().units.into()))
1794    }
1795
1796    pub fn get_base_mut(&mut self) -> &mut BasePath {
1797        match self {
1798            Path::ToPoint { base } => base,
1799            Path::Horizontal { base, .. } => base,
1800            Path::AngledLineTo { base, .. } => base,
1801            Path::Base { base } => base,
1802            Path::TangentialArcTo { base, .. } => base,
1803            Path::TangentialArc { base, .. } => base,
1804            Path::Circle { base, .. } => base,
1805            Path::CircleThreePoint { base, .. } => base,
1806            Path::Arc { base, .. } => base,
1807            Path::ArcThreePoint { base, .. } => base,
1808            Path::Ellipse { base, .. } => base,
1809            Path::Conic { base, .. } => base,
1810            Path::Bezier { base, .. } => base,
1811        }
1812    }
1813
1814    pub(crate) fn get_tangential_info(&self) -> GetTangentialInfoFromPathsResult {
1815        match self {
1816            Path::TangentialArc { center, ccw, .. }
1817            | Path::TangentialArcTo { center, ccw, .. }
1818            | Path::Arc { center, ccw, .. } => GetTangentialInfoFromPathsResult::Arc {
1819                center: *center,
1820                ccw: *ccw,
1821            },
1822            Path::ArcThreePoint { p1, p2, p3, .. } => {
1823                let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1824                GetTangentialInfoFromPathsResult::Arc {
1825                    center: circle.center,
1826                    ccw: crate::std::utils::is_points_ccw(&[*p1, *p2, *p3]) > 0,
1827                }
1828            }
1829            Path::Circle {
1830                center, ccw, radius, ..
1831            } => GetTangentialInfoFromPathsResult::Circle {
1832                center: *center,
1833                ccw: *ccw,
1834                radius: *radius,
1835            },
1836            Path::CircleThreePoint { p1, p2, p3, .. } => {
1837                let circle = crate::std::utils::calculate_circle_from_3_points([*p1, *p2, *p3]);
1838                let center_point = [circle.center[0], circle.center[1]];
1839                GetTangentialInfoFromPathsResult::Circle {
1840                    center: center_point,
1841                    // Note: a circle is always ccw regardless of the order of points
1842                    ccw: true,
1843                    radius: circle.radius,
1844                }
1845            }
1846            // TODO: (bc) fix me
1847            Path::Ellipse {
1848                center,
1849                major_axis,
1850                minor_radius,
1851                ccw,
1852                ..
1853            } => GetTangentialInfoFromPathsResult::Ellipse {
1854                center: *center,
1855                major_axis: *major_axis,
1856                _minor_radius: *minor_radius,
1857                ccw: *ccw,
1858            },
1859            Path::Conic { .. }
1860            | Path::ToPoint { .. }
1861            | Path::Horizontal { .. }
1862            | Path::AngledLineTo { .. }
1863            | Path::Base { .. }
1864            | Path::Bezier { .. } => {
1865                let base = self.get_base();
1866                GetTangentialInfoFromPathsResult::PreviousPoint(base.from)
1867            }
1868        }
1869    }
1870
1871    /// i.e. not a curve
1872    pub(crate) fn is_straight_line(&self) -> bool {
1873        matches!(self, Path::AngledLineTo { .. } | Path::ToPoint { .. })
1874    }
1875}
1876
1877/// Compute the straight-line distance between a pair of (2D) points.
1878#[rustfmt::skip]
1879fn linear_distance(
1880    [x0, y0]: &[f64; 2],
1881    [x1, y1]: &[f64; 2]
1882) -> f64 {
1883    let y_sq = (y1 - y0).powi(2);
1884    let x_sq = (x1 - x0).powi(2);
1885    (y_sq + x_sq).sqrt()
1886}
1887
1888/// An extrude surface.
1889#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1890#[ts(export)]
1891#[serde(tag = "type", rename_all = "camelCase")]
1892pub enum ExtrudeSurface {
1893    /// An extrude plane.
1894    ExtrudePlane(ExtrudePlane),
1895    ExtrudeArc(ExtrudeArc),
1896    Chamfer(ChamferSurface),
1897    Fillet(FilletSurface),
1898}
1899
1900// Chamfer surface.
1901#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1902#[ts(export)]
1903#[serde(rename_all = "camelCase")]
1904pub struct ChamferSurface {
1905    /// The id for the chamfer surface.
1906    pub face_id: uuid::Uuid,
1907    /// The tag.
1908    pub tag: Option<Node<TagDeclarator>>,
1909    /// Metadata.
1910    #[serde(flatten)]
1911    pub geo_meta: GeoMeta,
1912}
1913
1914// Fillet surface.
1915#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1916#[ts(export)]
1917#[serde(rename_all = "camelCase")]
1918pub struct FilletSurface {
1919    /// The id for the fillet surface.
1920    pub face_id: uuid::Uuid,
1921    /// The tag.
1922    pub tag: Option<Node<TagDeclarator>>,
1923    /// Metadata.
1924    #[serde(flatten)]
1925    pub geo_meta: GeoMeta,
1926}
1927
1928/// An extruded plane.
1929#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1930#[ts(export)]
1931#[serde(rename_all = "camelCase")]
1932pub struct ExtrudePlane {
1933    /// The face id for the extrude plane.
1934    pub face_id: uuid::Uuid,
1935    /// The tag.
1936    pub tag: Option<Node<TagDeclarator>>,
1937    /// Metadata.
1938    #[serde(flatten)]
1939    pub geo_meta: GeoMeta,
1940}
1941
1942/// An extruded arc.
1943#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1944#[ts(export)]
1945#[serde(rename_all = "camelCase")]
1946pub struct ExtrudeArc {
1947    /// The face id for the extrude plane.
1948    pub face_id: uuid::Uuid,
1949    /// The tag.
1950    pub tag: Option<Node<TagDeclarator>>,
1951    /// Metadata.
1952    #[serde(flatten)]
1953    pub geo_meta: GeoMeta,
1954}
1955
1956impl ExtrudeSurface {
1957    pub fn get_id(&self) -> uuid::Uuid {
1958        match self {
1959            ExtrudeSurface::ExtrudePlane(ep) => ep.geo_meta.id,
1960            ExtrudeSurface::ExtrudeArc(ea) => ea.geo_meta.id,
1961            ExtrudeSurface::Fillet(f) => f.geo_meta.id,
1962            ExtrudeSurface::Chamfer(c) => c.geo_meta.id,
1963        }
1964    }
1965
1966    pub fn face_id(&self) -> uuid::Uuid {
1967        match self {
1968            ExtrudeSurface::ExtrudePlane(ep) => ep.face_id,
1969            ExtrudeSurface::ExtrudeArc(ea) => ea.face_id,
1970            ExtrudeSurface::Fillet(f) => f.face_id,
1971            ExtrudeSurface::Chamfer(c) => c.face_id,
1972        }
1973    }
1974
1975    pub fn set_face_id(&mut self, face_id: uuid::Uuid) {
1976        match self {
1977            ExtrudeSurface::ExtrudePlane(ep) => ep.face_id = face_id,
1978            ExtrudeSurface::ExtrudeArc(ea) => ea.face_id = face_id,
1979            ExtrudeSurface::Fillet(f) => f.face_id = face_id,
1980            ExtrudeSurface::Chamfer(c) => c.face_id = face_id,
1981        }
1982    }
1983
1984    pub fn set_surface_tag(&mut self, tag: &TagNode) {
1985        match self {
1986            ExtrudeSurface::ExtrudePlane(extrude_plane) => extrude_plane.tag = Some(tag.clone()),
1987            ExtrudeSurface::ExtrudeArc(extrude_arc) => extrude_arc.tag = Some(tag.clone()),
1988            ExtrudeSurface::Chamfer(chamfer) => chamfer.tag = Some(tag.clone()),
1989            ExtrudeSurface::Fillet(fillet) => fillet.tag = Some(tag.clone()),
1990        }
1991    }
1992
1993    pub fn get_tag(&self) -> Option<Node<TagDeclarator>> {
1994        match self {
1995            ExtrudeSurface::ExtrudePlane(ep) => ep.tag.clone(),
1996            ExtrudeSurface::ExtrudeArc(ea) => ea.tag.clone(),
1997            ExtrudeSurface::Fillet(f) => f.tag.clone(),
1998            ExtrudeSurface::Chamfer(c) => c.tag.clone(),
1999        }
2000    }
2001}
2002
2003#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, ts_rs::TS)]
2004pub struct SketchVarId(pub usize);
2005
2006impl SketchVarId {
2007    pub const INVALID: Self = Self(usize::MAX);
2008
2009    pub fn to_constraint_id(self, range: SourceRange) -> Result<ezpz::Id, KclError> {
2010        self.0.try_into().map_err(|_| {
2011            KclError::new_type(KclErrorDetails::new(
2012                "Cannot convert to constraint ID since the sketch variable ID is too large".to_owned(),
2013                vec![range],
2014            ))
2015        })
2016    }
2017}
2018
2019#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2020#[ts(export_to = "Geometry.ts")]
2021#[serde(rename_all = "camelCase")]
2022pub struct SketchVar {
2023    pub id: SketchVarId,
2024    pub initial_value: f64,
2025    pub ty: NumericType,
2026    #[serde(skip)]
2027    pub meta: Vec<Metadata>,
2028}
2029
2030impl SketchVar {
2031    pub fn initial_value_to_solver_units(
2032        &self,
2033        exec_state: &mut ExecState,
2034        source_range: SourceRange,
2035        description: &str,
2036    ) -> Result<TyF64, KclError> {
2037        let x_initial_value = KclValue::Number {
2038            value: self.initial_value,
2039            ty: self.ty,
2040            meta: vec![source_range.into()],
2041        };
2042        let normalized_value =
2043            normalize_to_solver_distance_unit(&x_initial_value, source_range, exec_state, description)?;
2044        normalized_value.as_ty_f64().ok_or_else(|| {
2045            let message = format!(
2046                "Expected number after coercion, but found {}",
2047                normalized_value.human_friendly_type()
2048            );
2049            debug_assert!(false, "{}", &message);
2050            KclError::new_internal(KclErrorDetails::new(message, vec![source_range]))
2051        })
2052    }
2053}
2054
2055#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2056#[ts(export_to = "Geometry.ts")]
2057#[serde(tag = "type")]
2058pub enum UnsolvedExpr {
2059    Known(TyF64),
2060    Unknown(SketchVarId),
2061}
2062
2063impl UnsolvedExpr {
2064    pub fn var(&self) -> Option<SketchVarId> {
2065        match self {
2066            UnsolvedExpr::Known(_) => None,
2067            UnsolvedExpr::Unknown(id) => Some(*id),
2068        }
2069    }
2070}
2071
2072pub type UnsolvedPoint2dExpr = [UnsolvedExpr; 2];
2073
2074#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2075#[ts(export_to = "Geometry.ts")]
2076#[serde(rename_all = "camelCase")]
2077pub struct ConstrainablePoint2d {
2078    pub vars: crate::front::Point2d<SketchVarId>,
2079    pub object_id: ObjectId,
2080}
2081
2082#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2083#[ts(export_to = "Geometry.ts")]
2084pub enum ConstrainablePoint2dOrOrigin {
2085    Point(ConstrainablePoint2d),
2086    Origin,
2087}
2088
2089#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2090#[ts(export_to = "Geometry.ts")]
2091#[serde(rename_all = "camelCase")]
2092pub struct ConstrainableLine2d {
2093    pub vars: [crate::front::Point2d<SketchVarId>; 2],
2094    pub object_id: ObjectId,
2095}
2096
2097#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2098#[ts(export_to = "Geometry.ts")]
2099#[serde(rename_all = "camelCase")]
2100pub struct UnsolvedSegment {
2101    /// The engine ID.
2102    pub id: Uuid,
2103    pub object_id: ObjectId,
2104    pub kind: UnsolvedSegmentKind,
2105    #[serde(skip_serializing_if = "Option::is_none")]
2106    pub tag: Option<TagIdentifier>,
2107    #[serde(skip)]
2108    pub node_path: Option<NodePath>,
2109    #[serde(skip)]
2110    pub meta: Vec<Metadata>,
2111}
2112
2113#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2114#[ts(export_to = "Geometry.ts")]
2115#[serde(rename_all = "camelCase")]
2116pub enum UnsolvedSegmentKind {
2117    Point {
2118        position: UnsolvedPoint2dExpr,
2119        ctor: Box<PointCtor>,
2120    },
2121    Line {
2122        start: UnsolvedPoint2dExpr,
2123        end: UnsolvedPoint2dExpr,
2124        ctor: Box<LineCtor>,
2125        start_object_id: ObjectId,
2126        end_object_id: ObjectId,
2127        construction: bool,
2128    },
2129    Arc {
2130        start: UnsolvedPoint2dExpr,
2131        end: UnsolvedPoint2dExpr,
2132        center: UnsolvedPoint2dExpr,
2133        ctor: Box<ArcCtor>,
2134        start_object_id: ObjectId,
2135        end_object_id: ObjectId,
2136        center_object_id: ObjectId,
2137        construction: bool,
2138    },
2139    Circle {
2140        start: UnsolvedPoint2dExpr,
2141        center: UnsolvedPoint2dExpr,
2142        ctor: Box<CircleCtor>,
2143        start_object_id: ObjectId,
2144        center_object_id: ObjectId,
2145        construction: bool,
2146    },
2147}
2148
2149impl UnsolvedSegmentKind {
2150    /// What kind of object is this (point, line, arc, etc)
2151    /// Suitable for use in user-facing messages.
2152    pub fn human_friendly_kind_with_article(&self) -> &'static str {
2153        match self {
2154            Self::Point { .. } => "a Point",
2155            Self::Line { .. } => "a Line",
2156            Self::Arc { .. } => "an Arc",
2157            Self::Circle { .. } => "a Circle",
2158        }
2159    }
2160}
2161
2162#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2163#[ts(export_to = "Geometry.ts")]
2164#[serde(rename_all = "camelCase")]
2165pub struct Segment {
2166    /// The engine ID.
2167    pub id: Uuid,
2168    pub object_id: ObjectId,
2169    pub kind: SegmentKind,
2170    pub surface: SketchSurface,
2171    /// The engine ID of the sketch that this is a part of.
2172    pub sketch_id: Uuid,
2173    #[serde(skip)]
2174    pub sketch: Option<Sketch>,
2175    #[serde(skip_serializing_if = "Option::is_none")]
2176    pub tag: Option<TagIdentifier>,
2177    #[serde(skip)]
2178    pub node_path: Option<NodePath>,
2179    #[serde(skip)]
2180    pub meta: Vec<Metadata>,
2181}
2182
2183impl Segment {
2184    pub fn is_construction(&self) -> bool {
2185        match &self.kind {
2186            SegmentKind::Point { .. } => true,
2187            SegmentKind::Line { construction, .. } => *construction,
2188            SegmentKind::Arc { construction, .. } => *construction,
2189            SegmentKind::Circle { construction, .. } => *construction,
2190        }
2191    }
2192}
2193
2194#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2195#[ts(export_to = "Geometry.ts")]
2196#[serde(rename_all = "camelCase")]
2197pub enum SegmentKind {
2198    Point {
2199        position: [TyF64; 2],
2200        ctor: Box<PointCtor>,
2201        #[serde(skip_serializing_if = "Option::is_none")]
2202        freedom: Option<Freedom>,
2203    },
2204    Line {
2205        start: [TyF64; 2],
2206        end: [TyF64; 2],
2207        ctor: Box<LineCtor>,
2208        start_object_id: ObjectId,
2209        end_object_id: ObjectId,
2210        #[serde(skip_serializing_if = "Option::is_none")]
2211        start_freedom: Option<Freedom>,
2212        #[serde(skip_serializing_if = "Option::is_none")]
2213        end_freedom: Option<Freedom>,
2214        construction: bool,
2215    },
2216    Arc {
2217        start: [TyF64; 2],
2218        end: [TyF64; 2],
2219        center: [TyF64; 2],
2220        ctor: Box<ArcCtor>,
2221        start_object_id: ObjectId,
2222        end_object_id: ObjectId,
2223        center_object_id: ObjectId,
2224        #[serde(skip_serializing_if = "Option::is_none")]
2225        start_freedom: Option<Freedom>,
2226        #[serde(skip_serializing_if = "Option::is_none")]
2227        end_freedom: Option<Freedom>,
2228        #[serde(skip_serializing_if = "Option::is_none")]
2229        center_freedom: Option<Freedom>,
2230        construction: bool,
2231    },
2232    Circle {
2233        start: [TyF64; 2],
2234        center: [TyF64; 2],
2235        ctor: Box<CircleCtor>,
2236        start_object_id: ObjectId,
2237        center_object_id: ObjectId,
2238        #[serde(skip_serializing_if = "Option::is_none")]
2239        start_freedom: Option<Freedom>,
2240        #[serde(skip_serializing_if = "Option::is_none")]
2241        center_freedom: Option<Freedom>,
2242        construction: bool,
2243    },
2244}
2245
2246#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2247#[ts(export_to = "Geometry.ts")]
2248#[serde(rename_all = "camelCase")]
2249pub struct AbstractSegment {
2250    pub repr: SegmentRepr,
2251    #[serde(skip)]
2252    pub meta: Vec<Metadata>,
2253}
2254
2255#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2256pub enum SegmentRepr {
2257    Unsolved { segment: Box<UnsolvedSegment> },
2258    Solved { segment: Box<Segment> },
2259}
2260
2261#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2262#[ts(export_to = "Geometry.ts")]
2263#[serde(rename_all = "camelCase")]
2264pub struct SketchConstraint {
2265    pub kind: SketchConstraintKind,
2266    #[serde(skip)]
2267    pub meta: Vec<Metadata>,
2268}
2269
2270#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
2271#[ts(export_to = "Geometry.ts")]
2272#[serde(rename_all = "camelCase")]
2273pub enum SketchConstraintKind {
2274    Angle {
2275        line0: ConstrainableLine2d,
2276        line1: ConstrainableLine2d,
2277    },
2278    Distance {
2279        points: [ConstrainablePoint2dOrOrigin; 2],
2280    },
2281    Radius {
2282        points: [ConstrainablePoint2d; 2],
2283    },
2284    Diameter {
2285        points: [ConstrainablePoint2d; 2],
2286    },
2287    HorizontalDistance {
2288        points: [ConstrainablePoint2dOrOrigin; 2],
2289    },
2290    VerticalDistance {
2291        points: [ConstrainablePoint2dOrOrigin; 2],
2292    },
2293}
2294
2295impl SketchConstraintKind {
2296    pub fn name(&self) -> &'static str {
2297        match self {
2298            SketchConstraintKind::Angle { .. } => "angle",
2299            SketchConstraintKind::Distance { .. } => "distance",
2300            SketchConstraintKind::Radius { .. } => "radius",
2301            SketchConstraintKind::Diameter { .. } => "diameter",
2302            SketchConstraintKind::HorizontalDistance { .. } => "horizontalDistance",
2303            SketchConstraintKind::VerticalDistance { .. } => "verticalDistance",
2304        }
2305    }
2306}