oarust 0.20.1

GPU-first semantic computing runtime written in Rust
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
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
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
//! Host-side geometric estimation: homography, epipolar geometry, camera
//! pose, lens distortion, and calibration.
//!
//! Mirrors the `calib3d` estimator family: `findHomography`, `solvePnP`
//! and `solvePnPRansac`, `findFundamentalMat`/`findEssentialMat`,
//! `recoverPose`, `triangulatePoints`, `Rodrigues`, `initUndistortRectifyMap`,
//! `undistortPoints`, `calibrateCamera`, and `projectPoints`.
//!
//! ## Host/GPU split contract
//!
//! All estimation math runs on the host in f64 and returns plain F32
//! [`Matrix`] values, so no op here records device work and results compose
//! inside captured plans. Raster work stays device-side: a homography feeds
//! [`crate::image::warp_perspective`] and an undistortion map feeds
//! [`crate::image::remap`]. Robust sampling is seeded through the donor PCG32
//! sequence, so a nonzero `seed` reproduces the same consensus set on every
//! call.

use crate::linalg::{damped_least_squares, jacobi_eigen, smallest_right_singular};
use crate::matrix::Matrix;
use crate::{Error, Result};

/// Robust estimator selection shared by the correspondence ops.
///
/// `LeastSquares` consumes every point through the direct linear solver.
/// `Ransac` draws seeded minimal samples, keeps the largest consensus set
/// under the reprojection threshold, then refits on its inliers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EstimationMethod {
	LeastSquares,
	Ransac,
}

/// `find_homography` output: the 3x3 transform and the per-point inlier mask
/// (`1.0` inlier, `0.0` outlier; all ones under `LeastSquares`).
pub struct HomographyResult {
	pub matrix: Matrix,
	pub mask: Matrix,
}

/// `solve_pnp` output: rotation as a Rodrigues vector `[3, 1]` and the camera
/// translation `[3, 1]`.
pub struct PnpResult {
	pub rotation_vector: Matrix,
	pub translation_vector: Matrix,
}

/// `solve_pnp_ransac` output: pose plus the per-point inlier mask.
pub struct PnpRansacResult {
	pub rotation_vector: Matrix,
	pub translation_vector: Matrix,
	pub mask: Matrix,
}

/// `find_fundamental`/`find_essential` output: the 3x3 matrix and inlier mask.
pub struct EpipolarResult {
	pub matrix: Matrix,
	pub mask: Matrix,
}

/// `recover_pose` output: camera-2 relative rotation `[3, 3]`, unit
/// translation `[3, 1]`, and the per-point cheirality mask.
pub struct RecoverPoseResult {
	pub rotation: Matrix,
	pub translation: Matrix,
	pub mask: Matrix,
}

/// `calibrate_camera` output: intrinsic matrix `[3, 3]`, distortion
/// coefficients `[5, 1]` ordered `(k1, k2, p1, p2, k3)`, per-view rotation
/// vectors `[V, 3]`, per-view translations `[V, 3]`, and scalar RMS
/// reprojection error.
pub struct CalibrateCameraResult {
	pub camera: Matrix,
	pub distortion: Matrix,
	pub rotations: Matrix,
	pub translations: Matrix,
	pub rms_error: Matrix,
}

// ---- public operations -----------------------------------------------------

/// Estimate the 3x3 homography mapping `source` `[x, y]` points to
/// `destination` points. `LeastSquares` runs normalized DLT over all points;
/// `Ransac` draws `max_iterations` seeded 4-point samples, keeps the largest
/// consensus set under `ransac_threshold` symmetric transfer error (pixels),
/// and refits on its inliers. Both inputs are `[N, 2]` F32 with `N >= 4`.
///
/// # Errors
///
/// Returns an error for mismatched/degenerate inputs, a nonpositive
/// threshold or iteration bound, or when no nonsingular model exists.
pub fn find_homography(
	source: &Matrix,
	destination: &Matrix,
	method: EstimationMethod,
	ransac_threshold: f32,
	max_iterations: i32,
	seed: u64,
) -> Result<HomographyResult> {
	let source = read_points2(source, 4, "find_homography")?;
	let destination = read_points2(destination, 4, "find_homography")?;
	if source.len() != destination.len() {
		return Err(Error::invalid_argument(
			"find_homography source and destination must share point count",
		));
	}
	match method {
		EstimationMethod::LeastSquares => {
			let h = dlt_homography(&source, &destination).ok_or_else(|| degenerate("find_homography"))?;
			Ok(HomographyResult {
				matrix: mat3_f32(h)?,
				mask: ones_mask(source.len())?,
			})
		}
		EstimationMethod::Ransac => {
			if !ransac_threshold.is_finite() || ransac_threshold <= 0.0 {
				return Err(Error::invalid_argument(
					"find_homography ransac_threshold must be positive and finite",
				));
			}
			if max_iterations < 1 {
				return Err(Error::invalid_argument(
					"find_homography max_iterations must be positive",
				));
			}
			let n = source.len();
			let threshold = ransac_threshold as f64;
			let inliers = ransac_consensus(4, n, max_iterations as usize, seed, |sample| {
				let src: Vec<[f64; 2]> = sample.iter().map(|i| source[*i]).collect();
				let dst: Vec<[f64; 2]> = sample.iter().map(|i| destination[*i]).collect();
				dlt_homography(&src, &dst).map(|h| {
					inlier_indices(n, |i| {
						homography_error(&h, &source[i], &destination[i]) <= threshold
					})
				})
			})
			.ok_or_else(|| degenerate("find_homography"))?;
			let src: Vec<[f64; 2]> = inliers.iter().map(|i| source[*i]).collect();
			let dst: Vec<[f64; 2]> = inliers.iter().map(|i| destination[*i]).collect();
			let h = dlt_homography(&src, &dst).ok_or_else(|| degenerate("find_homography"))?;
			Ok(HomographyResult {
				matrix: mat3_f32(h)?,
				mask: index_mask(n, &inliers)?,
			})
		}
	}
}

/// Solve camera pose from `object` `[x, y, z]` world points and `image`
/// `[u, v]` projections under a `camera` `[3, 3]` intrinsic matrix. Runs a
/// normalized DLT pose estimate refined by damped Gauss-Newton on the
/// reprojection residuals, matching `solvePnP` `SOLVEPNP_ITERATIVE`.
/// `object` is `[N, 3]` and `image` `[N, 2]` F32 with `N >= 6`.
///
/// # Errors
///
/// Returns an error for degenerate inputs, a singular camera matrix, or
/// failure to initialize a nonsingular pose.
pub fn solve_pnp(object: &Matrix, image: &Matrix, camera: &Matrix) -> Result<PnpResult> {
	let object = read_points3(object, 4, "solve_pnp")?;
	let image = read_points2(image, 4, "solve_pnp")?;
	if object.len() != image.len() {
		return Err(Error::invalid_argument(
			"solve_pnp object and image must share point count",
		));
	}
	let k = read_mat3(camera, "solve_pnp")?;
	let pose = dlt_pose(&object, &image, &k).ok_or_else(|| degenerate("solve_pnp"))?;
	let pose = refine_pose(&object, &image, &k, pose).ok_or_else(|| degenerate("solve_pnp"))?;
	Ok(PnpResult {
		rotation_vector: vector_f32(&rodrigues_vector(&pose.0))?,
		translation_vector: vector_f32(&[pose.1[0], pose.1[1], pose.1[2]])?,
	})
}

/// Robust `solve_pnp`: draws `iterations` seeded 6-point samples, keeps the
/// largest consensus set under `reprojection_threshold` (pixels), refits by
/// damped Gauss-Newton over the inliers, and returns the inlier mask.
/// `SOLVEPNP_ITERATIVE`-style scoring matches `solvePnPRansac`.
///
/// # Errors
///
/// Returns an error for degenerate inputs, nonpositive bounds, or when no
/// sample yields a nonsingular pose.
pub fn solve_pnp_ransac(
	object: &Matrix,
	image: &Matrix,
	camera: &Matrix,
	reprojection_threshold: f32,
	max_iterations: i32,
	seed: u64,
) -> Result<PnpRansacResult> {
	let object = read_points3(object, 6, "solve_pnp_ransac")?;
	let image = read_points2(image, 6, "solve_pnp_ransac")?;
	if object.len() != image.len() {
		return Err(Error::invalid_argument(
			"solve_pnp_ransac object and image must share point count",
		));
	}
	if !reprojection_threshold.is_finite() || reprojection_threshold <= 0.0 {
		return Err(Error::invalid_argument(
			"solve_pnp_ransac reprojection_threshold must be positive and finite",
		));
	}
	if max_iterations < 1 {
		return Err(Error::invalid_argument(
			"solve_pnp_ransac max_iterations must be positive",
		));
	}
	let k = read_mat3(camera, "solve_pnp_ransac")?;
	let n = object.len();
	let threshold = reprojection_threshold as f64;
	let inliers = ransac_consensus(6, n, max_iterations as usize, seed, |sample| {
		let obj: Vec<[f64; 3]> = sample.iter().map(|i| object[*i]).collect();
		let img: Vec<[f64; 2]> = sample.iter().map(|i| image[*i]).collect();
		dlt_pose(&obj, &img, &k)
			.and_then(|pose| refine_pose(&obj, &img, &k, pose))
			.map(|pose| {
				inlier_indices(n, |i| {
					reprojection_error(&pose, &k, &object[i], &image[i]) <= threshold
				})
			})
	})
	.ok_or_else(|| degenerate("solve_pnp_ransac"))?;
	let obj: Vec<[f64; 3]> = inliers.iter().map(|i| object[*i]).collect();
	let img: Vec<[f64; 2]> = inliers.iter().map(|i| image[*i]).collect();
	let pose = dlt_pose(&obj, &img, &k)
		.and_then(|pose| refine_pose(&obj, &img, &k, pose))
		.ok_or_else(|| degenerate("solve_pnp_ransac"))?;
	Ok(PnpRansacResult {
		rotation_vector: vector_f32(&rodrigues_vector(&pose.0))?,
		translation_vector: vector_f32(&[pose.1[0], pose.1[1], pose.1[2]])?,
		mask: index_mask(n, &inliers)?,
	})
}

/// Convert between a `[3, 1]` Rodrigues rotation vector and a `[3, 3]`
/// rotation matrix, matching `Rodrigues` in both directions.
///
/// # Errors
///
/// Returns an error unless the input is a `[3, 1]`/`[1, 3]` or `[3, 3]` F32
/// matrix.
pub fn rodrigues(input: &Matrix) -> Result<Matrix> {
	let shape = input.shape();
	let values = input.read_f32()?;
	if shape == [3, 1].as_slice() || shape == [1, 3].as_slice() {
		let r = [values[0] as f64, values[1] as f64, values[2] as f64];
		let rotation = rodrigues_matrix(&r);
		mat3_f32(rotation)
	} else if shape == [3, 3].as_slice() {
		let m = read_mat3(input, "rodrigues")?;
		vector_f32(&rodrigues_vector(&m))
	} else {
		Err(Error::invalid_argument(
			"rodrigues requires a [3, 1]/[1, 3] rotation vector or [3, 3] rotation matrix",
		))
	}
}

/// Project `object` `[x, y, z]` points through `rvec`, `tvec`, `camera`, and
/// `distortion` coefficients `(k1, k2, p1, p2[, k3[, k4..]])`, matching
/// `projectPoints` with the radial+tangential model. `object` is `[N, 3]`,
/// `distortion` a `[k]`/`[k, 1]`/`[1, k]` vector (empty admitted), and the
/// output is `[N, 2]` F32.
///
/// # Errors
///
/// Returns an error for malformed inputs, a non-finite projection, or a
/// zero/negative optical-center denominator.
pub fn project_points(
	object: &Matrix,
	rvec: &Matrix,
	tvec: &Matrix,
	camera: &Matrix,
	distortion: &Matrix,
) -> Result<Matrix> {
	let object = read_points3(object, 1, "project_points")?;
	let r = read_vector(rvec, 3, "project_points")?;
	let t = read_vector(tvec, 3, "project_points")?;
	let k = read_mat3(camera, "project_points")?;
	let distortion = read_vector_free(distortion, "project_points")?;
	let rotation = rodrigues_matrix(&[r[0], r[1], r[2]]);
	let mut projected = Vec::with_capacity(object.len() * 2);
	for point in &object {
		let [x, y, z] = camera_project(&rotation, &[t[0], t[1], t[2]], point);
		if z.abs() < 1e-12 {
			return Err(Error::invalid_argument(
				"project_points produced a point at the camera plane",
			));
		}
		let (xn, yn) = distort_normalized(x / z, y / z, &distortion);
		projected.push((k[0][0] * xn + k[0][1] * yn + k[0][2]) as f32);
		projected.push((k[1][0] * xn + k[1][1] * yn + k[1][2]) as f32);
	}
	Matrix::new_from_f32([object.len(), 2], &projected)
}

/// Build the forward/undistort rectification coordinate map matching
/// `initUndistortRectifyMap` for a `width`x`height` output, returned as the
/// `[1, 2, height, width]` F32 map consumed by [`crate::image::remap`].
/// `rectification` is an optional `[3, 3]` rectifying rotation and
/// `projection` an optional `[3, 3]` new camera matrix (defaulting to the
/// input camera).
///
/// # Errors
///
/// Returns an error for malformed matrices or nonpositive dimensions.
pub fn undistort_rectify_map(
	camera: &Matrix,
	distortion: &Matrix,
	rectification: Option<&Matrix>,
	projection: Option<&Matrix>,
	width: i32,
	height: i32,
) -> Result<Matrix> {
	if width < 1 || height < 1 {
		return Err(Error::invalid_argument(
			"undistort_rectify_map requires positive width and height",
		));
	}
	let k = read_mat3(camera, "undistort_rectify_map")?;
	let distortion = read_vector_free(distortion, "undistort_rectify_map")?;
	let rotation = match rectification {
		Some(matrix) => read_mat3(matrix, "undistort_rectify_map")?,
		None => IDENTITY3,
	};
	let projection = match projection {
		Some(matrix) => read_mat3(matrix, "undistort_rectify_map")?,
		None => k,
	};
	let pinv = invert3(projection)
		.ok_or_else(|| Error::invalid_argument("undistort_rectify_map projection is singular"))?;
	let (w, h) = (width as usize, height as usize);
	let mut map = vec![0.0f32; 2 * w * h];
	for v in 0..h {
		for u in 0..w {
			// Output pixel -> normalized ideal coordinates through R^-1.
			let pin = [
				pinv[0][0] * u as f64 + pinv[0][1] * v as f64 + pinv[0][2],
				pinv[1][0] * u as f64 + pinv[1][1] * v as f64 + pinv[1][2],
				pinv[2][0] * u as f64 + pinv[2][1] * v as f64 + pinv[2][2],
			];
			let ideal = [
				rotation[0][0] * pin[0] + rotation[0][1] * pin[1] + rotation[0][2] * pin[2],
				rotation[1][0] * pin[0] + rotation[1][1] * pin[1] + rotation[1][2] * pin[2],
				rotation[2][0] * pin[0] + rotation[2][1] * pin[1] + rotation[2][2] * pin[2],
			];
			if ideal[2].abs() < 1e-12 {
				return Err(Error::invalid_argument(
					"undistort_rectify_map rectification produced a point at the plane",
				));
			}
			let (xd, yd) = distort_normalized(ideal[0] / ideal[2], ideal[1] / ideal[2], &distortion);
			map[v * w + u] = (k[0][0] * xd + k[0][1] * yd + k[0][2]) as f32;
			map[w * h + v * w + u] = (k[1][0] * xd + k[1][1] * yd + k[1][2]) as f32;
		}
	}
	Matrix::new_from_f32([1, 2, h, w], &map)
}

/// Undistort `[u, v]` `points` by iterative inversion of the distortion
/// model, matching `undistortPoints`. Without `projection` the result is
/// normalized camera coordinates; with `projection` (and optional
/// `rectification`) it is reprojected pixel coordinates.
///
/// # Errors
///
/// Returns an error for malformed matrices or a singular camera/rectification.
pub fn undistort_points(
	points: &Matrix,
	camera: &Matrix,
	distortion: &Matrix,
	rectification: Option<&Matrix>,
	projection: Option<&Matrix>,
) -> Result<Matrix> {
	let points = read_points2(points, 1, "undistort_points")?;
	let k = read_mat3(camera, "undistort_points")?;
	let distortion = read_vector_free(distortion, "undistort_points")?;
	let kinv =
		invert3(k).ok_or_else(|| Error::invalid_argument("undistort_points camera is singular"))?;
	let rotation = match rectification {
		Some(matrix) => read_mat3(matrix, "undistort_points")?,
		None => IDENTITY3,
	};
	let mut output = Vec::with_capacity(points.len() * 2);
	for &[u, v] in &points {
		let mut x = kinv[0][0] * u + kinv[0][1] * v + kinv[0][2];
		let mut y = kinv[1][0] * u + kinv[1][1] * v + kinv[1][2];
		let w_hom = kinv[2][0] * u + kinv[2][1] * v + kinv[2][2];
		if w_hom.abs() < 1e-12 {
			return Err(Error::invalid_argument(
				"undistort_points camera produced a point at infinity",
			));
		}
		x /= w_hom;
		y /= w_hom;
		// Fixed-point inversion of the forward distortion, OpenCV's iteration:
		// the distorted source stays fixed while the iterate's r^2 feeds the
		// radial divisor.
		let (x0, y0) = (x, y);
		let (k1, k2t, p1, p2, k3) = distortion_terms(&distortion);
		for _ in 0..8 {
			let r2 = x * x + y * y;
			let icd = 1.0 / (1.0 + k1 * r2 + k2t * r2 * r2 + k3 * r2 * r2 * r2);
			let delta_x = 2.0 * p1 * x * y + p2 * (r2 + 2.0 * x * x);
			let delta_y = p1 * (r2 + 2.0 * y * y) + 2.0 * p2 * x * y;
			x = (x0 - delta_x) * icd;
			y = (y0 - delta_y) * icd;
		}
		let rx = rotation[0][0] * x + rotation[0][1] * y + rotation[0][2];
		let ry = rotation[1][0] * x + rotation[1][1] * y + rotation[1][2];
		let rz = rotation[2][0] * x + rotation[2][1] * y + rotation[2][2];
		let (mut ox, mut oy) = (rx, ry);
		if let Some(projection_matrix) = projection {
			let p = read_mat3(projection_matrix, "undistort_points")?;
			if rz.abs() < 1e-12 {
				return Err(Error::invalid_argument(
					"undistort_points rectification produced a point at the plane",
				));
			}
			ox = p[0][0] * rx / rz + p[0][1] * ry / rz + p[0][2];
			oy = p[1][0] * rx / rz + p[1][1] * ry / rz + p[1][2];
		}
		output.push(ox as f32);
		output.push(oy as f32);
	}
	Matrix::new_from_f32([points.len(), 2], &output)
}

fn degenerate(operation: &str) -> Error {
	Error::invalid_argument(format!("{operation} point set is degenerate"))
}

// ---- matrix readers ---------------------------------------------------------

const IDENTITY3: [[f64; 3]; 3] = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

fn read_points2(matrix: &Matrix, minimum: usize, operation: &str) -> Result<Vec<[f64; 2]>> {
	let shape = matrix.shape();
	if shape.len() != 2 || shape[1] != 2 || shape[0] < minimum {
		return Err(Error::invalid_argument(format!(
			"{operation} requires an [N, 2] matrix with N >= {minimum}"
		)));
	}
	let values = matrix.read_f32()?;
	Ok(
		values
			.as_chunks::<2>()
			.0
			.iter()
			.map(|pair| [pair[0] as f64, pair[1] as f64])
			.collect(),
	)
}

fn read_points3(matrix: &Matrix, minimum: usize, operation: &str) -> Result<Vec<[f64; 3]>> {
	let shape = matrix.shape();
	if shape.len() != 2 || shape[1] != 3 || shape[0] < minimum {
		return Err(Error::invalid_argument(format!(
			"{operation} requires an [N, 3] matrix with N >= {minimum}"
		)));
	}
	let values = matrix.read_f32()?;
	Ok(
		values
			.as_chunks::<3>()
			.0
			.iter()
			.map(|tri| [tri[0] as f64, tri[1] as f64, tri[2] as f64])
			.collect(),
	)
}

fn read_vector(matrix: &Matrix, expected: usize, operation: &str) -> Result<Vec<f64>> {
	let values = matrix.read_f32()?;
	if values.len() != expected {
		return Err(Error::invalid_argument(format!(
			"{operation} requires a length-{expected} vector"
		)));
	}
	Ok(values.iter().map(|value| *value as f64).collect())
}

fn read_vector_free(matrix: &Matrix, operation: &str) -> Result<Vec<f64>> {
	let shape = matrix.shape();
	if shape.len() > 2 || (shape.len() == 2 && shape[0] != 1 && shape[1] != 1) {
		return Err(Error::invalid_argument(format!(
			"{operation} requires a vector-shaped distortion matrix"
		)));
	}
	let values = matrix.read_f32()?;
	Ok(values.iter().map(|value| *value as f64).collect())
}

fn read_mat3(matrix: &Matrix, operation: &str) -> Result<[[f64; 3]; 3]> {
	if matrix.shape() != [3, 3].as_slice() {
		return Err(Error::invalid_argument(format!(
			"{operation} requires a [3, 3] matrix"
		)));
	}
	let values = matrix.read_f32()?;
	let mut out = [[0.0f64; 3]; 3];
	for row in 0..3 {
		for column in 0..3 {
			out[row][column] = values[row * 3 + column] as f64;
		}
	}
	Ok(out)
}

fn mat3_f32(m: [[f64; 3]; 3]) -> Result<Matrix> {
	let mut values = [0.0f32; 9];
	for row in 0..3 {
		for column in 0..3 {
			values[row * 3 + column] = m[row][column] as f32;
		}
	}
	Matrix::new_from_f32([3, 3], &values)
}

fn vector_f32(values: &[f64]) -> Result<Matrix> {
	let flat: Vec<f32> = values.iter().map(|value| *value as f32).collect();
	Matrix::new_from_f32([values.len(), 1], &flat)
}

fn ones_mask(n: usize) -> Result<Matrix> {
	Matrix::new_from_f32([n, 1], &vec![1.0f32; n])
}

fn index_mask(n: usize, indices: &[usize]) -> Result<Matrix> {
	let mut mask = vec![0.0f32; n];
	for &index in indices {
		mask[index] = 1.0;
	}
	Matrix::new_from_f32([n, 1], &mask)
}

fn inlier_indices(n: usize, predicate: impl Fn(usize) -> bool) -> Vec<usize> {
	(0..n).filter(|&index| predicate(index)).collect()
}

// ---- numeric helpers ---------------------------------------------------------

fn invert3(m: [[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> {
	let det = m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
		- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
		+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
	if det.abs() < 1e-14 {
		return None;
	}
	let inv = 1.0 / det;
	Some([
		[
			(m[1][1] * m[2][2] - m[1][2] * m[2][1]) * inv,
			(m[0][2] * m[2][1] - m[0][1] * m[2][2]) * inv,
			(m[0][1] * m[1][2] - m[0][2] * m[1][1]) * inv,
		],
		[
			(m[1][2] * m[2][0] - m[1][0] * m[2][2]) * inv,
			(m[0][0] * m[2][2] - m[0][2] * m[2][0]) * inv,
			(m[0][2] * m[1][0] - m[0][0] * m[1][2]) * inv,
		],
		[
			(m[1][0] * m[2][1] - m[1][1] * m[2][0]) * inv,
			(m[0][1] * m[2][0] - m[0][0] * m[2][1]) * inv,
			(m[0][0] * m[1][1] - m[0][1] * m[1][0]) * inv,
		],
	])
}

fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
	[
		a[1] * b[2] - a[2] * b[1],
		a[2] * b[0] - a[0] * b[2],
		a[0] * b[1] - a[1] * b[0],
	]
}

fn norm(a: [f64; 3]) -> f64 {
	(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt()
}

fn normalize_vec(a: [f64; 3]) -> [f64; 3] {
	let n = norm(a);
	if n < 1e-14 {
		return a;
	}
	[a[0] / n, a[1] / n, a[2] / n]
}

fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
	a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

fn mul_mat3(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> [[f64; 3]; 3] {
	let mut out = [[0.0f64; 3]; 3];
	for i in 0..3 {
		for j in 0..3 {
			out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
		}
	}
	out
}

fn transpose3(m: [[f64; 3]; 3]) -> [[f64; 3]; 3] {
	[
		[m[0][0], m[1][0], m[2][0]],
		[m[0][1], m[1][1], m[2][1]],
		[m[0][2], m[1][2], m[2][2]],
	]
}

/// 3x3 SVD through the eigenbasis of `m^T m` / `m m^T`, ordered by descending
/// singular value with determinant-fixed orthonormal factors.
#[allow(clippy::type_complexity)]
fn svd3(m: [[f64; 3]; 3]) -> Option<([[f64; 3]; 3], [f64; 3], [[f64; 3]; 3])> {
	let mt = transpose3(m);
	let mtm = mul_mat3(&mt, &m);
	let mmt = mul_mat3(&m, &mt);
	let (vvals, vvec) = jacobi_eigen(&mtm, 3)?;
	let (uvals, uvec) = jacobi_eigen(&mmt, 3)?;
	let mut s = [0.0f64; 3];
	let mut v = [[0.0f64; 3]; 3];
	let mut u = [[0.0f64; 3]; 3];
	for column in 0..3 {
		s[column] = vvals[2 - column].max(0.0).sqrt();
		for row in 0..3 {
			v[row][column] = vvec[row][2 - column];
			u[row][column] = uvec[row][2 - column];
		}
	}
	let _ = uvals;
	// Rebuild U = M V / s for nonzero singular values; orthonormalize the rest.
	for column in 0..3 {
		if s[column] > 1e-10 {
			for row in 0..3 {
				u[row][column] =
					(m[row][0] * v[0][column] + m[row][1] * v[1][column] + m[row][2] * v[2][column])
						/ s[column];
			}
		}
	}
	// Fix degenerate components by orthonormal completion.
	for column in 0..3 {
		let column_vec = [u[0][column], u[1][column], u[2][column]];
		if norm(column_vec) < 0.5 {
			let others: Vec<[f64; 3]> = (0..3)
				.filter(|&c| c != column)
				.map(|c| [u[0][c], u[1][c], u[2][c]])
				.collect();
			let candidate = normalize_vec(cross(others[0], others[1]));
			for (row, ur) in u.iter_mut().enumerate() {
				ur[column] = candidate[row];
			}
		}
	}
	// Determinant signs: U and V must be rotations for pose candidates.
	if det3(&v) < 0.0 {
		for r in v.iter_mut() {
			r[2] = -r[2];
		}
	}
	if det3(&u) < 0.0 {
		for r in u.iter_mut() {
			r[2] = -r[2];
		}
	}
	Some((u, s, transpose3(v)))
}

fn det3(m: &[[f64; 3]; 3]) -> f64 {
	m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
		- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
		+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
}

// ---- homography -----------------------------------------------------------

/// Hartley-normalized DLT homography from at least four correspondences.
fn dlt_homography(source: &[[f64; 2]], destination: &[[f64; 2]]) -> Option<[[f64; 3]; 3]> {
	let (ts, ns) = normalize2d(source)?;
	let (td, nd) = normalize2d(destination)?;
	let n = ns.len();
	let mut a = vec![0.0f64; 2 * n * 9];
	for (index, (&[x, y], &[u, v])) in ns.iter().zip(nd.iter()).enumerate() {
		let row = 2 * index * 9;
		a[row..row + 9].copy_from_slice(&[x, y, 1.0, 0.0, 0.0, 0.0, -u * x, -u * y, -u]);
		a[row + 9..row + 18].copy_from_slice(&[0.0, 0.0, 0.0, x, y, 1.0, -v * x, -v * y, -v]);
	}
	let vectors = smallest_right_singular::<9>(&a, 2 * n)?;
	let mut h = [[0.0f64; 3]; 3];
	for row in 0..3 {
		for column in 0..3 {
			h[row][column] = vectors[row * 3 + column][0];
		}
	}
	// Denormalize: H = Td^-1 * Hn * Ts.
	let td_inv = invert3(td)?;
	let ts_mul = mul_mat3(&h, &ts);
	let full = mul_mat3(&td_inv, &ts_mul);
	let scale = full[2][2];
	if scale.abs() < 1e-14 {
		return None;
	}
	let mut out = [[0.0f64; 3]; 3];
	for row in 0..3 {
		for column in 0..3 {
			out[row][column] = full[row][column] / scale;
		}
	}
	Some(out)
}

/// Similarity normalization used by DLT estimators: centroid + rms scale.
#[allow(clippy::type_complexity)]
fn normalize2d(points: &[[f64; 2]]) -> Option<([[f64; 3]; 3], Vec<[f64; 2]>)> {
	let n = points.len() as f64;
	if n < 1.0 {
		return None;
	}
	let (mut cx, mut cy) = (0.0, 0.0);
	for &[x, y] in points {
		cx += x;
		cy += y;
	}
	cx /= n;
	cy /= n;
	let mut scale = 0.0;
	for &[x, y] in points {
		scale += ((x - cx) * (x - cx) + (y - cy) * (y - cy)).sqrt();
	}
	scale /= n;
	if scale < 1e-14 {
		scale = 1.0;
	}
	let s = 1.0 / scale;
	let transform = [[s, 0.0, -s * cx], [0.0, s, -s * cy], [0.0, 0.0, 1.0]];
	let normalized = points
		.iter()
		.map(|&[x, y]| [(x - cx) * s, (y - cy) * s])
		.collect();
	Some((transform, normalized))
}

/// Symmetric transfer error (pixels) for a homography correspondence.
fn homography_error(h: &[[f64; 3]; 3], source: &[f64; 2], destination: &[f64; 2]) -> f64 {
	let forward = match project_h(h, source) {
		Some([x, y]) => (x - destination[0]).powi(2) + (y - destination[1]).powi(2),
		None => return f64::MAX,
	};
	let backward = match invert3(*h).and_then(|hinv| project_h(&hinv, destination)) {
		Some([x, y]) => (x - source[0]).powi(2) + (y - source[1]).powi(2),
		None => return f64::MAX,
	};
	forward + backward
}

/// Apply a homogeneous transform to a `[x, y]` point.
fn project_h(h: &[[f64; 3]; 3], p: &[f64; 2]) -> Option<[f64; 2]> {
	let x = h[0][0] * p[0] + h[0][1] * p[1] + h[0][2];
	let y = h[1][0] * p[0] + h[1][1] * p[1] + h[1][2];
	let w = h[2][0] * p[0] + h[2][1] * p[1] + h[2][2];
	if w.abs() < 1e-14 {
		return None;
	}
	Some([x / w, y / w])
}

// ---- RANSAC ------------------------------------------------------------------

// Donor: oa/core/std/random.h, Random. Retain its PCG32 sequence=1 and
// two-draw nextU64 order so seeded RANSAC selection matches the donor stream.
struct Pcg32 {
	state: u64,
}

impl Pcg32 {
	fn new(seed: u64) -> Self {
		let mut rng = Self { state: 0 };
		rng.next_u32();
		rng.state = rng.state.wrapping_add(seed);
		rng.next_u32();
		rng
	}

	fn next_u32(&mut self) -> u32 {
		let old = self.state;
		self.state = old.wrapping_mul(6364136223846793005).wrapping_add(3);
		let shifted = (((old >> 18) ^ old) >> 27) as u32;
		shifted.rotate_right((old >> 59) as u32)
	}

	fn next_u64(&mut self) -> u64 {
		(u64::from(self.next_u32()) << 32) | u64::from(self.next_u32())
	}
}

/// Seeded minimal-sample consensus: draw `iterations` `sample_size` subsets,
/// evaluate each through `estimate` (returning the inlier index list for the
/// sampled model), and return the largest consensus set.
fn ransac_consensus(
	sample_size: usize,
	n: usize,
	iterations: usize,
	seed: u64,
	mut estimate: impl FnMut(&[usize]) -> Option<Vec<usize>>,
) -> Option<Vec<usize>> {
	if n < sample_size {
		return None;
	}
	let mut rng = Pcg32::new(seed);
	let mut best: Vec<usize> = Vec::new();
	for _ in 0..iterations {
		let mut sample: Vec<usize> = (0..n).collect();
		for count in (2..=n).rev() {
			if count <= n - sample_size {
				break;
			}
			let range = count as u64;
			let reject = range.wrapping_neg() % range;
			let value = loop {
				let value = rng.next_u64();
				if value >= reject {
					break value;
				}
			};
			sample.swap(count - 1, (value % range) as usize);
		}
		let sample = &sample[n - sample_size..];
		if let Some(inliers) = estimate(sample).filter(|i| i.len() > best.len()) {
			best = inliers;
		}
	}
	if best.is_empty() { None } else { Some(best) }
}

// ---- pose / projection ------------------------------------------------------

/// Camera-frame coordinates of a world point under rotation+translation.
fn camera_project(rotation: &[[f64; 3]; 3], t: &[f64; 3], point: &[f64; 3]) -> [f64; 3] {
	[
		rotation[0][0] * point[0] + rotation[0][1] * point[1] + rotation[0][2] * point[2] + t[0],
		rotation[1][0] * point[0] + rotation[1][1] * point[1] + rotation[1][2] * point[2] + t[1],
		rotation[2][0] * point[0] + rotation[2][1] * point[1] + rotation[2][2] * point[2] + t[2],
	]
}

/// Reprojection pixel error for a pose candidate.
fn reprojection_error(
	pose: &([[f64; 3]; 3], [f64; 3]),
	camera: &[[f64; 3]; 3],
	object: &[f64; 3],
	image: &[f64; 2],
) -> f64 {
	let [x, y, z] = camera_project(&pose.0, &pose.1, object);
	if z.abs() < 1e-12 {
		return f64::MAX;
	}
	let u = (camera[0][0] * x + camera[0][1] * y + camera[0][2] * z) / z;
	let v = (camera[1][0] * x + camera[1][1] * y + camera[1][2] * z) / z;
	let du = u - image[0];
	let dv = v - image[1];
	du * du + dv * dv
}

/// Rodrigues rotation vector -> rotation matrix.
fn rodrigues_matrix(r: &[f64; 3]) -> [[f64; 3]; 3] {
	let theta = norm(*r);
	if theta < 1e-12 {
		return IDENTITY3;
	}
	let k = [r[0] / theta, r[1] / theta, r[2] / theta];
	let (c, s) = (theta.cos(), theta.sin());
	let outer = |i: usize, j: usize| k[i] * k[j] * (1.0 - c);
	let skew = [
		[0.0, -k[2] * s, k[1] * s],
		[k[2] * s, 0.0, -k[0] * s],
		[-k[1] * s, k[0] * s, 0.0],
	];
	let mut out = [[0.0f64; 3]; 3];
	for i in 0..3 {
		for j in 0..3 {
			out[i][j] = (if i == j { c } else { 0.0 }) + outer(i, j) + skew[i][j];
		}
	}
	out
}

/// Rotation matrix -> Rodrigues rotation vector.
fn rodrigues_vector(rotation: &[[f64; 3]; 3]) -> [f64; 3] {
	let trace = rotation[0][0] + rotation[1][1] + rotation[2][2];
	let cosine = ((trace - 1.0) / 2.0).clamp(-1.0, 1.0);
	let theta = cosine.acos();
	if theta < 1e-8 {
		return [0.0, 0.0, 0.0];
	}
	let denominator = 2.0 * theta.sin();
	if denominator.abs() < 1e-8 {
		// Near-pi rotation: recover the axis from the diagonal terms.
		let mut axis = [
			((rotation[0][0] + 1.0) / 2.0).max(0.0).sqrt(),
			((rotation[1][1] + 1.0) / 2.0).max(0.0).sqrt(),
			((rotation[2][2] + 1.0) / 2.0).max(0.0).sqrt(),
		];
		if rotation[0][1] < 0.0 {
			axis[1] = -axis[1];
		}
		if rotation[0][2] < 0.0 {
			axis[2] = -axis[2];
		}
		return normalize_vec(axis).map(|v| v * theta);
	}
	let scale = theta / denominator;
	[
		(rotation[2][1] - rotation[1][2]) * scale,
		(rotation[0][2] - rotation[2][0]) * scale,
		(rotation[1][0] - rotation[0][1]) * scale,
	]
}

/// DLT camera pose: estimate R|t through a 12-parameter homogeneous solve on
/// normalized image coordinates, then orthonormalize the rotation block.
fn dlt_pose(
	object: &[[f64; 3]],
	image: &[[f64; 2]],
	camera: &[[f64; 3]; 3],
) -> Option<([[f64; 3]; 3], [f64; 3])> {
	let kinv = invert3(*camera)?;
	let normalized: Vec<[f64; 2]> = image
		.iter()
		.map(|&[u, v]| {
			let x = kinv[0][0] * u + kinv[0][1] * v + kinv[0][2];
			let y = kinv[1][0] * u + kinv[1][1] * v + kinv[1][2];
			let w = kinv[2][0] * u + kinv[2][1] * v + kinv[2][2];
			[x / w, y / w]
		})
		.collect();
	let n = object.len();
	let mut a = vec![0.0f64; 2 * n * 12];
	for (index, (&point, &[xn, yn])) in object.iter().zip(normalized.iter()).enumerate() {
		let [x, y, z] = point;
		let row = 2 * index * 12;
		a[row..row + 12].copy_from_slice(&[
			x,
			y,
			z,
			1.0,
			0.0,
			0.0,
			0.0,
			0.0,
			-xn * x,
			-xn * y,
			-xn * z,
			-xn,
		]);
		a[row + 12..row + 24].copy_from_slice(&[
			0.0,
			0.0,
			0.0,
			0.0,
			x,
			y,
			z,
			1.0,
			-yn * x,
			-yn * y,
			-yn * z,
			-yn,
		]);
	}
	let vectors = smallest_right_singular::<12>(&a, 2 * n)?;
	let mut rotation = [[0.0f64; 3]; 3];
	let mut translation = [0.0f64; 3];
	for row in 0..3 {
		for column in 0..3 {
			rotation[row][column] = vectors[row * 4 + column][0];
		}
		translation[row] = vectors[row * 4 + 3][0];
	}
	// Orthonormalize: R -> nearest rotation through eigen polar factor.
	let (mut u, _, vt) = svd3(rotation)?;
	let mut proper = mul_mat3(&u, &vt);
	if det3(&proper) < 0.0 {
		for r in u.iter_mut() {
			r[2] = -r[2];
		}
		proper = mul_mat3(&u, &vt);
	}
	let scale = norm([proper[0][0], proper[0][1], proper[0][2]]);
	let _ = scale;
	// Rescale translation to the orthonormalized rotation scale.
	let column_norms: Vec<f64> = (0..3)
		.map(|c| norm([rotation[0][c], rotation[1][c], rotation[2][c]]))
		.collect();
	let mean = (column_norms[0] + column_norms[1] + column_norms[2]) / 3.0;
	if mean < 1e-14 {
		return None;
	}
	let factor = 1.0 / mean;
	for t in translation.iter_mut() {
		*t *= factor;
	}
	// Ensure positive depth: flip the pose when the mean z is negative.
	let mut depth = 0.0;
	for &point in object {
		let p = camera_project(&proper, &translation, &point);
		depth += p[2];
	}
	if depth < 0.0 {
		for (r, t) in proper.iter_mut().zip(translation.iter_mut()) {
			for c in r.iter_mut() {
				*c = -*c;
			}
			*t = -*t;
		}
	}
	Some((proper, translation))
}

/// Damped Gauss-Newton pose refinement over the 6-dof rvec+tvec parameters.
fn refine_pose(
	object: &[[f64; 3]],
	image: &[[f64; 2]],
	camera: &[[f64; 3]; 3],
	pose: ([[f64; 3]; 3], [f64; 3]),
) -> Option<([[f64; 3]; 3], [f64; 3])> {
	let mut params = {
		let r = rodrigues_vector(&pose.0);
		[r[0], r[1], r[2], pose.1[0], pose.1[1], pose.1[2]]
	};
	let residuals = |params: &[f64]| -> Vec<f64> {
		let rotation = rodrigues_matrix(&[params[0], params[1], params[2]]);
		let t = [params[3], params[4], params[5]];
		let mut out = Vec::with_capacity(object.len() * 2);
		for (point, &target) in object.iter().zip(image.iter()) {
			let [x, y, z] = camera_project(&rotation, &[t[0], t[1], t[2]], point);
			let (u, v) = if z.abs() < 1e-9 {
				(f64::MAX * 1e-6, f64::MAX * 1e-6)
			} else {
				(
					(camera[0][0] * x + camera[0][1] * y + camera[0][2] * z) / z,
					(camera[1][0] * x + camera[1][1] * y + camera[1][2] * z) / z,
				)
			};
			out.push(u - target[0]);
			out.push(v - target[1]);
		}
		out
	};
	let n = params.len();
	let mut cost: f64 = residuals(&params).iter().map(|r| r * r).sum();
	for _ in 0..20 {
		let r = residuals(&params);
		let mut jacobian = vec![0.0f64; r.len() * n];
		let eps = 1e-6;
		for column in 0..n {
			let mut shifted = params;
			shifted[column] += eps;
			let shifted_residuals = residuals(&shifted);
			for (row, value) in shifted_residuals.iter().enumerate() {
				jacobian[row * n + column] = (value - r[row]) / eps;
			}
		}
		let step = damped_least_squares(&jacobian, &r, r.len(), n, 1e-3)?;
		let mut improved = false;
		for scale in [1.0, 0.5, 0.25] {
			let mut candidate = params;
			for i in 0..n {
				candidate[i] += step[i] * scale;
			}
			let candidate_cost: f64 = residuals(&candidate).iter().map(|r| r * r).sum();
			if candidate_cost < cost {
				params = candidate;
				cost = candidate_cost;
				improved = true;
				break;
			}
		}
		if !improved {
			break;
		}
	}
	Some((
		rodrigues_matrix(&[params[0], params[1], params[2]]),
		[params[3], params[4], params[5]],
	))
}

// ---- distortion ------------------------------------------------------------

/// Expand distortion coefficients to `(k1, k2, p1, p2, k3)` order.
fn distortion_terms(distortion: &[f64]) -> (f64, f64, f64, f64, f64) {
	let term = |index: usize| distortion.get(index).copied().unwrap_or(0.0);
	(term(0), term(1), term(2), term(3), term(4))
}

/// Forward distortion of normalized coordinates `(x, y)` using the
/// `(k1, k2, p1, p2, k3[, k4, k5, k6])` OpenCV rational model when extra
/// coefficients are present, else the radial+tangential base model.
fn distort_normalized(x: f64, y: f64, distortion: &[f64]) -> (f64, f64) {
	let (k1, k2, p1, p2, k3) = distortion_terms(distortion);
	let r2 = x * x + y * y;
	let radial = if distortion.len() >= 8 {
		let (k4, k5, k6) = (
			distortion.get(5).copied().unwrap_or(0.0),
			distortion.get(6).copied().unwrap_or(0.0),
			distortion.get(7).copied().unwrap_or(0.0),
		);
		(1.0 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2)
			/ (1.0 + k4 * r2 + k5 * r2 * r2 + k6 * r2 * r2 * r2)
	} else {
		1.0 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2
	};
	let dx = 2.0 * p1 * x * y + p2 * (r2 + 2.0 * x * x);
	let dy = p1 * (r2 + 2.0 * y * y) + 2.0 * p2 * x * y;
	(x * radial + dx, y * radial + dy)
}

// ---- epipolar geometry -----------------------------------------------------

/// Estimate the 3x3 fundamental matrix between `points1` and `points2`
/// (`[N, 2]` F32, `N >= 8`), matching `findFundamentalMat`. `LeastSquares`
/// runs the normalized eight-point solve; `Ransac` draws `max_iterations`
/// seeded 8-point minimal samples scored by symmetric epipolar distance
/// under `ransac_threshold` (pixels) and refits on the inlier set. The
/// estimate is enforced to rank 2 before return.
///
/// # Errors
///
/// Returns an error for mismatched/degenerate inputs, nonpositive bounds,
/// or when no rank-2 model exists.
pub fn find_fundamental(
	points1: &Matrix,
	points2: &Matrix,
	method: EstimationMethod,
	ransac_threshold: f32,
	max_iterations: i32,
	seed: u64,
) -> Result<EpipolarResult> {
	let points1 = read_points2(points1, 8, "find_fundamental")?;
	let points2 = read_points2(points2, 8, "find_fundamental")?;
	if points1.len() != points2.len() {
		return Err(Error::invalid_argument(
			"find_fundamental inputs must share point count",
		));
	}
	match method {
		EstimationMethod::LeastSquares => {
			let f = eight_point(&points1, &points2).ok_or_else(|| degenerate("find_fundamental"))?;
			Ok(EpipolarResult {
				matrix: mat3_f32(f)?,
				mask: ones_mask(points1.len())?,
			})
		}
		EstimationMethod::Ransac => {
			if !ransac_threshold.is_finite() || ransac_threshold <= 0.0 {
				return Err(Error::invalid_argument(
					"find_fundamental ransac_threshold must be positive and finite",
				));
			}
			if max_iterations < 1 {
				return Err(Error::invalid_argument(
					"find_fundamental max_iterations must be positive",
				));
			}
			let n = points1.len();
			let threshold = ransac_threshold as f64;
			let inliers = ransac_consensus(8, n, max_iterations as usize, seed, |sample| {
				let p1: Vec<[f64; 2]> = sample.iter().map(|i| points1[*i]).collect();
				let p2: Vec<[f64; 2]> = sample.iter().map(|i| points2[*i]).collect();
				eight_point(&p1, &p2).map(|f| {
					inlier_indices(n, |i| {
						epipolar_error(&f, &points1[i], &points2[i]) <= threshold
					})
				})
			})
			.ok_or_else(|| degenerate("find_fundamental"))?;
			let p1: Vec<[f64; 2]> = inliers.iter().map(|i| points1[*i]).collect();
			let p2: Vec<[f64; 2]> = inliers.iter().map(|i| points2[*i]).collect();
			let f = eight_point(&p1, &p2).ok_or_else(|| degenerate("find_fundamental"))?;
			Ok(EpipolarResult {
				matrix: mat3_f32(f)?,
				mask: index_mask(n, &inliers)?,
			})
		}
	}
}

/// Estimate the 3x3 essential matrix between `points1` and `points2`
/// (`[N, 2]` F32, `N >= 8`) under the `camera` intrinsic matrix, matching
/// `findEssentialMat`. Correspondences are normalized through `K^-1`, then
/// the same normalized eight-point solve applies; the singular values of the
/// result are collapsed to `(s, s, 0)` per the essential constraint.
///
/// # Errors
///
/// Returns an error for mismatched/degenerate inputs, a singular camera, or
/// nonpositive RANSAC bounds.
pub fn find_essential(
	points1: &Matrix,
	points2: &Matrix,
	camera: &Matrix,
	method: EstimationMethod,
	ransac_threshold: f32,
	max_iterations: i32,
	seed: u64,
) -> Result<EpipolarResult> {
	let k = read_mat3(camera, "find_essential")?;
	let kinv =
		invert3(k).ok_or_else(|| Error::invalid_argument("find_essential camera is singular"))?;
	let normalize = |matrix: &Matrix, op: &str| -> Result<Vec<[f64; 2]>> {
		let points = read_points2(matrix, 8, op)?;
		Ok(
			points
				.iter()
				.map(|&[u, v]| {
					let x = kinv[0][0] * u + kinv[0][1] * v + kinv[0][2];
					let y = kinv[1][0] * u + kinv[1][1] * v + kinv[1][2];
					let w = kinv[2][0] * u + kinv[2][1] * v + kinv[2][2];
					[x / w, y / w]
				})
				.collect(),
		)
	};
	let points1 = normalize(points1, "find_essential")?;
	let points2 = normalize(points2, "find_essential")?;
	if points1.len() != points2.len() {
		return Err(Error::invalid_argument(
			"find_essential inputs must share point count",
		));
	}
	// Essential threshold is in normalized-image units.
	match method {
		EstimationMethod::LeastSquares => {
			let e = eight_point(&points1, &points2)
				.and_then(essential_enforce)
				.ok_or_else(|| degenerate("find_essential"))?;
			Ok(EpipolarResult {
				matrix: mat3_f32(e)?,
				mask: ones_mask(points1.len())?,
			})
		}
		EstimationMethod::Ransac => {
			if !ransac_threshold.is_finite() || ransac_threshold <= 0.0 {
				return Err(Error::invalid_argument(
					"find_essential ransac_threshold must be positive and finite",
				));
			}
			if max_iterations < 1 {
				return Err(Error::invalid_argument(
					"find_essential max_iterations must be positive",
				));
			}
			let n = points1.len();
			let threshold = ransac_threshold as f64;
			let inliers = ransac_consensus(8, n, max_iterations as usize, seed, |sample| {
				let p1: Vec<[f64; 2]> = sample.iter().map(|i| points1[*i]).collect();
				let p2: Vec<[f64; 2]> = sample.iter().map(|i| points2[*i]).collect();
				eight_point(&p1, &p2).and_then(essential_enforce).map(|e| {
					inlier_indices(n, |i| {
						epipolar_error(&e, &points1[i], &points2[i]) <= threshold
					})
				})
			})
			.ok_or_else(|| degenerate("find_essential"))?;
			let p1: Vec<[f64; 2]> = inliers.iter().map(|i| points1[*i]).collect();
			let p2: Vec<[f64; 2]> = inliers.iter().map(|i| points2[*i]).collect();
			let e = eight_point(&p1, &p2)
				.and_then(essential_enforce)
				.ok_or_else(|| degenerate("find_essential"))?;
			Ok(EpipolarResult {
				matrix: mat3_f32(e)?,
				mask: index_mask(n, &inliers)?,
			})
		}
	}
}

/// Recover the relative camera pose `(R, t)` between views from an
/// `essential` `[3, 3]` matrix, matching `recoverPose`. `points1`/`points2`
/// are the pixel correspondences and `camera` supplies intrinsics; all four
/// `(±t, R | R^W)` candidates are triangulated and the cheirality-majority
/// candidate wins, with the mask recording in-front points.
///
/// # Errors
///
/// Returns an error for malformed inputs or when no candidate yields
/// positive depth.
#[allow(clippy::type_complexity)]
pub fn recover_pose(
	essential: &Matrix,
	points1: &Matrix,
	points2: &Matrix,
	camera: &Matrix,
) -> Result<RecoverPoseResult> {
	let e = read_mat3(essential, "recover_pose")?;
	let k = read_mat3(camera, "recover_pose")?;
	let kinv =
		invert3(k).ok_or_else(|| Error::invalid_argument("recover_pose camera is singular"))?;
	let points1 = read_points2(points1, 1, "recover_pose")?;
	let points2 = read_points2(points2, 1, "recover_pose")?;
	if points1.len() != points2.len() {
		return Err(Error::invalid_argument(
			"recover_pose inputs must share point count",
		));
	}
	let n = points1.len();
	let normalized = |points: &[[f64; 2]]| -> Vec<[f64; 2]> {
		points
			.iter()
			.map(|&[u, v]| {
				let x = kinv[0][0] * u + kinv[0][1] * v + kinv[0][2];
				let y = kinv[1][0] * u + kinv[1][1] * v + kinv[1][2];
				let w = kinv[2][0] * u + kinv[2][1] * v + kinv[2][2];
				[x / w, y / w]
			})
			.collect()
	};
	let pn1 = normalized(&points1);
	let pn2 = normalized(&points2);
	let (u, _, vt) = svd3(e).ok_or_else(|| degenerate("recover_pose"))?;
	let w = [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]];
	let wt = transpose3(w);
	let mut u_adj = u;
	if det3(&u_adj) < 0.0 {
		for r in u_adj.iter_mut() {
			r[2] = -r[2];
		}
	}
	let mut v_adj = transpose3(vt);
	if det3(&v_adj) < 0.0 {
		for r in v_adj.iter_mut() {
			r[2] = -r[2];
		}
	}
	let r1 = mul_mat3(&mul_mat3(&u_adj, &w), &transpose3(v_adj));
	let r2 = mul_mat3(&mul_mat3(&u_adj, &wt), &transpose3(v_adj));
	let t_pos = [u_adj[0][2], u_adj[1][2], u_adj[2][2]];
	let t_neg = [-t_pos[0], -t_pos[1], -t_pos[2]];
	let p1: [[f64; 4]; 3] = [
		[1.0, 0.0, 0.0, 0.0],
		[0.0, 1.0, 0.0, 0.0],
		[0.0, 0.0, 1.0, 0.0],
	];
	let mut best: Option<([[f64; 3]; 3], [f64; 3], Vec<usize>)> = None;
	for (rotation, t) in [(r1, t_pos), (r1, t_neg), (r2, t_pos), (r2, t_neg)] {
		let p2: [[f64; 4]; 3] = [
			[rotation[0][0], rotation[0][1], rotation[0][2], t[0]],
			[rotation[1][0], rotation[1][1], rotation[1][2], t[1]],
			[rotation[2][0], rotation[2][1], rotation[2][2], t[2]],
		];
		let mut in_front = Vec::new();
		for index in 0..n {
			if let Some([x, y, z, w]) = triangulate_point(&p1, &p2, &pn1[index], &pn2[index]) {
				if w.abs() < 1e-14 {
					continue;
				}
				let point = [x / w, y / w, z / w];
				let depth2 =
					rotation[2][0] * point[0] + rotation[2][1] * point[1] + rotation[2][2] * point[2] + t[2];
				if point[2] > 0.0 && depth2 > 0.0 {
					in_front.push(index);
				}
			}
		}
		if best
			.as_ref()
			.is_none_or(|(_, _, best)| in_front.len() > best.len())
		{
			best = Some((rotation, t, in_front));
		}
	}
	let (rotation, translation, in_front) = best.ok_or_else(|| degenerate("recover_pose"))?;
	if in_front.is_empty() {
		return Err(degenerate("recover_pose"));
	}
	Ok(RecoverPoseResult {
		rotation: mat3_f32(rotation)?,
		translation: vector_f32(&translation)?,
		mask: index_mask(n, &in_front)?,
	})
}

/// Triangulate `points1`/`points2` correspondences under the `projection1` /
/// `projection2` `[3, 4]` camera matrices, returning homogeneous `[N, 4]`
/// points matching `triangulatePoints`.
///
/// # Errors
///
/// Returns an error for malformed inputs or mismatched point counts.
pub fn triangulate_points(
	projection1: &Matrix,
	projection2: &Matrix,
	points1: &Matrix,
	points2: &Matrix,
) -> Result<Matrix> {
	let p1 = read_mat34(projection1, "triangulate_points")?;
	let p2 = read_mat34(projection2, "triangulate_points")?;
	let points1 = read_points2(points1, 1, "triangulate_points")?;
	let points2 = read_points2(points2, 1, "triangulate_points")?;
	if points1.len() != points2.len() {
		return Err(Error::invalid_argument(
			"triangulate_points inputs must share point count",
		));
	}
	let mut output = Vec::with_capacity(points1.len() * 4);
	for index in 0..points1.len() {
		let point = triangulate_point(&p1, &p2, &points1[index], &points2[index])
			.ok_or_else(|| degenerate("triangulate_points"))?;
		output.extend(point.iter().map(|v| *v as f32));
	}
	Matrix::new_from_f32([points1.len(), 4], &output)
}

fn read_mat34(matrix: &Matrix, operation: &str) -> Result<[[f64; 4]; 3]> {
	if matrix.shape() != [3, 4].as_slice() {
		return Err(Error::invalid_argument(format!(
			"{operation} requires a [3, 4] projection matrix"
		)));
	}
	let values = matrix.read_f32()?;
	let mut out = [[0.0f64; 4]; 3];
	for row in 0..3 {
		for column in 0..4 {
			out[row][column] = values[row * 4 + column] as f64;
		}
	}
	Ok(out)
}

/// Homogeneous DLT triangulation of one correspondence.
fn triangulate_point(
	p1: &[[f64; 4]; 3],
	p2: &[[f64; 4]; 3],
	point1: &[f64; 2],
	point2: &[f64; 2],
) -> Option<[f64; 4]> {
	let mut a = [0.0f64; 4 * 4];
	for column in 0..4 {
		a[column] = point1[0] * p1[2][column] - p1[0][column];
		a[4 + column] = point1[1] * p1[2][column] - p1[1][column];
		a[8 + column] = point2[0] * p2[2][column] - p2[0][column];
		a[12 + column] = point2[1] * p2[2][column] - p2[1][column];
	}
	let vectors = smallest_right_singular::<4>(&a, 4)?;
	Some([vectors[0][0], vectors[1][0], vectors[2][0], vectors[3][0]])
}

/// Normalized eight-point fundamental solve with rank-2 enforcement.
fn eight_point(points1: &[[f64; 2]], points2: &[[f64; 2]]) -> Option<[[f64; 3]; 3]> {
	let (t1, n1) = normalize2d(points1)?;
	let (t2, n2) = normalize2d(points2)?;
	let n = n1.len();
	let mut a = vec![0.0f64; n * 9];
	for (index, (&[x, y], &[u, v])) in n1.iter().zip(n2.iter()).enumerate() {
		let row = index * 9;
		a[row..row + 9].copy_from_slice(&[u * x, u * y, u, v * x, v * y, v, x, y, 1.0]);
	}
	let vectors = smallest_right_singular::<9>(&a, n)?;
	let mut f = [[0.0f64; 3]; 3];
	for row in 0..3 {
		for column in 0..3 {
			f[row][column] = vectors[row * 3 + column][0];
		}
	}
	// Enforce rank 2.
	let (u, s, vt) = svd3(f)?;
	let mut f_rank2 = [[0.0f64; 3]; 3];
	for i in 0..3 {
		for j in 0..3 {
			f_rank2[i][j] = s[0] * u[i][0] * vt[0][j] + s[1] * u[i][1] * vt[1][j];
		}
	}
	// Denormalize: F = T2^T * Fn * T1.
	let t2t = transpose3(t2);
	Some(mul_mat3(&t2t, &mul_mat3(&f_rank2, &t1)))
}

/// Collapse a rank-2 matrix to the essential constraint `(s, s, 0)`.
fn essential_enforce(e: [[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> {
	let (u, s, vt) = svd3(e)?;
	let mean = (s[0] + s[1]) / 2.0;
	let mut out = [[0.0f64; 3]; 3];
	for i in 0..3 {
		for j in 0..3 {
			out[i][j] = mean * (u[i][0] * vt[0][j] + u[i][1] * vt[1][j]);
		}
	}
	Some(out)
}

/// Symmetric epipolar distance for `p2^T F p1`.
fn epipolar_error(f: &[[f64; 3]; 3], p1: &[f64; 2], p2: &[f64; 2]) -> f64 {
	let x = [p1[0], p1[1], 1.0];
	let xp = [p2[0], p2[1], 1.0];
	let fx = [
		dot([f[0][0], f[0][1], f[0][2]], x),
		dot([f[1][0], f[1][1], f[1][2]], x),
		dot([f[2][0], f[2][1], f[2][2]], x),
	];
	let ftx = [
		dot([f[0][0], f[1][0], f[2][0]], xp),
		dot([f[0][1], f[1][1], f[2][1]], xp),
		dot([f[0][2], f[1][2], f[2][2]], xp),
	];
	let residual = dot(xp, fx);
	let denom1 = fx[0] * fx[0] + fx[1] * fx[1];
	let denom2 = ftx[0] * ftx[0] + ftx[1] * ftx[1];
	if denom1 < 1e-24 || denom2 < 1e-24 {
		return f64::MAX;
	}
	residual * residual * (1.0 / denom1 + 1.0 / denom2)
}

// ---- calibration -----------------------------------------------------------

/// Calibrate a monocular camera from `views >= 3` planar grids, matching
/// `calibrateCamera` on Zhang's method. `object` is `[V, N, 3]` object
/// coordinates (the plane must satisfy `z = 0`) and `image` `[V, N, 2]`
/// pixel observations with `N >= 4`. Intrinsics solve through the B-matrix
/// constraints; extrinsics recover per-view; `refine_iterations > 0` then
/// runs damped bundle adjustment over `(fx, fy, cx, cy, k1, k2)` plus
/// per-view pose. Returns camera, `(k1, k2, p1, p2, k3)` distortion,
/// per-view rvecs `[V, 3]`, tvecs `[V, 3]`, and RMS reprojection error.
///
/// # Errors
///
/// Returns an error for shape violations, degenerate grids, or a singular
/// B-matrix solve.
pub fn calibrate_camera(
	object: &Matrix,
	image: &Matrix,
	refine_iterations: i32,
) -> Result<CalibrateCameraResult> {
	let oshape = object.shape();
	let ishape = image.shape();
	if oshape.len() != 3 || oshape[2] != 3 {
		return Err(Error::invalid_argument(
			"calibrate_camera requires object [V, N, 3]",
		));
	}
	if ishape.len() != 3 || ishape[2] != 2 || ishape[0] != oshape[0] || ishape[1] != oshape[1] {
		return Err(Error::invalid_argument(
			"calibrate_camera requires image [V, N, 2] matching object",
		));
	}
	let (views, n) = (oshape[0], oshape[1]);
	if views < 3 {
		return Err(Error::invalid_argument(
			"calibrate_camera requires V >= 3 views",
		));
	}
	if n < 4 {
		return Err(Error::invalid_argument(
			"calibrate_camera requires N >= 4 points per view",
		));
	}
	if refine_iterations < 0 {
		return Err(Error::invalid_argument(
			"calibrate_camera refine_iterations must be nonnegative",
		));
	}
	let ovals = object.read_f32()?;
	let ivals = image.read_f32()?;
	let object_views: Vec<Vec<[f64; 3]>> = (0..views)
		.map(|v| {
			let base = v * n * 3;
			ovals[base..base + n * 3]
				.as_chunks::<3>()
				.0
				.iter()
				.map(|t| [t[0] as f64, t[1] as f64, t[2] as f64])
				.collect()
		})
		.collect();
	let image_views: Vec<Vec<[f64; 2]>> = (0..views)
		.map(|v| {
			let base = v * n * 2;
			ivals[base..base + n * 2]
				.as_chunks::<2>()
				.0
				.iter()
				.map(|t| [t[0] as f64, t[1] as f64])
				.collect()
		})
		.collect();
	for (v, view) in object_views.iter().enumerate() {
		if view.iter().any(|p| p[2].abs() > 1e-6) {
			return Err(Error::invalid_argument(format!(
				"calibrate_camera view {v} is not planar (z != 0)"
			)));
		}
	}
	// Per-view homographies from the planar grid to pixels.
	let homographies: Vec<[[f64; 3]; 3]> = object_views
		.iter()
		.zip(image_views.iter())
		.map(|(obj, img)| {
			let src: Vec<[f64; 2]> = obj.iter().map(|p| [p[0], p[1]]).collect();
			dlt_homography(&src, img)
		})
		.collect::<Option<Vec<_>>>()
		.ok_or_else(|| degenerate("calibrate_camera"))?;
	// B-matrix constraints: rows v12(b) and v11(b) - v22(b), solved as the
	// null vector of the 2V x 6 stack.
	let mut equations = vec![0.0f64; 2 * views * 6];
	for (v, h) in homographies.iter().enumerate() {
		let v_row = |p: usize, q: usize| -> [f64; 6] {
			let hp = |i: usize| h[i][p];
			let hq = |i: usize| h[i][q];
			[
				hp(0) * hq(0),
				hp(0) * hq(1) + hp(1) * hq(0),
				hp(1) * hq(1),
				hp(2) * hq(0) + hp(0) * hq(2),
				hp(2) * hq(1) + hp(1) * hq(2),
				hp(2) * hq(2),
			]
		};
		let v12 = v_row(0, 1);
		let v11 = v_row(0, 0);
		let v22 = v_row(1, 1);
		for i in 0..6 {
			equations[2 * v * 6 + i] = v12[i];
			equations[(2 * v + 1) * 6 + i] = v11[i] - v22[i];
		}
	}
	let b = smallest_right_singular::<6>(&equations, 2 * views)
		.ok_or_else(|| degenerate("calibrate_camera"))?;
	let b = [b[0][0], b[1][0], b[2][0], b[3][0], b[4][0], b[5][0]];
	// The null vector is sign-ambiguous; the B matrix must be positive.
	let mut camera = zhang_intrinsics(&b)
		.or_else(|| zhang_intrinsics(&b.map(|v| -v)))
		.ok_or_else(|| degenerate("calibrate_camera"))?;
	// Per-view extrinsics from K^-1 H.
	let kinv = invert3(camera).ok_or_else(|| degenerate("calibrate_camera"))?;
	let mut poses: Vec<([f64; 3], [f64; 3])> = Vec::with_capacity(views);
	for h in &homographies {
		let kh = mul_mat3(&kinv, h);
		let l1 = norm([kh[0][0], kh[1][0], kh[2][0]]);
		let l2 = norm([kh[0][1], kh[1][1], kh[2][1]]);
		if l1 < 1e-14 || l2 < 1e-14 {
			return Err(degenerate("calibrate_camera"));
		}
		let scale = 2.0 / (l1 + l2);
		let r1 = [kh[0][0] * scale, kh[1][0] * scale, kh[2][0] * scale];
		let r2 = [kh[0][1] * scale, kh[1][1] * scale, kh[2][1] * scale];
		let r3 = cross(r1, r2);
		let mut rotation = [
			[r1[0], r2[0], r3[0]],
			[r1[1], r2[1], r3[1]],
			[r1[2], r2[2], r3[2]],
		];
		// Orthonormalize through the polar factor.
		if let Some((u, _, vt)) = svd3(rotation) {
			rotation = mul_mat3(&u, &vt);
		}
		if det3(&rotation) < 0.0 {
			for r in rotation.iter_mut() {
				for c in r.iter_mut() {
					*c = -*c;
				}
			}
		}
		let t = [kh[0][2] * scale, kh[1][2] * scale, kh[2][2] * scale];
		poses.push((rodrigues_vector(&rotation), t));
	}
	let mut distortion = [0.0f64; 2];
	if refine_iterations > 0 {
		calibrate_refine(
			&mut camera,
			&mut distortion,
			&mut poses,
			&object_views,
			&image_views,
			refine_iterations as usize,
		);
	}
	// RMS reprojection error over every view/point.
	let rotation_mats: Vec<[[f64; 3]; 3]> = poses.iter().map(|(r, _)| rodrigues_matrix(r)).collect();
	let mut sse = 0.0;
	let mut count = 0usize;
	for (v, view) in object_views.iter().enumerate() {
		for (point, &target) in view.iter().zip(image_views[v].iter()) {
			let [x, y, z] = camera_project(&rotation_mats[v], &poses[v].1, point);
			if z.abs() < 1e-12 {
				continue;
			}
			let (xn, yn) = distort_normalized(x / z, y / z, &distortion);
			let u = camera[0][0] * xn + camera[0][1] * yn + camera[0][2];
			let w = camera[1][0] * xn + camera[1][1] * yn + camera[1][2];
			sse += (u - target[0]).powi(2) + (w - target[1]).powi(2);
			count += 1;
		}
	}
	let rms = if count > 0 {
		(sse / count as f64).sqrt()
	} else {
		0.0
	};
	let mut rvecs = Vec::with_capacity(views * 3);
	let mut tvecs = Vec::with_capacity(views * 3);
	for (r, t) in &poses {
		rvecs.extend(r.iter().map(|v| *v as f32));
		tvecs.extend(t.iter().map(|v| *v as f32));
	}
	Ok(CalibrateCameraResult {
		camera: mat3_f32(camera)?,
		distortion: vector_f32(&[distortion[0], distortion[1], 0.0, 0.0, 0.0])?,
		rotations: Matrix::new_from_f32([views, 3], &rvecs)?,
		translations: Matrix::new_from_f32([views, 3], &tvecs)?,
		rms_error: vector_f32(&[rms])?,
	})
}

/// Zhang closed-form intrinsics from the B-vector.
fn zhang_intrinsics(b: &[f64; 6]) -> Option<[[f64; 3]; 3]> {
	let [b0, b1, b2, b3, b4, b5] = *b;
	let denom = b0 * b2 - b1 * b1;
	if denom.abs() < 1e-24 {
		return None;
	}
	let v0 = (b1 * b3 - b0 * b4) / denom;
	let lambda = b5 - (b3 * b3 + v0 * (b1 * b3 - b0 * b4)) / b0;
	if lambda <= 0.0 || b0.abs() < 1e-24 {
		return None;
	}
	let alpha = (lambda / b0).sqrt();
	let inner = b0 * b2 - b1 * b1;
	if inner <= 0.0 {
		return None;
	}
	let beta = (lambda * b0 / inner).sqrt();
	let gamma = -b1 * alpha * alpha * beta / lambda;
	let u0 = gamma * v0 / beta - b3 * alpha * alpha / lambda;
	if !(alpha.is_finite() && beta.is_finite() && u0.is_finite() && v0.is_finite()) {
		return None;
	}
	Some([[alpha, gamma, u0], [0.0, beta, v0], [0.0, 0.0, 1.0]])
}

/// Damped bundle adjustment over intrinsics + (k1, k2) + per-view pose.
#[allow(clippy::type_complexity)]
fn calibrate_refine(
	camera: &mut [[f64; 3]; 3],
	distortion: &mut [f64; 2],
	poses: &mut Vec<([f64; 3], [f64; 3])>,
	object_views: &[Vec<[f64; 3]>],
	image_views: &[Vec<[f64; 2]>],
	iterations: usize,
) {
	let views = poses.len();
	// Parameter layout: [fx, skew, cx, fy, cy, k1, k2] + per-view [rvec(3), t(3)].
	let pack7 = |camera: &[[f64; 3]; 3], distortion: &[f64; 2]| -> Vec<f64> {
		vec![
			camera[0][0],
			camera[0][1],
			camera[0][2],
			camera[1][1],
			camera[1][2],
			distortion[0],
			distortion[1],
		]
	};
	let unpack = |p: &[f64]| -> ([[f64; 3]; 3], [f64; 2], Vec<([f64; 3], [f64; 3])>) {
		let cam = [[p[0], p[1], p[2]], [0.0, p[3], p[4]], [0.0, 0.0, 1.0]];
		let mut ps = Vec::with_capacity(views);
		for v in 0..views {
			let base = 7 + v * 6;
			ps.push((
				[p[base], p[base + 1], p[base + 2]],
				[p[base + 3], p[base + 4], p[base + 5]],
			));
		}
		(cam, [p[5], p[6]], ps)
	};
	let mut params = {
		let mut p = pack7(camera, distortion);
		for (r, t) in poses.iter() {
			p.extend_from_slice(r);
			p.extend_from_slice(t);
		}
		p
	};
	let residuals = |p: &[f64]| -> Vec<f64> {
		let (cam, dist, ps) = unpack(p);
		let mut out = Vec::new();
		for (v, view) in object_views.iter().enumerate() {
			let rotation = rodrigues_matrix(&ps[v].0);
			for (point, &target) in view.iter().zip(image_views[v].iter()) {
				let [x, y, z] = camera_project(&rotation, &ps[v].1, point);
				let (u, w) = if z.abs() < 1e-9 {
					(f64::MAX * 1e-6, f64::MAX * 1e-6)
				} else {
					let (xn, yn) = distort_normalized(x / z, y / z, &[dist[0], dist[1]]);
					(
						cam[0][0] * xn + cam[0][1] * yn + cam[0][2],
						cam[1][1] * yn + cam[1][2],
					)
				};
				out.push(u - target[0]);
				out.push(w - target[1]);
			}
		}
		out
	};
	let n = params.len();
	let mut cost: f64 = residuals(&params).iter().map(|r| r * r).sum();
	for _ in 0..iterations {
		let r = residuals(&params);
		let mut jacobian = vec![0.0f64; r.len() * n];
		for column in 0..n {
			let mut shifted = params.clone();
			let eps = (params[column].abs() * 1e-6).max(1e-6);
			shifted[column] += eps;
			let shifted_residuals = residuals(&shifted);
			for (row, value) in shifted_residuals.iter().enumerate() {
				jacobian[row * n + column] = (value - r[row]) / eps;
			}
		}
		let Some(step) = damped_least_squares(&jacobian, &r, r.len(), n, 1e-3) else {
			break;
		};
		let mut improved = false;
		for scale in [1.0, 0.5, 0.25] {
			let mut candidate = params.clone();
			for i in 0..n {
				candidate[i] += step[i] * scale;
			}
			let candidate_cost: f64 = residuals(&candidate).iter().map(|r| r * r).sum();
			if candidate_cost < cost {
				params = candidate;
				cost = candidate_cost;
				improved = true;
				break;
			}
		}
		if !improved {
			break;
		}
	}
	let (cam, dist, ps) = unpack(&params);
	*camera = cam;
	*distortion = dist;
	*poses = ps;
}