annotate python/other/bouncing_cube.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 534b94b40aa8
children
rev   line source
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
1 from PyQt4.QtGui import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
2 from PyQt4.QtCore import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
3 from PyQt4.QtOpenGL import QGLWidget
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
4 from OpenGL.GL import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
5 from OpenGL.GLU import gluPerspective
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
6 import sys
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
7 from random import random
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
8 from math import pi, cos, sin, fabs, sqrt
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
9 from numpy import mat, array, ones, zeros, eye
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
10 from numpy.linalg import norm
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
11 import numpy as np
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
12 import time
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
13 import scipy.integrate
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
14
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
15 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
16 Test script that lets a dice bounce.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
17 Converted from 20-sim equations into python code.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
18
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
19 20-sim website:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
20 http://www.20sim.com
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
21
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
22 """
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
23
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
24 # OpenGL drawing madness:
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
25 def drawCube(w):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
26 glBegin(GL_QUADS) # Start Drawing The Cube
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
27 glColor3f(0.0,1.0,0.0) # Set The Color To Blue
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
28 glVertex3f( w, w,-w) # Top Right Of The Quad (Top)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
29 glVertex3f(-w, w,-w) # Top Left Of The Quad (Top)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
30 glVertex3f(-w, w, w) # Bottom Left Of The Quad (Top)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
31 glVertex3f( w, w, w) # Bottom Right Of The Quad (Top)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
32
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
33 glColor3f(1.0,0.5,0.0) # Set The Color To Orange
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
34 glVertex3f( w,-w, w) # Top Right Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
35 glVertex3f(-w,-w, w) # Top Left Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
36 glVertex3f(-w,-w,-w) # Bottom Left Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
37 glVertex3f( w,-w,-w) # Bottom Right Of The Quad (Bottom)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
38
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
39 glColor3f(1.0,0.0,0.0) # Set The Color To Red
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
40 glVertex3f( w, w, w) # Top Right Of The Quad (Front)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
41 glVertex3f(-w, w, w) # Top Left Of The Quad (Front)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
42 glVertex3f(-w,-w, w) # Bottom Left Of The Quad (Front)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
43 glVertex3f( w,-w, w) # Bottom Right Of The Quad (Front)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
44
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
45 glColor3f(1.0,1.0,0.0) # Set The Color To Yellow
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
46 glVertex3f( w,-w,-w) # Bottom Left Of The Quad (Back)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
47 glVertex3f(-w,-w,-w) # Bottom Right Of The Quad (Back)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
48 glVertex3f(-w, w,-w) # Top Right Of The Quad (Back)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
49 glVertex3f( w, w,-w) # Top Left Of The Quad (Back)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
50
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
51 glColor3f(0.0,0.0,1.0) # Set The Color To Blue
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
52 glVertex3f(-w, w, w) # Top Right Of The Quad (Left)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
53 glVertex3f(-w, w,-w) # Top Left Of The Quad (Left)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
54 glVertex3f(-w,-w,-w) # Bottom Left Of The Quad (Left)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
55 glVertex3f(-w,-w, w) # Bottom Right Of The Quad (Left)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
56
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
57 glColor3f(1.0,0.0,1.0) # Set The Color To Violet
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
58 glVertex3f( w, w,-w) # Top Right Of The Quad (Right)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
59 glVertex3f( w, w, w) # Top Left Of The Quad (Right)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
60 glVertex3f( w,-w, w) # Bottom Left Of The Quad (Right)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
61 glVertex3f( w,-w,-w) # Bottom Right Of The Quad (Right)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
62 glEnd() # Done Drawing The Quad
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
63
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
64 def drawFloor(w, h):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
65 glBegin(GL_QUADS) # Start Drawing The Cube
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
66 glColor3f(1.0,0.5,0.0) # Set The Color To Orange
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
67 glVertex3f( w,-w,h)# Top Right Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
68 glVertex3f(-w,-w,h)# Top Left Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
69 glVertex3f(-w,w,h)# Bottom Left Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
70 glVertex3f( w,w,h)# Bottom Right Of The Quad (Bottom)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
71 glEnd() # Done Drawing The Quad
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
72
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
73 def drawAxis():
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
74 glLineWidth(0.5)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
75 glBegin(GL_LINES)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
76 glColor3f(1.0, 0.0, 0.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
77 glVertex3f(0,0,0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
78 glVertex3f(1,0,0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
79 glColor3f(0.0, 1.0, 0.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
80 glVertex3f(0,0,0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
81 glVertex3f(0,1,0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
82 glColor3f(0.0, 0.0, 1.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
83 glVertex3f(0,0,0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
84 glVertex3f(0,0,1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
85 glEnd()
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
86
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
87
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
88 # Math helper functions:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
89
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
90 def cross(A, B):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
91 a = A.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
92 b = B.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
93 return mat(np.cross(a, b)).T
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
94
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
95 def skew(X):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
96 Y = mat(zeros( (3, 3) ))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
97 a, b, c = X.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
98 Y[0,1] = -c
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
99 Y[0,2] = b
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
100 Y[1,0] = c
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
101 Y[1,2] = -a
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
102 Y[2,0] = -b
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
103 Y[2,1] = a
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
104 return Y
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
105
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
106 def adjoint(T):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
107 W = T[0:3, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
108 V = T[3:6, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
109 a = mat(zeros( (6, 6) ) )
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
110 a[0:3, 0:3] = skew(W)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
111 a[3:6, 0:3] = skew(V)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
112 a[3:6, 3:6] = skew(W)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
113 return a
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
114
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
115 def Adjoint(H):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
116 R = H[0:3, 0:3]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
117 P = H[0:3, 3]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
118 a = mat(zeros( (6, 6) ) )
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
119 a[0:3, 0:3] = R
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
120 a[3:6, 3:6] = R
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
121 a[3:6, 0:3] = skew(P) * R
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
122 return a
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
123
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
124 def quatToR(q):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
125 x, y, z, w = q.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
126 r = mat(eye(3))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
127 r[0,0] = 1 - (2*y**2+2*z**2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
128 r[0,1] = 2*x*y+2*z*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
129 r[0,2] = 2*x*z - 2*y*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
130 r[1,0] = 2*x*y-2*z*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
131 r[1,1] = 1 - (2*x**2 + 2*z**2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
132 r[1,2] = 2*y*z + 2*x*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
133 r[2,0] = 2*x*z+2*y*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
134 r[2,1] = 2*y*z - 2*x*w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
135 r[2,2] = 1 - (2*x**2+2*y**2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
136 return r
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
137
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
138 def rotateAbout(axis, angle):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
139 ax, ay, az = (axis/norm(axis)).A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
140 qx = ax*sin(angle/2.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
141 qy = ay*sin(angle/2.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
142 qz = az*sin(angle/2.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
143 qw = cos(angle/2.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
144 q = mat(array([qx,qy,qz,qw])).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
145 return q
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
146
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
147 def normalizeQuaternion(quat):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
148 x,y,z,w = quat.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
149 magnitude = sqrt(x*x + y*y + z*z + w*w)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
150 x = x / magnitude
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
151 y = y / magnitude
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
152 z = z / magnitude
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
153 w = w / magnitude
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
154 quat[0, 0] = x
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
155 quat[1, 0] = y
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
156 quat[2, 0] = z
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
157 quat[3, 0] = w
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
158 return quat
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
159
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
160 def VTo4x4(V):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
161 v1, v2, v3 = V.A1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
162 return mat(array( \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
163 [[0.0, -v3, v2, -v1], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
164 [ v3, 0.0, -v1, -v2], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
165 [-v2, v1, 0.0, -v3], \
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
166 [v1, v2, v3, 0.0] ]))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
167
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
168 def homogeneous(R, p):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
169 """ Create a H matrix from rotation and position """
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
170 H = mat(eye(4))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
171 H[0:3, 0:3] = R
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
172 H[0:3, 3] = p
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
173 return H
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
174
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
175
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
176 class Simulation:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
177 def simulate(self, endtime, dt):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
178 """ Evaluate the state of the system over a given time """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
179 state = self.initial()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
180 times = np.arange(0.0, endtime, dt)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
181 y0 = state.T.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
182 def fWrapper(y, t):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
183 y = mat(y).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
184 dy = self.rateOfChange(y, t)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
185 return dy.T.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
186 res = scipy.integrate.odeint(fWrapper, y0, times)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
187 states = []
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
188 res = res.T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
189 r,c = res.shape
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
190 for ci in range(0,c):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
191 states.append(mat(res[:,ci]).T)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
192 self.result = states
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
193 return states
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
194
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
195
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
196 class BouncingCube(Simulation):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
197 """ Simulation of a bouncing cube """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
198 def __init__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
199 # Parameters:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
200 self.gravity = mat( array([0,0,-9.81]) ).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
201 self.massI = mat(eye(6)) * 1.01 # Mass matrix
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
202
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
203 def initial(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
204 """ Return initial pose """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
205 PInitial = mat( zeros((6, 1)) )
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
206 posInitial = mat(array([-1.2, -1.3, 2.8])).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
207 quatInitial = rotateAbout(mat(array([0.2, 1.0, 0.4])).T, 0.5)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
208
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
209 # The state vector!
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
210 state = mat(zeros( (13,1) ))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
211 state[0:4, 0] = quatInitial
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
212 state[4:7, 0] = posInitial
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
213 state[7:13, 0] = PInitial
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
214 return state
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
215
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
216 def rateOfChange(self, states, thetime):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
217 """ Calculates rate of change given the current states. """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
218 quat = states[0:4, 0] # Orientation (4)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
219 pos = states[4:7, 0] # Position (3)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
220 P = states[7:13, 0] # Momentum (6)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
221 massI, gravity = self.massI, self.gravity
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
222 # Rigid body parts:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
223 # Forward Kinematic chain:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
224 H = homogeneous(quatToR(quat), pos) # Forward kinematics
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
225
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
226 AdjX2 = mat(eye(6)) # The connectionpoint in the real world
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
227 adjAdjX2_1 = adjoint(AdjX2[0:6,0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
228 adjAdjX2_2 = adjoint(AdjX2[0:6,1])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
229 adjAdjX2_3 = adjoint(AdjX2[0:6,2])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
230 adjAdjX2_4 = adjoint(AdjX2[0:6,3])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
231 adjAdjX2_5 = adjoint(AdjX2[0:6,4])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
232 adjAdjX2_6 = adjoint(AdjX2[0:6,5])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
233 AdjInv2 = Adjoint(H.I)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
234 M2 = AdjInv2.T * (massI * AdjInv2) # Transfor mass to base frame
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
235 MassMatrix = M2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
236
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
237 wrenchGrav2 = mat( zeros((1,6)) )
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
238 wrenchGrav2[0, 0:3] = -cross(gravity, pos).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
239 wrenchGrav2[0, 3:6] = gravity.T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
240
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
241 Bk = mat( zeros( (6,6) ))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
242 Bk[0:3, 0:3] = skew(P[0:3, 0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
243 Bk[3:6, 0:3] = skew(P[3:6, 0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
244 Bk[0:3, 3:6] = skew(P[3:6, 0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
245
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
246 # TODO: do this a cholesky:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
247 v = np.linalg.solve(MassMatrix, P) # Matrix inverse like thingy !
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
248
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
249 T2_00 = v # Calculate the relative twist!
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
250 TM2 = T2_00.T * M2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
251 twistExternal = T2_00 # Twist van het blokje
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
252 TMSum2 = TM2
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
253
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
254 PDotBodies = mat( zeros( (6,1)) )
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
255 PDotBodies[0,0] = TMSum2 * (adjAdjX2_1 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
256 PDotBodies[1,0] = TMSum2 * (adjAdjX2_2 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
257 PDotBodies[2,0] = TMSum2 * (adjAdjX2_3 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
258 PDotBodies[3,0] = TMSum2 * (adjAdjX2_4 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
259 PDotBodies[4,0] = TMSum2 * (adjAdjX2_5 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
260 PDotBodies[5,0] = TMSum2 * (adjAdjX2_6 * T2_00)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
261
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
262 PDot = -PDotBodies - Bk * v
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
263 PDot += wrenchGrav2.T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
264
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
265 ##### Contact wrench part:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
266 HB_W = H # Is H-matrix van het blokje
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
267 WrenchB = mat(zeros( (1,6) ))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
268 for px in [-0.5, 0.5]:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
269 for py in [-0.5, 0.5]:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
270 for pz in [-0.5, 0.5]:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
271 HB1_B = homogeneous(mat(eye(3)), mat([px,py,pz]).T)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
272 HB1_W = HB_W * HB1_B
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
273 HW1_W = homogeneous(mat(eye(3)), HB1_W[0:3,3])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
274 HW_W1 = HW1_W.I
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
275 HB_W1 = HW_W1 * HB_W
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
276
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
277 AdjHB_W1 = Adjoint(HB_W1)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
278 TB_W1_W1 = AdjHB_W1 * twistExternal
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
279 z = HB1_W[2, 3]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
280 vx, vy, vz = TB_W1_W1[3:6, 0].A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
281 if True:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
282 # Contact forces:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
283 Fx = 0 #np.exp(-5.0*vx)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
284 Fy = 0 # np.exp(-5.0*vy)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
285 Fz = np.exp(-z*10.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
286 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
287 Fx = 0.0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
288 Fy = 0.0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
289 Fz = 0.0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
290 # TODO: reflect impulse
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
291 WrenchW1 = mat([0,0,0,0,0,Fz])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
292 # Transform it back:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
293 WrenchB += (AdjHB_W1.T * WrenchW1.T).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
294 ##### End of contact wrench
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
295
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
296 PDot += (WrenchB * AdjInv2).T
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
297
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
298 # Position and orientation rates:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
299 QOmega = VTo4x4(v[0:3, 0])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
300 quatDot = 0.5 * QOmega * quat
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
301 vel = v[3:6, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
302 posDot = skew(v[0:3]) * pos + vel
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
303 # The rate vector:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
304 rates = mat(zeros( (13,1) ))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
305 rates[0:4, 0] = quatDot
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
306 rates[4:7, 0] = posDot
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
307 rates[7:13, 0] = PDot
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
308 return rates
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
309
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
310 def draw(self, state):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
311 Dicequat = state[0:4, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
312 Dicepos = state[4:7, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
313
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
314 x, y, z = Dicepos.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
315 R = quatToR(Dicequat)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
316
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
317 glTranslatef(x, y, z)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
318 # Trick to rotate the openGL matrix:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
319 r = R.A1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
320 rotR = (r[0], r[3], r[6], 0.0,
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
321 r[1], r[4], r[7], 0.0,
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
322 r[2], r[5], r[8], 0.0,
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
323 0.0, 0.0, 0.0, 1.0)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
324 glMultMatrixd(rotR)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
325
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
326 drawCube(0.6)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
327
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
328
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
329 class RollingPendulum(Simulation):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
330 """ A 2D pendulum with a rolling contact. """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
331 def __init__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
332 self.R = 0.01
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
333 self.L = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
334 self.M = 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
335 self.g = 9.81
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
336
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
337 def initial(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
338 i = mat(zeros((2,1)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
339 i[0, 0] =
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
340 return i
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
341
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
342 def rateOfChange(self, states, thetime):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
343 phi, omega = states[0:2, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
344 rates = mat(zeros((2,1)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
345 return rates
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
346
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
347 def draw(self, state):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
348 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
349 #drawCube(0.6)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
350
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
351
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
352 class WobbelingWheel(Simulation):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
353 """ A wheel pushed sideways over a surface """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
354 def __init__(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
355 self.r1 = 0.5
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
356 self.r2 = 0.05
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
357
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
358 def initial(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
359 i = mat(zeros((5,1)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
360 return i
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
361
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
362 def rateOfChange(self, states, thetime):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
363 x, y = states[0:2, 0]
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
364 rates = mat(zeros((5,1)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
365 return rates
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
366
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
367 def draw(self, state):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
368 pass
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
369 #drawCube(0.6)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
370
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
371
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
372 class W(QGLWidget):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
373 time = 0.0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
374 index = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
375 def __init__(self, sim):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
376 super().__init__()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
377 self.firstRun = True
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
378 self.savePNGS = False
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
379 self.index = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
380 self.sim = sim
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
381 self.resize(500,500)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
382 self.UP()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
383 t = QTimer(self)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
384 t.timeout.connect(self.UP)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
385 t.start(40)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
386
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
387 def UP(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
388 if self.sim.result:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
389 if self.index + 1 < len(self.sim.result):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
390 self.index += 1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
391 else:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
392 self.index = 0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
393 # Paint:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
394 self.update()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
395
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
396 if self.firstRun:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
397 # Create png images for the movie:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
398 if self.savePNGS:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
399 pm = self.renderPixmap()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
400 pm.save('image'+str(self.index)+'.png')
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
401
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
402 def initializeGL(self):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
403 glClearColor(0.0, 0.5, 0.0, 1.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
404 glEnable(GL_DEPTH_TEST)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
405 glDepthFunc(GL_LESS)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
406 glShadeModel(GL_SMOOTH)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
407
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
408 def resizeGL(self,w,h):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
409 glViewport(0, 0, w, h)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
410 glMatrixMode(GL_PROJECTION)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
411 glLoadIdentity()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
412 gluPerspective(45.0, float(w)/float(h), 0.1, 100.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
413 glMatrixMode(GL_MODELVIEW)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
414
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
415 def paintGL(self):
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
416 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear buffers
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
417 glLoadIdentity() # Reset The View
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
418 glTranslatef(0.0,-2.0,-10.0) # Move Left And Into The Screen
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
419 glRotatef(-90.0, 1.0, 0.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
420 drawFloor(2.0, 0.0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
421 drawAxis()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
422 self.renderText(1.0, 0.0, 0.0, 'X')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
423 self.renderText(0.0, 1.0, 0.0, 'Y')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
424 self.renderText(0.0, 0.0, 1.0, 'Z')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
425 self.renderText(0.0,0.0,1.2,str(self.time))
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
426 if self.sim.result:
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
427 self.sim.draw(self.sim.result[self.index])
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
428
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
429
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
430 et = 1.5
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
431 dt = 0.04
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
432 # sim = BouncingCube()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
433 sim = WobbelingWheel()
243
ef683881c64e Remove various files
Windel Bouwman
parents: 95
diff changeset
434 print('starting integration... endtime =', et, ' stepsize =', dt)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
435 t0 = time.time()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
436 states = sim.simulate(et, dt)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
437 t1 = time.time()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
438 print('That was heavy, it took me ', t1 -t0, ' seconds!')
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
439 app = QApplication(sys.argv)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
440 w = W(sim)
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
441 w.show()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
442 sys.exit(app.exec_())
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
443