Skip to content

Commit

Permalink
Merge pull request #303 from SebastianRuffert/VectorConstructor
Browse files Browse the repository at this point in the history
Add convenient constructor for vector
  • Loading branch information
lmh91 authored Jun 16, 2022
2 parents f1c26f9 + 155197d commit 8846031
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/ConstructiveSolidGeometry/PointsAndVectors/Vectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ struct CartesianVector{T} <: AbstractCoordinateVector{T, Cartesian}
z::T
end

#Type promotion happens here
function CartesianVector(x::TX, y::TY, z::TZ) where {TX<:Real,TY<:Real,TZ<:Real}
eltypes = _csg_get_promoted_eltype.((TX,TY,TZ))
T = float(promote_type(eltypes...))
CartesianVector{T}(T(x),T(y),T(z))
end

function CartesianVector(;
x = 0,
y = 0,
z = 0
)
CartesianVector(x,y,z)
end

function CartesianVector{T}(;
x = 0,
y = 0,
z = 0
) where {T}
CartesianVector{T}(T(x),T(y),T(z))
end

zero(VT::Type{<:AbstractCoordinateVector{T}}) where {T} = VT(zero(T),zero(T),zero(T))

# @inline rotate(pt::CartesianPoint{T}, r::RotMatrix{3,T,TT}) where {T, TT} = r.mat * pt
Expand Down Expand Up @@ -60,4 +83,25 @@ struct CylindricalVector{T} <: AbstractCoordinateVector{T, Cylindrical}
z::T
end

#Type promotion happens here
function CylindricalVector(r::TR, φ::TP, z::TZ) where {TR<:Real,TP<:Real,TZ<:Real}
eltypes = _csg_get_promoted_eltype.((TR,TP,TZ))
T = float(promote_type(eltypes...))
CylindricalVector{T}(T(r),T(φ),T(z))
end

function CylindricalVector(;
r = 0,
φ = 0,
z = 0
)
CylindricalVector(r,φ,z)
end

function CylindricalVector{T}(;
r = 0,
φ = 0,
z = 0
) where {T}
CylindricalVector{T}(T(r),T(φ),T(z))
end
8 changes: 8 additions & 0 deletions test/ConstructiveSolidGeometry/CSG_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ no_translations = (rotation = one(SMatrix{3, 3, T, 9}), translation = zero(Carte
ell2 = @inferred CSG.Ellipse(r = 1f0, φ=10f0)
@test ell1 == ell2
end
@testset "Vector" begin
cart = @inferred CSG.CartesianVector(x=2f0,z=1f0)
@inferred CSG.CartesianVector{Float32}(x=2)
cyl = @inferred CSG.CylindricalVector{Float32}(r=2.,z=1.)
cyl2 = @inferred CSG.CylindricalVector=3π)
@test CartesianVector(cyl) == cart
@test cart.x == Float32(2)
end
@testset "Point" begin
cart = @inferred CSG.CartesianPoint(x=2f0,z=1f0)
@inferred CSG.CartesianPoint{Float32}(x=2)
Expand Down

0 comments on commit 8846031

Please sign in to comment.