forked from orbingol/NURBS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompatibility.py
357 lines (276 loc) · 11 KB
/
compatibility.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
.. module:: compatibility
:platform: Unix, Windows
:synopsis: Provides compatibility functions for CAD interoperability
.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>
"""
def flip_ctrlpts_u(ctrlpts, size_u, size_v):
""" Flips a list of 1-dimensional control points from u-row order to v-row order.
**u-row order**: each row corresponds to a list of u values
**v-row order**: each row corresponds to a list of v values
:param ctrlpts: control points in u-row order
:type ctrlpts: list, tuple
:param size_u: size in u-direction
:type size_u: int
:param size_v: size in v-direction
:type size_v: int
:return: control points in v-row order
:rtype: list
"""
new_ctrlpts = []
for i in range(0, size_u):
for j in range(0, size_v):
temp = [float(c) for c in ctrlpts[i + (j * size_u)]]
new_ctrlpts.append(temp)
return new_ctrlpts
def flip_ctrlpts(ctrlpts, size_u, size_v):
""" Flips a list of 1-dimensional control points from v-row order to u-row order.
**u-row order**: each row corresponds to a list of u values
**v-row order**: each row corresponds to a list of v values
:param ctrlpts: control points in v-row order
:type ctrlpts: list, tuple
:param size_u: size in u-direction
:type size_u: int
:param size_v: size in v-direction
:type size_v: int
:return: control points in u-row order
:rtype: list
"""
new_ctrlpts = []
for i in range(0, size_v):
for j in range(0, size_u):
temp = [float(c) for c in ctrlpts[i + (j * size_v)]]
new_ctrlpts.append(temp)
return new_ctrlpts
def flip_ctrlpts2d(ctrlpts2d, size_u=0, size_v=0):
""" Flips a list of surface 2-D control points from *[u][v]* to *[v][u]* order.
:param ctrlpts2d: 2-D control points
:type ctrlpts2d: list, tuple
:param size_u: size in U-direction (row length)
:type size_u: int
:param size_v: size in V-direction (column length)
:type size_v: int
:return: flipped 2-D control points
:rtype: list
"""
if size_u <= 0 or size_v <= 0:
# Detect array shapes
size_u = len(ctrlpts2d)
size_v = len(ctrlpts2d[0])
new_ctrlpts2d = [[[] for _ in range(size_u)] for _ in range(size_v)]
for i in range(size_v):
for j in range(size_u):
new_ctrlpts2d[i][j] = [float(c) for c in ctrlpts2d[j][i]]
return new_ctrlpts2d
def generate_ctrlptsw(ctrlpts):
""" Generates weighted control points from unweighted ones in 1-D.
This function
#. Takes in a 1-D control points list whose coordinates are organized in (x, y, z, w) format
#. converts into (x*w, y*w, z*w, w) format
#. Returns the result
:param ctrlpts: 1-D control points (P)
:type ctrlpts: list
:return: 1-D weighted control points (Pw)
:rtype: list
"""
# Multiply control points by weight
new_ctrlpts = []
for cpt in ctrlpts:
temp = [float(pt * cpt[-1]) for pt in cpt]
temp[-1] = float(cpt[-1])
new_ctrlpts.append(temp)
return new_ctrlpts
def generate_ctrlptsw2d(ctrlpts2d):
""" Generates weighted control points from unweighted ones in 2-D.
This function
#. Takes in a 2D control points list whose coordinates are organized in (x, y, z, w) format
#. converts into (x*w, y*w, z*w, w) format
#. Returns the result
Therefore, the returned list could be a direct input of the NURBS.Surface class.
:param ctrlpts2d: 2-D control points (P)
:type ctrlpts2d: list
:return: 2-D weighted control points (Pw)
:rtype: list
"""
# Multiply control points by weight
new_ctrlpts2d = []
for row in ctrlpts2d:
ctrlptsw_v = []
for col in row:
temp = [float(c * col[-1]) for c in col]
temp[-1] = float(col[-1])
ctrlptsw_v.append(temp)
new_ctrlpts2d.append(ctrlptsw_v)
return new_ctrlpts2d
def generate_ctrlpts_weights(ctrlpts):
""" Generates unweighted control points from weighted ones in 1-D.
This function
#. Takes in 1-D control points list whose coordinates are organized in (x*w, y*w, z*w, w) format
#. Converts the input control points list into (x, y, z, w) format
#. Returns the result
:param ctrlpts: 1-D control points (P)
:type ctrlpts: list
:return: 1-D weighted control points (Pw)
:rtype: list
"""
# Divide control points by weight
new_ctrlpts = []
for cpt in ctrlpts:
temp = [float(pt / cpt[-1]) for pt in cpt]
temp[-1] = float(cpt[-1])
new_ctrlpts.append(temp)
return new_ctrlpts
def generate_ctrlpts2d_weights(ctrlpts2d):
""" Generates unweighted control points from weighted ones in 2-D.
This function
#. Takes in 2-D control points list whose coordinates are organized like (x*w, y*w, z*w, w)
#. Converts the input control points list into (x, y, z, w) format
#. Returns the result
:param ctrlpts2d: 2-D control points (P)
:type ctrlpts2d: list
:return: 2-D weighted control points (Pw)
:rtype: list
"""
# Divide control points by weight
new_ctrlpts2d = []
for row in ctrlpts2d:
ctrlptsw_v = []
for col in row:
temp = [float(c / col[-1]) for c in col]
temp[-1] = float(col[-1])
ctrlptsw_v.append(temp)
new_ctrlpts2d.append(ctrlptsw_v)
return new_ctrlpts2d
def combine_ctrlpts_weights(ctrlpts, weights=None):
""" Multiplies control points by the weights to generate weighted control points.
This function is dimension agnostic, i.e. control points can be in any dimension but weights should be 1D.
The ``weights`` function parameter can be set to None to let the function generate a weights vector composed of
1.0 values. This feature can be used to convert B-Spline basis to NURBS basis.
:param ctrlpts: unweighted control points
:type ctrlpts: list, tuple
:param weights: weights vector; if set to None, a weights vector of 1.0s will be automatically generated
:type weights: list, tuple or None
:return: weighted control points
:rtype: list
"""
if weights is None:
weights = [1.0 for _ in range(len(ctrlpts))]
ctrlptsw = []
for pt, w in zip(ctrlpts, weights):
temp = [float(c * w) for c in pt]
temp.append(float(w))
ctrlptsw.append(temp)
return ctrlptsw
def separate_ctrlpts_weights(ctrlptsw):
""" Divides weighted control points by weights to generate unweighted control points and weights vector.
This function is dimension agnostic, i.e. control points can be in any dimension but the last element of the array
should indicate the weight.
:param ctrlptsw: weighted control points
:type ctrlptsw: list, tuple
:return: unweighted control points and weights vector
:rtype: list
"""
ctrlpts = []
weights = []
for ptw in ctrlptsw:
temp = [float(pw / ptw[-1]) for pw in ptw[:-1]]
ctrlpts.append(temp)
weights.append(ptw[-1])
return [ctrlpts, weights]
def flip_ctrlpts2d_file(file_in='', file_out='ctrlpts_flip.txt'):
""" Flips u and v directions of a 2D control points file and saves flipped coordinates to a file.
:param file_in: name of the input file (to be read)
:type file_in: str
:param file_out: name of the output file (to be saved)
:type file_out: str
:raises IOError: an error occurred reading or writing the file
"""
# Read control points
ctrlpts2d, size_u, size_v = _read_ctrltps2d_file(file_in)
# Flip control points array
new_ctrlpts2d = flip_ctrlpts2d(ctrlpts2d, size_u, size_v)
# Save new control points
_save_ctrlpts2d_file(new_ctrlpts2d, size_u, size_v, file_out)
def generate_ctrlptsw2d_file(file_in='', file_out='ctrlptsw.txt'):
""" Generates weighted control points from unweighted ones in 2-D.
This function
#. Takes in a 2-D control points file whose coordinates are organized in (x, y, z, w) format
#. Converts into (x*w, y*w, z*w, w) format
#. Saves the result to a file
Therefore, the resultant file could be a direct input of the NURBS.Surface class.
:param file_in: name of the input file (to be read)
:type file_in: str
:param file_out: name of the output file (to be saved)
:type file_out: str
:raises IOError: an error occurred reading or writing the file
"""
# Read control points
ctrlpts2d, size_u, size_v = _read_ctrltps2d_file(file_in)
# Multiply control points by weight
new_ctrlpts2d = generate_ctrlptsw2d(ctrlpts2d)
# Save new control points
_save_ctrlpts2d_file(new_ctrlpts2d, size_u, size_v, file_out)
def generate_ctrlpts2d_weights_file(file_in='', file_out='ctrlpts_weights.txt'):
""" Generates unweighted control points from weighted ones in 2-D.
#. Takes in 2-D control points list whose coordinates are organized like (x*w, y*w, z*w, w)
#. Converts the input control points list into (x, y, z, w) format
#. Saves the result to a file
:param file_in: name of the input file (to be read)
:type file_in: str
:param file_out: name of the output file (to be saved)
:type file_out: str
:raises IOError: an error occurred reading or writing the file
"""
# Read control points
ctrlpts2d, size_u, size_v = _read_ctrltps2d_file(file_in)
# Divide control points by weight
new_ctrlpts2d = generate_ctrlpts2d_weights(ctrlpts2d)
# Save new control points
_save_ctrlpts2d_file(new_ctrlpts2d, size_u, size_v, file_out)
def _read_ctrltps2d_file(file_in):
ctrlpts = []
size_u = 0
size_v = 0
try:
with open(file_in, 'r') as fp:
for line in fp:
line = line.strip()
control_point_row = line.split(';')
size_v = 0
ctrlpts_v = []
for cpr in control_point_row:
cpt = cpr.split(',')
pt_temp = []
for pt in cpt:
pt_temp.append(float(pt.strip()))
ctrlpts_v.append(pt_temp)
size_v += 1
ctrlpts.append(ctrlpts_v)
size_u += 1
except IOError as e:
print("An error occurred: {}".format(e.args[-1]))
raise e
except Exception:
raise
return ctrlpts, size_u, size_v
def _save_ctrlpts2d_file(ctrlpts2d, size_u, size_v, file_out):
try:
with open(file_out, 'w') as fp:
fp.truncate()
for i in range(size_u):
line = ""
for j in range(size_v):
for idx, coord in enumerate(ctrlpts2d[i][j]):
if idx: # Add comma if we are not on the first element
line += ","
line += str(coord)
if j != size_u - 1:
line += ";"
else:
line += "\n"
fp.write(line)
except IOError as e:
print("An error occurred: {}".format(e.args[-1]))
raise e
except Exception:
raise