inochi2d-parser 0.2.2

Typed parser and intermediate representation for Inochi2D INP/INX files
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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
//! Export a parsed Inochi2D puppet to the INR container.

use std::collections::{BTreeMap, HashMap};

use crate::owned::{
    BindingValues, BlendMode, Interpolation, Mask, MaskMode, MergeMode, Mesh, Node, NodeDataType,
    Param, ParamName, PhysicsMapMode, PhysicsModelType, Puppet, Texture, TextureData,
    TextureFormat,
};

use super::*;

/// Serialize a doc + binary blob into the INR container.
pub fn write_container(doc: &InrDoc, bin: &[u8]) -> Result<Vec<u8>, InrError> {
    let mut json = serde_json::to_vec(doc)?;
    while json.len() % 4 != 0 {
        json.push(b' ');
    }
    let mut out = Vec::with_capacity(16 + json.len() + bin.len());
    out.extend_from_slice(&MAGIC);
    out.extend_from_slice(&VERSION.to_le_bytes());
    out.extend_from_slice(&(json.len() as u32).to_le_bytes());
    out.extend_from_slice(&(bin.len() as u32).to_le_bytes());
    out.extend_from_slice(&json);
    out.extend_from_slice(bin);
    Ok(out)
}

/// Convert a parsed puppet into INR bytes.
pub fn export_puppet(puppet: &Puppet) -> Result<Vec<u8>, InrError> {
    let mut bin = BinWriter::default();

    // Pre-order walk: node index map first (masks/bindings reference indices)
    let mut flat: Vec<(&Node, Option<u32>)> = Vec::new();
    walk(&puppet.nodes, None, &mut flat);
    let node_index: HashMap<u32, u32> = flat
        .iter()
        .enumerate()
        .map(|(i, (n, _))| (n.uuid, i as u32))
        .collect();

    // Param order: sorted by uuid for determinism
    let mut params_sorted: Vec<&Param> = puppet.params.values().collect();
    params_sorted.sort_by_key(|p| p.uuid);
    let param_index: HashMap<u32, i32> = params_sorted
        .iter()
        .enumerate()
        .map(|(i, p)| (p.uuid, i as i32))
        .collect();

    let textures = puppet
        .textures
        .iter()
        .map(|tex| {
            let (width, height, mut rgba) = decode_texture(tex)?;
            // Inochi2D sources are premultiplied in sRGB space. INR stores
            // straight alpha so consumers can sample through hardware sRGB
            // views (decode-then-premultiply is only correct with straight
            // data). Color is dilated into transparent texels so bilinear
            // filtering doesn't bleed black at edges.
            unpremultiply(&mut rgba);
            dilate_edges(width as usize, height as usize, &mut rgba);
            Ok(TextureDesc {
                width,
                height,
                format: InrTextureFormat::Rgba8,
                color_space: InrColorSpace::Srgb,
                premultiplied: false,
                view: bin.push(&rgba),
            })
        })
        .collect::<Result<Vec<_>, InrError>>()?;

    let mut nodes: Vec<InrNode> = flat
        .iter()
        .map(|(node, parent)| build_node(node, *parent, &node_index, &param_index, &mut bin))
        .collect();

    let compose_hints = bake_compose_hints(puppet);
    for n in &mut nodes {
        if let Some(c) = &mut n.composite {
            c.compose_hint = compose_hints.get(&n.uuid).copied();
        }
    }

    let params = params_sorted
        .iter()
        .map(|p| build_param(p, &node_index, &mut bin))
        .collect();

    let mut anims_sorted: Vec<_> = puppet.animations.values().collect();
    anims_sorted.sort_by(|a, b| a.name.cmp(&b.name));
    let animations = anims_sorted
        .iter()
        .map(|a| InrAnimation {
            name: clean_name(&a.name),
            timestep: a.timestep,
            additive: a.additive,
            length: a.length,
            lead_in: a.lead_in,
            lead_out: a.lead_out,
            weight: a.weight,
            lanes: a
                .lanes
                .iter()
                .map(|l| InrLane {
                    param: param_index.get(&l.param_uuid).copied().unwrap_or(-1),
                    target: l.target,
                    interpolation: interpolation_inr(l.interpolation),
                    merge_mode: merge_mode_inr(l.merge_mode),
                    keyframes: l
                        .keyframes
                        .iter()
                        .map(|k| [k.frame as f32, k.value, k.tension])
                        .collect(),
                })
                .collect(),
        })
        .collect();

    let doc = InrDoc {
        asset: Asset {
            generator: format!("inochi2d-parser {}", env!("CARGO_PKG_VERSION")),
            version: VERSION,
        },
        meta: Meta {
            name: puppet.meta.name.clone(),
            rigger: puppet.meta.rigger.clone(),
            artist: puppet.meta.artist.clone(),
            rights: puppet.meta.rights.clone(),
            copyright: puppet.meta.copyright.clone(),
            license_url: puppet.meta.license_url.clone(),
            contact: puppet.meta.contact.clone(),
            reference: puppet.meta.reference.clone(),
            source_version: Some(puppet.meta.version.clone()),
        },
        physics: Physics {
            pixels_per_meter: puppet.physics.pixels_per_meter,
            gravity: puppet.physics.gravity,
        },
        buffer_views: bin.views.clone(),
        textures,
        nodes,
        params,
        animations,
        mask_contours: bake_mask_contours(puppet),
    };

    write_container(&doc, &bin.data)
}

/// Export and write to a file.
pub fn export_to_file<P: AsRef<std::path::Path>>(
    puppet: &Puppet,
    path: P,
) -> Result<(), InrError> {
    std::fs::write(path, export_puppet(puppet)?)?;
    Ok(())
}

#[derive(Default)]
struct BinWriter {
    data: Vec<u8>,
    views: Vec<BufferView>,
}

impl BinWriter {
    fn push(&mut self, bytes: &[u8]) -> u32 {
        while !self.data.len().is_multiple_of(4) {
            self.data.push(0);
        }
        let id = self.views.len() as u32;
        self.views.push(BufferView {
            offset: self.data.len() as u32,
            length: bytes.len() as u32,
        });
        self.data.extend_from_slice(bytes);
        id
    }

    fn push_f32(&mut self, values: &[f32]) -> u32 {
        self.push(bytemuck::cast_slice(values))
    }

    fn push_u32(&mut self, values: &[u32]) -> u32 {
        self.push(bytemuck::cast_slice(values))
    }
}

fn walk<'a>(node: &'a Node, parent: Option<u32>, out: &mut Vec<(&'a Node, Option<u32>)>) {
    let idx = out.len() as u32;
    out.push((node, parent));
    for child in &node.children {
        walk(child, Some(idx), out);
    }
}

fn build_node(
    node: &Node,
    parent: Option<u32>,
    node_index: &HashMap<u32, u32>,
    param_index: &HashMap<u32, i32>,
    bin: &mut BinWriter,
) -> InrNode {
    use NodeDataType as T;

    let mut out = InrNode {
        name: clean_name(&node.name),
        uuid: node.uuid,
        parent,
        kind: InrNodeKind::Node,
        enabled: node.enabled,
        zsort: node.zsort,
        lock_to_root: node.lock_to_root,
        translation: node.transform.translation,
        rotation: node.transform.rotation,
        scale: node.transform.scale,
        mesh: None,
        part: None,
        composite: None,
        physics: None,
    };

    match &node.type_node {
        T::Part(d) => {
            out.kind = InrNodeKind::Part;
            out.mesh = d.mesh.as_ref().map(|m| build_mesh(m, bin));
            out.part = Some(InrPart {
                textures: d.textures.map(|t| if t == u32::MAX { -1 } else { t as i32 }),
                blend_mode: blend_mode_inr(d.blend_mode),
                tint: d.tint,
                screen_tint: d.screen_tint,
                opacity: d.opacity,
                emission_strength: d.emission_strength,
                mask_threshold: d.mask_threshold,
                masks: build_masks(&d.mask, node_index),
            });
        }
        T::Composite(d) => {
            out.kind = InrNodeKind::Composite;
            out.composite = Some(InrComposite {
                blend_mode: blend_mode_inr(d.blend_mode),
                tint: d.tint,
                screen_tint: d.screen_tint,
                opacity: d.opacity,
                mask_threshold: d.mask_threshold,
                masks: build_masks(&d.mask, node_index),
                // Filled by the compose-hint post-pass in `export_puppet`.
                compose_hint: None,
            });
        }
        T::Mask(d) => {
            out.kind = InrNodeKind::Mask;
            out.mesh = d.mesh.as_ref().map(|m| build_mesh(m, bin));
        }
        T::MeshGroup(d) => {
            out.kind = InrNodeKind::MeshGroup;
            out.mesh = d.mesh.as_ref().map(|m| build_mesh(m, bin));
        }
        T::SimplePhysics(d) => {
            out.kind = InrNodeKind::SimplePhysics;
            out.physics = Some(InrPhysics {
                param: param_index.get(&d.param).copied().unwrap_or(-1),
                model: match d.model_type {
                    PhysicsModelType::Pendulum => InrPhysicsModel::Pendulum,
                    PhysicsModelType::SpringPendulum => InrPhysicsModel::SpringPendulum,
                },
                map_mode: match d.map_mode {
                    PhysicsMapMode::AngleLength => InrMapMode::AngleLength,
                    PhysicsMapMode::XY => InrMapMode::Xy,
                    PhysicsMapMode::LengthAngle => InrMapMode::LengthAngle,
                    PhysicsMapMode::YX => InrMapMode::Yx,
                },
                gravity: d.gravity,
                length: d.length,
                frequency: d.frequency,
                angle_damping: d.angle_damping,
                length_damping: d.length_damping,
                output_scale: d.output_scale,
                local_only: d.local_only.unwrap_or(false),
            });
        }
        T::Camera(_) => out.kind = InrNodeKind::Camera,
        T::Generic => {}
    }

    out
}

fn build_mesh(mesh: &Mesh, bin: &mut BinWriter) -> InrMesh {
    InrMesh {
        vertex_count: (mesh.vertices.len() / 2) as u32,
        positions: bin.push_f32(&mesh.vertices),
        uvs: bin.push_f32(&mesh.uvs),
        indices: bin.push_u32(&mesh.indices),
        origin: mesh.origin,
    }
}

fn build_masks(masks: &[Mask], node_index: &HashMap<u32, u32>) -> Vec<InrMask> {
    masks
        .iter()
        .filter_map(|m| {
            Some(InrMask {
                node: *node_index.get(&m.source)?,
                mode: match m.mode {
                    MaskMode::Mask => InrMaskMode::Mask,
                    MaskMode::Dodge => InrMaskMode::Dodge,
                },
            })
        })
        .collect()
}

fn build_param(param: &Param, node_index: &HashMap<u32, u32>, bin: &mut BinWriter) -> InrParam {
    let bindings = param
        .bindings
        .iter()
        .filter_map(|b| {
            let node = *node_index.get(&b.node)?;
            let is_set: Vec<bool> = b.is_set.iter().flatten().copied().collect();
            let y_count = b.is_set.first().map(|r| r.len()).unwrap_or(1).max(1) as u32;

            let (kind, view, x_count) = match &b.values {
                BindingValues::Transform(t) => (
                    InrBindingKind::Scalar,
                    bin.push_f32(&t.data),
                    t.frames.max(1) as u32,
                ),
                BindingValues::Deform(d) => (
                    InrBindingKind::Deform,
                    bin.push_f32(bytemuck::cast_slice(&d.data)),
                    d.frames.max(1) as u32,
                ),
                // Unknown binding payloads are dropped on export
                BindingValues::Other(_) => return None,
            };

            Some(InrBinding {
                node,
                target: binding_target_inr(&b.param_name),
                interpolation: interpolation_inr(b.interpolate_mode),
                x_count,
                y_count,
                is_set,
                kind,
                view,
            })
        })
        .collect();

    InrParam {
        name: clean_name(&param.name),
        uuid: param.uuid,
        is_vec2: param.is_vec2,
        min: param.min,
        max: param.max,
        defaults: param.defaults,
        axis_points: param.axis_points.clone(),
        merge_mode: merge_mode_inr(param.merge_mode),
        bindings,
    }
}

fn decode_texture(tex: &Texture) -> Result<(u32, u32, Vec<u8>), InrError> {
    match (&tex.data, tex.format) {
        (TextureData::Rgba(data), _) => Ok((tex.width, tex.height, data.clone())),
        (TextureData::Encoded(_), TextureFormat::Bc7) => Err(InrError::Io(
            std::io::Error::other("BC7 textures are not supported by the INR exporter"),
        )),
        (TextureData::Encoded(data), format) => {
            let fmt = match format {
                TextureFormat::Png => image::ImageFormat::Png,
                TextureFormat::Tga => image::ImageFormat::Tga,
                TextureFormat::Bc7 => unreachable!(),
            };
            let img = image::load_from_memory_with_format(data, fmt)?.to_rgba8();
            let (w, h) = img.dimensions();
            Ok((w, h, img.into_raw()))
        }
    }
}

fn blend_mode_inr(mode: BlendMode) -> InrBlendMode {
    match mode {
        BlendMode::Normal => InrBlendMode::Normal,
        BlendMode::Multiply => InrBlendMode::Multiply,
        BlendMode::Screen => InrBlendMode::Screen,
        BlendMode::Overlay => InrBlendMode::Overlay,
        BlendMode::Darken => InrBlendMode::Darken,
        BlendMode::Lighten => InrBlendMode::Lighten,
        BlendMode::ColorDodge => InrBlendMode::ColorDodge,
        BlendMode::LinearDodge => InrBlendMode::LinearDodge,
        BlendMode::Add => InrBlendMode::Add,
        BlendMode::ColorBurn => InrBlendMode::ColorBurn,
        BlendMode::HardLight => InrBlendMode::HardLight,
        BlendMode::SoftLight => InrBlendMode::SoftLight,
        BlendMode::Subtract => InrBlendMode::Subtract,
        BlendMode::Difference => InrBlendMode::Difference,
        BlendMode::Exclusion => InrBlendMode::Exclusion,
        BlendMode::Inverse => InrBlendMode::Inverse,
        BlendMode::DestinationIn => InrBlendMode::DestinationIn,
        BlendMode::ClipToLower => InrBlendMode::ClipToLower,
        BlendMode::SliceFromLower => InrBlendMode::SliceFromLower,
    }
}

fn merge_mode_inr(mode: MergeMode) -> InrMergeMode {
    match mode {
        MergeMode::Additive => InrMergeMode::Additive,
        MergeMode::Multiplicative => InrMergeMode::Multiplicative,
        MergeMode::Override => InrMergeMode::Override,
        MergeMode::Forced => InrMergeMode::Forced,
    }
}

fn interpolation_inr(i: Interpolation) -> InrInterpolation {
    match i {
        Interpolation::Linear => InrInterpolation::Linear,
        Interpolation::Stepped => InrInterpolation::Stepped,
        Interpolation::Nearest => InrInterpolation::Nearest,
        Interpolation::Cubic => InrInterpolation::Cubic,
    }
}

fn binding_target_inr(name: &ParamName) -> InrBindingTarget {
    match name {
        ParamName::TransformTX => InrBindingTarget::TranslateX,
        ParamName::TransformTY => InrBindingTarget::TranslateY,
        ParamName::TransformTZ => InrBindingTarget::TranslateZ,
        ParamName::TransformSX => InrBindingTarget::ScaleX,
        ParamName::TransformSY => InrBindingTarget::ScaleY,
        ParamName::TransformRX => InrBindingTarget::RotateX,
        ParamName::TransformRY => InrBindingTarget::RotateY,
        ParamName::TransformRZ => InrBindingTarget::RotateZ,
        ParamName::Deform => InrBindingTarget::Deform,
        ParamName::Opacity => InrBindingTarget::Opacity,
        ParamName::Other(s) => InrBindingTarget::Other(s.clone()),
    }
}

/// Some authoring tools pad names with trailing NULs (fixed-size buffers);
/// strip them so INR JSON stays clean.
fn clean_name(name: &str) -> String {
    name.trim_end_matches('\0').to_owned()
}

/// Bakes alpha silhouettes for every part used as a mask source. The runtime
/// uses these contours instead of the source mesh triangles when CPU-clipping
/// masked parts — the mesh is usually a loose quad and gives a coarse
/// silhouette (visible as overshot mask edges on small shapes like blush or
/// eye highlights). One threshold per source: the part's own `mask_threshold`
/// (defaults to 0.5 when unset).
fn bake_mask_contours(puppet: &Puppet) -> std::collections::BTreeMap<u32, Vec<Vec<[f32; 2]>>> {
    use std::collections::{BTreeMap, BTreeSet};

    let mut sources: BTreeSet<u32> = BTreeSet::new();
    for node in puppet.nodes.iter() {
        match &node.type_node {
            NodeDataType::Part(p) => {
                for m in &p.mask {
                    sources.insert(m.source);
                }
            }
            NodeDataType::Composite(c) => {
                for m in &c.mask {
                    sources.insert(m.source);
                }
            }
            _ => {}
        }
    }

    let mut out: BTreeMap<u32, Vec<Vec<[f32; 2]>>> = BTreeMap::new();
    for uuid in sources {
        let Some(node) = puppet.nodes.iter().find(|n| n.uuid == uuid) else {
            continue;
        };
        let Some(part) = node.type_node.as_part() else {
            continue;
        };
        let tex_idx = part.textures[0];
        let Some(tex) = puppet.textures.get(tex_idx as usize) else {
            continue;
        };
        let Ok((w, h, rgba)) = decode_texture(tex) else {
            continue;
        };
        let raw = (part.mask_threshold.clamp(0.0, 1.0) * 255.0) as u8;
        let threshold = if raw == 0 { 128 } else { raw };
        let contours = marching_squares_alpha(&rgba, w, h, threshold);
        // Drop sub-pixel noise (alias artifacts near alpha edges).
        let big: Vec<Vec<[f32; 2]>> = contours
            .into_iter()
            .filter(|c| signed_area(c).abs() >= 4.0)
            .collect();
        let outers = drop_holes(big);
        let uv: Vec<Vec<[f32; 2]>> = outers
            .into_iter()
            .map(|c| douglas_peucker(&c, 1.0))
            .filter(|c| c.len() >= 3)
            .map(|c| {
                c.into_iter()
                    .map(|p| [p[0] / w as f32, p[1] / h as f32])
                    .collect()
            })
            .collect();
        if !uv.is_empty() {
            out.insert(uuid, uv);
        }
    }
    out
}

/// Quantize a float endpoint to a stable integer key for chain lookup.
/// Cell-edge midpoints are deterministic floats but we still quantize to
/// guard against denormal mismatches between adjacent cells.
fn qkey(p: [f32; 2]) -> (i64, i64) {
    const Q: f32 = 1024.0;
    ((p[0] * Q).round() as i64, (p[1] * Q).round() as i64)
}

/// Marching squares on the alpha channel. Returns closed polygons in pixel
/// space (origin at image top-left, y down). Outer contours and hole
/// contours both come out; `drop_holes` filters the holes afterward.
fn marching_squares_alpha(rgba: &[u8], w: u32, h: u32, threshold: u8) -> Vec<Vec<[f32; 2]>> {
    if w < 2 || h < 2 {
        return Vec::new();
    }

    // Alpha is 0 outside the image bounds — a virtual transparent border.
    // Without it, shapes that touch the texture edge (common: art painted
    // flush to the UV island) leave an open isoline: marching squares never
    // emits the closing segments along that edge, and the chain-walk breaks
    // there, corrupting the contour. Iterating one extra cell ring around
    // the image (x,y from -1..=w/h) lets those edge-touching shapes close
    // naturally against the synthetic zero border.
    let alpha = |x: i64, y: i64| -> u8 {
        if x < 0 || y < 0 || x >= w as i64 || y >= h as i64 {
            0
        } else {
            rgba[((y as u32 * w + x as u32) * 4 + 3) as usize]
        }
    };
    let interp = |a: u8, b: u8| -> f32 {
        // Linear crossing of `threshold` between two corner alpha values.
        let (af, bf, tf) = (a as f32, b as f32, threshold as f32);
        if af == bf {
            0.5
        } else {
            ((tf - af) / (bf - af)).clamp(0.0, 1.0)
        }
    };

    // segments: list of (start, end) line segments.
    let mut segments: Vec<([f32; 2], [f32; 2])> = Vec::new();
    for y in -1..h as i64 {
        for x in -1..w as i64 {
            let (tl, tr) = (alpha(x, y), alpha(x + 1, y));
            let (bl, br) = (alpha(x, y + 1), alpha(x + 1, y + 1));
            let bit = |a: u8| if a >= threshold { 1u8 } else { 0u8 };
            let cfg = (bit(tl) << 3) | (bit(tr) << 2) | (bit(br) << 1) | bit(bl);
            if cfg == 0 || cfg == 15 {
                continue;
            }
            let fx = x as f32;
            let fy = y as f32;
            // Side midpoints (linear interp on the differing corners).
            let top = [fx + interp(tl, tr), fy];
            let right = [fx + 1.0, fy + interp(tr, br)];
            let bottom = [fx + interp(bl, br), fy + 1.0];
            let left = [fx, fy + interp(tl, bl)];
            // Edge orientation per config so the inside stays on the left
            // while walking — yields CCW outer / CW hole polygons.
            let add = |segs: &mut Vec<([f32; 2], [f32; 2])>, a, b| segs.push((a, b));
            match cfg {
                1 => add(&mut segments, bottom, left),
                2 => add(&mut segments, right, bottom),
                3 => add(&mut segments, right, left),
                4 => add(&mut segments, top, right),
                5 => {
                    // Saddle — resolve by the average corner.
                    let avg = (tl as u32 + tr as u32 + br as u32 + bl as u32) / 4;
                    if (avg as u8) >= threshold {
                        add(&mut segments, top, right);
                        add(&mut segments, bottom, left);
                    } else {
                        add(&mut segments, top, left);
                        add(&mut segments, bottom, right);
                    }
                }
                6 => add(&mut segments, top, bottom),
                7 => add(&mut segments, top, left),
                8 => add(&mut segments, left, top),
                9 => add(&mut segments, bottom, top),
                10 => {
                    let avg = (tl as u32 + tr as u32 + br as u32 + bl as u32) / 4;
                    if (avg as u8) >= threshold {
                        add(&mut segments, left, bottom);
                        add(&mut segments, right, top);
                    } else {
                        add(&mut segments, left, top);
                        add(&mut segments, right, bottom);
                    }
                }
                11 => add(&mut segments, right, top),
                12 => add(&mut segments, left, right),
                13 => add(&mut segments, bottom, right),
                14 => add(&mut segments, left, bottom),
                _ => {}
            }
        }
    }

    chain_segments(segments)
}

/// Chain oriented segments into closed loops by matching each segment's end
/// point to another segment's start point at the same location. Multiple
/// segments can start at the same point (the corner shared by 4 cells), so
/// the index is a `Vec` — pick the first unused match when walking.
fn chain_segments(segments: Vec<([f32; 2], [f32; 2])>) -> Vec<Vec<[f32; 2]>> {
    let mut by_start: BTreeMap<(i64, i64), Vec<usize>> = BTreeMap::new();
    for (i, seg) in segments.iter().enumerate() {
        by_start.entry(qkey(seg.0)).or_default().push(i);
    }
    let mut used = vec![false; segments.len()];
    let mut polygons: Vec<Vec<[f32; 2]>> = Vec::new();

    for start_idx in 0..segments.len() {
        if used[start_idx] {
            continue;
        }
        let mut poly: Vec<[f32; 2]> = Vec::new();
        let mut cur = start_idx;
        loop {
            if used[cur] {
                break;
            }
            used[cur] = true;
            poly.push(segments[cur].0);
            let next_key = qkey(segments[cur].1);
            let next = by_start
                .get(&next_key)
                .and_then(|cands| cands.iter().copied().find(|&n| !used[n]));
            #[cfg(test)]
            if let Some(cands) = by_start.get(&next_key) {
                let unused: Vec<usize> = cands.iter().copied().filter(|&n| !used[n]).collect();
                if unused.len() > 1 {
                    diag_hooks::note_ambiguous(segments[cur].1, unused.len());
                }
            }
            match next {
                Some(next) => cur = next,
                None => {
                    poly.push(segments[cur].1);
                    #[cfg(test)]
                    diag_hooks::note_break(segments[cur].1);
                    break;
                }
            }
        }
        if poly.len() >= 3 {
            polygons.push(poly);
        }
    }
    polygons
}

#[cfg(test)]
mod diag_hooks {
    //! Tracing sinks for `diag_tests::diag_back_hoodie` — flag chain breaks
    //! and ambiguous branch points during chaining. No-ops unless a break
    //! falls in the watched region; keeps normal test output quiet.
    use std::cell::RefCell;

    type Ambiguous = ([f32; 2], usize);

    thread_local! {
        static BREAKS: RefCell<Vec<[f32; 2]>> = const { RefCell::new(Vec::new()) };
        static AMBIGUOUS: RefCell<Vec<Ambiguous>> = const { RefCell::new(Vec::new()) };
    }

    pub(super) fn note_break(p: [f32; 2]) {
        BREAKS.with(|b| b.borrow_mut().push(p));
    }

    pub(super) fn note_ambiguous(p: [f32; 2], candidates: usize) {
        AMBIGUOUS.with(|a| a.borrow_mut().push((p, candidates)));
    }

    pub(super) fn drain() -> (Vec<[f32; 2]>, Vec<Ambiguous>) {
        (
            BREAKS.with(|b| std::mem::take(&mut *b.borrow_mut())),
            AMBIGUOUS.with(|a| std::mem::take(&mut *a.borrow_mut())),
        )
    }
}

/// Drops polygons whose centroid lies inside another, larger polygon —
/// `i_overlay`'s NonZero fill rule then sees the outers only and we get a
/// filled silhouette without holes.
fn drop_holes(polys: Vec<Vec<[f32; 2]>>) -> Vec<Vec<[f32; 2]>> {
    let areas: Vec<f32> = polys.iter().map(|p| signed_area(p).abs()).collect();
    let mut order: Vec<usize> = (0..polys.len()).collect();
    order.sort_by(|&a, &b| areas[b].partial_cmp(&areas[a]).unwrap_or(std::cmp::Ordering::Equal));

    let mut keep = vec![false; polys.len()];
    let mut outers: Vec<usize> = Vec::new();
    for idx in order {
        let centroid = centroid(&polys[idx]);
        let nested = outers
            .iter()
            .any(|&o| point_in_polygon(centroid, &polys[o]));
        if !nested {
            keep[idx] = true;
            outers.push(idx);
        }
    }
    polys
        .into_iter()
        .enumerate()
        .filter_map(|(i, p)| if keep[i] { Some(p) } else { None })
        .collect()
}

fn signed_area(p: &[[f32; 2]]) -> f32 {
    let mut s = 0.0;
    let n = p.len();
    for i in 0..n {
        let a = p[i];
        let b = p[(i + 1) % n];
        s += a[0] * b[1] - b[0] * a[1];
    }
    s * 0.5
}

fn centroid(p: &[[f32; 2]]) -> [f32; 2] {
    let (mut x, mut y) = (0.0f32, 0.0f32);
    for v in p {
        x += v[0];
        y += v[1];
    }
    let n = p.len().max(1) as f32;
    [x / n, y / n]
}

fn point_in_polygon(pt: [f32; 2], poly: &[[f32; 2]]) -> bool {
    let mut inside = false;
    let n = poly.len();
    let mut j = n - 1;
    for i in 0..n {
        let (xi, yi) = (poly[i][0], poly[i][1]);
        let (xj, yj) = (poly[j][0], poly[j][1]);
        let intersect = ((yi > pt[1]) != (yj > pt[1]))
            && (pt[0] < (xj - xi) * (pt[1] - yi) / (yj - yi + f32::EPSILON) + xi);
        if intersect {
            inside = !inside;
        }
        j = i;
    }
    inside
}

/// Douglas-Peucker simplification on a closed polygon.
fn douglas_peucker(pts: &[[f32; 2]], eps: f32) -> Vec<[f32; 2]> {
    if pts.len() < 4 {
        return pts.to_vec();
    }
    // Find the pair of farthest-apart points to anchor the split — guarantees
    // a stable starting baseline regardless of the input start index.
    let (mut a, mut b) = (0usize, 0usize);
    let mut best = -1.0f32;
    for i in 0..pts.len() {
        for j in i + 1..pts.len() {
            let dx = pts[j][0] - pts[i][0];
            let dy = pts[j][1] - pts[i][1];
            let d = dx * dx + dy * dy;
            if d > best {
                best = d;
                a = i;
                b = j;
            }
        }
    }
    let lo = a.min(b);
    let hi = a.max(b);
    let mut left = simplify_segment(&pts[lo..=hi], eps);
    let right_pts: Vec<[f32; 2]> = pts[hi..].iter().chain(pts[..=lo].iter()).copied().collect();
    let mut right = simplify_segment(&right_pts, eps);
    if !right.is_empty() {
        right.pop();
    }
    if !left.is_empty() {
        left.pop();
    }
    left.append(&mut right);
    let _ = right_pts;
    left
}

fn simplify_segment(pts: &[[f32; 2]], eps: f32) -> Vec<[f32; 2]> {
    if pts.len() < 3 {
        return pts.to_vec();
    }
    let (a, b) = (pts[0], pts[pts.len() - 1]);
    let mut max_d = 0.0f32;
    let mut max_i = 0usize;
    for (i, p) in pts.iter().enumerate().skip(1).take(pts.len() - 2) {
        let d = perp_distance(*p, a, b);
        if d > max_d {
            max_d = d;
            max_i = i;
        }
    }
    if max_d > eps {
        let mut left = simplify_segment(&pts[..=max_i], eps);
        let right = simplify_segment(&pts[max_i..], eps);
        left.pop();
        left.extend(right);
        left
    } else {
        vec![a, b]
    }
}

fn perp_distance(p: [f32; 2], a: [f32; 2], b: [f32; 2]) -> f32 {
    let dx = b[0] - a[0];
    let dy = b[1] - a[1];
    let denom = (dx * dx + dy * dy).sqrt();
    if denom < f32::EPSILON {
        let ex = p[0] - a[0];
        let ey = p[1] - a[1];
        return (ex * ex + ey * ey).sqrt();
    }
    ((dy * p[0] - dx * p[1] + b[0] * a[1] - b[1] * a[0]).abs()) / denom
}

// ---------------------------------------------------------------------------
// Compose hints — conservative overlap analysis for non-identity composites
// ---------------------------------------------------------------------------

/// 2D affine transform: `[a c tx; b d ty]` column-major pair layout.
#[derive(Clone, Copy)]
struct Affine {
    m: [f32; 4],
    t: [f32; 2],
}

impl Affine {
    fn from_trs(t: [f32; 2], rz: f32, s: [f32; 2]) -> Self {
        let (sin, cos) = rz.sin_cos();
        Self {
            m: [cos * s[0], sin * s[0], -sin * s[1], cos * s[1]],
            t,
        }
    }

    /// `self ∘ other` — apply `other` first, then `self`.
    fn then(&self, other: &Affine) -> Self {
        Self {
            m: [
                self.m[0] * other.m[0] + self.m[2] * other.m[1],
                self.m[1] * other.m[0] + self.m[3] * other.m[1],
                self.m[0] * other.m[2] + self.m[2] * other.m[3],
                self.m[1] * other.m[2] + self.m[3] * other.m[3],
            ],
            t: [
                self.m[0] * other.t[0] + self.m[2] * other.t[1] + self.t[0],
                self.m[1] * other.t[0] + self.m[3] * other.t[1] + self.t[1],
            ],
        }
    }

    fn apply(&self, p: [f32; 2]) -> [f32; 2] {
        [
            self.m[0] * p[0] + self.m[2] * p[1] + self.t[0],
            self.m[1] * p[0] + self.m[3] * p[1] + self.t[1],
        ]
    }
}

/// A renderable child of a composite: the part's mesh plus the node chain
/// from the composite's direct child down to the part (inclusive).
struct ChildGeom<'a> {
    chain: Vec<&'a Node>,
    mesh: &'a Mesh,
}

/// Pose = one param driven to a fractional grid coordinate, all other params
/// at rest. `None` is the rest pose.
type Pose<'a> = Option<(&'a Param, f32, f32)>;

/// Decide, for every non-identity composite, whether its child parts can ever
/// overlap each other. Children that never overlap make the group blend
/// distributable per child (`ChildrenDisjoint`), which lets a renderer skip
/// offscreen compositing entirely.
///
/// Sampled poses: rest, plus every authored grid point of every param binding
/// that moves nodes inside the composite, plus midpoints between adjacent
/// grid points (linear interpolation between samples can produce poses not
/// bounded by the endpoints, midpoints halve that blind spot). Anything the
/// analysis cannot model exactly — MeshGroup deforms, X/Y rotations, unknown
/// binding targets — resolves to `ChildrenOverlap`.
fn bake_compose_hints(puppet: &Puppet) -> HashMap<u32, InrComposeHint> {
    let mut out = HashMap::default();
    for node in puppet.nodes.iter() {
        if let NodeDataType::Composite(data) = &node.type_node {
            if composite_is_identity(data) {
                continue;
            }
            out.insert(node.uuid, analyze_composite(node, puppet));
        }
    }
    out
}

fn composite_is_identity(d: &crate::owned::CompositeData) -> bool {
    const EPS: f32 = 1e-6;
    matches!(d.blend_mode, BlendMode::Normal)
        && (d.opacity - 1.0).abs() < EPS
        && d.tint.iter().all(|c| (c - 1.0).abs() < EPS)
        && d.screen_tint.iter().all(|c| c.abs() < EPS)
}

fn analyze_composite(composite: &Node, puppet: &Puppet) -> InrComposeHint {
    use InrComposeHint::{ChildrenDisjoint, ChildrenOverlap};

    // Collect renderable children with their chain below the composite.
    let mut children: Vec<ChildGeom> = Vec::new();
    let mut subtree: Vec<u32> = Vec::new();
    let mut part_uuids: Vec<u32> = Vec::new();
    for top in &composite.children {
        let mut stack: Vec<(Vec<&Node>, &Node)> = vec![(Vec::new(), top)];
        while let Some((path, node)) = stack.pop() {
            subtree.push(node.uuid);
            let mut chain = path.clone();
            chain.push(node);
            if let NodeDataType::Part(part) = &node.type_node
                && let Some(mesh) = &part.mesh
                && !mesh.vertices.is_empty()
                && !mesh.indices.is_empty()
            {
                part_uuids.push(node.uuid);
                children.push(ChildGeom {
                    chain: chain.clone(),
                    mesh,
                });
            }
            for child in &node.children {
                stack.push((chain.clone(), child));
            }
        }
    }
    if children.len() < 2 {
        return ChildrenDisjoint;
    }

    // Params whose bindings target the subtree; unsupported bindings = doubt.
    let mut relevant: Vec<&Param> = Vec::new();
    for param in puppet.params.values() {
        let mut touches = false;
        for b in &param.bindings {
            if !subtree.contains(&b.node) {
                continue;
            }
            match &b.param_name {
                ParamName::TransformTX
                | ParamName::TransformTY
                | ParamName::TransformRZ
                | ParamName::TransformSX
                | ParamName::TransformSY => touches = true,
                // Depth/opacity never move 2D geometry.
                ParamName::TransformTZ | ParamName::Opacity => {}
                ParamName::Deform => {
                    // Deforms are only modelled on the parts themselves. A
                    // deform on an intermediate node (MeshGroup) warps the
                    // children through grid interpolation — not modelled.
                    if !part_uuids.contains(&b.node) {
                        eprintln!("[hint/{}] doubt: deform on intermediate node {} (param '{}')", composite.name, b.node, param.name);
                        return ChildrenOverlap;
                    }
                    touches = true;
                }
                ParamName::TransformRX | ParamName::TransformRY | ParamName::Other(_) => {
                    eprintln!("[hint/{}] doubt: unsupported binding {:?} on node {} (param '{}')", composite.name, b.param_name, b.node, param.name);
                    return ChildrenOverlap;
                }
            }
        }
        if touches {
            relevant.push(param);
        }
    }

    // Pose sweep. Rest first (cheap early exit for statically overlapping
    // children).
    let mut poses: Vec<Pose> = vec![None];
    for param in relevant {
        let (nx, ny) = param_grid_dims(param);
        let mut xs: Vec<f32> = Vec::new();
        for i in 0..nx {
            xs.push(i as f32);
            if i + 1 < nx {
                xs.push(i as f32 + 0.5);
            }
        }
        let mut ys: Vec<f32> = Vec::new();
        for j in 0..ny {
            ys.push(j as f32);
            if j + 1 < ny {
                ys.push(j as f32 + 0.5);
            }
        }
        for &fx in &xs {
            for &fy in &ys {
                poses.push(Some((param, fx, fy)));
            }
        }
    }

    for pose in &poses {
        let clouds: Option<Vec<Vec<[f32; 2]>>> = children
            .iter()
            .map(|c| child_world_vertices(c, pose))
            .collect();
        let Some(clouds) = clouds else {
            eprintln!("[hint/{}] doubt: inconsistent binding data at pose {:?}", composite.name, pose.map(|(p,x,y)| (p.name.clone(),x,y)));
            return ChildrenOverlap; // inconsistent binding data = doubt
        };
        let boxes: Vec<([f32; 2], [f32; 2])> = clouds.iter().map(|c| aabb(c)).collect();
        for i in 0..children.len() {
            for j in (i + 1)..children.len() {
                if !aabb_overlap(boxes[i], boxes[j]) {
                    continue;
                }
                if meshes_overlap(
                    &clouds[i],
                    &children[i].mesh.indices,
                    &clouds[j],
                    &children[j].mesh.indices,
                ) {
                    eprintln!("[hint/{}] overlap: '{}' vs '{}' at pose {:?}", composite.name, children[i].chain.last().unwrap().name, children[j].chain.last().unwrap().name, pose.map(|(p,x,y)| (p.name.clone(),x,y)));
                    return ChildrenOverlap;
                }
            }
        }
    }
    ChildrenDisjoint
}

/// Grid dimensions (x-points, y-points) of a param's binding tables.
fn param_grid_dims(param: &Param) -> (usize, usize) {
    let nx = param.axis_points[0].len().max(1);
    let ny = param.axis_points[1].len().max(1);
    (nx, ny)
}

/// Vertices of one composite child in composite-local space at `pose`.
/// Returns `None` when binding tables disagree with the mesh/grid shape.
fn child_world_vertices(child: &ChildGeom, pose: &Pose) -> Option<Vec<[f32; 2]>> {
    let vcount = child.mesh.vertices.len() / 2;
    let mut verts: Vec<[f32; 2]> = (0..vcount)
        .map(|i| [child.mesh.vertices[i * 2], child.mesh.vertices[i * 2 + 1]])
        .collect();

    let part_uuid = child.chain.last().unwrap().uuid;
    if let Some((param, fx, fy)) = pose {
        for b in &param.bindings {
            if b.node != part_uuid || b.param_name != ParamName::Deform {
                continue;
            }
            let BindingValues::Deform(dv) = &b.values else {
                return None;
            };
            if dv.frames == 0 {
                continue;
            }
            if dv.vertices_per_frame % vcount != 0 {
                return None;
            }
            let ny = dv.vertices_per_frame / vcount;
            for (v, vert) in verts.iter_mut().enumerate() {
                let sample = |ix: usize, iy: usize| -> [f32; 2] {
                    dv.data[ix * dv.vertices_per_frame + iy * vcount + v]
                };
                let off = bilinear(*fx, *fy, dv.frames, ny, sample)?;
                vert[0] += off[0];
                vert[1] += off[1];
            }
        }
    }

    // Chain transforms, innermost (part) first.
    let mut world = Affine::from_trs([0.0, 0.0], 0.0, [1.0, 1.0]);
    for node in &child.chain {
        world = world.then(&node_affine(node, pose)?);
    }
    // world currently maps part-space -> composite-space applying the top of
    // the chain last; composition above already ordered top-down because
    // `then` applies the right-hand side first and we fold left-to-right
    // from the composite's direct child down to the part.
    Some(verts.iter().map(|v| world.apply(*v)).collect())
}

/// Local affine of a node at `pose` (base transform + binding offsets).
/// Translation and Z-rotation offsets add; scale offsets multiply — matching
/// runtime semantics.
fn node_affine(node: &Node, pose: &Pose) -> Option<Affine> {
    let mut tx = node.transform.translation[0];
    let mut ty = node.transform.translation[1];
    let mut rz = node.transform.rotation[2];
    let mut sx = node.transform.scale[0];
    let mut sy = node.transform.scale[1];

    if let Some((param, fx, fy)) = pose {
        for b in &param.bindings {
            if b.node != node.uuid {
                continue;
            }
            let field = match &b.param_name {
                ParamName::TransformTX => &mut tx,
                ParamName::TransformTY => &mut ty,
                ParamName::TransformRZ => &mut rz,
                ParamName::TransformSX => &mut sx,
                ParamName::TransformSY => &mut sy,
                _ => continue,
            };
            let BindingValues::Transform(tv) = &b.values else {
                return None;
            };
            if tv.frames == 0 {
                continue;
            }
            let ny = tv.values_per_frame.max(1);
            let sample = |ix: usize, iy: usize| -> [f32; 2] {
                [tv.data[ix * tv.values_per_frame + iy], 0.0]
            };
            let v = bilinear(*fx, *fy, tv.frames, ny, sample)?[0];
            match &b.param_name {
                ParamName::TransformSX | ParamName::TransformSY => *field *= v,
                _ => *field += v,
            }
        }
    }
    Some(Affine::from_trs([tx, ty], rz, [sx, sy]))
}

/// Bilinear interpolation over a grid at fractional coords, clamped to the
/// grid. Returns `None` if the fractional coords land outside a 1-cell
/// overshoot of the table (shape mismatch).
fn bilinear(
    fx: f32,
    fy: f32,
    nx: usize,
    ny: usize,
    sample: impl Fn(usize, usize) -> [f32; 2],
) -> Option<[f32; 2]> {
    if nx == 0 || ny == 0 {
        return None;
    }
    let cx = fx.clamp(0.0, (nx - 1) as f32);
    let cy = fy.clamp(0.0, (ny - 1) as f32);
    let x0 = cx.floor() as usize;
    let y0 = cy.floor() as usize;
    let x1 = (x0 + 1).min(nx - 1);
    let y1 = (y0 + 1).min(ny - 1);
    let tx = cx - x0 as f32;
    let ty = cy - y0 as f32;
    let lerp = |a: [f32; 2], b: [f32; 2], t: f32| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
    let top = lerp(sample(x0, y0), sample(x1, y0), tx);
    let bot = lerp(sample(x0, y1), sample(x1, y1), tx);
    Some(lerp(top, bot, ty))
}

fn aabb(points: &[[f32; 2]]) -> ([f32; 2], [f32; 2]) {
    let mut min = [f32::INFINITY; 2];
    let mut max = [f32::NEG_INFINITY; 2];
    for p in points {
        min[0] = min[0].min(p[0]);
        min[1] = min[1].min(p[1]);
        max[0] = max[0].max(p[0]);
        max[1] = max[1].max(p[1]);
    }
    (min, max)
}

fn aabb_overlap(a: ([f32; 2], [f32; 2]), b: ([f32; 2], [f32; 2])) -> bool {
    a.0[0] <= b.1[0] && b.0[0] <= a.1[0] && a.0[1] <= b.1[1] && b.0[1] <= a.1[1]
}

/// Exact triangle-vs-triangle sweep between two indexed meshes. Zero-area
/// triangles cover no pixels and are skipped.
fn meshes_overlap(va: &[[f32; 2]], ia: &[u32], vb: &[[f32; 2]], ib: &[u32]) -> bool {
    const AREA_EPS: f32 = 1e-9;
    let tris = |v: &[[f32; 2]], idx: &[u32]| -> Vec<[[f32; 2]; 3]> {
        idx.chunks_exact(3)
            .filter_map(|t| {
                let tri = [
                    *v.get(t[0] as usize)?,
                    *v.get(t[1] as usize)?,
                    *v.get(t[2] as usize)?,
                ];
                (tri_area2(&tri).abs() > AREA_EPS).then_some(tri)
            })
            .collect()
    };
    let ta = tris(va, ia);
    let tb = tris(vb, ib);
    let boxes_b: Vec<_> = tb.iter().map(|t| aabb(t)).collect();
    for a in &ta {
        let box_a = aabb(a);
        for (b, box_b) in tb.iter().zip(&boxes_b) {
            if aabb_overlap(box_a, *box_b) && tris_overlap(a, b) {
                return true;
            }
        }
    }
    false
}

fn tri_area2(t: &[[f32; 2]; 3]) -> f32 {
    (t[1][0] - t[0][0]) * (t[2][1] - t[0][1]) - (t[2][0] - t[0][0]) * (t[1][1] - t[0][1])
}

/// SAT overlap test for two non-degenerate triangles. Shared edges/vertices
/// count as overlap (conservative).
fn tris_overlap(a: &[[f32; 2]; 3], b: &[[f32; 2]; 3]) -> bool {
    !(has_separating_axis(a, b) || has_separating_axis(b, a))
}

fn has_separating_axis(a: &[[f32; 2]; 3], b: &[[f32; 2]; 3]) -> bool {
    for i in 0..3 {
        let p0 = a[i];
        let p1 = a[(i + 1) % 3];
        let axis = [p0[1] - p1[1], p1[0] - p0[0]];
        if axis[0].abs() < f32::EPSILON && axis[1].abs() < f32::EPSILON {
            continue;
        }
        let project = |t: &[[f32; 2]; 3]| -> (f32, f32) {
            let mut min = f32::INFINITY;
            let mut max = f32::NEG_INFINITY;
            for p in t {
                let d = p[0] * axis[0] + p[1] * axis[1];
                min = min.min(d);
                max = max.max(d);
            }
            (min, max)
        };
        let (min_a, max_a) = project(a);
        let (min_b, max_b) = project(b);
        if max_a < min_b || max_b < min_a {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod diag_tests {
    //! Temporary instrumentation for the Back Hoodie contour bug
    //! (2026-07-03). Run manually: `cargo test --features inr,inr-export
    //! diag_back_hoodie -- --ignored --nocapture`. Not part of CI — depends
    //! on an asset path outside this repo. Remove once the fix lands.
    use super::*;

    #[test]
    #[ignore]
    fn diag_back_hoodie() {
        let path = "/home/husky/Rust/dev-bevy_inochi2d/assets/Arch Chan.inr";
        let model = crate::inr::InrModel::open(path).expect("open inr");
        let node = model
            .doc
            .nodes
            .iter()
            .find(|n| n.name == "Back Hoodie")
            .expect("part not found");
        let part = node.part.as_ref().expect("no part");
        let tex = &model.doc.textures[part.textures[0] as usize];
        let raw = model.view_bytes(tex.view).expect("view bytes");
        let (w, h) = (tex.width, tex.height);

        let raw_threshold = 128u8;
        let contours = marching_squares_alpha(raw, w, h, raw_threshold);

        let (breaks, ambiguous) = diag_hooks::drain();
        println!(
            "chain breaks: {} total, in x=[470..670]: {}",
            breaks.len(),
            breaks.iter().filter(|p| p[0] >= 470.0 && p[0] <= 670.0).count()
        );
        for p in breaks.iter().filter(|p| p[0] >= 470.0 && p[0] <= 670.0) {
            println!("  break at ({:.2}, {:.2}) u=({:.4}, {:.4})", p[0], p[1], p[0] / w as f32, p[1] / h as f32);
        }
        println!(
            "ambiguous branch points: {} total, in x=[470..670]: {}",
            ambiguous.len(),
            ambiguous.iter().filter(|(p, _)| p[0] >= 470.0 && p[0] <= 670.0).count()
        );
        for (p, n) in ambiguous.iter().filter(|(p, _)| p[0] >= 470.0 && p[0] <= 670.0) {
            println!("  ambiguous at ({:.2}, {:.2}) candidates={n}", p[0], p[1]);
        }

        println!("raw marching-squares polygons: {}", contours.len());
        for (i, c) in contours.iter().enumerate() {
            let area = signed_area(c).abs();
            let xs = c.iter().map(|p| p[0]).fold(f32::INFINITY, f32::min)
                ..c.iter().map(|p| p[0]).fold(f32::NEG_INFINITY, f32::max);
            println!(
                "  raw[{i}] pts={} area={:.1} x=[{:.1}..{:.1}] (u=[{:.4}..{:.4}])",
                c.len(),
                area,
                xs.start,
                xs.end,
                xs.start / w as f32,
                xs.end / w as f32,
            );
        }

        let big: Vec<Vec<[f32; 2]>> = contours
            .into_iter()
            .filter(|c| signed_area(c).abs() >= 4.0)
            .collect();
        println!("after area>=4.0 filter: {}", big.len());

        let outers = drop_holes(big);
        println!("after drop_holes: {}", outers.len());
        for (i, c) in outers.iter().enumerate() {
            let xs = c.iter().map(|p| p[0]).fold(f32::INFINITY, f32::min)
                ..c.iter().map(|p| p[0]).fold(f32::NEG_INFINITY, f32::max);
            println!(
                "  outer[{i}] pts={} x=[{:.1}..{:.1}] (u=[{:.4}..{:.4}])",
                c.len(),
                xs.start,
                xs.end,
                xs.start / w as f32,
                xs.end / w as f32,
            );
        }

        let simplified: Vec<Vec<[f32; 2]>> = outers
            .into_iter()
            .map(|c| douglas_peucker(&c, 1.0))
            .filter(|c| c.len() >= 3)
            .collect();
        let mut img = image::RgbaImage::from_raw(w, h, raw.to_vec()).expect("build image");
        for p in &simplified {
            for i in 0..p.len() {
                let a = p[i];
                let b = p[(i + 1) % p.len()];
                draw_line_diag(&mut img, a[0], a[1], b[0], b[1]);
            }
        }
        img.save("/tmp/back_hoodie_overlay_fixed2.png").expect("save png");
        println!("fixed overlay written to /tmp/back_hoodie_overlay_fixed2.png");
    }

    fn draw_line_diag(img: &mut image::RgbaImage, x0: f32, y0: f32, x1: f32, y1: f32) {
        let steps = (x1 - x0).abs().max((y1 - y0).abs()).ceil() as i32 + 1;
        let (w, h) = img.dimensions();
        for i in 0..=steps {
            let t = i as f32 / steps as f32;
            let x = (x0 + (x1 - x0) * t).round() as i64;
            let y = (y0 + (y1 - y0) * t).round() as i64;
            for dx in -1..=1 {
                for dy in -1..=1 {
                    let px = x + dx;
                    let py = y + dy;
                    if px >= 0 && py >= 0 && (px as u32) < w && (py as u32) < h {
                        img.put_pixel(px as u32, py as u32, image::Rgba([255, 0, 0, 255]));
                    }
                }
            }
        }
    }
}