-
Notifications
You must be signed in to change notification settings - Fork 706
/
csg.py
229 lines (200 loc) · 7.31 KB
/
csg.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from . import geometry
class CSGUnion(geometry.Geometry):
"""Construct an object by CSG Union."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} | {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGUnion, self).__init__(
geom1.dim,
(
np.minimum(geom1.bbox[0], geom2.bbox[0]),
np.maximum(geom1.bbox[1], geom2.bbox[1]),
),
geom1.diam + geom2.diam,
)
self.geom1 = geom1
self.geom2 = geom2
def inside(self, x):
return self.geom1.inside(x) or self.geom2.inside(x)
def on_boundary(self, x):
return (self.geom1.on_boundary(x) and not self.geom2.inside(x)) or (
self.geom2.on_boundary(x) and not self.geom1.inside(x)
)
def boundary_normal(self, x):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
return self.geom1.boundary_normal(x)
if self.geom2.on_boundary(x) and not self.geom1.inside(x):
return self.geom2.boundary_normal(x)
return np.zeros(self.dim)
def random_points(self, n, random="pseudo"):
x = []
while len(x) < n:
tmp = (
np.random.rand(n, self.dim) * (self.bbox[1] - self.bbox[0])
+ self.bbox[0]
)
x += filter(self.inside, tmp)
return np.array(x[:n])
def random_boundary_points(self, n, random="pseudo"):
x = [
i
for i in self.geom1.random_boundary_points(n, random=random)
if not self.geom2.inside(i)
]
x += [
i
for i in self.geom2.random_boundary_points(n, random=random)
if not self.geom1.inside(i)
]
if n != len(x):
print(
"Warning: {} points required, but {} points sampled. Uniform random is not guaranteed.".format(
n, len(x)
)
)
return np.array(x)
def periodic_point(self, x, component):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
y = self.geom1.periodic_point(x, component)
if self.on_boundary(y):
return y
if self.geom2.on_boundary(x) and not self.geom1.inside(x):
y = self.geom2.periodic_point(x, component)
if self.on_boundary(y):
return y
return x
class CSGDifference(geometry.Geometry):
"""Construct an object by CSG Difference."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} - {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGDifference, self).__init__(geom1.dim, geom1.bbox, geom1.diam)
self.geom1 = geom1
self.geom2 = geom2
def inside(self, x):
return self.geom1.inside(x) and not self.geom2.inside(x)
def on_boundary(self, x):
return (self.geom1.on_boundary(x) and not self.geom2.inside(x)) or (
self.geom1.inside(x) and self.geom2.on_boundary(x)
)
def boundary_normal(self, x):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
return self.geom1.boundary_normal(x)
if self.geom1.inside(x) and self.geom2.on_boundary(x):
return -self.geom2.boundary_normal(x)
return np.zeros(self.dim)
def random_points(self, n, random="pseudo"):
x = []
while len(x) < n:
x += [
i
for i in self.geom1.random_points(n, random=random)
if not self.geom2.inside(i)
]
return np.array(x[:n])
def random_boundary_points(self, n, random="pseudo"):
x = [
i
for i in self.geom1.random_boundary_points(n, random=random)
if not self.geom2.inside(i)
]
x += [
i
for i in self.geom2.random_boundary_points(n, random=random)
if self.geom1.inside(i)
]
if n != len(x):
print(
"Warning: {} points required, but {} points sampled. Uniform random is not guaranteed.".format(
n, len(x)
)
)
return np.array(x)
def periodic_point(self, x, component):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
y = self.geom1.periodic_point(x, component)
if self.on_boundary(y):
return y
return x
class CSGIntersection(geometry.Geometry):
"""Construct an object by CSG Intersection."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} & {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGIntersection, self).__init__(
geom1.dim,
(
np.maximum(geom1.bbox[0], geom2.bbox[0]),
np.minimum(geom1.bbox[1], geom2.bbox[1]),
),
min(geom1.diam, geom2.diam),
)
self.geom1 = geom1
self.geom2 = geom2
def inside(self, x):
return self.geom1.inside(x) and self.geom2.inside(x)
def on_boundary(self, x):
return (self.geom1.on_boundary(x) and self.geom2.inside(x)) or (
self.geom1.inside(x) and self.geom2.on_boundary(x)
)
def boundary_normal(self, x):
if self.geom1.on_boundary(x) and self.geom2.inside(x):
return self.geom1.boundary_normal(x)
if self.geom1.inside(x) and self.geom2.on_boundary(x):
return self.geom2.boundary_normal(x)
return np.zeros(self.dim)
def random_points(self, n, random="pseudo"):
x = []
while len(x) < n:
x += [
i
for i in self.geom1.random_points(n, random=random)
if self.geom2.inside(i)
]
return np.array(x[:n])
def random_boundary_points(self, n, random="pseudo"):
x = []
while len(x) < n:
x += [
i
for i in self.geom1.random_boundary_points(n, random=random)
if self.geom2.inside(i)
]
x += [
i
for i in self.geom2.random_boundary_points(n, random=random)
if self.geom1.inside(i)
]
if n != len(x):
print(
"Warning: {} points required, but {} points sampled. Uniform random is not guaranteed.".format(
n, len(x)
)
)
return np.array(x)
def periodic_point(self, x, component):
if self.geom1.on_boundary(x) and self.geom2.inside(x):
y = self.geom1.periodic_point(x, component)
if self.on_boundary(y):
return y
if self.geom2.on_boundary(x) and self.geom1.inside(x):
y = self.geom2.periodic_point(x, component)
if self.on_boundary(y):
return y
return x