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

Improve StaticArrays usage #27

Merged
merged 2 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
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
21 changes: 6 additions & 15 deletions src/box.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ end
Box(c::SVector{N}, d::SVector{N},
axes::SMatrix{N,N,<:Real,L}=@SMatrix(eye(N)), # columns are axes unit vectors
data::D=nothing) where {N,L,D} =
Box{N,L,D}(c, 0.5d, inv((axes' ./ sqrt.(sum(abs2,axes,Val{1}))[1,:])'), data)
# Use this after StaticArrays issue 242 is fixed:
# Box{N,L,D}(c, 0.5d, inv(axes ./ sqrt.(sum(abs2,axes,Val{1}))), data)
Box{N,L,D}(c, 0.5d, inv(axes ./ sqrt.(sum(abs2,axes,Val{1}))), data)

Box(c::AbstractVector, d::AbstractVector, axes=eye(length(c)), data=nothing) =
(N = length(c); Box(SVector{N}(c), SVector{N}(d), SMatrix{N,N}(axes), data))
Expand Down Expand Up @@ -58,19 +56,12 @@ function surfpt_nearby(x::SVector{N}, b::Box{N}) where {N}
return x+∆x, nout
end

signmatrix(b::Shape{1}) = SMatrix{1,2}(1,-1)
signmatrix(b::Shape{2}) = SMatrix{2,4}(1,1, -1,1, 1,-1, -1,-1)
signmatrix(b::Shape{3}) = SMatrix{3,8}(1,1,1, -1,1,1, 1,-1,1, 1,1,-1, -1,-1,1, -1,1,-1, 1,-1,-1, -1,-1,-1)
signmatrix(b::Box{1}) = SMatrix{1,1}(1)
signmatrix(b::Box{2}) = SMatrix{2,2}(1,1, -1,1)
signmatrix(b::Box{3}) = SMatrix{3,4}(1,1,1, -1,1,1, 1,-1,1, 1,1,-1)

function bounds(b::Box)
# Below, inv(b.p) .* b.r' would have been conceptually better because its columns
# are scaled axes vectors. However, then the result is not SMatrix because b.r'
# is not SVector. Then, we cannot use maximum(..., Val{2}), which is type-stable
# for SMatrix. A workaround is to calculate inv(b.p') .* b.r and take transpose.
A = (inv(b.p') .* b.r)' # SMatrix
# Use this after StaticArrays issue 242 is fixed:
# A = inv(b.p) .* b.r'

m = maximum(A * signmatrix(b), Val{2})[:,1] # extrema of all 2^N corners of the box
A = inv(b.p) .* b.r'
m = maximum(abs.(A * signmatrix(b)), Val{2})[:,1] # extrema of all 2^N corners of the box
return (b.c-m,b.c+m)
end
4 changes: 1 addition & 3 deletions src/ellipsoid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ end
Ellipsoid(c::SVector{N}, r::SVector{N},
axes::SMatrix{N,N,<:Real,L}=@SMatrix(eye(N)), # columns are axes unit vectors
data::D=nothing) where {N,L,D} =
Ellipsoid{N,L,D}(c, float.(r).^-2, inv((axes' ./ sqrt.(sum(abs2,axes,Val{1}))[1,:])'), data)
# Use this after StaticArrays issue 242 is fixed:
# Ellipsoid{N,L,D}(c, float.(r).^-2, inv(axes ./ sqrt.(sum(abs2,axes,Val{1}))), data)
Ellipsoid{N,L,D}(c, float.(r).^-2, inv(axes ./ sqrt.(sum(abs2,axes,Val{1}))), data)

Ellipsoid(c::AbstractVector, r::AbstractVector, axes::AbstractMatrix=eye(length(c)), data=nothing) =
(N = length(c); Ellipsoid(SVector{N}(c), SVector{N}(r), SMatrix{N,N}(axes), data))
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ end

Cin = R * (GeometryPrimitives.signmatrix(br) .* (one⁻ .* [r1,r2])) # around corners, inside
Cout = R * (GeometryPrimitives.signmatrix(br) .* (one⁺ .* [r1,r2])) # around corners, outside
for j = 1:4; @test Cin[:,j] ∈ br; end
for j = 1:4; @test Cout[:,j] ∉ br; end
for j = 1:2; @test Cin[:,j] ∈ br; end
for j = 1:2; @test Cout[:,j] ∉ br; end

@test br == deepcopy(br)
@test hash(br) == hash(deepcopy(br))
Expand Down