1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! API for transforming objects

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

use fj_math::{Transform, Vector};

use crate::{
    insert::Insert,
    objects::Objects,
    partial::{HasPartial, MaybePartial, Partial},
    storage::Handle,
    validate::{Validate, ValidationError},
};

/// 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
    fn transform(
        self,
        transform: &Transform,
        objects: &Objects,
    ) -> Result<Self, ValidationError>;

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

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

impl<T> TransformObject for Handle<T>
where
    T: HasPartial + Insert,
    T::Partial: TransformObject,
    ValidationError: From<<T as Validate>::Error>,
{
    fn transform(
        self,
        transform: &Transform,
        objects: &Objects,
    ) -> Result<Self, ValidationError> {
        Ok(self
            .to_partial()
            .transform(transform, objects)?
            .build(objects)?
            .insert(objects)?)
    }
}

impl<T> TransformObject for MaybePartial<T>
where
    T: HasPartial,
    Handle<T>: TransformObject,
    T::Partial: TransformObject,
{
    fn transform(
        self,
        transform: &Transform,
        objects: &Objects,
    ) -> Result<Self, ValidationError> {
        let transformed = match self {
            Self::Full(full) => {
                full.to_partial().transform(transform, objects)?
            }
            Self::Partial(partial) => partial.transform(transform, objects)?,
        };

        // Transforming a `MaybePartial` *always* results in a partial object.
        // This provides the most flexibility to the caller, who might want to
        // use the transformed partial object for merging or whatever else,
        // before building it themselves.
        Ok(Self::Partial(transformed))
    }
}