1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! API for transforming objects

mod curve;
mod cycle;
mod edge;
mod face;
mod path;
mod shell;
mod sketch;
mod solid;
mod surface;
mod vertex;

use fj_math::{Transform, Vector};

use crate::{
    partial::{HasPartial, MaybePartial, Partial},
    stores::Stores,
};

/// Transform an object
///
/// # Implementation Note
///
/// So far, a general `transform` method is available, along some convenience
/// methods for more specific transformations.
///
/// More convenience methods can be added as required. The only reason this
/// hasn't been done so far, is that no one has put in the work yet.
pub trait TransformObject: Sized {
    /// Transform the object
    #[must_use]
    fn transform(self, transform: &Transform, stores: &Stores) -> Self;

    /// Translate the object
    ///
    /// Convenience wrapper around [`TransformObject::transform`].
    #[must_use]
    fn translate(self, offset: impl Into<Vector<3>>, stores: &Stores) -> Self {
        self.transform(&Transform::translation(offset), stores)
    }

    /// Rotate the object
    ///
    /// Convenience wrapper around [`TransformObject::transform`].
    #[must_use]
    fn rotate(self, axis_angle: impl Into<Vector<3>>, stores: &Stores) -> Self {
        self.transform(&Transform::rotation(axis_angle), stores)
    }
}

impl<T> TransformObject for T
where
    T: HasPartial,
    T::Partial: TransformObject,
{
    fn transform(self, transform: &Transform, stores: &Stores) -> Self {
        self.to_partial().transform(transform, stores).build(stores)
    }
}

impl<T> TransformObject for MaybePartial<T>
where
    T: HasPartial + TransformObject,
    T::Partial: TransformObject,
{
    fn transform(self, transform: &Transform, stores: &Stores) -> Self {
        match self {
            Self::Full(full) => Self::Full(full.transform(transform, stores)),
            Self::Partial(partial) => {
                Self::Partial(partial.transform(transform, stores))
            }
        }
    }
}