-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.lua
More file actions
155 lines (136 loc) · 6.18 KB
/
stack.lua
File metadata and controls
155 lines (136 loc) · 6.18 KB
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
function Stack(row, col, level, config)
-- 参数检查
if type(row) ~= "number" or type(col) ~= "number" or type(level) ~= "number" then
print('初始化Stack失败,输入的row col level参数不正确:', row, col, level)
print(debug.traceback())
end
-- 处理没有输入config的情况
if config == nil then
config = {}
end
local stack = {}
stack.type = config.type or 'stack'
-- 模型参数
stack.clength = config.clength or 6.06
stack.cwidth = config.cwidth or 2.44
stack.cheight = config.cheight or 2.42
stack.cspan = config.cspan or {0.6, 0.6} -- {xspan, zspan}
stack.containerUrls = config.containerUrls or
{'/res/ct/container.glb', '/res/ct/container_brown.glb', '/res/ct/container_blue.glb',
'/res/ct/container_yellow.glb'}
-- 位置参数
stack.origin = config.origin or {0, 0, 0} -- 原点:用于计算集装箱相对位置
stack.rot = config.rot or 0 -- 旋转弧度
-- 容量参数
stack.row = row or 0 -- 行数
stack.col = col or 0 -- 列数
stack.level = level or 0 -- 层数
-- 内置变量
stack.containerPositions = {} -- 堆场各集装箱位置坐标列表(row,bay,level)
stack.containers = {} -- 集装箱对象列表(使用相对坐标索引)
-- 初始化计算参数
function stack:init()
stack.length = stack.clength * stack.col + (stack.col - 1) * stack.cspan[2] -- 堆场长度
stack.width = stack.cwidth * stack.row + (stack.row - 1) * stack.cspan[1] -- 堆场宽度
stack.anchorPoint = stack.anchorPoint or
{stack.origin[1] + stack.width / 2, stack.origin[2], stack.origin[3] + stack.length} -- 锚点,可能自定义
-- 集装箱层数
stack.levelPos = {} -- 层数y坐标集合,已经考虑了origin的位置
for i = 1, level + 1 do -- 考虑移动层
stack.levelPos[i] = stack.origin[2] + stack.cheight * (i - 1)
end
-- 初始化集装箱对象表
for i = 1, stack.row do
stack.containerPositions[i] = {} -- 初始化
stack.containers[i] = {}
for j = 1, stack.col do
stack.containerPositions[i][j] = {}
stack.containers[i][j] = {}
for k = 1, stack.level do
-- 初始化集装箱位置
local x = stack.origin[1] + (i - 1) * (stack.cwidth + stack.cspan[1]) + stack.cwidth / 2
local y = stack.origin[2] + stack.cheight * (k - 1)
local z = stack.origin[3] + (stack.col - j) * (stack.clength + stack.cspan[2]) + stack.clength / 2
stack.containerPositions[i][j][k] = {x, y, z}
-- 初始化集装箱对象列表
stack.containers[i][j][k] = nil
end
end
end
end
stack:init()
-- 绘制stack范围
scene.addobj('polyline', {
vertices = {stack.origin[1], stack.origin[2], stack.origin[3], stack.origin[1] + stack.width, stack.origin[2],
stack.origin[3], stack.origin[1] + stack.width, stack.origin[2], stack.origin[3] + stack.length,
stack.origin[1], stack.origin[2], stack.origin[3] + stack.length, stack.origin[1], stack.origin[2],
stack.origin[3]},
color = 'green'
})
-- 将堆场所有可用位置填充集装箱
function stack:fillAllContainerPositions()
for i = 1, stack.row do
for j = 1, stack.col do
for k = 1, stack.level do
stack:fillWithContainer(i, j, k)
end
end
end
end
--- 根据sum随机生成每个(stack.bay,stack.row)位置的集装箱数量
---@param sum number 生成的集装箱总数
---@param containerUrls table 集装箱链接(颜色)列表,可选参数
function stack:fillRandomContainerPositions(sum, containerUrls)
-- 参数检查
if type(sum) ~= "number" then
print(debug.traceback())
assert(type(sum) == "number", '没有输入随机生成集装箱的总数,或输入值有误:' .. type(sum))
end
if type(containerUrls) ~= "nil" and type(containerUrls) ~= "table" then
print(debug.traceback())
assert(type(containerUrls) == "table" or type(containerUrls) == "nil", '输入的containerUrls参数类型不正确:' .. type(containerUrls))
end
-- 注入集装箱颜色列表
if containerUrls ~= nil then
stack.containerUrls = containerUrls -- 修改stack的集装箱颜色列表
end
-- 初始化
local containerNum = {}
for i = 1, stack.row do
containerNum[i] = {}
for j = 1, stack.col do
containerNum[i][j] = 0
end
end
-- 随机生成
local summon = 0
while summon < sum do
local row = math.random(stack.row)
local bay = math.random(stack.col)
if containerNum[row][bay] < stack.level then
containerNum[row][bay] = containerNum[row][bay] + 1
summon = summon + 1
end
end
-- 填充
for i = 1, stack.row do
for j = 1, stack.col do
for k = 1, stack.level do
-- 如果层数小于当前(row, bay)生成的层数,则放置集装箱,否则不放置
if k <= containerNum[i][j] then
stack:fillWithContainer(i, j, k)
end
end
end
end
end
-- 在指定的(row, bay, level)位置生成集装箱
function stack:fillWithContainer(row, bay, level)
local url = stack.containerUrls[math.random(1, #stack.containerUrls)] -- 随机选择集装箱颜色
local containerPos = stack.containerPositions[row][bay][level] -- 获取集装箱位置
local container = scene.addobj(url) -- 生成集装箱
container:setpos(table.unpack(containerPos)) -- 设置集装箱位置
stack.containers[row][bay][level] = container
end
return stack
end