1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | from itertools import izip |
---|
4 | from math import sqrt,atan2,pi,hypot,cos,sin |
---|
5 | |
---|
6 | def grouped(iterable, n): |
---|
7 | "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..." |
---|
8 | return izip(*[iter(iterable)]*n) |
---|
9 | |
---|
10 | def norm(u): |
---|
11 | return sqrt(u["x"]**2+u["y"]**2+u["z"]**2) |
---|
12 | |
---|
13 | |
---|
14 | def rotate(y,z,rotate_deg): |
---|
15 | r = hypot(z,y) |
---|
16 | theta = atan2(z,y) + rotate_deg*pi/180 |
---|
17 | y = r*cos(theta) |
---|
18 | z = r*sin(theta) |
---|
19 | return [y,z] |
---|
20 | |
---|
21 | |
---|
22 | def makevect(points,a,b): |
---|
23 | res = {} |
---|
24 | res["x"] = points[b,"x"] - points[a,"x"] |
---|
25 | res["y"] = points[b,"y"] - points[a,"y"] |
---|
26 | res["z"] = points[b,"z"] - points[a,"z"] |
---|
27 | return res |
---|
28 | |
---|
29 | |
---|
30 | def pvect(u,v): |
---|
31 | res = {} |
---|
32 | res["x"] = u["y"]*v["z"]- u["z"]*v["y"] |
---|
33 | res["y"] = u["z"]*v["x"]- u["x"]*v["z"] |
---|
34 | res["z"] = u["x"]*v["y"]- u["y"]*v["x"] |
---|
35 | return res |
---|
36 | |
---|
37 | |
---|
38 | def getparts(data,subpart=""): |
---|
39 | if subpart != "" : |
---|
40 | subpart = "."+subpart |
---|
41 | res = None |
---|
42 | i = data.index("root"+subpart+".children :") |
---|
43 | i+=1 |
---|
44 | res = data[i].strip().split(",") |
---|
45 | return res |
---|
46 | |
---|
47 | def dumpparts(list_parts,subpart=""): |
---|
48 | if subpart != "" : |
---|
49 | subpart = "."+subpart |
---|
50 | res = "root"+subpart+".children :\n"+ ",".join(list_parts)+"\n\n" |
---|
51 | return res |
---|
52 | |
---|
53 | def getfield(part,field,data,datatype="integer",subpart=""): |
---|
54 | if subpart != "" : |
---|
55 | subpart = "."+subpart |
---|
56 | |
---|
57 | i = data.index("root."+part+subpart+"."+field+" :") |
---|
58 | i +=1 |
---|
59 | j = i |
---|
60 | while data[j] != "" : |
---|
61 | j += 1 |
---|
62 | out = filter(None,",".join(data[i:j]).split(",")) |
---|
63 | |
---|
64 | |
---|
65 | if field == "children" : |
---|
66 | datatype = "spacedstring" |
---|
67 | |
---|
68 | |
---|
69 | if datatype == "spacedstring": |
---|
70 | pass |
---|
71 | if datatype == "float": |
---|
72 | out =[float(x) for x in out] |
---|
73 | if datatype == "integer": |
---|
74 | out =[int(x) for x in out] |
---|
75 | return out |
---|
76 | |
---|
77 | def dumpfield(part,field,listdata,subpart="") : |
---|
78 | if subpart != "" : |
---|
79 | subpart = "."+subpart |
---|
80 | |
---|
81 | dump = "root."+part+subpart+"."+field+" :\n" |
---|
82 | maxcol = 10 |
---|
83 | col = 1 |
---|
84 | for item in listdata: |
---|
85 | dump += str(item) +"," |
---|
86 | if col == maxcol : |
---|
87 | dump += "\n" |
---|
88 | col = 1 |
---|
89 | col += 1 |
---|
90 | |
---|
91 | dump += "\n\n" |
---|
92 | return dump |
---|