Skip to content

Commit

Permalink
Merge pull request #159 from hervasa2/v0.6
Browse files Browse the repository at this point in the history
Distance to Surface for Remaining Surface Primitives
  • Loading branch information
fhagemann authored May 17, 2021
2 parents 68f90b8 + 8997784 commit 493ddc6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/ConstructiveSolidGeometry/SurfacePrimitives/Rectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,32 @@ function get_vertices(r::RectangleZ{T}) where {T}
CartesianPoint{T}(lMax, wMin, r.loc),
CartesianPoint{T}(lMin, wMax, r.loc),
CartesianPoint{T}(lMax, wMax, r.loc))
end

_get_rectangle_coordinates(point::CartesianPoint, r::RectangleX) = (point.y, point.z, point.x)
_get_rectangle_coordinates(point::CartesianPoint, r::RectangleY) = (point.x, point.z, point.y)
_get_rectangle_coordinates(point::CartesianPoint, r::RectangleZ) = (point.x, point.y, point.z)

_get_rectangle_coordinates(point::CylindricalPoint, r::Rectangle) = _get_rectangle_coordinates(CartesianPoint(point), r)

function distance_to_surface(point::AbstractCoordinatePoint{T}, r::Rectangle{<:Any, T})::T where {T}
l, w, d = _get_rectangle_coordinates(point, r)
lMin::T, lMax::T = get_l_limits(r)
wMin::T, wMax::T = get_w_limits(r)
if lMin l lMax && wMin w wMax
return abs(d - r.loc)
else
if lMin l lMax
y = w < wMin ? wMin : wMax
return hypot(w - y, d - r.loc)
elseif wMin w wMax
x = l < lMin ? lMin : lMax
return hypot(l - x, d - r.loc)
else
lnear = l < lMin ? lMin : lMax
wnear = w < wMin ? wMin : wMax
return hypot(l - lnear, w - wnear, d - r.loc)
end
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function RegularPolygon(N::Integer, rOuter::R, z::Z) where {R<:Real, Z<:Real}
RegularPolygon( N, T, T(rOuter), T(z))
end


@inline in(p::CylindricalPoint, rp::RegularPolygon{N, T, <:Real}) where {N,T} = begin
_isapprox_z(p, rp.z) && p.r * cos(T/N) - mod(p.φ, T(2π/N))) / cos(T/N)) <= rp.r
end
Expand Down Expand Up @@ -129,4 +128,21 @@ function sample(rp::RegularPolygon{N,T}, g::CartesianTicksTuple{T})::Vector{Cart
for x in vcat(get_missing_x_at_y(rMin, corners, g, y), get_missing_x_at_y(rMax, corners, g, y))
]
)
end

function distance_to_surface(point::AbstractCoordinatePoint{T}, rp::RegularPolygon{N,T})::T where {N,T}
pcy = CylindricalPoint(point)
rMin::T, rMax::T = get_r_limits(rp)
φ = mod(pcy.φ, T(2π/N))
r_poly = pcy.r * cos(T/N) - φ) / cos(T/N))
if rMin r_poly rMax
return abs(pcy.z - rp.z)
else
sn, cn = sincos(2π/N)
r = r_poly < rMin ? rMin : rMax
line = LineSegment(T, PlanarPoint{T}(r, 0), PlanarPoint{T}(r*cn, r*sn))
sφ, cφ = sincos(φ)
d = distance_to_line(PlanarPoint{T}(pcy.r*cφ, pcy.r*sφ), line)
return hypot(d, pcy.z - rp.z)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ function sample(rp::RegularPrismMantle{N,T}, g::CartesianTicksTuple{T})::Vector{
)
end

function distance_to_surface(point::AbstractCoordinatePoint{T}, rp::RegularPrismMantle{N,T})::T where {N,T}
pcy = CylindricalPoint(point)
zMin::T, zMax::T = get_z_limits(rp)
φ = mod(pcy.φ, T(2π/N))
sn, cn = sincos(2π/N)
line = LineSegment(T, PlanarPoint{T}(rp.r, 0), PlanarPoint{T}(rp.r*cn, rp.r*sn))
sφ, cφ = sincos(φ)
d = distance_to_line(PlanarPoint{T}(pcy.r*cφ, pcy.r*sφ), line)
if zMin pcy.z zMax
return d
else
z = pcy.z < zMin ? zMin : zMax
return hypot(d, pcy.z - z)
end
end

0 comments on commit 493ddc6

Please sign in to comment.