private bool testAABB(AtlasRect rect1, AtlasRect rect2) { return(rect1.X < rect2.X + rect2.W && rect1.X + rect1.W > rect2.X && rect1.Y < rect2.Y + rect2.H && rect1.H + rect1.Y > rect2.Y); }
private AtlasRect newRect(int x, int y, int w, int h) { AtlasRect rect = new AtlasRect(); rect.X = x; rect.Y = y; rect.W = w; rect.H = h; return(rect); }
private bool spaceIsFree(AreaRow row, AtlasRect rect) { foreach (FilledTex tex in row.FilledSpaces) { if (testAABB(rect, tex.rect)) { return(false); } } foreach (AtlasRect empty in row.EmptySpaces) { if (testAABB(rect, empty)) { return(false); } } return(true); }
private void insertIntoEmptySpace(ref AreaRow row, ref Bitmap bmp, int emptySpaceIndex, uint newLocalID) { AtlasRect EmptySpaceRect = row.EmptySpaces[emptySpaceIndex]; FilledTex newTex = new FilledTex(); newTex.rect = newRect( EmptySpaceRect.X, EmptySpaceRect.Y, bmp.Width, bmp.Height ); newTex.localID = newLocalID; newTex.actualID = (uint)bmp.Tag; /* * Console.WriteLine("Adding new tex! (X/Y/W/H/LID: " + * EmptySpaceRect.X + "/" + * EmptySpaceRect.Y + "/" + * bmp.Width + "/" + * bmp.Height + "/" + * newTex.localID.ToString("X2") + ")" * ); */ row.FilledSpaces.Add(newTex); row.EmptySpaces.RemoveAt(emptySpaceIndex); // Remove Old empty space; /* * if (EmptySpaceRect.Width == newTex.rect.Width && * EmptySpaceRect.Height == newTex.rect.Height) * return; */ int yd = row.height - (newTex.rect.Y + newTex.rect.H); int xd = row.width - (newTex.rect.X + newTex.rect.W); if (yd > 0) { /* * Console.WriteLine("Adding new empty space below! (X/Y/W/H: "+ * EmptySpaceRect.X + "/" + * (newTex.rect.Y + newTex.rect.H) + "/" + * newTex.rect.W + "/" + * yd + ")" * ); */ AtlasRect newSpace = newRect( EmptySpaceRect.X, newTex.rect.Y + newTex.rect.H, newTex.rect.W + (row.width - (EmptySpaceRect.X + newTex.rect.W)), yd ); if (spaceIsFree(row, newSpace)) { row.EmptySpaces.Add(newSpace); } } if (xd > 0) { /* * Console.WriteLine("Adding new empty space to the right! (X/Y/W/H: " + * (newTex.rect.X + newTex.rect.W) + "/" + * EmptySpaceRect.Y + "/" + * xd + "/" + * newTex.rect.H + ")" * ); */ AtlasRect newSpace = newRect( newTex.rect.X + newTex.rect.W, EmptySpaceRect.Y, xd, newTex.rect.H ); if (spaceIsFree(row, newSpace)) { row.EmptySpaces.Add(newSpace); } } row.EmptySpaces.Sort((x, y) => { var ret = x.H.CompareTo(y.H); if (ret == 0) { ret = x.W.CompareTo(y.W); } return(ret); }); }