private void drawRectNode(RectNode node, double per = 1) { System.Windows.Shapes.Rectangle dRect = new Rectangle() { Width = node.m_rect.Width * per, Height = node.m_rect.Height * per, Margin = new Thickness( node.m_rect.X * per + (mx_canvas.ActualWidth / 4) * (node.m_packCount % 4), node.m_rect.Y * per + (mx_canvas.ActualHeight / 4) * (node.m_packCount / 4), 0, 0), Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xff, 0x00, 0x00, 0x00)), StrokeThickness = 0, }; if (node.m_isAdd) { dRect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0x88, 0x33, 0x99, 0xff)); } else { dRect.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0x00, 0x33, 0x99, 0xff)); } mx_canvas.Children.Add(dRect); }
private static int getRectNestingByPreset(Dictionary<string, RectNode> mapRectNode, int width = conf_maxSizeOfPreset, int height = conf_maxSizeOfPreset) { var resultByWidth = from pair in mapRectNode orderby pair.Value.m_rect.Width descending select pair; int count = 0; ArrayList arrMapGrid = new ArrayList(); ArrayList tmpMapGrid = new ArrayList(); ArrayList tmpArr = new ArrayList(); RectNode tmpNode = new RectNode(new System.Drawing.Rectangle(0, 0, width, height), false); int refreshCount = 0; tmpArr.Add(tmpNode); tmpMapGrid.Add(tmpArr); arrMapGrid.Add(tmpMapGrid); if (ImageNesting.s_pW != null && ImageNesting.s_pW.mx_canvas != null) { ImageNesting.s_pW.mx_canvas.Children.Clear(); } foreach (KeyValuePair<string, RectNode> pair in resultByWidth) { count++; for (int i = 0; true; i++) { if (!enableToPutIn((ArrayList)arrMapGrid[i], pair.Value)) { if (i == arrMapGrid.Count - 1) { tmpMapGrid = new ArrayList(); tmpArr = new ArrayList(); tmpNode = new RectNode(new System.Drawing.Rectangle(0, 0, width, height), false); tmpArr.Add(tmpNode); tmpMapGrid.Add(tmpArr); arrMapGrid.Add(tmpMapGrid); } } else { pair.Value.m_packCount = i; break; } } printString("\r\n进度:" + count.ToString() + "/" + mapRectNode.Count.ToString() + "\n" + "包数:" + arrMapGrid.Count, true); if (ImageNesting.s_pW != null && ImageNesting.s_pW.mx_canvas != null) { ImageNesting.s_pW.drawRectNode(pair.Value, ImageNesting.s_pW.mx_canvas.ActualWidth / width / 4); } refreshCount++; if (refreshCount % 17 == 0) { DoEvents(); } } Dictionary<string, RectNode> lstMap = new Dictionary<string, RectNode>(); foreach (KeyValuePair<string, RectNode> pair in mapRectNode) { if (pair.Value.m_packCount == arrMapGrid.Count - 1) { lstMap.Add(pair.Key, pair.Value); } } int maxPow = 0; //得到预期的2的整数次幂 maxPow = getMaxPow(lstMap.Values.ToList()); if (maxPow != 11) { maxPow--; do { maxPow++; if (ImageNesting.s_pW != null) { ImageNesting.s_pW.clearChildGrid(arrMapGrid.Count - 1); } printString("\r\n即将完成,开始尾包尺寸重定" + Math.Pow(2, maxPow), true); } while (!getRectNesting(lstMap, (int)Math.Pow(2, maxPow), (int)Math.Pow(2, maxPow), true)); } printString("\r\n[完成]\r\n"); return arrMapGrid.Count; }
private static bool getRectNesting(Dictionary<string, RectNode> mapRectNode, int width, int height, bool isPreset = false) { var resultByWidth = from pair in mapRectNode orderby pair.Value.m_rect.Width descending select pair; ArrayList mapGrid = new ArrayList(); ArrayList firstArr = new ArrayList(); RectNode firstNode = new RectNode(new System.Drawing.Rectangle(0, 0, width, height), false); int refreshCount = 0; firstArr.Add(firstNode); mapGrid.Add(firstArr); if (ImageNesting.s_pW != null) { if (!isPreset) { ImageNesting.s_pW.mx_canvas.Children.Clear(); } } foreach (KeyValuePair<string, RectNode> pair in resultByWidth) { if (!enableToPutIn(mapGrid, pair.Value)) { return false; } if (!isPreset) { printString("\r\n进度:" + mapGrid.Count.ToString() + "/" + (mapRectNode.Count + 1).ToString(), true); if (ImageNesting.s_pW != null) { ImageNesting.s_pW.drawRectNode(pair.Value, ImageNesting.s_pW.mx_canvas.ActualWidth / width); } } else { if (ImageNesting.s_pW != null) { ImageNesting.s_pW.drawRectNode(pair.Value, ImageNesting.s_pW.mx_canvas.ActualWidth / conf_maxSizeOfPreset / 4); } } refreshCount++; if (refreshCount % 17 == 0) { DoEvents(); } } if (!isPreset) { printString("[完成]\r\n"); } return true; }
private static bool enableToPutIn(ArrayList mapGrid, RectNode rNode) { //按照高度,从高到低寻找空格子 for (int i = 0; i < mapGrid.Count; i++) { for (int j = 0; j < ((ArrayList)mapGrid[i]).Count; j++) { RectNode nodeFirst = (RectNode)((ArrayList)mapGrid[i])[j]; int sW = 0; int sH = 0; int di = 0; int dj = 0; if (nodeFirst.m_rect.Width == 0 || nodeFirst.m_rect.Height == 0) { continue; } for (di = 0; (i + di) < mapGrid.Count; di++) { RectNode wRn = (RectNode)((ArrayList)mapGrid[i + di])[j]; if (wRn.m_isAdd == true) { goto ctn; } sW += wRn.m_rect.Width; if (sW >= rNode.m_rect.Width) { goto hLoop; } } goto ctn; hLoop: for (dj = 0; (j + dj) < ((ArrayList)mapGrid[i]).Count; dj++) { RectNode hRn = (RectNode)((ArrayList)mapGrid[i])[j + dj]; if (hRn.m_isAdd == true) { goto ctn; } sH += hRn.m_rect.Height; if (sH >= rNode.m_rect.Height) { goto lastNode; } } goto ctn; lastNode: RectNode rn = (RectNode)((ArrayList)mapGrid[i + di])[j + dj]; if (rn.m_isAdd == true) { goto ctn; } else { rNode.m_rect.X = getXFromGrid(mapGrid, i); rNode.m_rect.Y = getYFromGrid(mapGrid, j); rNode.m_isAdd = true; crossInsertToGrid( mapGrid, i, j, di, dj, rn.m_rect.Width - sW + rNode.m_rect.Width, rn.m_rect.Height - sH + rNode.m_rect.Height); return true; } ctn: continue; } } return false; }
private static void crossInsertToGrid(ArrayList mapGrid, int i0, int j0, int di, int dj, int dw, int dh) { //十字插入 //左上描黑,di和dj最低可以为0,也就是只占了一个、一列或一行格子的情况,这种情况不需要描黑。 if (dw == 0) { di++; } if (dh == 0) { dj++; } for (int i = 0; i < di; i++) { for (int j = 0; j < dj; j++) { RectNode node = ((RectNode)(((ArrayList)mapGrid[i0 + i])[j0 + j])); node.m_isAdd = true; } } if (dw > 0) { //纵向插入 ArrayList newArr = new ArrayList(); mapGrid.Insert(i0 + di + 1, newArr); for (int j = 0; j < ((ArrayList)mapGrid[i0 + di]).Count; j++) { RectNode node = ((RectNode)(((ArrayList)mapGrid[i0 + di])[j])); RectNode newNode = new RectNode( new System.Drawing.Rectangle( node.m_rect.X + dw, node.m_rect.Y, node.m_rect.Width - dw, node.m_rect.Height), false); node.m_rect.Width = dw; if (j >= j0 && j <= j0 + dj) { node.m_isAdd = true; newNode.m_isAdd = false; } else { newNode.m_isAdd = node.m_isAdd; } newArr.Add(newNode); } } if (dh > 0) { //横向插入 for (int i = 0; i < mapGrid.Count; i++) { RectNode node = ((RectNode)(((ArrayList)mapGrid[i])[j0 + dj])); RectNode newNode = new RectNode( new System.Drawing.Rectangle( node.m_rect.X, node.m_rect.Y + dh, node.m_rect.Width, node.m_rect.Height - dh), false); node.m_rect.Height = dh; if (i >= i0 && i <= i0 + di) { node.m_isAdd = true; newNode.m_isAdd = false; } else { newNode.m_isAdd = node.m_isAdd; } ((ArrayList)mapGrid[i]).Insert(j0 + dj + 1, newNode); } } }
private static int addFileToArr(string basicPath, string filter, Dictionary<string, RectNode> mapRectNode) { DirectoryInfo di = new DirectoryInfo(basicPath); FileInfo[] arrFileInfo = di.GetFiles(filter); int retCount = arrFileInfo.Count(); //string strPixelWidth = ""; foreach (FileInfo fi in arrFileInfo) { System.Drawing.Image img = System.Drawing.Image.FromFile(fi.FullName); //int pixelSize = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); //strPixelWidth += pixelSize + "|"; //<inc>检查每一个像素是否255。 //因为差值什么的,所以要+2。 RectNode rn = new RectNode(new System.Drawing.Rectangle(0, 0, img.Width + 2, img.Height + 2), false); mapRectNode.Add(fi.Name, rn); } //Public.ResultLink.createResult(strPixelWidth, true); return retCount; }