From ea761eb0c43ba515c827b138136e66aa44bf1e9f Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Sat, 15 Jun 2024 09:35:29 +0100 Subject: [PATCH] #79: Sympylify plane calculation --- movement/math.py | 34 +++++++++++----------------------- pyproject.toml | 2 +- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/movement/math.py b/movement/math.py index 022057d..2f74a37 100644 --- a/movement/math.py +++ b/movement/math.py @@ -1,4 +1,5 @@ import numpy as np +import sympy __all__ = [] @@ -52,25 +53,12 @@ def _equation_of_plane(a, b, c): :returns: a function of three variables representing the plane :rtype: :class:`~.Callable` """ - assert len(a) == 3 - assert len(b) == 3 - assert len(c) == 3 - a = np.array(a) - b = np.array(b) - c = np.array(c) - n = np.cross(b - a, c - a) - - # Colinear case - if np.allclose(n, 0): - return None - - def f(x, y, z): - return x * n[0] + y * n[1] + z * n[2] - np.dot(n, a) - - assert np.isclose(f(*a), 0) - assert np.isclose(f(*b), 0) - assert np.isclose(f(*c), 0) - return f + plane = sympy.Plane(sympy.Point3D(a), sympy.Point3D(b), sympy.Point3D(c)) + + def equation(x, y, z): + return plane.distance(sympy.Point3D((x, y, z))) + + return equation def equation_of_plane(*points): @@ -87,8 +75,8 @@ def equation_of_plane(*points): while len(indices) >= 3: np.random.shuffle(indices) i, j, k = indices[:3] - f = _equation_of_plane(points[i], points[j], points[k]) - if f is not None: - return f - indices.pop(0) + try: + return _equation_of_plane(points[i], points[j], points[k]) + except ValueError: + indices.pop(0) raise ValueError("Could not determine a plane for the provided points.") diff --git a/pyproject.toml b/pyproject.toml index a73926c..0d7b416 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ requires = ["setuptools"] [project] name = "movement" version = "0.2" -dependencies = ["cffconvert", "pre-commit", "ruff"] +dependencies = ["cffconvert", "pre-commit", "ruff", "sympy"] authors = [ {name = "Joseph G. Wallwork", email = "joe.wallwork@outlook.com"}, {name = "Stephan C. Kramer"},