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

Add convenient constructor for TorusMantle #292

Merged
merged 3 commits into from
May 24, 2022
Merged
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
62 changes: 54 additions & 8 deletions src/ConstructiveSolidGeometry/SurfacePrimitives/TorusMantle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,59 @@ Surface primitive describing the mantle of a [`Torus`](@ref).
* `origin::CartesianPoint{T}`: The position of the center of the `TorusMantle`.
* `rotation::SMatrix{3,3,T,9}`: Matrix that describes a rotation of the `TorusMantle` around its `origin`.
"""
@with_kw struct TorusMantle{T,TP<:Union{Nothing,T},TT,D} <: AbstractCurvedSurfacePrimitive{T}
r_torus::T = 1
r_tube::T = 1
φ::TP = nothing
θ::TT = nothing

origin::CartesianPoint{T} = zero(CartesianPoint{T})
rotation::SMatrix{3,3,T,9} = one(SMatrix{3, 3, T, 9})
struct TorusMantle{T,TP<:Union{Nothing,T},TT,D} <: AbstractCurvedSurfacePrimitive{T}
r_torus::T
r_tube::T
φ::TP
θ::TT

origin::CartesianPoint{T}
rotation::SMatrix{3,3,T,9}
end

#Type conversion happens here
function TorusMantle{T}(D,r_torus, r_tube, φ::TP, θ, origin, rotation) where {T,TP}
_r_torus = _csg_convert_args(T, r_torus)
_r_tube = _csg_convert_args(T, r_tube)
(_φ, _rotation) = _handle_phi(_csg_convert_args(T, φ), rotation)
_θ = _csg_convert_args(T, θ)
TorusMantle{T,typeof(_φ),typeof(_θ),D}(_r_torus, _r_tube, _φ, _θ, origin, _rotation)
end

#Type promotion happens here
function TorusMantle(D,r_torus::TRTo, r_tube::TRTu, φ::Nothing, θ::TT, origin::PT, rotation::ROT) where {TRTo, TRTu, TT, PT, ROT}
eltypes = _csg_get_promoted_eltype.((TRTo, TRTu, TT, PT, ROT))
T = float(promote_type(eltypes...))
TorusMantle{T}(D,r_torus, r_tube, φ, θ, origin, rotation)
end

function TorusMantle(D,r_torus::TRTo, r_tube::TRTu, φ::TP, θ::TT, origin::PT, rotation::ROT) where {TRTo, TRTu, TP, TT, PT, ROT}
eltypes = _csg_get_promoted_eltype.((TRTo, TRTu, TP, TT, PT, ROT))
T = float(promote_type(eltypes...))
TorusMantle{T}(D,r_torus, r_tube, φ, θ, origin, rotation)
end

function TorusMantle(D::Symbol=:outwards;
# define default parameters as Int to not influence type promotion
r_torus = 1,
r_tube = 1,
φ = nothing,
θ = nothing,
origin = zero(CartesianPoint{Int}),
rotation = one(SMatrix{3, 3, Int, 9})
)
TorusMantle(D,r_torus, r_tube, φ, θ, origin, rotation)
end

function TorusMantle{T}(D::Symbol=:outwards;
r_torus = 1.0,
r_tube = 1.0,
φ = nothing,
θ= nothing,
origin = zero(CartesianPoint{Float64}),
rotation = one(SMatrix{3, 3, Float64, 9})
) where {T}
TorusMantle{T}(D,r_torus, r_tube, φ, θ, origin, rotation)
end

flip(t::TorusMantle{T,TP,TT,:inwards}) where {T,TP,TT} =
Expand All @@ -40,6 +85,7 @@ get_φ_limits(tm::TorusMantle{T,Nothing}) where {T} = T(0), T(2π)

get_θ_limits(tm::TorusMantle{T,<:Any,Tuple{T,T}}) where {T} = tm.θ[1], tm.θ[2]
get_θ_limits(tm::TorusMantle{T,<:Any,Nothing}) where {T} = T(0), T(2π)
get_θ_limits(tm::TorusMantle{T,<:Any,T}) where {T} = T(0), tm.θ
lmh91 marked this conversation as resolved.
Show resolved Hide resolved

function normal(tm::TorusMantle{T,TP,TT,:outwards}, pt::CartesianPoint{T}) where {T,TP,TT}
pto = _transform_into_object_coordinate_system(pt, tm)
Expand Down