-
Notifications
You must be signed in to change notification settings - Fork 27
/
volume.jl
163 lines (153 loc) · 4.8 KB
/
volume.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Test
using Polyhedra
# simple algorithm for calculating area of polygons
# requires vertices to be sorted (counter)clockwise
function shoelace(verts::AbstractMatrix{<:Real})
@assert size(verts, 1) == 2 "shoelace only works for polygons"
xs = verts[begin,:]
ys = verts[end,:]
A = (ys[end]+ys[begin])*(xs[end]-xs[begin])
for i in axes(verts,2)[begin:end-1]
A += (ys[i]+ys[i+1])*(xs[i]-xs[i+1])
end
A = abs(A)
A isa AbstractFloat ? A/2 : A//2
end
isvertsapprox(verts, points) = all(any(isapprox(p, v) for v in verts) for p in points)
# issue 285: area of square [-0.5, -0.5] x [0.5, 0.5]
function check_vol_issue285(lib)
ineq = [
HalfSpace([-2.0, -2.0], 4.0),
HalfSpace([-2.0, -1.0], 2.5),
HalfSpace([-2.0, 0.0], 2.0),
HalfSpace([-2.0, 1.0], 2.5),
HalfSpace([-2.0, 2.0], 4.0),
HalfSpace([-1.0, -2.0], 2.5),
HalfSpace([-1.0, -1.0], 1.0),
HalfSpace([-1.0, 0.0], 0.5),
HalfSpace([-1.0, 1.0], 1.0),
HalfSpace([-1.0, 2.0], 2.5),
HalfSpace([0.0, -2.0], 2.0),
HalfSpace([0.0, -1.0], 0.5),
HalfSpace([0.0, 0.0], 0.0),
HalfSpace([0.0, 1.0], 0.5),
HalfSpace([0.0, 2.0], 2.0),
HalfSpace([1.0, -2.0], 2.5),
HalfSpace([1.0, -1.0], 1.0),
HalfSpace([1.0, 0.0], 0.5),
HalfSpace([1.0, 1.0], 1.0),
HalfSpace([1.0, 2.0], 2.5),
HalfSpace([2.0, -2.0], 4.0),
HalfSpace([2.0, -1.0], 2.5),
HalfSpace([2.0, 0.0], 2.0),
HalfSpace([2.0, 1.0], 2.5),
HalfSpace([2.0, 2.0], 4.0),
]
square = polyhedron(reduce(intersect, ineq), lib)
sqverts = [-1 1 1 -1; -1 -1 1 1]/2
@assert isvertsapprox(eachcol(sqverts), points(square))
return volume(square) ≈ shoelace(sqverts)
end
# issue 249
function check_vol_issue249_1(lib)
poly = polyhedron(HalfSpace([-1.0, 0.0], 0.0) ∩
HalfSpace([0.0, -1.0], 0.0) ∩
HalfSpace([1.0, 0.0], 1.0) ∩
HalfSpace([0.0, 1.0], 1.0) ∩
HalfSpace([-0.2, -0.8], -0.0) ∩
HalfSpace([0.6, 0.4], 0.6), lib)
verts = [1/3 1 0 0; 1 0 0 1]
@assert isvertsapprox(eachcol(verts), points(poly))
return volume(poly) ≈ shoelace(verts)
end
function check_vol_issue249_2(lib)
poly2 = polyhedron(# HalfSpace([-1.0, 0.0], 0.0) ∩
HalfSpace([0.0, -1.0], 0.0) ∩ # Comment here to get correct area
HalfSpace([1.0, 0.0], 1.0) ∩
HalfSpace([0.0, 1.0], 1.0) ∩
HalfSpace([-0.6, -0.4], -0.6) ∩
HalfSpace([0.2, 0.8], 0.95), lib)
verts2 = [1/3 3/4 1 1; 1 1 15/16 0]
@assert isvertsapprox(eachcol(verts2), points(poly2))
return volume(poly2) ≈ shoelace(verts2)
end
function check_vol_issue249_3(lib)
L3 = polyhedron(vrep([
0 0 0 0 0 0
0 0 1 0 0 0
0 1 0 0 0 0
0 1 1 0 0 1
1 0 0 0 0 0
1 0 1 0 1 0
1 1 0 1 0 0
1 1 1 1 1 1
]),lib)
# reportedly solution taken from https://doi.org/10.1016/j.dam.2018.10.038
return volume(L3) == 1//180
end
# examples similar to Cohen-Hickey paper, table 1
# https://doi.org/10.1145/322139.322141
function check_vol_simplex(n, lib)
s = polyhedron(vrep([i==j for i in 0:n, j in 1:n]), lib)
return volume(s) == 1//factorial(n)
end
function check_vol_isocahedron(lib)
ϕ = (1 + √5)/2
isoc = polyhedron(vrep([
0 1 ϕ
0 1 -ϕ
0 -1 ϕ
0 -1 -ϕ
1 ϕ 0
1 -ϕ 0
-1 ϕ 0
-1 -ϕ 0
ϕ 0 1
ϕ 0 -1
-ϕ 0 1
-ϕ 0 -1
]), lib)
# https://en.wikipedia.org/wiki/Regular_icosahedron
return volume(isoc) ≈ (5/12)*(3+√5)*2^3
end
function check_vol_dodecahedron(lib)
ϕ = (1 + √5)/2
ϕ² = ϕ^2
dodec = polyhedron(vrep([
ϕ ϕ ϕ
ϕ ϕ -ϕ
ϕ -ϕ ϕ
ϕ -ϕ -ϕ
-ϕ ϕ ϕ
-ϕ ϕ -ϕ
-ϕ -ϕ ϕ
-ϕ -ϕ -ϕ
0 1 ϕ²
0 1 -ϕ²
0 -1 ϕ²
0 -1 -ϕ²
1 ϕ² 0
1 -ϕ² 0
-1 ϕ² 0
-1 -ϕ² 0
ϕ² 0 1
ϕ² 0 -1
-ϕ² 0 1
-ϕ² 0 -1
]), lib)
# https://en.wikipedia.org/wiki/Regular_dodecahedron
return volume(dodec) ≈ (1/4)*(15+7*√5)*2^3
end
@testset "volumes" begin
lib_float = DefaultLibrary{Float64}()
lib_exact = DefaultLibrary{Int}()
@testset "simplex" for n in 1:5
@test check_vol_simplex(n, lib_exact)
end
@test check_vol_issue285(lib_float)
@test check_vol_issue249_1(lib_float)
@test check_vol_issue249_2(lib_float)
@test check_vol_issue249_3(lib_exact)
@test check_vol_isocahedron(lib_float)
@test check_vol_dodecahedron(lib_float)
end