1 | |
---|
2 | |
---|
3 | def load_ungenerate(ofile): |
---|
4 | """ |
---|
5 | import a file, ofile, with the format |
---|
6 | [poly] |
---|
7 | poly format: |
---|
8 | First line: <# of vertices> <x centroid> <y centroid> |
---|
9 | Following lines: <x> <y> |
---|
10 | last line: "END" |
---|
11 | |
---|
12 | Note: These are clockwise. |
---|
13 | |
---|
14 | Returns a dict containing "points", "segments", and "polygons". |
---|
15 | """ |
---|
16 | fd = open(ofile,'r') |
---|
17 | Dict = readUngenerateFile(fd) |
---|
18 | fd.close() |
---|
19 | return Dict |
---|
20 | |
---|
21 | |
---|
22 | def readUngenerateFile(fd): |
---|
23 | """ |
---|
24 | import a file, ofile, with the format |
---|
25 | [poly] |
---|
26 | poly format: |
---|
27 | First line: <# of polynomial> <x centroid> <y centroid> |
---|
28 | Following lines: <x> <y> |
---|
29 | last line: "END" |
---|
30 | """ |
---|
31 | |
---|
32 | END_DELIMITER = 'END' |
---|
33 | |
---|
34 | points = [] |
---|
35 | segments = [] |
---|
36 | polygons = [] |
---|
37 | |
---|
38 | isEnd = False |
---|
39 | line = fd.readline() #not used <# of polynomial> <x> <y> |
---|
40 | while not isEnd: |
---|
41 | poly = [] |
---|
42 | line = fd.readline() |
---|
43 | fragments = line.split() |
---|
44 | x = float(fragments.pop(0)) |
---|
45 | y = float(fragments.pop(0)) |
---|
46 | points.append([x,y]) |
---|
47 | poly.append([x,y]) |
---|
48 | PreviousVertIndex = len(points)-1 |
---|
49 | firstVertIndex = PreviousVertIndex |
---|
50 | |
---|
51 | line = fd.readline() #Read the next line |
---|
52 | while not line.startswith(END_DELIMITER): |
---|
53 | #print "line >" + line + "<" |
---|
54 | fragments = line.split() |
---|
55 | x = float(fragments.pop(0)) |
---|
56 | y = float(fragments.pop(0)) |
---|
57 | points.append([x,y]) |
---|
58 | poly.append([x,y]) |
---|
59 | thisVertIndex = len(points)-1 |
---|
60 | segment = [PreviousVertIndex,thisVertIndex] |
---|
61 | segments.append(segment) |
---|
62 | PreviousVertIndex = thisVertIndex |
---|
63 | line = fd.readline() #Read the next line |
---|
64 | # If the last and first segments are the same, |
---|
65 | # Remove the last segment and the last vertex |
---|
66 | # then add a segment from the second last vert to the 1st vert |
---|
67 | thisVertIndex = len(points)-1 |
---|
68 | firstVert = points[firstVertIndex] |
---|
69 | thisVert = points[thisVertIndex] |
---|
70 | #print "firstVert",firstVert |
---|
71 | #print "thisVert",thisVert |
---|
72 | if (firstVert[0] == thisVert[0] and firstVert[1] == thisVert[1]): |
---|
73 | points.pop() |
---|
74 | segments.pop() |
---|
75 | poly.pop() |
---|
76 | thisVertIndex = len(points)-1 |
---|
77 | segments.append([thisVertIndex, firstVertIndex]) |
---|
78 | |
---|
79 | line = fd.readline() # read <# of polynomial> <x> <y> OR END |
---|
80 | #print "line >>" + line + "<<" |
---|
81 | # do poly stuff here |
---|
82 | polygons.append(poly) |
---|
83 | if line.startswith(END_DELIMITER): |
---|
84 | isEnd = True |
---|
85 | |
---|
86 | #print "points", points |
---|
87 | #print "segments", segments |
---|
88 | ungenerated_dict = {} |
---|
89 | ungenerated_dict['points'] = points |
---|
90 | ungenerated_dict['segments'] = segments |
---|
91 | ungenerated_dict['polygons'] = polygons |
---|
92 | return ungenerated_dict |
---|