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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Object frames: where an object is placed and, when its source says so,
//! which way it faces.
//!
//! Clearance, placement and orientation checks are measured in an object's
//! own frame: the area in front of a component, beside a fixture, along a
//! bay. This seam supplies that frame as a [`MetricFrame`] in canonical
//! metres, grounded on the requested object, together with the front the
//! source states for it.
//!
//! The frame's axes are the source's placement axes, nothing more. Its
//! `forward` axis is the placement's second axis and says nothing about which
//! side of the object is its front. A front is reported only where the source
//! states one ([`ObjectFront::Stated`]); otherwise it is
//! [`ObjectFront::NotStated`], never guessed from the axes, the shape or the
//! object's type.
//!
//! The same service supplies a door's leaves and a window's panels
//! ([`ObjectFrameService::leaves`], see [`crate::DoorLeaves`]): they are
//! placed in the door's or window's frame, so the source that places it
//! states them. The method refuses by default.
use std::sync::Arc;
use axioval_ir::{Evidence, ObjectId, SourceId};
use thiserror::Error;
use crate::services::reviewable_exact_evidence;
use crate::{
DoorLeaves, DoorLeavesError, MetricDirection, MetricFrame, SnapshotBoundService, SourceSnapshot,
};
/// Failure to supply an object's frame.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum ObjectFrameError {
/// The service does not cover the object's source.
#[error("object-frame service does not cover source `{0}`")]
UncoveredSource(SourceId),
/// The object is not part of the source.
#[error("object `{0}` is not in the source")]
UnknownObject(ObjectId),
/// The source states no placement for the object, so it has no frame.
/// Never read as the source's origin.
#[error("the source states no placement for `{0}`")]
NotPlaced(ObjectId),
/// The object is placed by a construct the service cannot resolve
/// exactly.
#[error("object placement unsupported: {0}")]
Unsupported(String),
/// The placement, or the unit it is stated in, is malformed or cannot be
/// read exactly.
#[error("object placement cannot be read exactly: {0}")]
Unreadable(String),
/// The frame is not grounded on the object, or its axes are not a
/// right-handed orthonormal triple.
#[error("object frame is invalid")]
InvalidFrame,
/// The frame's evidence is not exact, not reviewable, or from another
/// source.
#[error("object-frame evidence is not exact and reviewable")]
InexactEvidence,
/// The service answered for another object.
#[error("object-frame service answered for another object")]
ResponseRequestMismatch,
}
/// Which way an object faces, as far as its source states it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ObjectFront {
/// The source states the object's front: the direction, in canonical
/// coordinates, from the object towards the side it is used from.
Stated(MetricDirection),
/// The source states no front. A rule that needs one cannot be decided
/// from this frame; the placement axes are not a substitute.
NotStated,
}
/// One object's placement frame and stated front, with provenance.
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectFrame {
object: ObjectId,
frame: MetricFrame,
front: ObjectFront,
evidence: Evidence,
}
impl ObjectFrame {
/// The frame of `object`.
///
/// The frame's origin must be grounded on `object`, and the evidence must
/// be exact, reviewable and from the object's source: a placement is
/// stated, never estimated.
pub fn try_new(
object: ObjectId,
frame: MetricFrame,
front: ObjectFront,
evidence: Evidence,
) -> Result<Self, ObjectFrameError> {
if frame.origin().subject() != &object {
return Err(ObjectFrameError::InvalidFrame);
}
if !reviewable_exact_evidence(&evidence) || evidence.source != object.source {
return Err(ObjectFrameError::InexactEvidence);
}
Ok(Self {
object,
frame,
front,
evidence,
})
}
/// The object this frame belongs to.
#[must_use]
pub fn object(&self) -> &ObjectId {
&self.object
}
/// The placement frame: origin in canonical metres and right-handed
/// orthonormal right, forward and up axes.
#[must_use]
pub fn frame(&self) -> &MetricFrame {
&self.frame
}
/// The front the source states, or [`ObjectFront::NotStated`].
#[must_use]
pub fn front(&self) -> ObjectFront {
self.front
}
/// Reviewable provenance of the placement.
#[must_use]
pub fn evidence(&self) -> &Evidence {
&self.evidence
}
}
/// Trusted adapter seam supplying objects' placement frames.
pub trait ObjectFrameService: Send + Sync + 'static {
/// Exact source snapshots this service answers for.
fn source_snapshots(&self) -> &[SourceSnapshot];
/// The placement frame of `object`, or why it has none.
fn object_frame(&self, object: &ObjectId) -> Result<ObjectFrame, ObjectFrameError>;
/// The leaves of `door`, a door or a window: each leaf's (or window
/// panel's) closed position, width, motion, hinge side and swing
/// sector, and a window panel's height and tilt sector.
///
/// The default refuses with [`DoorLeavesError::Unsupported`], so a
/// service that reads no door operation fails closed.
fn leaves(&self, door: &ObjectId) -> Result<DoorLeaves, DoorLeavesError> {
let _ = door;
Err(DoorLeavesError::Unsupported)
}
}
/// Registry handle for an [`ObjectFrameService`].
#[derive(Clone)]
pub struct ObjectFrameServiceHandle(Arc<dyn ObjectFrameService>);
impl ObjectFrameServiceHandle {
/// Wraps a trusted object-frame service.
#[must_use]
pub fn new(service: Arc<dyn ObjectFrameService>) -> Self {
Self(service)
}
/// The frame of `object`. Objects of an uncovered source are refused,
/// and so is a frame of another object.
pub fn object_frame(&self, object: &ObjectId) -> Result<ObjectFrame, ObjectFrameError> {
if !self
.0
.source_snapshots()
.iter()
.any(|snapshot| *snapshot.source() == object.source)
{
return Err(ObjectFrameError::UncoveredSource(object.source.clone()));
}
let frame = self.0.object_frame(object)?;
if frame.object() != object {
return Err(ObjectFrameError::ResponseRequestMismatch);
}
Ok(frame)
}
/// The leaves of `door`, a door or a window. Objects of an uncovered
/// source are refused, and so are leaves of another object.
pub fn leaves(&self, door: &ObjectId) -> Result<DoorLeaves, DoorLeavesError> {
if !self
.0
.source_snapshots()
.iter()
.any(|snapshot| *snapshot.source() == door.source)
{
return Err(DoorLeavesError::UncoveredSource(door.source.clone()));
}
let leaves = self.0.leaves(door)?;
if leaves.door() != door {
return Err(DoorLeavesError::ResponseRequestMismatch);
}
Ok(leaves)
}
}
impl SnapshotBoundService for ObjectFrameServiceHandle {
fn source_snapshots(&self) -> &[SourceSnapshot] {
self.0.source_snapshots()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::MetricPoint;
fn source() -> SourceId {
SourceId::new("cad", "m").unwrap()
}
fn id(local: &str) -> ObjectId {
ObjectId::new(source(), local).unwrap()
}
fn direction(vector: [f64; 3]) -> MetricDirection {
MetricDirection::try_new(vector).unwrap()
}
fn frame(subject: &str) -> MetricFrame {
MetricFrame::try_new(
MetricPoint::try_new(id(subject), [1.0, 2.0, 0.0]).unwrap(),
direction([1.0, 0.0, 0.0]),
direction([0.0, 1.0, 0.0]),
direction([0.0, 0.0, 1.0]),
)
.unwrap()
}
fn exact() -> Evidence {
Evidence::exact(source(), "placement:a")
}
#[test]
fn a_frame_is_grounded_on_its_object() {
assert!(ObjectFrame::try_new(id("a"), frame("a"), ObjectFront::NotStated, exact()).is_ok());
assert_eq!(
ObjectFrame::try_new(id("a"), frame("b"), ObjectFront::NotStated, exact()),
Err(ObjectFrameError::InvalidFrame)
);
}
#[test]
fn a_frame_needs_exact_reviewable_evidence_from_its_source() {
let mut approximate = exact();
approximate.exact = false;
let unlocated = Evidence::exact(source(), " ");
let foreign = Evidence::exact(SourceId::new("cad", "other").unwrap(), "placement:a");
for evidence in [approximate, unlocated, foreign] {
assert_eq!(
ObjectFrame::try_new(id("a"), frame("a"), ObjectFront::NotStated, evidence),
Err(ObjectFrameError::InexactEvidence)
);
}
}
#[test]
fn a_left_handed_frame_cannot_be_formed() {
let mirrored = MetricFrame::try_new(
MetricPoint::try_new(id("a"), [0.0; 3]).unwrap(),
direction([-1.0, 0.0, 0.0]),
direction([0.0, 1.0, 0.0]),
direction([0.0, 0.0, 1.0]),
);
assert!(mirrored.is_err());
}
struct Fixed(Vec<SourceSnapshot>, ObjectFrame);
impl ObjectFrameService for Fixed {
fn source_snapshots(&self) -> &[SourceSnapshot] {
&self.0
}
fn object_frame(&self, _: &ObjectId) -> Result<ObjectFrame, ObjectFrameError> {
Ok(self.1.clone())
}
}
fn handle() -> ObjectFrameServiceHandle {
let snapshot = SourceSnapshot::try_new(source(), "r1", "sha256:1").unwrap();
let answer = ObjectFrame::try_new(
id("b"),
frame("b"),
ObjectFront::Stated(direction([0.0, -1.0, 0.0])),
exact(),
)
.unwrap();
ObjectFrameServiceHandle::new(Arc::new(Fixed(vec![snapshot], answer)))
}
#[test]
fn the_handle_binds_answers_to_the_request() {
let handle = handle();
let frame = handle.object_frame(&id("b")).unwrap();
assert_eq!(
frame.front(),
ObjectFront::Stated(direction([0.0, -1.0, 0.0]))
);
assert_eq!(
handle.object_frame(&id("a")),
Err(ObjectFrameError::ResponseRequestMismatch)
);
let foreign = ObjectId::new(SourceId::new("cad", "other").unwrap(), "b").unwrap();
assert!(matches!(
handle.object_frame(&foreign),
Err(ObjectFrameError::UncoveredSource(_))
));
}
#[test]
fn door_leaves_are_refused_by_default() {
let handle = handle();
assert_eq!(handle.leaves(&id("b")), Err(DoorLeavesError::Unsupported));
let foreign = ObjectId::new(SourceId::new("cad", "other").unwrap(), "b").unwrap();
assert!(matches!(
handle.leaves(&foreign),
Err(DoorLeavesError::UncoveredSource(_))
));
}
}