public static KDTreeNode BuildKdTree(BoundingBox boundingBox, int treeLevel) { treeLevel++; if ((boundingBox.Shapes.Count <= int.Parse(ConfigurationManager.AppSettings["maxBoundingBoxObjects"])) || (treeLevel >= int.Parse(ConfigurationManager.AppSettings["maxTreeDepth"]))) { return new KDTreeNode(boundingBox); } else { SplitDirection splitDirection; if (boundingBox.Width > boundingBox.Hight) { splitDirection = SplitDirection.X; } else { splitDirection = SplitDirection.Y; } BoundingBox leftBox = null; BoundingBox rightBox = null; boundingBox.Split(0.5f, splitDirection, out leftBox, out rightBox); KDTreeNode root = new KDTreeNode(boundingBox); root.LeftChild = BuildKdTree(leftBox, treeLevel); root.RightChild = BuildKdTree(rightBox, treeLevel); return root; } }
public void Split(float splitCoordinate, SplitDirection direction, out BoundingBox boundingBox1, out BoundingBox boundingBox2) { if ((splitCoordinate <= 0) || (splitCoordinate >= 1)) { throw new ArgumentException("The argument splitCoordinate must be between 0 and 1, excluding both."); } if (direction == SplitDirection.X) { boundingBox1 = new BoundingBox(this._Position, this._Width * splitCoordinate, this._Hight); boundingBox2 = new BoundingBox(new Point(this._Position.X + (splitCoordinate * this._Width), this._Position.Y), this._Width - (this._Width * splitCoordinate), this._Hight); foreach (Shape singleShape in this._Shapes) { if (singleShape.Position.X < this._Position.X + (splitCoordinate * this._Width)) { boundingBox1.Shapes.Add(singleShape); if (singleShape.OutherPoint.X > this._Position.X + (splitCoordinate * this._Width)) { boundingBox2.Shapes.Add(singleShape); } } else { boundingBox2.Shapes.Add(singleShape); } } } else { boundingBox1 = new BoundingBox(this._Position, this._Width, this._Hight * splitCoordinate); boundingBox2 = new BoundingBox(new Point(this._Position.X, this._Position.Y - (this._Hight * splitCoordinate)), this._Width, this._Hight - (this._Hight - (this._Hight * splitCoordinate))); foreach (Shape singleShape in this._Shapes) { if (singleShape.Position.Y > this._Position.Y - (splitCoordinate * this._Hight)) { boundingBox1.Shapes.Add(singleShape); if (singleShape.BottomPoint.Y < this._Position.Y - (splitCoordinate * this._Hight)) { boundingBox2.Shapes.Add(singleShape); } } else { boundingBox2.Shapes.Add(singleShape); } } } }
public static BoundingBox GetSceneBoundingBox(string sceneFilePath) { try { string[] lines = File.ReadAllLines(sceneFilePath); BoundingBox boxToReturn = new BoundingBox(new Point(0, 1000), 1000, 1000); foreach (var line in lines) { string[] data = line.Split(' '); string shapeName = data[0]; if (data[1] == "circle") { float x = float.Parse(data[2]); float y = float.Parse(data[3]); float radius = float.Parse(data[4]); Circle newCircle = new Circle(shapeName, new Point(x, y), radius); boxToReturn.Shapes.Add(newCircle); } else if (data[1] == "rect") { float x = float.Parse(data[2]); float y = float.Parse(data[3]); float width = float.Parse(data[4]); float hight = float.Parse(data[5]); Rectangle newRectangle = new Rectangle(shapeName, new Point(x, y), width, hight); boxToReturn.Shapes.Add(newRectangle); } else { throw new InvalidSceneFileException("The file contains invalid shape."); } } return boxToReturn; } catch (Exception e) { throw new InvalidSceneFileException("The file contains invalid data."); } }
public KDTreeNode(BoundingBox nodeBox) { this._NodeBox = nodeBox; }