Skip to content

Commit

Permalink
Add get_tangent() to curves
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Apr 22, 2024
1 parent 3c19f0b commit 0a94600
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/classy_blocks/construct/curves/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from classy_blocks.base.element import ElementBase
from classy_blocks.construct.point import Point
from classy_blocks.types import NPPointListType, NPPointType, ParamCurveFuncType, PointListType, PointType
from classy_blocks.types import NPPointListType, NPPointType, ParamCurveFuncType, PointListType, PointType, VectorType
from classy_blocks.util import functions as f
from classy_blocks.util.constants import DTYPE
from classy_blocks.util.constants import DTYPE, TOL


class CurveBase(ElementBase):
Expand Down Expand Up @@ -79,6 +79,13 @@ def get_closest_param(self, point: PointType) -> float:
i_distance = np.argmin(distances)
return params[i_distance]

def get_tangent(self, param: float, delta: float = TOL) -> VectorType:
"""Returns a normalized tangent to the curve at given parameter"""
prev_point = self.get_point(param - delta / 2)
next_point = self.get_point(param + delta / 2)

return f.unit_vector(next_point - prev_point)


class PointCurveBase(CurveBase):
"""A base object for curves, defined by a list of points"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_construct/test_curves/test_analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ def test_transform(self):
with self.assertRaises(NotImplementedError):
self.curve.translate([1, 1, 1])

@parameterized.expand(
(
(0, [0, 1, 0]),
(np.pi / 2, [-1, 0, 0]),
(np.pi, [0, -1, 0]),
)
)
def test_tangent(self, param, tangent):
np.testing.assert_almost_equal(self.curve.get_tangent(param), tangent)


class LineCurveTests(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 0a94600

Please sign in to comment.