-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamlines.py
More file actions
executable file
·277 lines (209 loc) · 9.69 KB
/
Copy pathStreamlines.py
File metadata and controls
executable file
·277 lines (209 loc) · 9.69 KB
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
"""
Generate a set of streamlines on the surface of a mesh corresponding to a vector
field defined in the tangent space of that mesh
"""
import random, cmath
from math import cos, sin, pi
from Utilities import *
def generateStreamlines(mesh, nLines, lineLength, vectorFieldName,
nSym = 1, countMax=100,
definedOn='vertex', isTangentVector=False,
includeNormals=False):
"""
Return a collection of lines that are streamlines for the vector field.
The returned object is a list, where each element in that list is a list
of positions
- vectorFieldName: String giving the name of the vector field attribute to use.
Should be vectors in R3 (will be normalized).
- includeNormals: if True, also returns a list of normal vectors to go with each point in each line
each element of the set is now a (line, normals) tuple of lists
"""
print("\nTracing " + str(nLines) + " streamlines on the surface of the mesh. Please hold...")
print 'is tangent vector = ',isTangentVector
# Bookkeeping
valueCache = prepareVectorField(mesh,
vectorFieldName,
definedOn,
isTangentVector=isTangentVector,
nSym=nSym)
lines = []
# Make sure every mesh has at least one line passing through it before we
# start repeating
emptyFaces = set(mesh.faces)
for i in range(nLines):
# If every face has 1, start puting 2 in every face (etc...)
if len(emptyFaces) == 0:
emptyFaces = set(mesh.faces)
startingFace = random.sample(emptyFaces, 1)[0]
# For now, the starting point is just the barycenter of the face
startingPoint = startingFace.center
# Traces this line
if includeNormals:
line, normals, facesUsed = traceStreamline(startingFace, startingPoint,
lineLength, valueCache,
countMax=countMax,
includeNormals=True)
lines.append((line, normals))
else:
line, facesUsed = traceStreamline(startingFace, startingPoint,
lineLength, valueCache,
countMax=countMax,
includeNormals=False)
lines.append(line)
# Remove all faces that were used for this line
emptyFaces -= facesUsed
print(" ...done tracing streamlines. Please come again.")
return lines
def prepareVectorField(mesh, vectorFieldName, definedOn, isTangentVector=False, nSym=1):
"""
Make sure we have a good vector field defined on faces to compute streamlines.
Post: Each face will have an attribute _streamVec which is the
unit-norm constant vector field within that face
"""
if definedOn == 'vertex':
for face in mesh.faces:
if isTangentVector:
# Extend a vector field defined at vertices to the faces
# First, LC-transport all of the vertex fields to the first to get a uniform representation
firstVert = None
for vertex in face.adjacentVerts():
if firstVert is None:
firstVert = vertex
centralVal = cmath.exp(1.0j * getattr(vertex, vectorFieldName) * nSym)
else:
he = vertex.halfedgeTo(firstVert)
centralVal += cmath.exp(1.0j * (getattr(vertex, vectorFieldName) + he.transportAngle)*nSym)
centralAngle = cmath.phase(centralVal**(1.0/nSym))
meanVec = firstVert.tangentAngleInR3(centralAngle)
face._streamVec = normalized(face.projectToTangentSpace(meanVec))
else:
if nSym > 1:
raise ValueError("ERROR: Symmetric vector fields only supported as tangent angles")
vecs = [normalized(getattr(vert, vectorFieldName)) for vert in face.adjacentVerts()]
meanVec = sum(vecs)
face._streamVec = normalized(face.projectToTangentSpace(meanVec))
elif definedOn == 'face':
if isTangentVector:
raise ValueError("ERROR Don't know how to process tangent vectors on faces")
for face in mesh.faces:
face._streamVec = normalized(face.projectToTangentSpace(getattr(face, vectorFieldName)))
else:
raise ValueError("Illegal definedOn setting: " + str(definedOn))
# Pre-compute some values that we will be using repeatedly
delTheta = 2.0*pi / nSym
rotMat = np.array([[cos(delTheta), -sin(delTheta)],[sin(delTheta), cos(delTheta)]])
valueCache = {}
for face in mesh.faces:
xDir = face.anyHalfEdge.vector
yDir = cross(xDir, face.normal)
v0 = face.anyHalfEdge.vertex.position
# Generate a vector for each direction in a symmetric field
uVecFirst = np.array(( dot(face._streamVec, xDir) , dot(face._streamVec, yDir) ))
uVecThis = uVecFirst
uVecs = []
for i in range(nSym):
# Project in to 3D
uVec3 = uVecThis[0] * xDir + uVecThis[1] * yDir
# Save
uVecs.append((uVecThis.copy(), uVec3))
# Rotate for the next direction
uVecThis = rotMat.dot(uVecThis)
valueCache[face] = (xDir, yDir, v0, uVecs)
for he in face.adjacentHalfEdges():
edgePoint3D = he.vertex.position - v0
edgePoint = np.array(( dot(edgePoint3D, xDir), dot(edgePoint3D, yDir) ))
edgeVec3D = -he.vector
edgeVec = np.array(( dot(edgeVec3D, xDir), dot(edgeVec3D, yDir) ))
valueCache[(face,he)] = (edgePoint, edgeVec)
return valueCache
def traceStreamline(startingFace, startingPoint, lineLength, valueCache, countMax = 100, includeNormals=False):
"""
Traces a single streamline through the mesh, returning the line as a list
of points.
"""
line = [startingPoint]
if(includeNormals):
normals = [startingFace.normal]
facesUsed = set()
length = 0.0
currFace = startingFace
currPoint = startingPoint
currV = None
while (length < lineLength) and (currFace is not None):
facesUsed.add(currFace)
# Trace out to the next point
nextFace, nextPoint = traceStreamlineThroughFace(currFace, currPoint, currV, valueCache)
# Measure the velocity and length
currV = nextPoint - currPoint
length += norm(currV)
# Save the new point and continue
line.append(nextPoint)
if includeNormals:
if nextFace is None:
normals.append(currFace.normal)
else:
normals.append(nextFace.normal)
currFace = nextFace
currPoint = nextPoint
# Catch infinte loops that might happen for numerical reasons
if(len(line) > countMax):
break
if includeNormals:
return line, normals, facesUsed
else:
return line, facesUsed
def traceStreamlineThroughFace(startFace, startPoint, currV, valueCache):
"""
Trace a point through a triangle, returning (newFace, newPoint)
If the stream goes off a boundary, return (None, newPoint)
- currV is the current "velocity" of the line, in 3D. Used to choose which
direction to follow in symmetric fields
Pre: startPoint is strictly inside startFace
Post: newPoint is strictly inside newFace (if not boundary)
Assumes that the vector field is a constant within the triangle,
"""
## Raycast to decide which of the faces of the triangle we pass through
uMin = float('inf')
xDir, yDir, v0, uVecsR2R3 = valueCache[startFace]
startPointLocal = startPoint - v0
startPoint2D = np.array(( dot(startPointLocal, xDir), dot(startPointLocal, yDir) ))
# For symmetric fields, choose the rotation direction which is closest to
# current "velocity" of the streamline
if currV is None:
uVec, uVecR3 = random.sample(uVecsR2R3,1)[0]
else:
currDot = -float('inf')
uVec = None
for uR2,uR3 in uVecsR2R3:
if dot(uR3, currV) > currDot:
currDot = dot(uR3, currV)
uVec = uR2
uVecR3 = uR3
for he in startFace.adjacentHalfEdges():
edgePoint, edgeVec = valueCache[(startFace,he)]
# Line/plane intersection
u = cross2D(startPoint2D - edgePoint, edgeVec) / cross2D(edgeVec, uVec)
# Check if this is the closest
if u > 0 and u < uMin:
uMin = u
acrossHe = he
t = cross2D(startPoint2D - edgePoint, uVec) / cross2D(edgeVec, uVec)
t = clamp(t, 0.005, 0.995)
# TODO sometimes things can go wrong from numerical errors... just give up if that happens
if uMin == float('inf'):
return None, startPoint
# Compute the new point. Extend the vector just a little so the next numerical problem is well-posed
# TODO this could be a bug for exceptionally skinny triangles
newPoint = acrossHe.vertex.position - t * acrossHe.vector + (uMin * 0.00001) * uVecR3
if acrossHe.isBoundary:
return None, newPoint
else:
newFace = acrossHe.twin.face
return newFace, newPoint
def cross2D(v1, v2):
return v1[0]*v2[1] - v1[1]*v2[0]
def constainToFace(point, face):
"""
Given a point which is supposed to lie inside a
"""
pass