在使用 Python 中的 CGAL(Computational Geometry Algorithms Library)库计算 alpha shape 时,遇到了问题。虽然遵循了相关示例和教程,但计算结果却与预期不符。
2. 解决方案
经过仔细排查,发现问题出在使用了错误的库版本上。具体来说,在代码中使用了 CGAL 的 5.1 版本,而示例中使用的是 4.12 版本。这两个版本的库在接口和功能上存在一些差异,导致了错误的结果。
为了解决这个问题,将 CGAL 库版本更新到 4.12,然后再次运行代码。这次,计算结果与预期的一致,成功获得了正确的 alpha shape。
以下是代码中需要修改的部分:
from CGAL.Alpha_shapes_2 import *
from CGAL.Triangulations_2 import Delaunay_triangulation_2
from CGAL.Kernel import *
将上述代码替换为:
from CGAL import Alpha_shapes_2, Delaunay_triangulation_2, Kernel
修改后,代码可以正常运行并获得正确的结果。
以下是完整的代码:
from CGAL import Alpha_shapes_2, Delaunay_triangulation_2, Kernel
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(