-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_grid.py
299 lines (242 loc) · 8.24 KB
/
main_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
from visual import *
from math import *
import time
#######################################
# #
# Author: Mads Ynddal #
# All Rights Reserved 2012 #
# #
#######################################
#######################################
#
# Settings - Start
#
#Wireframe: 1 wire, 2 faces, 3 both
wireframe=2
#Floor: 1 on, 0 off
Floor=0
#Window setup:
Width=800 #Width of window
Height=750 #Height of window
Center=(0,70,0) #Point for the camera to look
AOI=(25,40,1) #Area of Interest, for the camera to cover
Backcolor=(.5,.5,.5) #Background color
FOV=pi/9.0 #Field of view in radians
Allowspin=1 #Allow or disallow spinning the scene
Scrresx=1280 #input screen resolution
#Animation
D2=0 #2D True/False
Sides=20 #Number of sides on 3D model to render
Definition=20 #Number of rows vertically to render
#Debug
Debug=0 #If enabled a wireframe and faces scene will de drawn
FPS=60
strength_as_color=1
debug_vectors1=0
debug_vectors2=0
debug_vectors3=0
debug_vectors3_multiplier=10**6
#
# Settings - End
#
#######################################
# Building scenes
scene = display(title='Helmholtz coin',width=Width, height=Height,autoscale = False,scale=(0.03,0.03,0.03))
#scene1 = display(title='Solid of Revolution',width=Width, height=Height,center=Center, background=Backcolor,x=Scrresx-Width,y=0,fov=FOV,userspin=Allowspin)
coil1=curve(pos=[])
coil2=curve(pos=[])
vectors=[]
col=0
debug_offset=0
Vp=4*pi*10**-7
I=1
constant=Vp*I/(4*pi)
d=1
#P=[7.5,1*10**-10,1*10**-10]
#P=[0,3,3]
P=[]
#OP=P
coiloffset=-15./2 #Distance from each coil divided by two
#dm=2*pi
dm=15*8*2*pi #Definitionsmængden for funktionen (rundes op)
defi=1. #punkter pr. dm
grid_sizex=12
grid_sizey=3 #3
grid_sizez=12
max_blen=20
hue_multiplier=1./3
for tt in range(int(dm*defi)+1): #+1 da den sidste mangler
#her tages 1 ad gangen, i udregningener tages 2...
t=tt/defi
#print t
x=t*0.0005+15./2+debug_offset
y=31.5/2*sin(t)
z=31.5/2*cos(t)
coil1.append((x,y,z))
x=t*0.0005-15./2-debug_offset
y=31.5/2*sin(t)
z=31.5/2*cos(t)
coil2.append((x,y,z))
ball = sphere (pos=(0,0,0), radius=1, color=color.red, opacity=0.5)
OP_ref_spacing=4
def vlen(a):
return sqrt(a[0]**2+a[1]**2+a[2]**2)
#Vector length
def vsub(a,b):
return [a[0]-b[0],a[1]-b[1],a[2]-b[2]]
#Substract vectors a,b
def vadd(a,b):
return [a[0]+b[0],a[1]+b[1],a[2]+b[2]]
#Add vectors a,b
def vdiv(a,b):
return [a[0]/float(b),a[1]/float(b),a[2]/float(b)]
#Divide vector by scalar b
def cprod(a,b):
return [a[1]*b[2]-a[2]*b[1],
a[2]*b[0]-a[0]*b[2],
a[0]*b[1]-a[1]*b[0]]
#Cross product
def dlxrr3(dl,r):
return vdiv(cprod(dl,r),vlen(r)**3)
def s1s2(t1):
s1=[t1*0.0005+coiloffset,31.5/2*sin(t1),31.5/2*cos(t1)]
t2=t1+1/float(defi)
#print t1,t2
s2=[t2*0.0005+coiloffset,31.5/2*sin(t2),31.5/2*cos(t2)]
#sleep(4)
return s1,s2
def inte(OP):
global coiloffset,col
Bsum1=[0,0,0]
Bsum2=[0,0,0]
#Første spole
coiloffset*=-1
for tt in range(int(dm*defi)):
t=tt/float(defi)
s1,s2=s1s2(t)
dl=vsub(s2,s1)
#print "vlen:",vlen(dl) - Tjek
m=vdiv(vadd(s1,s2),2) #S1-S2 eller S1+S2 ???
r=vsub(OP,m)
#print "t",t,"s1",s1,"s2",s2,"dl",dl,"m",m,"r",r
if debug_vectors1==1:
if col==0:
vectors.append(curve(color=color.red,pos= [
(0,0,0),
(m[0],m[1],m[2]),
(r[0]+m[0],r[1]+m[1],r[2]+m[2]),
(0,0,0)
]))
col=1
else:
vectors.append(curve(color=color.green,pos= [
(0,0,0),
(m[0],m[1],m[2]),
(r[0]+m[0],r[1]+m[1],r[2]+m[2]),
(0,0,0)
]))
col=0
#a=raw_input()
Bsum1=vadd(Bsum1,dlxrr3(dl,r))
#sleep(4)
coiloffset*=-1
#Anden spole
for tt in range(int(dm*defi)):
t=tt/float(defi)
s1,s2=s1s2(t)
dl=vsub(s2,s1)
m=vdiv(vadd(s1,s2),2)
r=vsub(OP,m)
if debug_vectors2==1:
if col==0:
vectors.append(curve(color=color.red,pos= [
(0,0,0),
(m[0],m[1],m[2]),
(r[0]+m[0],r[1]+m[1],r[2]+m[2]),
(0,0,0)
]))
col=1
else:
vectors.append(curve(color=color.green,pos= [
(0,0,0),
(m[0],m[1],m[2]),
(r[0]+m[0],r[1]+m[1],r[2]+m[2]),
(0,0,0)
]))
col=0
Bsum2=vadd(Bsum2,dlxrr3(dl,r))
return Bsum1,Bsum2
def Apply_contant(Bsum1,Bsum2):
Bsum=vdiv(vadd(Bsum1,Bsum2),1/constant)
#Bsum=vdiv(vsub(Bsum1,Bsum2),1/constant)
Bsum1=vdiv(Bsum1,1/constant)
Bsum2=vdiv(Bsum2,1/constant)
return Bsum
P=[]
Bsum=[]
#rate(FPS)
time_stamp=time.time()
for xx in range(grid_sizex):
#print str(xx+1)+"/"+str(grid_sizex),"\t",time.time()-time_stamp
#time_stamp=time.time()
for yy in range(grid_sizey):
for zz in range(grid_sizez):
P=[xx*OP_ref_spacing-((grid_sizex-1)*OP_ref_spacing)/2,yy*OP_ref_spacing-((grid_sizey-1)*OP_ref_spacing)/2,zz*OP_ref_spacing-((grid_sizez-1)*OP_ref_spacing)/2]
n=xx+yy+zz
#print n
#Bsum.append()
Bsum=vdiv(Apply_contant(*inte(P)),1./debug_vectors3_multiplier)
Blen=vlen(Bsum)
if strength_as_color==1:
Blen=vlen(Bsum)
#print (Blen*1./(max_blen/2)*hue_multiplier)
vcolor=color.hsv_to_rgb((1./4-(Blen*1./(max_blen/2)*hue_multiplier),1,1))
Bsum=vdiv(Bsum,Blen)
else:
vcolor=color.red
if Blen<max_blen:
curve(color=vcolor,pos=[(P[0],P[1],P[2]),(P[0]+Bsum[0],P[1]+Bsum[1],P[2]+Bsum[2])])
#if i/FPS>pi:
#for n in range(number_of_refs):
# OP_ref[n].opacity=0.15
print time.time()-time_stamp
print "done"
#print scene.forward
scene.forward=(0,-1,0)
particle = sphere (pos=(20,0,-12), radius=1, color=color.green, opacity=0.4)
speed = label() # initially blank text
i=0.
auto_rotate=1
while(1):
i+=1
rate(FPS)
if auto_rotate==1:
scene.forward=(-1*sin(i/FPS/5),-1,-1*cos(i/FPS/5))
#Particle
Bsum=vdiv(Apply_contant(*inte(particle.pos)),1./debug_vectors3_multiplier)
particle.pos.x+=Bsum[0]/30.
particle.pos.y+=Bsum[1]/30.
particle.pos.z+=Bsum[2]/30.
speed.pos=particle.pos
speed.pos.x+=4
speed.text=str(vlen(Bsum))[0:3]
#Particle
if scene.kb.keys: # is there an event waiting to be processed?
c = scene.kb.getkey() # obtain keyboard information
if c=="r":
particle.pos=(20,0,-12)
if c=="w":
auto_rotate=0
scene.forward=(0,-1,0)
if c=="s":
auto_rotate=0
scene.forward=(-1,0,0)
if c=="a":
auto_rotate=0
scene.forward=(0,0,-1)
if c=="d":
if auto_rotate==0:
auto_rotate=1
else:
auto_rotate=0