Skip to content
Prev Previous commit
Next Next commit
retain original performance
  • Loading branch information
Moelf committed Sep 22, 2022
commit 2f2a458d8e3cb7c409f917e43a8c38319d246eee
93 changes: 0 additions & 93 deletions src/Boolean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,96 +162,3 @@ end
function safetyToIn(shape::BooleanIntersection{T, SL, SR}, point::Point3{T})::T where {T,SL,SR}
return 0
end

function distanceToIn(shape::BooleanIntersection{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape

positionA = inside(left, point)
lpoint = transformation * point
positionB = inside(right, lpoint)

inLeft = positionA == kInside
inRight = positionB == kInside

inLeft && inRight && return T(-1)

dist = T(0)
npoint = point
ldir = transformation * dir
# main loop
while true
d1 = d2 = 0
if !inLeft
d1 = distanceToIn(left, npoint, dir)
d1 = max(d1, kTolerance(T))
d1 == T(Inf) && return T(Inf)
end
if !inRight
d2 = distanceToIn(right, lpoint, ldir)
d2 = max(d2, kTolerance(T))
d2 == T(Inf) && return T(Inf)
end
if d1 > d2
# propagate to left shape
dist += d1
inleft = true
npoint += d1 * dir
lpoint = transformation * npoint
# check if propagated point is inside right shape, check is done with a little push
inRight = inside(right, lpoint + kTolerance(T) * ldir) == kInside
inRight && return dist
# here inleft=true, inright=false
else
# propagate to right shape
dist += d2
inright = true
# check if propagated point is inside left shape, check is done with a little push
npoint += d2 * dir
lpoint = transformation * npoint
inLeft = inside(left, npoint + kTolerance(T) * dir) == kInside
inLeft && return dist
end
end
return dist
end

function distanceToIn(shape::BooleanSubtraction{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape

lpoint = transformation * point
ldir = transformation * dir
positionB = inside(left, lpoint)
inRight = positionB == kInside

npoint = point
dist = T(0)

while true
if inRight
# propagate to outside of '- / RightShape'
d1 = distanceToOut(right, lpoint, ldir)
dist += (d1 >= 0 && d1 < Inf) ? d1 + kPushTolerance(T) : 0
npoint = point + (dist + kPushTolerance(T)) * dir
lpoint = transformation * npoint
# now master outside 'B'; check if inside 'A'
inside(left, npoint) == kInside && distanceToOut(left, npoint, dir) > kPushTolerance(T) && return dist
end

# if outside of both we do a max operation master outside '-' and outside '+' ; find distances to both
d2 = distanceToIn(left, npoint, dir)
d2 = max(d2, 0)
d2 == T(Inf) && return T(Inf)

d1 = distanceToIn(right, lpoint, ldir)
if d2 < d1 - kTolerance(T)
dist += d2 + kPushTolerance(T)
return dist
end

# propagate to '-'
dist += (d1 >= 0 && d1 < Inf) ? d1 : 0
npoint = point + (dist + kPushTolerance(T)) * dir
lpoint = transformation * npoint
inRight = true
end
end
104 changes: 52 additions & 52 deletions src/DistanceIn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,58 @@ function distanceToIn_booleanunion(shape::BooleanUnion{T, SL, SR}, point::Point3
distB = distanceToIn(right, transformation * point, transformation * dir)
return min(distA, distB)
end
# function distanceToIn_booleanintersection(shape::BooleanIntersection{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
# (; left, right, transformation) = shape

# positionA = inside(left, point)
# lpoint = transformation * point
# positionB = inside(right, lpoint)

# inLeft = positionA == kInside
# inRight = positionB == kInside

# inLeft && inRight && return T(-1)

# dist = T(0)
# npoint = point
# ldir = transformation * dir
# # main loop
# while true
# d1 = d2 = 0
# if !inLeft
# d1 = distanceToIn(left, npoint, dir)
# d1 = max(d1, kTolerance(T))
# d1 == T(Inf) && return T(Inf)
# end
# if !inRight
# d2 = distanceToIn(right, lpoint, ldir)
# d2 = max(d2, kTolerance(T))
# d2 == T(Inf) && return T(Inf)
# end
# if d1 > d2
# # propagate to left shape
# dist += d1
# inleft = true
# npoint += d1 * dir
# lpoint = transformation * npoint
# # check if propagated point is inside right shape, check is done with a little push
# inRight = inside(right, lpoint + kTolerance(T) * ldir) == kInside
# inRight && return dist
# # here inleft=true, inright=false
# else
# # propagate to right shape
# dist += d2
# inright = true
# # check if propagated point is inside left shape, check is done with a little push
# npoint += d2 * dir
# lpoint = transformation * npoint
# inLeft = inside(left, npoint + kTolerance(T) * dir) == kInside
# inLeft && return dist
# end
# end
# return dist
# end
function distanceToIn_booleansubtraction(shape, point::Point3{T}, dir) where T
function distanceToIn_booleanintersection(shape::BooleanIntersection{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape

positionA = inside(left, point)
lpoint = transformation * point
positionB = inside(right, lpoint)

inLeft = positionA == kInside
inRight = positionB == kInside

inLeft && inRight && return T(-1)

dist = T(0)
npoint = point
ldir = transformation * dir
# main loop
while true
d1 = d2 = 0
if !inLeft
d1 = distanceToIn(left, npoint, dir)
d1 = max(d1, kTolerance(T))
d1 == T(Inf) && return T(Inf)
end
if !inRight
d2 = distanceToIn(right, lpoint, ldir)
d2 = max(d2, kTolerance(T))
d2 == T(Inf) && return T(Inf)
end
if d1 > d2
# propagate to left shape
dist += d1
inleft = true
npoint += d1 * dir
lpoint = transformation * npoint
# check if propagated point is inside right shape, check is done with a little push
inRight = inside(right, lpoint + kTolerance(T) * ldir) == kInside
inRight && return dist
# here inleft=true, inright=false
else
# propagate to right shape
dist += d2
inright = true
# check if propagated point is inside left shape, check is done with a little push
npoint += d2 * dir
lpoint = transformation * npoint
inLeft = inside(left, npoint + kTolerance(T) * dir) == kInside
inLeft && return dist
end
end
return dist
end
function distanceToIn_booleansubtraction(shape::BooleanSubtraction{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape

lpoint = transformation * point
Expand Down
6 changes: 3 additions & 3 deletions src/DistanceOut.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Boolean
function distanceToOut_booleanunion(shape, point, dir::Vector3{T})::T where T
function distanceToOut_booleanunion(shape::BooleanUnion{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape
dist = T(0)
positionA = inside(left, point)
Expand Down Expand Up @@ -54,13 +54,13 @@ function distanceToOut_booleanunion(shape, point, dir::Vector3{T})::T where T
end
end
end
function distanceToOut_booleanintersection(shape, point, dir::Vector3{T})::T where T
function distanceToOut_booleanintersection(shape::BooleanIntersection{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape
distA = distanceToOut(left, point, dir)
distB = distanceToOut(right, transformation * point, transformation * dir)
return min(distA, distB)
end
function distanceToOut_booleansubtraction(shape, point, dir::Vector3{T})::T where T
function distanceToOut_booleansubtraction(shape::BooleanSubtraction{T, SL, SR}, point::Point3{T}, dir::Vector3{T})::T where {T,SL,SR}
(; left, right, transformation) = shape
distA = distanceToOut(left, point, dir)
distB = distanceToIn(right, transformation * point, transformation * dir)
Expand Down