Skip to content

Commit 25f5a33

Browse files
committed
Change AbstractCoordinatePoint to not be a FieldVector
1 parent e4daebe commit 25f5a33

4 files changed

Lines changed: 103 additions & 4 deletions

File tree

src/ConstructiveSolidGeometry/PointsAndVectors/Points.jl

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,29 @@ function CartesianPoint{T}(;
4747
CartesianPoint{T}(T(x),T(y),T(z))
4848
end
4949

50-
zero(PT::Type{<:AbstractCoordinatePoint{T}}) where {T} = PT(zero(T),zero(T),zero(T))
50+
function Base.convert(::Type{CartesianPoint{T}}, pt::CartesianPoint{U}) where {T,U}
51+
return CartesianPoint{T}(convert(T, pt.x), convert(T, pt.y), convert(T, pt.z))
52+
end
53+
54+
55+
Base.:(==)(a::CartesianPoint, b::CartesianPoint) = a.x == b.x && a.y == b.y && a.z == b.z
56+
57+
function Base.isapprox(a::CartesianPoint, b::CartesianPoint; kwargs...)
58+
return isapprox(a.x, b.x; kwargs...) && isapprox(a.y, b.y; kwargs...) && isapprox(a.z, b.z; kwargs...)
59+
end
60+
61+
62+
@inline Base.:(+)(pt::CartesianPoint, v::CartesianVector) = CartesianPoint(pt.x + v.x, pt.y + v.y, pt.z + v.z)
63+
@inline Base.:(-)(pt::CartesianPoint, v::CartesianVector) = CartesianPoint(pt.x - v.x, pt.y - v.y, pt.z - v.z)
64+
@inline Base.:(-)(a::CartesianPoint, b::CartesianPoint) = CartesianVector(a.x - b.x, a.y - b.y, a.z - b.z)
65+
66+
67+
Base.:(*)(A::StaticMatrix{3,3}, pt::CartesianPoint) = _ascartpoint(A * _asvector(pt))
68+
69+
@inline _asvector(pt::CartesianPoint{T}) where {T} = SVector(pt.x, pt.y, pt.z)
70+
@inline _ascartpoint(v::StaticVector{3}) = CartesianPoint(v[1], v[2], v[3])
71+
72+
5173

5274
"""
5375
struct CylindricalPoint{T} <: AbstractCoordinatePoint{T, Cylindrical}
@@ -109,6 +131,26 @@ end
109131
@inline _convert_point(pt::AbstractCoordinatePoint, ::Type{Cylindrical}) = CylindricalPoint(pt)
110132
@inline _convert_point(pt::AbstractCoordinatePoint, ::Type{Cartesian}) = CartesianPoint(pt)
111133

134+
135+
function Base.convert(::Type{CylindricalPoint{T}}, pt::CylindricalPoint{U}) where {T,U}
136+
return CylindricalPoint{T}(convert(T, pt.r), convert(T, pt.φ), convert(T, pt.z))
137+
end
138+
139+
140+
Base.:(==)(a::CylindricalPoint, b::CylindricalPoint) = a.r == b.r && a.φ == b.φ && a.z == b.z
141+
142+
function Base.isapprox(a::CylindricalPoint, b::CylindricalPoint; kwargs...)
143+
return isapprox(a.r, b.r; kwargs...) && isapprox(a.φ, b.φ; kwargs...) && isapprox(a.z, b.z; kwargs...)
144+
end
145+
146+
147+
@inline Base.:(+)(pt::CylindricalPoint, v::CylindricalVector) = CylindricalPoint(pt.r + v.r, pt.φ + v.φ, pt.z + v.z)
148+
149+
@inline Base.:(-)(pt::CylindricalPoint, v::CylindricalVector) = CylindricalPoint(pt.r - v.r, pt.φ - v.φ, pt.z - v.z)
150+
151+
@inline Base.:(-)(a::CylindricalPoint, b::CylindricalPoint) = CylindricalVector(a.r - b.r, a.φ - b.φ, a.z - b.z)
152+
153+
112154
# function _Δφ(φ1::T, φ2::T)::T where {T}
113155
# δφ = mod(φ2 - φ1, T(2π))
114156
# min(δφ, T(2π) - δφ)
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
abstract type AbstractCoordinatePoint{T, S} <: StaticArrays.FieldVector{3, T} end
1+
abstract type AbstractCoordinatePoint{T, S} end
2+
3+
zero(PT::Type{<:AbstractCoordinatePoint{T}}) where {T} = PT(zero(T),zero(T),zero(T))
4+
5+
@inline StaticArrays.Size(::PT) where {PT<:AbstractCoordinatePoint} = Size(PT)
6+
@inline StaticArrays.Size(::Type{PT}) where {PT<:AbstractCoordinatePoint} = Size{(fieldcount(PT),)}()
7+
@inline Base.length(pt::AbstractCoordinatePoint) = prod(Size(pt))::Int
8+
@inline Base.size(pt::AbstractCoordinatePoint) = Tuple(Size(pt))
9+
10+
Base.@propagate_inbounds Base.getindex(pt::AbstractCoordinatePoint, i::Int) = getfield(pt, i)
11+
12+
213
abstract type AbstractCoordinateVector{T, S} <: StaticArrays.FieldVector{3, T} end
314

4-
include("Points.jl")
515
include("Vectors.jl")
16+
include("Points.jl")
617

718
@inline distance_squared(v::CartesianVector) = v.x * v.x + v.y * v.y + v.z * v.z
819
@inline distance_squared(p1::CartesianPoint{T}, p2::CartesianPoint{T}) where {T <: Real} = distance_squared(CartesianVector(p1 .- p2))
9-
export distance_squared
20+
export distance_squared

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ using SolidStateDetectors
77

88
include("test_utils.jl")
99

10+
@timed_testset "Basic types" begin
11+
include("test_points_and_vectors.jl")
12+
end
13+
1014
@timed_testset "Comparison to analytic solutions" begin
1115
include("test_analytic_solutions.jl")
1216
end

test/test_points_and_vectors.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This file is a part of SolidStateDetectors.jl, licensed under the MIT License (MIT).
2+
3+
using Test
4+
5+
using SolidStateDetectors: CartesianPoint, CartesianVector, CylindricalPoint, CylindricalVector
6+
using StaticArrays: Size, SVector, SMatrix
7+
8+
@testset "points_and_vectors" begin
9+
a = CartesianPoint(1.0, 2.0, 3.0)
10+
b = CartesianPoint(3.0, 1.0, 2.0)
11+
v = CartesianVector(0.1, 0.2, 0.3)
12+
A = SMatrix{3,3}(1,2,3,4,5,6,7,8,9)
13+
14+
@test @inferred(a + v) == CartesianPoint(1.1, 2.2, 3.3)
15+
@test @inferred(a - v) == CartesianPoint(0.9, 1.8, 2.7)
16+
@test @inferred(a - v) == CartesianPoint(0.9, 1.8, 2.7)
17+
@test @inferred(a - b) == CartesianVector(-2.0, 1.0, 1.0)
18+
@test @inferred(A * a) == CartesianPoint(30.0, 36.0, 42.0)
19+
20+
@test @inferred(Size(a)) === Size(v)
21+
@test @inferred(size(a)) === size(v)
22+
@test @inferred(length(a)) === length(v)
23+
@test @inferred(CartesianPoint(a[1], a[2], a[3])) === a
24+
@test @inferred(CartesianPoint(a[1], a[2], a[3])) == a
25+
@test @inferred(CartesianPoint(a[1], a[2], a[3])) a
26+
27+
28+
a = CylindricalPoint(1.0, 2.0, 3.0)
29+
b = CylindricalPoint(3.0, 1.0, 2.0)
30+
31+
v = CylindricalVector(0.1, 0.2, 0.3)
32+
33+
@test @inferred(a + v) == CylindricalPoint(1.1, 2.2, 3.3)
34+
@test @inferred(a - v) == CylindricalPoint(0.9, 1.8, 2.7)
35+
@test @inferred(a - b) == CylindricalVector(-2.0, 1.0, 1.0)
36+
@test @inferred(Size(a)) === Size(v)
37+
@test @inferred(size(a)) === size(v)
38+
@test @inferred(length(a)) === length(v)
39+
@test @inferred(CylindricalPoint(a[1], a[2], a[3])) === a
40+
@test @inferred(CylindricalPoint(a[1], a[2], a[3])) == a
41+
@test @inferred(CylindricalPoint(a[1], a[2], a[3])) a
42+
end

0 commit comments

Comments
 (0)