[579] | 1 | """Alpha shape |
---|
| 2 | """ |
---|
| 3 | |
---|
| 4 | import exceptions |
---|
| 5 | from Numeric import array, Float |
---|
| 6 | from load_mesh.loadASCII import load_xya_file, export_boundary_file |
---|
| 7 | |
---|
| 8 | class PointError(exceptions.Exception): pass |
---|
| 9 | |
---|
| 10 | OUTPUT_FILE_TITLE = "# The alpha shape boundary defined by point index pairs of edges" |
---|
| 11 | |
---|
| 12 | def alpha_shape_via_files(point_file, boundary_file, alpha= None): |
---|
| 13 | |
---|
| 14 | from load_mesh.loadASCII import load_xya_file |
---|
| 15 | |
---|
| 16 | point_dict = load_xya_file(point_file) |
---|
| 17 | points = point_dict['pointlist'] |
---|
| 18 | #title_string = point_dict['title'] |
---|
| 19 | |
---|
| 20 | alpha = Alpha_Shape(points) |
---|
| 21 | alpha.write_boundary(boundary_file) |
---|
| 22 | |
---|
| 23 | class Alpha_Shape: |
---|
| 24 | |
---|
| 25 | def __init__(self, points, alpha = None): |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | """ Build interpolation matrix mapping from |
---|
| 29 | function values at vertices to function values at data points |
---|
| 30 | |
---|
| 31 | Inputs: |
---|
| 32 | |
---|
| 33 | points: List of coordinate pairs [[x1, y1],[x2, y2]..] |
---|
| 34 | |
---|
| 35 | alpha: alpha shape parameter |
---|
| 36 | |
---|
| 37 | """ |
---|
| 38 | self._set_points(points) |
---|
| 39 | self._alpha_shape_algorithm() |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | def _set_points(self, points): |
---|
| 43 | """ |
---|
| 44 | """ |
---|
| 45 | |
---|
| 46 | if len (points) <= 2: |
---|
| 47 | raise PointError, "Too few points to find an alpha shape" |
---|
| 48 | |
---|
| 49 | #Convert input to Numeric arrays |
---|
| 50 | self.points = array(points).astype(Float) |
---|
| 51 | |
---|
| 52 | if len (points) <= 2: |
---|
| 53 | raise PointError, "Too few points to find an alpha shape" |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | def write_boundary(self,file_name): |
---|
| 57 | """ |
---|
| 58 | Write the boundary to a file |
---|
| 59 | """ |
---|
| 60 | #print " this info will be in the file",boundary |
---|
| 61 | export_boundary_file(file_name, self.get_boundary(), |
---|
| 62 | OUTPUT_FILE_TITLE, delimiter = ',') |
---|
| 63 | |
---|
| 64 | def get_boundary(self): |
---|
| 65 | """ |
---|
| 66 | """ |
---|
| 67 | return self.boundary |
---|
| 68 | |
---|
| 69 | def _alpha_shape_algorithm(self): |
---|
| 70 | |
---|
| 71 | # A python style guide suggests using _[function name] to |
---|
| 72 | # specify interal functions |
---|
| 73 | #- this is the first time I've used it though - DSG |
---|
| 74 | |
---|
| 75 | # this produces a baaad boundary |
---|
| 76 | boundary = [] |
---|
| 77 | for point_index in range(len(self.points)-1): |
---|
| 78 | boundary.append([point_index,point_index +1]) |
---|
| 79 | boundary.append([len(self.points)-1,0]) |
---|
| 80 | self.boundary = boundary |
---|
| 81 | |
---|
| 82 | #------------------------------------------------------------- |
---|
| 83 | if __name__ == "__main__": |
---|
| 84 | """ |
---|
[580] | 85 | Load in a data point file. |
---|
| 86 | Determine the alpha shape boundary |
---|
| 87 | Save the boundary to a file. |
---|
| 88 | |
---|
| 89 | usage: alpha_shape.py point_file.xya boundary_file.bnd [alpha] |
---|
| 90 | |
---|
| 91 | The alpha value is optional. |
---|
[579] | 92 | """ |
---|
[580] | 93 | |
---|
[579] | 94 | import os, sys |
---|
[580] | 95 | usage = "usage: %s point_file.xya boundary_file.bnd [alpha]"\ |
---|
[579] | 96 | %os.path.basename(sys.argv[0]) |
---|
[580] | 97 | # I made up the .bnd affix. Other ideas welcome. -DSG |
---|
[579] | 98 | if len(sys.argv) < 3: |
---|
| 99 | print usage |
---|
| 100 | else: |
---|
| 101 | point_file = sys.argv[1] |
---|
| 102 | boundary_file = sys.argv[2] |
---|
| 103 | if len(sys.argv) > 4: |
---|
| 104 | alpha = sys.argv[3] |
---|
| 105 | else: |
---|
| 106 | alpha = None |
---|
| 107 | alpha_shape_via_files(point_file, boundary_file, alpha) |
---|
| 108 | |
---|