forked from 541435721/myVTKPythonLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputePrincipalDirections.py
184 lines (157 loc) · 6.41 KB
/
computePrincipalDirections.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
#coding=utf8
########################################################################
### ###
### Created by Martin Genet, 2012-2016 ###
### ###
### University of California at San Francisco (UCSF), USA ###
### Swiss Federal Institute of Technology (ETH), Zurich, Switzerland ###
### École Polytechnique, Palaiseau, France ###
### ###
########################################################################
import math
import numpy
import myVTKPythonLibrary as myVTK
from mat_vec_tools import *
########################################################################
def computePrincipalDirections(
field,
field_storage="vec",
orient=0,
farray_eRR=None,
farray_eCC=None,
farray_eLL=None,
verbose=1):
myVTK.myPrint(verbose, "*** computePrincipalDirections ***")
if (field_storage == "vec"):
assert (field.GetNumberOfComponents() == 6), "Wrong numpber of components ("+str(field.GetNumberOfComponents())+"). Aborting."
elif (field_storage == "Cmat"):
assert (field.GetNumberOfComponents() == 9), "Wrong numpber of components ("+str(field.GetNumberOfComponents())+"). Aborting."
elif (field_storage == "Fmat"):
assert (field.GetNumberOfComponents() == 9), "Wrong numpber of components ("+str(field.GetNumberOfComponents())+"). Aborting."
else:
assert (0), "Wrong storage (field_storage="+str(field_storage)+"). Aborting."
n_tuples = field.GetNumberOfTuples()
farray_Lmin = myVTK.createFloatArray('Lmin', 1, n_tuples)
farray_Lmid = myVTK.createFloatArray('Lmid', 1, n_tuples)
farray_Lmax = myVTK.createFloatArray('Lmax', 1, n_tuples)
farray_Vmin = myVTK.createFloatArray('Vmin', 3, n_tuples)
farray_Vmid = myVTK.createFloatArray('Vmid', 3, n_tuples)
farray_Vmax = myVTK.createFloatArray('Vmax', 3, n_tuples)
mat = numpy.empty((3,3))
if (field_storage == "vec"):
vec = numpy.empty(6)
elif (field_storage == "Cmat"):
vec = numpy.empty(9)
elif (field_storage == "Fmat"):
vec = numpy.empty(9)
if (orient):
eCC = numpy.empty(3)
eLL = numpy.empty(3)
for k_tuple in xrange(n_tuples):
#print "k_tuple: "+str(k_tuple)
field.GetTuple(k_tuple, vec)
if (field_storage == "vec"):
vec_col6_to_mat_sym33(vec, mat)
elif (field_storage == "Cmat"):
cvec9_to_mat33(vec, mat)
elif (field_storage == "Fmat"):
fvec9_to_mat33(vec, mat)
if (numpy.linalg.norm(mat) > 1e-6):
#if (verbose): print + 'k_tuple =', k_tuple
vals, vecs = numpy.linalg.eig(mat)
#if (verbose): print + 'vals =', vals
#if (verbose): print + 'vecs =', vecs
#if (verbose): print + 'det =', numpy.linalg.det(vecs)
idx = vals.argsort()
vals = vals[idx]
vecs = vecs[:,idx]
#if (verbose): print + 'vals =', vals
#if (verbose): print + 'vecs =', vecs
#if (verbose): print + 'det =', numpy.linalg.det(vecs)
mat_Lmin = vals[0]
mat_Lmid = vals[1]
mat_Lmax = vals[2]
mat_Vmax = vecs[:,2]
mat_Vmid = vecs[:,1]
if (orient):
farray_eCC.GetTuple(k_tuple, eCC)
farray_eLL.GetTuple(k_tuple, eLL)
mat_Vmax = math.copysign(1, numpy.dot(mat_Vmax, eCC)) * mat_Vmax
mat_Vmid = math.copysign(1, numpy.dot(mat_Vmid, eLL)) * mat_Vmid
mat_Vmin = numpy.cross(mat_Vmax, mat_Vmid)
else:
mat_Lmin = 0.
mat_Lmid = 0.
mat_Lmax = 0.
mat_Vmin = [0.]*3
mat_Vmid = [0.]*3
mat_Vmax = [0.]*3
farray_Lmin.SetTuple1(k_tuple, mat_Lmin)
farray_Lmid.SetTuple1(k_tuple, mat_Lmid)
farray_Lmax.SetTuple1(k_tuple, mat_Lmax)
farray_Vmin.SetTuple(k_tuple, mat_Vmin)
farray_Vmid.SetTuple(k_tuple, mat_Vmid)
farray_Vmax.SetTuple(k_tuple, mat_Vmax)
return (farray_Lmin,
farray_Lmid,
farray_Lmax,
farray_Vmin,
farray_Vmid,
farray_Vmax)
########################################################################
def addPrincipalDirections(
ugrid,
field_name,
field_support="cell",
field_storage="vec",
orient=0,
verbose=1):
myVTK.myPrint(verbose, "*** addPrincipalDirections ***")
assert (field_support in ["point", "cell"]), "\"field_support\" must be \"point\" or \"cell\". Aborting."
assert (field_storage in ["vec", "Cmat", "Fmat"]), "\"field_storage\" must be \"vec\", \"Cmat\" or \"Fmat\". Aborting."
if (field_support == "cell" ): ugrid_data = ugrid.GetCellData()
elif (field_support == "point"): ugrid_data = ugrid.GetPointData()
field = ugrid_data.GetArray(field_name)
if (orient):
(farray_Lmin,
farray_Lmid,
farray_Lmax,
farray_Vmin,
farray_Vmid,
farray_Vmax) = computePrincipalDirections(
field=field,
field_storage=field_storage,
orient=orient,
farray_eRR=ugrid_data.GetArray("eRR"),
farray_eCC=ugrid_data.GetArray("eCC"),
farray_eLL=ugrid_data.GetArray("eLL"),
verbose=verbose-1)
else:
(farray_Lmin,
farray_Lmid,
farray_Lmax,
farray_Vmin,
farray_Vmid,
farray_Vmax) = computePrincipalDirections(
field=field,
field_storage=field_storage,
orient=orient,
verbose=verbose-1)
farray_Lmin.SetName(field_name+"_Lmin")
farray_Lmid.SetName(field_name+"_Lmid")
farray_Lmax.SetName(field_name+"_Lmax")
farray_Vmin.SetName(field_name+"_Vmin")
farray_Vmid.SetName(field_name+"_Vmid")
farray_Vmax.SetName(field_name+"_Vmax")
ugrid_data.AddArray(farray_Lmin)
ugrid_data.AddArray(farray_Lmid)
ugrid_data.AddArray(farray_Lmax)
ugrid_data.AddArray(farray_Vmin)
ugrid_data.AddArray(farray_Vmid)
ugrid_data.AddArray(farray_Vmax)
return (farray_Lmin,
farray_Lmid,
farray_Lmax,
farray_Vmin,
farray_Vmid,
farray_Vmax)