Statistics
| Revision:

root / PyOpenGL-Demo / dek / OglSurface / Geometry.py @ 1

History | View | Annotate | Download (1.1 kB)

1
# This is statement is required by the build system to query build info
2
if __name__ == '__build__':
3
        raise Exception
4

    
5

    
6
try:
7
        import numpy as Numeric
8
except ImportError, err:
9
        try: 
10
                import Numeric
11
        except ImportError, err:
12
                print "This demo requires the numpy or Numeric extension, sorry"
13
                import sys
14
                sys.exit()
15

    
16
def vv3(x, y):
17
        return Numeric.add.reduce(x*y)
18

    
19
def vxv3(a, b):
20
        return Numeric.array([
21
                a[1] * b[2] - a[2] * b[1],
22
                a[2] * b[0] - a[0] * b[2],
23
                a[0] * b[1] - a[1] * b[0]
24
                ])
25

    
26
def dist3(x,y):
27
        return Numeric.sqrt(Numeric.add.reduce((y-x)**2))
28

    
29
def angle(x,y,z):
30
        d1 = dist3(x,y)
31
        d2 = dist3(z,y)
32
        if (d1 <= 0 or d2 <= 0):
33
                return 0.0
34

    
35
        acc = Numeric.add.reduce((y-x) * (y-z)) / (d1*d2)
36

    
37
        if (acc > 1):
38
                acc = 1.
39
        elif (acc < -1):
40
                acc = -1.
41

    
42
        return Numeric.arccos(acc)
43

    
44
def dihedral(crds):
45
        q = crds[1] - crds[0]
46
        r = crds[1] - crds[2]
47
        s = crds[2] - crds[3]
48

    
49
        t = vxv3(q, r)
50
        u = vxv3(s, r)
51
        v = vxv3(u, t)
52

    
53
        w = vv3(v, r)
54
        z = Numeric.zeros(3, Numeric.Float)
55
        acc = angle(t, z, u)
56
        ang = acc / Numeric.pi * 180.
57

    
58
        if (w < 0):
59
                ang = 360-ang
60

    
61
        return ang