python: quick example of geometric transform

This commit is contained in:
Chris Hodapp 2021-08-05 22:20:31 -04:00
parent 8ca32a67dd
commit 895f1f86a7
2 changed files with 23 additions and 10 deletions

3
.gitignore vendored
View File

@ -9,3 +9,6 @@ pom.xml.asc
/.nrepl-port /.nrepl-port
*~ *~
*/__pycache__ */__pycache__
clojure/.rebel_readline_history
clojure/resources/public/js/*
clojure/target/*

View File

@ -40,6 +40,7 @@ class VectorField(object):
Returns: Returns:
(3,3) numpy array containing gradients at (x,y,z) (3,3) numpy array containing gradients at (x,y,z)
""" """
# TODO: Why am I not using automatic differentiation here?
p = self.eval(x, y, z) p = self.eval(x, y, z)
p_dx = self.eval(x+eps, y, z) p_dx = self.eval(x+eps, y, z)
p_dy = self.eval(x, y+eps, z) p_dy = self.eval(x, y+eps, z)
@ -69,9 +70,9 @@ class VectorField(object):
class SimplexPotential(VectorField): class SimplexPotential(VectorField):
"""Represents a potential function for a vector field.""" """Represents a potential function for a vector field."""
def __init__(self): def __init__(self):
self.x_spx = opensimplex.OpenSimplex(seed=0) self.x_spx = opensimplex.OpenSimplex(seed=1)
self.y_spx = opensimplex.OpenSimplex(seed=12345) self.y_spx = opensimplex.OpenSimplex(seed=12346)
self.z_spx = opensimplex.OpenSimplex(seed=45678) self.z_spx = opensimplex.OpenSimplex(seed=45679)
def eval(self, x: float, y: float, z: float) -> np.array: def eval(self, x: float, y: float, z: float) -> np.array:
y2 = y + 0.1*math.sin(1*x) + 0.1*math.sin(1.25*z) y2 = y + 0.1*math.sin(1*x) + 0.1*math.sin(1.25*z)
x2 = x x2 = x
@ -100,12 +101,12 @@ class TentacleWtf(VectorField):
class KindaTwist(VectorField): class KindaTwist(VectorField):
def eval(self, x: float, y: float, z: float) -> np.array: def eval(self, x: float, y: float, z: float) -> np.array:
f = 2.0 f = 0.0
x2 = x*math.cos(f*y) - z*math.sin(f*y) #x2 = x*math.cos(f*y) - z*math.sin(f*y)
z2 = x*math.sin(f*y) + z*math.cos(f*y) #z2 = x*math.sin(f*y) + z*math.cos(f*y)
y2 = 0 #y2 = 0
x3 = 0.5*z2 x3 = z
y3 = y2 y3 = y
z3 = 0 z3 = 0
f1 = np.array([ f1 = np.array([
x3, y3, z3, x3, y3, z3,
@ -114,10 +115,19 @@ class KindaTwist(VectorField):
def generate(grid): def generate(grid):
p = KindaTwist() p = KindaTwist()
#p = SimplexPotential()
grads = np.array([p.grad(*pt) for pt in grid]) grads = np.array([p.grad(*pt) for pt in grid])
curl = p.curl_3d(grads) curl = p.curl_3d(grads)
return curl return curl
def twist_xform(pts):
f = 0.3
x, y, z = pts[..., 0], pts[..., 1], pts[..., 2]
x2 = x*np.cos(f*y) - z*np.sin(f*y)
z2 = x*np.sin(f*y) + z*np.cos(f*y)
y2 = y
return np.stack((x2, y2, z2), axis=-1)
class Data(object): class Data(object):
def __init__(self, view): def __init__(self, view):
self.use_tubes = False self.use_tubes = False
@ -147,6 +157,7 @@ class Data(object):
p = p2 p = p2
points.append(p) points.append(p)
points = np.stack(points, axis=1) / s points = np.stack(points, axis=1) / s
points = twist_xform(points)
# points = (count*count, N, 3) where first dimension chooses which # points = (count*count, N, 3) where first dimension chooses which
# trajectory, and second dimension proceeds along time/iterations # trajectory, and second dimension proceeds along time/iterations
# of that trajectory. # of that trajectory.
@ -160,7 +171,6 @@ class Data(object):
t = 0 if ev is None else ev.elapsed t = 0 if ev is None else ev.elapsed
# Get velocity for current points: # Get velocity for current points:
curl = generate(self.points) curl = generate(self.points)
#
a1 = self.points a1 = self.points
a2 = self.points + curl*0.1*self.s a2 = self.points + curl*0.1*self.s
lines = np.hstack((a1, a2)).reshape(self.points.shape[0]*2, -1) / self.s lines = np.hstack((a1, a2)).reshape(self.points.shape[0]*2, -1) / self.s