1
我希望使用CGAL來獲得alpha形狀。CGAL alpha形狀python簡單的例子出錯了?
@sloriot提供an extremely relevant script和我的定製後:
from sys import *
path.append("../../cgal_package")
from CGAL.Alpha_shapes_2 import *
from CGAL.Triangulations_2 import Delaunay_triangulation_2
from CGAL.Kernel import *
from random import *
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import constants
def Point_2_str(self):
return "Point_2"+str((self.x(), self.y()))
# now we turn it into a member function
Point_2.__str__ = Point_2_str
def show_alpha_values(AS):
print "Alpha spectrum"
for alpha_value in AS.alpha:
print alpha_value
def read_points(file):
result = []
dataFile = open(file, 'r')
for i, line in enumerate(dataFile):
coordinateList = [float(x) for x in line.strip().split()]
for j in range(0, len(coordinateList), 2):
result.append(Point_2(coordinateList[j], coordinateList[j+1]))
dataFile.close()
return result
def getAlphaShape():
L =[]
verbose = True
list_of_points = read_points(constants.PROJECT_PATH + '\\data\\clusters.txt')
a = Alpha_shape_2()
a.make_alpha_shape(list_of_points)
a.set_mode(Alpha_shape_2.Mode.REGULARIZED)
a.set_alpha(1000)
alpha_shape_edges = []
alpha_shape_vertices = []
for it in a.alpha_shape_edges:
alpha_shape_edges.append(a.segment(it))
for it in a.alpha_shape_vertices:
alpha_shape_vertices.append(it)
_showAlphaShape(list_of_points, alpha_shape_vertices)
print "alpha_shape_edges"
print len(alpha_shape_edges)
print "alpha_shape_vertices"
print len(alpha_shape_vertices)
print "Optimal alpha: "
print a.find_optimal_alpha(2).next()
def _showAlphaShape(points, vertices):
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes.axis('equal')
axes.set_xlabel(r'$x (m)$')
axes.set_ylabel(r'$y (m)$')
axes.set_title(r'$\alpha$-shape of clusters')
# draw cluster points
x = [pt.x() for pt in points]
y = [pt.y() for pt in points]
axes.scatter(x, y, s=1)
# draw alpha shape
for i in range(len(vertices)-1):
x1 = vertices[i].point()[0]
x2 = vertices[i+1].point()[0]
y1 = vertices[i].point()[1]
y2 = vertices[i+1].point()[1]
axes.plot([[x1, x2], [y1, y2]], color='b')
fontP = FontProperties()
fontP.set_size('7')
axes.legend(loc=3, prop=fontP)
fig.savefig(constants.PROJECT_PATH + '\\data\\1.svg')
當我運行它,我得到
藍色部分顯然是不的字母形狀。 哪裏出錯了?
你在閱讀什麼文檔頁面?您需要將C++示例轉換爲python。 [Here](http://doc.cgal.org/latest/Alpha_shapes_2/index.html#title4)就是其中之一。僅供參考,在cgal-python網站上寫明,此軟件包不再維護,現在[cgal-bindings](http://code.google.com/p/cgal-bindings/)是推薦的替代選擇。 – sloriot
@sloriot我在這裏有同樣的問題。我只需要一組阿爾法形狀的一組點,就是這樣。所以沒有太多要求,只需要cgal-python包就足夠了。你能不能好好展示一下我的目的的實例?簡單地說,隨機點集的阿爾法形狀。非常感謝!這也將有助於其他人,因爲文檔非常簡陋。 – 2013-11-12 06:49:08
看[這裏](https://gforge.inria.fr/scm/viewvc.php/trunk/cgal-python/test/Alpha_shapes_2/test_alpha_shape_2.py?view=markup&root=cgal-python) – sloriot