Example #1
0
        private void CreateOctree(Vector3[] verts, float radius, int maxLevels)
        {
            LinkedList <IndexedVertex> nodes = new LinkedList <IndexedVertex>();

            for (int i = 0; i < verts.Length; i++)
            {
                nodes.AddLast(new IndexedVertex(i, verts[i]));
            }

            _node = new OctreeInternalNode();
            _node.Process(nodes, 0, 0, 0, 0, radius, maxLevels);
        }
        public void Process(LinkedList <IndexedVertex> nodes, int depth, float x, float y, float z, float radius,
                            int maxDepth)
        {
            _x      = x;
            _y      = y;
            _z      = z;
            _radius = radius;
            _count  = nodes.Count;

            LinkedList <IndexedVertex>[] lists = new LinkedList <IndexedVertex> [8];
            for (int i = 0; i < 8; i++)
            {
                lists[i] = new LinkedList <IndexedVertex>();
            }

            foreach (var node in nodes)
            {
                var current = node.Vertex;
                lists[GetSubtreeIndex(current.X, current.Y, current.Z)].AddLast(node);
            }



            for (int i = 0; i < 8; i++)
            {
                if (depth < maxDepth && lists[i].Count > 1)
                {
                    subTrees[i] = new OctreeInternalNode();
                }
                else
                {
                    subTrees[i] = new OctreeLeaf();
                }

                subTrees[i].Process(lists[i], depth + 1, GetX(i), GetY(i), GetZ(i),
                                    radius / 2, maxDepth);
            }
        }