In [1]:
import sys,os,imp
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


#this works apparently only for savefig stuff
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()

%load_ext autoreload
%autoreload 2

# Import General Packages from me
from Tools.Parsers import *
from Tools.BoundingBox import *
from Tools.Transformations import *


/home/zfmgpu/Desktop/Repository/SimulationFramework/SourceCode/Projects/SimulationFramework/Simulations/PythonScripts/Tools/Transformations/Transformations.py:1890: UserWarning: failed to import module _transformations
  warnings.warn("failed to import module %s" % name)

In [2]:
from scipy.spatial import ConvexHull
from mpl_toolkits.mplot3d import Axes3D

def rotMatrix(axis,theta):
    axis = axis/math.sqrt(np.dot(axis,axis))
    a = math.cos(theta/2)
    b,c,d = axis*math.sin(theta/2)
    return np.array([[a*a+b*b-c*c-d*d, 2*(b*c-a*d), 2*(b*d+a*c)],
                     [2*(b*c+a*d), a*a+c*c-b*b-d*d, 2*(c*d-a*b)],
                     [2*(b*d-a*c), 2*(c*d+a*b), a*a+d*d-b*b-c*c]]);


def plotCube(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(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);

            
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)  


def makeSkewSym3(a):
    m = np.zeros((3,3));
    m[0,0]=a[0];
    m[0,1]=a[1]; m[1,0]=a[1];
    m[0,2]=a[2]; m[2,0]=a[2];
    m[1,1]=a[3];
    m[1,2]=a[4]; m[2,1]=a[4];
    m[2,2]=a[5];
    return m;

def calculateBoundingBoxBinetTensor(pointsIn, withConvexHull=False):
    I_theta_G = np.zeros((6,1));
    I_r_G = np.zeros((3));

    
    points = pointsIn.copy();
    if(withConvexHull):
        from scipy.spatial import ConvexHull
        hull = ConvexHull(points.T)
        points = points[:,hull.vertices];
        print("Computed Convex Hull")
    
    nPoints = points.shape[1];
    
    for p in points.T:
#         print(type(p),p.ndim)
#         print(I_r_G.shape,p.shape)
        I_r_G += p;
        
        I_theta_G[0] += p[0]*p[0];
        I_theta_G[1] += p[0]*p[1];
        I_theta_G[2] += p[0]*p[2];
        I_theta_G[3] += p[1]*p[1];
        I_theta_G[4] += p[1]*p[2];
        I_theta_G[5] += p[2]*p[2];
    
    I_r_G /= nPoints;
    
    # move binet tensor to center of mass G [mass for every point m=1]
    I_theta_G[0] -= I_r_G[0]*I_r_G[0] * nPoints;
    I_theta_G[1] -= I_r_G[0]*I_r_G[1] * nPoints;
    I_theta_G[2] -= I_r_G[0]*I_r_G[2]* nPoints;
    I_theta_G[3] -= I_r_G[1]*I_r_G[1]* nPoints;
    I_theta_G[4] -= I_r_G[1]*I_r_G[2]* nPoints;
    I_theta_G[5] -= I_r_G[2]*I_r_G[2]* nPoints;
    
    print("Computed Binet Inertia Tensor")
    
    A = makeSkewSym3(I_theta_G);
    print("I_Theta_G: " , A);
    eigs = np.linalg.eigh(A);
    A_IK = eigs[1];
    print("Computed Binet Inertia Tensor, Eigenvector Decomposition")
    
    A_IK[:,0] /= np.linalg.norm(A_IK[:,0]);
    A_IK[:,1] /= np.linalg.norm(A_IK[:,1]);
    A_IK[:,2] /= np.linalg.norm(A_IK[:,2]);
    
    #find projected maximas
    minP = np.zeros((3));
    maxP = np.zeros((3));
    for p in points.T:
        x = A_IK.T.dot(p - I_r_G);
        maxP = np.maximum(maxP,x);
        minP = np.minimum(minP,x);
    
    print("Computed Bounding Box")
    print("A_IK: " ,A_IK)
    print("orthnormality: " ,A_IK.dot(A_IK.T))
    print("min: " ,minP)
    print("max: " ,maxP)
    return (I_r_G,A_IK,minP,maxP,points); # returns A_IK;

In [3]:
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 [8]:
fig = plt.figure(1,(10,10))
ax = Axes3D(fig)

np.random.seed(101)
# points = np.random.rand(3,6000)

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),
                     (1,1,1),
                     (0.01,0.5,0.1)],dtype=float).T;

ax.scatter(points[0],points[1],points[2])

# points = np.append(points,points*0.5,0)
# points = np.append(points,points*0.3,0)
# points = np.append(points,points*0.1,0)
# print(points)
# points = points.T
    

# version 1.0.x syntax:
#ax = fig.add_subplot(111, projection='3d')
# version 0.99.x syntax: (accepted by 1.0.x as well)

# R_KI= rotMatrix(np.array([1,0,0]),20/180.0*math.pi);
# R_KI = R_KI.dot(rotMatrix(np.array([0,0,1]),20/180.0*math.pi));
# trans = np.array([0.2,0,0])
# minP = np.array([-2,-1,-1]);
# maxP = np.array([2,1,1]);
# center = (maxP + minP)/2 + trans;
# plotCube(minP,maxP,trans,R_KI)
# plotAxis(center,R_KI,1)

#ax.scatter(points[0],points[1],points[2])

center, A_IK, minP, maxP, pointsOut = calculateBoundingBoxBinetTensor(points,False)

ax.scatter(pointsOut[0],pointsOut[1],pointsOut[2],color='r')
plotCube(minP,maxP,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
axisEqual3D(ax)


Computed Binet Inertia Tensor
I_Theta_G:  [[ 8.89809  0.8535   0.8899 ]
 [ 0.8535   9.025    0.885  ]
 [ 0.8899   0.885    8.889  ]]
Computed Binet Inertia Tensor, Eigenvector Decomposition
Computed Bounding Box
A_IK:  [[-0.62607086  0.53592697 -0.56640759]
 [-0.14086594 -0.79216347 -0.5938298 ]
 [ 0.7669368   0.29199199 -0.57144432]]
orthnormality:  [[  1.00000000e+00   0.00000000e+00   5.55111512e-17]
 [  0.00000000e+00   1.00000000e+00   1.66533454e-16]
 [  5.55111512e-17   1.66533454e-16   1.00000000e+00]]
min:  [-1.53387361 -1.58750565 -1.5225412 ]
max:  [ 1.53387361  1.65265921  1.94082222]
[ 0.          0.03257678  0.20914051]

In [10]:
fig = plt.figure(1,(10,10))
ax = Axes3D(fig)
v, t, n, f = ObjLoader.loadObj("./Tools/BoundingBox/TestFiles/bunny.obj");

v = v.T;
nP = 100000;
v = v[:,0:-1]
# v = np.random.rand(3,100)
# print(v.shape)
center, A_IK, minP, maxP, pointsOut = calculateBoundingBoxBinetTensor(v,True)
ax.scatter(pointsOut[0],pointsOut[1],pointsOut[2],color='r')
plotCube(minP,maxP,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)

# center, A_IK, minP, maxP, pointsOut = calculateBoundingBoxBinetTensor(v,False)
# ax.scatter(pointsOut[0],pointsOut[1],pointsOut[2],color='r')
# plotCube(minP,maxP,center,A_IK) # A_IK = R_KI (rotation from I to K)
# plotAxis(center,A_IK,1)

axisEqual3D(ax)


Computed Convex Hull
Computed Binet Inertia Tensor
I_Theta_G:  [[ 5.3127925  -2.68240367 -0.31951755]
 [-2.68240367  3.23163872 -0.1606258 ]
 [-0.31951755 -0.1606258   1.30440366]]
Computed Binet Inertia Tensor, Eigenvector Decomposition
Computed Bounding Box
A_IK:  [[ 0.38571766  0.41168697 -0.82567289]
 [ 0.52542195  0.63760905  0.56337064]
 [ 0.75838886 -0.65112866  0.0296277 ]]
orthnormality:  [[  1.00000000e+00  -2.22044605e-16  -1.21430643e-16]
 [ -2.22044605e-16   1.00000000e+00  -9.71445147e-17]
 [ -1.21430643e-16  -9.71445147e-17   1.00000000e+00]]
min:  [-0.08011379 -0.07543226 -0.07939604]
max:  [ 0.03596974  0.08618646  0.10171412]
[-0.02207202  0.0053771   0.01115904]

In [11]:
points = np.random.rand(30, 3)   # 30 random points in 2-D
hull = ConvexHull(points)
hull.vertices


Out[11]:
array([ 0,  1,  4,  7,  8, 10, 11, 12, 13, 14, 15, 17, 19, 20, 22, 26, 28,
       29], dtype=int32)

In [19]:
def parseNDArray(f, dt, deli=" ", comm='#'):
    #build structred types (dtype for numpy)
    return np.loadtxt(f,dtype=dt, delimiter=deli, comments=comm);


points = parseNDArray("../PointsSimulation.txt" , dt=float )

fig = plt.figure(1,(10,10))
ax = Axes3D(fig)

pointsT = points.T  #points = [[x,y,z],[x,y,z]]
#center, A_IK, minP, maxP, pointsOut = calculateBoundingBoxBinetTensor(points,True)
ax.scatter(pointsT[0],pointsT[1],pointsT[2],color='r')
hull = ConvexHull(points)

#print("Points:", points)
# Make Edge Set

edgeSet = set();    
for simplex in hull.simplices:
    edgeSet.add(CalculateMVBB.Edge( simplex[0], simplex[1] ));
    edgeSet.add(CalculateMVBB.Edge( simplex[1], simplex[2] ));
    edgeSet.add(CalculateMVBB.Edge( simplex[2], simplex[0] ));
    #print(simplex)

print("Edges: ", len(edgeSet), "instead of " , len(hull.simplices)*3)
# for e in edgeSet:
#     print(e[0],e[1]);
for simplex in hull.simplices:
    ax.plot(points[simplex,0], points[simplex,1], points[simplex,2], 'k-')


corner, v1, v2, v3, u,v,w = CalculateMVBB.findOBBEdge(list(edgeSet),points)

print(v1,v2,v3)


Edges:  480 instead of  480
[-1.94711545 -1.93490626  0.01770744] [  5.35885411e+00  -5.39265151e+00   1.83580192e-03] [-0.03194867 -0.03421722 -7.2520307 ]
/home/zfmgpu/Desktop/Repository/SimulationFramework/SourceCode/Projects/SimulationFramework/Simulations/PythonScripts/Tools/BoundingBox/CalculateMVBB.py:106: RuntimeWarning: invalid value encountered in true_divide
  w /= np.sqrt(np.sum(v**2))
/home/zfmgpu/Desktop/Repository/SimulationFramework/SourceCode/Projects/SimulationFramework/Simulations/PythonScripts/Tools/BoundingBox/CalculateMVBB.py:99: RuntimeWarning: invalid value encountered in true_divide
  p = d / v2

In [20]:
np.array([u,v,w])


Out[20]:
array([[ -4.60465732e+00,  -4.57578427e+00,   4.18756247e-02],
       [  4.57588652e+00,  -4.60474587e+00,   1.56757789e-03],
       [ -2.85985410e-02,  -3.06292131e-02,  -6.49158442e+00]])

In [51]:
3<float("inf")


Out[51]:
True

In [15]:
plt.close('all')
points = np.loadtxt("../ConvexHullTest1.txt");
hullP = np.loadtxt("../ConvexHullTest1Out.txt");

fig = plt.figure(1,(10,10))
hullP = np.vstack((hullP,hullP[0]))
plt.scatter(points.T[0],points.T[1])
plt.plot(hullP.T[0],hullP.T[1],'b-o',ms=20, markerfacecolor='None')


Out[15]:
[<matplotlib.lines.Line2D at 0x7fe42b342d68>]

In [6]:
points = np.loadtxt("../ConvexHullTest2.txt");
hullP = np.loadtxt("../ConvexHullTest2Out.txt");

fig = plt.figure(2,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o',ms=20, markerfacecolor='None')


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-6-37ecb56105c0> in <module>()
      1 points = np.loadtxt("../ConvexHullTest2.txt");
----> 2 hullP = np.loadtxt("../ConvexHullTestOut2.txt");
      3 
      4 fig = plt.figure(2,(10,10))
      5 plt.scatter(points.T[0],points.T[1])

/opt/python3.4env/lib/python3.4/site-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
    737                 fh = iter(open(fname, 'U'))
    738             else:
--> 739                 fh = iter(open(fname))
    740         else:
    741             fh = iter(fname)

FileNotFoundError: [Errno 2] No such file or directory: '../ConvexHullTestOut2.txt'

In [16]:
points = np.loadtxt("../ConvexHullTest3.txt");
hullP = np.loadtxt("../ConvexHullTest3Out.txt");

fig = plt.figure(3,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o',ms=20, markerfacecolor='None')


Out[16]:
[<matplotlib.lines.Line2D at 0x7fe42397d9e8>]

In [17]:
points = np.loadtxt("../ConvexHullTest4.txt");
hullP = np.loadtxt("../ConvexHullTest4Out.txt");

fig = plt.figure(4,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o',ms=20, markerfacecolor='None')


Out[17]:
[<matplotlib.lines.Line2D at 0x7fe4239c3da0>]

In [18]:
points = np.loadtxt("../ConvexHullTest5.txt");
hullP = np.loadtxt("../ConvexHullTest5Out.txt");

fig = plt.figure(5,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-18-9c3cb51ae294> in <module>()
----> 1 points = np.loadtxt("../ConvexHullTest5.txt");
      2 hullP = np.loadtxt("../ConvexHullTest5Out.txt");
      3 
      4 fig = plt.figure(5,(10,10))
      5 plt.scatter(points.T[0],points.T[1])

/opt/python3.4env/lib/python3.4/site-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
    737                 fh = iter(open(fname, 'U'))
    738             else:
--> 739                 fh = iter(open(fname))
    740         else:
    741             fh = iter(fname)

FileNotFoundError: [Errno 2] No such file or directory: '../ConvexHullTest5.txt'

In [19]:
points = np.atleast_2d(np.loadtxt("../ConvexHullTest6.txt"));
hullP = np.atleast_2d(np.loadtxt("../ConvexHullTest6Out.txt"));

fig = plt.figure(6,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')


Out[19]:
[<matplotlib.lines.Line2D at 0x7fe4232e5f98>]

In [20]:
points = np.atleast_2d(np.loadtxt("../ConvexHullTest7.txt"));
hullP = np.atleast_2d(np.loadtxt("../ConvexHullTest7Out.txt"));

fig = plt.figure(7,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')


Out[20]:
[<matplotlib.lines.Line2D at 0x7fe4200c7d68>]

In [21]:
plt.close("all")
points = np.atleast_2d(np.loadtxt("../ConvexHullTest8.txt"));
hullP = np.atleast_2d(np.loadtxt("../ConvexHullTest8Out.txt"));

fig = plt.figure(8,(10,10))
plt.scatter(points.T[0],points.T[1])
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') # <--
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')


/opt/python3.4env/lib/python3.4/site-packages/matplotlib/text.py:1788: UserWarning: You have used the `textcoords` kwarg, but not the `xytext` kwarg.  This can lead to surprising results.
  warnings.warn("You have used the `textcoords` kwarg, but not "
Out[21]:
[<matplotlib.lines.Line2D at 0x7fe42008b080>]

In [22]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest1.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest1Out.txt"));
fig = plt.figure(1,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[22]:
(-1.0, 1.0, -0.80000000000000004, 1.0)

In [23]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest2.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest2Out.txt"));
fig = plt.figure(2,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[23]:
(-2.0, 4.0, -0.5, 3.5)

In [24]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest3.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest3Out.txt"));
fig = plt.figure(3,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[24]:
(-0.5, 2.5, -0.5, 2.5)

In [25]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest4.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest4Out.txt"));
fig = plt.figure(4,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[25]:
(-0.20000000000000001,
 1.2000000000000002,
 -0.20000000000000001,
 1.2000000000000002)

In [26]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest5.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest5Out.txt"));
fig = plt.figure(5,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[26]:
(-0.20000000000000001, 1.2000000000000002, -1.5, 1.5)

In [27]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest6.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest6Out.txt"));
fig = plt.figure(6,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

plt.axis('equal')


Out[27]:
(-0.5, 2.5, -0.5, 2.5)

In [28]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest7.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest7Out.txt"));
fig = plt.figure(7,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')

#plt.axis('equal')


Out[28]:
[<matplotlib.lines.Line2D at 0x7fe420ff4f28>]

In [29]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest8.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest8Out.txt"));
fig = plt.figure(8,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')
#plt.axis('equal')


Out[29]:
[<matplotlib.lines.Line2D at 0x7fe41f521f28>]

In [29]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest10.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest10Out.txt"));
fig = plt.figure(10,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')
#plt.axis('equal')


Out[29]:
[<matplotlib.lines.Line2D at 0x7f5d90135470>]

In [ ]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest11.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest11Out.txt"));
fig = plt.figure(11,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')
#plt.axis('equal')

In [4]:
points = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest12.txt"));
hullP = np.atleast_2d(np.loadtxt("../MinAreaRectangleTest12Out.txt"));
fig = plt.figure(12,(10,10))
plt.scatter(points.T[0],points.T[1])
hullP = np.vstack((hullP,hullP[0]))
plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')
plt.axis('equal')


Out[4]:
(0.0, 4.0, -3.5, 1.0)

In [23]:
points = np.atleast_2d(np.loadtxt("../DiameterTest1.txt"));
OOBB = np.atleast_2d(np.loadtxt("../DiameterTest1Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest1Out2.txt"));

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)

fig = plt.figure(1,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[  1.92808330e-03   9.99997796e-01   8.31002599e-04]
 [  9.99996844e-01  -1.92941845e-03   1.60887523e-03]
 [  1.61047504e-03   8.27897931e-04  -9.99998360e-01]] [-0.99316946 -0.99514351 -0.99913574] [ 0.99573536  0.99879283  0.99756498] [ 0.  0.  0.]
[ 0.00128295  0.00182466 -0.00078538]

In [10]:
points = np.atleast_2d(np.loadtxt("../DiameterTest2.txt"));
OOBB = np.atleast_2d(np.loadtxt("../DiameterTest2Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest2Out2.txt"));

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)

fig = plt.figure(2,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[ -3.77906862e-04   5.51819883e-04  -9.99999776e-01]
 [ -4.56465737e-04  -9.99999744e-01  -5.51647363e-04]
 [ -9.99999824e-01   4.56257164e-04   3.78158652e-04]] [-0.9992613  -0.99997177 -0.99976107] [ 1.00015176  0.99988911  1.00009392] [ 0.  0.  0.]
[  4.45232719e-04  -4.13304799e-05   1.66428705e-04]

In [9]:
#points = np.atleast_2d(np.loadtxt("../DiameterTest3.txt"));
OOBB = np.atleast_2d(np.loadtxt("../DiameterTest3Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest3Out2.txt"));

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)

fig = plt.figure(3,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[ 0.94521616  0.00269758 -0.32643397]
 [-0.02105905 -0.9973791  -0.06922031]
 [-0.32576514  0.07230254 -0.94268203]] [-2.32737597 -1.89934064 -3.06626709] [ 3.33764264  0.83700384  2.54623918] [ 0.  0.  0.]
[ 0.50513333 -0.5311684  -0.26001396]

In [8]:
plt.close("all")
points = np.atleast_2d(np.loadtxt("../DiameterTest4.txt"));
OOBB = np.atleast_2d(np.loadtxt("../DiameterTest4Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest4Out2.txt"));

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)

fig = plt.figure(4,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


# Somethings are wrong with min area rectangle (load it)
# points = np.atleast_2d(np.loadtxt("../computeMVBBIn.txt"));
# hullP = np.atleast_2d(np.loadtxt("../computeMVBBRect.txt"));
# fig = plt.figure(8,(10,10))
# plt.scatter(points.T[0],points.T[1])
# hullP = np.vstack((hullP,hullP[0]))
# plt.plot(hullP.T[0],hullP.T[1],'r-', ms=20, markerfacecolor='None')
#plt.axis('equal')


[[-0.34451304 -0.24446597 -0.90639239]
 [ 0.15400116 -0.96713619  0.20231467]
 [-0.92606393 -0.06988544  0.37083907]] [-2.29723646 -2.64940905 -2.15309511] [ 3.36778215  2.96309722  0.58324937] [ 0.  0.  0.]
[ 0.53527284  0.15684409 -0.78492287]
/opt/python3.4env/lib/python3.4/site-packages/mpl_toolkits/mplot3d/axes3d.py:1094: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.button_pressed in self._rotate_btn:

In [13]:
plt.close("all")

OOBB = np.atleast_2d(np.loadtxt("../DiameterTest5Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest5Out2.txt"));

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)

fig = plt.figure(5,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[ 0.55851992 -0.65368427 -0.51063918]
 [-0.70731683 -0.6968971   0.11847923]
 [-0.43331097  0.29501068 -0.8515928 ]] [  192.71633437 -1020.91538709  -290.09405895] [ 1121.74051832   590.14630896   188.82789762] [ 0.  0.  0.]
[ 657.22842635 -215.38453906  -50.63308067]

In [30]:
plt.close("all")

OOBB = np.atleast_2d(np.loadtxt("../DiameterTest6Out.txt"));
sampled = np.atleast_2d(np.loadtxt("../DiameterTest6Out2.txt"));

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)

fig = plt.figure(5,(10,10))
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(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[-0.26067635 -0.51579687  0.81608911]
 [ 0.96539685 -0.14586404  0.21617726]
 [ 0.0075345   0.84420215  0.53597198]] [ 0.02563387 -0.045806   -0.05215631] [ 0.19323253  0.06807014  0.07398787] [ 0.  0.  0.]
[ 0.1094332   0.01113207  0.01091578]

In [ ]:
Ry = Transformations.quaternion_about_axis(m.pi/2,[0,1,0]);
Rz = Transformations.quaternion_about_axis(m.pi/2,[1,0,0]);
R = Transformations.quaternion_multiply(Rz,Ry);
print(R)
print(Transformations.quaternion_matrix(R)[0:3,0:3].dot(np.array([1,0,0]) ))
print(Transformations.quaternion_matrix(R)[0:3,0:3].dot(np.array([0,1,0]) ))

In [21]:
points = np.atleast_2d(np.loadtxt("../MVBBTest1.txt"));
OOBB = np.atleast_2d(np.loadtxt("../MVBBTest1Out.txt"));

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)

fig = plt.figure(1,(10,10))
ax = Axes3D(fig)
ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
plotCube(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[-0.93894279 -0.31291763 -0.14306989]
 [-0.03530683 -0.32599271  0.94471275]
 [-0.34225701  0.89208257  0.29504037]] [-1.37922702 -0.76020592 -0.6010551 ] [ 0.51916525  0.61285669  0.54538999] [ 0.  0.  0.]
[-0.43003088 -0.07367461 -0.02783256]

In [17]:
points = np.atleast_2d(np.loadtxt("../MVBBTest2.txt"));
OOBB = np.atleast_2d(np.loadtxt("../MVBBTest2Out.txt"));

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)

fig = plt.figure(2,(10,10))
ax = Axes3D(fig)
ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
plotCube(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[ 0.27866265 -0.93271683 -0.22888085]
 [-0.54875024 -0.35022144  0.75909032]
 [-0.7881753  -0.0859317  -0.60942222]] [-0.64261932 -1.66830231 -0.44631466] [ 1.36006564  0.3270688   1.54994174] [ 0.  0.  0.]
[ 0.35872316 -0.67061676  0.55181354]

In [18]:
points = np.atleast_2d(np.loadtxt("../MVBBTest3.txt"));
OOBB = np.atleast_2d(np.loadtxt("../MVBBTest3Out.txt"));

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)

fig = plt.figure(3,(10,10))
ax = Axes3D(fig)
ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
plotCube(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[-0.86022616 -0.20319879 -0.46767639]
 [-0.50257424  0.49290677  0.71025491]
 [ 0.08619792  0.84602196 -0.52613378]] [ 0.92411454  0.41557335 -0.69135441] [ 1.0153881   0.47700973 -0.65271786] [ 0.  0.  0.]
[ 0.96975132  0.44629154 -0.67203614]

In [14]:
#points = np.atleast_2d(np.loadtxt("../MVBBTest4.txt"));
OOBB = np.atleast_2d(np.loadtxt("../MVBBTest4Out.txt"));

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)

fig = plt.figure(4,(10,10))
ax = Axes3D(fig)
#ax.scatter(points.T[0],points.T[1],points.T[2],c='b')
plotCube(K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)
plotAxis(center,A_IK,1)
plotAxis(center,np.identity(3),0.5)
axisEqual3D(ax)


[[ 0.72261187 -0.43070324 -0.54067255]
 [-0.69081925 -0.4776944  -0.54274932]
 [-0.02451236  0.76570411 -0.64272573]] [ 175.31056593 -401.54766317 -578.66024522] [ 966.32670468   21.45036885  992.9832851 ] [ 0.  0.  0.]
[ 570.81863531 -190.04864716  207.16151994]

In [ ]: