Skip to content

Commit

Permalink
Add flip book
Browse files Browse the repository at this point in the history
  • Loading branch information
valentjn committed Apr 1, 2018
1 parent add2ea4 commit 80f4e33
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
88 changes: 88 additions & 0 deletions gfx/py/flipBookBSpline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/python3
# number of output figures = 25

import multiprocessing

import matplotlib.patches
import numpy as np

import helper.basis
from helper.figure import Figure

def drawImage(imageNumber):
fig = Figure.create(figsize=(5, 5 * aspect), scale=0.5)
ax = fig.gca()

xUnits = imageNumber * xUnitsPerImage
if startsInXUnits[pMin] < xUnits:
p = max([p for p in range(pMin, pMax + 1) if startsInXUnits[p] < xUnits])
else:
p = pMin
xUnits -= startsInXUnits[p]

for pCur in range(pMax, -1, -1):
if pCur == p:
xx = np.linspace(min(max(xUnits, 0), pCur + 1), pCur + 1, 200)
color = colorLight
else:
xx = np.linspace(0, pCur + 1, 200)
color = (colorDark if pCur < p else colorLight)
b = helper.basis.CardinalBSpline(pCur)
yy = b.evaluate(xx)
if pCur == 0: yy[-1] = 1
ax.plot(xx, yy, "-", color=color, clip_on=False)

b = helper.basis.CardinalBSpline(p)
xx = np.linspace(0, min(max(xUnits, 0), p + 1), 200)
yy = b.evaluate(xx)
ax.plot(xx, yy, "-", color=colorDark, clip_on=False)

if (0 < xUnits < p + 1) and (p > 0):
bPrev = helper.basis.CardinalBSpline(p - 1)
xx = np.linspace(max(xUnits - 1, 0), xUnits, 1000)
yy = bPrev.evaluate(xx)
xxyy = np.column_stack((np.hstack((xx, xx[::-1])),
np.hstack((np.zeros_like(yy), yy[::-1]))))
ax.add_patch(matplotlib.patches.Polygon(
xxyy, ec="none", fc=colorLight, alpha=0.5, clip_on=False))

x = np.array([xUnits])
y = b.evaluate(x)
ax.plot(x, y, "o", color=colorDark, markersize=3, clip_on=False)

ax.set_xlim(*xLim)
ax.set_ylim(*yLim)
ax.set_aspect("equal")
ax.set_axis_off()

fig.save(graphicsNumber=imageNumber+1, crop=False,
tightLayout={"pad" : 0, "h_pad" : 0, "w_pad" : 0})



numberOfImages = 25
pauseStartInXUnits = 0.2
pauseBetweenInXUnits = 0.2
pauseEndInXUnits = 0.2
pMin = 1
pMax = 3
colorBase = Figure.COLORS["anthrazit"]
colorDarkBrightness = 0.3
colorLightBrightness = 0.6

colorDark = [x + colorDarkBrightness * (1 - x) for x in colorBase]
colorLight = [x + colorLightBrightness * (1 - x) for x in colorBase]
startsInXUnits = {pMin : pauseStartInXUnits}

for p in range(pMin + 1, pMax + 1):
startsInXUnits[p] = startsInXUnits[p - 1] + p + pauseBetweenInXUnits

lengthInXUnits = startsInXUnits[pMax] + (pMax + 1) + pauseEndInXUnits
xUnitsPerImage = lengthInXUnits / (numberOfImages - 1)

xLim = [0, pMax + 1]
yLim = [0, 1]
aspect = (yLim[1] - yLim[0]) / (xLim[1] - xLim[0])

with multiprocessing.Pool() as pool:
pool.map(drawImage, range(numberOfImages))
109 changes: 109 additions & 0 deletions gfx/py/flipBookSG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/python3
# number of output figures = 25

import multiprocessing

import numpy as np
import scipy.linalg

from helper.figure import Figure
import helper.grid

def createRotationMatrix(axis, angle):
return scipy.linalg.expm(np.cross(
np.eye(3), axis / scipy.linalg.norm(axis) * angle * np.pi / 180))

def rotate(matrix, x, y, z):
rotatedVectors = np.dot(matrix, np.row_stack((x, y, z)) - 0.5) + 0.5
return rotatedVectors[0,:], rotatedVectors[1,:], rotatedVectors[2,:]

def axisEqual3D(ax):
extents = np.array([ax.get_xlim(), ax.get_ylim(), ax.get_zlim()])
size = extents[:,1] - extents[:,0]
centers = np.mean(extents, axis=1)
maxSizeHalf = max(abs(size)) / 2
ax.set_xlim(centers[0] - maxSizeHalf, centers[0] + maxSizeHalf)
ax.set_ylim(centers[1] - maxSizeHalf, centers[1] + maxSizeHalf)
ax.set_zlim(centers[2] - maxSizeHalf, centers[2] + maxSizeHalf)

def drawImage(imageNumber):
angle = startAngle + numberOfRevolutions * 360 * imageNumber / numberOfImages

fig = Figure.create(figsize=(1, 1.2), scale=1.5)
ax = fig.add_subplot(111, projection="3d")

grid = helper.grid.RegularSparseBoundary(n, d, b)
X, _, _ = grid.generate()

prerotationMatrix = createRotationMatrix(prerotationAxis, prorotationAngle)
viewRotationMatrix1 = createRotationMatrix([1, 0, 0], elevation)
viewRotationMatrix2 = createRotationMatrix([0, 0, 1], angle)
viewRotationMatrix = np.dot(viewRotationMatrix2, viewRotationMatrix1)
rotationMatrix = np.dot(viewRotationMatrix, prerotationMatrix)

xs = [[0, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0], [1, 1], [0, 0], [1, 1]]
ys = [[0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [0, 0], [0, 0], [1, 1], [1, 1]]
zs = [[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
lines = []
for x, y, z in zip(xs, ys, zs):
for i in range(len(x) - 1):
xx = np.linspace(x[i], x[i + 1], 10)
yy = np.linspace(y[i], y[i + 1], 10)
zz = np.linspace(z[i], z[i + 1], 10)
for j in range(xx.shape[0] - 1):
xCur, yCur, zCur = xx[j:j+2], yy[j:j+2], zz[j:j+2]
xCur, yCur, zCur = rotate(rotationMatrix, xCur, yCur, zCur)
brightness = getBrightness(xCur[0])
color = [x + brightness * (1 - x) for x in colorBase]
lines.append([xCur[0], xCur, yCur, zCur, color])

lines.sort(key=lambda x: x[0])
for _, xCur, yCur, zCur, color in lines:
ax.plot(xCur, yCur, zs=zCur, ls="-", marker="", color=color)

x, y, z = X[:,0], X[:,1], X[:,2]
x, y, z = rotate(rotationMatrix, x, y, z)
k = x.argsort()
x, y, z = x[k], y[k], z[k]
for xCur, yCur, zCur in zip(x, y, z):
markerSize = 2 + 1.0 * xCur**2
brightness = getBrightness(xCur)
color = [x + brightness * (1 - x) for x in colorBase]
ax.plot([xCur], [yCur], zs=[zCur], color=color, marker="o", ls="", ms=markerSize)

ax.set_xlim(*xLim)
ax.set_ylim(*yLim)
ax.set_zlim(*zLim)
axisEqual3D(ax)
ax.set_axis_off()

ax.view_init(0, 0)
#ax.view_init(elevation, angle)
fig.save(graphicsNumber=imageNumber+1, crop=False,
tightLayout={"pad" : 0, "h_pad" : 0, "w_pad" : 0})



numberOfImages = 25
numberOfRevolutions = 1.0
startAngle = 30
elevation = 20
prerotationAxis = [0, 1, 0]
prorotationAngle = 20
xLim = [-0.3, 1.3]
#yLim = [0, 1]
yLim = [-0.3, 1.3]
zLim = [-0.3, 1.3]
n = 4
d = 3
b = 1
colorBase = Figure.COLORS["anthrazit"]
colorDarkBrightness = 0.3
colorLightBrightness = 0.7

colorDark = [x + colorDarkBrightness * (1 - x) for x in colorBase]
getBrightness = (lambda x: ((x - xLim[0]) / (xLim[1] - xLim[0])) *
(colorDarkBrightness - colorLightBrightness) + colorLightBrightness)

with multiprocessing.Pool() as pool:
pool.map(drawImage, range(numberOfImages))
5 changes: 5 additions & 0 deletions tex/switches.tex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
\newtoggle{showGlossaryDefinitionsMode}
\togglefalse{showGlossaryDefinitionsMode}

% flipBookMode switch:
% add flip book images in corners of page
\newtoggle{flipBookMode}
\togglefalse{flipBookMode}

% partialCompile switches:
% only compile a single chapter to save compilation time
\newtoggle{partialCompileMode}
Expand Down

0 comments on commit 80f4e33

Please sign in to comment.