-
Notifications
You must be signed in to change notification settings - Fork 1
/
preycap_model.py~
executable file
·241 lines (215 loc) · 9.36 KB
/
preycap_model.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
import sys
from pandac.PandaModules import WindowProperties
import pandac.PandaModules
# from panda3d.core import Shader
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
import numpy as np
from scipy.ndimage.filters import gaussian_filter
pandac.PandaModules.loadPrcFileData("", """
fullscreen 1
load-display pandagl
win-origin 0 0
undecorated 1
win-size 1920 1080
sync-video 1
""")
class MyApp(ShowBase):
def __init__(self):
simulation = True
if not simulation:
para_cont_window = np.load('para_continuity_window.npy')
para_cont_window = int(para_cont_window)
print para_cont_window
para_positions = np.load('3D_paracoords.npy')[:, para_cont_window:]
fish_position = np.load('ufish_origin.npy')
fish_orientation = np.load('ufish.npy')
else:
para_positions = np.load('para_simulation.npy')
fish_position = np.load('origin_model.npy')
fish_orientation = np.load('uf_model.npy')
end_fp = [fish_position[-1] for i in range(60)]
end_fo = [fish_orientation[-1] for i in range(60)]
fish_position = np.concatenate((fish_position, end_fp))
fish_orientation = np.concatenate((fish_orientation, end_fo))
self.numpara = para_positions.shape[0]
self.numframes = para_positions.shape[1]
self.para_positions = para_positions
dfx = gaussian_filter([x[0] for x in fish_position], 1)
dfy = gaussian_filter([y[1] for y in fish_position], 1)
dfz = gaussian_filter([z[2] for z in fish_position], 1)
fish_position_filt = np.array(
[[x, y, z] for x, y, z in zip(dfx, dfy, dfz)])
self.fish_position = fish_position_filt
fox = gaussian_filter([x[0] for x in fish_orientation], 1)
foy = gaussian_filter([y[1] for y in fish_orientation], 1)
foz = gaussian_filter([z[2] for z in fish_orientation], 1)
fish_orientation_filt = np.array(
[[x, y, z] for x, y, z in zip(fox, foy, foz)])
self.fish_orientation = fish_orientation_filt
print fish_orientation.shape
print fish_position.shape
print para_positions.shape
print('numframes')
print self.numframes
ShowBase.__init__(self)
self.accept("escape", sys.exit)
props = WindowProperties()
props.setCursorHidden(False)
props.setMouseMode(WindowProperties.M_absolute)
self.win.requestProperties(props)
self.lens1 = pandac.PandaModules.PerspectiveLens()
self.lens1.setFov(90, 90)
self.lens1.setNearFar(.1, 10000)
# self.lens1.setAspectRatio(1920/1080.)
self.lens1.setAspectRatio(1280/800.)
self.cam.node().setLens(self.lens1)
pivot = render.attachNewNode("pivot")
# pivot.setPos(-1200, -1200, 944)
pivot.setPos(3000, 3000, 944)
self.cam.reparentTo(pivot)
# self.cam.setH(100)
# self.cam.setPos(-450, 944, 944)
self.setBackgroundColor(1, 1, 1, 1)
# Some Lines That Define Tank Boundaries
self.d2 = pandac.PandaModules.LineSegs()
self.d2.setColor(.5, .5, .5, 1)
self.d2.setThickness(2)
self.d2.moveTo(0, 0, 0)
self.d2.drawTo(1888, 0, 0)
self.d2.moveTo(0, 0, 0)
self.d2.drawTo(0, 0, 1888)
self.d2.moveTo(0, 0, 0)
self.d2.drawTo(0, 1888, 0)
self.d2.moveTo(1888, 1888, 0)
self.d2.drawTo(1888, 1888, 1888)
self.d2.moveTo(0, 1888, 1888)
self.d2.drawTo(1888, 1888, 1888)
self.d2.moveTo(1888, 0, 1888)
self.d2.drawTo(1888, 1888, 1888)
self.d2.moveTo(1888, 0, 0)
self.d2.drawTo(1888, 1888, 0)
self.d2.moveTo(1888, 0, 0)
self.d2.drawTo(1888, 0, 1888)
self.d2.moveTo(0, 1888, 0)
self.d2.drawTo(1888, 1888, 0)
self.d2.moveTo(0, 1888, 0)
self.d2.drawTo(0, 1888, 1888)
self.d2.moveTo(0, 0, 1888)
self.d2.drawTo(0, 1888, 1888)
self.d2.moveTo(0, 0, 1888)
self.d2.drawTo(1888, 0, 1888)
self.reference = self.loader.loadModel("sphere-highpoly")
self.reference.reparentTo(self.render)
self.reference.setScale(.01, .01, .01)
self.reference.setColor(1, 1, 1)
self.reference.setPos(944, 944, 944)
self.cam.lookAt(self.reference)
drawtank = True
scale = 1888
if drawtank:
self.tank = self.loader.loadModel("rgbCube.egg")
self.tank.reparentTo(self.render)
self.tank.setScale(scale, scale, scale)
self.tank.setTransparency(1)
self.tank.setAlphaScale(0.2)
self.tank.setColor(.3, .6, .9)
self.tank.setPos(scale / 2, scale / 2, scale / 2)
# #
geom2 = self.d2.create()
self.nodegeom2 = self.render.attachNewNode(geom2)
# Load the environment model.
self.fishcone = self.loader.loadModel("Spotlight.egg")
self.fishcone.setTexture(self.loader.loadTexture("white.png"), 1)
self.fishcone.reparentTo(self.render)
self.fishcone.setPos(0, 0, 0)
self.fishcone.setScale(10, 10, 10)
self.fishcone.setTransparency(1)
self.fishcone.setAlphaScale(.5)
self.fishcone.setColor(0, 0, 1)
''' These three lines make sure this is drawn before the tank.
If you don't do this, tank blocks out the fishcone.'''
self.fishcone.setBin("fixed", 0)
self.fishcone.setDepthTest(False)
self.fishcone.setDepthWrite(False)
self.fishcone.show()
self.spheres = dict({})
for i in range(int(self.numpara/3)):
self.spheres[i] = self.loader.loadModel("sphere.egg")
# self.spheres[i] = Actor("models/panda-model",
# {"walk": "models/panda-walk4"})
self.spheres[i].reparentTo(self.render)
self.spheres[i].setScale(15, 15, 15)
self.spheres[i].setColor(.25, .25, .25)
# text = pandac.PandaModules.TextNode('node name')
# text.setText(' ' + str(i))
# textNodePath = self.spheres[i].attachNewNode(text)
# textNodePath.setScale(10)
# textNodePath.setTwoSided(True)
# textNodePath.setPos(-10, 0, 0)
# textNodePath.setHpr(180, 0, 0)
# self.sphere_fish = self.loader.loadModel("sphere-highpoly.egg")
self.sphere_fish = self.loader.loadModel("sphere.egg")
self.sphere_fish.reparentTo(self.render)
self.sphere_fish.setScale(35, 35, 35)
self.sphere_fish.setColor(1, 0, 0)
self.sphere_fish.setTransparency(0)
self.sphere_fish.setAlphaScale(.9)
self.fish_uvec = self.loader.loadModel("sphere-highpoly")
self.fish_uvec.reparentTo(self.render)
self.fish_uvec.setScale(.01, .01, .01)
self.fish_uvec.setColor(1, 1, 1)
# Add the spinCameraTask procedure to the task manager.
self.iteration = 0
self.taskMgr.add(self.movepara, "movepara")
# Define a procedure to move the camera.
def movepara(self, task):
strike = False
curr_frame = np.floor(self.iteration / 2).astype(np.int)
# curr_frame = self.iteration
para_positions = self.para_positions[:, curr_frame]
fish_position = self.fish_position[curr_frame]
fish_orientation = self.fish_orientation[curr_frame]
for i in np.arange(0, self.numpara, 3):
x = para_positions[i]
y = para_positions[i+1]
z = para_positions[i+2]
if not np.isnan(x) and not np.isnan(y) and not np.isnan(z):
self.spheres[i/3].show()
self.spheres[i/3].setPos(x, y, z)
else:
self.spheres[i/3].hide()
strike = True
x_fish = fish_position[0]
y_fish = fish_position[1]
z_fish = fish_position[2]
correction = 100
correction_x = fish_orientation[0]*correction
correction_y = fish_orientation[1]*correction
correction_z = fish_orientation[2]*correction
ux = fish_orientation[0]*500
uy = fish_orientation[1]*500
uz = fish_orientation[2]*500
for i in range(int(self.numpara/3)):
self.spheres[i].lookAt(self.sphere_fish)
self.sphere_fish.setPos(x_fish, y_fish, z_fish)
if strike:
text = pandac.PandaModules.TextNode('node name')
text.setText('STRIKE')
textNodePath = self.sphere_fish.attachNewNode(text)
textNodePath.setScale(3)
textNodePath.setTwoSided(True)
textNodePath.setPos(-15, 0, 0)
textNodePath.setHpr(120, 0, 0)
self.fish_uvec.setPos(x_fish - ux, y_fish - uy, z_fish - uz)
self.fishcone.setPos(x_fish + correction_x,
y_fish + correction_y,
z_fish + correction_z)
self.fishcone.lookAt(self.fish_uvec)
if self.iteration == self.numframes:
self.iteration = 0
else:
self.iteration += 1
return Task.cont
app = MyApp()
app.run()