public void AddRegion(RegionData data) { var headAddress = BoctAddressTools.FromString(data.HeadAddress); var head = Find(headAddress, true); if (head == null) { throw new BoctException("Could not found the boct."); } if (head.RegionId != BoctRegion.EmptyId) { throw new BoctException("RegionId is not empty: " + head.RegionId); } head.ClearChildren(); if (data.HeadMaterialId == BoctMaterial.EmptyId) { for (int i = 0; i < data.AddressList.Count; i++) { var byteAddress = BoctAddressTools.FromString(data.HeadAddress + data.AddressList[i]); BoctTools.InsertBoct(byteAddress, _head, data.MaterialIdList[i]); } } else { head.MaterialId = data.HeadMaterialId; } head.ExtensionId = data.HeadExtensionId; AddRegion(head, data.GUID, data.LUID); }
void UpdateRegionR(Boct b) { if (b == null) { Debug.LogWarning("Boct is null."); return; } if (BoctTools.LargerThan(b, RegionSize)) { b.RegionId = BoctRegion.EmptyId; if (b.HasChild) { for (int i = 0; i < 8; i++) { if (b.Children[i] != null) { UpdateRegionR(b.Children[i]); } } } } else { AddRegion(b); } }
public List <int> GetExpandInflictRegionList(Boct boct) { var list = new List <int>(); var dic = new Dictionary <int, int>(); var children = boct.Parent.Children; foreach (var child in children) { if (child != null && child != boct) { BoctTools.GetRegionBoctCount(child, dic); } } foreach (var d in dic) { var rid = d.Key; // Ignore the empty ID and the self ID. if (rid != BoctRegion.EmptyId && rid != boct.RegionId) { list.Add(rid); } } return(list); }
public static Vector3 GetCenter(BoctModel model) { if (model.Regions == null) { return(Vector3.zero); } if (model.Regions.Count == 0) { return(Vector3.zero); } var sum = Vector3.zero; int count = 0; foreach (var r in model.Regions) { var solidList = BoctTools.GetSolidBoctArray(r.Value.Head); for (int i = 0; i < solidList.Length; i++) { var boct = solidList[i]; sum += BoctAddressTools.GetCoordinate(boct.Address); count++; } } return(sum / count); }
/// <summary> /// Get the flood boct list. /// </summary> public static List <Boct> GetFloodBoctList(Boct target, Boct head) { // Debug.Log("[GetFloodBoctList]"); var sameDepthBoctList = new List <Boct>(); BoctTools.FindSameDepthBoct(target.Depth, head, sameDepthBoctList); // Debug.Log("SameDepthBoct: " + sameDepthBoctList.Count); var sameMaterialBoctList = sameDepthBoctList.Where(b => b.MaterialId == target.MaterialId).ToList <Boct>(); // Debug.Log("SameMaterialBoct: " + sameMaterialBoctList.Count); // Debug.Log("Dictionary"); var dic = BoctTools.ToDictionary(sameMaterialBoctList); //foreach (var kv in dic) //{ // Debug.Log(kv.Key + " " + kv.Value.ToString()); //} // Remove target. dic.Remove(target.AddressString); var output = new List <Boct>(); output.Add(target); var searchList = new List <Boct>(); searchList.Add(target); while (searchList.Count > 0) { var b = searchList[0]; searchList.RemoveAt(0); for (int i = 0; i < 6; i++) { var address = BoctAddressTools.GetAdjoiningBoctAddress(b.Address, i); if (address == null) { continue; } var addressString = BoctAddressTools.ToString(address); // Debug.Log("Address" + i + ": " + addressString); if (dic.ContainsKey(addressString)) { output.Add(dic[addressString]); searchList.Add(dic[addressString]); dic.Remove(addressString); } } } return(output); }
public static void MirrorCopy(Direction direction, Boct target) { Assert.IsTrue(target.HasChild); byte[] mirrorTable = null; switch (direction) { case Direction.South: target.TrimChild(new int[] { 1, 2, 5, 6 }); mirrorTable = MirrorConvertTableSN; break; case Direction.East: target.TrimChild(new int[] { 2, 3, 6, 7 }); mirrorTable = MirrorConvertTableEW; break; case Direction.North: target.TrimChild(new int[] { 0, 3, 4, 7 }); mirrorTable = MirrorConvertTableSN; break; case Direction.West: target.TrimChild(new int[] { 0, 1, 4, 5 }); mirrorTable = MirrorConvertTableEW; break; case Direction.Top: target.TrimChild(new int[] { 0, 1, 2, 3 }); mirrorTable = MirrorConvertTableTB; break; case Direction.Bottom: target.TrimChild(new int[] { 4, 5, 6, 7 }); mirrorTable = MirrorConvertTableTB; break; } var sourceAddressList = GetSolidBoctArray(target); var mirrorAddressList = new List <List <byte> >(sourceAddressList.Length); for (int i = 0; i < sourceAddressList.Length; i++) { var sourceAddress = sourceAddressList[i].Address; var mirrorAddress = new List <byte>(sourceAddress.Count); for (int j = 0; j < sourceAddress.Count; j++) { mirrorAddress.Add(mirrorTable[sourceAddress[j]]); } mirrorAddressList.Add(mirrorAddress); } for (int i = 0; i < sourceAddressList.Length; i++) { BoctTools.InsertBoct(mirrorAddressList[i], target, sourceAddressList[i].MaterialId); } }
public ExpandResult Expand(Boct boct) { if (!boct.HasMaterial) { return(ExpandResult.Failed); } if (!boct.HasParent) { return(ExpandResult.Failed); } var rid = boct.RegionId; bool outsideRegion = false; if (boct.Parent.RegionId != boct.RegionId) { var dic = new Dictionary <int, int>(); var children = boct.Parent.Children; foreach (var child in children) { if (child != null && child != boct) { BoctTools.GetRegionBoctCount(child, dic); } } foreach (var d in dic) { var id = d.Key; if (id != BoctRegion.EmptyId) { _regions[id].Dispose(true); } } _regions[rid].Head = boct.Parent; outsideRegion = true; } boct.ClearChildren(); var parent = boct.Parent; boct.Parent.ClearChildren(); parent.MaterialId = boct.MaterialId; parent.RegionId = boct.RegionId; parent.ExtensionId = boct.ExtensionId; _regions[rid].SetDirty(); return(outsideRegion ? ExpandResult.OutsideRegion: ExpandResult.InsideRegion); }
public static Dictionary <int, int> CountMaterials(Boct head, Dictionary <int, BoctMaterial> materials) { var dic = CreateMaterialContainer(materials); var solidBoctList = BoctTools.GetSolidBoctArray(head); for (int i = 0; i < solidBoctList.Length; i++) { var boct = solidBoctList[i]; dic[boct.MaterialId]++; } return(dic); }
public static void DivideArea(Boct target, int depth) { // Debug.Log("DivideArea: depth=" + depth); var dic = BoctTools.GetHierarchizedSolidBoctList(target, depth); foreach (var kv in dic) { int d = depth - kv.Key; //Kenny.D(string.Format("Key: {0}, Count: {1}, d: {2}", kv.Key, kv.Value.Count, d)); List <Boct> list = kv.Value; for (int i = 0; i < list.Count; i++) { DivideDeeply(list[i], d); } } }
void AddRegion(Boct head, Guid?guid = null, int?regionId = null) { var region = new BoctRegion(); region.GUID = guid ?? Guid.NewGuid(); region.LUID = regionId ?? RegionIndex; BoctTools.FillRegionId(head, region.LUID); region.Head = head; Regions.Add(region.LUID, region); if (NewRegionIdList != null) { NewRegionIdList.Add(RegionIndex); } if (regionId == null) { RegionIndex++; } region.CurrentState.Subscribe(state => { switch (state) { case BoctRegion.State.Ready: break; case BoctRegion.State.Disposed: Regions.Remove(region.LUID); break; case BoctRegion.State.Dirty: region.MaterialCounts = BoctMaterialTools.CountMaterials(region.Head, MaterialList.Materials); break; } }); // Call last time. RegionStream.OnNext(region); }
/// <summary> /// Update a bounds of solid bocts. /// </summary> /// <param name="scanBoct"></param> public void UpdateSolidBounds(bool scanBoct = true) { SolidBounds = BoctTools.GetBounds(this, scanBoct); //Kenny.D("Bounds: " + SolidBounds.ToString("F4")); }
public static Bounds GetBounds(BoctModel model, bool scanBoct = true) { var bounds = new Bounds(); if (model.Regions == null) { return(bounds); } if (model.Regions.Count == 0) { return(bounds); } var xRange = new Vector2(float.MaxValue, float.MinValue); var yRange = new Vector2(float.MaxValue, float.MinValue); var zRange = new Vector2(float.MaxValue, float.MinValue); if (scanBoct) { foreach (var r in model.Regions) { var solidList = BoctTools.GetSolidBoctArray(r.Value.Head); for (int j = 0; j < solidList.Length; j++) { var boct = solidList[j]; var boctBounds = BoctAddressTools.GetBounds(boct.Address); //Debug.Log("Bound: " + boctBounds.ToString("F4")); if (xRange.x > boctBounds.x - boctBounds.w) { xRange.x = boctBounds.x - boctBounds.w; } if (xRange.y < boctBounds.x + boctBounds.w) { xRange.y = boctBounds.x + boctBounds.w; } if (yRange.x > boctBounds.y - boctBounds.w) { yRange.x = boctBounds.y - boctBounds.w; } if (yRange.y < boctBounds.y + boctBounds.w) { yRange.y = boctBounds.y + boctBounds.w; } if (zRange.x > boctBounds.z - boctBounds.w) { zRange.x = boctBounds.z - boctBounds.w; } if (zRange.y < boctBounds.z + boctBounds.w) { zRange.y = boctBounds.z + boctBounds.w; } } } } else { foreach (var r in model.Regions) { var boct = r.Value.Head; var boctBounds = BoctAddressTools.GetBounds(boct.Address); //Debug.Log("Bound: " + boctBounds.ToString("F4")); if (xRange.x > boctBounds.x - boctBounds.w) { xRange.x = boctBounds.x - boctBounds.w; } if (xRange.y < boctBounds.x + boctBounds.w) { xRange.y = boctBounds.x + boctBounds.w; } if (yRange.x > boctBounds.y - boctBounds.w) { yRange.x = boctBounds.y - boctBounds.w; } if (yRange.y < boctBounds.y + boctBounds.w) { yRange.y = boctBounds.y + boctBounds.w; } if (zRange.x > boctBounds.z - boctBounds.w) { zRange.x = boctBounds.z - boctBounds.w; } if (zRange.y < boctBounds.z + boctBounds.w) { zRange.y = boctBounds.z + boctBounds.w; } } } var center = new Vector3( (xRange.x + xRange.y) * 0.5f, (yRange.x + yRange.y) * 0.5f, (zRange.x + zRange.y) * 0.5f ); var extents = new Vector3( (xRange.y - xRange.x) * 0.5f, (yRange.y - yRange.x) * 0.5f, (zRange.y - zRange.x) * 0.5f ); bounds.center = center; bounds.extents = extents; return(bounds); }
public static void Rotate(Direction direction, Boct target) { Assert.IsTrue(target.HasChild); byte[,] rotateTable = null; int iteration = 0; switch (direction) { case Direction.South: rotateTable = RotateTableY; iteration = 0; break; case Direction.East: rotateTable = RotateTableZ; iteration = 0; break; case Direction.North: rotateTable = RotateTableY; iteration = 2; break; case Direction.West: rotateTable = RotateTableZ; iteration = 2; break; case Direction.Top: rotateTable = RotateTableX; iteration = 0; break; case Direction.Bottom: rotateTable = RotateTableX; iteration = 2; break; } var sourceAddressList = GetSolidBoctArray(target); var rotateAddressList = new List <List <byte> >(sourceAddressList.Length); for (int i = 0; i < sourceAddressList.Length; i++) { var sourceAddress = sourceAddressList[i].Address; var mirrorAddress = new List <byte>(sourceAddress.Count); for (int j = 0; j < sourceAddress.Count; j++) { mirrorAddress.Add(rotateTable[iteration, sourceAddress[j]]); } rotateAddressList.Add(mirrorAddress); } target.Children = null; for (int i = 0; i < sourceAddressList.Length; i++) { BoctTools.InsertBoct(rotateAddressList[i], target, sourceAddressList[i].MaterialId); } }