In [2]:
from shapeanalyzer.boundings import BoundingGeometry
from shapeanalyzer.calipers import RotatingCaliper
from shapeanalyzer.shapes import *
import math
points = []
fp = open("data/twpoi.csv")
lines = fp.readlines()
del lines[0]
for r in lines:
r = r.strip().split(",")
pt = Point(float(r[0]), float(r[1]))
points.append(pt)
bg = BoundingGeometry(points)
bbox = bg.Envelope()
hull = bg.ConvexHull()
circle, support = bg.MinimumAreaCircle()
rect = bg.MinimumWidthRectangle()
rc = RotatingCaliper(hull)
antipodals = rc.AntiPodals()
diameter, dsup = rc.Diameter()
width, wsup = rc.Width()
#print bbox
#print hull
#print circle, support
#print antipodals
print diameter, dsup
print width, wsup
print rect
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
plt.axis("equal")
fig_size = [10,10]
plt.rcParams["figure.figsize"] = fig_size
def plot_bbox(bbox):
minx, miny, maxx, maxy = bbox
xs = [minx, minx, maxx, maxx, minx]
ys = [miny, maxy, maxy, miny, miny]
plt.plot(xs,ys,'r')
def plot_poly(poly):
parr = []
for p in poly:
parr.append(p.coords())
parr.append(poly[len(poly)-1].coords())
parr = np.asarray(parr)
plt.plot(parr[:,0], parr[:,1],"g-")
def plot_circle(circle):
#plt.plot(circle.center.x, circle.center.y, "or")
circle = plt.Circle((circle.center.x, circle.center.y), radius=circle.radius, color='c', fill=False)
plt.gca().add_patch(circle)
plt.axis('scaled')
def plot_rect(rect):
plt.plot(rect.center.x, rect.center.y, "or")
center = rect.center.coords()
axx = [rect.axes[0].x,rect.axes[0].y]
axy = [rect.axes[1].x,rect.axes[1].y]
extx = rect.extents[0] / 2
exty = rect.extents[1] / 2
pt1 = np.asarray(center) + extx * np.asarray(axx) + exty * np.asarray(axy)
pt2 = np.asarray(center) + extx * np.asarray(axx) - exty * np.asarray(axy)
pt3 = np.asarray(center) - extx * np.asarray(axx) - exty * np.asarray(axy)
pt4 = np.asarray(center) - extx * np.asarray(axx) + exty * np.asarray(axy)
r = np.array([pt1,pt2,pt3,pt4,pt1])
plt.plot(r[:,0], r[:,1],"m-")
parr = []
for p in points:
parr.append(p.coords())
parr = np.asarray(parr)
plt.plot(parr[:,0], parr[:,1],"ko")
plot_bbox(bbox)
plot_poly(hull)
plot_circle(circle)
plot_poly(wsup)
plot_rect(rect)
plt.show()
In [ ]:
In [ ]: