/
decompose.jl
145 lines (138 loc) · 6.63 KB
/
decompose.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
import GeometryTypes
struct Face
points::Vector{Vector{Float64}}
end
Face(args...) = Face([args...])
function Base.isapprox(a::Face, b::Face; atol=1e-8, rtol=1e-8)
for i in 1:3
ok = false
for j in 1:3
if !isapprox(a.points[j], b.points[((j+i-1)%3)+1]; atol=atol, rtol=rtol)
ok = true
end
end
ok && return true
end
return false
end
nfaces(d::Dict{<:Any, Face}) = length(d)
nfaces(d::Dict{<:Any, <:Vector}) = sum(map(length, values(d)))
function test_decompose(p::Polyhedra.Mesh, d::Dict)
@test GeometryTypes.isdecomposable(GeometryTypes.Point{3, Float32}, typeof(p))
points = GeometryTypes.decompose(GeometryTypes.Point{3, Float32}, p)
@test GeometryTypes.isdecomposable(GeometryTypes.Face{3, Int}, typeof(p))
faces = GeometryTypes.decompose(GeometryTypes.Face{3, Int}, p)
@test GeometryTypes.isdecomposable(GeometryTypes.Normal{3,Float64}, typeof(p))
normals = GeometryTypes.decompose(GeometryTypes.Normal{3,Float64}, p)
nf = nfaces(d)
@test length(points) == 3nf
@test length(faces) == nf
@test length(normals) == 3nf
for i in eachindex(faces)
a, b, c = faces[i]
@test normals[3i] == normals[3i-1] == normals[3i-2]
normal = map(x -> x === -0.0 ? 0.0 : x, round.(normals[3i], digits=6))
face = Face(points[3i-2],
points[3i-1],
points[3i])
@test normal in keys(d)
f = d[normal]
if f isa Face
@test f ≈ face
delete!(d, normal)
else
j = findfirst(fa -> fa ≈ face, f)
@test j isa Int && j > 0
deleteat!(f, j)
if isempty(f)
delete!(d, normal)
end
end
end
@test isempty(d)
end
test_decompose(p::Polyhedron, d::Dict) = test_decompose(Polyhedra.Mesh(p), d)
function orthantdecomposetest(lib::Polyhedra.Library)
v = vrep([Ray([1., 0, 0]),
Ray([0, 1., 0]),
Ray([0, 0, 1.])])
p = polyhedron(v, lib)
d = Dict([-1.0, 0.0, 0.0] => Face([0.0, 0.0, 0.0],
[0.0, 2.0, 0.0],
[0.0, 0.0, 2.0]),
[ 0.0, -1.0, 0.0] => Face([0.0, 0.0, 0.0],
[0.0, 0.0, 2.0],
[2.0, 0.0, 0.0]),
[ 0.0, 0.0, -1.0] => Face([0.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[0.0, 2.0, 0.0]))
test_decompose(p, d)
end
function cubedecomposetest(lib::Polyhedra.Library)
p = polyhedron(convexhull([ 1., 1, 1],
[ 1., -1, 1],
[ 1., 1, -1],
[ 1., -1, -1],
[-1., 1, 1],
[-1., -1, 1],
[-1., 1, -1],
[-1., -1, -1]), lib)
# FIXME failing
#p = polyhedron(convexhull(SymPoint([1., 1, 1]),
# SymPoint([1., -1, 1]),
# SymPoint([1., 1, -1]),
# SymPoint([1., -1, -1])))
d = Dict([ 0.0, 0.0, -1.0] => [Face([-1.0, -1.0, -1.0], [ 1.0, -1.0, -1.0], [ 1.0, 1.0, -1.0]),
Face([ 1.0, 1.0, -1.0], [-1.0, 1.0, -1.0], [-1.0, -1.0, -1.0])],
[-1.0, 0.0, 0.0] => [Face([-1.0, -1.0, -1.0], [-1.0, 1.0, -1.0], [-1.0, 1.0, 1.0]),
Face([-1.0, 1.0, 1.0], [-1.0, -1.0, 1.0], [-1.0, -1.0, -1.0])],
[ 0.0, -1.0, 0.0] => [Face([-1.0, -1.0, 1.0], [ 1.0, -1.0, 1.0], [ 1.0, -1.0, -1.0]),
Face([ 1.0, -1.0, -1.0], [-1.0, -1.0, -1.0], [-1.0, -1.0, 1.0])],
[ 0.0, 1.0, 0.0] => [Face([-1.0, 1.0, -1.0], [ 1.0, 1.0, -1.0], [ 1.0, 1.0, 1.0]),
Face([ 1.0, 1.0, 1.0], [-1.0, 1.0, 1.0], [-1.0, 1.0, -1.0])],
[ 0.0, 0.0, 1.0] => [Face([-1.0, 1.0, 1.0], [ 1.0, 1.0, 1.0], [ 1.0, -1.0, 1.0]),
Face([ 1.0, -1.0, 1.0], [-1.0, -1.0, 1.0], [-1.0, 1.0, 1.0])],
[ 1.0, 0.0, 0.0] => [Face([ 1.0, -1.0, 1.0], [ 1.0, 1.0, 1.0], [ 1.0, 1.0, -1.0]),
Face([ 1.0, 1.0, -1.0], [ 1.0, -1.0, -1.0], [ 1.0, -1.0, 1.0])])
test_decompose(p, d)
end
function largedecomposetest(lib::Polyhedra.Library)
V = [-1 -1 1;
-1 1 1;
1 -1 1;
1 1 1;
0 1 -1;
1 0 -1;
-1 0 -1;
0 -1 -1]
R = [0 0 1]
v = vrep(V, R)
p = polyhedron(v, lib)
d13 = round(1/3, digits=6)
d23 = round(2/3, digits=6)
md13 = round(-1/3, digits=6)
md23 = round(-2/3, digits=6)
d = Dict([ 1.0, 0.0, 0.0] => [Face([1.0,1.0,1.0], [1.0,-1.0,1.0], [1.0,1.0,2.0]),
Face([1.0,-1.0,1.0], [1.0,-1.0,2.0], [1.0,1.0,2.0]),
Face([1.0,1.0,1.0], [1.0,0.0,-1.0], [1.0,-1.0,1.0])],
[ 0.0, 1.0, 0.0] => [Face([-1.0,1.0,1.0], [1.0,1.0,1.0], [-1.0,1.0,2.0]),
Face([1.0,1.0,1.0], [1.0,1.0,2.0], [-1.0,1.0,2.0]),
Face([-1.0,1.0,1.0], [0.0,1.0,-1.0], [1.0,1.0,1.0])],
[ d23, d23,md13] => [Face([1.0,1.0,1.0], [0.0,1.0,-1.0], [1.0,0.0,-1.0])],
[ 0.0,-1.0, 0.0] => [Face([1.0,-1.0,1.0], [-1.0,-1.0,1.0], [1.0,-1.0,2.0]),
Face([-1.0,-1.0,1.0], [-1.0,-1.0,2.0], [1.0,-1.0,2.0]),
Face([1.0,-1.0,1.0], [0.0,-1.0,-1.0], [-1.0,-1.0,1.0])],
[-1.0, 0.0, 0.0] => [Face([-1.0,-1.0,1.0], [-1.0,1.0,1.0], [-1.0,-1.0,2.0]),
Face([-1.0,1.0,1.0], [-1.0,1.0,2.0], [-1.0,-1.0,2.0]),
Face([-1.0,-1.0,1.0], [-1.0,0.0,-1.0], [-1.0,1.0,1.0])],
[md23,md23,md13] => [Face([-1.0,-1.0,1.0], [0.0,-1.0,-1.0], [-1.0,0.0,-1.0])],
[md23, d23,md13] => [Face([-1.0,0.0,-1.0], [0.0,1.0,-1.0], [-1.0,1.0,1.0])],
[ d23,md23,md13] => [Face([1.0,0.0,-1.0], [0.0,-1.0,-1.0], [1.0,-1.0,1.0])],
[ 0.0, 0.0,-1.0] => [Face([0.0,-1.0,-1.0], [1.0,0.0,-1.0], [0.0,1.0,-1.0]),
Face([0.0,1.0,-1.0], [-1.0,0.0,-1.0], [0.0,-1.0,-1.0])])
test_decompose(p, d)
end
decomposetests = Dict("orthantdecompose" => orthantdecomposetest,
"cubedecompose" => cubedecomposetest,
"largedecompose" => largedecomposetest)
@polytestset decompose