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
|
# Copyright (c) 2018-2020, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
import math
from ezdxf.math import (
ConstructionArc,
ConstructionCircle,
ConstructionLine,
UCS,
Vec3,
Vec2,
arc_segment_count,
arc_chord_length,
)
from math import isclose
def test_arc_from_2p_angle_complex():
p1 = (-15.73335, 10.98719)
p2 = (-12.67722, 8.76554)
angle = 55.247230
arc = ConstructionArc.from_2p_angle(start_point=p1, end_point=p2, angle=angle)
arc_result = ConstructionArc(
center=(-12.08260, 12.79635),
radius=4.07443,
start_angle=-153.638906,
end_angle=-98.391676,
)
assert arc.center.isclose(arc_result.center, abs_tol=1e-5)
assert isclose(arc.radius, arc_result.radius, abs_tol=1e-5)
assert isclose(arc.start_angle, arc_result.start_angle, abs_tol=1e-4)
assert isclose(arc.end_angle, arc_result.end_angle, abs_tol=1e-4)
def test_arc_from_2p_angle_simple():
p1 = (2, 1)
p2 = (0, 3)
angle = 90
arc = ConstructionArc.from_2p_angle(start_point=p1, end_point=p2, angle=angle)
assert arc.center.isclose((0, 1))
assert isclose(arc.radius, 2)
assert isclose(arc.start_angle, 0, abs_tol=1e-12)
assert isclose(arc.end_angle, 90)
arc = ConstructionArc.from_2p_angle(start_point=p2, end_point=p1, angle=angle)
assert arc.center.isclose((2, 3))
assert isclose(arc.radius, 2)
assert isclose(arc.start_angle, 180)
assert isclose(arc.end_angle, -90)
def test_arc_from_2p_radius():
p1 = (2, 1)
p2 = (0, 3)
radius = 2
arc = ConstructionArc.from_2p_radius(start_point=p1, end_point=p2, radius=radius)
assert arc.center.isclose((0, 1))
assert isclose(arc.radius, radius)
assert isclose(arc.start_angle, 0)
assert isclose(arc.end_angle, 90)
arc = ConstructionArc.from_2p_radius(start_point=p2, end_point=p1, radius=radius)
assert arc.center.isclose((2, 3))
assert isclose(arc.radius, radius)
assert isclose(arc.start_angle, 180)
assert isclose(arc.end_angle, -90)
def test_arc_from_3p():
p1 = (-15.73335, 10.98719)
p2 = (-12.67722, 8.76554)
p3 = (-8.00817, 12.79635)
arc = ConstructionArc.from_3p(start_point=p1, end_point=p2, def_point=p3)
arc_result = ConstructionArc(
center=(-12.08260, 12.79635),
radius=4.07443,
start_angle=-153.638906,
end_angle=-98.391676,
)
assert arc.center.isclose(arc_result.center, abs_tol=1e-5)
assert isclose(arc.radius, arc_result.radius, abs_tol=1e-5)
assert isclose(arc.start_angle, arc_result.start_angle, abs_tol=1e-4)
assert isclose(arc.end_angle, arc_result.end_angle, abs_tol=1e-4)
def test_spatial_arc_from_3p():
start_point_wcs = Vec3(0, 1, 0)
end_point_wcs = Vec3(1, 0, 0)
def_point_wcs = Vec3(0, 0, 1)
ucs = UCS.from_x_axis_and_point_in_xy(
origin=def_point_wcs,
axis=end_point_wcs - def_point_wcs,
point=start_point_wcs,
)
start_point_ucs = ucs.from_wcs(start_point_wcs)
end_point_ucs = ucs.from_wcs(end_point_wcs)
def_point_ucs = Vec3(0, 0)
arc = ConstructionArc.from_3p(start_point_ucs, end_point_ucs, def_point_ucs)
dwg = ezdxf.new("R12")
msp = dwg.modelspace()
dxf_arc = arc.add_to_layout(msp, ucs)
assert dxf_arc.dxftype() == "ARC"
assert isclose(dxf_arc.dxf.radius, 0.81649658, abs_tol=1e-9)
assert isclose(dxf_arc.dxf.start_angle, -30)
assert isclose(dxf_arc.dxf.end_angle, -150)
assert dxf_arc.dxf.extrusion.isclose(
(0.57735027, 0.57735027, 0.57735027), abs_tol=1e-9
)
def test_bounding_box():
bbox = ConstructionArc(
center=(0, 0), radius=1, start_angle=0, end_angle=90
).bounding_box
assert bbox.extmin.isclose((0, 0))
assert bbox.extmax.isclose((1, 1))
bbox = ConstructionArc(
center=(0, 0), radius=1, start_angle=0, end_angle=180
).bounding_box
assert bbox.extmin.isclose((-1, 0))
assert bbox.extmax.isclose((1, 1))
bbox = ConstructionArc(
center=(0, 0), radius=1, start_angle=270, end_angle=90
).bounding_box
assert bbox.extmin.isclose((0, -1))
assert bbox.extmax.isclose((1, 1))
def test_angles():
arc = ConstructionArc(radius=1, start_angle=30, end_angle=60)
assert tuple(arc.angles(2)) == (30, 60)
assert tuple(arc.angles(3)) == (30, 45, 60)
arc.start_angle = 180
arc.end_angle = 0
assert tuple(arc.angles(2)) == (180, 0)
assert tuple(arc.angles(3)) == (180, 270, 0)
arc.start_angle = -90
arc.end_angle = -180
assert tuple(arc.angles(2)) == (270, 180)
assert tuple(arc.angles(4)) == (270, 0, 90, 180)
def test_vertices():
angles = [0, 45, 90, 135, -45, -90, -135, 180]
arc = ConstructionArc(center=(1, 1))
vertices = list(arc.vertices(angles))
for v, a in zip(vertices, angles):
a = math.radians(a)
assert v.isclose(Vec2((1 + math.cos(a), 1 + math.sin(a))))
def test_tangents():
angles = [0, 45, 90, 135, -45, -90, -135, 180]
sin45 = math.sin(math.pi / 4)
result = [
(0, 1),
(-sin45, sin45),
(-1, 0),
(-sin45, -sin45),
(sin45, sin45),
(1, 0),
(sin45, -sin45),
(0, -1),
]
arc = ConstructionArc(center=(1, 1))
vertices = list(arc.tangents(angles))
for v, r in zip(vertices, result):
assert v.isclose(Vec2(r))
def test_angle_span():
assert ConstructionArc(start_angle=30, end_angle=270).angle_span == 240
# crossing 0-degree:
assert (
ConstructionArc(
start_angle=30, end_angle=270, is_counter_clockwise=False
).angle_span
== 120
)
# crossing 0-degree:
assert ConstructionArc(start_angle=300, end_angle=60).angle_span == 120
assert (
ConstructionArc(
start_angle=300, end_angle=60, is_counter_clockwise=False
).angle_span
== 240
)
def test_arc_segment_count():
radius = 100
max_sagitta = 2
assert arc_segment_count(radius, math.tau, max_sagitta) == 16
alpha = math.tau / 16
l2 = math.sin(alpha / 2) * radius
sagitta = radius - math.sqrt(radius**2 - l2**2)
assert max_sagitta / 2 < sagitta < max_sagitta
class TestArcSegmentCountErrors:
def test_radius_zero(self):
assert arc_segment_count(radius=0, angle=math.tau, sagitta=1) == 1
def test_sagitta_gt_radius(self):
assert arc_segment_count(radius=1, angle=math.tau, sagitta=2) == 1
def test_arc_chord_length_domain_error():
radius = 0.1
assert arc_chord_length(radius, radius * 4) == 0.0
@pytest.mark.parametrize(
"r, s, e, sagitta, count",
[
(1, 0, 180, 0.35, 3),
(1, 0, 180, 0.10, 5),
(0, 0, 360, 0.10, 0), # radius 0 works but yields nothing
(-1, 0, 180, 0.35, 3), # negative radius same as positive radius
(1, 270, 90, 0.10, 5), # start angle > end angle
(1, 90, -90, 0.10, 5),
(1, 0, 0, 0.10, 0), # angle span 0 works but yields nothing
(1, -45, -45, 0.10, 0),
],
)
def test_flattening(r, s, e, sagitta, count):
arc = ConstructionArc((0, 0), r, s, e)
assert len(list(arc.flattening(sagitta))) == count
@pytest.mark.parametrize("p", [(2, 0), (2, 2), (0, 2), (2, -2), (0, -2)])
def test_point_is_in_arc_range(p):
"""
Test if the angle defined by arc.center and point "p" is in the range
arc.start_angle to arc.end_angle:
"""
arc = ConstructionArc((0, 0), 1, -90, 90)
assert arc._is_point_in_arc_range(Vec2(p)) is True
@pytest.mark.parametrize("p", [(-2, 0), (-2, 2), (-2, -2)])
def test_point_is_not_in_arc_range(p):
"""
Test if the angle defined by arc.center and point "p" is NOT in the range
arc.start_angle to arc.end_angle:
"""
arc = ConstructionArc((0, 0), 1, -90, 90)
assert arc._is_point_in_arc_range(Vec2(p)) is False
@pytest.mark.parametrize(
"s, e",
[
[(0, 0), (2, 0)], # touches the arc
[(0, 0), (3, 0)], # intersect
[(0, 0), (0, 2)], # touches the arc
[(0, 0), (0, 3)], # intersect
[(0, 0), (2, 2)], # intersect
[(0, -1), (2, -1)], # intersect
],
)
def test_arc_intersect_line_in_one_point(s, e):
arc = ConstructionArc((0, 0), 2, -90, 90)
assert len(arc.intersect_line(ConstructionLine(s, e))) == 1
@pytest.mark.parametrize(
"s, e",
[
[(-2, 0), (2, 0)], # touches
[(-2, 1), (2, 1)], # intersect
],
)
def test_arc_intersect_line_in_two_points(s, e):
arc = ConstructionArc((0, 0), 2, 0, 180)
assert len(arc.intersect_line(ConstructionLine(s, e))) == 2
@pytest.mark.parametrize(
"s, e",
[
[(0, 2), (1, 2)],
[(2, 0), (2, 1)],
[(1, 1), (2, 2)],
],
)
def test_arc_does_not_intersect_line(s, e):
arc = ConstructionArc((0, 0), 1, 0, 90)
assert len(arc.intersect_line(ConstructionLine(s, e))) == 0
@pytest.mark.parametrize(
"c, r",
[
[(0.0, 1.0), 1.0],
[(0.0, 0.5), 0.5],
[(2.0, 0.0), 1.0],
],
)
def test_arc_intersect_circle_in_one_point(c, r):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_circle(ConstructionCircle(c, r))) == 1
@pytest.mark.parametrize(
"c, r",
[
[(1.0, 0.0), 1.0],
[(0.5, 0.0), 1.0],
],
)
def test_arc_intersect_circle_in_two_points(c, r):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_circle(ConstructionCircle(c, r))) == 2
@pytest.mark.parametrize(
"c, r",
[
[(0.0, 0.0), 0.5], # concentric circle
[(0.0, 0.0), 1.0], # concentric circle
[(0.0, 0.0), 2.0], # concentric circle
[(2.0, 0.0), 0.5], # ) O
],
)
def test_arc_does_not_intersect_circle(c, r):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_circle(ConstructionCircle(c, r))) == 0
@pytest.mark.parametrize(
"c, r, s, e",
[
[(2.0, 0.0), 1.0, 90, 270], # touches in one point: )(
[(1.5, 0.0), 1.0, 90, 180], # intersect
],
)
def test_arc_intersect_arc_in_one_point(c, r, s, e):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_arc(ConstructionArc(c, r, s, e))) == 1
@pytest.mark.parametrize(
"c, r, s, e",
[
[(0.5, 0.0), 1.0, 90, 270], # intersect
[(1.5, 0.0), 1.0, 90, 270], # intersect
],
)
def test_arc_intersect_arc_in_two_points(c, r, s, e):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_arc(ConstructionArc(c, r, s, e))) == 2
@pytest.mark.parametrize(
"c, r, s, e",
[
[(0.0, 0.0), 1.0, 90, 270], # concentric arcs
[(-0.5, 0.0), 1.0, 90, 270], # insect circle but not arc: ( )
],
)
def test_arc_does_not_intersect_arc(c, r, s, e):
arc = ConstructionArc((0, 0), 1, -90, 90)
assert len(arc.intersect_arc(ConstructionArc(c, r, s, e))) == 0
|