Skip to main content

printobserver_server/
judgements.rs

1//! The records of what the server judged of a print's camera frames.
2//!
3//! Every judgement of a print's first-layer alignment and of its camera's view
4//! is written into the print's history, each naming by its SHA-256 the stored
5//! frame it was made on ([`printobserver_core::ImageRef::sha256`]), so a
6//! reader can open the very frame behind any verdict. Two further kinds are
7//! change notices rather than judgements: the correction a pass put in force
8//! while the landmarks had moved ([`CameraViewRefitPayload`]), and each change
9//! in which image a print's checks are shown, with its reason
10//! ([`CheckImageChangedPayload`]).
11//!
12//! This crate alone writes these kinds, so it declares them, as it declares the
13//! startup reconciliation.
14
15use printobserver_panels::{ViewCorrection, ViewOutcome};
16use printobserver_supervisor_api::{ImageFallback, ImageKind};
17use printobserver_types::EventPayload;
18use printobserver_types::contract::Sample;
19use printobserver_types::schemars::JsonSchema;
20use printobserver_types::serde::{Deserialize, Serialize};
21
22/// What one judgement of a print's first layer came to.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
24#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
25#[schemars(crate = "printobserver_types::schemars")]
26pub enum AlignmentJudgedVerdict {
27    /// The first layer is clearly found, and its offset is the print's
28    /// correction from now on.
29    Passed,
30    /// The frame could not tell; a later check's frame is judged again.
31    Inconclusive,
32    /// The camera's frames cannot be judged against the calibration, which
33    /// holds for the rest of the print.
34    Failed,
35}
36
37/// One judgement of a print's first-layer alignment, on one frame.
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
39#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
40#[schemars(crate = "printobserver_types::schemars")]
41pub struct FirstLayerAlignmentJudgedPayload {
42    /// What it came to.
43    pub verdict: AlignmentJudgedVerdict,
44    /// How far the printed first layer was found from its uncorrected
45    /// projection, in calibration pixels; null when it was not searched for.
46    pub offset_px: Option<f64>,
47    /// The correction the pass put in force, `[dx, dy]` pixels; null unless it
48    /// passed.
49    pub correction_px: Option<[f64; 2]>,
50    /// The peak smoothed contrast the layer was found at, in grey levels; null
51    /// when it was not searched for.
52    pub match_strength: Option<f64>,
53    /// `clearly_found` for a pass, `covered` or `not_clearly_found` for an
54    /// inconclusive judgement, and the failure's own sentence for a failure.
55    pub reason: String,
56    /// The stored frame it was judged on.
57    pub frame_sha256: String,
58    /// The correction in force when the frame was judged; null before the
59    /// print's first pass.
60    pub view_correction: Option<ViewCorrection>,
61}
62
63impl EventPayload for FirstLayerAlignmentJudgedPayload {
64    const KIND: &'static str = "first_layer_alignment_judged";
65}
66
67/// One judgement of a print's camera view, on one frame: one for every
68/// judgement, whatever it found and whether or not it repeats the last.
69#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
70#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
71#[schemars(crate = "printobserver_types::schemars")]
72pub struct CameraViewJudgedPayload {
73    /// The stored frame it was judged on.
74    pub frame_sha256: String,
75    /// What it found.
76    pub outcome: ViewOutcome,
77    /// The correction in force when the frame was judged; null when none was.
78    pub view_correction: Option<ViewCorrection>,
79    /// Why the frame may not be drawn on: for the three refusals, and for a
80    /// refit view, whose camera has moved since its calibration; null for an
81    /// unshifted one.
82    pub reason: Option<String>,
83}
84
85impl EventPayload for CameraViewJudgedPayload {
86    const KIND: &'static str = "camera_view_judged";
87}
88
89/// A first-layer pass put a correction in force while the landmarks had moved
90/// from the calibration: the print's first, or one taken again after the
91/// camera moved again.
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
93#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
94#[schemars(crate = "printobserver_types::schemars")]
95pub struct CameraViewRefitPayload {
96    /// The stored frame the correction was taken on.
97    pub frame_sha256: String,
98    /// The correction now in force.
99    pub view_correction: ViewCorrection,
100}
101
102impl EventPayload for CameraViewRefitPayload {
103    const KIND: &'static str = "camera_view_refit";
104}
105
106/// The image a print's checks are shown changed: its first check's image, and
107/// every check whose image kind or fallback code differs from the last one
108/// recorded.
109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
110#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
111#[schemars(crate = "printobserver_types::schemars")]
112pub struct CheckImageChangedPayload {
113    /// Which image the check was shown.
114    pub kind: ImageKind,
115    /// Why it was the whole frame; null for the three-panel image.
116    pub fallback: Option<ImageFallback>,
117    /// The stored frame the check's image was made of.
118    pub frame_sha256: String,
119}
120
121impl EventPayload for CheckImageChangedPayload {
122    const KIND: &'static str = "check_image_changed";
123}
124
125/// A frame's SHA-256, as samples name one.
126const SAMPLE_SHA256: &str = "9f2c4d1e8b7a6f5e4d3c2b1a0f9e8d7c6b5a4f3e2d1c0b9a8f7e6d5c4b3a2f1e";
127
128/// The correction samples carry.
129fn sample_correction() -> ViewCorrection {
130    ViewCorrection::translation(
131        [14.0, 4.0],
132        vec![
133            printobserver_panels::ReferenceLandmark {
134                name: "frame-screws-right-front".to_owned(),
135                offset_px: [59.1, 0.7],
136                score: 0.83,
137            },
138            printobserver_panels::ReferenceLandmark {
139                name: "frame-label-right".to_owned(),
140                offset_px: [79.6, 0.2],
141                score: 0.84,
142            },
143        ],
144    )
145}
146
147impl Sample for FirstLayerAlignmentJudgedPayload {
148    fn sample_full() -> Self {
149        Self {
150            verdict: AlignmentJudgedVerdict::Passed,
151            offset_px: Some(14.6),
152            correction_px: Some([14.0, 4.0]),
153            match_strength: Some(57.6),
154            reason: "clearly_found".to_owned(),
155            frame_sha256: SAMPLE_SHA256.to_owned(),
156            view_correction: Some(sample_correction()),
157        }
158    }
159
160    fn sample_minimal() -> Self {
161        Self {
162            verdict: AlignmentJudgedVerdict::Inconclusive,
163            offset_px: None,
164            correction_px: None,
165            match_strength: None,
166            reason: "not_clearly_found".to_owned(),
167            frame_sha256: SAMPLE_SHA256.to_owned(),
168            view_correction: None,
169        }
170    }
171
172    fn sample_alternates() -> Vec<Self> {
173        vec![Self {
174            verdict: AlignmentJudgedVerdict::Failed,
175            reason: "the camera is not the stream the calibration was fitted on".to_owned(),
176            ..Self::sample_minimal()
177        }]
178    }
179}
180
181impl Sample for CameraViewJudgedPayload {
182    fn sample_full() -> Self {
183        Self {
184            frame_sha256: SAMPLE_SHA256.to_owned(),
185            outcome: ViewOutcome::ViewRefitRejected,
186            view_correction: Some(sample_correction()),
187            reason: Some(
188                "the camera has moved again since this print's first-layer correction was taken: frame-label-right is 10.2 px from where that correction's frame had it"
189                    .to_owned(),
190            ),
191        }
192    }
193
194    fn sample_minimal() -> Self {
195        Self {
196            frame_sha256: SAMPLE_SHA256.to_owned(),
197            outcome: ViewOutcome::Unshifted,
198            view_correction: None,
199            reason: None,
200        }
201    }
202
203    fn sample_alternates() -> Vec<Self> {
204        [
205            ViewOutcome::Refit,
206            ViewOutcome::LandmarksNotFound,
207            ViewOutcome::FrameMismatch,
208        ]
209        .into_iter()
210        .map(|outcome| Self {
211            outcome,
212            ..Self::sample_minimal()
213        })
214        .collect()
215    }
216}
217
218impl Sample for CameraViewRefitPayload {
219    fn sample_full() -> Self {
220        Self {
221            frame_sha256: SAMPLE_SHA256.to_owned(),
222            view_correction: sample_correction(),
223        }
224    }
225}
226
227impl Sample for CheckImageChangedPayload {
228    fn sample_full() -> Self {
229        Self {
230            kind: ImageKind::WholeFrame,
231            fallback: Some(ImageFallback::sample_full()),
232            frame_sha256: SAMPLE_SHA256.to_owned(),
233        }
234    }
235
236    fn sample_minimal() -> Self {
237        Self {
238            kind: ImageKind::ThreePanel,
239            fallback: None,
240            frame_sha256: SAMPLE_SHA256.to_owned(),
241        }
242    }
243}