Skip to content

Constructor with units #305

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
7 changes: 5 additions & 2 deletions src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@ module ConstructiveSolidGeometry

abstract type AbstractConstructiveGeometry{T} <: AbstractGeometry{T} end

_csg_convert_args(eltype::Type{T}, r::Real) where T = convert(T, r)
_csg_convert_args(eltype::Type{T}, r::Real) where T = convert(T, r)
_csg_convert_args(eltype::Type{T}, r::Tuple) where T = broadcast(x -> _csg_convert_args(T, x), r)
_csg_convert_args(eltype::Type{T}, r::Nothing) where T = nothing
_csg_convert_args(eltype::Type{T}, r::Quantity) where T = convert(T,ustrip(uconvert(u"m",r)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works for LengthQuantity, not for angles etc.

_csg_convert_args(eltype::Type{T}, r::PtOrVec) where {T, PtOrVec<:StaticArrays.FieldVector} = broadcast(x -> _csg_convert_args(T, x), r)

_csg_get_promoted_eltype(::Type{T}) where {T <: AbstractArray} = eltype(T)
_csg_get_promoted_eltype(::Type{T}) where {T <: AbstractArray} = _csg_get_promoted_eltype.(eltype(T))
_csg_get_promoted_eltype(::Type{T}) where {T <: Real} = T
_csg_get_promoted_eltype(::Type{Nothing}) = Int
_csg_get_promoted_eltype(::Type{Tuple{T}}) where {T<:Real} = T
_csg_get_promoted_eltype(::Type{Tuple{T1,T2}}) where {T1<:Real, T2<:Real} = promote_type(T1, T2)
_csg_get_promoted_eltype(::Type{Tuple{T1,T2}}) where {T1<:Union{Real, Tuple}, T2<:Union{Real, Tuple}} = promote_type(_csg_get_promoted_eltype(T1), _csg_get_promoted_eltype(T2))
_csg_get_promoted_eltype(::Type{Tuple{Nothing,T2}}) where {T2<:Union{Real, Tuple}} = _csg_get_promoted_eltype(T2)
_csg_get_promoted_eltype(::Type{Tuple{T1,Nothing}}) where {T1<:Union{Real, Tuple}} = _csg_get_promoted_eltype(T1)
_csg_get_promoted_eltype(::Type{Quan}) where {Quan<:Quantity}= Quan.parameters[1]

_handle_phi(φ, rotation) = (φ, rotation)
_handle_phi(φ::Tuple, rotation) = (abs(φ[2]-φ[1]), rotation*RotZ(φ[1]))
Expand Down
2 changes: 2 additions & 0 deletions src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ end
@inline _convert_point(pt::AbstractCoordinatePoint, ::Type{Cylindrical}) = CylindricalPoint(pt)
@inline _convert_point(pt::AbstractCoordinatePoint, ::Type{Cartesian}) = CartesianPoint(pt)

scale(cart::CartesianPoint{T}, fact) where T = CartesianPoint{T}(cart.x*fact, cart.y*fact, cart.z*fact)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be careful with this!
If we look at the docstring, CartesianPoint will always be saved in units of m.
I would prefer to have a constructor with units for CartesianPoint and solve this via the constructor and not with scale..


# function _Δφ(φ1::T, φ2::T)::T where {T}
# δφ = mod(φ2 - φ1, T(2π))
# min(δφ, T(2π) - δφ)
Expand Down
7 changes: 4 additions & 3 deletions src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ function Box{T}(CO,hX, hY, hZ, origin, rotation) where {T}
_hX = _csg_convert_args(T, hX)
_hY = _csg_convert_args(T, hY)
_hZ = _csg_convert_args(T, hZ)
Box{T,CO}(_hX, _hY, _hZ, origin, rotation)
_origin = _csg_convert_args(T, origin)
Box{T,CO}(_hX, _hY, _hZ, _origin, rotation)
end

#Type promotion happens here
function Box(CO, hX::TX, hY::TY, hZ::TZ, origin::PT, rotation::ROT) where {TX, TY, TZ, PT, ROT}
eltypes = _csg_get_promoted_eltype.((TX, TY, TZ, PT, ROT))
function Box(CO, hX::TX, hY::TY, hZ::TZ, origin::PT, rotation::ROT) where {TX,TY,TZ,PT,ROT}
eltypes = _csg_get_promoted_eltype.((TX,TY,TZ,PT,ROT))
T = float(promote_type(eltypes...))
Box{T}(CO,T(hX), T(hY), T(hZ), origin, rotation)
end
Expand Down
4 changes: 2 additions & 2 deletions test/ConstructiveSolidGeometry/CSG_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ no_translations = (rotation = one(SMatrix{3, 3, T, 9}), translation = zero(Carte
@test in(CartesianPoint{Float64}(1e-8,0,0),ellip_open_trafo)
end
@testset "Box" begin
box1 = @inferred CSG.Box(CSG.ClosedPrimitive,hX=1f0, hY=2f0, hZ=1f0, origin = zero(CartesianPoint{Float16}),rotation = one(SMatrix{3, 3, Float16, 9}))
box2 = @inferred CSG.Box{Float32}(hX=1.0, hY=2f0, hZ=1f0)
box1 = @inferred CSG.Box(CSG.ClosedPrimitive,hX=1f0u"mm", hY=2f0, hZ=1f0, origin = CartesianPoint(1u"mm",1u"nm",1u"m"),rotation = one(SMatrix{3, 3, Float16, 9}))
box2 = @inferred CSG.Box{Float32}(hX=1.0u"mm", hY=2f0, hZ=1f0, origin = CartesianPoint(1u"mm",1u"nm",1u"m"))
@test box1 === box2

dict = Dict("box" => Dict(
Expand Down