Skip to content

Commit 9b518a6

Browse files
committed
change Rect() to return Rect(NaN..., 0...)
1 parent 4ce0b2b commit 9b518a6

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/primitives/rectangles.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ Rect() = Rect{2,Float32}()
5656
RectT{T}() where {T} = Rect{2,T}()
5757
Rect{N}() where {N} = Rect{N,Float32}()
5858

59-
function Rect{N,T}() where {T,N}
60-
# empty constructor such that update will always include the first point
61-
return Rect{N,T}(Vec{N,T}(typemax(T)), Vec{N,T}(typemin(T)))
62-
end
59+
Rect{N,T}() where {T <: AbstractFloat,N} = Rect{N,T}(Vec{N,T}(NaN), Vec{N,T}(0))
60+
Rect{N,T}() where {T, N} = throw(MethodError(Rect{N,T}, tuple()))
61+
# TODO: what about integers and other types? No reasonable default?
6362

6463
# Rect(numbers...)
6564
Rect(args::Vararg{Number, N}) where {N} = Rect{div(N, 2), promote_type(typeof.(args)...)}(args...)
@@ -302,9 +301,9 @@ end
302301
"""
303302
isempty(h::Rect)
304303
305-
Return `true` if any of the widths of `h` are negative.
304+
Return `true` if any of the widths of `h` are zero or negative.
306305
"""
307-
Base.isempty(h::Rect{N,T}) where {N,T} = any(<(zero(T)), h.widths)
306+
Base.isempty(h::Rect{N,T}) where {N,T} = any(<=(zero(T)), h.widths)
308307

309308
"""
310309
union(r1::Rect{N}, r2::Rect{N})

test/geometrytypes.jl

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,35 @@ end
8181
# TODO: Do these actually make sense?
8282
# Should they not be Rect(NaN..., 0...)?
8383
@testset "Empty Constructors" begin
84+
function nan_equal(r1::Rect, r2::Rect)
85+
o1 = origin(r1); o2 = origin(r2)
86+
return ((isnan(o1) && isnan(o2)) || (o1 == o2)) && (widths(r1) == widths(r2))
87+
end
88+
8489
for constructor in [Rect, Rect{2}, Rect2, RectT, Rect2f]
85-
@test constructor() == Rect{2, Float32}(Inf, Inf, -Inf, -Inf)
90+
@test nan_equal(constructor(), Rect{2, Float32}(NaN, NaN, 0, 0))
8691
end
8792
for constructor in [Rect{3}, Rect3, Rect3f]
88-
@test constructor() == Rect{3, Float32}((Inf, Inf, Inf), (-Inf, -Inf, -Inf))
93+
@test nan_equal(constructor(), Rect{3, Float32}((NaN, NaN, NaN), (0, 0, 0)))
8994
end
9095

91-
for T in [UInt32, Int16, Float64]
96+
for T in [UInt32, Int16]
9297
a = typemax(T)
9398
b = typemin(T)
9499
for constructor in [Rect{2, T}, Rect2{T}, RectT{T, 2}]
95-
@test constructor() == Rect{2, T}(a, a, b, b)
100+
@test_throws MethodError constructor()
96101
end
97102
for constructor in [Rect{3, T}, Rect3{T}, RectT{T, 3}]
98-
@test constructor() == Rect{3, T}(Point(a, a, a), Vec(b, b, b))
103+
@test_throws MethodError constructor()
99104
end
100105
end
106+
107+
for constructor in [Rect{2, Float64}, Rect2{Float64}, RectT{Float64, 2}]
108+
@test nan_equal(constructor(), Rect{2, Float64}(NaN, NaN, 0, 0))
109+
end
110+
for constructor in [Rect{3, Float64}, Rect3{Float64}, RectT{Float64, 3}]
111+
@test nan_equal(constructor(), Rect{3, Float64}(Point3(NaN), Vec3(0)))
112+
end
101113
end
102114

103115
@testset "Constructor arg conversions" begin
@@ -182,16 +194,15 @@ end
182194
end
183195
end
184196

185-
# TODO: These don't really make sense...
186197
r = Rect2f()
187-
@test origin(r) == Vec(Inf, Inf)
188-
@test minimum(r) == Vec(Inf, Inf)
198+
@test isnan(origin(r))
199+
@test isnan(minimum(r))
189200
@test isnan(maximum(r))
190-
@test width(r) == -Inf
191-
@test height(r) == -Inf
192-
@test widths(r) == Vec(-Inf, -Inf)
193-
@test area(r) == Inf
194-
@test volume(r) == Inf
201+
@test width(r) == 0
202+
@test height(r) == 0
203+
@test widths(r) == Vec2(0)
204+
@test area(r) == 0
205+
@test volume(r) == 0
195206

196207
a = Rect(Vec(0, 1), Vec(2, 3))
197208
pt_expa = Point{2,Int}[(0, 1), (2, 1), (2, 4), (0, 4)]

0 commit comments

Comments
 (0)