Skip to content

Commit 1110a7d

Browse files
authored
bring back integer rounding (#242)
* bring back integer rounding * add round() method and fix ambiguity * skip round() for now
1 parent c0da282 commit 1110a7d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/primitives/rectangles.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ Formally it is the Cartesian product of intervals, which is represented by the
99
struct HyperRectangle{N,T} <: GeometryPrimitive{N,T}
1010
origin::Vec{N,T}
1111
widths::Vec{N,T}
12+
13+
function HyperRectangle{N, T}(o::VecTypes, w::VecTypes) where {N, T}
14+
return new{N, T}(convert(Vec{N, T}, o), convert(Vec{N, T}, w))
15+
end
16+
function HyperRectangle{N, T}(o::VecTypes, w::VecTypes) where {N, T <: Integer}
17+
return new{N, T}(convert(Vec{N, T}, round.(T, o)), convert(Vec{N, T}, round.(T, w)))
18+
end
1219
end
1320

1421
##
@@ -154,6 +161,12 @@ height(prim::Rect) = prim.widths[2]
154161
volume(prim::HyperRectangle) = prod(prim.widths)
155162
area(prim::Rect2) = volume(prim)
156163

164+
# function Base.round(::Type{Rect{N, T}}, x::Rect{N}, mode::RoundingMode=RoundNearest) where {N, T}
165+
# mini = round.(T, minimum(x))
166+
# maxi = round.(T, maximum(x))
167+
# return Rect{N, T}(mini, maxi .- mini)
168+
# end
169+
157170
"""
158171
split(rectangle, axis, value)
159172

test/geometrytypes.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ end
182182
end
183183
end
184184

185+
# TODO: consider deprecating this
186+
@testset "Integer rounding" begin
187+
@test Rect2i(0.3, 0.4, 0.6, 0.5) == Rect2i(0,0,1,0)
188+
@test Rect2{UInt32}(0.8, 0.1, 1.3, 1.9) == Rect2i(1,0,1,2)
189+
@test Rect3i(0.3, 0.6, 0.9, 1.2, 1.5, 1.8) == Rect3i(0,1,1,1,2,2)
190+
end
191+
185192
# TODO: These don't really make sense...
186193
r = Rect2f()
187194
@test origin(r) == Vec(Inf, Inf)

0 commit comments

Comments
 (0)