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
|
# Copyright (c) 2022, Manfred Moitzi
# License: MIT License
import pytest
from ezdxf.math import Vec2, Bezier4P
from ezdxf.render import hatching, forms
from ezdxf import path
class TestHatchBaseLine:
def test_positive_line_distance(self):
line = hatching.HatchBaseLine(
origin=Vec2((1, 2)), direction=Vec2(2, 0), offset=Vec2(2, 2)
)
assert line.normal_distance == pytest.approx(2.0)
def test_negative_line_distance(self):
line = hatching.HatchBaseLine(
origin=Vec2((1, 2)), direction=Vec2(2, 0), offset=Vec2(2, -2)
)
assert line.normal_distance == pytest.approx(-2.0)
def test_hatch_line_direction_error(self):
with pytest.raises(hatching.HatchLineDirectionError):
hatching.HatchBaseLine(Vec2(), direction=Vec2(), offset=Vec2(1, 0))
def test_dense_hatching_error(self):
with pytest.raises(hatching.DenseHatchingLinesError):
hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(1, 0)
)
with pytest.raises(hatching.DenseHatchingLinesError):
hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(-1, 0)
)
def test_no_offset_error(self):
with pytest.raises(hatching.DenseHatchingLinesError):
hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 0)
)
def test_very_small_offset_error(self):
with pytest.raises(hatching.DenseHatchingLinesError):
hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1e-6)
)
class TestIntersectHatchLine:
@pytest.fixture
def horizontal_baseline(self):
return hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1)
)
def test_intersect_line_collinear(self, horizontal_baseline):
a = Vec2(3, 0)
b = Vec2(10, 0)
distance = 0
hatch_line = horizontal_baseline.hatch_line(distance)
ip = hatch_line.intersect_line(a, b, distance, distance)
assert ip.type == hatching.IntersectionType.COLLINEAR
assert ip.p0 is a
assert ip.p1 is b
def test_intersect_line_start(self, horizontal_baseline):
a = Vec2(0, 0)
b = Vec2(0, 10)
hatch_line = horizontal_baseline.hatch_line(0)
ip = hatch_line.intersect_line(a, b, 0, 10)
assert ip.type == hatching.IntersectionType.START
assert ip.p0 is a
def test_intersect_line_end(self, horizontal_baseline):
a = Vec2(0, 0)
b = Vec2(0, 10)
hatch_line = horizontal_baseline.hatch_line(10)
ip = hatch_line.intersect_line(a, b, 0, 10)
assert ip.type == hatching.IntersectionType.END
assert ip.p0 is b
@pytest.mark.parametrize("d", [-2, 0, 6])
def test_intersect_line_regular(self, horizontal_baseline, d):
a = Vec2(4, -3)
b = Vec2(4, 7)
dist_a = horizontal_baseline.signed_distance(a)
dist_b = horizontal_baseline.signed_distance(b)
hatch_line = horizontal_baseline.hatch_line(d)
ip = hatch_line.intersect_line(a, b, dist_a, dist_b)
assert ip.type == hatching.IntersectionType.REGULAR
assert ip.p0.isclose((4, d))
def test_cubic_bezier_curve(self, horizontal_baseline):
# low level intersection tests:
# test_630b - TestRayCubicBezierCurve2dIntersection()
curve = Bezier4P(Vec2.list([(0, -2), (2, 6), (4, -6), (6, 2)]))
hatch_line = horizontal_baseline.hatch_line(0)
ips = hatch_line.intersect_cubic_bezier_curve(curve)
assert len(ips) == 3
assert ips[0].p0.isclose((0.6762099922755492, 0.0))
assert ips[1].p0.isclose((3.0, 0.0))
assert ips[2].p0.isclose((5.323790007724451, 0.0))
def test_missing_line_in_gear_example(self):
baseline = hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 1), offset=Vec2(-1, 1)
)
polygon = [
Vec2(-5.099019513592784, 6.164414002968977),
Vec2(-6.892024376045109, 7.245688373094721),
Vec2(-7.245688373094716, 6.892024376045114),
Vec2(-6.164414002968974, 5.099019513592788),
]
lines = list(hatching.hatch_polygons(baseline, [polygon]))
assert len(lines) == 2
DATA = [
("10 l 10 l 10", 10),
("2 l 2 r 2 r 2 l 6 " "l 10 l 2 l 2 r 2 r 2 l 6", 14),
(
"2 l 2 r 2 l 2 r 2 r 4 l 4 l 10 l 2 l 2 r 2 l 2 r 2 r 4 l 4",
18,
),
(
"2 r 2 l 2 r 2 l 2 l 4 r 4 l 10 l 2 r 2 l 2 r 2 l 2 l 4 r 4",
18,
),
(
"2 l 2 r 2 r 2 l 2 l 4 r 2 r 4 l 2 l 10 l 2 r 2 l 2 l 2 r 2 r 4 l 2 l 4 r 2",
22,
),
("3 @2,2 @2,-2 3 l 10 l @-2,-2 @-2,2 2 @-2,-2 @-2,2", 14),
(
"3 @1,1 @1,1 @1,-1 @1,-1 3 l 10 l @-1,-1 @-1,-1 @-1,1 @-1,1 2 @-1,-1 @-1,-1 @-1,1 @-1,1",
14,
),
]
@pytest.mark.parametrize("d,count", DATA)
def test_hatch_polygons(d: str, count):
"""Visual check by the function collinear_hatching() in script
exploration/hatching.py,
"""
baseline = hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1)
)
lines = list(hatching.hatch_polygons(baseline, [list(forms.turtle(d))]))
assert len(lines) == count
@pytest.mark.parametrize("d,count", DATA)
def test_hatch_paths(d: str, count):
"""Visual check by the function collinear_hatching() in script
exploration/hatching.py,
"""
baseline = hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1)
)
lines = list(
hatching.hatch_paths(
baseline, [path.from_vertices(forms.turtle(d), close=True)]
)
)
assert len(lines) == count
def test_hatch_curved_path():
"""Visual check by the function collinear_hatching() in script
exploration/hatching.py,
"""
p = path.Path((0, 0))
p.line_to((10, 0))
p.curve3_to((10, 10), (14, 5))
p.line_to((0, 10))
baseline = hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1)
)
lines = list(hatching.hatch_paths(baseline, [p]))
assert len(lines) == 10
def test_hatch_path_with_hole():
baseline = hatching.HatchBaseLine(
Vec2(), direction=Vec2(1, 0), offset=Vec2(0, 1)
)
polygons = [
list(forms.square(10)),
list(forms.translate(forms.square(6), (2, 2))),
]
ctrL_lines = list(hatching.hatch_polygons(baseline, polygons))
p = path.Path((0, 0))
p.line_to((10, 0))
p.curve3_to((10, 10), (14, 5))
p.line_to((0, 10))
p.close_sub_path()
p.move_to((2, 2))
p.line_to((8, 2))
p.line_to((8, 8))
p.line_to((2, 8))
lines = list(hatching.hatch_paths(baseline, [p]))
assert len(lines) == len(ctrL_lines)
def test_vertical_hatching_with_hole():
"""Visual check by the 1st example for 90 deg in function hole_examples()
in script exploration/hatching.py,
"""
size = 10
angle = 90
polygons = [
list(forms.square(size)),
list(forms.translate(forms.square(size - 2), (1, 1))),
list(forms.translate(forms.square(3), (2, 2))),
list(forms.translate(forms.square(3), (4, 3))),
]
direction = Vec2.from_deg_angle(angle)
offset = direction.orthogonal() * 0.1
baseline = hatching.HatchBaseLine(
Vec2(0, 0), direction=direction, offset=offset
)
lines = list(hatching.hatch_polygons(baseline, polygons))
assert len(lines) == 241
class TestLinePatternRendering:
@pytest.fixture
def baseline(self):
return hatching.HatchBaseLine(
Vec2(),
direction=Vec2(1, 0),
offset=Vec2(0, 1),
line_pattern=[1.0, -0.5, 1.5, -1.0],
)
def test_pattern_length(self, baseline):
assert baseline.pattern_renderer(0).pattern_length == 4.0
def test_render_full_pattern(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(renderer.render_full_pattern(0))
dash1, dash2 = lines
assert dash1[0] == (0, 0)
assert dash1[1] == (1, 0)
assert dash2[0] == (1.5, 0)
assert dash2[1] == (3.0, 0)
def test_render_start_to_offset(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(
renderer.render_offset_to_offset(
index=0, s_offset=0.0, e_offset=2.0
)
)
dash1, dash2 = lines
assert dash1[0] == (0, 0)
assert dash1[1] == (1, 0)
assert dash2[0] == (1.5, 0)
assert dash2[1] == (2.0, 0)
def test_render_offset_to_end(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(
renderer.render_offset_to_offset(
index=0, s_offset=0.5, e_offset=4.0
)
)
dash1, dash2 = lines
assert dash1[0] == (0.5, 0)
assert dash1[1] == (1, 0)
assert dash2[0] == (1.5, 0)
assert dash2[1] == (3.0, 0)
def test_render_offset_to_offset(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(
renderer.render_offset_to_offset(
index=0, s_offset=0.5, e_offset=2.0
)
)
dash1, dash2 = lines
assert dash1[0] == (0.5, 0)
assert dash1[1] == (1, 0)
assert dash2[0] == (1.5, 0)
assert dash2[1] == (2.0, 0)
def test_hatch_line_full_pattern(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(renderer.render(Vec2(0, 0), Vec2(12, 0)))
assert len(lines) == 6, "expected 3 full pattern sequences"
assert lines[0][0] == (0, 0)
assert lines[-1][1] == (11, 0)
def test_hatch_line_with_start_and_end_offset(self, baseline):
renderer = baseline.pattern_renderer(0)
lines = list(renderer.render(Vec2(1, 0), Vec2(10, 0)))
assert len(lines) == 6
assert lines[0][0] == (1, 0)
assert lines[-1][1] == (10, 0)
def test_explode_earth1_pattern():
"""Visual check by the function explode_hatch_pattern() in script
exploration/hatching.py,
"""
from ezdxf.entities import Hatch
hatch = Hatch.new()
hatch.set_pattern_definition(
[
[0.0, (0.0, 0.0), (1.5875, 1.5875), [1.5875, -1.5875]],
[0.0, (0.0, 0.5953125), (1.5875, 1.5875), [1.5875, -1.5875]],
[0.0, (0.0, 1.190625), (1.5875, 1.5875), [1.5875, -1.5875]],
[
90.0,
(0.1984375, 1.3890625),
(-1.5875, 1.5875),
[1.5875, -1.5875],
],
[90.0, (0.79375, 1.3890625), (-1.5875, 1.5875), [1.5875, -1.5875]],
[
90.0,
(1.3890625, 1.3890625),
(-1.5875, 1.5875),
[1.5875, -1.5875],
],
]
)
hatch.dxf.solid_fill = 0
# 1. polyline path
hatch.paths.add_polyline_path(
[
(0.0, 223.0, 0.0),
(10.0, 223.0, 0.0),
(10.0, 233.0, 0.0),
(0.0, 233.0, 0.0),
],
is_closed=1,
flags=3,
)
# jiggle_origin=True has random behavior, which is not good for a test!
lines = list(hatching.hatch_entity(hatch, jiggle_origin=False))
assert len(lines) == 139
if __name__ == "__main__":
pytest.main([__file__])
|