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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
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
"""
椭圆几何特征提取(对应论文 §4.1.1–§4.2)。

输入:手绘圆的骨架二值图(笔画为前景)。
输出:
  - 全局特征 G_global(规范化后)
  - 角向序列:S_res、S_base、理想椭圆采样点、理想/实际采样角等
  - §4.2 频谱幅值:分窗段级 (M, s_l, 4) 与整圆 (N_θ/2, 4) FFT(CPU/numpy,无训练参数)

可训练谱域 Conv1d 见 section42;§4.1.4 / §4.3 见 section41 / section43。
"""

from __future__ import annotations

import argparse
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional, Sequence, Tuple, Union

import cv2
import numpy as np
import torch

PathLike = Union[str, np.ndarray]

# 半宽归一化:默认按图像宽度 W,取 half_norm = W/2(100×100 图时为 50)
DEFAULT_IMG_SIZE = 100
DEFAULT_HALF_NORM = 50  # None → 由输入图宽自动推断
DEFAULT_N_THETA = 360

# OpenCV fitEllipse 返回角度为【度】;论文 §4.1 规范化用 θ/360(与 θ_rad/(2π) 等价)
THETA_NORM_DIVISOR = 360.0

# §4.2:S_res 四通道与默认分段数
S_RES_CHANNELS = 4
DEFAULT_N_SEGMENTS = 10


@dataclass
class GlobalEllipseFeatures:
    """规范化后的全局几何量(可直接拼接为 g_vec 的输入,MLP 另训)。"""

    c_x: float
    c_y: float
    a: float
    b: float
    theta: float  # 度制 /360,等价于弧度 /(2π)
    e_quad: float
    raw: Tuple[float, float, float, float, float, float]  # 未归一化 (cx,cy,a,b,theta_deg,e_quad)

    def as_vector(self) -> np.ndarray:
        return np.array(
            [self.c_x, self.c_y, self.a, self.b, self.theta, self.e_quad],
            dtype=np.float32,
        )


@dataclass
class SequenceEllipseFeatures:
    """角向人工几何序列(§4.1.2)。"""

    n_theta: int
    half_norm: float
    # 基准轨迹 p_t,已除以 half_norm
    s_base: np.ndarray  # (N_theta, 2)
    # 理想椭圆上同极角参考点 tilde_p_t
    p_tilde: np.ndarray  # (N_theta, 2)
    # 几何残差 (Δr, Δθ, κ, τ);插值标记单独存放
    s_res: np.ndarray  # (N_theta, 4) 逐角位 L2 归一化后
    s_res_phys: np.ndarray  # (N_theta, 4) L2 归一化前的物理四维残差
    interp_flag: np.ndarray  # (N_theta,) 1=插值;valid=1-interp
    # 理想采样角、实际采样角(弧度,范围约 [-π, π])
    theta_ideal: np.ndarray  # (N_theta,)
    theta_actual: np.ndarray  # (N_theta,)
    # 未归一化的原始坐标(可选调试)
    s_base_px: np.ndarray
    p_tilde_px: np.ndarray

    def s_man_raw(self) -> np.ndarray:
        """未过 MLP 的 u_man:[Δr,Δθ,κ,τ, x_t,y_t, φ_t, valid_t];valid=1 实测,0 插值。"""
        valid = (1.0 - self.interp_flag).astype(np.float32)
        return np.concatenate(
            [
                self.s_res[:, :4],
                self.s_base,
                self.theta_ideal[:, None],
                valid[:, None],
            ],
            axis=1,
        )


@dataclass
class SegmentGeometry:
    """§4.2 分段与扩窗几何(由 N_θ, M 唯一确定)。"""

    n_theta: int
    n_segments: int
    s_l: int
    half: int
    l_win: int

    @property
    def segment_starts(self) -> np.ndarray:
        return (np.arange(self.n_segments) * self.s_l) % self.n_theta


@dataclass
class SpectralFFTFeatures:
    """§4.2 预计算 FFT 幅值(numpy,供 DataLoader / section42 复用)。"""

    geom: SegmentGeometry
    amp_segment: np.ndarray  # (M, s_l, 4) 分窗扩窗后逐段 FFT
    amp_global: np.ndarray  # (n_theta//2, 4) 整圆 360 点 FFT,无分窗


@dataclass
class EllipseFeatureBundle:
    global_feat: GlobalEllipseFeatures
    sequence: SequenceEllipseFeatures
    ellipse_cv: Tuple[Tuple[float, float], Tuple[float, float], float]  # center, (MA,ma), angle_deg
    spectral: Optional[SpectralFFTFeatures] = None


# ---------------------------------------------------------------------------
# 特征列向量:打包 / 反解(供 DataLoader、§4.1/§4.2 复用)
# ---------------------------------------------------------------------------

FEA_DIM_G = 6
FEA_DIM_U_MAN = 8
FEA_DIM_AMP_GLOBAL = DEFAULT_N_THETA // 2 * 4          # 720 → (180, 4)
FEA_DIM_AMP_SEGMENT = DEFAULT_N_SEGMENTS * (DEFAULT_N_THETA // DEFAULT_N_SEGMENTS) * 4  # 1440 → (10, 36, 4)
FEA_DIM_DEEP = 448

U_MAN_SLICE_S_RES = slice(0, 4)
U_MAN_SLICE_S_BASE = slice(4, 6)
U_MAN_IDX_PHI = 6
U_MAN_IDX_VALID = 7


class FeaPackLayout:
    """
    列向量 data 的存储布局(单样本长度 L,batch 时为 (B, L))。

    | 区间 [off, off+len) | 形状(反解)        | 说明 |
    |--------------------|---------------------|------|
    | g_global           | (6,)                | 椭圆全局 6 维 |
    | u_man              | (N_theta, 8)        | [Δr,Δθ,κ,τ,x,y,φ,valid] |
    | amp_global         | (180, 4)            | FFT(u_man[:,:4]) 整圆 |
    | amp_segment        | (10, 36, 4)         | FFT 分段 |
    | deep (可选)        | (N_theta, 448)      | ResNet 深度 |

    u_man 内 s_res = [:,:4],valid = [:,7],无需单独存。
    """

    n_theta: int = DEFAULT_N_THETA
    dim_g: int = FEA_DIM_G
    dim_u: int = FEA_DIM_U_MAN
    dim_amp_g: int = FEA_DIM_AMP_GLOBAL
    dim_amp_s: int = FEA_DIM_AMP_SEGMENT
    dim_deep: int = FEA_DIM_DEEP

    @classmethod
    def offsets(cls, with_deep: bool = False) -> Dict[str, Tuple[int, int]]:
        off = 0
        layout: Dict[str, Tuple[int, int]] = {}
        layout["g_global"] = (off, off := off + cls.dim_g)
        layout["u_man"] = (off, off := off + cls.n_theta * cls.dim_u)
        layout["amp_global"] = (off, off := off + cls.dim_amp_g)
        layout["amp_segment"] = (off, off := off + cls.dim_amp_s)
        if with_deep:
            layout["deep"] = (off, off := off + cls.n_theta * cls.dim_deep)
        return layout

    @classmethod
    def len_core(cls) -> int:
        return cls.offsets(with_deep=False)["amp_segment"][1]

    @classmethod
    def len_full(cls) -> int:
        return cls.offsets(with_deep=True)["deep"][1]


def pack_fea_column(
    g_global: torch.Tensor,
    u_man: torch.Tensor,
    amp_global: torch.Tensor,
    amp_segment: torch.Tensor,
    deep: Optional[torch.Tensor] = None,
) -> torch.Tensor:
    """将 g_global / u_man / amp_* / deep 拼成列向量 (L,)。"""
    parts = [
        g_global.reshape(-1),
        u_man.reshape(-1),
        amp_global.reshape(-1),
        amp_segment.reshape(-1),
    ]
    if deep is not None:
        parts.append(deep.reshape(-1))
    return torch.cat(parts, dim=0)


def unpack_fea_column(
    data: torch.Tensor,
    with_deep: bool = False,
) -> Dict[str, torch.Tensor]:
    """按 FeaPackLayout 从列向量反解各字段。"""
    batched = data.dim() == 2
    flat = data if batched else data.unsqueeze(0)
    layout = FeaPackLayout.offsets(with_deep=with_deep)
    n = FeaPackLayout.n_theta
    half = n // 2
    s_l = n // DEFAULT_N_SEGMENTS
    m = DEFAULT_N_SEGMENTS

    a, b = layout["g_global"]
    g = flat[:, a:b]
    a, b = layout["u_man"]
    u = flat[:, a:b].view(flat.shape[0], n, FeaPackLayout.dim_u)
    a, b = layout["amp_global"]
    ag = flat[:, a:b].view(flat.shape[0], half, 4)
    a, b = layout["amp_segment"]
    aseg = flat[:, a:b].view(flat.shape[0], m, s_l, 4)

    out: Dict[str, torch.Tensor] = {
        "g_global": g.squeeze(0) if not batched else g,
        "u_man": u.squeeze(0) if not batched else u,
        "amp_global": ag.squeeze(0) if not batched else ag,
        "amp_segment": aseg.squeeze(0) if not batched else aseg,
    }
    if with_deep:
        a, b = layout["deep"]
        d = flat[:, a:b].view(flat.shape[0], n, FeaPackLayout.dim_deep)
        out["deep"] = d.squeeze(0) if not batched else d
    return out


@dataclass
class FeaFields:
    """
    反解后的分字段特征,供 section41 / section42 / deep_elipse_model 直接使用。
    各字段形状见属性注释;batch 时均带 leading B 维。
    """
    g_global: torch.Tensor       # (6,) 或 (B, 6)
    u_man: torch.Tensor          # (N_theta, 8) 或 (B, N_theta, 8)
    amp_global: torch.Tensor     # (N_theta/2, 4) 或 (B, N_theta/2, 4)
    amp_segment: torch.Tensor    # (M, s_l, 4) 或 (B, M, s_l, 4)
    deep: Optional[torch.Tensor] = None  # (N_theta, 448) 或 (B, N_theta, 448)

    @property
    def s_res(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_SLICE_S_RES]

    @property
    def s_base(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_SLICE_S_BASE]

    @property
    def phi(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_IDX_PHI]

    @property
    def valid(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_IDX_VALID]


def unpack_fea_fields(
    fea: Union["FeaTensor", torch.Tensor],
    with_deep: Optional[bool] = None,
) -> FeaFields:
    """从 FeaTensor 或列向量 data 反解为分字段 FeaFields。"""
    if isinstance(fea, FeaTensor):
        data = fea.data
        if with_deep is None:
            with_deep = fea.with_deep
    else:
        data = fea
        if with_deep is None:
            L = data.shape[-1]
            with_deep = L == FeaPackLayout.len_full()
    parts = unpack_fea_column(data, with_deep=with_deep)
    return FeaFields(
        g_global=parts["g_global"],
        u_man=parts["u_man"],
        amp_global=parts["amp_global"],
        amp_segment=parts["amp_segment"],
        deep=parts.get("deep"),
    )


@dataclass
class FeaTensor:
    """
    fea_tensor:内部为列向量 data,按 FeaPackLayout 打包。
    使用时调用 .fields 或 unpack_fea_fields() 得到各独立特征,供 §4.1/§4.2 使用。
    """
    data: torch.Tensor  # (L,) 或 (B, L)

    def __post_init__(self):
        if self.data.dim() not in (1, 2):
            raise ValueError(f"data 须为 (L,) 或 (B,L),got {tuple(self.data.shape)}")

    @property
    def batched(self) -> bool:
        return self.data.dim() == 2

    @property
    def with_deep(self) -> bool:
        L = self.data.shape[-1]
        if L == FeaPackLayout.len_full():
            return True
        if L == FeaPackLayout.len_core():
            return False
        raise ValueError(
            f"data 长度 {L} 与布局不符 (core={FeaPackLayout.len_core()}, full={FeaPackLayout.len_full()})"
        )

    @property
    def fields(self) -> FeaFields:
        return unpack_fea_fields(self)

    def _parts(self) -> Dict[str, torch.Tensor]:
        return unpack_fea_column(self.data, with_deep=self.with_deep)

    @property
    def n_theta(self) -> int:
        return FeaPackLayout.n_theta

    @property
    def g_global(self) -> torch.Tensor:
        return self._parts()["g_global"]

    @property
    def u_man(self) -> torch.Tensor:
        return self._parts()["u_man"]

    @property
    def s_res(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_SLICE_S_RES]

    @property
    def s_base(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_SLICE_S_BASE]

    @property
    def phi(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_IDX_PHI]

    @property
    def valid(self) -> torch.Tensor:
        return self.u_man[..., U_MAN_IDX_VALID]

    @property
    def amp_global_map(self) -> torch.Tensor:
        return self._parts()["amp_global"]

    @property
    def amp_segment_map(self) -> torch.Tensor:
        return self._parts()["amp_segment"]

    @property
    def deep(self) -> Optional[torch.Tensor]:
        if not self.with_deep:
            return None
        return self._parts()["deep"]

    @property
    def compact_numel(self) -> int:
        return int(self.data.numel())

    @classmethod
    def from_tensors(
        cls,
        g_global: torch.Tensor,
        u_man: torch.Tensor,
        amp_global: torch.Tensor,
        amp_segment: torch.Tensor,
        deep: Optional[torch.Tensor] = None,
    ) -> "FeaTensor":
        """从各分量打包为列向量 fea_tensor。"""
        if u_man.dim() == 3:
            b = u_man.shape[0]
            rows = []
            for i in range(b):
                rows.append(
                    pack_fea_column(
                        g_global[i] if g_global.dim() > 1 else g_global,
                        u_man[i],
                        amp_global[i] if amp_global.dim() > 1 else amp_global,
                        amp_segment[i] if amp_segment.dim() > 1 else amp_segment,
                        deep[i] if deep is not None else None,
                    )
                )
            return cls(data=torch.stack(rows, dim=0))
        return cls(
            data=pack_fea_column(
                g_global,
                u_man,
                amp_global.reshape(-1) if amp_global.dim() > 2 else amp_global,
                amp_segment.reshape(-1) if amp_segment.dim() > 2 else amp_segment,
                deep,
            )
        )

    def to(self, device=None, dtype=None, non_blocking=False):
        kwargs = {"non_blocking": non_blocking}
        if dtype is not None:
            kwargs["dtype"] = dtype
        moved = self.data.to(device, **kwargs) if device is not None else self.data.to(**kwargs)
        return FeaTensor(data=moved)

    @classmethod
    def stack(cls, items: Sequence["FeaTensor"]) -> "FeaTensor":
        if not items:
            raise ValueError("empty FeaTensor list")
        return cls(data=torch.stack([x.data.reshape(-1) for x in items], dim=0))


# ---------------------------------------------------------------------------
# §4.2 分段扩窗、L2 归一化与 FFT(CPU / numpy)
# ---------------------------------------------------------------------------


def build_segment_geometry(
    n_theta: int = DEFAULT_N_THETA,
    n_segments: int = DEFAULT_N_SEGMENTS,
) -> SegmentGeometry:
    if n_theta % n_segments != 0:
        raise ValueError(f"n_theta={n_theta} 须能被 M={n_segments} 整除")
    s_l = n_theta // n_segments
    if s_l % 2 != 0:
        raise ValueError(f"s_l={s_l} 须为偶数以便半段扩窗")
    return SegmentGeometry(
        n_theta=n_theta,
        n_segments=n_segments,
        s_l=s_l,
        half=s_l // 2,
        l_win=2 * s_l,
    )


def _arc_indices_ccw(start: int, length: int, n_theta: int) -> np.ndarray:
    """从 start 起沿圆周逆时针走 length 步(角标递增 mod N)。"""
    return np.array([(start + j) % n_theta for j in range(length)], dtype=np.int64)


def responsibility_indices(m: int, geom: SegmentGeometry) -> np.ndarray:
    """第 m 段责任弧角标(长度 s_l),逆时针有序。"""
    return _arc_indices_ccw(geom.segment_starts[m], geom.s_l, geom.n_theta)


def window_indices(m: int, geom: SegmentGeometry) -> np.ndarray:
    """扩窗角标 I_m(L_win=2·s_l),起点 o_m = m·s_l − s_l/2。"""
    o_m = (geom.segment_starts[m] - geom.half) % geom.n_theta
    return _arc_indices_ccw(o_m, geom.l_win, geom.n_theta)


def l2_normalize_channels(
    seq: np.ndarray,
    eps: float = 1e-8,
) -> np.ndarray:
    """对最后一维各通道沿时间轴(第 0 维)做 L2 归一化。seq: (T, C)。"""
    w = seq.astype(np.float32, copy=True)
    n_ch = w.shape[1] if w.ndim > 1 else 1
    if w.ndim == 1:
        w = w[:, None]
    for c in range(n_ch):
        n = float(np.linalg.norm(w[:, c]))
        if n > eps:
            w[:, c] /= n
    return w


def l2_normalize_window(window: np.ndarray, eps: float = 1e-8) -> np.ndarray:
    """窗内四通道 L2 归一化;等价于 l2_normalize_channels。"""
    return l2_normalize_channels(window, eps=eps)


def extract_window(
    s_res: np.ndarray,
    m: int,
    geom: SegmentGeometry,
) -> np.ndarray:
    """S_res^(m) ∈ R^{L_win×4},逆时针取样 + L2 归一化。"""
    idx = window_indices(m, geom)
    win = s_res[idx, :].astype(np.float32)
    return l2_normalize_window(win)


def fft_amplitude_matrix(
    window: np.ndarray,
    s_l: int,
) -> np.ndarray:
    """
    逐通道 rFFT 幅值,保留前 s_l 个非冗余频率 bin。

    window: (L_win, 4),通常 L_win = 2·s_l → 输出 (s_l, 4)
    """
    l_win = window.shape[0]
    if l_win != 2 * s_l and l_win != s_l:
        raise ValueError(
            f"window length {l_win} 须为 s_l={s_l}(整圆)或 2·s_l={2*s_l}(扩窗)"
        )
    out = np.zeros((s_l, S_RES_CHANNELS), dtype=np.float32)
    for c in range(S_RES_CHANNELS):
        spec = np.fft.rfft(window[:, c], n=l_win)
        out[:, c] = np.abs(spec[:s_l]).astype(np.float32)
        norm = np.linalg.norm(out[:, c])
        if norm > 1e-8:
            out[:, c] = out[:, c] / norm
    return out


def compute_fft_amplitude_segmented(
    s_res: np.ndarray,
    geom: SegmentGeometry,
) -> np.ndarray:
    """所有段幅值谱 A^(m),形状 (M, s_l, 4)。"""
    m_count = geom.n_segments
    amps = np.zeros((m_count, geom.s_l, S_RES_CHANNELS), dtype=np.float32)
    for m in range(m_count):
        win = extract_window(s_res, m, geom)
        amps[m] = fft_amplitude_matrix(win, geom.s_l)
    return amps


def compute_fft_amplitude_global(
    s_res: np.ndarray,
    *,
    l2_normalize: bool = True,
) -> np.ndarray:
    """
    整圆 S_res 序列 FFT,无分窗。

    对 (N_θ, 4) 逐通道 L2 归一化后 rFFT(n=N_θ),保留前 N_θ/2 个幅值 bin → (N_θ/2, 4)。
    """
    n_theta = s_res.shape[0]
    if n_theta % 2 != 0:
        raise ValueError(f"整圆 FFT 要求 N_θ 为偶数,got {n_theta}")
    w = s_res.astype(np.float32, copy=True)
    if l2_normalize:
        w = l2_normalize_window(w)
    s_l = n_theta // 2
    return fft_amplitude_matrix(w, s_l)


def compute_spectral_fft_features(
    s_res: np.ndarray,
    n_segments: int = DEFAULT_N_SEGMENTS,
) -> SpectralFFTFeatures:
    """§4.2 分窗 + 整圆 FFT 一次算齐。"""
    n_theta = s_res.shape[0]
    geom = build_segment_geometry(n_theta, n_segments)
    amp_segment = compute_fft_amplitude_segmented(s_res, geom)
    amp_global = compute_fft_amplitude_global(s_res)
    return SpectralFFTFeatures(
        geom=geom,
        amp_segment=amp_segment,
        amp_global=amp_global,
    )


# 与旧名兼容(section42 / 可视化脚本)
compute_fft_amplitude_batch = compute_fft_amplitude_segmented


# ---------------------------------------------------------------------------
# §4.2 扩展:连续小波变换(CWT)角向 scalogram
# ---------------------------------------------------------------------------

CWT_WAVELET_DEFAULT = "morl"
CWT_N_SCALES_DEFAULT = 40
CWT_SCALE_MIN = 2.0
CWT_SCALE_MAX = 48.0
CWT_PAD_FRACTION = 0.25


def default_cwt_scales(n_scales: int = CWT_N_SCALES_DEFAULT) -> np.ndarray:
    """Morlet CWT 尺度 a,几何间隔覆盖约 4°–90° 等效周期(N_θ=360)。"""
    return np.geomspace(CWT_SCALE_MIN, CWT_SCALE_MAX, num=n_scales).astype(np.float64)


def circular_pad_cwt_1d(
    y: np.ndarray,
    scales: np.ndarray,
    wavelet: str = CWT_WAVELET_DEFAULT,
    pad_fraction: float = CWT_PAD_FRACTION,
) -> np.ndarray:
    """
    单通道圆周延拓 CWT,返回幅值 |W(a,b)|。

    Parameters
    ----------
    y : (N_theta,)
    scales : (K,)

    Returns
    -------
    coef : (K, N_theta)
    """
    import pywt

    y = np.asarray(y, dtype=np.float64)
    y = y - y.mean()
    n = len(y)
    pad = max(4, int(n * pad_fraction))
    y_pad = np.concatenate([y[-pad:], y, y[:pad]])
    coef, _ = pywt.cwt(y_pad, scales, wavelet)
    return np.abs(coef[:, pad : pad + n]).astype(np.float32)


def compute_cwt_scalogram(
    s_res: np.ndarray,
    *,
    n_scales: int = CWT_N_SCALES_DEFAULT,
    wavelet: str = CWT_WAVELET_DEFAULT,
    scales: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    对 S_res 四通道做 Morlet CWT,输出 scalogram 幅值。

    Parameters
    ----------
    s_res : (N_theta, 4)

    Returns
    -------
    cwt_scalo : (4, K, N_theta)  — |W(a,b)| per channel
    """
    if s_res.ndim != 2 or s_res.shape[1] != S_RES_CHANNELS:
        raise ValueError(f"s_res 须为 (N_theta, {S_RES_CHANNELS}),got {s_res.shape}")
    sc = scales if scales is not None else default_cwt_scales(n_scales)
    n_theta = s_res.shape[0]
    out = np.zeros((S_RES_CHANNELS, len(sc), n_theta), dtype=np.float32)
    for c in range(S_RES_CHANNELS):
        out[c] = circular_pad_cwt_1d(s_res[:, c], sc, wavelet=wavelet)
    return out


def default_cwt_scale_indices(
    n_scales: int = CWT_N_SCALES_DEFAULT,
    n_stages: int = 4,
) -> List[List[int]]:
    """将 K 个尺度均分为 n_stages 组,供 stage–尺度亲和(E2 uniform)。"""
    per = n_scales // n_stages
    indices: List[List[int]] = []
    for s in range(n_stages):
        start = s * per
        end = n_scales if s == n_stages - 1 else (s + 1) * per
        indices.append(list(range(start, end)))
    return indices


def pyramid_stage_lengths_from_n_theta(
    n_theta: int,
    n_stages: int = 4,
    stem_stride: int = 2,
) -> List[int]:
    """ConvNeXt1D 金字塔各 stage 角向长度(stem 后逐级 //2)。"""
    lengths: List[int] = []
    L = max(n_theta // stem_stride, 1)
    for _ in range(n_stages):
        lengths.append(L)
        L = max(L // 2, 1)
    return lengths


def physics_cwt_scale_indices(
    n_scales: int = CWT_N_SCALES_DEFAULT,
    n_stages: int = 4,
    *,
    n_theta: int = DEFAULT_N_THETA,
    stage_lengths: Optional[Sequence[int]] = None,
    wavelet: str = CWT_WAVELET_DEFAULT,
    scales: Optional[np.ndarray] = None,
) -> List[List[int]]:
    """
    按 CWT 等效周期 T(a) 与 pyramid stage 角向步长 Δθ=360/L_s 对齐分组(H3 / E2-phys)。

    细 stage(小 Δθ)→ 短周期(高频)尺度;粗 stage → 长周期尺度。
    """
    if stage_lengths is None:
        stage_lengths = pyramid_stage_lengths_from_n_theta(n_theta, n_stages)
    if len(stage_lengths) != n_stages:
        raise ValueError(f"stage_lengths 长度须为 {n_stages},got {len(stage_lengths)}")

    scales_arr = np.asarray(
        scales if scales is not None else default_cwt_scales(n_scales)
    )
    periods = scale_to_period_degrees(scales_arr, wavelet=wavelet, n_theta=n_theta)
    delta_theta = np.array([n_theta / float(L) for L in stage_lengths], dtype=np.float64)

    log_p = np.log(np.maximum(periods, 1e-6))
    log_d = np.log(np.maximum(delta_theta, 1e-6))

    indices: List[List[int]] = [[] for _ in range(n_stages)]
    for k in range(len(scales_arr)):
        s = int(np.argmin(np.abs(log_p[k] - log_d)))
        indices[s].append(k)

    for s in range(n_stages):
        if indices[s]:
            continue
        donor = max(range(n_stages), key=lambda i: len(indices[i]))
        if len(indices[donor]) <= 1:
            break
        best_k = min(indices[donor], key=lambda k: abs(log_p[k] - log_d[s]))
        indices[donor].remove(best_k)
        indices[s].append(best_k)

    for s in range(n_stages):
        indices[s].sort()
    return indices


def describe_cwt_scale_groups(
    scale_indices: List[List[int]],
    *,
    n_scales: int = CWT_N_SCALES_DEFAULT,
    n_theta: int = DEFAULT_N_THETA,
    stage_lengths: Optional[Sequence[int]] = None,
    wavelet: str = CWT_WAVELET_DEFAULT,
) -> str:
    """可读日志:各 stage 尺度子集与 T(a)、Δθ 对照。"""
    if stage_lengths is None:
        stage_lengths = pyramid_stage_lengths_from_n_theta(
            n_theta, len(scale_indices)
        )
    scales_arr = default_cwt_scales(n_scales)
    periods = scale_to_period_degrees(scales_arr, wavelet=wavelet, n_theta=n_theta)
    lines = ["CWT scale–stage groups:"]
    for sid, idx in enumerate(scale_indices):
        d_theta = n_theta / float(stage_lengths[sid])
        if idx:
            p_sub = periods[idx]
            p_rng = f"{p_sub.min():.1f}–{p_sub.max():.1f}°"
        else:
            p_rng = "—"
        lines.append(
            f"  stage{sid} L={stage_lengths[sid]} Δθ={d_theta:.1f}° "
            f"scales={idx} T(a)={p_rng}"
        )
    return "\n".join(lines)


def scale_to_period_degrees(
    scales: np.ndarray,
    wavelet: str = CWT_WAVELET_DEFAULT,
    n_theta: int = DEFAULT_N_THETA,
) -> np.ndarray:
    """尺度 a → 等效周期(度)。"""
    import pywt

    sampling_period = 360.0 / n_theta
    freqs = pywt.scale2frequency(wavelet, scales) / sampling_period
    freqs = np.maximum(freqs, 1e-12)
    return 1.0 / freqs


def _load_skeleton(image: PathLike) -> np.ndarray:
    if isinstance(image, str):
        img = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
        if img is None:
            raise FileNotFoundError(f"Cannot read image: {image}")
    else:
        img = np.asarray(image)
        if img.ndim == 3:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 前景 = 非零
    return (img > 0).astype(np.uint8)


def _skeleton_pixels(mask: np.ndarray) -> np.ndarray:
    ys, xs = np.nonzero(mask)
    if len(xs) < 5:
        raise ValueError("Skeleton has fewer than 5 foreground pixels; cannot fit ellipse.")
    return np.stack([xs.astype(np.float64), ys.astype(np.float64)], axis=1)


def fit_ellipse_least_squares(points_xy: np.ndarray) -> Tuple[float, float, float, float, float]:
    """
    对骨架点做最小二乘椭圆拟合(OpenCV fitEllipse 约定)。

    返回 (cx, cy, a, b, theta_deg):
      - a = size.width/2,沿 fitEllipse 给出的 angle 方向
      - b = size.height/2,与 angle 垂直
      不再强行交换长短轴,避免相对真实方向偏转约 90°。
    """
    pts = points_xy.reshape(-1, 1, 2).astype(np.float32)
    (cx, cy), (width, height), angle_deg = cv2.fitEllipse(pts)
    return float(cx), float(cy), float(width) * 0.5, float(height) * 0.5, float(angle_deg)


def _ellipse_point_on_ray(
    cx: float,
    cy: float,
    a: float,
    b: float,
    theta_deg: float,
    phi_rad: float,
) -> Tuple[float, float]:
    """
    从中心 (cx,cy) 沿极角 phi 的射线与拟合椭圆的**正向交点**(OpenCV 轴约定)。

    射线:(cx,cy)+r*(cosφ,sinφ);椭圆局部坐标下 (u/a)^2+(v/b)^2=1 → 解析求 r>0。
    保证 p̃_t 与 p_t 同在一条过中心的射线上。
    """
    th = np.deg2rad(theta_deg)
    c = np.cos(phi_rad - th)
    s = np.sin(phi_rad - th)
    denom = (c / a) ** 2 + (s / b) ** 2
    if denom < 1e-14:
        r = float(max(a, b))
    else:
        r = float(1.0 / np.sqrt(denom))
    return cx + r * np.cos(phi_rad), cy + r * np.sin(phi_rad)


def _quadrant_error(
    points_xy: np.ndarray,
    cx: float,
    cy: float,
    a: float,
    b: float,
    theta_deg: float,
) -> float:
    """四象限平均径向残差之和,作为 E_quad。"""
    if len(points_xy) == 0:
        return 0.0
    rel = points_xy - np.array([cx, cy])
    quad = np.zeros(4, dtype=np.float64)
    cnt = np.zeros(4, dtype=np.int64)
    for px, py in points_xy:
        dx, dy = px - cx, py - cy
        phi = np.arctan2(dy, dx)
        ex, ey = _ellipse_point_on_ray(cx, cy, a, b, theta_deg, phi)
        res = np.hypot(px - ex, py - ey)
        q = int(dx >= 0) + 2 * int(dy >= 0)
        quad[q] += res
        cnt[q] += 1
    total = 0.0
    for q in range(4):
        if cnt[q] > 0:
            total += quad[q] / cnt[q]
    return float(total)


def _ray_hit_skeleton(
    mask: np.ndarray,
    cx: float,
    cy: float,
    phi_rad: float,
    max_radius: Optional[float] = None,
) -> Optional[Tuple[float, float]]:
    """从中心沿极角 phi 发射射线,取最近骨架交点。"""
    h, w = mask.shape
    if max_radius is None:
        max_radius = float(np.hypot(w, h))
    cos_p, sin_p = np.cos(phi_rad), np.sin(phi_rad)
    # 亚像素步进
    for r in np.linspace(1.0, max_radius, int(max_radius * 2)):
        x = int(round(cx + r * cos_p))
        y = int(round(cy + r * sin_p))
        if x < 0 or x >= w or y < 0 or y >= h:
            continue
        if mask[y, x]:
            return float(x), float(y)
    return None


def _interpolate_pt_circular(
    points: np.ndarray,
    valid: np.ndarray,
    fallback: Optional[np.ndarray] = None,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    沿闭合角向序列对无效点做线性插值。

    Parameters
    ----------
    points : (N, 2),无效位置可为 NaN
    valid : (N,) bool,True 表示射线命中骨架的实测点
    fallback : 全无实测时使用的备用坐标(通常为 p̃_t)

    Returns
    -------
    filled : (N, 2)
    interp_flag : (N,) 1=插值,0=实测
    """
    n = points.shape[0]
    filled = points.copy()
    interp_flag = np.zeros(n, dtype=np.float64)
    valid = np.asarray(valid, dtype=bool)

    if not valid.any():
        if fallback is not None:
            filled = fallback.copy()
        interp_flag[:] = 1.0
        return filled, interp_flag

    if valid.all():
        return filled, interp_flag

    for t in range(n):
        if valid[t]:
            continue
        t_prev, d_prev = None, 0
        t_next, d_next = None, 0
        for k in range(1, n + 1):
            i = (t - k) % n
            if valid[i]:
                t_prev, d_prev = i, k
                break
        for k in range(1, n + 1):
            i = (t + k) % n
            if valid[i]:
                t_next, d_next = i, k
                break

        if t_prev is None and t_next is None:
            if fallback is not None:
                filled[t] = fallback[t]
            interp_flag[t] = 1.0
        elif t_prev is None:
            filled[t] = filled[t_next]
            interp_flag[t] = 1.0
        elif t_next is None:
            filled[t] = filled[t_prev]
            interp_flag[t] = 1.0
        else:
            w = d_prev / (d_prev + d_next)
            filled[t] = (1.0 - w) * filled[t_prev] + w * filled[t_next]
            interp_flag[t] = 1.0

    return filled, interp_flag


def half_norm_from_width(width: int) -> float:
    return float(width) / 2.0


def normalize_theta_global(theta_deg: float) -> float:
    """
    全局倾角规范化。fitEllipse 输出为度,采用论文约定除以 360。
    (若先转弧度再除以 2π,数值上与除以 360 一致。)
    """
    return float(theta_deg) / THETA_NORM_DIVISOR


def preprocess_rgb_to_skeleton(
    rgb: PathLike,
    size: int = DEFAULT_IMG_SIZE,
) -> dict:
    """
    RGB 原图 → resize → 大津二值(黑笔迹→白前景)→ 形态学迭代骨架化。

    Returns
    -------
    dict with keys: rgb, gray, foreground (uint8 0/255), skeleton (uint8 0/1), size
    """
    if isinstance(rgb, (str, Path)):
        bgr = cv2.imread(str(rgb), cv2.IMREAD_COLOR)
        if bgr is None:
            raise FileNotFoundError(str(rgb))
    else:
        bgr = np.asarray(rgb)
        if bgr.ndim == 2:
            bgr = cv2.cvtColor(bgr, cv2.COLOR_GRAY2BGR)

    bgr = cv2.resize(bgr, (size, size), interpolation=cv2.INTER_AREA)
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    # 背景亮、笔画暗 → 反相大津,使笔迹为白色前景
    _, foreground = cv2.threshold(
        gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU
    )
    skeleton = skeletonize_foreground(foreground)
    return {
        "rgb": bgr,
        "gray": gray,
        "foreground": foreground,
        "skeleton": skeleton,
        "size": size,
    }


def skeletonize_image(binary_image: np.ndarray) -> np.ndarray:
    """形态学骨架化(迭代细化,Zhang-Suen 风格形态学骨架)。"""
    temp_img = (binary_image > 0).astype(np.uint8)
    size = np.size(temp_img)
    skel = np.zeros(temp_img.shape, np.uint8)
    element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
    done = False

    while not done:
        eroded = cv2.erode(temp_img, element)
        temp = cv2.dilate(eroded, element)
        temp = cv2.subtract(temp_img, temp)
        skel = cv2.bitwise_or(skel, temp)
        temp_img = eroded.copy()
        zeros = size - cv2.countNonZero(temp_img)
        if zeros == size:
            done = True
    return skel


def skeletonize_foreground(foreground255: np.ndarray) -> np.ndarray:
    """对大津二值前景做形态学骨架化,返回 0/1 掩膜(不做剪枝/删连通域)。"""
    skel = skeletonize_image(foreground255)
    return (skel > 0).astype(np.uint8)


def extract_global_features(
    cx: float,
    cy: float,
    a: float,
    b: float,
    theta_deg: float,
    e_quad: float,
    half_norm: Optional[float] = DEFAULT_HALF_NORM,
) -> GlobalEllipseFeatures:
    hn = float(half_norm if half_norm is not None else DEFAULT_IMG_SIZE / 2.0)
    return GlobalEllipseFeatures(
        c_x=cx / hn,
        c_y=cy / hn,
        a=a / hn,
        b=b / hn,
        theta=normalize_theta_global(theta_deg),
        e_quad=e_quad / hn,
        raw=(cx, cy, a, b, theta_deg, e_quad),
    )


def extract_sequence_features(
    mask: np.ndarray,
    cx: float,
    cy: float,
    a: float,
    b: float,
    theta_deg: float,
    n_theta: int = DEFAULT_N_THETA,
    half_norm: Optional[float] = DEFAULT_HALF_NORM,
    tau_normalize: bool = True,
) -> SequenceEllipseFeatures:
    """
    在骨架上按角度采样,构造 S_base、S_res 及扩展几何序列。

    Δr_t, Δθ_t, κ_t, τ_t 在**插值补全后的** p_t 上计算;第 5 维 interp:实测=0,插值=1。
    """
    hn = float(half_norm if half_norm is not None else mask.shape[1] / 2.0)
    p_tilde_px = np.zeros((n_theta, 2), dtype=np.float64)
    theta_ideal = np.zeros(n_theta, dtype=np.float64)
    raw_pt_px = np.full((n_theta, 2), np.nan, dtype=np.float64)
    valid_hit = np.zeros(n_theta, dtype=bool)

    for t in range(n_theta):
        phi = 2.0 * np.pi * t / n_theta
        theta_ideal[t] = phi
        tx, ty = _ellipse_point_on_ray(cx, cy, a, b, theta_deg, phi)
        p_tilde_px[t, 0], p_tilde_px[t, 1] = tx, ty

        hit = _ray_hit_skeleton(mask, cx, cy, phi)
        if hit is not None:
            raw_pt_px[t, 0], raw_pt_px[t, 1] = hit
            valid_hit[t] = True

    s_base_px, interp_flag = _interpolate_pt_circular(
        raw_pt_px, valid_hit, fallback=p_tilde_px
    )
    theta_actual = np.arctan2(s_base_px[:, 1] - cy, s_base_px[:, 0] - cx)

    s_res = np.zeros((n_theta, 4), dtype=np.float64)

    # 径向残差 Δr_t = ||p_t-c|| - ||p̃_t-c||(带符号,不对 Δr 取 abs)
    # r_act、r_ref 为到中心的距离(非负);其差可正可负:
    #   Δr > 0:骨架点比同角位椭圆参考点更远离中心;Δr < 0:更靠近中心。
    # 除以 half_norm 仅作幅值缩放,不改变符号。
    for t in range(n_theta):
        px, py = s_base_px[t]
        tx, ty = p_tilde_px[t]
        r_act = np.hypot(px - cx, py - cy)
        r_ref = np.hypot(tx - cx, ty - cy)
        delta_r = (r_act - r_ref) / hn
        delta_theta = (theta_actual[t] - theta_ideal[t] + np.pi) % (2 * np.pi) - np.pi
        delta_theta_norm = delta_theta / np.pi
        s_res[t, 0] = float(delta_r)
        s_res[t, 1] = delta_theta_norm

    # 曲率 κ_t 与切向步长 τ_t(闭合:t-1 对 359 回绕)
    tau = np.zeros(n_theta, dtype=np.float64)
    for t in range(n_theta):
        tp = (t - 1) % n_theta
        dp = s_base_px[t] - s_base_px[tp]
        tau[t] = np.linalg.norm(dp)

    if tau_normalize and tau.sum() > 1e-8:
        tau = tau / tau.sum()

    # 离散曲率 κ_t = Δφ_t / ||p_t - p_{t-1}||(论文 §4.1.2)
    # 注意:每 1° 射线采样在 100×100 骨架上常落到同一像素 → ||dp||≈0;
    # 分母仅用 ε=1e-8 会使 κ≈(π/2)/ε≈1.58e8,属数值退化而非真实几何。
    min_arc_px = 1.0  # 最小有效弧长(像素),低于此视为无效段
    kappa = np.zeros(n_theta, dtype=np.float64)
    for t in range(n_theta):
        tp = (t - 1) % n_theta
        tpp = (t - 2) % n_theta
        dp = s_base_px[t] - s_base_px[tp]
        dm = s_base_px[tp] - s_base_px[tpp]
        nu = float(np.linalg.norm(dp))
        mu = float(np.linalg.norm(dm))
        if nu < min_arc_px or mu < min_arc_px:
            # 相邻角位同像素或步长 <1px:无法稳定估计切向夹角,曲率置 0
            kappa[t] = 0.0
            continue
        u_hat = dp / nu
        m_hat = dm / mu
        cos_a = float(np.clip(np.dot(u_hat, m_hat), -1.0, 1.0))
        delta_phi = float(np.arccos(cos_a))
        kappa[t] = delta_phi / nu

    s_res[:, 2] = kappa
    s_res[:, 3] = tau

    s_res_phys = s_res.copy()

    for t in range(n_theta):  #
        norm = np.linalg.norm(s_res[t])
        if norm > 1e-8:
            s_res[t] = s_res[t] / norm

    s_base = s_base_px / hn
    p_tilde = p_tilde_px / hn

    return SequenceEllipseFeatures(
        n_theta=n_theta,
        half_norm=hn,
        s_base=s_base.astype(np.float32),
        p_tilde=p_tilde.astype(np.float32),
        s_res=s_res.astype(np.float32),
        s_res_phys=s_res_phys.astype(np.float32),
        interp_flag=interp_flag.astype(np.float32),
        theta_ideal=theta_ideal.astype(np.float32),
        theta_actual=theta_actual.astype(np.float32),
        s_base_px=s_base_px.astype(np.float32),
        p_tilde_px=p_tilde_px.astype(np.float32),
    )


def extract_ellipse_features(
    skeleton: PathLike,
    n_theta: int = DEFAULT_N_THETA,
    half_norm: Optional[float] = DEFAULT_HALF_NORM,
    tau_normalize: bool = True,
    compute_fft: bool = True,
    n_segments: int = DEFAULT_N_SEGMENTS,
) -> EllipseFeatureBundle:
    """
    从骨架图提取全局 + 序列椭圆特征,可选 §4.2 FFT 幅值。

    Parameters
    ----------
    skeleton : 路径或 H×W 二值/灰度数组(前景为笔画)
    compute_fft : 是否计算分窗段级与整圆 FFT 幅值(CPU numpy)
    n_segments : 段数 M,须整除 n_theta
    """
    mask = _load_skeleton(skeleton)
    hn = half_norm if half_norm is not None else mask.shape[1] / 2.0
    points = _skeleton_pixels(mask)
    cx, cy, a, b, theta_deg = fit_ellipse_least_squares(points)
    e_quad = _quadrant_error(points, cx, cy, a, b, theta_deg)

    g = extract_global_features(cx, cy, a, b, theta_deg, e_quad, hn)
    seq = extract_sequence_features(
        mask, cx, cy, a, b, theta_deg, n_theta, hn, tau_normalize
    )
    ellipse_cv = ((cx, cy), (2.0 * a, 2.0 * b), theta_deg)
    spectral = None
    if compute_fft:
        spectral = compute_spectral_fft_features(seq.s_res, n_segments=n_segments)
    return EllipseFeatureBundle(
        global_feat=g,
        sequence=seq,
        ellipse_cv=ellipse_cv,
        spectral=spectral,
    )


def visualize_sequence_on_ellipse(
    rgb_bgr: np.ndarray,
    bundle: EllipseFeatureBundle,
    out_path: Optional[str] = None,
    step: int = 15,
) -> np.ndarray:
    """
    在拟合椭圆轨迹采样位置上叠加 S_res 四维序列(颜色+数值标注)。
    返回 BGR 拼图(上:原图+椭圆+采样点;下:Δr/Δθ/κ/τ 四通道着色)。
    """
    h, w = rgb_bgr.shape[:2]
    base = rgb_bgr.copy()
    seq = bundle.sequence
    cx, cy, a, b, theta_deg, _ = bundle.global_feat.raw

    center, axes, ang = bundle.ellipse_cv
    box = cv2.boxPoints(((center[0], center[1]), axes, ang))
    cv2.polylines(base, [np.int32(box)], True, (0, 200, 0), 1, cv2.LINE_AA)

    names = [
        "d_r (S_res[0])",
        "d_th (S_res[1])",
        "kappa (S_res[2])",
        "tau (S_res[3])",
    ]
    panels = []
    for ch in range(4):
        panel = base.copy()
        vals = seq.s_res[:, ch]
        vmin, vmax = float(np.percentile(vals, 5)), float(np.percentile(vals, 95))
        if vmax - vmin < 1e-6:
            vmax = vmin + 1e-6
        for t in range(0, seq.n_theta, max(1, step)):
            x, y = int(seq.s_base_px[t, 0]), int(seq.s_base_px[t, 1])
            if not (0 <= x < w and 0 <= y < h):
                continue
            u = (vals[t] - vmin) / (vmax - vmin)
            u = float(np.clip(u, 0, 1))
            color = _scalar_to_bgr(u)
            cv2.circle(panel, (x, y), 4, color, -1, cv2.LINE_AA)
            if t % (step * 2) == 0:
                txt = f"{vals[t]:+.2f}" if ch < 2 else f"{vals[t]:.2f}"
                cv2.putText(
                    panel,
                    txt,
                    (x + 5, y - 3),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.28,
                    (20, 20, 20),
                    1,
                    cv2.LINE_AA,
                )
        # title bar
        bar = np.full((22, w, 3), 240, np.uint8)
        cv2.putText(
            bar,
            names[ch],
            (6, 16),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.45,
            (30, 30, 30),
            1,
            cv2.LINE_AA,
        )
        panels.append(np.vstack([bar, panel]))

    top_bar = np.full((26, w, 3), 245, np.uint8)
    g = bundle.global_feat
    info = (
        f"G: cx={g.c_x:.3f} cy={g.c_y:.3f} a={g.a:.3f} b={g.b:.3f} "
        f"th={g.theta:.4f}(deg/360) Eq={g.e_quad:.3f}"
    )
    cv2.putText(
        top_bar,
        info,
        (4, 17),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.38,
        (10, 10, 10),
        1,
        cv2.LINE_AA,
    )
    top = np.vstack([top_bar, base])
    row1 = top
    row2 = np.hstack(panels[:2])
    row3 = np.hstack(panels[2:])
    # 对齐宽度
    target_w = max(row1.shape[1], row2.shape[1], row3.shape[1])

    def _pad_w(img: np.ndarray) -> np.ndarray:
        if img.shape[1] == target_w:
            return img
        pad = np.full((img.shape[0], target_w - img.shape[1], 3), 255, np.uint8)
        return np.hstack([img, pad])

    mosaic = np.vstack([_pad_w(row1), _pad_w(row2), _pad_w(row3)])
    if out_path:
        cv2.imwrite(out_path, mosaic)
    return mosaic


def _scalar_to_bgr(u: float) -> Tuple[int, int, int]:
    """简易蓝→红渐变。"""
    b = int(255 * (1 - u))
    r = int(255 * u)
    return (b, 80, r)


def run_random_batch_test(
    image_dir: str,
    n_samples: int = 10,
    seed: int = 42,
    out_dir: str = "outputs/ellipse_feat_test",
    img_size: int = DEFAULT_IMG_SIZE,
) -> None:
    import csv
    import random
    from pathlib import Path

    root = Path(image_dir)
    paths = sorted(root.glob("*.png")) + sorted(root.glob("*.jpg"))
    if not paths:
        raise FileNotFoundError(f"No images under {image_dir}")

    random.seed(seed)
    picks = random.sample(paths, min(n_samples, len(paths)))
    out_root = Path(out_dir)
    out_root.mkdir(parents=True, exist_ok=True)

    rows = []
    print(f"theta normalization: degrees / {THETA_NORM_DIVISOR} "
          f"(equivalent to radians / 2pi); half_norm = width/2 = {img_size/2}")

    for p in picks:
        name = p.stem
        try:
            prep = preprocess_rgb_to_skeleton(str(p), size=img_size)
            bundle = extract_ellipse_features(
                prep["skeleton"],
                half_norm=img_size / 2.0,
            )
            g = bundle.global_feat
            vec = g.as_vector()

            # 保存预处理中间结果
            cv2.imwrite(str(out_root / f"{name}_rgb.png"), prep["rgb"])
            cv2.imwrite(str(out_root / f"{name}_fg.png"), prep["foreground"])
            skel_vis = (prep["skeleton"] * 255).astype(np.uint8)
            cv2.imwrite(str(out_root / f"{name}_skel.png"), skel_vis)

            overlay_path = str(out_root / f"{name}_seq_overlay.png")
            visualize_sequence_on_ellipse(prep["rgb"], bundle, overlay_path)

            save_kw = dict(
                g_global=vec,
                s_res=bundle.sequence.s_res,
                s_base=bundle.sequence.s_base,
                p_tilde=bundle.sequence.p_tilde,
                theta_ideal=bundle.sequence.theta_ideal,
                theta_actual=bundle.sequence.theta_actual,
                interp_flag=bundle.sequence.interp_flag,
            )
            if bundle.spectral is not None:
                sp = bundle.spectral
                save_kw["amp_segment"] = sp.amp_segment
                save_kw["amp_global"] = sp.amp_global
            np.savez(str(out_root / f"{name}_features.npz"), **save_kw)

            row = {
                "file": p.name,
                "cx_n": g.c_x,
                "cy_n": g.c_y,
                "a_n": g.a,
                "b_n": g.b,
                "theta_n": g.theta,
                "e_quad_n": g.e_quad,
                "theta_deg_raw": g.raw[4],
            }
            rows.append(row)

            print(f"\n[{name}] G_global (normalized): {vec}")
            print(
                f"  raw deg={g.raw[4]:.2f} -> theta_n={g.theta:.6f} "
                f"(/{THETA_NORM_DIVISOR}); half_norm={img_size/2}"
            )
            print(f"  saved: {overlay_path}")
        except Exception as e:
            print(f"[{name}] FAILED: {e}")

    if rows:
        csv_path = out_root / "global_features_summary.csv"
        with open(csv_path, "w", newline="", encoding="utf-8") as f:
            w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
            w.writeheader()
            w.writerows(rows)
        print(f"\nCSV: {csv_path}")


def _demo_visualize(
    skeleton_path: str,
    bundle: EllipseFeatureBundle,
    out_path: Optional[str] = None,
) -> None:
    """叠加显示拟合椭圆与角向采样点。"""
    mask = _load_skeleton(skeleton_path)
    vis = cv2.cvtColor((mask * 255).astype(np.uint8), cv2.COLOR_GRAY2BGR)
    cx, cy = bundle.global_feat.raw[0], bundle.global_feat.raw[1]
    a, b = bundle.global_feat.raw[2], bundle.global_feat.raw[3]
    theta_deg = bundle.global_feat.raw[4]
    center, axes, ang = bundle.ellipse_cv
    box = cv2.boxPoints(((center[0], center[1]), axes, ang))
    box = np.int32(box)
    cv2.polylines(vis, [box], True, (0, 255, 0), 1)
    seq = bundle.sequence
    for t in range(0, seq.n_theta, 8):
        x, y = seq.s_base_px[t]
        cv2.circle(vis, (int(x), int(y)), 2, (0, 0, 255), -1)
        tx, ty = seq.p_tilde_px[t]
        cv2.circle(vis, (int(tx), int(ty)), 1, (255, 0, 0), -1)
    if out_path:
        cv2.imwrite(out_path, vis)
    else:
        cv2.imshow("ellipse_features", vis)
        cv2.waitKey(0)
        cv2.destroyAllWindows()


def main(
    skeleton: Optional[str] = None,
    *,
    batch_test: bool = True,
    n_theta: int = DEFAULT_N_THETA,
    half_norm: Optional[float] = None,
    viz: bool = False,
    out_viz: Optional[str] = None,
    image_dir: str = "data/images",
    n: int = 3,
    seed: int = 42,
    out_dir: str = "outputs/ellipse_feat_test",
) -> None:
    """
    默认 batch_test=True、n=3,便于 PyCharm 直接 Debug 本文件(无需配置命令行参数)。

    单张图调试示例:main(skeleton="data/images/00001.png", batch_test=False, viz=True)
    """
    if batch_test:
        run_random_batch_test(
            image_dir,
            n_samples=n,
            seed=seed,
            out_dir=out_dir,
        )
        return

    img_path = skeleton or "data/images/00001.png"
    prep = preprocess_rgb_to_skeleton(img_path)
    bundle = extract_ellipse_features(
        prep["skeleton"],
        n_theta=n_theta,
        half_norm=half_norm if half_norm is not None else prep["size"] / 2.0,
    )
    g = bundle.global_feat
    s = bundle.sequence

    print("=== G_global (normalized) ===")
    print(f"  c_x={g.c_x:.6f}, c_y={g.c_y:.6f}, a={g.a:.6f}, b={g.b:.6f}")
    print(f"  theta={g.theta:.6f} (deg/{THETA_NORM_DIVISOR}), E_quad={g.e_quad:.6f}")
    print("  vector:", g.as_vector())

    print("\n=== Sequence shapes ===")
    print(f"  S_base {s.s_base.shape}, p_tilde {s.p_tilde.shape}, S_res {s.s_res.shape}")
    print(f"  theta_ideal {s.theta_ideal.shape}, theta_actual {s.theta_actual.shape}")
    print(f"  S_man_raw (extended) {s.s_man_raw().shape}")

    if bundle.spectral is not None:
        sp = bundle.spectral
        print("\n=== §4.2 FFT (elipse_feature) ===")
        print(f"  amp_segment {sp.amp_segment.shape} (M, s_l, 4)")
        print(f"  amp_global  {sp.amp_global.shape} (N_theta/2, 4)")

    print("\n=== S_res sample (t=0,90,180,270) ===")
    for t in (0, 90, 180, 270):
        print(
            f"  t={t:3d}: d_r={s.s_res[t,0]:+.4f}, d_th={s.s_res[t,1]:+.4f}, "
            f"k={s.s_res[t,2]:.4f}, tau={s.s_res[t,3]:.4f}"
        )

    overlay = out_viz or str(Path(out_dir) / f"{Path(img_path).stem}_seq_overlay.png")
    if viz or out_viz:
        Path(out_dir).mkdir(parents=True, exist_ok=True)
        visualize_sequence_on_ellipse(prep["rgb"], bundle, overlay)
        print(f"  saved overlay: {overlay}")


def _parse_cli() -> dict:
    parser = argparse.ArgumentParser(description="Extract ellipse geometry features from skeleton.")
    parser.add_argument(
        "skeleton",
        nargs="?",
        default=None,
        help="Path to skeleton/RGB image; omit with --batch-test",
    )
    parser.add_argument("--n-theta", type=int, default=DEFAULT_N_THETA)
    parser.add_argument("--half-norm", type=float, default=None)
    parser.add_argument("--viz", action="store_true", help="Save/show overlay visualization")
    parser.add_argument("--out-viz", type=str, default=None)
    parser.add_argument(
        "--batch-test",
        action="store_true",
        help="Random images from data/images with full preprocess+viz",
    )
    parser.add_argument("--image-dir", type=str, default="data/images")
    parser.add_argument("--n", type=int, default=10)
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--out-dir", type=str, default="outputs/ellipse_feat_test")
    args = parser.parse_args()
    if not args.batch_test and not args.skeleton:
        parser.error("skeleton path required unless --batch-test")
    return {
        "skeleton": args.skeleton,
        "batch_test": args.batch_test,
        "n_theta": args.n_theta,
        "half_norm": args.half_norm,
        "viz": args.viz,
        "out_viz": args.out_viz,
        "image_dir": args.image_dir,
        "n": args.n,
        "seed": args.seed,
        "out_dir": args.out_dir,
    }


if __name__ == "__main__":
    import sys

    if len(sys.argv) > 1:
        main(**_parse_cli())
    else:
        main()