From 47c2294e5f2d295ddd35135b485d6fb2d77b98e7 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Wed, 15 Jun 2022 08:58:49 +0200 Subject: [PATCH 01/15] Add unit conversion --- .../VolumePrimitives/Box.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index add510ef6..91c57cba4 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -70,10 +70,18 @@ function Box(CO, hX::TX, hY::TY, hZ::TZ, origin::PT, rotation::ROT) where {TX, T Box{T}(CO,T(hX), T(hY), T(hZ), origin, rotation) end +#Unit convesion +function Box(CO, hX::Quantity, hY::Quantity, hZ::Quantity, origin, rotation) + _hX = to_internal_units(hX) + _hY = to_internal_units(hY) + _hZ = to_internal_units(hZ) + Box(CO, _hX, _hY, _hZ, origin, rotation) +end + function Box(::Type{CO}=ClosedPrimitive; - hX = 1, - hY = 1, - hZ = 1, + hX = 1u"m", + hY = 1u"m", + hZ = 1u"m", origin = zero(CartesianPoint{Int}), rotation = one(SMatrix{3, 3, Int, 9}) ) where {CO} From 9e2c58799f33931da079f16a198c1260e378d251 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Wed, 15 Jun 2022 11:03:49 +0200 Subject: [PATCH 02/15] Other tries --- src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index 91c57cba4..390273d54 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -79,9 +79,9 @@ function Box(CO, hX::Quantity, hY::Quantity, hZ::Quantity, origin, rotation) end function Box(::Type{CO}=ClosedPrimitive; - hX = 1u"m", - hY = 1u"m", - hZ = 1u"m", + hX = 1, + hY = 1, + hZ = 1, origin = zero(CartesianPoint{Int}), rotation = one(SMatrix{3, 3, Int, 9}) ) where {CO} From fe3ad82fcf4848447d2ddccf0899f382767b2fe4 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Wed, 15 Jun 2022 11:33:01 +0200 Subject: [PATCH 03/15] Unit conversion using permutations of argument types --- .../VolumePrimitives/Box.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index 390273d54..07eec2370 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -71,12 +71,13 @@ function Box(CO, hX::TX, hY::TY, hZ::TZ, origin::PT, rotation::ROT) where {TX, T end #Unit convesion -function Box(CO, hX::Quantity, hY::Quantity, hZ::Quantity, origin, rotation) - _hX = to_internal_units(hX) - _hY = to_internal_units(hY) - _hZ = to_internal_units(hZ) - Box(CO, _hX, _hY, _hZ, origin, rotation) -end +Box(CO, hX::Quantity, hY, hZ, origin, rotation) = Box(CO, to_internal_units(hX), hY, hZ, origin, rotation) +Box(CO, hX, hY::Quantity, hZ, origin, rotation) = Box(CO, hX, to_internal_units(hY), hZ, origin, rotation) +Box(CO, hX, hY, hZ::Quantity, origin, rotation) = Box(CO, hX, hY, to_internal_units(hZ), origin, rotation) +Box(CO, hX::Quantity, hY::Quantity, hZ, origin, rotation) = Box(CO, to_internal_units(hX), to_internal_units(hY), hZ, origin, rotation) +Box(CO, hX, hY::Quantity, hZ::Quantity, origin, rotation) = Box(CO, hX, to_internal_units(hY), to_internal_units(hZ), origin, rotation) +Box(CO, hX::Quantity, hY, hZ::Quantity, origin, rotation) = Box(CO, to_internal_units(hX), hY, to_internal_units(hZ), origin, rotation) +Box(CO, hX::Quantity, hY::Quantity, hZ::Quantity, origin, rotation) = Box(CO, to_internal_units(hX), to_internal_units(hY), to_internal_units(hZ), origin, rotation) function Box(::Type{CO}=ClosedPrimitive; hX = 1, From 275b0c106ccb4cc476586658e1581a1496ce26b6 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 21 Jun 2022 09:51:19 +0200 Subject: [PATCH 04/15] Add constructor with units as function argument --- src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index 07eec2370..dde292ed5 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -89,14 +89,16 @@ function Box(::Type{CO}=ClosedPrimitive; Box(CO, hX, hY, hZ, origin, rotation) end -function Box{T}(::Type{CO}=ClosedPrimitive; +function Box{T}(unt::UN = u"m", + ::Type{CO}=ClosedPrimitive; hX = 1.0, hY = 1.0, hZ = 1.0, origin = zero(CartesianPoint{Float64}), rotation = one(SMatrix{3, 3, Float64, 9}) -) where {T, CO} - Box{T}(CO, hX, hY, hZ, origin, rotation) +) where {T, CO, UN<:Unitful.Units} + unit_factor = ustrip(uconvert(unt,1u"m")) + Box{T}(CO, hX/unit_factor, hY/unit_factor, hZ/unit_factor, scale(CartesianPoint{T}(origin),1/unit_factor), rotation) end Box{T, CO}( b::Box{T, CO}; COT = CO, From 71a38d702a8c54c1ecbf5a1ca06fa7db19dea140 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 21 Jun 2022 09:51:52 +0200 Subject: [PATCH 05/15] Add scale function for `CartesianPoint` --- src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl b/src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl index 00cc5db8c..612999b67 100644 --- a/src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl +++ b/src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl @@ -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) + # function _Δφ(φ1::T, φ2::T)::T where {T} # δφ = mod(φ2 - φ1, T(2π)) # min(δφ, T(2π) - δφ) From efd89fc71b518fa3dae947e75005d4db62309d5e Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 21 Jun 2022 10:35:29 +0200 Subject: [PATCH 06/15] Adapt Geometry to unit option --- src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index dde292ed5..06c537e78 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -131,7 +131,7 @@ function Geometry(::Type{T}, ::Type{Box}, dict::AbstractDict, input_units::Named _parse_value(T, dict["hY"], length_unit), _parse_value(T, dict["hZ"], length_unit) end - box = Box{T}(ClosedPrimitive, + box = Box{T}(u"m",ClosedPrimitive, hX = hX, hY = hY, hZ = hZ, From 5e6ee6e289983ba647fb016ad79d5862f5a6bfec Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Thu, 7 Jul 2022 11:48:42 +0200 Subject: [PATCH 07/15] Enable constructor with units for `Box` --- .../VolumePrimitives/Box.jl | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index 06c537e78..b1575a68a 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -60,25 +60,17 @@ 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 -#Unit convesion -Box(CO, hX::Quantity, hY, hZ, origin, rotation) = Box(CO, to_internal_units(hX), hY, hZ, origin, rotation) -Box(CO, hX, hY::Quantity, hZ, origin, rotation) = Box(CO, hX, to_internal_units(hY), hZ, origin, rotation) -Box(CO, hX, hY, hZ::Quantity, origin, rotation) = Box(CO, hX, hY, to_internal_units(hZ), origin, rotation) -Box(CO, hX::Quantity, hY::Quantity, hZ, origin, rotation) = Box(CO, to_internal_units(hX), to_internal_units(hY), hZ, origin, rotation) -Box(CO, hX, hY::Quantity, hZ::Quantity, origin, rotation) = Box(CO, hX, to_internal_units(hY), to_internal_units(hZ), origin, rotation) -Box(CO, hX::Quantity, hY, hZ::Quantity, origin, rotation) = Box(CO, to_internal_units(hX), hY, to_internal_units(hZ), origin, rotation) -Box(CO, hX::Quantity, hY::Quantity, hZ::Quantity, origin, rotation) = Box(CO, to_internal_units(hX), to_internal_units(hY), to_internal_units(hZ), origin, rotation) - function Box(::Type{CO}=ClosedPrimitive; hX = 1, hY = 1, @@ -89,16 +81,14 @@ function Box(::Type{CO}=ClosedPrimitive; Box(CO, hX, hY, hZ, origin, rotation) end -function Box{T}(unt::UN = u"m", - ::Type{CO}=ClosedPrimitive; +function Box{T}(::Type{CO}=ClosedPrimitive; hX = 1.0, hY = 1.0, hZ = 1.0, origin = zero(CartesianPoint{Float64}), rotation = one(SMatrix{3, 3, Float64, 9}) -) where {T, CO, UN<:Unitful.Units} - unit_factor = ustrip(uconvert(unt,1u"m")) - Box{T}(CO, hX/unit_factor, hY/unit_factor, hZ/unit_factor, scale(CartesianPoint{T}(origin),1/unit_factor), rotation) +) where {T, CO} + Box{T}(CO, hX, hY, hZ, origin, rotation) end Box{T, CO}( b::Box{T, CO}; COT = CO, @@ -131,7 +121,7 @@ function Geometry(::Type{T}, ::Type{Box}, dict::AbstractDict, input_units::Named _parse_value(T, dict["hY"], length_unit), _parse_value(T, dict["hZ"], length_unit) end - box = Box{T}(u"m",ClosedPrimitive, + box = Box{T}(ClosedPrimitive, hX = hX, hY = hY, hZ = hZ, From ea697b2b0dadc4f31b1808f1aff57edf1175d4c0 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Thu, 7 Jul 2022 11:50:18 +0200 Subject: [PATCH 08/15] Add conversion and promotion functions for Quantities --- src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl index 80f3a37bd..db4d71cf3 100644 --- a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl +++ b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl @@ -50,11 +50,13 @@ 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))) + _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 @@ -62,6 +64,7 @@ module ConstructiveSolidGeometry _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])) From 3c62d0bf385d6f1b2e5c12476914f6af2235fd1d Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Thu, 7 Jul 2022 12:27:02 +0200 Subject: [PATCH 09/15] Use units in tests --- test/ConstructiveSolidGeometry/CSG_primitives.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ConstructiveSolidGeometry/CSG_primitives.jl b/test/ConstructiveSolidGeometry/CSG_primitives.jl index 692dd8adc..ca7273d38 100644 --- a/test/ConstructiveSolidGeometry/CSG_primitives.jl +++ b/test/ConstructiveSolidGeometry/CSG_primitives.jl @@ -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( From 56e4fc90e6e093b33f025eece69578ebe961a207 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Thu, 4 Aug 2022 09:47:04 +0200 Subject: [PATCH 10/15] Add `Box` with Quantity --- src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl index db4d71cf3..c2a6ef840 100644 --- a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl +++ b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl @@ -38,7 +38,7 @@ module ConstructiveSolidGeometry const CartesianTicksTuple{T} = NamedTuple{(:x,:y,:z), NTuple{3,Vector{T}}} const CylindricalTicksTuple{T} = NamedTuple{(:r,:φ,:z), NTuple{3,Vector{T}}} - abstract type AbstractGeometry{T <: AbstractFloat} end + abstract type AbstractGeometry{T <: Number} end abstract type AbstractPrimitive{T} <: AbstractGeometry{T} end abstract type ClosedPrimitive end @@ -53,7 +53,8 @@ module ConstructiveSolidGeometry _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))) + _csg_convert_args(eltype::Type{Quantity{T}}, r::Quantity) where {T} = T(r) + _csg_convert_args(eltype::Type{Quantity{T}}, r::Real) where {T} = T(r) * T(1)u"m" _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} = _csg_get_promoted_eltype.(eltype(T)) From 783bf9f38d24a2a98de5a44cf7a2de402c0dc281 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Thu, 4 Aug 2022 09:53:21 +0200 Subject: [PATCH 11/15] Type promotion with quantities --- src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl index c2a6ef840..7a0d17b63 100644 --- a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl +++ b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl @@ -65,7 +65,7 @@ module ConstructiveSolidGeometry _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] + _csg_get_promoted_eltype(::Type{Q}) where {Q <: Quantity}= Q _handle_phi(φ, rotation) = (φ, rotation) _handle_phi(φ::Tuple, rotation) = (abs(φ[2]-φ[1]), rotation*RotZ(φ[1])) From 8278c9f1105e1038153ac38fd1769e335e406a21 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 9 Aug 2022 12:17:43 +0200 Subject: [PATCH 12/15] Split precision type and T in Box --- .../ConstructiveSolidGeometry.jl | 9 ++++++++- .../VolumePrimitives/Box.jl | 16 +++++++++------- test/ConstructiveSolidGeometry/CSG_primitives.jl | 2 +- test/runtests.jl | 14 +++++++------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl index 7a0d17b63..02db82a9f 100644 --- a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl +++ b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl @@ -28,7 +28,7 @@ module ConstructiveSolidGeometry abstract type Cylindrical <: AbstractCoordinateSystem end const CoordinateSystemType = Union{Type{Cartesian}, Type{Cylindrical}} - import Base: print, show + import Base: print, show, float print(io::IO, ::Type{Cartesian}) = print(io, "Cartesian") print(io::IO, ::Type{Cylindrical}) = print(io, "Cylindrical") show(io::IO, CS::CoordinateSystemType) = print(io, CS) @@ -56,6 +56,8 @@ module ConstructiveSolidGeometry _csg_convert_args(eltype::Type{Quantity{T}}, r::Quantity) where {T} = T(r) _csg_convert_args(eltype::Type{Quantity{T}}, r::Real) where {T} = T(r) * T(1)u"m" _csg_convert_args(eltype::Type{T}, r::PtOrVec) where {T, PtOrVec<:StaticArrays.FieldVector} = broadcast(x -> _csg_convert_args(T, x), r) + _csg_convert_args(::Type{RT}, ::Type{T}) where {RT<:Real,T<:Real} = RT + _csg_convert_args(::Type{RT}, ::Type{T}) where {RT<:Real,T<:Quantity} = Quantity{RT} _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 @@ -69,6 +71,11 @@ module ConstructiveSolidGeometry _handle_phi(φ, rotation) = (φ, rotation) _handle_phi(φ::Tuple, rotation) = (abs(φ[2]-φ[1]), rotation*RotZ(φ[1])) + + _precision_type(::Type{T}) where {T <: Real} = T + _precision_type(::Type{Quantity{T}}) where {T <: Real} = T + + float(::Type{Quantity{T}}) where T = T include("Units.jl") include("PointsAndVectors/PointsAndVectors.jl") diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index b1575a68a..230134cc9 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -46,29 +46,30 @@ box: See also [Constructive Solid Geometry (CSG)](@ref). """ -struct Box{T,CO} <: AbstractVolumePrimitive{T,CO} +struct Box{T,RT,CO} <: AbstractVolumePrimitive{T,CO} hX::T hY::T hZ::T origin::CartesianPoint{T} - rotation::SMatrix{3,3,T,9} + rotation::SMatrix{3,3,RT,9} end #Type conversion happens here -function Box{T}(CO,hX, hY, hZ, origin, rotation) where {T} +function Box{T,RT}(CO, hX, hY, hZ, origin, rotation) where {T,RT<:Real} _hX = _csg_convert_args(T, hX) _hY = _csg_convert_args(T, hY) _hZ = _csg_convert_args(T, hZ) _origin = _csg_convert_args(T, origin) - Box{T,CO}(_hX, _hY, _hZ, _origin, rotation) + Box{T,RT,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)) - T = float(promote_type(eltypes...)) - Box{T}(CO,T(hX), T(hY), T(hZ), origin, rotation) + RT = float(promote_type(eltypes...)) + T = _csg_convert_args(RT,typeof(promote_type(eltypes...))) + Box{T,RT}(CO, RT(hX), RT(hY), RT(hZ), origin, rotation) end function Box(::Type{CO}=ClosedPrimitive; @@ -88,7 +89,8 @@ function Box{T}(::Type{CO}=ClosedPrimitive; origin = zero(CartesianPoint{Float64}), rotation = one(SMatrix{3, 3, Float64, 9}) ) where {T, CO} - Box{T}(CO, hX, hY, hZ, origin, rotation) + RT = _precision_type(T) + Box{T,RT}(CO, hX, hY, hZ, origin, rotation) end Box{T, CO}( b::Box{T, CO}; COT = CO, diff --git a/test/ConstructiveSolidGeometry/CSG_primitives.jl b/test/ConstructiveSolidGeometry/CSG_primitives.jl index ca7273d38..b0cdd9f8f 100644 --- a/test/ConstructiveSolidGeometry/CSG_primitives.jl +++ b/test/ConstructiveSolidGeometry/CSG_primitives.jl @@ -185,7 +185,7 @@ 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=1f0u"mm", hY=2f0, hZ=1f0, origin = CartesianPoint(1u"mm",1u"nm",1u"m"),rotation = one(SMatrix{3, 3, Float16, 9})) + box1 = @inferred CSG.Box(CSG.ClosedPrimitive,hX=1f0, hY=2f0, hZ=1f0, origin = CartesianPoint(1,1,1),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 diff --git a/test/runtests.jl b/test/runtests.jl index cfe840480..79d774243 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,13 +13,13 @@ using Unitful T = Float32 device_array_type = (@isdefined CUDAKernels) ? CUDAKernels.CUDA.CuArray : Array -@testset "Comparison to analytic solutions" begin - include("comparison_to_analytic_solutions.jl") -end - -@testset "SOR GPU Backend" begin - include("SOR_GPU_Backend.jl") -end +# @testset "Comparison to analytic solutions" begin +# include("comparison_to_analytic_solutions.jl") +# end +# +# @testset "SOR GPU Backend" begin +# include("SOR_GPU_Backend.jl") +# end @testset "ConstructiveSolidGeometry" begin include("ConstructiveSolidGeometry/CSG_IO.jl") From dd4d9e656dbcbce6b764bbaf10fc20a26adc4f01 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 16 Aug 2022 14:48:49 +0200 Subject: [PATCH 13/15] Fix bugs --- .../VolumePrimitives/Box.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl index 230134cc9..7662738b7 100644 --- a/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl +++ b/src/ConstructiveSolidGeometry/VolumePrimitives/Box.jl @@ -67,8 +67,8 @@ 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)) - RT = float(promote_type(eltypes...)) - T = _csg_convert_args(RT,typeof(promote_type(eltypes...))) + RT = _float_precision(promote_type(eltypes...)) + T = _csg_convert_args(RT,promote_type(eltypes...)) Box{T,RT}(CO, RT(hX), RT(hY), RT(hZ), origin, rotation) end @@ -93,18 +93,18 @@ function Box{T}(::Type{CO}=ClosedPrimitive; Box{T,RT}(CO, hX, hY, hZ, origin, rotation) end -Box{T, CO}( b::Box{T, CO}; COT = CO, +Box{T, RT, CO}( b::Box{T, RT, CO}; COT = CO, origin::CartesianPoint{T} = b.origin, - rotation::SMatrix{3,3,T,9} = b.rotation) where {T, CO<:Union{ClosedPrimitive, OpenPrimitive}} = - Box{T, COT}(b.hX, b.hY, b.hZ, origin, rotation) + rotation::SMatrix{3,3,T,9} = b.rotation) where {T, RT, CO<:Union{ClosedPrimitive, OpenPrimitive}} = + Box{T, RT, COT}(b.hX, b.hY, b.hZ, origin, rotation) -function _in(pt::CartesianPoint{T}, b::Box{<:Any, ClosedPrimitive}; csgtol::T = csg_default_tol(T)) where {T} +function _in(pt::CartesianPoint{T}, b::Box{<:Any, <:Any, ClosedPrimitive}; csgtol::T = csg_default_tol(T)) where {T} abs(pt.x) <= b.hX + csgtol && abs(pt.y) <= b.hY + csgtol && abs(pt.z) <= b.hZ + csgtol end -_in(pt::CartesianPoint{T}, b::Box{<:Any, OpenPrimitive}; csgtol::T = csg_default_tol(T)) where {T} = +_in(pt::CartesianPoint{T}, b::Box{<:Any, <:Any, OpenPrimitive}; csgtol::T = csg_default_tol(T)) where {T} = abs(pt.x) < b.hX - csgtol && abs(pt.y) < b.hY - csgtol && abs(pt.z) < b.hZ - csgtol From 6126087718342c89e5f11939345edef9dd2057bb Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 16 Aug 2022 14:49:33 +0200 Subject: [PATCH 14/15] Fix type piracy --- src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl index 02db82a9f..8bfbd4878 100644 --- a/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl +++ b/src/ConstructiveSolidGeometry/ConstructiveSolidGeometry.jl @@ -75,7 +75,8 @@ module ConstructiveSolidGeometry _precision_type(::Type{T}) where {T <: Real} = T _precision_type(::Type{Quantity{T}}) where {T <: Real} = T - float(::Type{Quantity{T}}) where T = T + _float_precision(::Type{Quantity{T}}) where T = float(T) + _float_precision(::Type{T}) where T = float(T) include("Units.jl") include("PointsAndVectors/PointsAndVectors.jl") From 772bbdba387608ac0a03ed1329bf1ee0492f81a1 Mon Sep 17 00:00:00 2001 From: Sebastian Ruffert Date: Tue, 16 Aug 2022 14:50:09 +0200 Subject: [PATCH 15/15] Adapt tests to box with units --- test/ConstructiveSolidGeometry/CSG_primitives.jl | 7 +++++-- test/runtests.jl | 14 +++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/test/ConstructiveSolidGeometry/CSG_primitives.jl b/test/ConstructiveSolidGeometry/CSG_primitives.jl index b0cdd9f8f..8aeb8ac98 100644 --- a/test/ConstructiveSolidGeometry/CSG_primitives.jl +++ b/test/ConstructiveSolidGeometry/CSG_primitives.jl @@ -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 = CartesianPoint(1,1,1),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")) + box1 = @inferred CSG.Box(CSG.ClosedPrimitive,hX=1f0, hY=2f0, hZ=1f0, origin = CartesianPoint{Float32}(1,1,1),rotation = one(SMatrix{3, 3, Float16, 9})) + box2 = @inferred CSG.Box{Float32}(hX=1.0, hY=2f0, hZ=1f0, origin = CartesianPoint(1,1,1)) @test box1 === box2 dict = Dict("box" => Dict( @@ -204,6 +204,9 @@ no_translations = (rotation = one(SMatrix{3, 3, T, 9}), translation = zero(Carte @test !in(CartesianPoint{Float64}(1,1,3),box_open_trafo) @test !in(CartesianPoint{Float64}(1,1,3+tol),box_closed_trafo) @test in(CartesianPoint{Float64}(1,1,3-tol),box_open_trafo) + # Test box with units + @inferred CSG.Box(CSG.ClosedPrimitive, hX=1u"m", hY=2f0, hZ=1f0) + @inferred CSG.Box{Quantity{Float32}}(CSG.ClosedPrimitive, hX=1u"mm", hY=2f0, hZ=1f0) end @testset "RegularPrism" begin prism1 = @inferred CSG.RegularPrism{3}(CSG.ClosedPrimitive,r=1f0, hZ = 2f0) diff --git a/test/runtests.jl b/test/runtests.jl index 79d774243..cfe840480 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,13 +13,13 @@ using Unitful T = Float32 device_array_type = (@isdefined CUDAKernels) ? CUDAKernels.CUDA.CuArray : Array -# @testset "Comparison to analytic solutions" begin -# include("comparison_to_analytic_solutions.jl") -# end -# -# @testset "SOR GPU Backend" begin -# include("SOR_GPU_Backend.jl") -# end +@testset "Comparison to analytic solutions" begin + include("comparison_to_analytic_solutions.jl") +end + +@testset "SOR GPU Backend" begin + include("SOR_GPU_Backend.jl") +end @testset "ConstructiveSolidGeometry" begin include("ConstructiveSolidGeometry/CSG_IO.jl")