Created
March 24, 2015 19:42
-
-
Save clynamen/cac5b6e9ba5f4f3cd7aa to your computer and use it in GitHub Desktop.
matplotlib-trial2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# In[146]: | |
get_ipython().magic('matplotlib inline') | |
from pyrr import Quaternion, Matrix44, Vector3, Vector4 | |
import math | |
from math import sin, cos | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
from matplotlib.patches import FancyArrowPatch | |
from mpl_toolkits.mplot3d import proj3d | |
π=math.pi | |
# In[106]: | |
class Arrow3D(FancyArrowPatch): | |
def __init__(self, xs, ys, zs, *args, **kwargs): | |
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs) | |
self._verts3d = xs, ys, zs | |
def draw(self, renderer): | |
xs3d, ys3d, zs3d = self._verts3d | |
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M) | |
self.set_positions((xs[0],ys[0]),(xs[1],ys[1])) | |
FancyArrowPatch.draw(self, renderer) | |
class Vector3D(Arrow3D): | |
def __init__(self, xs, ys, zs, *args, **kwargs): | |
defaults={"arrowstyle": "-|>", "mutation_scale": 20, "lw": 2} | |
defaults.update(kwargs) | |
Arrow3D.__init__(self, [0, xs], [0, ys], [0,zs], *args, **defaults) | |
# In[77]: | |
def cylinder(radius=1, height=1, faces=6): | |
# vertices | |
phi_delta=2*π/faces | |
x = np.cos(phi_delta*np.arange(0, faces)) | |
y = np.sin(phi_delta*np.arange(0, faces)) | |
x = np.tile(x, 2) | |
y = np.tile(y, 2) | |
z = np.tile([-height/2], faces) | |
z=np.append(z, np.tile([height/2], faces)) | |
# indices | |
indices=np.array([0, 1, faces, 1, faces+1, faces]) | |
indices=np.tile(indices, faces-1) | |
indices=np.add(indices, np.repeat(np.arange(0, faces-1), 6)) | |
indices=np.append(indices, [faces-1, faces, faces*2-1, 0, faces, faces-1]) | |
indices=np.reshape(indices, (-1, 3)) | |
return x, y, z, indices | |
# In[213]: | |
x, y, z, triangles = cylinder(faces=20) | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection='3d') | |
ax.scatter3D(x, y, z) | |
k=ax.plot_trisurf(x, y, z, triangles=triangles) | |
k.set_alpha(0.4) | |
p=Quaternion([0, 0 , 1, 0]) | |
q=Quaternion() | |
q=q*Quaternion.from_x_rotation(π/4) | |
q=q*Quaternion.from_y_rotation(0*π/4) | |
q=q*Quaternion.from_z_rotation(0*-π) | |
pf=q*p*(q.inverse) | |
toVec3 = lambda q: Vector3([q[0], q[1], q[2]]) | |
M=Matrix44.from_quaternion(q) | |
quatToVec4 = lambda q: Vector4([q[0], q[1], q[2], 1]) | |
ps = M*quatToVec4(p) | |
pa = Vector3D(*toVec3(p), color="y") | |
pfa = Vector3D(*toVec3(pf), color="g") | |
psa = Vector3D(*toVec3(pf), color="r", lw=0.1) | |
tmat = np.matrix(M) | |
vm = np.vstack([x, y, z, np.ones(len(x))]) | |
rm = tmat*vm | |
[xf, yf, zf, wf] = np.vsplit(rm, 4) | |
trans_arr = lambda x: np.array(x).reshape(-1) | |
xf=trans_arr(xf) | |
yf=trans_arr(yf) | |
zf=trans_arr(zf) | |
wf=trans_arr(wf) | |
xf=np.divide(xf, wf) | |
yf=np.divide(yf, wf) | |
zf=np.divide(zf, wf) | |
ax.plot_trisurf(xf, yf, zf, triangles=triangles, color="r") | |
ax.add_artist(pa) | |
ax.add_artist(pfa) | |
ax.add_artist(psa) | |
plt.show() | |
# In[29]: | |
p = Quaternion( [0, 0, 0, 1]) | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment