99from py_ballisticcalc .vector import Vector
1010
1111
12- def make_base (t : float , pos : float , vel : float , mach : float ) -> BaseTrajData :
13- return BaseTrajData (time = t , position = Vector (pos , 0.0 , 0.0 ), velocity = Vector (vel , 0.0 , 0.0 ), mach = mach )
12+ class TestInterpolationBasic :
1413
14+ @staticmethod
15+ def make_base (t : float , pos : float , vel : float , mach : float ) -> BaseTrajData :
16+ return BaseTrajData (time = t , position = Vector (pos , 0.0 , 0.0 ), velocity = Vector (vel , 0.0 , 0.0 ), mach = mach )
1517
16- class TestInterpolationBasic :
1718 def test_pchip_monotone_preserves_shape_scalar (self ):
1819 x0 , x1 , x2 = 0.0 , 1.0 , 2.0
1920 y0 , y1 , y2 = 0.0 , 1.0 , 1.5
@@ -33,9 +34,9 @@ def test_linear_scalar_agrees_with_exact_line(self):
3334 assert math .isclose (y , y0 + (y1 - y0 ) * (x - x0 ) / (x1 - x0 ), rel_tol = 0 , abs_tol = 1e-12 )
3435
3536 def test_basetrajdata_interpolate_method_switch (self ):
36- p0 = make_base (0.0 , 0.0 , 3000.0 , 2.5 )
37- p1 = make_base (1.0 , 1.0 , 2800.0 , 2.3 )
38- p2 = make_base (2.0 , 1.5 , 2600.0 , 2.1 )
37+ p0 = self . make_base (0.0 , 0.0 , 3000.0 , 2.5 )
38+ p1 = self . make_base (1.0 , 1.0 , 2800.0 , 2.3 )
39+ p2 = self . make_base (2.0 , 1.5 , 2600.0 , 2.1 )
3940 # mid-point in first interval
4041 res_pchip = BaseTrajData .interpolate ('time' , 0.5 , p0 , p1 , p2 , method = 'pchip' )
4142 res_linear = BaseTrajData .interpolate ('time' , 0.5 , p0 , p1 , p2 , method = 'linear' )
@@ -45,17 +46,29 @@ def test_basetrajdata_interpolate_method_switch(self):
4546
4647 def test_basetrajdata_interpolate_dimension_switch_on_position (self ):
4748 # Use position.x as key
48- p0 = make_base (0.0 , 0.0 , 3000.0 , 2.5 )
49- p1 = make_base (1.0 , 100.0 , 2800.0 , 2.3 )
50- p2 = make_base (2.0 , 200.0 , 2600.0 , 2.1 )
49+ p0 = self . make_base (0.0 , 0.0 , 3000.0 , 2.5 )
50+ p1 = self . make_base (1.0 , 100.0 , 2800.0 , 2.3 )
51+ p2 = self . make_base (2.0 , 200.0 , 2600.0 , 2.1 )
5152 res_lin = BaseTrajData .interpolate ('position.x' , 50.0 , p0 , p1 , p2 , method = 'linear' )
5253 res_pc = BaseTrajData .interpolate ('position.x' , 50.0 , p0 , p1 , p2 , method = 'pchip' )
5354 # Ensure times are between neighbor times when keying on position.x
5455 assert p0 .time <= res_lin .time <= p1 .time
5556 assert p0 .time <= res_pc .time <= p1 .time
5657
58+ def test_basetraj_linear_chooses_correct_segment (self ):
59+ # Create non-uniform key spacing and values; request linear at key in right segment
60+ p0 = self .make_base (0.0 , 0.0 , 0.0 , 0.2 )
61+ p1 = self .make_base (0.1 , 1.0 , 0.0 , 0.3 )
62+ p2 = self .make_base (2.0 , 2.0 , 0.0 , 0.4 )
63+ # Interpolate position.x keyed by time=1.0 (right segment)
64+ r = BaseTrajData .interpolate ('time' , 1.0 , p0 , p1 , p2 , method = "linear" )
65+ # Expect linear between p1 and p2 positions.x
66+ expected = interpolate_2_pt (1.0 , 0.1 , p1 .position .x , 2.0 , p2 .position .x )
67+ assert abs (r .position .x - expected ) < 1e-12
68+
5769
5870class TestInterpolationEdge :
71+
5972 def test_pchip_no_overshoot_near_peak_and_valley (self ):
6073 # Local peak around x=1: increasing then decreasing
6174 x0 , x1 , x2 = 0.0 , 1.0 , 2.0
@@ -147,17 +160,6 @@ def test_pchip_equals_linear_on_colinear_points(self):
147160 y_ref = interpolate_2_pt (x , x0 , y0 , x1 , y1 ) if x <= x1 else interpolate_2_pt (x , x1 , y1 , x2 , y2 )
148161 assert abs (y_p - y_ref ) < 1e-12
149162
150- def test_basetraj_linear_chooses_correct_segment (self ):
151- # Create non-uniform key spacing and values; request linear at key in right segment
152- p0 = make_base (0.0 , 0.0 , 0.0 , 0.2 )
153- p1 = make_base (0.1 , 1.0 , 0.0 , 0.3 )
154- p2 = make_base (2.0 , 2.0 , 0.0 , 0.4 )
155- # Interpolate position.x keyed by time=1.0 (right segment)
156- r = BaseTrajData .interpolate ('time' , 1.0 , p0 , p1 , p2 , method = "linear" )
157- # Expect linear between p1 and p2 positions.x
158- expected = interpolate_2_pt (1.0 , 0.1 , p1 .position .x , 2.0 , p2 .position .x )
159- assert abs (r .position .x - expected ) < 1e-12
160-
161163
162164class TestTrajectoryDataInterpolation :
163165
0 commit comments