logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! Geometric graph traits.

// Geometric traits like `FaceNormal` and `EdgeMidpoint` are defined in such a
// way to reduce the contraints necessary for writing generic user code. With
// few exceptions, these traits only depend on `AsPosition` being implemented by
// the `Vertex` type in their definition. If a more complex implementation is
// necessary, constraints are specified there so that they do not pollute user
// code.

use theon::ops::{Cross, Interpolate, Project};
use theon::query::Plane;
use theon::space::{EuclideanSpace, FiniteDimensional, InnerSpace, Vector, VectorSpace};
use theon::{AsPosition, Position};
use typenum::U3;

use crate::entity::borrow::Reborrow;
use crate::entity::storage::AsStorage;
use crate::graph::data::{GraphData, Parametric};
use crate::graph::edge::{Arc, ArcView, Edge, ToArc};
use crate::graph::face::{Face, ToRing};
use crate::graph::mutation::Consistent;
use crate::graph::vertex::{Vertex, VertexView};
use crate::graph::{GraphError, OptionExt as _, ResultExt as _};
use crate::IteratorExt as _;

pub type VertexPosition<G> = Position<<G as GraphData>::Vertex>;

pub trait VertexCentroid: GraphData
where
    Self::Vertex: AsPosition,
{
    fn centroid<B>(vertex: VertexView<B>) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>;
}

impl<G> VertexCentroid for G
where
    G: GraphData,
    G::Vertex: AsPosition,
{
    fn centroid<B>(vertex: VertexView<B>) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
    {
        Ok(VertexPosition::<Self>::centroid(
            vertex
                .adjacent_vertices()
                .map(|vertex| *vertex.data.as_position()),
        )
        .expect_consistent())
    }
}

pub trait VertexNormal: FaceNormal
where
    Self::Vertex: AsPosition,
{
    fn normal<B>(vertex: VertexView<B>) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target: AsStorage<Arc<Self>>
            + AsStorage<Face<Self>>
            + AsStorage<Vertex<Self>>
            + Consistent
            + Parametric<Data = Self>;
}

impl<G> VertexNormal for G
where
    G: FaceNormal,
    G::Vertex: AsPosition,
{
    fn normal<B>(vertex: VertexView<B>) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target: AsStorage<Arc<Self>>
            + AsStorage<Face<Self>>
            + AsStorage<Vertex<Self>>
            + Consistent
            + Parametric<Data = Self>,
    {
        Vector::<VertexPosition<Self>>::mean(
            vertex
                .adjacent_faces()
                .map(<Self as FaceNormal>::normal)
                .collect::<Result<Vec<_>, _>>()?,
        )
        .expect_consistent()
        .normalize()
        .ok_or(GraphError::Geometry)
    }
}

pub trait ArcNormal: GraphData
where
    Self::Vertex: AsPosition,
{
    fn normal<B>(arc: ArcView<B>) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>;
}

impl<G> ArcNormal for G
where
    G: GraphData,
    G::Vertex: AsPosition,
    VertexPosition<G>: EuclideanSpace,
    Vector<VertexPosition<G>>: Project<Output = Vector<VertexPosition<G>>>,
{
    fn normal<B>(arc: ArcView<B>) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
    {
        let (a, b) = arc
            .adjacent_vertices()
            .map(|vertex| *vertex.position())
            .try_collect()
            .expect_consistent();
        let c = *arc.next_arc().destination_vertex().position();
        let ab = a - b;
        let cb = c - b;
        let p = b + ab.project(cb);
        (p - c).normalize().ok_or(GraphError::Geometry)
    }
}

pub trait EdgeMidpoint: GraphData
where
    Self::Vertex: AsPosition,
{
    fn midpoint<B, T>(edge: T) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target: AsStorage<Arc<Self>>
            + AsStorage<Edge<Self>>
            + AsStorage<Vertex<Self>>
            + Consistent
            + Parametric<Data = Self>,
        T: ToArc<B>;
}

impl<G> EdgeMidpoint for G
where
    G: GraphData,
    G::Vertex: AsPosition,
    VertexPosition<G>: Interpolate<Output = VertexPosition<G>>,
{
    fn midpoint<B, T>(edge: T) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target: AsStorage<Arc<Self>>
            + AsStorage<Edge<Self>>
            + AsStorage<Vertex<Self>>
            + Consistent
            + Parametric<Data = Self>,
        T: ToArc<B>,
    {
        let arc = edge.into_arc();
        let (a, b) = arc
            .adjacent_vertices()
            .map(|vertex| *vertex.position())
            .try_collect()
            .expect_consistent();
        Ok(a.midpoint(b))
    }
}

pub trait FaceCentroid: GraphData
where
    Self::Vertex: AsPosition,
{
    fn centroid<B, T>(ring: T) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
        T: ToRing<B>;
}

impl<G> FaceCentroid for G
where
    G: GraphData,
    G::Vertex: AsPosition,
{
    fn centroid<B, T>(ring: T) -> Result<VertexPosition<Self>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
        T: ToRing<B>,
    {
        let ring = ring.into_ring();
        Ok(
            VertexPosition::<Self>::centroid(ring.vertices().map(|vertex| *vertex.position()))
                .expect_consistent(),
        )
    }
}

pub trait FaceNormal: GraphData
where
    Self::Vertex: AsPosition,
{
    fn normal<B, T>(ring: T) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
        T: ToRing<B>;
}

impl<G> FaceNormal for G
where
    G: FaceCentroid + GraphData,
    G::Vertex: AsPosition,
    Vector<VertexPosition<G>>: Cross<Output = Vector<VertexPosition<G>>>,
    VertexPosition<G>: EuclideanSpace,
{
    fn normal<B, T>(ring: T) -> Result<Vector<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
        T: ToRing<B>,
    {
        let ring = ring.into_ring();
        let (a, b) = ring
            .vertices()
            .take(2)
            .map(|vertex| *vertex.position())
            .try_collect()
            .expect_consistent();
        let c = G::centroid(ring)?;
        let ab = a - b;
        let bc = b - c;
        ab.cross(bc).normalize().ok_or(GraphError::Geometry)
    }
}

pub trait FacePlane: GraphData
where
    Self::Vertex: AsPosition,
    VertexPosition<Self>: FiniteDimensional<N = U3>,
{
    fn plane<B, T>(ring: T) -> Result<Plane<VertexPosition<Self>>, GraphError>
    where
        B: Reborrow,
        B::Target:
            AsStorage<Arc<Self>> + AsStorage<Vertex<Self>> + Consistent + Parametric<Data = Self>,
        T: ToRing<B>;
}

// TODO: The `lapack` feature depends on `ndarray-linalg` and Intel MKL. MKL is
//       dynamically linked, but the linkage fails during doctests and may fail
//       when launching a binary. The `lapack` feature and this implementation
//       have been disabled until an upstream fix is available. See
//       https://github.com/olson-sean-k/plexus/issues/58 and
//       https://github.com/rust-ndarray/ndarray-linalg/issues/229
// TODO: The `lapack` feature only supports Linux. See
//       https://github.com/olson-sean-k/theon/issues/1
//
//#[cfg(target_os = "linux")]
//mod lapack {
//    use super::*;
//
//    use smallvec::SmallVec;
//    use theon::adjunct::{FromItems, IntoItems};
//    use theon::lapack::Lapack;
//    use theon::space::Scalar;
//
//    impl<G> FacePlane for G
//    where
//        G: GraphData,
//        G::Vertex: AsPosition,
//        VertexPosition<G>: EuclideanSpace + FiniteDimensional<N = U3>,
//        Scalar<VertexPosition<G>>: Lapack,
//        Vector<VertexPosition<G>>: FromItems + IntoItems,
//    {
//        fn plane<B, T>(ring: T) -> Result<Plane<VertexPosition<G>>, GraphError>
//        where
//            B: Reborrow,
//            B::Target: AsStorage<Arc<Self>>
//                + AsStorage<Vertex<Self>>
//                + Consistent
//                + Parametric<Data = Self>,
//            T: ToRing<B>,
//        {
//            let ring = ring.into_ring();
//            let points = ring
//                .vertices()
//                .map(|vertex| *vertex.data.as_position())
//                .collect::<SmallVec<[_; 4]>>();
//            Plane::from_points(points).ok_or(GraphError::Geometry)
//        }
//    }
//}