def test_are_collinear(arrays): array_a, array_b, array_c = arrays assert Points([array_a, array_a, array_a]).are_collinear(tol=ATOL) assert Points([array_a, array_a, array_b]).are_collinear(tol=ATOL) all_different = not (Point(array_a).is_close(array_b, abs_tol=ATOL) or Point(array_b).is_close(array_c, abs_tol=ATOL)) if Points([array_a, array_b, array_c]).are_collinear() and all_different: line_ab = Line.from_points(array_a, array_b) line_bc = Line.from_points(array_b, array_c) assert line_ab.contains_point(array_c, abs_tol=ATOL) assert line_bc.contains_point(array_a, abs_tol=ATOL) assert line_ab.is_coplanar(line_bc)
def test_from_points(objs): point_a, vector = objs point_b = point_a + vector line = Line(point_a, vector) line_from_points = Line.from_points(point_a, point_b) assert line.is_close(line_from_points, abs_tol=ATOL) # The line of best fit should be the same # as the line from two points. line_fit = Line.best_fit([point_a, point_b]) assert line_fit.is_close(line_from_points, abs_tol=ATOL)
def project_on_vector(L, R, M): '''gets left and closest Right and measured points, returns projection on the vector btw. R&L of M''' line = Line(point = L[0:2], direction=R[0:2]) point = Point(M[0:2]) line_const = line2pts(L, R) point_projected = line.project_point(point) line_projection = Line.from_points(point, point_projected) result = {"Point" : point_projected, "Line" : line_projection, "Distance": distance_pt2line(line_const["m"], line_const["b"], M[0:2])} return result
""" 2D Point-Line Projection ======================== Project a point onto a line. """ from skspatial.objects import Point, Line from skspatial.plotting import plot_2d line = Line(point=[0, 0], direction=[1, 1]) point = Point([1, 4]) point_projected = line.project_point(point) line_projection = Line.from_points(point, point_projected) _, ax = plot_2d( line.plotter(t_2=5, c='k'), line_projection.plotter(c='k', linestyle='--'), point.plotter(s=75, c='k'), point_projected.plotter(c='r', s=75, zorder=3), ) ax.axis('equal')
def spatial_parameters(point_a_i: array_like, point_b: array_like, point_a_f: array_like) -> Dict[str, np.float64]: """ Calculate spatial gait parameters for a stride. Positions are input in temporal order (foot A initial, foot B, foot A final). Parameters ---------- point_a_i : array_like Initial position of foot A. point_b : array_like Position of foot B. point_a_f : array_like Final position of foot A. Returns ------- dict Dictionary consisting of stride length, absolute step length, step length, and stride width. Examples -------- >>> import numpy as np >>> point_l_1 = [764.253, 28.798] >>> point_r_1 = [696.834, 37.141] >>> point_l_2 = [637.172, 24.508] >>> point_r_2 = [579.102, 35.457] >>> point_l_3 = [518.030, 30.507] >>> values = list(spatial_parameters(point_l_1, point_r_1, point_l_2).values()) >>> np.round(values, 1) array([127.2, 61. , 60.1, 10.6]) >>> values = list(spatial_parameters(point_r_1, point_l_2, point_r_2).values()) >>> np.round(values, 1) array([117.7, 59.1, 57.9, 11.8]) >>> values = list(spatial_parameters(point_l_2, point_r_2, point_l_3).values()) >>> np.round(values, 1) array([119.3, 61.3, 60.7, 8. ]) """ line_a = Line.from_points(point_a_i, point_a_f) point_b_proj = line_a.project_point(point_b) stride_length = line_a.direction.norm() absolute_step_length = Vector.from_points(point_b, point_a_f).norm() step_length = Vector.from_points(point_b_proj, point_a_f).norm() stride_width = Vector.from_points(point_b_proj, point_b).norm() return { 'stride_length': stride_length, 'absolute_step_length': absolute_step_length, 'step_length': step_length, 'stride_width': stride_width, }
def test_from_points_failure(array_a, array_b): message_expected = "The vector must not be the zero vector." with pytest.raises(ValueError, match=message_expected): Line.from_points(array_a, array_b)
def test_from_points(array_a, array_b, line_expected): assert Line.from_points(array_a, array_b).is_close(line_expected)
def test_from_points_failure(array_a, array_b): with pytest.raises(Exception): Line.from_points(array_a, array_b)