File: test_904_acc_bezier4p.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (134 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#  Copyright (c) 2020, Manfred Moitzi
#  License: MIT License
# Test only basic features of Cython implementation,
# Full testing and compatibility check with Python implementation
# is located in test suite 630a.

import pytest
import math

bezier = pytest.importorskip("ezdxf.acc.bezier4p")
Bezier4P = bezier.Bezier4P
from ezdxf.acc.vector import Vec3, Vec2
from ezdxf.acc.matrix44 import Matrix44

# check functions:
from ezdxf.math._bezier4p import (
    cubic_bezier_arc_parameters,
    cubic_bezier_from_arc,
    cubic_bezier_from_ellipse,
)
from ezdxf.math.ellipse import ConstructionEllipse

POINTS = Vec2.list([(0, 0), (1, 0), (1, 1), (0, 1)])


def test_default_constructor():
    c = Bezier4P(POINTS)
    assert c.control_points == Vec3.tuple(POINTS)


@pytest.fixture
def curve():
    return Bezier4P(POINTS)


def test_point(curve):
    assert curve.point(0.5) == (0.75, 0.5)


def test_tangent(curve):
    assert curve.tangent(0.5) == (0.0, 1.5)


def test_approximate(curve):
    points = curve.approximate(10)
    assert len(points) == 11


def test_flattening(curve):
    points = curve.flattening(0.01)
    assert len(points) == 17


def test_approximated_length(curve):
    assert curve.approximated_length(32) == 1.999232961352048


def test_reverse(curve):
    r = curve.reverse()
    assert r.control_points == Vec3.tuple(reversed(POINTS))


def test_transform(curve):
    m = Matrix44.translate(1, 1, 0)
    r = curve.transform(m)
    assert r.control_points == Vec3.tuple([(1, 1), (2, 1), (2, 2), (1, 2)])


@pytest.mark.parametrize(
    "s, e",
    [
        (0, math.pi),
        (math.pi, math.tau),
        (0, math.tau),
    ],
)
def test_correctness_arc_parameters(s, e):
    expected = list(cubic_bezier_arc_parameters(s, e))
    result = list(bezier.cubic_bezier_arc_parameters(s, e))
    assert result == expected


@pytest.mark.parametrize(
    "s, e",
    [
        (0, 180),
        (180, 360),
        (0, 360),
    ],
)
def test_correctness_bezier_from_arc(s, e):
    expected = list(
        cubic_bezier_from_arc(
            center=Vec3(1, 2),
            start_angle=s,
            end_angle=e,
        )
    )
    result = list(
        bezier.cubic_bezier_from_arc(
            center=Vec3(1, 2),
            start_angle=s,
            end_angle=e,
        )
    )
    for e, r in zip(expected, result):
        assert e.control_points == r.control_points


@pytest.mark.parametrize(
    "s, e",
    [
        (0, math.pi),
        (math.pi, math.tau),
        (0, math.tau),
    ],
)
def test_correctness_bezier_from_ellipse(s, e):
    ellipse = ConstructionEllipse(
        center=(1, 2),
        major_axis=(2, 0),
        ratio=0.5,
        start_param=s,
        end_param=e,
    )
    expected = list(cubic_bezier_from_ellipse(ellipse))
    result = list(bezier.cubic_bezier_from_ellipse(ellipse))
    for e, r in zip(expected, result):
        for ep, rp in zip(e.control_points, r.control_points):
            assert ep.isclose(rp)


if __name__ == "__main__":
    pytest.main([__file__])