In [ ]:
import sys,os,imp,re
import math
import numpy as np
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
from mpl_toolkits.mplot3d import Axes3D
mpl.rcParams['figure.figsize']=(6.0,4.0) #(6.0,4.0)
mpl.rcParams['font.size']=10 #10
mpl.rcParams['savefig.dpi']=400 #72
mpl.rcParams['figure.subplot.bottom']=.1 #.125
plt.rc('font', family='serif')
plt.rc('text', usetex=True)
#inline Shit
%matplotlib inline
%config InlineBackend.figure_format='svg'
%config InlineBackend.rc = {'figure.facecolor': 'white', 'figure.subplot.bottom': 0.125, 'figure.edgecolor': 'white', 'savefig.dpi': 400, 'figure.figsize': (12.0, 8.0), 'font.size': 10}
#GUi shit
%matplotlib tk
mpl.get_configdir()
In [ ]:
def plotCube(ax,minP = np.array([-1.0,-1.0,-1.0]), maxP=np.array([1.0,1.0,1.0]),
trans= np.array([0.0,0.0,0.0]),rotationMatrix=np.diag([1,1,1])):
from itertools import product, combinations
r = [-1, 1]
centerPos = (maxP + minP)/2.0;
#print(centerPos)
extent = (maxP - minP)/2.0;
points = np.array([(-1, -1, -1),
(-1, -1, 1),
(-1, 1, -1),
(-1, 1, 1),
(1, -1, -1),
(1, -1, 1),
(1, 1, -1),
(1, 1, 1)]);
for s, e in combinations(points, 2):
if np.sum(np.abs(s-e)) == r[1]-r[0]: # no diagonal lines
p1 = np.array(s,dtype=float); p2 = np.array(e,dtype=float);
#scale points
p1*=extent; p2*=extent;
#rotate and translate points
p1 = rotationMatrix.dot(p1 + centerPos) + trans;
p2 = rotationMatrix.dot(p2+centerPos) + trans;
ax.plot3D(*zip(p1,p2), color="b")
def plotAxis(ax,centerPos,A_IK,plotAxisScale=1):
for i,c in zip([0,1,2],['r','g','b']):
I_eK_i = A_IK[:,i];
lines = list(zip(centerPos,plotAxisScale*I_eK_i+centerPos))
v = Arrow3D(*lines, mutation_scale=50, lw=1, arrowstyle="-|>", color=c);
ax.plot3D(*lines, color=c)
ax.add_artist(v);
def plotAxis2d(ax,centerPos,u,v,plotAxisScale=1):
x = np.vstack((centerPos,plotAxisScale*u+centerPos))
y = np.vstack((centerPos,plotAxisScale*v+centerPos))
ax.plot(x.T[0],x.T[1],'r',lw=2)
ax.plot(y.T[0],y.T[1],'b',lw=2)
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
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)
In [ ]:
def axisEqual3D(ax):
extents = np.array([getattr(ax, 'get_{}lim'.format(dim))() for dim in 'xyz'])
sz = extents[:,1] - extents[:,0]
centers = np.mean(extents, axis=1)
maxsize = max(abs(sz))
r = maxsize/2
for ctr, dim in zip(centers, 'xyz'):
getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)
In [ ]:
# get all files
def loadFiles(folderPath,filePathRegex):
files = [ os.path.join(folderPath,f) for f in os.listdir(folderPath) if os.path.isfile( os.path.join(folderPath,f) ) ]
filePaths=dict();
for f in files:
res = re.match(filePathRegex,f)
if(res):
fileNr = res.group(1);
filePaths[int(fileNr)] = f;
return filePaths;
In [ ]:
plt.close("all")
fig = plt.figure("MinAreaRectangleTest",(10,10))
ax = fig.gca();
plotAxis2d(ax,np.array([1,1]),np.array([1,0]),np.array([0,1]));
In [ ]:
plt.close("all")
files = loadFiles("./" , ".*ConvexHullTest(\d+)\.bin");
filesOut = loadFiles("./" , ".*ConvexHullTest(\d+)Out\.bin");
for i,f in files.items():
fOut = filesOut[i]; print(i,f,fOut)
fig = plt.figure("ConvexHullTest"+str(i),(10,10))
points = np.fromfile(f,dtype=np.float64); points=np.reshape(points,(-1,2))
hullP = np.fromfile(fOut,dtype=np.float64); hullP=np.reshape(hullP,(-1,2))
#hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')
plt.scatter(points.T[0],points.T[1],c='r')
if(len(points)<300):
nrRange = [(i,p[0],p[1]) for i,p in enumerate(points) ]
for x in nrRange:
plt.annotate('%s' % x[0], xy=x[1:3], textcoords='offset points') # <--
# if(len(hullP)<300):
# nrRange = [(i,p[0],p[1]) for i,p in enumerate(hullP) ]
# for x in nrRange:
# plt.annotate('%s' % x[0], xy=x[1:3], textcoords='offset points') # <--
In [ ]:
plt.close("all")
files = loadFiles("./" , ".*MinAreaRectangleTest(\d+)\.bin");
filesOut = loadFiles("./" , ".*MinAreaRectangleTest(\d+)Out\.bin");
for i,f in files.items():
if(i==9): continue # skip test 9, it is empty!
fOut = filesOut[i]; print(i,f,fOut)
fig = plt.figure("MinAreaRectangleTest"+str(i),(10,10))
ax = fig.gca();
points = np.fromfile(f,dtype=np.float64); points=np.reshape(points,(-1,2))
rectData = np.fromfile(fOut,dtype=np.float64); rectData=np.reshape(rectData,(-1,2))
rect = rectData[0:5,]
axis = rectData[5:,]
print(axis)
plt.plot(rect.T[0],rect.T[1],'r-', ms=20, markerfacecolor='None')
plotAxis2d(ax,rect[0],axis[0],axis[1]);
if(i!=12):
plt.scatter(points.T[0],points.T[1])
plt.axis('equal')
In [ ]:
plt.close("all")
In [ ]:
plt.close("all")
files = loadFiles("./" , ".*DiameterTest(\d+)\.bin");
filesOut = loadFiles("./" , ".*DiameterTest(\d+)Out\.txt");
filesOut2 = loadFiles("./" , ".*DiameterTest(\d+)Out2\.bin");
for i,f in files.items():
if(i==9): continue # skip test 9, it is empty!
fOut = filesOut[i]; fOut2 = filesOut2[i]; print(i,f,fOut,fOut2)
fig = plt.figure("DiameterTest"+str(i),(10,10))
points = np.fromfile(f,dtype=np.float64); points=np.reshape(points,(-1,3))
OOBB = np.atleast_2d(np.loadtxt(fOut));
sampled = np.fromfile(fOut2,dtype=np.float64); sampled=np.reshape(sampled,(-1,3))
K_min = OOBB[0,0:3]
K_max = OOBB[1,0:3]
A_IK = OOBB[2:,0:3]
center = np.zeros((3,));
#print(A_IK,K_min,K_max,center)
ax = Axes3D(fig)
ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
ax.scatter(sampled.T[0],sampled.T[1],sampled.T[2],c='r', marker='o', s=200)
plotCube(ax,K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(ax,center,A_IK,1)
plotAxis(ax,center,np.identity(3),0.5)
axisEqual3D(ax)
In [ ]:
plt.close("all")
In [ ]:
plt.close("all")
files = loadFiles("./" , ".*MVBBTest(\d+)\.bin");
filesOut = loadFiles("./" , ".*MVBBTest(\d+)Out\.txt");
for i,f in files.items():
if(i==9): continue # skip test 9, it is empty!
fOut = filesOut[i]; print(i,f,fOut);
fig = plt.figure("MVBBTest" + str(i),(10,10))
points = np.fromfile(f,dtype=np.float64); points=np.reshape(points,(-1,3))
OOBB = np.atleast_2d(np.loadtxt(fOut));
K_min = OOBB[0,0:3]
K_max = OOBB[1,0:3]
A_IK = OOBB[2:,0:3]
center = np.zeros((3,));
print(A_IK,K_min,K_max,center)
ax = Axes3D(fig)
if(len(points) < 10000):
ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
plotCube(ax,K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(ax,center,A_IK,1)
plotAxis(ax,center,np.identity(3),0.5)
axisEqual3D(ax)
In [ ]: