Skip to content

Commit 173abb6

Browse files
mforetsMarcelo Foretsschillic
authored
Generalize check if point is in hyperplane (#3304)
* generalize check if point is in hyperplane * Update test/Sets/Tetrahedron.jl Co-authored-by: Christian Schilling <git@christianschilling.net> --------- Co-authored-by: Marcelo Forets <marcelo@planting.space> Co-authored-by: Christian Schilling <git@christianschilling.net>
1 parent 33cc2d2 commit 173abb6

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/Sets/Tetrahedron.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Type that represents a tetrahedron in vertex representation.
1212
### Examples
1313
1414
A tetrahedron can be constructed by passing the list of vertices.
15-
The following builds the tetrahedron with edge length 2 from the [wikipedia page Tetrahedron](https://en.wikipedia.org/wiki/Tetrahedron):
15+
The following builds the tetrahedron with edge length 2 from the [wikipedia page Tetrahedron](https://en.wikipedia.org/wiki/Tetrahedron):
1616
1717
```jldoctest
1818
julia> vertices = [[1, 0, -1/sqrt(2)], [-1, 0, -1/sqrt(2)], [0, 1, 1/sqrt(2)], [0, -1, 1/sqrt(2)]];
@@ -110,20 +110,22 @@ function ∈(x::AbstractVector, T::Tetrahedron)
110110
same_side(v[2], v[3], v[4], v[1], x)
111111
end
112112

113-
# Return `true` iff point `x` lies in the same half-space as `v4` with respect to the hyperplane determined by `v1`, `v2` and `v3`.
113+
# Return `true` iff point `x` lies in the same half-space as `v4` with respect to the hyperplane `H` determined by `v1`, `v2` and `v3`.
114114
function same_side(v1, v2, v3, v4, x)
115115
normal = cross(v2 - v1, v3 - v1)
116116
dotv4 = dot(normal, v4 - v1)
117117
dotx = dot(normal, x - v1)
118-
return signbit(dotv4) == signbit(dotx)
118+
# If the point `x` lies in `H` then `dotx` is zero.
119+
return signbit(dotv4) == signbit(dotx) || isapproxzero(dotx)
119120
end
120121

121-
function rand(::Type{Tetrahedron}; N::Type{<:Real}=Float64, rng::AbstractRNG=GLOBAL_RNG, seed::Union{Int,Nothing}=nothing)
122+
function rand(::Type{Tetrahedron}; N::Type{<:Real}=Float64, rng::AbstractRNG=GLOBAL_RNG,
123+
seed::Union{Int,Nothing}=nothing)
122124
P = rand(VPolytope; N=N, dim=3, rng=rng, seed=seed, num_vertices=4)
123125
vertices = P.vertices
124126
return Tetrahedron(vertices)
125127
end
126128

127129
function constraints_list(T::Tetrahedron)
128-
constraints_list(convert(VPolytope, T))
130+
return constraints_list(convert(VPolytope, T))
129131
end

test/Sets/Tetrahedron.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
for N in [Float64, Float32, Rational{Int}]
22
# constructor
3-
vertices = [N[1, 0, -1/sqrt(2)], N[-1, 0, -1/sqrt(2)], N[0, 1, 1/sqrt(2)], N[0, -1, 1/sqrt(2)]]
3+
vertices = [N[1, 0, -1 / sqrt(2)], N[-1, 0, -1 / sqrt(2)], N[0, 1, 1 / sqrt(2)],
4+
N[0, -1, 1 / sqrt(2)]]
45
T = Tetrahedron(vertices)
56
@test eltype(T) == N
67
@test dim(T) == 3
@@ -10,11 +11,15 @@ for N in [Float64, Float32, Rational{Int}]
1011
@test Tmat == T
1112

1213
# suport vector
13-
@test σ(ones(3), T) == N[0, 1, 1/sqrt(2)]
14+
@test σ(ones(3), T) == N[0, 1, 1 / sqrt(2)]
1415

1516
# membership
1617
@test zeros(N, 3) T
1718

1819
# is_polyhedral
1920
@test is_polyhedral(T)
21+
22+
# LazySets#3303
23+
T = Tetrahedron([N[0, 0, 0], N[0, 1, 0], N[1, 0, 0], N[1, 0, 1]])
24+
@test zeros(N, 3) T
2025
end

0 commit comments

Comments
 (0)