本文整理汇总了Python中scipy.spatial.ConvexHull方法的典型用法代码示例。如果您正苦于以下问题:Python spatial.ConvexHull方法的具体用法?Python spatial.ConvexHull怎么用?Python spatial.ConvexHull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.spatial
的用法示例。
在下文中一共展示了spatial.ConvexHull方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_chempot_qhull
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def get_chempot_qhull(self):
faces = list(self.hull)
A = []
for face in faces:
A.append([face.chem_pots[e] for e in self.elements])
A = np.array(A)
conv_hull = ConvexHull(A)
uhull = set()
for facet in conv_hull.simplices:
face = frozenset([ faces[i] for i in facet
if i < len(faces) ])
uhull.add(face)
return uhull
示例2: compute_convex_hull
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def compute_convex_hull(landmarks):
""" compute convex hull around landmarks
* http://lagrange.univ-lyon1.fr/docs/scipy/0.17.1/generated/scipy.spatial.ConvexHull.html
* https://stackoverflow.com/questions/21727199
:param ndarray landmarks: set of points
:return ndarray: pints of polygon
>>> np.random.seed(0)
>>> pts = np.random.randint(15, 30, (10, 2))
>>> compute_convex_hull(pts)
array([[27, 20],
[27, 25],
[22, 24],
[16, 21],
[15, 18],
[26, 18]])
"""
chull = spatial.ConvexHull(landmarks)
chull_points = landmarks[chull.vertices]
return chull_points
示例3: get_polygon_center
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def get_polygon_center(pc):
# hull = ConvexHull(pc)
# import pdb; pdb.set_trace()
# try:
# pc_new = pc[hull.vertices,:]
# except:
# import pdb; pdb.set_trace()
# return np.sum(pc_new, axis = 0)/ len(pc_new)
# try:
sample_size = 100
if len(pc) > sample_size:
random.sample(np.arange(len(pc)).tolist(), sample_size)
pc = np.array(pc)
center = np.sum(pc, axis=0) / len(pc)
circle = smallestenclosingcircle.make_circle(pc[:, 0:2])
# except:
# import pdb; pdb.set_trace()
return np.array([circle[0], circle[1], center[2]])
示例4: buffer_shape
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def buffer_shape(shape, radius, sides=4):
'''
Return a buffered version of a shape.
The buffer radius is radius.
'''
if isinstance(shape, b2CircleShape):
return b2CircleShape(pos=shape.pos, radius=shape.radius+radius)
thetas = [(i+.5)*2*np.pi/sides for i in xrange(sides)]
buffer_vertices = [radius*np.array([np.cos(theta), np.sin(theta)]) for theta in thetas]
new_vertices = []
for v in shape.vertices:
for b in buffer_vertices:
new_vertices.append(np.array(v) + b)
hull = ConvexHull(new_vertices)
hull_vertices = [tuple(new_vertices[i]) for i in hull.vertices]
return b2PolygonShape(vertices=hull_vertices)
示例5: get_mar
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def get_mar(polygon):
""" Given a list of points, returns a minimum area rectangle that will
contain all points. It will not necessarily be vertically or horizontally
aligned.
Returns
-------
list((int, int)): 4 corner points of rectangle.
"""
polygon = tuple(polygon)
hull_ordered = [polygon[index] for index in ConvexHull(polygon).vertices]
hull_ordered.append(hull_ordered[0])
hull_ordered = tuple(hull_ordered)
min_rectangle = _bounding_area(0, hull_ordered)
for i in range(1, len(hull_ordered) - 1):
rectangle = _bounding_area(i, hull_ordered)
if rectangle['area'] < min_rectangle['area']:
min_rectangle = rectangle
min_rectangle['unit_vector_angle'] = atan2(min_rectangle['unit_vector'][1], min_rectangle['unit_vector'][0])
min_rectangle['rectangle_center'] = _to_xy_coordinates(min_rectangle['unit_vector_angle'],
min_rectangle['rectangle_center'])
points_list = _rectangle_corners(min_rectangle)
return points_list
示例6: get_linear_inequalities
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def get_linear_inequalities(self):
"""
Returns the linear inequalities defining the zontope vertices, i.e., Ax<=b.
:param Subspaces self:
An instance of the Subspaces object.
:return:
**A**: The matrix for setting the linear inequalities.
:return:
**b**: The right-hand-side vector for setting the linear inequalities.
"""
if self.Y is None:
self.Y = self.get_zonotope_vertices()
n = self.Y.shape[1]
if n == 1:
A = np.array([[1],[-1]])
b = np.array([[max(self.Y)],[min(self.Y)]])
return A, b
else:
convexHull = ConvexHull(self.Y)
A = convexHull.equations[:,:n]
b = -convexHull.equations[:,n]
return A, b
示例7: draw_cube
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def draw_cube():
"""
Draws a cube using only the vertices.
Obtains faces using the scipy convex hull
method.
"""
c = Cube(3)
verts = np.array([i.binary for i in c.vertices])-np.array([.5,.5,.5])
hull = ConvexHull(verts)
simps = hull.simplices
planes = np.array([[verts[j] for j in i] for i in simps])
for i in range(20):
im = Image.new("RGB", (2048, 2048), (1,1,1))
draw = ImageDraw.Draw(im,'RGBA')
r = np.transpose(rotation(3,np.pi*(9+i)/15)) #i=9
planes = [orient_face(pl) for pl in planes]
render_solid_planes(planes, draw, r)
im.save(".\\im" + str(i) + ".png")
示例8: drawSamples
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def drawSamples(self, nsamples):
import matplotlib.pyplot as plt
plt.figure(1)
plt.clf()
plt.hold(True)
k = ConvexHull(self.bot_pts.T).vertices
k = np.hstack((k, k[0]))
n = self.planar_polyhedron.generators.shape[0]
plt.plot(self.planar_polyhedron.generators.T[0,list(range(n)) + [0]],
self.planar_polyhedron.generators.T[1,list(range(n)) + [0]], 'r.-')
samples = sample_convex_polytope(self.c_space_polyhedron.A,
self.c_space_polyhedron.b,
500)
for i in range(samples.shape[1]):
R = np.array([[np.cos(samples[2,i]), -np.sin(samples[2,i])],
[np.sin(samples[2,i]), np.cos(samples[2,i])]])
V = R.dot(self.bot_pts[:,k])
V = V + samples[:2, i].reshape((2,1))
plt.plot(V[0,:], V[1,:], 'k-')
plt.show()
示例9: find_perimeter_nodes
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def find_perimeter_nodes(graph):
"""Find nodes on the perimeter of a graph.
Uses a convex hull to locate the perimeter nodes of a graph.
Parameters
----------
graph : graph_like
A Graph of nodes (just requires *xy_of_node*).
Returns
-------
ndarray of int
Identifiers of the perimeter nodes.
"""
from scipy.spatial import ConvexHull
hull = ConvexHull(graph.xy_of_node, qhull_options="Qt")
return as_id_array(hull.vertices)
示例10: cvHull
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def cvHull(pL):
if len(pL) < 3:
return pL
from scipy.spatial import ConvexHull
from scipy.spatial.qhull import QhullError
S = [xx.std() for xx in pL]
try:
hull = ConvexHull(S)
except QhullError:
xL = [xx.x for xx in pL]
xx, pL = order(xL, pL)
return [pL[0], pL[-1]]
P = [pL[i] for i in hull.vertices]
P.append(pL[hull.vertices[0]])
return P
示例11: convex_hull_intersection
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def convex_hull_intersection(p1, p2):
""" Compute area of two convex hull's intersection area.
p1,p2 are a list of (x,y) tuples of hull vertices.
return a list of (x,y) for the intersection and its volume
"""
inter_p = polygon_clip(p1,p2)
if inter_p is not None:
hull_inter = ConvexHull(inter_p)
return inter_p, hull_inter.volume
else:
return None, 0.0
示例12: _convex_completion
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def _convex_completion(self, arr):
blob_points = np.transpose(np.nonzero(arr))
assert len(blob_points) >= 3
hull = ConvexHull(blob_points)
for simplex in hull.simplices:
line_start, line_end = blob_points[simplex[0]], blob_points[simplex[1]]
arr = self._draw_line(arr, line_start, line_end)
arr = crop(arr)
arr = self._fill_interior(arr)
return arr
示例13: get_polygon_area
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def get_polygon_area(pc):
# import pdb; pdb.set_trace()
try:
pc = np.array(pc)
hull = ConvexHull(pc[:, 0:2])
return Polygon(pc[hull.vertices, 0:2]).area
except:
return 0
示例14: drizzle_footprint
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def drizzle_footprint(weight_image, shrink=10, ext=0, outfile=None, label=None):
"""
Footprint of image pixels where values > 0. Works best with drizzled
weight images.
"""
from scipy.spatial import ConvexHull
im = pyfits.open(weight_image)
wcs = pywcs.WCS(im[ext].header, fobj=im)
sh = np.array(im[ext].data.shape)//shrink
yp, xp = np.indices(tuple(sh))*shrink
nonzero = im[ext].data[yp, xp] > 0
h = ConvexHull(np.array([xp[nonzero], yp[nonzero]]).T)
hx = xp[nonzero][h.vertices]
hy = yp[nonzero][h.vertices]
hrd = wcs.all_pix2world(np.stack([hx, hy]).T, 0)
pstr = 'polygon('+','.join(['{0:.6f}'.format(i) for i in hrd.flatten()])+')'
if label is not None:
pstr += ' # text={{{0}}}'.format(label)
if outfile is None:
return pstr
fp = open(outfile, 'w')
fp.write('fk5\n')
fp.write(pstr+'\n')
fp.close()
示例15: convex_hull_intersection
# 需要导入模块: from scipy import spatial [as 别名]
# 或者: from scipy.spatial import ConvexHull [as 别名]
def convex_hull_intersection(p1, pt):
"""
compute area of two convex hull's intersection area
:param p1: a list of (x,y) tuples of hull vertices
:param pt: a list of (x,y) tuples of hull vertices
:return:
a list of (x,y) for the intersection and its volume
"""
inter_p = polygon_clip(p1, pt)
if inter_p is not None:
hull_inter = ConvexHull(inter_p)
return inter_p, hull_inter.volume
else:
return None, 0.0