-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_small_mesh_blocks.py
164 lines (142 loc) · 4.56 KB
/
find_small_mesh_blocks.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
#finds and deletes small blocks
#calculate volumne
import os
import numpy as np
def file_len(fname):
with open(fname) as f:
for i,l in enumerate(f):
pass
return i + 1
directory = 'C:/Users/Rebecca Napolitano/Google Drive/Documents/Research/mikehess/palazzo vecchio/2017_9_7_ElementiModels/FoundationModels/ExistingGeometry/'
#filename ='testy.txt'
filename = "GVol.3ddat"
outFile = 'blockstesty.txt'
outFile2 = 'thisisatest.txt'
os.chdir(directory)
#create the files and delete other existing ones
outfile = open(outFile, "w")
outfile2 = open(outFile2,"w")
outfile.close()
#count blocks
total = 0
with open(filename) as f:
for line in f:
finded = line.find('block')
if finded != -1 and finded != 0:
total += 1
#print(total)
f.close()
#read in the gvol file
data = open(filename).read().split('\n')
#find discrete block sections
#change ' ' to ';;;'
#open outfile for writing
outfile = open(outFile, "w+")
for line in data:
line= line.replace(' ', ';;;')
outfile.write(line + '\n')
outfile.close()
#keep lines with ';;;'
data = open(outFile).read().split('\n')
flag = ';;;'
for line in data:
if ';;;' in line:
line = line.replace(';;;','').replace('&','')
outfile2.write(line + '\n')
#check for number of blocks
i = 0 #for line counting function
fileLength = file_len(outFile2) #works!
numberBlocks = int(fileLength / 12)
#if numberBlocks != total:
# print("Error with the number of blocks!")
#iterate through the list of lines, and put the blocks faces and blocks back together
#each face has 3 sets of points and each block has 4 faces, so each block has 12 lines of code
start = 0
j = 1
data = open(outFile2).read().split('\n')
while j <= numberBlocks:
#grab twelve lines for that block
end = start + 12
#print("for block " + str(j) + "start is "+ str(start) + "end is " + str(end))
blockLine = data[start : end]
#remove spaces at the end of some lines
dataFixed = []
for line in blockLine:
if line[-1] == ' ':
line = line[0:-1]
dataFixed.append(line)
uniqueLines = set(dataFixed)
#print("This is block " + str(j))
#print(uniqueLines)
if len(uniqueLines) != 4:
print("Error in number of vertices!!")
dataFixed2 = []
#make array not list of strings
for line in uniqueLines:
line = line
dataFixed2.append(line)
#break it into each point
dataFixed3 = []
for line in dataFixed2:
line = line.split(" ")
dataFixed3.append(line)
#convert all strings to floats
dataFixed4 = []
for line in dataFixed3:
newLine = []
for entry in line:
entry = float(entry)
newLine.append(entry)
dataFixed4.append(newLine)
#calculate volume of triangle base pyramid
#>4 odd it is a pyramid; >4 even it is a prism; #do these using rhino functions
#find ab
abx = dataFixed4[1][0]-dataFixed4[0][0]
aby = dataFixed4[1][1]-dataFixed4[0][1]
abz = dataFixed4[1][2]-dataFixed4[0][2]
#find ac
acx = dataFixed4[2][0]-dataFixed4[0][0]
acy = dataFixed4[2][1]-dataFixed4[0][1]
acz = dataFixed4[2][2]-dataFixed4[0][2]
#find ad
adx = dataFixed4[3][0]-dataFixed4[0][0]
ady = dataFixed4[3][1]-dataFixed4[0][1]
adz = dataFixed4[3][2]-dataFixed4[0][2]
#make a matrix
matrixBlock = [[abx, aby, abz], [acx, acy, acz], [adx, ady, adz]]
#find triple product or determinate
det = np.linalg.det(matrixBlock)
#take absolute value
det = abs(det)
#find volume (abs(det)/6)
volume = det/6
#find centroid by looping over all vertices and points for each block
vertex = 0
while vertex <= 3:
xCent = 0
xPoint = 0
while xPoint <= 2:
xCent = xCent + dataFixed4[vertex][xPoint]
xPoint = xPoint + 1
xCent = xCent / 4
yCent = 0
yPoint = 0
while yPoint <=2:
yCent = yCent + dataFixed4[vertex][yPoint]
yPoint = yPoint + 1
yCent = yCent / 4
zCent = 0
zPoint = 0
while zPoint <= 2:
zCent = zCent + dataFixed4[vertex][zPoint]
zPoint = zPoint + 1
zCent = zCent / 4
vertex = vertex + 1
centroid = [xCent, yCent, zCent]
if volume < 1e-5:
print("Volume of block " + str(j) + " is: " + str(volume) + " and the centroid is " + str(centroid))
start = start + 12 #increments for each block
j = j + 1
outfile2.close()
#make sure all files are closed
#delete outfiles created during process