-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions_np.py
More file actions
295 lines (227 loc) · 9.84 KB
/
functions_np.py
File metadata and controls
295 lines (227 loc) · 9.84 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from abc import ABC, abstractmethod
import numpy as np
import math
## ABC
class ImplicitFunction(ABC):
def __init__(self, **kwargs):
pass
def __call__(self, **kwargs):
pass
def postprocess4bitmap(self, bitmap, **kwargs):
pass
def bitmapPadding(self, img, x_padding=4, y_padding=0):
if y_padding > 0:
img[:y_padding, :] = 255
img[-y_padding:, :] = 255
if x_padding > 0:
img[:, :x_padding] = 255
img[:, -x_padding:] = 255
return img
def padding(self, x, y, value, x_padding, y_padding):
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
if x_padding > 0:
value[x - xmin <= x_padding or xmax - x <= x_padding] = 255
if y_padding > 0:
value[y - ymin <= y_padding or ymax - y <= y_padding] = 255
return value
class ImplicitFunctionFactory():
pass
class Gyroid(ImplicitFunction):
def __init__(self, cycleLayerNumber=25, **kwargs):
super().__init__(**kwargs)
self.savedSlicer = dict()
self.cycleLayerNumber = cycleLayerNumber
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x, _y, _z = x * np.pi * 2 / 2, y * np.pi * 2 / 2, z * np.pi * 2 / 2
value = np.sin(_x) * np.cos(_y) + \
np.sin(_y) * np.cos(_z) + \
np.sin(_z) * np.cos(_x)
bitmap = (value > 0).astype(int) * 255
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
class Gyroid1(ImplicitFunction):
def __init__(self, cycleLayerNumber=1e9, isovalue=0, **kwargs):
super().__init__(**kwargs)
self.savedSlicer = dict()
self.cycleLayerNumber = cycleLayerNumber
self.isovalue = isovalue
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x, _y, _z = x * np.pi * 2 / 10, y * np.pi * 2 / 10, z * np.pi * 2 / 10
value = np.sin(_x) * np.cos(_y) + \
np.sin(_y) * np.cos(_z) + \
np.sin(_z) * np.cos(_x)
bitmap = (value > self.isovalue).astype(int) * 255
if self.cycleLayerNumber < 3000:
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
## ABC
class TPMS(ImplicitFunction):
def __init__(self, k, isovalue=0, cycleLayerNumber=1e9, **kwargs):
super().__init__(**kwargs)
self.savedSlicer = dict()
self.cycleLayerNumber = cycleLayerNumber
self.k = k
self.isovalue = isovalue
self.x_padding_px = 0 if 'x_padding_px' not in kwargs else kwargs['x_padding_px']
self.y_padding_px = 0 if 'y_padding_px' not in kwargs else kwargs['y_padding_px']
self.x_padding_mm = 0 if 'x_padding_mm' not in kwargs else kwargs['x_padding_mm']
self.y_padding_mm = 0 if 'y_padding_mm' not in kwargs else kwargs['y_padding_mm']
def __call__(self, x, y, z, zi):
pass
def postprocess4bitmap(self, bitmap):
return self.bitmapPadding(bitmap, self.x_padding_px, self.y_padding_px)
def postprocess(self, x, y, value):
return self.padding(x, y, value, self.x_padding_mm, self.y_padding_mm)
class GyroidSolid(TPMS):
def __init__(self, k, isovalue=0, cycleLayerNumber=1e9, **kwargs):
super().__init__(k, isovalue, cycleLayerNumber, **kwargs)
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x = x * np.pi * 2 / self.k
_y = y * np.pi * 2 / self.k
_z = z * np.pi * 2 / self.k
value = np.sin(_x) * np.cos(_y) + \
np.sin(_y) * np.cos(_z) + \
np.sin(_z) * np.cos(_x)
bitmap = (value > self.isovalue).astype(int) * 255
if self.cycleLayerNumber < 3000:
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
class GyroidSheet(TPMS):
def __init__(self, k, isovalue=0, cycleLayerNumber=1e9, **kwargs):
super().__init__(k, isovalue, cycleLayerNumber, **kwargs)
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x = x * np.pi * 2 / self.k
_y = y * np.pi * 2 / self.k
_z = z * np.pi * 2 / self.k
value = np.sin(_x) * np.cos(_y) + \
np.sin(_y) * np.cos(_z) + \
np.sin(_z) * np.cos(_x)
value = np.abs(value)
bitmap = (value < self.isovalue).astype(int) * 255
if self.cycleLayerNumber < 3000:
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
class FKSSolid(TPMS):
def __init__(self, k, isovalue=0, cycleLayerNumber=1e9, **kwargs):
super().__init__(k, isovalue, cycleLayerNumber, **kwargs)
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x = x * np.pi * 2 / self.k
_y = y * np.pi * 2 / self.k
_z = z * np.pi * 2 / self.k
value = np.cos(2 * _x) * np.sin(_y) * np.cos(_z) + \
np.cos(_x) * np.cos(2 * _y) * np.sin(_z) + \
np.sin(_x) * np.cos(_y) * np.cos(2 * _z)
bitmap = (value > self.isovalue).astype(int) * 255
if self.cycleLayerNumber < 3000:
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
class FKSSheet(TPMS):
def __init__(self, k, isovalue=0, cycleLayerNumber=1e9, **kwargs):
super().__init__(k, isovalue, cycleLayerNumber, **kwargs)
def __call__(self, x, y, z, zi):
if zi < self.cycleLayerNumber:
_x = x * np.pi * 2 / self.k
_y = y * np.pi * 2 / self.k
_z = z * np.pi * 2 / self.k
value = np.cos(2 * _x) * np.sin(_y) * np.cos(_z) + \
np.cos(_x) * np.cos(2 * _y) * np.sin(_z) + \
np.sin(_x) * np.cos(_y) * np.cos(2 * _z)
value = np.abs(value)
bitmap = (value < self.isovalue).astype(int) * 255
if self.cycleLayerNumber < 3000:
self.savedSlicer[zi] = bitmap.copy()
return bitmap
else:
oldz = zi % self.cycleLayerNumber
return self.savedSlicer[oldz]
class GyroidSolidOBBRotation(TPMS):
def __init__(self, k, isovalue, OBBSize=None, theta=math.pi / 6):
super().__init__(k, isovalue)
if OBBSize is None:
self.OBBSize = np.asarray([300, 25, 300])
self.theta = theta # 30 degrees in radians
sinTheta = math.sin(theta)
cosTheta = math.cos(theta)
# [300., 171.65063509, 272.30762114]
self.AABBSize = np.asarray([self.OBBSize[0],
self.OBBSize[2] * sinTheta + self.OBBSize[1] * cosTheta,
self.OBBSize[2] * cosTheta + self.OBBSize[1] * sinTheta])
self.R = np.array([
[1, 0, 0],
[0, np.cos(self.theta), np.sin(self.theta)],
[0, -np.sin(self.theta), np.cos(self.theta)]
])
self.O = np.array([0, self.OBBSize[1] * cosTheta, self.OBBSize[1] * sinTheta])
self.k = k
self.isovalue = isovalue
self.OBBCenter = self.AABBSize / 2
self.OBBLength = self.OBBSize / 2
self.OBBAxis = self.R @ np.eye(3)
self.Tz = k * self.OBBSize[1] / 2
def gyroid(self, xyz):
x, y, z = xyz[:, 0], xyz[:, 1], xyz[:, 2]
return np.sin(x) * np.cos(y) + np.sin(y) * np.cos(z) + np.sin(z) * np.cos(x)
def __call__(self, xx, yy, zz, zi):
xyz = np.vstack((xx, yy, zz + np.zeros_like(xx))) # position
xyzNew = xyz * np.pi * 2 / self.k # used to calculate the TPMS
Tz = np.zeros_like(xyzNew)
Tz[:, 1] = self.Tz
new_xyz = self.R @ xyzNew + Tz
xyzinObbMasked = self.are_points_in_obb(xyz.T, self.OBBCenter, self.OBBAxis, self.OBBLength)
valueMasked = (self.gyroid(new_xyz.T[xyzinObbMasked]) > self.isovalue).astype(int) * 255
value = np.zeros_like(xx).astype(int) # debug for obb
value[xyzinObbMasked] = valueMasked
return value
@staticmethod
def are_points_in_axis_aligned_cuboid(points, center, lengths):
half_lengths = lengths / 2
# Translate points to the cuboid center
translated_points = points - center
# Check if points are within the half-lengths
within_half_lengths = np.all(np.abs(translated_points) <= half_lengths, axis=1)
return within_half_lengths
@staticmethod
def are_points_in_obb(points, center, axes, half_lengths):
# Translate points to the OBB center
translated_points = points - center
# Initialize a boolean array to track if each point is inside the OBB
inside = np.ones(points.shape[0], dtype=bool)
# Check each axis
for axis, half_length in zip(axes, half_lengths):
# Project the local points onto the OBB's axis
projection_lengths = np.dot(translated_points, axis)
# Check if the projection lengths are within the half-lengths
inside = np.logical_and(inside, np.abs(projection_lengths) <= half_length)
return inside
@staticmethod
def bin_search(f, a, b, tol):
while (b - a) / 2 > tol:
c = (a + b) / 2
if abs(f(c)) < tol:
return c
elif f(a) * f(c) < 0:
b = c
else:
a = c
return (a + b) / 2