forked from CorticalComputer/DXNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visor.erl
272 lines (252 loc) · 9.3 KB
/
visor.erl
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
%% This source code and work is provided and developed by DXNN Research Group WWW.DXNNResearch.COM
%%
%Copyright (C) 2009 by Gene Sher, DXNN Research Group, CorticalComputer@gmail.com
%All rights reserved.
%
%This code is licensed under the version 3 of the GNU General Public License. Please see the LICENSE file that accompanies this project for the terms of use.
-module(visor).
-include("records.hrl").
-compile(export_all).
-record(state,{window,canvas,update_rate,filter,scape,scape_type}).
-define(WIDTH,1440).
-define(HEIGHT,900).
-define(INIT_SCAPE_TYPE,flatland).
zo()->
visor ! {self(),new_Filter,{0.2,200,200}}.
zo(Filter)->
visor ! {self(),new_Filter,Filter}.
legend()->
visor ! ctlegend_on.
start()->
spawn(visor,loop,[]).
stop()->
visor ! {self(),terminate}.
loop()->
GS = gs:start(),
Window = gs:create(window,GS,[{title,"Visor"},{width,?WIDTH},{height,?HEIGHT}]),
Canvas = gs:create(canvas,Window,[{width,?WIDTH},{height,?HEIGHT}]),
register(visor,self()),
put(canvas,Canvas),
gs:config(Window,{map,true}),
ScapeType = ?INIT_SCAPE_TYPE,
{Scape_PId,ScapeType} = gen_server:call(polis,{get_scape,ScapeType}),
% io:format("Inside Visor:: Scape_PId:~p~n",[Scape_PId]),
gen_server:cast(Scape_PId,{self(),subscribe,Canvas}),
InitState = #state{
window = Window,
canvas = Canvas,
update_rate = 10,
filter = {1.0,0,0},
scape = Scape_PId,
scape_type = ScapeType
},
io:format("Visor started. Scape_PId:~p ScapeType:~p~n",[Scape_PId,ScapeType]),
loop(InitState).
loop(S)->
Scape_PId = S#state.scape,
receive
{From,new_Filter,NewFilter}->
visor:loop(S#state{filter=NewFilter});
{From,new_UpdateRate,NewUpdateRate}->
visor:loop(S#state{update_rate=NewUpdateRate});
{From,change_scape,ScapeType}->
gen_server:cast(S#state.scape,{self(),unsubscribe}),
New_ScapePId = gen_server:call(polis,{get_scape,ScapeType}),
visor:loop(S#state{scape=New_ScapePId});
{Scape_PId,draw_object,Object}->
%io:format("~p~n",[Object]),
Id=draw_object(S#state.canvas,Object),
Scape_PId ! {self(),Id},
visor:loop(S);
{Scape_PId,destroy_object,Id}->
gs:destroy(Id),
visor:loop(S);
ctlegend_on ->
Legend=ct_legend(),
Canvas = S#state.canvas,
io:format("Legend:~p~n",[Legend]),
put(legend,gs:create(text,Canvas,[{fg,red},{coords,[{50,50}]},{text,Legend}])),
visor:loop(S);
ctlegend_off ->
gs:destroy(get(legend)),
erase(legend),
io:format("Legend off.~n"),
visor:loop(S);
{From,terminate}->
done = gen_server:call(S#state.scape,{self(),unsubscribe}),
io:format("Visor unsubscribed from:~p and terminated.~n",[S#state.scape])
after S#state.update_rate ->
%io:format("Visor is refreshing.~n"),
gen_server:cast(S#state.scape,{self(),redraw,S#state.filter}),
visor:loop(S)
end.
draw_avatars(Canvas,[Avatar|Avatars],Acc)->
U_Avatar = draw_avatar(Canvas,Avatar),
draw_avatars(Canvas,Avatars,[U_Avatar|Acc]);
draw_avatars(Canvas,[],Acc)->
Acc.
draw_avatar(Canvas,Avatar)->
Objects = Avatar#avatar.objects,
U_Objects = draw_objects(Canvas,Objects,[]),
%draw_stats(Canvas,Avatar),
Avatar#avatar{objects=U_Objects}.
draw_objects(Canvas,[skip|Objects],Acc)->
draw_objects(Canvas,Objects,Acc);
draw_objects(Canvas,[Object|Objects],Acc)->
{ObjName,_IdPlaceHolder,Color,Pivot,Coords,Parameter} = Object,
Id = case ObjName of
circle ->
[{Cx,Cy}] = Coords,
R = Parameter,
Draw_Coords = [{Cx-R,Cy-R},{Cx+R,Cy+R}],
gs:create(oval,Canvas,[{coords,Draw_Coords},{fill,Color},{fg,Color}]);
arrow ->
gs:create(line,Canvas,[{coords,Coords},{arrow,last},{fg,Color}]);
polygon ->
gs:create(polygon,Canvas,[{coords,Coords},{fill,Color},{fg,Color}]);
_ ->
gs:create(ObjName,Canvas,[{coords,Coords},{fg,Color}])
end,
%io:format("Coords:~p, Id:~p~n",[Coords,Id]),
U_Object = {ObjName,Id,Color,Pivot,Coords,Parameter},
draw_objects(Canvas,Objects,[U_Object|Acc]);
draw_objects(_Canvas,[],Acc)->
Acc.
draw_object(Canvas,Object)->
{ObjName,undefined,Color,Pivot,Coords,Parameter} = Object,
Id = case ObjName of
circle ->
[{Cx,Cy}] = Coords,
R = Parameter,
Draw_Coords = [{Cx-R,Cy-R},{Cx+R,Cy+R}],
gs:create(oval,Canvas,[{coords,Draw_Coords},{fill,Color},{fg,Color}]);
arrow ->
gs:create(line,Canvas,[{coords,Coords},{arrow,last},{fg,Color}]);
_ ->
gs:create(ObjName,Canvas,[{coords,Coords},{fg,Color}])
end.
draw_stats(Canvas,Avatar) when (Avatar#avatar.type == flatlander) or (Avatar#avatar.type == prey)->
%{actuator,Actuator,ActuatorId,Parameters} = Tag, [{CF_PId,CFVL,CFTag}|CF]
CF = Avatar#avatar.actuators,
CT = Avatar#avatar.sensors,
Loc = Avatar#avatar.loc,
%io:format("draw_stats:CF:~p CT:~p Loc:~p~n",[CF,CT,Loc]),
CFStatIds = [gs:create(text,Canvas,[{coords,[Loc]},{text,atom_to_list(Actuator)}]) || {CF_PId,CFVL,{actuator,Actuator,ActuatorId,Parameters}}<-CF],
CTStatIds = [gs:create(text,Canvas,[{coords,[Loc]},{text,atom_to_list(Sensor)}]) || {CT_PId,CTVL,{sensor,Sensor,SensorId,Parameters}}<-CT],
[gs:destroy(Id) || Id<- CFStatIds],
[gs:destroy(Id) || Id<- CTStatIds];
draw_stats(_,_)->
done.
redraw_avatars(Filter,[Avatar|Avatars])->
Objects = Avatar#avatar.objects,
%io:format("Objects:~p~n",[Objects]),
redraw_objects(Filter,Objects),
redraw_avatars(Filter,Avatars);
redraw_avatars(_Filter,[])->
done.
redraw_objects({Zoom,PanX,PanY},[Object|Objects])->
{ObjName,Id,Color,Pivot,Coords,Parameter} = Object,
Draw_Coords =case ObjName of
circle ->
[{Cx,Cy}] = Coords,
R = Parameter,
[{Cx-R,Cy-R},{Cx+R,Cy+R}];
_ ->
Coords
end,
Filtered_Coords = [{X*Zoom+PanX,Y*Zoom+PanY} || {X,Y} <- Draw_Coords],
if
(ObjName == circle) -> gs:config(Id,[{coords,Filtered_Coords},{fill,Color},{fg,Color}]);
true -> gs:config(Id,[{coords,Filtered_Coords},{fg,Color}])
end,
%gs:config(Id,[{coords,Filtered_Coords},{fg,Color}]),
%io:format("Coords:~p, Id:~p~n",[Coords,Id]),
redraw_objects({Zoom,PanX,PanY},Objects);
redraw_objects(_Filter,[])->
done.
%color_scaner(Op,{Zoom,PanX,PanY},Density,Spread,Loc,Direction,Avatars)
redraw_SensorVisualization(Filter,[Avatar|Avatars],AllAvatars)->
sensors:color_scaner(draw,Filter,45,math:pi()*2,Avatar#avatar.loc,Avatar#avatar.direction,AllAvatars--[Avatar]),
redraw_SensorVisualization(Filter,Avatars,AllAvatars);
redraw_SensorVisualization(_Filter,[],_AllAvatars)->
done.
ct2color(Sensors)->
io:format("Sensors:~p~n",[Sensors]),
SensorNames = [Name ||{sensor,Name,SensorId,VL,Parameters}<-Sensors],
SensorTuple = count_SensorTypes(SensorNames,{0,0,0,0}),
case SensorTuple of
{_,0,0,0}-> red;%prey_sensor
{0,_,0,0}-> blue;%plant_sensor
{_,_,0,0}-> white;%prey_sensor and plant_sensor
{0,_,_,0}-> cyan; %plant_sensor, predator_sensor
{_,0,_,0}-> grey; %prey_sensor, predator_sensor
{_,_,_,_}-> black %prey_sensor, plant_sensor, predator_sensor
end.
%[coned_prey_sensor,coned_plant_sensor,coned_flatlander_sensor]]
count_SensorTypes([Sensor|Sensors],{PreySensors,PlantSensors,FlatlanderSensors,OtherSensors})->
case Sensor of
coned_prey_sensor->count_SensorTypes(Sensors,{PreySensors+1,PlantSensors,FlatlanderSensors,OtherSensors});
coned_plant_sensor->count_SensorTypes(Sensors,{PreySensors,PlantSensors+1,FlatlanderSensors,OtherSensors});
coned_flatlander_sensor->count_SensorTypes(Sensors,{PreySensors,PlantSensors,FlatlanderSensors+1,OtherSensors});
_-> count_SensorTypes(Sensors,{PreySensors,PlantSensors,FlatlanderSensors,OtherSensors+1})
end;
count_SensorTypes([],Tuple)->
Tuple.
ct_legend()->
["Red -> prey_sensor\n",
"Blue -> plant_sensor\n",
"White -> prey_sensor & plant_sensor\n",
"Cyan -> plant_sensor & predator_sensor\n",
"Grey -> prey_sensor & predator_sensor\n",
"Black -> prey_sensor & plant_sensor & predator_sensor\n"].
check_pivot(Avatar)->
Objects = Avatar#avatar.objects,
[{Pivot,Coords}] = [{Pivot,Coords} || {arrow,Id,Color,Pivot,Coords,Parameter}<-Objects],
case length([1||Coord<-Coords, Pivot==Coord])of
1 ->
io:format("hit~n");
0 ->
io:format("mis~n")
end.
t()-> spawn(visor,test,[]).
test()->
GS = gs:start(),
Window = gs:create(window,GS,[{title,"Visor"},{width,1400},{height,900}]),
Canvas = gs:create(canvas,Window,[{width,1400},{height,900}]),
register(test,self()),
put(canvas,Canvas),
gs:config(Window,{map,true}),
CT = [{self(),4,{sensor,coned_plant_sensor,id,[4]}}],
Avatars = [scape:create_avatar(flatlander,self(),self(),{cf,CT,1},void) || _<- lists:duplicate(20,1)],
U_Avatars = draw_avatars(Canvas,Avatars,[]),
test(U_Avatars,Window,Canvas,100000,random).
test(_,_,_,0,_)->
done;
test(Avatars,Window,Canvas,Step,Mode)->
timer:sleep(4),
U_Avatars = case Mode of
manual ->
receive
{move, Speed}->
[scape:move(Avatar,Speed) || Avatar <- Avatars];
{rotate,Angle}->
[scape:rotate(Avatar,Angle) || Avatar <- Avatars]
after 4 ->
Avatars
end;
random ->
case random:uniform(2) of
1 ->%io:format("move~n"),
[scape:move(Avatar,random:uniform()) || Avatar <- Avatars];
2 ->%io:format("rotate~n"),
[scape:rotate(Avatar,(random:uniform()-0.5)/2*math:pi()) || Avatar <- Avatars]
end
end,
%check_pivot(U_Avatar),
Zoom = 1.0,
PanX = Zoom*0,
PanY = Zoom*0,
Filter = {Zoom,PanX,PanY},
redraw_avatars(Filter,U_Avatars),
redraw_SensorVisualization(Filter,U_Avatars,U_Avatars),
visor:test(U_Avatars,Window,Canvas,Step-1,Mode).