Mercurial > hg > python
annotate safe.py @ 24:6df2f6dcc809
fixed interval creation bug, made result better for clipping
author | Henry Thompson <ht@markup.co.uk> |
---|---|
date | Sat, 29 May 2021 21:29:08 +0100 |
parents | 1670a33e3e6d |
children | 5488b5d3ba10 |
rev | line source |
---|---|
23 | 1 #!/usr/bin/python3 |
2 '''Find a safe landing space (i.e. in air, not above lava) near a cluster''' | |
3 import sys, math | |
4 | |
5 from util import * | |
6 from bisect import insort | |
7 | |
8 def usage(): | |
9 print("""Usage: safe.py [-d n] [filename] | |
10 n is maximum distance of landing to some point in the cluster, default is 10. | |
11 | |
12 Feed stdin with lines from the output of cluster.py, having produced | |
13 air.tsv with the same settings as the cluster input, and lava.tsv | |
14 with a lower-bound on Y of 0""") | |
15 | |
16 n=10.0 | |
17 if len(sys.argv)>1: | |
18 if sys.argv[1]=='-d': | |
19 sys.argv.pop() | |
20 n=float(sys.argv.pop(1)) | |
21 | |
22 if len(sys.argv)>1: | |
23 infile=open(sys.argv.pop(1)) | |
24 else: | |
25 infile=sys.stdin | |
26 | |
27 class Block: | |
28 '''Shared by Air and Lava, | |
29 holds points and columns. | |
30 Columns are dictionaries indexed by (x,z) coords, | |
31 value is upward-ordered list of pairs, closed intervals of y''' | |
32 def __init__(self,filename): | |
33 with open(filename,'r') as file: | |
34 self.readHeaders(file) | |
35 self.readPoints(file) | |
36 | |
37 cc=self.columns={} | |
38 tc={} | |
39 for p in self.points: | |
40 k=(p[0],p[2]) | |
41 y=p[1] | |
42 try: | |
43 yy=tc[k] | |
44 insort(yy,y) | |
45 except KeyError: | |
46 tc[k]=[y] | |
47 # convert to lists of intervals | |
48 for k,yy in tc.items(): | |
49 tyy=[yy] | |
50 ii=[] | |
24
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
51 if k==(108.5, -45.5): |
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
52 pass |
23 | 53 while True: |
54 clean=True | |
55 for j,yy in enumerate(tyy): | |
56 if len(yy)==1+(yy[-1]-yy[0]): | |
57 ii.append((yy[0],yy[-1])) | |
58 else: | |
59 clean=False | |
60 for i in range(len(yy)-1): | |
61 if yy[i]+1!=yy[i+1]: | |
24
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
62 ii.append((yy[0],yy[i])) |
23 | 63 tyy=[yy[i+1:]]+tyy[j+1:] |
64 break | |
65 if clean: | |
66 break | |
67 self.columns[k]=set(ii) # so we can merge later | |
68 | |
69 def readPoints(self,file): | |
70 file.readline() | |
71 self.points=[[float(i) for i in l.split('\t')[2].split(',')] for l in file] | |
72 | |
73 Block.readHeaders=readHeaders # from util | |
74 | |
75 class Air(Block): | |
76 pass | |
77 | |
78 A=Air('air.tsv') | |
79 | |
80 class Lava(Block): | |
81 pass | |
82 | |
83 L=Lava('lava.tsv') | |
84 | |
85 def d(p1,p2): | |
86 dx=p1[0]-p2[0] | |
87 dz=p1[1]-p2[1] | |
88 dy=p1[2]-p2[2] | |
89 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz)) | |
90 | |
91 def safety(p): | |
92 ka=[((a[0],a[2]),d(a,p)) for a in A.points if d(a,p)<=n] | |
93 return [(k,A.columns[k[0]],L.columns[k[0]]) for k in ka if k[0] in L.columns] | |
94 | |
95 readHeaders(sys.modules['__main__'],infile,False) | |
96 for l in infile: | |
97 c=eval(l) | |
98 ss=[] | |
99 hits={} | |
100 misses=[] | |
101 for p in c: | |
102 ss=safety(p) | |
103 if ss: | |
104 for (k,a,l) in ss: | |
105 try: | |
106 (aa,ll)=hits[k] | |
107 aa.update(a) | |
108 ll.update(l) | |
109 except KeyError: | |
110 hits[k]=(a,l) | |
111 else: | |
112 misses.append(p) | |
113 print(c) | |
114 if hits: | |
115 print(' %s nearby landing columns'%len(hits)) | |
24
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
116 done={} |
23 | 117 for k in sorted(hits.keys(),key=lambda k:k[1]): |
24
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
118 if k[0] not in done: |
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
119 done[k[0]]=1 |
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
120 aa,ll=hits[k] |
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
121 laa=sorted(list(aa)) |
6df2f6dcc809
fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents:
23
diff
changeset
|
122 print("(%s %s %s)@%0.1f: %s %s"%(k[0][0],laa[0][0],k[0][1],k[1],laa,sorted(list(ll)))) |
23 | 123 else: |
124 print(' No nearby landing zones') | |
125 | |
126 if infile!=sys.stdin: | |
127 infile.close() |