% 函数populate - 分配占用并随机填充
% pedestrins, doors and obstacles
% Occupied的初始化
% 表面上将Doors定义为nDoors行2列的矩阵,实际上Doors是宽度行2列的矩阵。
% 例如,只有一个宽度为7的横向的门,则Doors是7行2列的矩阵,每一行对应于门的一个cell。
% 障碍物的 Occupied 好像设置的是100
function [Occupied, Doors, lengths, Obstacles, population] = populate(Density, gridDims, paramsDoors, paramsObstacles)
n = gridDims(1); % gridDims[50,50]
m = gridDims(2); % 即n = 50,m = 50.
nDoors = paramsDoors(1); % nDoors = 2
minDoorLength = paramsDoors(2);
maxDoorLength = paramsDoors(3);
nObstacles = paramsObstacles(1);
minObstacleLength = paramsObstacles(2);
maxObstacleLength = paramsObstacles(3);
% 已知Density = 0.3
population = floor(Density * n* m); % floor函数是取小于或等于该元素的最接近整数。
Occupied = zeros(n, m); % 将Occupied网格初始化为n*m的全为0的网格
populated = 0;
% 边界是被墙体占据的,绘制四面墙体
Occupied(1,:) = 1;
Occupied(n,:) = 1;
Occupied(:,1) = 1;
Occupied(:,m) = 1;
%预先分配速度
%门的宽度在min和max之间随机取值
lengths = zeros(nDoors, 1); % 几个门就几行一列的矩阵
for i = 1:nDoors
lengths(i) = randi(maxDoorLength-minDoorLength+1) + minDoorLength - 1;
end % 给两个门的宽度任意各取一个在min=5和max=10之间的随机值
%障碍物的宽度也是在min和max之间随机取值
obstaclesLengths = zeros(nObstacles, 1);
for i = 1: nObstacles
obstaclesLengths(i) = randi(maxObstacleLength-minObstacleLength+1) + minObstacleLength - 1;
end
Doors = zeros(size(lengths,1), 2); % size(lengths,1) 即lengths的行数。lengths在这里是两行一列的矩阵。
Obstacles = zeros(size(obstaclesLengths, 1), 2);
% gen random doors
cnt = 1;
for i = 1: nDoors
event1 = rand(1);
event2 = rand(1);
if(event1 <= 0.5) % vertical door
id = randi(m-2 - lengths(i)) + 1; % start y pos 这里的id是[2,48-随机宽度]之间的值
idI = 1; % idI 是什么?
if(event2 > 0.5) % left or right wall
idI = n; % 这是怎么靠这两个event来分左右墙的?
end
for j = 0:lengths(i)-1 % append in right direction
Occupied(idI, id+j) = 0; % 这个地方应该是在设置门这个区域的对应值吧
Doors(cnt,:) = [idI, id+j];
cnt = cnt + 1;
end
else % horizontal door
id = randi(n-2-lengths(i)) + 1; % start x pos
idJ = 1;
if(event2 > 0.5) % bottom or up wall
idJ = m;
end
for j = 0: lengths(i)-1 % append in top direction
Occupied(id+j, idJ) = 0;
Doors(cnt,:) = [id+j, idJ];
cnt = cnt+1;
end
end
end
% 下面这几行是用来固定的门的位置的代码,使用时需将原本的随机生成门的代码注释掉
% Doors=[23,1;24,1;25,1;26,1;27,1;28,1;23,50;24,50;25,50;26,50;27,50;28,50];
% for i=1:size(Doors)
% Occupied(Doors(i,1),Doors(i,2))=0;
% end
%
% lengths=[6;6];
disp('门的坐标')
disp(Doors)
% gen random obstacles
cnt = 1;
for i = 1: nObstacles
event1 = rand(1);
if(event1 <= 0.33) % vertical obsracle
idJ = randi(m-4 - obstaclesLengths(i)) + 2; % not to obstruct the door
idI = randi(n-4) + 2; % not to obstract the door
for j = 0:obstaclesLengths(i)-1
if(Occupied(idI, idJ+j) ~= 0)
break;
end
Occupied(idI, idJ+j) = 100;
Obstacles(cnt,:) = [idI, idJ+j];
cnt = cnt + 1;
end
elseif (event1 <=0.66) % horizontal obstacle 水平障碍
idJ = randi(m-4) + 2; % not to obstuct the door 不阻碍门
idI = randi(m-4 - obstaclesLengths(i)) + 2; % not to obstuct the door
for j = 0:obstaclesLengths(i)-1
if(Occupied(idI+j, idJ) ~= 0)
break;
end
Occupied(idI+j, idJ) = 100;
Obstacles(cnt,:) = [idI+j, idJ];
cnt = cnt + 1;
end
else % diagonal obstacle
idJ = randi(m-4 - obstaclesLengths(i)) + 2; % not to obstuct the door
idI = randi(m-4 - obstaclesLengths(i)) + 2; % not to obstuct the door
for j = 0:obstaclesLengths(i)-1
if(Occupied(idI+j, idJ+j) ~= 0)
break;
end
Occupied(idI+j, idJ+j) = 100;
Obstacles(cnt,:) = [idI+j, idJ+j];
cnt = cnt + 1;
end
end
end
% populate after obstacles has been generated 在障碍物产生后填充行人
% for a=5:20 % 可以这样自由定义行人的初始位置。
% if(Occupied(a, 5) == 0)
% Occupied(a, 5) = 1;
% end
while(populated ~= population) % population = 750;
% 这里的代码就是用来放置行人初始位置的,这里是随机的。
i = randi(n-2) + 1;
j = randi(m-2) + 1;
if(Occupied(i, j) == 0) % 确保不会在已有障碍物的cell中放置行人
Occupied(i, j) = 1;
populated = populated + 1;
end
end
end
% function [Occupied, Doors, lengths, Obstacles, population] = populate(Density, gridDims, paramsDoors, paramsObstacles)
% Occupied=0;Doors=[0,0;0,0];lengths=0;Obstacles=0;population=0;
% end