Skip to content

Commit

Permalink
#79: Sympylify plane calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallwork23 committed Jun 15, 2024
1 parent cce2718 commit ea761eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
34 changes: 11 additions & 23 deletions movement/math.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import sympy

__all__ = []

Expand Down Expand Up @@ -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):
Expand All @@ -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.")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit ea761eb

Please sign in to comment.