-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_export_coordinates.jl
381 lines (288 loc) · 10.7 KB
/
temp_export_coordinates.jl
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using Printf
function grid3D(x,y,z)
X = [ i for i ∈ x , j ∈ 1:length(y), k ∈ 1:length(z) ]
Y = [ j for i ∈ 1:length(x), j ∈ y , k ∈ 1:length(z) ]
Z = [ k for i ∈ 1:length(x), j ∈ 1:length(y), k ∈ z ]
return X, Y, Z
end
function occursOnce(virtualFaceIndices)
L=zeros(Bool,size(virtualFaceIndices))
for q=1:1:length(virtualFaceIndices)
L[q]=count(virtualFaceIndices.==virtualFaceIndices[q])==1
end
return L
end
function getBoundaryFaces(F)
#Select boundary faces only
Fs=sort(F,dims=2) #Sorted version of F for uniqueness test
sizVirtual=maximum(max,F) #-minimum(min,F)+1
virtualFaceIndices=sub2indn(sizVirtual.*ones(1,size(F,2)),Fs)
#virtualFaceIndices_uni=unique(virtualFaceIndices)
# indFacesUnique=indexin(virtualFaceIndices_uni,virtualFaceIndices)
# F=F[indFacesUnique,:] #Unique faces
#Remove non-boundary faces (internal faces are shared by >1 element)
#c=[count(x->x==q,virtualFaceIndices)==1 for q in virtualFaceIndices]
L=occursOnce(virtualFaceIndices)
return L
end
function sub2indn(siz,A)
numDim = length(siz)
k = cumprod([siz[i] for i in 1:length(siz)],dims=1)
if numDim < 2
error("Invalid size specified. The number of dimensions should be equal or larger than 2")
end
if any(size(A).==0)
A=[];
end
if !isempty(A)
if size(A,2) != numDim
error("The specified array size and number of subscript index columns do not match")
end
#Verify subscripts are within range
(m,indMax)=findmax(A,dims=1)
if any(A.<1) || any(m.>siz)
error("Index out of range")
end
ind=A[:,1];
for q=2:1:numDim
ind = ind .+ (A[:,q].-1).*k[q-1]
end
else
ind=[]
end
return ind
end
function sub2ind(siz,I,J,K)
numDim = length(siz);
k = cumprod([siz[i] for i in 1:length(siz)],dims=1)
if numDim < 2
error("Invalid size specified. The number of dimensions should be equal or larger than 2")
end
if isempty(I) || isempty(J) || isempty(K)
ind=[]
else
#Verify subscripts are within range
if any(I.<1) || any(I.>siz[1])
error("Row index out of range")
end
if any(J.<1) || any(J.>siz[2])
error("Column index out of range")
end
if any(K.<1) || any(K.>siz[3])
error("Slice index out of range")
end
ind = I .+ (J.-1).*k[1].+ (K.-1).*k[2]
end
return ind
end
function ind2subn(siz,ind)
numDim = length(siz);
k = cumprod([siz[i] for i in 1:length(siz)],dims=1)
if numDim < 2
error("Invalid size specified. The number of dimensions should be equal or larger than 2")
end
#Verify subscripts are within range
if any(ind.<1) || any(ind.>prod(siz))
error("Index out of range")
end
A=zeros(Int64,length(ind),numDim) #Initializing output array
for q=numDim:-1:1 #For all dimensions
if q==1 #First 1st dimension
A[:,1]=rem.(ind.-1,k[1]).+1;
else
p=rem.(ind.-1,k[q-1]) .+ 1 # "previous"
A[:,q]=(ind.-p)./k[q-1] .+ 1 # Current
ind=p #Set indices as "previous"
end
end
return A
end
function ind2subn(siz,ind)
numDim = length(siz);
k = cumprod([siz[i] for i in 1:length(siz)],dims=1)
if numDim < 2
error("Invalid size specified. The number of dimensions should be equal or larger than 2")
end
#Verify subscripts are within range
if any(ind.<1) || any(ind.>prod(siz))
error("Index out of range")
end
A=zeros(Int64,length(ind),numDim) #Initializing output array
for q=numDim:-1:1 #For all dimensions
if q==1 #First 1st dimension
A[:,1]=rem.(ind.-1,k[1]).+1;
else
p=rem.(ind.-1,k[q-1]) .+ 1 # "previous"
A[:,q]=(ind.-p)./k[q-1] .+ 1 # Current
ind=p #Set indices as "previous"
end
end
return A
end
function ind2sub(siz,ind)
numDim = length(siz);
k = cumprod([siz[i] for i in 1:length(siz)],dims=1)
if numDim < 2
error("Invalid size specified. The number of dimensions should be equal or larger than 2")
end
#Verify subscripts are within range
if any(ind.<1) || any(ind.>prod(siz))
error("Index out of range")
end
A=zeros(Int64,length(ind),numDim) #Initializing output array
for q=numDim:-1:1 #For all dimensions
if q==1 #First 1st dimension
A[:,1]=rem.(ind.-1,k[1]).+1;
else
p=rem.(ind.-1,k[q-1]) .+ 1 # "previous"
A[:,q]=(ind.-p)./k[q-1] .+ 1 # Current
ind=p #Set indices as "previous"
end
end
return A[:,1],A[:,2],A[:,3]
end
function ind2faces(siz,ijk)
Fi=[ijk[:,1] ijk[:,1].+1 ijk[:,1].+1 ijk[:,1] ; #Top
ijk[:,1] ijk[:,1].+1 ijk[:,1].+1 ijk[:,1] ; #Bottom
ijk[:,1] ijk[:,1].+1 ijk[:,1].+1 ijk[:,1] ; #Front
ijk[:,1] ijk[:,1].+1 ijk[:,1].+1 ijk[:,1] ; #Back
ijk[:,1] ijk[:,1] ijk[:,1] ijk[:,1] ; #Side 1
ijk[:,1].+1 ijk[:,1].+1 ijk[:,1].+1 ijk[:,1].+1; #Side 2
]
Fj=[ijk[:,2].+1 ijk[:,2].+1 ijk[:,2] ijk[:,2] ; #Top
ijk[:,2] ijk[:,2] ijk[:,2].+1 ijk[:,2].+1; #Bottom
ijk[:,2] ijk[:,2] ijk[:,2] ijk[:,2] ; #Front
ijk[:,2].+1 ijk[:,2].+1 ijk[:,2].+1 ijk[:,2].+1; #Back
ijk[:,2] ijk[:,2].+1 ijk[:,2].+1 ijk[:,2] ; #Side 1
ijk[:,2].+1 ijk[:,2] ijk[:,2] ijk[:,2].+1; #Side 2
]
Fk=[ijk[:,3] ijk[:,3] ijk[:,3] ijk[:,3] ; #Top
ijk[:,3].+1 ijk[:,3].+1 ijk[:,3].+1 ijk[:,3].+1; #Bottom
ijk[:,3] ijk[:,3] ijk[:,3].+1 ijk[:,3].+1; #Front
ijk[:,3].+1 ijk[:,3].+1 ijk[:,3] ijk[:,3] ; #Back
ijk[:,3].+1 ijk[:,3].+1 ijk[:,3] ijk[:,3] ; #Side 1
ijk[:,3].+1 ijk[:,3].+1 ijk[:,3] ijk[:,3] ; #Side 2
]
F = sub2ind(siz.+1,Fi,Fj,Fk)
n=size(ijk,1)
N = [repeat([-1 0 0 ],n,1);
repeat([ 0 0 1 ],n,1);
repeat([-1 0 0 ],n,1);
repeat([ 1 0 0 ],n,1);
repeat([ 0 -1 0 ],n,1);
repeat([ 0 1 0 ],n,1);
]
return F,N
end
function toSTL(F,V,N,fileName)
fileHandle = open(fileName, "w")
#Start solid section
write(fileHandle, "solid part \n")
#Loop over faces
for qf=1:1:size(F,1) #For all faces
write(fileHandle, " facet normal " * @sprintf("%d",N[qf,1]) * " " * @sprintf("%d",N[qf,2]) * " " * @sprintf("%d",N[qf,3]) * "\n")
write(fileHandle, " outer loop \n")
for qv=1:1:size(F,2) #For all face vertices
#@sprintf("%.12e",pi)
write(fileHandle," vertex " * @sprintf("%.12e",V[F[qf,qv],1]) * " " * @sprintf("%.12e",V[F[qf,qv],2]) * " " * @sprintf("%.12e",V[F[qf,qv],3]) * "\n")
end
write(fileHandle, " endloop \n")
write(fileHandle, " endfacet \n")
end
write(fileHandle, "endsolid part \n")
close(fileHandle)
end
function im2patch(imageSize,indFound)
ijkFound=ind2subn(imageSize,indFound)
#Composed raw faces array
F,N=ind2faces(imageSize,ijkFound)
logicBoundaryFaces=getBoundaryFaces(F)
F=F[logicBoundaryFaces,:]
N=N[logicBoundaryFaces,:]
#Check which nodes are used
indUsed=unique(F)
#Fix face indices in anticipation of a reduced coordinate set
indFix1=1:1:length(indUsed)
indFix2=zeros(Int64,maximum(max,F),1)
indFix2[indUsed]=indFix1
F=indFix2[F]
#Create coordinate array
I,J,K=ind2sub(imageSize.+1,indUsed)
Iv=convert.(Float64,I).-0.5
Jv=convert.(Float64,J).-0.5
Kv=convert.(Float64,K).-0.5
V=[Iv Jv Kv] #Coordinates
return F,V,N
end
function toOBJ(F,V,N,fileName)
splitName = splitext(fileName)
fileNameNoExt = splitName[1]
fileNameNoExt = splitdir(fileNameNoExt)
pathName = fileNameNoExt[1]
objName = fileNameNoExt[2]
createUseMTL=false
# Setup OBJ file for writing
fileHandleOBJ = open(fileName, "w") #OBJ file handle
# Comment line
write(fileHandleOBJ, "# Created using Julia code \n")
if createUseMTL
# Setup MTL file for writing
fileNameMTL = joinpath(pathName,objName*".mtl")
# mtllib line
write(fileHandleOBJ, "mtllib " * objName * ".mtl \n")
end
# Start object line
write(fileHandleOBJ, "o " * objName * " \n")
# Define vertex field
for q=1:1:size(V,1) #For all vertices
write(fileHandleOBJ,"v " * @sprintf("%.12e",V[q,1]) * " " * @sprintf("%.12e",V[q,2]) * " " * @sprintf("%.12e",V[q,3]) * "\n")
end
# Define normal vector field
for q=1:1:size(N,1) #For all normal vectors
write(fileHandleOBJ,"vn " * @sprintf("%.12e",N[q,1]) * " " * @sprintf("%.12e",N[q,2]) * " " * @sprintf("%.12e",N[q,3]) * "\n")
end
# Solid? line
write(fileHandleOBJ, "s 1 \n")
if createUseMTL
# MTL spec line
write(fileHandleOBJ, "usemtl " * objName * " \n")
end
# Define face field
for q=1:1:size(F,1) #For all faces
textToWrite="f "
for qf=1:1:size(F,2)
textToWrite*= @sprintf("%d",F[q,qf]) * "//" * @sprintf("%d",F[q,qf]) * " "
end
write(fileHandleOBJ,textToWrite * "\n")
end
close(fileHandleOBJ)
# Create MTL file
if createUseMTL
fileHandleMTL = open(fileNameMTL, "w")
write(fileHandleMTL, "newmtl " * objName * " \n")
write(fileHandleMTL, "Ns 0.000000 \n")
write(fileHandleMTL, "Ka 1.000000 1.000000 1.000000 \n")
write(fileHandleMTL, "Ks 0.000000 0.000000 0.000000 \n")
write(fileHandleMTL, "Ke 0.000000 0.000000 0.000000 \n")
write(fileHandleMTL, "Ni 1.450000 \n")
write(fileHandleMTL, "illum 1 \n")
write(fileHandleMTL, "Ni 1.450000 \n")
write(fileHandleMTL, "map_Kd " * objName * ".png" * " \n")
write(fileHandleMTL, "map_d " * objName * ".png" * " \n")
close(fileHandleMTL)
end
end
##################
s=0.2
r=1;
X,Y,Z= grid3D(-r:s:r,-r:s:r,-r:s:r)
M = sqrt.(X.^2 .+ Y.^2 .+ Z.^2)
imageSize=size(M)
indFound=[sub2ind(imageSize,i[1],i[2],i[3]) for i ∈ findall(M.<=(r+s/100))]
#ijkFound=ind2subn(imageSize,indFound);
F,V,N = im2patch(imageSize,indFound)
FT=[F[:,1] F[:,2] F[:,3]; F[:,3] F[:,4] F[:,1]] #Convert to triangles
NT=[N;N]
toSTL(FT,V,NT,"/home/kevin/Desktop/temp.stl")
fileName = "/home/kevin/Desktop/temp.obj"
toOBJ(F,V,N,fileName)