-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec_structs.py
204 lines (139 loc) · 5.33 KB
/
vec_structs.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from math import floor, sqrt
from vec_ops import build_average_field
class Point(object):
"""
Represnts a 3 dimensional cartesian coordinate.
"""
def __init__(self, x, y, z):
self.x = float(x)
self.y = float(y)
self.z = float(z)
def __eq__(self, other):
return issubclass(type(other), Point) and self.x == other.x \
and self.y == other.y and self.z == other.z
def __ne__(self, other):
return not self == other
def __add__(self, other):
if not issubclass(type(other), Point):
raise ValueError
return Point(self.x + other.x, self.y + other.y, self.z + other.z)
def __str__(self):
return "<{}: x={}, y={}, z={}>".format(self.__class__.__name__,
self.x, self.y, self.z)
def is_zero(self):
return self.x == 0 and self.y == 0 and self.z == 0
def as_indices(self):
return int(self.x), int(self.y), int(self.z)
class Vector(Point):
"""
Represents a 3 dimensional cartesian vector.
"""
def scale(self, val):
self.x *= val
self.y *= val
self.z *= val
def length(self):
return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def normalize(self):
length = self.length()
if length != 0:
self.scale(1.0 / length)
class VectorField(object):
"""
Wraps a numpy array of shape (x, y, z, 3).
"""
def __init__(self, data):
if data.shape[3] != 3:
raise ValueError
self.field = data
self.shape = data.shape[0:3]
def __str__(self):
return "<{}: shape={}>".format(self.__class__.__name__, self.shape)
def contains_point(self, point):
return 0 <= point.x and point.x < self.shape[0] \
and 0 <= point.y and point.y < self.shape[1] \
and 0 <= point.z and point.z < self.shape[2]
def get_raw(self, x, y, z):
assert self.contains_point(Point(x, y, z))
return Vector(x, y, z)
def get(self, point):
"""
Retrieves an interpolated vector from the field.
"""
assert self.contains_point(point)
# indices in each dimension of the cell
i = int(floor(point.x))
j = int(floor(point.y))
k = int(floor(point.z))
i1 = i + 1 if i < self.shape[0] - 1 else i
j1 = j + 1 if j < self.shape[1] - 1 else j
k1 = k + 1 if k < self.shape[2] - 1 else k
# weights for each dimension
u = point.x - i
v = point.y - j
w = point.z - k
# trilinear interpolation on each vector component
vector = (u * v * w * self.field[i,j,k]) \
+ (u * v * (1.0 - w) * self.field[i,j,k1]) \
+ (u * (1.0 - v) * w * self.field[i,j1,k]) \
+ (u * (1.0 - v) * (1.0 - w) * self.field[i,j1,k1]) \
+ ((1.0 - u) * v * w * self.field[i1,j,k]) \
+ ((1.0 - u) * v * (1.0 - w) * self.field[i1,j,k1]) \
+ ((1.0 - u) * (1.0 - v) * w * self.field[i1,j1,k]) \
+ ((1.0 - u) * (1.0 - v) * (1.0 - w) * self.field[i1,j1,k1])
return Vector(*vector)
def _do_integration(self, seed, delta_t, steps, forwards):
result = []
direction = 1.0 if forwards else -1.0
point = seed
while steps > 0 and self.contains_point(point):
result.append(point)
offset = self.get(point)
if offset.is_zero():
break
offset.scale(direction * delta_t)
point += offset
steps -= 1
# Special indexing to remove the seed from the list
return result[1:] if forwards else result[:0:-1]
def make_streamline(self, seed, delta_t, steps, forwards=True,
backwards=True):
"""
Create a streamline in the field based on Euler integration.
Args:
seed: a Point to start the streamline.
delta_t: the time step between points.
steps: the number of integration steps to perform in
either direction.
forwards: whether or not to generate the stream line in the
forwards direction.
backwards: whether or not to generate the stream line in the
backwards direction.
Returns:
A list of Points that descibe the streamline from back to front.
"""
assert self.contains_point(seed)
assert delta_t > 0 and steps > 0
assert forwards or backwards
front = self._do_integration(seed, delta_t, steps, True) \
if forwards else []
back = self._do_integration(seed, delta_t, steps, False) \
if backwards else []
return back + [seed] + front
@staticmethod
def build_average_field(fields):
assert len(fields) > 0
result = np.zeros(*(fields[0].shape + [3]))
for field in fields:
result += field
result /= 1.0 * len(fields)
return result
class VectorEnsemble(object):
def __init__(self, fields):
self.fields = fields
self.average_field = VectorField.build_average_field(fields)
self.shape = fields[0].shape
self.member_count = len(fields)
def __str__(self):
return "<{}: shape={}, member_count: {}>".format(
self.__class__.__name__, self.shape, self.member_count)