您的位置:首页 > 其它

read numbers from file and exchange their positions then write into another file

2016-03-04 00:30 615 查看
Recently I have been trying to implement the paper named "hierarchical position based dynamics", one of the meshes the author used is very regular like the picture below:



but the off file in CGAL I can find is like this:



Therefore, I have to change the off file into another topological structure just like the red one. So let's begin:

we copy the face part of the off file into 1.txt of which parts are given like:

3 0 1 22
3 0 22 21
3 21 22 43
3 21 43 42
3 42 43 64
3 42 64 63
3 63 64 85
3 63 85 84
3 84 85 106
3 84 106 105
3 105 106 127
3 105 127 126
3 126 127 148
3 126 148 147
3 147 148 169
3 147 169 168
3 168 169 190
3 168 190 189
3 189 190 211
3 189 211 210
3 210 211 232
3 210 232 231
3 231 232 253
3 231 253 252
3 252 253 274
3 252 274 273
3 273 274 295
3 273 295 294
3 294 295 316
3 294 316 315
3 315 316 337
3 315 337 336
3 336 337 358
3 336 358 357
3 357 358 379
3 357 379 378
3 378 379 400
3 378 400 399
3 399 400 421
3 399 421 420
3 1 2 23
3 1 23 22
3 22 23 44
3 22 44 43
3 43 44 65
3 43 65 64
3 64 65 86
3 64 86 85
3 85 86 107
3 85 107 106
3 106 107 128
3 106 128 127
3 127 128 149
3 127 149 148
3 148 149 170
3 148 170 169
3 169 170 191
3 169 191 190
3 190 191 212
3 190 212 211
3 211 212 233
3 211 233 232
3 232 233 254
3 232 254 253
3 253 254 275
3 253 275 274
3 274 275 296
3 274 296 295
3 295 296 317
3 295 317 316
3 316 317 338
3 316 338 337
3 337 338 359
3 337 359 358
3 358 359 380
3 358 380 379
3 379 380 401
3 379 401 400
3 400 401 422
3 400 422 421


Here is the point, when two faces in (even columns and even rows) or (odd columns and odd rows), then topology need to change. Note the index is based on 0.

the change rule is like below:

f1_1 f1_2 f1_3

f2_1 f2_2 f2_3

change to

f1_1 f1_2 f2_3

f2_3 f1_2 f1_3

here is the python source code:

f_in = open("1.txt")
f_out = open("2.txt", "w")
for col in range(0,20):
even_col = col % 2 # 0 is an even col
for row in range(0,20):
should_change = (row + even_col) % 2
line1 = f_in.readline()
line2 = f_in.readline()
numbers1 = list(map(int, line1.split()))
numbers2 = list(map(int, line2.split()))
if( should_change):
f_out.write("%d %d %d %d\n" % (numbers1[0], numbers1[1], numbers1[2], numbers2[3]))
f_out.write("%d %d %d %d\n" % (numbers2[0], numbers2[3], numbers1[2], numbers1[3]))
print("%d %d %d %d" % (numbers1[0], numbers1[1], numbers1[2], numbers2[3]))
print("%d %d %d %d" % (numbers2[0], numbers2[3], numbers1[2], numbers1[3]))
else:
f_out.write("%d %d %d %d\n" % (numbers1[0], numbers1[1], numbers1[2], numbers1[3]))
f_out.write("%d %d %d %d\n" % (numbers2[0], numbers2[1], numbers2[2], numbers2[3]))
print("%d %d %d %d" % (numbers1[0], numbers1[1], numbers1[2], numbers1[3]))
print("%d %d %d %d" % (numbers2[0], numbers2[1], numbers2[2], numbers2[3]))
if not line2: break  # EOF
print("exchange done!")
f_out.close()
f_in.close()


After changing, we write the new topological faces into 2.txt, and then replace the face part of off file with 2.txt. Finally, we get what we want.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: