Question

I wish to obain the alpha shape using CGAL.

@sloriot provides an extremely relevant script and after my customization:

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')

Once I run it, I get

enter image description here

The blue part is obviously not the alpha-shape. Where went wrong?

Was it helpful?

Solution

See from your detailed question here. I highly suspect it is due to the mismatch between your CGAL and your Python package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top