[258] | 1 | |
---|
| 2 | |
---|
| 3 | def angle(v): |
---|
| 4 | """Compute angle between e1 (the unit vector in the x-direction) |
---|
| 5 | and the specified vector |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | from math import acos, pi, sqrt |
---|
| 9 | from Numeric import sum, array |
---|
| 10 | |
---|
| 11 | l = sqrt( sum (array(v)**2)) |
---|
| 12 | v1 = v[0]/l |
---|
| 13 | v2 = v[1]/l |
---|
| 14 | |
---|
| 15 | theta = acos(v1) |
---|
| 16 | |
---|
| 17 | if v2 < 0: |
---|
| 18 | #Quadrant 3 or 4 |
---|
| 19 | theta = 2*pi-theta |
---|
| 20 | |
---|
| 21 | return theta |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | def anglediff(v0, v1): |
---|
| 25 | """Compute difference between angle of vector x0, y0 and x1, y1. |
---|
| 26 | This is used for determining the ordering of vertices, |
---|
| 27 | e.g. for checking if they are counter clockwise. |
---|
| 28 | |
---|
| 29 | Always return a positive value |
---|
| 30 | """ |
---|
| 31 | |
---|
| 32 | from math import pi |
---|
| 33 | |
---|
| 34 | a0 = angle(v0) |
---|
| 35 | a1 = angle(v1) |
---|
| 36 | |
---|
| 37 | #Ensure that difference will be positive |
---|
| 38 | if a0 < a1: |
---|
| 39 | a0 += 2*pi |
---|
| 40 | |
---|
| 41 | return a0-a1 |
---|
| 42 | |
---|
| 43 | |
---|
[274] | 44 | def mean(x): |
---|
| 45 | from Numeric import sum |
---|
| 46 | return sum(x)/len(x) |
---|
[258] | 47 | |
---|
| 48 | #################################################################### |
---|
| 49 | #Python versions of function that are also implemented in util_gateway.c |
---|
| 50 | # |
---|
| 51 | |
---|
| 52 | def gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2): |
---|
| 53 | """ |
---|
| 54 | """ |
---|
| 55 | |
---|
| 56 | det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0) |
---|
| 57 | a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0) |
---|
| 58 | a /= det |
---|
| 59 | |
---|
| 60 | b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0) |
---|
| 61 | b /= det |
---|
| 62 | |
---|
| 63 | return a, b |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | ############################################## |
---|
| 70 | #Initialise module |
---|
| 71 | |
---|
| 72 | import compile |
---|
| 73 | if compile.can_use_C_extension('util_ext.c'): |
---|
| 74 | from util_ext import gradient |
---|
| 75 | else: |
---|
| 76 | gradient = gradient_python |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | if __name__ == "__main__": |
---|
| 80 | pass |
---|
| 81 | |
---|