Skip to content

Commit

Permalink
Merge pull request #51 from davibarreira/bug-arc
Browse files Browse the repository at this point in the history
🐛 Fixing arc primitive.
  • Loading branch information
davibarreira authored Oct 4, 2024
2 parents 64c719e + 69fe5de commit c30191d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/primitives/arc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ struct Arc <: GeometricPrimitive
function Arc(rx, ry, c, rot, initangle, finalangle)
@assert rx > 0.0 "Radius must be a positive real number"
@assert ry 0.0 "Radius must be a positive real number"
if finalangle 2π || finalangle 2π
finalangle = 2π * 0.999999

initangle = mod2pi(Float64(initangle))
finalangle = mod2pi(Float64(finalangle))
if initangle > finalangle
initangle = initangle - 2π
end

return new(rx, ry, c, rot, initangle, finalangle)
Expand Down Expand Up @@ -54,20 +57,21 @@ function ψ(p::CovArc)

# Compute the angle. Note that this solves for p = [rx * cos(ang), ry * cos(ang)]
# For which we wish to find `ang`.
x,y = rotatevec(p._4 - p._3,-rot)
initangle = atan2pi([x/rx,y/ry])
x, y = rotatevec(p._4 - p._3, -rot)
initangle = atan2pi([x / rx, y / ry])

x, y = rotatevec(p._5 - p._3, -rot)
finalangle = atan2pi([x / rx, y / ry])

x,y = rotatevec(p._5 - p._3,-rot)
finalangle = atan2pi([x/rx,y/ry])
return Arc(rx, ry, c, rot, initangle, finalangle)
end

function ϕ(p::Arc)
p1 = rotatevec([p.rx, 0], p.rot) + p.c
p2 = rotatevec([0, p.ry], p.rot) + p.c
p3 = p.c
p4 = rotatevec(point_on_ellipse(p.initangle, p.rx, p.ry, [0.,0.]), p.rot) + p.c
p5 = rotatevec(point_on_ellipse(p.finalangle, p.rx, p.ry, [0.,0.]), p.rot) + p.c
p4 = rotatevec(point_on_ellipse(p.initangle, p.rx, p.ry, [0.0, 0.0]), p.rot) + p.c
p5 = rotatevec(point_on_ellipse(p.finalangle, p.rx, p.ry, [0.0, 0.0]), p.rot) + p.c
return CovArc(p1, p2, p3, p4, p5)
end

Expand Down
34 changes: 28 additions & 6 deletions test/primitives/test_arc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ end
initangle = π / 6
finalangle = π
arc = Arc(rx=rx, ry=ry, c=c, rot=rot, initangle=initangle, finalangle=finalangle)

@test arc.rx == rx
@test arc.ry == ry
@test arc.c == c
@test arc.rot == rot
@test arc.initangle == initangle
@test arc.finalangle == finalangle
@test arc.initangle initangle
@test arc.finalangle finalangle
end

# Test Arc with invalid parameters
Expand All @@ -37,19 +37,41 @@ end
# Test Arc finalangle adjustment
@testset "Arc Final Angle Adjustment" begin
arc = Arc(1, 1, [0, 0], 0, 0, 2π)
@test arc.finalangle 2π * 0.999999
@test arc.finalangle 2π
end

# Test conversion between Arc and CovArc
@testset "Arc and CovArc Conversion" begin
arc = Arc(2.0, 3.0, [1.0, 1.0], π / 4, π / 6, π)
covarc = ϕ(arc)
arc_converted = ψ(covarc)


@test arc.rx arc_converted.rx
@test arc.ry arc_converted.ry
@test arc.c arc_converted.c
@test arc.rot arc_converted.rot
@test arc.initangle arc_converted.initangle
@test arc.finalangle arc_converted.finalangle


# For negative angles
arc = Arc(c=[0, 0], rx=2, ry=1.0, initangle=-π / 4, finalangle=π + π / 4, rot=0π / 4)
covarc = ϕ(arc)
arc_converted = ψ(covarc)
@test arc.rx arc_converted.rx
@test arc.ry arc_converted.ry
@test arc.c arc_converted.c
@test arc.rot arc_converted.rot
@test arc.initangle arc_converted.initangle
@test arc.finalangle arc_converted.finalangle
end

arc = Arc(c=[0, 0], rx=1, ry=1.0, initangle=-π / 4, finalangle=-π + π / 4, rot=0π / 4)
covarc = ϕ(arc)
arc_converted = ψ(covarc)
@test arc.rx arc_converted.rx
@test arc.ry arc_converted.ry
@test arc.c arc_converted.c
@test arc.rot arc_converted.rot
@test arc.initangle arc_converted.initangle
@test arc.finalangle arc_converted.finalangle
end

0 comments on commit c30191d

Please sign in to comment.