source: inundation/ga/storm_surge/alpha_shape/alpha_shape.py @ 643

Last change on this file since 643 was 580, checked in by duncan, 20 years ago

added comments

File size: 3.0 KB
Line 
1"""Alpha shape   
2"""
3
4import exceptions
5from Numeric import array, Float
6from load_mesh.loadASCII import load_xya_file, export_boundary_file
7
8class PointError(exceptions.Exception): pass
9
10OUTPUT_FILE_TITLE = "# The alpha shape boundary defined by point index pairs of edges"
11
12def 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
23class 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#-------------------------------------------------------------
83if __name__ == "__main__":
84    """
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.
92    """
93   
94    import os, sys
95    usage = "usage: %s point_file.xya boundary_file.bnd [alpha]"\
96            %os.path.basename(sys.argv[0])
97    # I made up the .bnd affix. Other ideas welcome. -DSG
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       
Note: See TracBrowser for help on using the repository browser.