1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
# Copyright (c) 2022, Manfred Moitzi
# License: MIT License
import pytest
from ezdxf.math import (
ApproxParamT,
Bezier3P,
Bezier4P,
BSpline,
open_uniform_knot_vector,
Vec2,
)
class TestGenericFeatures:
@pytest.fixture
def curve(self):
return Bezier3P(Vec2.list([(0, 0), (1, 2), (2, 4)]))
def test_access_to_construction_polyline(self, curve):
approx = ApproxParamT(curve, segments=100)
assert approx.polyline.length == pytest.approx(4.472135954999586)
def test_get_max_t(self, curve):
approx = ApproxParamT(curve, segments=100)
assert approx.max_t == 1.0
class TestQuadraticBezier:
@pytest.fixture(scope="class")
def approx(self):
return ApproxParamT(Bezier3P(Vec2.list([(0, 0), (1, 2), (2, 4)])))
def test_start_param(self, approx):
assert approx.param_t(0) == pytest.approx(0.0)
def test_end_param(self, approx):
assert approx.param_t(approx.polyline.length) == pytest.approx(1.0)
def test_certain_distance(self, approx):
t = 0.4472135954999581
assert approx.param_t(2.0) == pytest.approx(t)
assert approx.distance(t) == pytest.approx(2.0)
class TestCubicBezier:
@pytest.fixture(scope="class")
def approx(self):
return ApproxParamT(Bezier4P(Vec2.list([(0, 0), (1, 2), (2, 4), (3, 1)])))
def test_start_param(self, approx):
assert approx.param_t(0) == pytest.approx(0.0)
def test_end_param(self, approx):
assert approx.param_t(approx.polyline.length) == pytest.approx(1.0)
def test_certain_distance(self, approx):
t = 0.31949832059570293
assert approx.param_t(2.0) == pytest.approx(t)
assert approx.distance(t) == pytest.approx(2.0)
class TestCubicSpline:
@pytest.fixture(scope="class")
def spline(self):
points = [(0, 0), (1, 2), (2, 4), (3, 1), (4, 2)]
# by default knot values are normalized in the range [0, 1],
# this creates a not normalized knot vector in the range [0, 2]:
knots = open_uniform_knot_vector(count=len(points), order=4)
return BSpline(
[(0, 0), (1, 2), (2, 4), (3, 1), (4, 2)],
knots=knots,
)
@pytest.fixture(scope="class")
def approx(self, spline):
return ApproxParamT(spline, max_t=spline.max_t)
def test_max_t_is_not_1(self, spline):
"""This class should test a different parameter range!"""
assert spline.max_t == pytest.approx(2.0)
def test_start_param(self, approx):
assert approx.param_t(0) == pytest.approx(0.0)
assert approx.distance(0.0) == pytest.approx(0.0)
def test_end_param(self, approx):
length = approx.polyline.length
assert approx.param_t(length) == pytest.approx(2.0)
assert approx.distance(2.0) == pytest.approx(length)
def test_certain_distance(self, approx):
t = 0.6692720686599521
assert approx.param_t(3.0) == pytest.approx(t)
assert approx.distance(t) == pytest.approx(3.0)
if __name__ == "__main__":
pytest.main([__file__])
|