import math import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import bl_bake as bb class TestEstimateRayDistance(unittest.TestCase): def test_two_percent_of_bbox_diagonal(self): # 3-4-12 直角箱对角线 = 13 self.assertAlmostEqual(bb.estimate_ray_distance((3.0, 4.0, 12.0)), 0.26) def test_custom_pct(self): self.assertAlmostEqual( bb.estimate_ray_distance((1.0, 0.0, 0.0), pct=0.5), 0.5) class TestOutputStem(unittest.TestCase): def test_strips_low_suffix(self): self.assertEqual(bb.output_stem("well1500_low"), "well1500") def test_keeps_name_without_suffix(self): self.assertEqual(bb.output_stem("well1500"), "well1500") def test_only_strips_trailing_suffix(self): self.assertEqual(bb.output_stem("low_poly_low"), "low_poly") class TestBboxMismatch(unittest.TestCase): def test_identical_ok(self): self.assertFalse(bb.bbox_mismatch((1.0, 2.0, 3.0), (1.0, 2.0, 3.0))) def test_within_10_percent_ok(self): self.assertFalse(bb.bbox_mismatch((1.0, 2.0, 3.0), (1.05, 1.9, 3.2))) def test_one_axis_exceeds(self): self.assertTrue(bb.bbox_mismatch((1.0, 2.0, 3.0), (1.0, 2.0, 3.5))) def test_zero_axis_ignored(self): # 平面模型某轴为 0,不应误报 self.assertFalse(bb.bbox_mismatch((1.0, 0.0, 3.0), (1.0, 0.0, 3.0))) class TestPercentile(unittest.TestCase): def test_interpolates(self): self.assertAlmostEqual(bb.percentile([0.0, 10.0], 0.25), 2.5) def test_rejects_empty(self): with self.assertRaises(ValueError): bb.percentile([], 0.99) def test_rejects_bad_q(self): with self.assertRaises(ValueError): bb.percentile([1.0], 1.1) class TestAdaptiveRayDistance(unittest.TestCase): def test_p99_with_safety(self): r = bb.adaptive_ray_distance([0.002] * 20, (1.0, 0.0, 0.0)) self.assertAlmostEqual(r["distance_p99"], 0.002) self.assertAlmostEqual(r["value"], 0.003) # 0.002*1.5,未夹 self.assertFalse(r["capped"]) def test_lower_floor(self): r = bb.adaptive_ray_distance([0.0], (1.0, 0.0, 0.0)) self.assertAlmostEqual(r["value"], 0.0005) # 下限 0.05% self.assertFalse(r["capped"]) def test_upper_cap(self): r = bb.adaptive_ray_distance([0.02], (1.0, 0.0, 0.0)) self.assertAlmostEqual(r["value"], 0.01) # 夹到 1% 上限 self.assertTrue(r["capped"]) def test_zero_bbox_raises(self): with self.assertRaises(ValueError): bb.adaptive_ray_distance([0.01], (0.0, 0.0, 0.0)) if __name__ == "__main__": unittest.main()