1
z8018
2025-04-08 e69f814f50fd59739dbedd88518dc8cb8d2ed3ee
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_IBasicInfoService;
 
namespace WIDESEAWCS_BasicInfoService
{
    public class PlaceBlockService
    {
        public const int SPACING = 5;
 
        /// <summary>
        /// 容器尺寸
        /// </summary>
        public ContainerSize ContainerSize { get; private set; }
 
        /// <summary>
        /// 已放置货物
        /// </summary>
        public List<PlacedBlock> PlacedBlocks { get; private set; }
 
        private readonly PlacedBlock containerFloor;
 
        public PlaceBlockService(ContainerSize containerSize, List<PlacedBlock>? placedBlocks = null)
        {
            containerSize.Length = containerSize.Length + 2 * SPACING;
            containerSize.Width = containerSize.Width + 2 * SPACING;
            ContainerSize = containerSize;
            if (placedBlocks == null || placedBlocks.Count == 0)
            {
                PlacedBlocks = new List<PlacedBlock>();
            }
            else
            {
                PlacedBlocks = placedBlocks;
            }
 
 
            containerFloor = new PlacedBlock(new Point3D(SPACING, SPACING, 0), ContainerSize.Length - 2 * SPACING, ContainerSize.Width - 2 * SPACING, 0);
        }
 
        /// <summary>
        /// 主放置方法:尝试放置指定尺寸的货物
        /// </summary>
        /// <param name="length">货物长度(X轴方向)</param>
        /// <param name="width">货物宽度(Y轴方向)</param>
        /// <param name="height">货物高度(Z轴方向)</param>
        /// <returns>
        /// 成功:返回可放置位置的左下前角Point3D坐标
        /// 失败:返回null(尺寸无效或空间不足)
        /// </returns>
        public Point3D? PlaceBlock(int length, int width, int height)
        {
            if (!IsValidBlock(length, width, height))
                return null;
 
            return FindStackablePosition(length, width, height);
        }
 
        /// <summary>
        /// 验证货物尺寸有效性
        /// 校验规则:
        /// 1. 各维度尺寸必须大于等于50mm
        /// 2. 各维度尺寸不得超过容器对应维度(扣除间隔后)
        /// 3. 高度必须 <= 容器剩余高度
        /// </summary>
        /// <param name="l"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        private bool IsValidBlock(int l, int w, int h)
        {
            return l > 0 && w > 0 && h > 0 &&
                   l <= ContainerSize.Length - 2 * SPACING &&
                   w <= ContainerSize.Width - 2 * SPACING &&
                   h < ContainerSize.Height;
        }
 
        /// <summary>
        /// 在现有货物顶部寻找叠放位置(堆叠模式)
        /// 叠放条件:
        /// - 支撑面积 >= 被支撑面面积的70%
        /// - 新货物完全位于支撑货物上方
        /// - 满足间隔要求
        /// </summary>
        /// <param name="l">长度</param>
        /// <param name="w">宽度</param>
        /// <param name="h">高度</param>
        /// <returns></returns>
        private Point3D? FindStackablePosition(int l, int w, int h)
        {
            // 生成候选支撑层(包含容器底部)
            var candidateLayers = PlacedBlocks
                .Select(b => b.Position.Z + b.Height)
                .Append(0)
                .Distinct()
                .OrderBy(z => z)
                .ToList();
 
            foreach (var baseZ in candidateLayers)
            {
                if (baseZ + h > ContainerSize.Height) continue;
 
                // 获取当前层的支撑块(包含虚拟容器底部)
                var supports = GetSupportBlocks(baseZ);
 
                foreach (var support in supports)
                {
                    // 计算有效叠放区域(修正间隔逻辑)
                    int xStart = support.Position.X;
                    int yStart = support.Position.Y;
 
                    // 容器底部支撑必须内缩间隔(即使尺寸相同)
                    if (support == containerFloor)
                    {
                        xStart = support.Position.X;
                        yStart = support.Position.Y;
                    }
 
                    int xEnd = support.Position.X + support.Length - l;
                    int yEnd = support.Position.Y + support.Width - w;
 
                    // 普通支撑块仅在尺寸不同时加间隔
                    if (support != containerFloor &&
                       (l != support.Length && w != support.Width))
                    {
                        xStart += SPACING;
                        yStart += SPACING;
                        xEnd -= SPACING;
                        yEnd -= SPACING;
                    }
 
                    // 最终容器边界约束
                    xEnd = Math.Min(xEnd, ContainerSize.Length - l - SPACING);
                    yEnd = Math.Min(yEnd, ContainerSize.Width - w - SPACING);
 
                    // 验证支撑面积
                    //int supportArea = support.Length * support.Width;
                    //int requiredSupportArea = (int)(l * w * 0.7); // 支撑面积 >= 被支撑面面积的70%
                    //if (supportArea < requiredSupportArea) continue;
 
                    if (xStart > xEnd || yStart > yEnd) continue;
 
                    // 优化搜索:优先角落位置
                    for (int y = yStart; y <= yEnd; y += 10)
                    {
                        for (int x = xStart; x <= xEnd; x += 10)
                        {
                            var candidate = new Point3D(x, y, baseZ);
                            if (IsPositionValid(candidate, l, w, h))
                            {
                                var placed = new PlacedBlock(candidate, l, w, h);
                                //PlacedBlocks.Add(placed);
                                return candidate;
                            }
                        }
                    }
                }
            }
            return null;
        }
 
        /// <summary>
        /// 获取支撑当前货物的底层货物列表
        /// 特殊处理:当baseZ=0时返回虚拟容器底部作为支撑
        /// 支撑条件:货物底面与支撑货物顶面接触且Z坐标匹配
        /// </summary>
        /// <param name="baseZ">支撑块高度</param>
        /// <returns></returns>
        private IEnumerable<PlacedBlock> GetSupportBlocks(int baseZ)
        {
            var blocks = PlacedBlocks
                .Where(b => b.Position.Z + b.Height == baseZ)
                .OrderByDescending(b => b.Length * b.Width).ToList();
 
            // 当baseZ=0时添加容器底部支撑
            if (baseZ == 0 && blocks.Count == 0)
            {
                return new List<PlacedBlock> { containerFloor };
            }
 
            return blocks;
 
 
            //// 获取所有在 baseZ 高度的支撑块
            //var blocks = PlacedBlocks
            //    .Where(b => b.Position.Z + b.Height == baseZ)
            //    .ToList();
 
            //// 当 baseZ=0 时添加容器底部支撑
            //if (baseZ == 0 && !blocks.Any())
            //{
            //    return new List<PlacedBlock> { containerFloor };
            //}
 
            //// 计算整体平面
            //if (blocks.Any())
            //{
            //    int minX = blocks.Min(b => b.Position.X);
            //    int minY = blocks.Min(b => b.Position.Y);
            //    int maxX = blocks.Max(b => b.Position.X + b.Length);
            //    int maxY = blocks.Max(b => b.Position.Y + b.Width);
 
            //    var overallSupport = new PlacedBlock(
            //        new Point3D(minX, minY, baseZ),
            //        maxX - minX,
            //        maxY - minY,
            //        0
            //    );
 
            //    return new List<PlacedBlock> { overallSupport };
            //}
 
            //return blocks;
        }
 
        /// <summary>
        /// 验证指定位置是否合法
        /// 校验内容:
        /// 1. 边界条件:货物不得超出容器有效空间
        /// 2. 碰撞检测:与已放置货物无空间重叠
        /// 3. 间隔要求:保持最小间隔(SPACING常量)
        /// </summary>
        /// <param name="pos">坐标</param>
        /// <param name="l">长度</param>
        /// <param name="w">宽度</param>
        /// <param name="h">高度</param>
        /// <returns></returns>
        private bool IsPositionValid(Point3D pos, int l, int w, int h)
        {
            // 边界检查(含容器边缘间隔)
            if (pos.X < SPACING ||
                pos.Y < SPACING ||
                pos.X + l > ContainerSize.Length - SPACING ||
                pos.Y + w > ContainerSize.Width - SPACING)
                return false;
 
            // 三维碰撞检测
            var newBlock = new PlacedBlock(pos, l, w, h);
            return !PlacedBlocks.Any(existing =>
            {
                bool xOverlap = newBlock.Position.X < existing.Position.X + existing.Length + SPACING &&
                              newBlock.Position.X + newBlock.Length + SPACING > existing.Position.X;
 
                bool yOverlap = newBlock.Position.Y < existing.Position.Y + existing.Width + SPACING &&
                              newBlock.Position.Y + newBlock.Width + SPACING > existing.Position.Y;
 
                bool zOverlap = newBlock.Position.Z < existing.Position.Z + existing.Height &&
                              newBlock.Position.Z + h > existing.Position.Z;
 
                return xOverlap && yOverlap && zOverlap;
            });
        }
    }
 
    /// <summary>
    /// 三维坐标结构体
    /// 表示货物放置位置的左下前角坐标(X,Y,Z)
    /// 坐标系说明:
    /// - X轴:沿容器长度方向
    /// - Y轴:沿容器宽度方向
    /// - Z轴:垂直方向(高度)
    /// </summary>
    public struct Point3D
    {
        /// <summary>X轴坐标(毫米)</summary>
        public int X { get; }
 
        /// <summary>Y轴坐标(毫米)</summary>
        public int Y { get; }
 
        /// <summary>Z轴坐标(毫米)</summary>
        public int Z { get; }
 
        public Point3D(int x, int y, int z)
        {
            X = x;
            Y = y;
            Z = z;
        }
    }
 
    /// <summary>
    /// 已放置货物信息类
    /// 记录每个货物的位置和尺寸信息
    /// 坐标系说明:位置点为货物的左下前角坐标
    /// </summary>
    public class PlacedBlock
    {
        /// <summary>货物左下前角坐标</summary>
        public Point3D Position { get; }
 
        /// <summary>沿X轴方向长度(毫米)</summary>
        public int Length { get; }
 
        /// <summary>沿Y轴方向宽度(毫米)</summary>
        public int Width { get; }
 
        /// <summary>沿Z轴方向高度(毫米)</summary>
        public int Height { get; }
 
        public PlacedBlock(Point3D position, int length, int width, int height)
        {
            Position = position;
            Length = length;
            Width = width;
            Height = height;
        }
    }
 
    public struct ContainerSize
    {
        public int Length { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
 
        public ContainerSize(int length, int width, int height)
        {
            Length = length;
            Width = width;
            Height = height;
        }
    }
}