File: test_226_spline.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (568 lines) | stat: -rw-r--r-- 11,658 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# Copyright (c) 2019-2020 Manfred Moitzi
# License: MIT License
import pytest
import math

import ezdxf
from ezdxf.entities.spline import Spline
from ezdxf.lldxf.tagwriter import TagCollector, basic_tags_from_text
from ezdxf.math import Vec3, Matrix44, required_knot_values

SPLINE = """0
SPLINE
5
0
330
0
100
AcDbEntity
8
0
100
AcDbSpline
70
0
71
3
72
0
73
0
74
0
"""


def test_registered():
    from ezdxf.entities.factory import ENTITY_CLASSES

    assert "SPLINE" in ENTITY_CLASSES


def test_default_init():
    entity = Spline()
    assert entity.dxftype() == "SPLINE"
    assert entity.dxf.handle is None
    assert entity.dxf.owner is None


def test_default_new():
    entity = Spline.new(
        handle="ABBA",
        owner="0",
        dxfattribs={
            "color": 7,
            "flags": 4,
            "degree": 4,
            "knot_tolerance": 42,
            "control_point_tolerance": 43,
            "fit_tolerance": 44,
            "start_tangent": (1, 2, 3),
            "end_tangent": (4, 5, 6),
            "extrusion": (7, 8, 9),
        },
    )
    assert entity.dxf.layer == "0"
    assert entity.dxf.color == 7
    assert entity.dxf.flags == 4
    assert entity.dxf.degree == 4
    assert entity.dxf.knot_tolerance == 42
    assert entity.dxf.control_point_tolerance == 43
    assert entity.dxf.fit_tolerance == 44
    assert entity.dxf.start_tangent == (1, 2, 3)
    assert entity.dxf.end_tangent == (4, 5, 6)
    assert entity.dxf.extrusion == (7, 8, 9)


def test_load_from_text():
    entity = Spline.from_text(SPLINE)
    assert entity.dxf.layer == "0"
    assert entity.dxf.color == 256, "default color is 256 (by layer)"
    assert entity.dxf.extrusion == (0, 0, 1)
    assert entity.dxf.flags == 0
    assert entity.dxf.degree == 3
    assert entity.dxf.n_knots == 0
    assert entity.dxf.n_control_points == 0
    assert entity.dxf.n_fit_points == 0


def test_write_dxf():
    entity = Spline.from_text(SPLINE2)
    result = TagCollector.dxftags(entity)
    expected = basic_tags_from_text(SPLINE2)
    assert cmp_tags(result, expected, abs_tol=1e-4) == 0


def cmp_tags(result, expected, abs_tol=1e-6):
    if len(result) != len(expected):
        return -1
    pos = 0
    for r, e in zip(result, expected):
        rc, rv = r
        ec, ev = e
        if rc != ec:
            return pos
        if isinstance(rv, float):
            if not math.isclose(rv, ev, abs_tol=abs_tol):
                return pos
        elif rv != ev:
            return pos
        pos += 1
    return 0


@pytest.fixture
def spline():
    return Spline.from_text(SPLINE)


@pytest.fixture
def points():
    return [(1.0, 1.0, 0.0), (2.5, 3.0, 0.0), (4.5, 2.0, 0), (6.5, 4.0, 0)]


@pytest.fixture
def weights():
    return [1, 2, 3, 4]


@pytest.mark.parametrize("name", ["start", "end"])
def test_set_tangent(spline, name):
    spline = spline
    name += "_tangent"
    spline.dxf.set(name, (1, 2, 3))
    assert (1, 2, 3) == spline.dxf.get(name)


@pytest.mark.parametrize("name", ["start", "end"])
def test_users_cant_set_invalid_tangents(spline, name):
    with pytest.raises(ValueError):
        spline.dxf.set(name + "_tangent", (0, 0, 0))


SPLINE_INVALID_TANGENT = """0
SPLINE
5
0
330
0
100
AcDbEntity
8
0
100
AcDbSpline
70
0
71
3
72
0
73
0
74
0
12
0
22
0
32
0
13
0
23
0
33
0
"""


def test_ignore_invalid_tangent_values_at_loading_stage():
    spline = Spline.from_text(SPLINE_INVALID_TANGENT)
    assert spline.dxf.hasattr("start_tangent") is False
    assert spline.dxf.hasattr("end_tangent") is False


def test_knot_values(spline):
    spline = spline
    values = [1, 2, 3, 4, 5, 6, 7]
    spline.knots = values
    assert 7 == spline.dxf.n_knots
    assert values == list(spline.knots)


def test_weights(spline):
    spline = spline
    weights = [1, 2, 3, 4, 5, 6, 7]
    spline.weights = weights
    assert weights == list(spline.weights)


def test_control_points(spline, points):
    spline.control_points = points
    assert spline.dxf.n_control_points == 4
    assert cmp_vertices(spline.control_points, points) is True


def test_fit_points(spline, points):
    spline.fit_points = points
    assert spline.dxf.n_fit_points == 4
    assert cmp_vertices(spline.fit_points, points) is True

def cmp_vertices(a, b):
    return all(Vec3(v0).isclose(v1) for v0, v1 in zip(a, b))

def test_set_open_uniform(spline, points):
    spline.set_open_uniform(points, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert spline.dxf.n_control_points == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_set_uniform(spline, points):
    spline.set_uniform(points, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert spline.dxf.n_control_points == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_set_closed(spline, points):
    # closed spline: first `degree` control points will be repeated at the end
    degree = 3
    spline.set_closed(points, degree=degree)
    assert spline.dxf.degree == degree
    assert spline.dxf.n_control_points == len(points) + degree
    assert (
        spline.dxf.n_knots == len(spline.control_points) + degree + 1
    )  # n + p + 2
    assert spline.closed is True


def test_set_open_rational(spline, points, weights):
    spline.set_open_rational(points, weights, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert list(spline.weights) == weights
    assert spline.dxf.n_control_points == len(points)
    assert len(spline.weights) == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_set_uniform_rational(spline, points, weights):
    spline.set_uniform_rational(points, weights, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert list(spline.weights) == weights
    assert spline.dxf.n_control_points == len(points)
    assert len(spline.weights) == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_set_closed_rational(spline, points, weights):
    # closed spline: first `degree` control points and weights will be repeated at the end
    degree = 3
    spline.set_closed_rational(points, weights, degree=degree)
    assert spline.dxf.degree == 3
    assert spline.dxf.n_control_points == len(points) + degree
    assert len(spline.weights) == len(points) + degree
    assert (
        spline.dxf.n_knots == len(spline.control_points) + degree + 1
    )  # n + p + 2
    assert spline.closed is True


@pytest.fixture(scope="module")
def msp():
    doc = ezdxf.new("R2000")
    return doc.modelspace()


def test_open_spline(msp, points):
    spline = msp.add_open_spline(points, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert spline.dxf.n_control_points == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_open_rational_spline(msp, points, weights):
    spline = msp.add_rational_spline(points, weights, degree=3)
    assert spline.dxf.degree == 3
    assert cmp_vertices(spline.control_points, points) is True
    assert list(spline.weights) == weights
    assert spline.dxf.n_control_points == len(points)
    assert len(spline.weights) == len(points)
    assert (
        spline.dxf.n_knots == len(points) + spline.dxf.degree + 1
    )  # n + p + 2
    assert spline.closed is False


def test_spline_transform_interface():
    spline = Spline()
    spline.set_uniform([(1, 0, 0), (3, 3, 0), (6, 0, 1)])
    spline.dxf.start_tangent = Vec3(1, 0, 0)
    spline.dxf.end_tangent = Vec3(2, 0, 0)
    spline.dxf.extrusion = Vec3(3, 0, 0)
    spline.transform(Matrix44.translate(1, 2, 3))
    assert tuple(spline.control_points[0]) == (2, 2, 3)
    # direction vectors are not transformed by translation
    assert spline.dxf.start_tangent == (1, 0, 0)
    assert spline.dxf.end_tangent == (2, 0, 0)
    assert spline.dxf.extrusion == (3, 0, 0)


def test_from_circle():
    from ezdxf.entities import Circle

    spline = Spline.from_arc(
        Circle.new(
            dxfattribs={
                "center": (1, 1),
                "radius": 2,
                "layer": "circle",
            }
        )
    )
    assert spline.dxf.handle is None
    assert spline.dxf.owner is None
    assert spline.dxf.layer == "circle"
    assert spline.dxf.degree == 2
    assert len(spline.control_points) > 2
    assert len(spline.weights) > 2
    assert len(spline.fit_points) == 0
    assert len(spline.knots) == required_knot_values(
        len(spline.control_points), spline.dxf.degree + 1
    )


def test_from_arc():
    from ezdxf.entities import Arc

    spline = Spline.from_arc(
        Arc.new(
            dxfattribs={
                "center": (1, 1),
                "radius": 2,
                "start_angle": 30,  # degrees
                "end_angle": 150,
                "layer": "arc",
            }
        )
    )
    assert spline.dxf.layer == "arc"
    assert spline.dxf.degree == 2
    assert len(spline.control_points) > 2
    assert len(spline.weights) > 2
    assert len(spline.fit_points) == 0
    assert len(spline.knots) == required_knot_values(
        len(spline.control_points), spline.dxf.degree + 1
    )


def test_from_ellipse():
    from ezdxf.entities import Ellipse

    spline = Spline.from_arc(
        Ellipse.new(
            dxfattribs={
                "center": (1, 1),
                "major_axis": (2, 0),
                "ratio": 0.5,
                "start_param": 0.5,  # radians
                "end_param": 3,
                "layer": "ellipse",
            }
        )
    )
    assert spline.dxf.layer == "ellipse"
    assert spline.dxf.degree == 2
    assert len(spline.control_points) > 2
    assert len(spline.weights) > 2
    assert len(spline.fit_points) == 0
    assert len(spline.knots) == required_knot_values(
        len(spline.control_points), spline.dxf.degree + 1
    )


def test_from_line_with_type_error():
    from ezdxf.entities import Line

    with pytest.raises(TypeError):
        Spline.from_arc(Line.new())


SPLINE2 = """  0
SPLINE
  5
95
330
1F
100
AcDbEntity
  8
0
100
AcDbSpline
210
0.0
220
0.0
230
1.0
 70
  1064
 71
     3
 72
    13
 73
     9
 74
     7
 42
0.000000001
 43
0.0000000001
 44
0.0
 40
0.0
 40
0.0
 40
0.0
 40
0.0
 40
127.1219886565656
 40
245.5280695814911
 40
411.2748644222964
 40
568.8185091687602
 40
643.1421258730084
 40
742.177473098128
 40
742.177473098128
 40
742.177473098128
 40
742.177473098128
 10
226.0
 20
276.0
 30
0.0
 10
175.2490432562881
 20
266.5994639691058
 30
0.0
 10
77.22678471247674
 20
248.4429240259472
 30
0.0
 10
139.5678769445405
 20
69.31979038269674
 30
0.0
 10
292.532981827299
 20
56.54395007082465
 30
0.0
 10
442.5199073432509
 20
34.24994434874142
 30
0.0
 10
505.7887841571028
 20
159.181589221269
 30
0.0
 10
446.0991144985865
 20
192.7966266652906
 30
0.0
 10
412.0
 20
212.0
 30
0.0
 11
226.0
 21
276.0
 31
0.0
 11
110.0
 21
224.0
 31
0.0
 11
142.0
 21
110.0
 31
0.0
 11
298.0
 21
54.0
 31
0.0
 11
454.0
 21
76.0
 31
0.0
 11
484.0
 21
144.0
 31
0.0
 11
412.0
 21
212.0
 31
0.0
"""