BREP_instructions 0.4.1

Manufacturing instructions for next.BREP.io (Rust B-rep CAD) assemblies: the instructions document model, the step-order wizard, step editing, re-targeting to a new assembly revision, page layout and the self-contained HTML player.
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
//! The interactive HTML player: a trusted template and its data format, kept
//! apart.
//!
//! * [`TEMPLATE`] (`player/player.html`) is code: one script block, one style
//!   block, no inline event handlers, and it never puts data into the page as
//!   markup (text goes in as text, pictures only as `data:image/…` or same-site
//!   `/api/tools/…` URLs). A server serves it under its own content security
//!   policy with a per-response nonce ([`served_html`]).
//! * [`PlayerData`] is data: the steps flat in reading order, each with its
//!   parent's id and depth, the meshes deduplicated per part, a node per
//!   occurrence with its matrix, and per step what is placed, what is being
//!   added, the insertion offsets and the camera. A server stores it per
//!   instructions revision and the template fetches it. Data written before
//!   the step tree (schema 1: chapters of steps) reads as a flat list.
//! * Beside the steps the data carries the assembly's occurrence tree
//!   ([`PlayerComponent`], one row per placed component with its thumbnail
//!   and details), which the page's Model tab shows as an expandable tree
//!   over the model with every part's visibility its own, and its Parts tab
//!   as one card per part with the steps that use it; and per step the PMI
//!   its view shows ([`PlayerPmi`]: dimensions, notes, balloons, leaders,
//!   laid out by the same `pmi_present` the viewport, the sheets and the 3D
//!   PDF use), drawn over the parts.
//! * The offline copy is the same template with the data inlined
//!   ([`offline_html`]): one self-contained `.html`.

use crate::edit;
use crate::model::{Instructions, Style, Target, ToolUse};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};

/// The player template.
pub const TEMPLATE: &str = include_str!("../player/player.html");

/// The player data's schema version.
pub const DATA_SCHEMA: u32 = 2;

/// The two player modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
    /// Touch-first, with a step checklist and per-step sign-off.
    #[default]
    Shop,
    /// Pictures first, minimal text.
    Customer,
}

impl Mode {
    pub fn parse(text: &str) -> Option<Self> {
        match text {
            "shop" => Some(Self::Shop),
            "customer" => Some(Self::Customer),
            _ => None,
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Shop => "shop",
            Self::Customer => "customer",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerData {
    pub schema: u32,
    pub title: String,
    #[serde(default)]
    pub subtitle: String,
    pub target: Target,
    /// Every step in reading order (a step's children before it).
    pub steps: Vec<PlayerStep>,
    /// Distinct tools, in first-use order.
    #[serde(default)]
    pub tools: Vec<ToolUse>,
    /// A picture per tool of the library (by `toolId`): the server's own
    /// `/api/tools/<id>/picture` as built, which the page the PLM serves
    /// loads same-site; the offline page carries only `data:image` pictures
    /// ([`PlayerData::inline_tool_pictures`]), the rest dropped.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub tool_pictures: BTreeMap<String, String>,
    pub meshes: Vec<PlayerMesh>,
    pub nodes: Vec<PlayerNode>,
    /// The assembly's occurrence tree, depth first (a component before its
    /// children): the Model tab's tree and the Parts tab's cards. Absent from
    /// data made before the tree was carried, and from a motion study's.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub assembly: Vec<PlayerComponent>,
    /// A motion study's baked frames: with no chapters the page is the study's
    /// player (a timeline over the frames); a step may carry one too (its
    /// motion). Absent from instructions made before motion existed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub motion: Option<Motion>,
    /// How the page draws the steps: the document's style. Data made before
    /// styles existed reads as Shaded, as it was drawn; a motion study is
    /// Shaded.
    #[serde(default = "Style::saved_before_styles")]
    pub style: Style,
    /// What an offline page keys a build's record by in the browser's
    /// storage ([`record_key`]): the instructions' content, so two documents,
    /// or two revisions of one, never share values. Absent from data made
    /// before the record was kept, and from a motion study's.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub record_key: String,
}

/// The identity an offline page keeps a build's record under: a digest of
/// everything the author wrote (the target revision, the steps, their
/// fields), the same on every machine and in every build. Any edit, and any
/// new revision of the assembly, is a new key.
pub fn record_key(block: &Instructions) -> String {
    // FNV-1a, 64 bit, over the block's JSON in the model's own field order: stable by construction (a
    // hasher from the standard library may change between releases).
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for byte in serde_json::to_string(block).unwrap_or_default().bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{hash:016x}")
}

/// A motion study baked for the player: every frame's poses, in the order
/// they play. Keyed by OCCURRENCE id (a node's `occurrence` is its solid's
/// scene name, `ACOMP2:Part`, under the occurrence), so the same JSON is also
/// what a document keeps as a study's published `motion`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Motion {
    pub fps: u32,
    /// Seconds.
    pub duration: f64,
    pub frames: Vec<MotionFrame>,
}

/// One frame: the occurrences whose pose differs from the document, each as
/// the delta over its document pose (column-major 4×4, applied over the
/// node's own matrix: `model = pose × node.matrix`); the occurrences a
/// visibility clip hides; opacities (0..1) a transparency clip sets; the
/// camera when a camera clip drives it (absolute: eye, target, up, and the
/// projection); whether parts interfere, and which.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct MotionFrame {
    pub time: f64,
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
    pub poses: std::collections::BTreeMap<String, [f64; 16]>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hidden: Vec<String>,
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
    pub opacity: std::collections::BTreeMap<String, f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub camera: Option<Camera>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub clashing: Vec<String>,
}

impl MotionFrame {
    /// Whether parts interfere at this frame.
    pub fn clash(&self) -> bool {
        !self.clashing.is_empty()
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerStep {
    pub id: String,
    /// 1-based, in reading order.
    pub number: usize,
    /// The parent step's id; none at the top level.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    /// 0 at the top level.
    #[serde(default)]
    pub depth: usize,
    pub title: String,
    #[serde(default)]
    pub notes: String,
    pub adds: Vec<PlayerAdd>,
    #[serde(default)]
    pub tools: Vec<ToolUse>,
    /// `12 N·m`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub torque: Option<String>,
    #[serde(default)]
    pub callouts: Vec<PlayerCallout>,
    pub signoff: bool,
    /// What the person doing the work records at the step (shop mode).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<crate::fields::StepField>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub camera: Option<Camera>,
    /// Node indices drawn ghosted.
    pub placed: Vec<usize>,
    /// Node indices highlighted.
    pub adding: Vec<usize>,
    /// Where an adding node starts its insertion motion.
    #[serde(default)]
    pub insertion: Vec<Insertion>,
    /// The step view's explode poses: a matrix per posed node, applied after
    /// the node's own matrix (world space), as the step's pictures pose it.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub poses: Vec<Pose>,
    /// The PMI the step's view shows, drawn over the parts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pmi: Option<PlayerPmi>,
    /// A rendered picture of the step (PNG data URL), for customer mode and
    /// for viewers without WebGL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    /// A motion study's published motion attached to the step: played in
    /// place of the insertion animation, keyed by the scene's occurrences
    /// (the study's keys under the wrapper).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub motion: Option<Motion>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerAdd {
    /// The PLM part id, or the name ([`crate::model::Add::part`]).
    pub part: String,
    /// The display name (what the page shows; `part` when absent).
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub find_number: String,
    pub quantity: usize,
    /// The occurrences, so shop mode can ask for one serial per unit.
    pub occurrences: Vec<String>,
    #[serde(default)]
    pub serial_tracked: bool,
    #[serde(default)]
    pub subassembly: bool,
    /// The part's thumbnail (PNG data URL).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerCallout {
    pub kind: crate::model::CalloutKind,
    pub text: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Camera {
    pub eye: [f64; 3],
    pub target: [f64; 3],
    pub up: [f64; 3],
    /// Perspective field of view in degrees; `None` = orthographic.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fov_deg: Option<f64>,
    /// Orthographic half height, in model units.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub half_height: Option<f64>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Insertion {
    pub node: usize,
    pub offset: [f64; 3],
    /// Which of the step's arrows moves the node: the nodes of one arrow (a
    /// part's solids, a sub-assembly's parts) share one drawn arrow. Data
    /// made before it reads 0.
    #[serde(default)]
    pub arrow: usize,
}

/// A posed node of a step: its view's explode pose as a column-major 4×4.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pose {
    pub node: usize,
    pub matrix: [f64; 16],
}

/// A step view's PMI as the page draws it: every enabled, resolved
/// annotation of the view (not its explode or hide rows) laid out in world
/// space, sized as the viewport sized it when the view was saved. Text is
/// stroked with the PMI font, so the page needs no font of its own. Arrays
/// are little-endian base64 like a mesh's.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerPmi {
    /// f32 xyz pairs: dimension, extension and leader lines, frames and the
    /// stroked text, as line segments.
    pub lines: String,
    /// f32 xyz triples: the filled arrowheads, as triangles.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub arrows: String,
    /// How many annotations are drawn.
    pub annotations: usize,
    /// Each drawn annotation's text, in the view's order (for a reader
    /// without WebGL and for automation).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub texts: Vec<String>,
}

impl PlayerPmi {
    /// The drawing of `annotations` annotations from their polylines (each
    /// at least two points; every polyline becomes its consecutive segments)
    /// and arrowhead triangles. `None` when there is nothing to draw.
    pub fn new(polylines: &[Vec<[f64; 3]>], arrows: &[[[f64; 3]; 3]], annotations: usize, texts: Vec<String>) -> Option<Self> {
        let mut segments: Vec<f32> = Vec::new();
        for line in polylines {
            for pair in line.windows(2) {
                segments.extend(pair[0].iter().chain(&pair[1]).map(|&v| v as f32));
            }
        }
        let triangles: Vec<f32> = arrows.iter().flatten().flatten().map(|&v| v as f32).collect();
        if segments.is_empty() && triangles.is_empty() {
            return None;
        }
        Some(Self {
            lines: encode_f32(&segments),
            arrows: if triangles.is_empty() { String::new() } else { encode_f32(&triangles) },
            annotations,
            texts,
        })
    }
}

/// One placed component of the assembly: a row of the Model tab's tree and,
/// with the other rows of its part, a card of the Parts tab.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerComponent {
    /// The occurrence path (`ACOMP1:ACOMP3`): a node belongs to the deepest
    /// component whose path its occurrence starts with (then `:`).
    pub path: String,
    /// The parent component's path; none at the top of the assembly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    /// 0 at the top of the assembly.
    #[serde(default)]
    pub depth: usize,
    /// The part's name.
    pub name: String,
    /// What a step's add names the part by: its PLM id, or its name.
    pub part: String,
    /// The PLM part id when the part was placed from a PLM revision.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub part_id: Option<String>,
    #[serde(default)]
    pub find_number: String,
    #[serde(default)]
    pub subassembly: bool,
    #[serde(default)]
    pub fastener: bool,
    #[serde(default)]
    pub serial_tracked: bool,
    #[serde(default)]
    pub grounded: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub torque: Option<String>,
    /// The part's attributes as `[name, value]` text pairs.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attributes: Vec<(String, String)>,
    /// The part's thumbnail (PNG data URL); every row of one part carries
    /// the same.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail: Option<String>,
}

impl PlayerComponent {
    /// One row per component of `root` and its sub-assemblies, depth first.
    /// Thumbnails are filled by [`PlayerData::build`] from the scene's.
    pub fn rows(root: &crate::wizard::Level) -> Vec<Self> {
        let mut out = Vec::new();
        fn walk(level: &crate::wizard::Level, parent: Option<&str>, depth: usize, out: &mut Vec<PlayerComponent>) {
            for c in &level.components {
                out.push(PlayerComponent {
                    path: c.path.clone(),
                    parent: parent.map(str::to_string),
                    depth,
                    name: c.part.clone(),
                    part: c.part_key().to_string(),
                    part_id: c.part_id.clone(),
                    find_number: c.find_number.clone(),
                    subassembly: c.children.is_some(),
                    fastener: c.fastener,
                    serial_tracked: c.serial_tracked,
                    grounded: c.grounded,
                    torque: c.torque.clone(),
                    attributes: c.attributes.clone(),
                    thumbnail: None,
                });
                if let Some(children) = &c.children {
                    walk(children, Some(&c.path), depth + 1, out);
                }
            }
        }
        walk(root, None, 0, &mut out);
        out
    }
}

/// One triangle mesh, shared by every node of the same part. Arrays are
/// little-endian and base64-encoded.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerMesh {
    pub id: String,
    /// f32 xyz.
    pub positions: String,
    /// f32 xyz, one per position.
    pub normals: String,
    /// u32 triangle indices.
    pub indices: String,
    /// f32 xyz pairs: the solid's display edges as line segments, drawn over
    /// the faces as the viewport draws them. Absent from data written before
    /// edges were carried.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub edges: String,
}

impl PlayerMesh {
    pub fn new(id: &str, positions: &[f32], normals: &[f32], indices: &[u32]) -> Self {
        Self { id: id.into(), positions: encode_f32(positions), normals: encode_f32(normals), indices: encode_u32(indices), edges: String::new() }
    }

    /// The same mesh with its edges, given as polylines (each at least two
    /// points): every polyline becomes its consecutive segments.
    /// Take the mesh back through `placement` (a rigid column-major 4×4): the
    /// vertices through its inverse, the normals and edges through the
    /// rotation's transpose, so the mesh reads in the part's own frame.
    fn localise(&mut self, placement: &[f64; 16]) {
        let m = placement;
        let r = |p: [f32; 3]| -> [f32; 3] {
            // Rᵀ · p: rows of R are the matrix's columns' first three entries.
            let p = [f64::from(p[0]), f64::from(p[1]), f64::from(p[2])];
            [
                (m[0] * p[0] + m[1] * p[1] + m[2] * p[2]) as f32,
                (m[4] * p[0] + m[5] * p[1] + m[6] * p[2]) as f32,
                (m[8] * p[0] + m[9] * p[1] + m[10] * p[2]) as f32,
            ]
        };
        let back = |p: [f32; 3]| -> [f32; 3] { r([p[0] - m[12] as f32, p[1] - m[13] as f32, p[2] - m[14] as f32]) };
        let map = |text: &str, f: &dyn Fn([f32; 3]) -> [f32; 3]| -> String {
            let values = decode_f32(text);
            let mapped: Vec<f32> = values.chunks_exact(3).flat_map(|p| f([p[0], p[1], p[2]])).collect();
            encode_f32(&mapped)
        };
        self.positions = map(&self.positions, &back);
        self.normals = map(&self.normals, &r);
        if !self.edges.is_empty() {
            self.edges = map(&self.edges, &back);
        }
    }

    pub fn with_edges(mut self, polylines: &[Vec<[f32; 3]>]) -> Self {
        let mut segments: Vec<f32> = Vec::new();
        for line in polylines {
            for pair in line.windows(2) {
                segments.extend_from_slice(&pair[0]);
                segments.extend_from_slice(&pair[1]);
            }
        }
        self.edges = if segments.is_empty() { String::new() } else { encode_f32(&segments) };
        self
    }
}

/// One drawn solid of one occurrence.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerNode {
    /// The occurrence path the solid belongs to.
    pub occurrence: String,
    /// Index into `meshes`.
    pub mesh: usize,
    /// Column-major 4 × 4 placement.
    pub matrix: [f64; 16],
    /// sRGB colour, 0..1.
    pub color: [f32; 3],
}

/// What the caller supplies beside the block: the meshes and nodes of the
/// assembly as placed, each step's camera and picture, and part thumbnails.
#[derive(Debug, Clone, Default)]
pub struct Scene {
    pub meshes: Vec<PlayerMesh>,
    pub nodes: Vec<PlayerNode>,
    pub cameras: HashMap<String, Camera>,
    pub step_images: HashMap<String, String>,
    pub thumbnails: HashMap<String, String>,
    /// Each step's explode poses, by step id.
    pub poses: HashMap<String, Vec<Pose>>,
    /// Each step's view PMI, by step id.
    pub pmi: HashMap<String, PlayerPmi>,
    /// The arrows a step's parts come home along when they are not the
    /// step's own (a Line art step without arrows gets one per part from the
    /// engine), by step id; a step not listed uses its own `arrows`.
    pub arrows: HashMap<String, Vec<crate::model::Arrow>>,
    /// The assembly's occurrence tree ([`PlayerComponent::rows`]); each
    /// row's thumbnail is taken from `thumbnails` by its part.
    pub assembly: Vec<PlayerComponent>,
    /// The wrapper occurrence the instructions place the assembly under
    /// (`ACOMP1`): a step's motion, keyed by the assembly's own occurrences,
    /// is re-keyed under it. Empty when the nodes are the assembly's own.
    pub wrapper: String,
}

/// The tolerance of a rigid fit, as a share of the mesh's extent.
const RIGID_FIT_TOLERANCE: f64 = 1e-4;

impl Scene {
    /// One mesh per part. `keys` is one key per node (its part and member)
    /// and `placements` one optional placement per node (column-major, the
    /// engine's pose of the occurrence in the world). Among the nodes of one
    /// key, the first is the reference:
    ///
    /// * with placements, its mesh is made part-local (its vertices taken back
    ///   through its placement, normals through the rotation) and every placed
    ///   node of the key shares it, each carrying its own placement as its
    ///   matrix, whatever that copy's own tessellation was;
    /// * without, a node whose mesh is the same tessellation as the reference's
    ///   moved rigidly shares it and carries the rigid move, decided by the
    ///   vertices themselves (the same index order and count, every vertex
    ///   within `RIGID_FIT_TOLERANCE` of the fit), so a mirrored or
    ///   re-tessellated copy keeps its own.
    ///
    /// Meshes left unreferenced are dropped and the rest renumbered.
    pub fn share_meshes(&mut self, keys: &[String], placements: &[Option<[f64; 16]>]) {
        let decoded: Vec<Vec<[f64; 3]>> = self.meshes.iter().map(|mesh| decode_f32(&mesh.positions).chunks(3).map(|p| [f64::from(p[0]), f64::from(p[1]), f64::from(p[2])]).collect()).collect();
        let placement = |i: usize| placements.get(i).copied().flatten();
        let mut first_by_key: HashMap<&str, usize> = HashMap::new();
        let mut shared: Vec<Option<(usize, [f64; 16])>> = vec![None; self.nodes.len()];
        let mut localised: Vec<Option<[f64; 16]>> = vec![None; self.nodes.len()];
        for (i, node) in self.nodes.iter().enumerate() {
            let Some(key) = keys.get(i) else { continue };
            match first_by_key.get(key.as_str()) {
                None => {
                    first_by_key.insert(key, i);
                    if let Some(at) = placement(i) {
                        localised[i] = Some(at);
                    }
                }
                Some(&reference) => {
                    let rm = self.nodes[reference].mesh;
                    match (placement(reference), placement(i)) {
                        (Some(_), Some(at)) => shared[i] = Some((rm, at)),
                        _ => {
                            let cm = node.mesh;
                            if rm != cm && self.meshes[rm].indices == self.meshes[cm].indices {
                                if let Some(matrix) = rigid_fit(&decoded[rm], &decoded[cm]) {
                                    shared[i] = Some((rm, matrix));
                                }
                            }
                        }
                    }
                }
            }
        }
        for (i, at) in localised.iter().enumerate() {
            if let Some(at) = at {
                let mesh = self.nodes[i].mesh;
                self.meshes[mesh].localise(at);
                self.nodes[i].matrix = *at;
            }
        }
        for (i, found) in shared.iter().enumerate() {
            if let Some((mesh, matrix)) = found {
                self.nodes[i].mesh = *mesh;
                self.nodes[i].matrix = *matrix;
            }
        }
        let used: Vec<bool> = (0..self.meshes.len()).map(|m| self.nodes.iter().any(|n| n.mesh == m)).collect();
        let mut renumbered = vec![0usize; self.meshes.len()];
        let mut next = 0;
        for (m, keep) in used.iter().enumerate() {
            if *keep {
                renumbered[m] = next;
                next += 1;
            }
        }
        let meshes = std::mem::take(&mut self.meshes);
        self.meshes = meshes.into_iter().enumerate().filter(|(m, _)| used[*m]).map(|(_, mesh)| mesh).collect();
        for node in &mut self.nodes {
            node.mesh = renumbered[node.mesh];
        }
    }
}

/// The rigid move (a proper rotation and a translation, column-major 4×4)
/// taking every point of `a` onto the point of `b` with the same index, or
/// `None` when there is none within tolerance (a different shape, a mirror,
/// or too few points to fix a frame).
fn rigid_fit(a: &[[f64; 3]], b: &[[f64; 3]]) -> Option<[f64; 16]> {
    if a.len() != b.len() || a.len() < 3 {
        return None;
    }
    let sub = |p: [f64; 3], q: [f64; 3]| [p[0] - q[0], p[1] - q[1], p[2] - q[2]];
    let dot = |p: [f64; 3], q: [f64; 3]| p[0] * q[0] + p[1] * q[1] + p[2] * q[2];
    let cross = |p: [f64; 3], q: [f64; 3]| [p[1] * q[2] - p[2] * q[1], p[2] * q[0] - p[0] * q[2], p[0] * q[1] - p[1] * q[0]];
    let norm = |p: [f64; 3]| dot(p, p).sqrt();
    let scale = |p: [f64; 3], k: f64| [p[0] * k, p[1] * k, p[2] * k];
    // Three points that fix a frame: the farthest from the first, then the
    // farthest from that line.
    let i0 = 0;
    let i1 = (0..a.len()).max_by(|&i, &j| norm(sub(a[i], a[i0])).partial_cmp(&norm(sub(a[j], a[i0]))).unwrap_or(std::cmp::Ordering::Equal))?;
    let axis = sub(a[i1], a[i0]);
    let extent = norm(axis);
    if extent <= 0.0 {
        return None;
    }
    let off_line = |i: usize| norm(cross(axis, sub(a[i], a[i0]))) / extent;
    let i2 = (0..a.len()).max_by(|&i, &j| off_line(i).partial_cmp(&off_line(j)).unwrap_or(std::cmp::Ordering::Equal))?;
    let tolerance = RIGID_FIT_TOLERANCE * extent.max(1e-9);
    if off_line(i2) <= tolerance {
        return None;
    }
    let frame = |p: &[[f64; 3]]| -> Option<[[f64; 3]; 3]> {
        let u = sub(p[i1], p[i0]);
        let u = scale(u, 1.0 / norm(u));
        let v0 = sub(p[i2], p[i0]);
        let v = sub(v0, scale(u, dot(v0, u)));
        let lv = norm(v);
        if lv <= 0.0 {
            return None;
        }
        let v = scale(v, 1.0 / lv);
        Some([u, v, cross(u, v)])
    };
    let (fa, fb) = (frame(a)?, frame(b)?);
    // R = Fb · Faᵀ: column k of R = Σ_j fb[j] * fa[j][k].
    let mut r = [[0.0f64; 3]; 3];
    for col in 0..3 {
        for row in 0..3 {
            r[col][row] = (0..3).map(|j| fb[j][row] * fa[j][col]).sum();
        }
    }
    let apply = |p: [f64; 3]| [
        r[0][0] * p[0] + r[1][0] * p[1] + r[2][0] * p[2],
        r[0][1] * p[0] + r[1][1] * p[1] + r[2][1] * p[2],
        r[0][2] * p[0] + r[1][2] * p[1] + r[2][2] * p[2],
    ];
    let t = sub(b[i0], apply(a[i0]));
    for (p, q) in a.iter().zip(b) {
        let moved = apply(*p);
        if norm(sub([moved[0] + t[0], moved[1] + t[1], moved[2] + t[2]], *q)) > tolerance {
            return None;
        }
    }
    Some([
        r[0][0], r[0][1], r[0][2], 0.0,
        r[1][0], r[1][1], r[1][2], 0.0,
        r[2][0], r[2][1], r[2][2], 0.0,
        t[0], t[1], t[2], 1.0,
    ])
}

impl Motion {
    /// The same motion with every occurrence key put under `wrapper`
    /// (`ACOMP2` → `ACOMP1:ACOMP2`); unchanged when `wrapper` is empty.
    pub fn under(&self, wrapper: &str) -> Motion {
        if wrapper.is_empty() {
            return self.clone();
        }
        let key = |k: &str| format!("{wrapper}:{k}");
        let frames = self
            .frames
            .iter()
            .map(|frame| MotionFrame {
                time: frame.time,
                poses: frame.poses.iter().map(|(k, v)| (key(k), *v)).collect(),
                hidden: frame.hidden.iter().map(|k| key(k)).collect(),
                opacity: frame.opacity.iter().map(|(k, v)| (key(k), *v)).collect(),
                camera: frame.camera.clone(),
                clashing: frame.clashing.iter().map(|k| key(k)).collect(),
            })
            .collect();
        Motion { fps: self.fps, duration: self.duration, frames }
    }
}

/// A step's attached motion (the study's published JSON, `{study,
/// assemblySignature}` beside the frames) re-keyed under the wrapper; a
/// value that is not a motion is refused like a node naming a missing mesh.
fn step_motion(step: &crate::model::Step, wrapper: &str) -> Result<Option<Motion>, String> {
    let Some(value) = &step.motion else { return Ok(None) };
    let motion: Motion = serde_json::from_value(value.clone()).map_err(|e| format!("step '{}': its motion is not a motion study's: {e}", step.id))?;
    if motion.frames.is_empty() {
        return Err(format!("step '{}': its motion has no frames", step.id));
    }
    Ok(Some(motion.under(wrapper)))
}

impl PlayerData {
    /// The player data of `block` over `scene`.
    pub fn build(block: &Instructions, scene: Scene) -> Result<Self, String> {
        for (i, node) in scene.nodes.iter().enumerate() {
            if node.mesh >= scene.meshes.len() {
                return Err(format!("node {i} names mesh {} of {}", node.mesh, scene.meshes.len()));
            }
        }
        let occurrences: Vec<String> = scene.nodes.iter().map(|n| n.occurrence.clone()).collect();
        let mut steps = Vec::new();
        for (number, (depth, step)) in block.steps().enumerate() {
            let number = number + 1;
            let parent = block.parent_id(&step.id).flatten().map(str::to_string);
            {
                let presentation = edit::presentation(block, &step.id, &occurrences)?;
                let index_of = |paths: &[String]| -> Vec<usize> {
                    let set: std::collections::HashSet<&str> = paths.iter().map(String::as_str).collect();
                    occurrences.iter().enumerate().filter(|(_, o)| set.contains(o.as_str())).map(|(i, _)| i).collect()
                };
                let adding = index_of(&presentation.adding);
                let arrows = scene.arrows.get(&step.id).unwrap_or(&step.arrows);
                let insertion = adding
                    .iter()
                    .filter_map(|&node| {
                        let path = &occurrences[node];
                        arrows
                            .iter()
                            .enumerate()
                            .find(|(_, a)| path == &a.occurrence || (path.starts_with(&a.occurrence) && path[a.occurrence.len()..].starts_with(':')))
                            .map(|(arrow, a)| Insertion { node, offset: a.start_offset(), arrow })
                    })
                    .collect();
                steps.push(PlayerStep {
                    id: step.id.clone(),
                    number,
                    parent,
                    depth,
                    title: step.title.clone(),
                    notes: step.notes.clone(),
                    adds: step
                        .adds
                        .iter()
                        .map(|a| PlayerAdd {
                            part: a.part.clone(),
                            name: a.display_name().to_string(),
                            find_number: a.find_number.clone(),
                            quantity: a.quantity(),
                            occurrences: a.occurrences.clone(),
                            serial_tracked: a.serial_tracked,
                            subassembly: a.subassembly,
                            thumbnail: scene.thumbnails.get(&a.part).cloned(),
                        })
                        .collect(),
                    tools: step.tools.clone(),
                    torque: step.torque.as_ref().map(|t| t.text()),
                    callouts: step.callouts.iter().map(|c| PlayerCallout { kind: c.kind, text: c.text.clone() }).collect(),
                    signoff: step.signoff.required,
                    fields: step.fields.clone(),
                    camera: scene.cameras.get(&step.id).cloned(),
                    placed: index_of(&presentation.placed),
                    adding,
                    insertion,
                    poses: scene.poses.get(&step.id).cloned().unwrap_or_default(),
                    pmi: scene.pmi.get(&step.id).cloned(),
                    image: scene.step_images.get(&step.id).cloned(),
                    motion: step_motion(step, &scene.wrapper)?,
                });
            }
        }
        let title = if block.front.title.is_empty() { block.target.name.clone() } else { block.front.title.clone() };
        let mut assembly = scene.assembly;
        for row in &mut assembly {
            if row.thumbnail.is_none() {
                row.thumbnail = scene.thumbnails.get(&row.part).cloned();
            }
        }
        Ok(Self {
            schema: DATA_SCHEMA,
            title,
            subtitle: block.front.subtitle.clone(),
            target: block.target.clone(),
            steps,
            tool_pictures: block
                .tools_list()
                .iter()
                .filter_map(|(tool, _)| tool.tool_id.as_deref())
                .filter(|id| !id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'))
                .map(|id| (id.to_string(), format!("/api/tools/{id}/picture")))
                .collect(),
            tools: block.tools_list().into_iter().map(|(t, _)| t).collect(),
            meshes: scene.meshes,
            nodes: scene.nodes,
            assembly,
            motion: None,
            style: block.template.style,
            record_key: record_key(block),
        })
    }

    /// Put the fetched tool pictures in: for each tool with a picture entry,
    /// `fetched[id]` as `Some((media type, bytes))` becomes a `data:image`
    /// URL when the type is one the page shows (PNG, JPEG, WebP) and the
    /// bytes are within [`TOOL_PICTURE_MAX_BYTES`]; any other entry (not
    /// fetched, no picture, another type, too large) is dropped, so an
    /// offline page never carries a picture it cannot load. Returns how many
    /// were put in.
    pub fn inline_tool_pictures(&mut self, fetched: &BTreeMap<String, Option<(String, Vec<u8>)>>) -> usize {
        let mut inlined = 0;
        let pictures = std::mem::take(&mut self.tool_pictures);
        for (id, url) in pictures {
            if url.starts_with("data:image/") {
                self.tool_pictures.insert(id, url);
                inlined += 1;
                continue;
            }
            let Some(Some((media, bytes))) = fetched.get(&id) else { continue };
            let media = media.split(';').next().unwrap_or("").trim().to_ascii_lowercase();
            if !matches!(media.as_str(), "image/png" | "image/jpeg" | "image/webp") || bytes.len() > TOOL_PICTURE_MAX_BYTES {
                continue;
            }
            self.tool_pictures.insert(id, format!("data:{media};base64,{}", base64(bytes)));
            inlined += 1;
        }
        inlined
    }

    /// A motion study's player: no steps, the scene's meshes and nodes, and
    /// the study's frames as `motion`. `title` is the study's name.
    pub fn motion_study(title: &str, subtitle: &str, target: Target, scene: Scene, motion: Motion) -> Result<Self, String> {
        for (i, node) in scene.nodes.iter().enumerate() {
            if node.mesh >= scene.meshes.len() {
                return Err(format!("node {i} names mesh {} of {}", node.mesh, scene.meshes.len()));
            }
        }
        if motion.frames.is_empty() {
            return Err("a motion study's player needs at least one frame".into());
        }
        Ok(Self {
            schema: DATA_SCHEMA,
            title: title.to_string(),
            subtitle: subtitle.to_string(),
            target,
            steps: Vec::new(),
            tools: Vec::new(),
            tool_pictures: BTreeMap::new(),
            meshes: scene.meshes,
            nodes: scene.nodes,
            assembly: Vec::new(),
            motion: Some(motion),
            style: Style::Shaded,
            record_key: String::new(),
        })
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("player data serializes")
    }

    /// Read the data, schema 1 (chapters of steps) included: its steps become
    /// one flat list, every step at the top.
    pub fn from_json(text: &str) -> Result<Self, String> {
        let mut value: serde_json::Value = serde_json::from_str(text).map_err(|e| format!("player data: {e}"))?;
        let schema = value.get("schema").and_then(serde_json::Value::as_u64).unwrap_or(0);
        if schema == 0 || schema > u64::from(DATA_SCHEMA) {
            return Err(format!("player data schema {schema} is not supported"));
        }
        if schema == 1 {
            if let Some(object) = value.as_object_mut() {
                let chapters = object.remove("chapters").and_then(|c| c.as_array().cloned()).unwrap_or_default();
                if !object.contains_key("steps") {
                    let steps: Vec<serde_json::Value> =
                        chapters.into_iter().flat_map(|c| c.get("steps").and_then(|s| s.as_array().cloned()).unwrap_or_default()).collect();
                    object.insert("steps".into(), serde_json::Value::Array(steps));
                }
                object.insert("schema".into(), serde_json::Value::from(DATA_SCHEMA));
            }
        }
        serde_json::from_value(value).map_err(|e| format!("player data: {e}"))
    }
}

/// How the template finds its data and what it may do.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
struct Config<'a> {
    mode: &'a str,
    /// Where to fetch the data; `None` = inlined.
    #[serde(skip_serializing_if = "Option::is_none")]
    data_url: Option<&'a str>,
    /// The job whose steps shop mode signs off; `None` = a local checklist.
    #[serde(skip_serializing_if = "Option::is_none")]
    job: Option<&'a str>,
}

/// The self-contained offline copy: the template with `data` inlined. Shop
/// mode keeps a local checklist (no server to sign off to).
pub fn offline_html(data: &PlayerData, mode: Mode) -> String {
    let config = Config { mode: mode.as_str(), data_url: None, job: None };
    // An offline page loads no `/api/tools/…` picture: only the inlined ones go.
    let json = if data.tool_pictures.values().all(|url| url.starts_with("data:image/")) {
        data.to_json()
    } else {
        let mut data = data.clone();
        data.tool_pictures.retain(|_, url| url.starts_with("data:image/"));
        data.to_json()
    };
    fill(None, &serde_json::to_string(&config).unwrap(), &json)
}

/// The largest tool picture an offline page inlines, in bytes.
pub const TOOL_PICTURE_MAX_BYTES: usize = 1024 * 1024;

/// The template as a server serves it: `nonce` goes on the script and style
/// blocks, the page fetches its data from `data_url` (same origin), and in shop
/// mode with a `job` its sign-offs go to that job.
pub fn served_html(nonce: &str, data_url: &str, mode: Mode, job: Option<&str>) -> Result<String, String> {
    if nonce.is_empty() || !nonce.bytes().all(|b| b.is_ascii_alphanumeric() || b"+/=_-".contains(&b)) {
        return Err("the nonce must be base64 or base64url text".into());
    }
    if !data_url.starts_with('/') || data_url.starts_with("//") {
        return Err("the data URL must be a same-origin path".into());
    }
    let job = job.filter(|_| mode == Mode::Shop);
    let config = Config { mode: mode.as_str(), data_url: Some(data_url), job };
    Ok(fill(Some(nonce), &serde_json::to_string(&config).unwrap(), "null"))
}

fn fill(nonce: Option<&str>, config: &str, data: &str) -> String {
    let nonce_attribute = nonce.map(|n| format!(" nonce=\"{n}\"")).unwrap_or_default();
    TEMPLATE
        .replace("{{NONCE}}", &nonce_attribute)
        .replace("{{CONFIG}}", &script_json(config))
        .replace("{{DATA}}", &script_json(data))
}

/// JSON made safe inside a `<script type="application/json">` block: no `<`,
/// `>` or `&` (so no `</script>` and no comment openers), and no U+2028/9.
pub fn script_json(json: &str) -> String {
    let mut out = String::with_capacity(json.len());
    for ch in json.chars() {
        match ch {
            '<' => out.push_str("\\u003c"),
            '>' => out.push_str("\\u003e"),
            '&' => out.push_str("\\u0026"),
            '\u{2028}' => out.push_str("\\u2028"),
            '\u{2029}' => out.push_str("\\u2029"),
            _ => out.push(ch),
        }
    }
    out
}

const BASE64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/// Standard base64 with padding.
pub fn base64(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    for chunk in bytes.chunks(3) {
        let b = [chunk[0], *chunk.get(1).unwrap_or(&0), *chunk.get(2).unwrap_or(&0)];
        let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
        out.push(BASE64[(n >> 18) as usize & 63] as char);
        out.push(BASE64[(n >> 12) as usize & 63] as char);
        out.push(if chunk.len() > 1 { BASE64[(n >> 6) as usize & 63] as char } else { '=' });
        out.push(if chunk.len() > 2 { BASE64[n as usize & 63] as char } else { '=' });
    }
    out
}

/// Standard base64 back to bytes: padding (at most two `=`) only at the end.
/// `None` for any other character, so a corrupt string is never read as the
/// prefix before its first bad character.
pub fn unbase64(text: &str) -> Option<Vec<u8>> {
    let value = |c: u8| BASE64.iter().position(|&b| b == c);
    let body = text.strip_suffix("==").or_else(|| text.strip_suffix('=')).unwrap_or(text);
    let mut out = Vec::with_capacity(text.len() / 4 * 3);
    let mut acc: u32 = 0;
    let mut bits = 0;
    for c in body.bytes() {
        let v = value(c)?;
        acc = (acc << 6) | v as u32;
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            out.push((acc >> bits) as u8);
            acc &= (1 << bits) - 1;
        }
    }
    Some(out)
}

/// The f32 values an [`encode_f32`] string carries; none for a string that is
/// not base64 (never a short read of its good prefix).
pub fn decode_f32(text: &str) -> Vec<f32> {
    unbase64(text).unwrap_or_default().chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect()
}

pub fn encode_f32(values: &[f32]) -> String {
    base64(&values.iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>())
}

pub fn encode_u32(values: &[u32]) -> String {
    base64(&values.iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>())
}

/// A PNG as a data URL, the only picture form the player shows from data.
pub fn png_data_url(png: &[u8]) -> String {
    format!("data:image/png;base64,{}", base64(png))
}