public void CalculateStatistic(Node node) { foreach (var subNode in node.Nodes) { if (subNode.Nodes.Any()) { CalculateStatistic(subNode); } else { _minCount = Math.Min(_minCount, subNode.Objects.Count); _maxCount = Math.Max(_maxCount, subNode.Objects.Count); _totalCount += subNode.Objects.Count; _leafCount++; } } if (_leafCount != 0) { _avrCount = _totalCount/_leafCount; } }
public OctTree Build(BoundingBox box, IList<Geomertry> objects, int maxDepth, int minObjectsInCube) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Node root = new Node(); root.Box = box; root.Objects = objects; _maxDepth = maxDepth; _minObjectsInCube = minObjectsInCube; _minObjectsInCube = Math.Min(_minObjectsInCube, objects.Count); Devide(root, 0); // _loger.Log(Level.Info, string.Format("Oct tree build time: {0} ms", stopwatch.ElapsedMilliseconds)); return new OctTree(root); }
private void GetObjects(Ray ray, Node node, List<Geomertry> result) { if (!ray.Intersects(node.Box).HasValue) return; if (node.Nodes.Any()) { foreach (var subNode in node.Nodes) { GetObjects(ray, subNode, result); } } else { result.AddRange(node.Objects); } }
public OctTree(Node root) { Root = root; }
private void Devide(Node node, int level) { if (level > _maxDepth) { return; } Vector3 min = node.Box.Min; float xs = (node.Box.Max.X - min.X)/2; float ys = (node.Box.Max.Y - min.Y)/2; float zs = (node.Box.Max.Z - min.Z)/2; for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { for (int z = 0; z <= 1; z++) { Node subNode = new Node(); var bMin = new Vector3(min.X + xs*x, min.Y + ys*y, min.Z + zs*z); var bMax = new Vector3(min.X + xs*x + xs, min.Y + ys*y + ys, min.Z + zs*z + zs); BoundingBox box = new BoundingBox(bMin, bMax); subNode.Box = box; subNode.Objects = node.Objects.Where(t => ContaiseGeomertry(box, t)).ToList(); if (subNode.Objects.Any()) { node.Nodes.Add(subNode); if (subNode.Objects.Count >= _minObjectsInCube) { Devide(subNode, level + 1); } } } } } node.Objects = null; }