Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance to Surface for Remaining Surface Primitives #159

Merged
merged 4 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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