166 lines
4.9 KiB
166 lines
4.9 KiB
# coding=utf-8
|
|
import numpy as np
|
|
from scipy import io
|
|
|
|
from BasicCalculaters import calculatervector,myvectorizationNorm,dist2linevectorization
|
|
from offsetNodelist import *
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
def drawVoxel_3(x,low=0.3):
|
|
r,b,g = 1-0.2*x,1-0.6*x,1-0.6*x
|
|
colors = np.stack([r,b,g],axis=-1)
|
|
fig = plt.figure()
|
|
|
|
ax = fig.gca(projection='3d')
|
|
|
|
ax.set_axis_off()
|
|
|
|
ax.voxels(x>=low,facecolors=colors)
|
|
plt.show()
|
|
|
|
def LatticeCellSDF(IF,rs,resolution):
|
|
resu = np.zeros([resolution + 1, resolution + 1, resolution + 1], dtype=float)
|
|
k = 128
|
|
for i in range(len(IF)):
|
|
tempM = IF[i] - rs[i]
|
|
resu = resu + np.exp(-k * tempM)
|
|
FF = np.log(resu) / k
|
|
return FF
|
|
|
|
def p2voxel(mtype, parameters, resolution = 30):
|
|
M = io.loadmat('./PYline3Data.mat')
|
|
lines = M['lines']-1
|
|
|
|
|
|
# resolution = 30
|
|
# r = 0.036
|
|
|
|
strutlist_idx = M['line3_valid'][mtype,:]-1
|
|
now_strutlist = []
|
|
|
|
for i in strutlist_idx:
|
|
now_strutlist.append(lines[i])
|
|
strutlist = np.array(now_strutlist, dtype=np.int32)
|
|
|
|
nodelist = get_nodelist(parameters)
|
|
rs = [parameters['R1'],parameters['R2'],parameters['R3']] * 64
|
|
VN_T,Edge = symm48(nodelist,strutlist)
|
|
|
|
|
|
h = 1.0 / resolution
|
|
x, y, z = np.mgrid[0:1 + h / 2:h, 0:1 + h / 2:h, 0:1 + h / 2:h]
|
|
qm = np.zeros([resolution + 1, resolution + 1, resolution + 1, 3], dtype=float)
|
|
qm[:, :, :, 0] = x
|
|
qm[:, :, :, 1] = y
|
|
qm[:, :, :, 2] = z
|
|
|
|
IF = []
|
|
# construct implicit function per truss
|
|
for sdf in range(Edge.shape[0]):
|
|
temp_ff = np.zeros([resolution + 1, resolution + 1, resolution + 1], dtype=float)
|
|
sp = VN_T[Edge[sdf, 0], :] # start point
|
|
ep = VN_T[Edge[sdf, 1], :] # end point
|
|
|
|
rim = calculatervector(sp, ep, qm)
|
|
sp_normm = myvectorizationNorm(np.subtract(qm, sp), 2)
|
|
ep_normm = myvectorizationNorm(np.subtract(qm, ep), 2)
|
|
distm = dist2linevectorization(sp, ep, qm)
|
|
|
|
Index = rim <= 0
|
|
temp_ff[Index] = sp_normm[Index]
|
|
|
|
Index = rim >= 1
|
|
temp_ff[Index] = ep_normm[Index]
|
|
|
|
Index = ((rim > 0) & (rim < 1))
|
|
temp_ff[Index] = distm[Index]
|
|
|
|
IF.append(temp_ff)
|
|
|
|
FF = LatticeCellSDF(IF,rs,resolution=resolution)
|
|
FF[FF>=0]=1
|
|
FF[FF<0]=0
|
|
|
|
# voxel = FF
|
|
voxel = np.array(FF, dtype=np.float32)
|
|
return voxel
|
|
|
|
def showPara(mtype):
|
|
M = io.loadmat('./PYline3Data.mat')
|
|
lines = M['lines']
|
|
|
|
strutlist_idx = M['line3_valid'][mtype, :] - 1
|
|
now_strutlist = []
|
|
|
|
for i in strutlist_idx:
|
|
now_strutlist.append(lines[i])
|
|
strutlist = np.array(now_strutlist, dtype=np.int32)
|
|
|
|
keyPoints = [1, 2, 3, 5, 6, 9, 11]
|
|
|
|
in_parameter_names = ['R1','R2','R3']
|
|
out_parameter_names = []
|
|
node_unique = np.unique(strutlist)
|
|
for node in node_unique:
|
|
if 4 < node < 11:
|
|
if node in keyPoints:
|
|
out_parameter_names.append('E' + str(node))
|
|
else:
|
|
in_parameter_names.append('E' + str(node))
|
|
# print('E' + str(node))
|
|
if 10 < node < 15:
|
|
if node in keyPoints:
|
|
out_parameter_names += ['F' + str(node) + '_1', 'F' + str(node) + '_2']
|
|
else:
|
|
in_parameter_names += ['F' + str(node) + '_1', 'F' + str(node) + '_2']
|
|
# print('F' + str(node) + '_1', 'F' + str(node) + '_2')
|
|
if node == 15:
|
|
# print('T1', 'T2', 'T3')
|
|
in_parameter_names += ['T1', 'T2', 'T3']
|
|
return in_parameter_names, out_parameter_names
|
|
|
|
|
|
def setPara(mtype, in_parameter_values, out_parameter_values):
|
|
### 参数范围0<E{num}<1, 0<F{num}_1+F{num}_2 < 1
|
|
# R表示半径
|
|
# 0<E{num}<1
|
|
# 0<F{num}_1+F{num}_2 < 1
|
|
# 0<T1+T2+T3<1
|
|
# 0<所有参数值<1
|
|
init_parameters = {
|
|
'R1':0.036, 'R2':0.036, 'R3':0.036,
|
|
'E5':0.1,'E6':0.1,'E7':0.1,'E8':0.1,'E9':0.1,'E10':0.1,
|
|
'F11_1':0.2,'F12_1':0.2,'F13_1':0.2,'F14_1':0.2,
|
|
'F11_2':0.2,'F12_2':0.2,'F13_2':0.2,'F14_2':0.2,
|
|
'T1':0.1,'T2':0.1,'T3':0.1
|
|
}
|
|
|
|
in_parameter_names, out_parameter_names = showPara(mtype)
|
|
for value, key in zip(in_parameter_values, in_parameter_names):
|
|
init_parameters[key] = value
|
|
for value, key in zip(out_parameter_values, out_parameter_names):
|
|
init_parameters[key] = value
|
|
return init_parameters
|
|
|
|
if __name__ == "__main__":
|
|
|
|
mtype = 387 - 1
|
|
|
|
in_parameter_names, out_parameter_names = showPara(mtype)
|
|
print('in_parameters', in_parameter_names)
|
|
print('out_parameters', out_parameter_names)
|
|
|
|
|
|
default = 0.123
|
|
in_parameter_values = [default]*len(in_parameter_names)
|
|
in_parameter_values[0] = 0.036
|
|
in_parameter_values[1] = 0.1
|
|
in_parameter_values[2] = 0.036
|
|
out_parameter_values = [default]*len(out_parameter_names)
|
|
parameters = setPara(mtype, in_parameter_values, out_parameter_values)
|
|
|
|
voxel = p2voxel(mtype, parameters, resolution=40)
|
|
|
|
# np.save('mircostruct', voxel)
|
|
drawVoxel_3(voxel)
|