printrun-src/printrun/gl/trackball.py

Fri, 28 Jul 2017 16:05:08 +0200

author
mdd
date
Fri, 28 Jul 2017 16:05:08 +0200
changeset 30
9188235e3bbf
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added module watcher for easier development (automatic module reload)

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 from pyglet.gl import GLdouble
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 def cross(v1, v2):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 return [v1[1] * v2[2] - v1[2] * v2[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 v1[2] * v2[0] - v1[0] * v2[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 v1[0] * v2[1] - v1[1] * v2[0]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 def trackball(p1x, p1y, p2x, p2y, r):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 TRACKBALLSIZE = r
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 if p1x == p2x and p1y == p2y:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 return [0.0, 0.0, 0.0, 1.0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 p1 = [p1x, p1y, project_to_sphere(TRACKBALLSIZE, p1x, p1y)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 p2 = [p2x, p2y, project_to_sphere(TRACKBALLSIZE, p2x, p2y)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 a = cross(p2, p1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 d = map(lambda x, y: x - y, p1, p2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 t = math.sqrt(sum(map(lambda x: x * x, d))) / (2.0 * TRACKBALLSIZE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 if t > 1.0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 t = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 if t < -1.0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 t = -1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 phi = 2.0 * math.asin(t)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 return axis_to_quat(a, phi)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 def axis_to_quat(a, phi):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 lena = math.sqrt(sum(map(lambda x: x * x, a)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 q = map(lambda x: x * (1 / lena), a)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 q = map(lambda x: x * math.sin(phi / 2.0), q)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 q.append(math.cos(phi / 2.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 return q
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 def build_rotmatrix(q):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 m = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 m[0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 m[1] = 2.0 * (q[0] * q[1] - q[2] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 m[2] = 2.0 * (q[2] * q[0] + q[1] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 m[3] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 m[4] = 2.0 * (q[0] * q[1] + q[2] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 m[5] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 m[6] = 2.0 * (q[1] * q[2] - q[0] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 m[7] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 m[8] = 2.0 * (q[2] * q[0] - q[1] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 m[9] = 2.0 * (q[1] * q[2] + q[0] * q[3])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 m[10] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 m[11] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 m[12] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 m[13] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 m[14] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 m[15] = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 return m
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 def project_to_sphere(r, x, y):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 d = math.sqrt(x * x + y * y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 if (d < r * 0.70710678118654752440):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 return math.sqrt(r * r - d * d)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 t = r / 1.41421356237309504880
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 return t * t / d
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 def mulquat(q1, rq):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return [q1[3] * rq[0] + q1[0] * rq[3] + q1[1] * rq[2] - q1[2] * rq[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 q1[3] * rq[1] + q1[1] * rq[3] + q1[2] * rq[0] - q1[0] * rq[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 q1[3] * rq[2] + q1[2] * rq[3] + q1[0] * rq[1] - q1[1] * rq[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 q1[3] * rq[3] - q1[0] * rq[0] - q1[1] * rq[1] - q1[2] * rq[2]]

mercurial