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

Generalize check if point is in hyperplane #3304

Merged
merged 2 commits into from
Apr 17, 2023
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
12 changes: 7 additions & 5 deletions src/Sets/Tetrahedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Type that represents a tetrahedron in vertex representation.
### Examples

A tetrahedron can be constructed by passing the list of vertices.
The following builds the tetrahedron with edge length 2 from the [wikipedia page Tetrahedron](https://en.wikipedia.org/wiki/Tetrahedron):
The following builds the tetrahedron with edge length 2 from the [wikipedia page Tetrahedron](https://en.wikipedia.org/wiki/Tetrahedron):

```jldoctest
julia> vertices = [[1, 0, -1/sqrt(2)], [-1, 0, -1/sqrt(2)], [0, 1, 1/sqrt(2)], [0, -1, 1/sqrt(2)]];
Expand Down Expand Up @@ -110,20 +110,22 @@ function ∈(x::AbstractVector, T::Tetrahedron)
same_side(v[2], v[3], v[4], v[1], x)
end

# Return `true` iff point `x` lies in the same half-space as `v4` with respect to the hyperplane determined by `v1`, `v2` and `v3`.
# 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`.
function same_side(v1, v2, v3, v4, x)
normal = cross(v2 - v1, v3 - v1)
dotv4 = dot(normal, v4 - v1)
dotx = dot(normal, x - v1)
return signbit(dotv4) == signbit(dotx)
# If the point `x` lies in `H` then `dotx` is zero.
return signbit(dotv4) == signbit(dotx) || isapproxzero(dotx)
end

function rand(::Type{Tetrahedron}; N::Type{<:Real}=Float64, rng::AbstractRNG=GLOBAL_RNG, seed::Union{Int,Nothing}=nothing)
function rand(::Type{Tetrahedron}; N::Type{<:Real}=Float64, rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int,Nothing}=nothing)
P = rand(VPolytope; N=N, dim=3, rng=rng, seed=seed, num_vertices=4)
vertices = P.vertices
return Tetrahedron(vertices)
end

function constraints_list(T::Tetrahedron)
constraints_list(convert(VPolytope, T))
return constraints_list(convert(VPolytope, T))
end
9 changes: 7 additions & 2 deletions test/Sets/Tetrahedron.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
for N in [Float64, Float32, Rational{Int}]
# constructor
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)]]
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)]]
T = Tetrahedron(vertices)
@test eltype(T) == N
@test dim(T) == 3
Expand All @@ -10,11 +11,15 @@ for N in [Float64, Float32, Rational{Int}]
@test Tmat == T

# suport vector
@test σ(ones(3), T) == N[0, 1, 1/sqrt(2)]
@test σ(ones(3), T) == N[0, 1, 1 / sqrt(2)]

# membership
@test zeros(N, 3) ∈ T

# is_polyhedral
@test is_polyhedral(T)

# LazySets#3303
T = Tetrahedron([N[0, 0, 0], N[0, 1, 0], N[1, 0, 0], N[1, 0, 1]])
@test zeros(N, 3) ∈ T
end