Skip to content

Instantly share code, notes, and snippets.

@hfiller
Last active November 19, 2020 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hfiller/9cfcb3ff6fa0cf7166767092bbd230c8 to your computer and use it in GitHub Desktop.
Save hfiller/9cfcb3ff6fa0cf7166767092bbd230c8 to your computer and use it in GitHub Desktop.
Basic program to get 2d Projection
import numpy as np
import matplotlib.pyplot as plt
import json
with open("/path/to/state.txt") as f:
bookmarks = json.load(f)
bookmark = bookmarks[0] # We'll look at the first bookmark
eye = np.array(bookmark["cameraDef"]["position"])
at = np.array(bookmark["cameraDef"]["target"])
up = np.array([0, 1, 0])
zaxis = at - eye
zaxis = zaxis / np.linalg.norm(zaxis)
xaxis = np.cross(zaxis, up)
xaxis = xaxis / np.linalg.norm(xaxis)
yaxis = np.cross(xaxis, zaxis)
zaxis = -zaxis
viewMatrix = np.matrix(
[
[xaxis[0], xaxis[1], xaxis[2], -xaxis.dot(eye)],
[yaxis[0], yaxis[1], yaxis[2], -yaxis.dot(eye)],
[zaxis[0], zaxis[1], zaxis[2], -zaxis.dot(eye)],
[0, 0, 0, 1],
]
)
x = []
y = []
z = []
for point in bookmark["projections"]:
cameraCoordinates = (
viewMatrix
* np.matrix([[point["pca-0"]], [point["pca-1"]], [point["pca-2"]], [1]])
).tolist()
x.append((cameraCoordinates[0][0] / cameraCoordinates[3][0]))
y.append((cameraCoordinates[1][0] / cameraCoordinates[3][0]))
z.append((cameraCoordinates[2][0] / cameraCoordinates[3][0]))
plt.style.use("seaborn-whitegrid")
plt.scatter(x, y, c=z, cmap="RdYlGn")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment