-
Notifications
You must be signed in to change notification settings - Fork 1
/
t_sim.py
124 lines (93 loc) · 5.66 KB
/
t_sim.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
from ursina import *
from PIL import Image
import utils
import parameters
import set_up_state
from typing import List, Dict
from set_up_state import Visualizer
##some settings
class Simulation(Entity):
def __init__(self):
super().__init__()
self.currentTick = 0
self.currentTickDistance = 0
self.zoffset = 2
self.visualisers = []
def create_occluder(self,tex):
#occluder params texture is an IMAGE instance not TEXTURE
occluder = Entity(model='plane', texture=Texture(tex), position=Vec3(0,self.zoffset,0))
return occluder
def create_visualisers(self, visuals):
l = []
for i, v in enumerate(visuals, start=1):
spacing = 4/len(visuals) #TODO - The length is distorted from the value in parameters - not sure what a good fix is
if i != len(visuals):
tex = Texture(Image.new(mode="RGBA", size=(parameters.Instance.lowResolution, parameters.Instance.lowResolution), color=(255,0,0,100)))
plane = Entity(model='plane', texture=tex, position=(0,self.zoffset-i*spacing,0))
else: #special case for final visualiser AKA detector
tex = Texture(Image.new(mode="RGBA", size=(parameters.Instance.lowResolution, parameters.Instance.lowResolution), color=(0,0,0,255)))
plane = Entity(model='plane', texture=tex, position=(0,self.zoffset-i*spacing,0))
l.append(plane)
return l
def apply_pixels(visualisers, self):
pass
visgroup: List[Entity] = []
occluder = None
def begin(self):
#get initialised planes
#self.visualisers = set_up_state.setUpTimeState(parameters.Instance, cache=1, usecache=0)
#self.occluder = self.create_occluder(parameters.Instance.occluder)
#self.visgroup += (self.create_visualisers(self.visualisers))
#DEBUG UV SQUARE
#res = parameters.Instance.lowResolution
#uvtex = Texture(Image.new(mode="RGBA", size=(res,res), color=(255,0,0,255)))
#uvtex.default_filtering = None
#uv = Entity(model='plane', texture=uvtex, position=(0,-3,0)) # set a PIL texture
#for x in range (0, uv.texture.width):
# for y in range (0, uv.texture.height):
# uv.texture.set_pixel(x, y, rgb(x*255/res, y*255/res,0))
#uv.texture.apply()
#From old slower way
#self.visualisers = set_up_state.setUpTimeState(parameters.Instance)
#For newer faster way
self.tempTuple = set_up_state.modifiedSetUpTimeState(parameters.Instance)
self.planesToAddOverTime:List[List[Dict[Vec2,Vec2]]] = self.tempTuple[0]
self.visualisers:List[Visualizer] = self.tempTuple[1]
self.lastTick = self.tempTuple[2]
self.occluder = self.create_occluder(parameters.Instance.occluder)
self.visgroup += (self.create_visualisers(self.visualisers))
print("begun")
#Logic for this code once it's cleaned up
#update every pixel of every visualizer to add any waves that have reached it
def update(self):
if math.ceil(self.currentTickDistance / parameters.Instance.tick_distance) <= self.lastTick:
t = time.perf_counter()
print(f"update frame {self.currentTick} of {self.lastTick}")
for i, visualizer in enumerate(self.visualisers):
currentVisualizerPlaneToAdd = self.planesToAddOverTime[i] #Putting this here so it doesn't have to do an extra accessing element on a list every time
variable = math.ceil(self.currentTickDistance / parameters.Instance.tick_distance) - 1 #speedy
for visualizerPixel in visualizer.pixels:
#Old slower code from older set up function
#for contribution in visualizerPixel.contributions:
# if (self.currentTickDistance-parameters.Instance.tick_distance) < contribution.dist and contribution.dist <= self.currenttickdistance:
# visualizerPixel.totalContribution += contribution.vec
#Newer faster code for modified set up function
if (currentVisualizerPlaneToAdd[variable] is not None) and (visualizerPixel.coordinates in currentVisualizerPlaneToAdd[variable]): #TODO stop redoing calc over and over again - done
visualizerPixel.totalContribution += currentVisualizerPlaneToAdd[variable][visualizerPixel.coordinates]
'''
[i] - acesses the visualizer
[math.ceil(self.currenttickdistance / parameters.Instance.tick_distance)] - acesses the dictionary for the given distance step
[visualizerPixel.coordinates] - acesses the key that is the position vector of the pixel on the visualizer (the value is the contribution to add for that frame)
'''
#color pixels
v = self.visgroup[i]
b = min(int(utils.length(visualizerPixel.totalContribution)*parameters.Instance.brightnessFactor), 255)
v.texture.set_pixel(int(visualizerPixel.coordinates.x),
int(visualizerPixel.coordinates.y), rgb(b, b, b))
v.texture.apply()
#print(f"{visualizerPixel.coordinates.x} - {visualizerPixel.coordinates.y}")
print(time.perf_counter()-t)
self.currentTick += 1
self.currentTickDistance += parameters.Instance.tick_distance
#Then just need to draw it on the screen now that the pixel values are updated
#