Example #1
0
        public Octree(MeshToolkit mesh, bool cubic = true)
        {
            this._mesh  = mesh;
            this._cubic = cubic;
            //this.triangles = _mesh.Triangles();
            this.vertices         = _mesh.Vertices();
            this.vertexIndexByTri = Core.List.Chop <int>(_mesh.VertexIndicesByTri(), 3);
            BoundingBox bbox = Graphical.Geometry.MeshToolkit.BoundingBox(this._mesh);

            // Extending the BoundingBox to be cubical from the centre.
            // Done by getting the max component of the Bounding box and translating min and max points outwards.
            // https://github.com/diwi/Space_Partitioning_Octree_BVH/blob/b7f66fe04e4af3b98ab9404363ab33f5dc1628a9/SpacePartitioning/src/DwOctree/Octree.java#L83
            if (cubic)
            {
                using (Point center = Geometry.Point.MidPoint(bbox.MinPoint, bbox.MaxPoint))
                {
                    Vector   bboxSize       = Vector.ByTwoPoints(bbox.MinPoint, bbox.MaxPoint);
                    Vector   halfSize       = bboxSize.Scale(0.5);
                    double[] halfComponents = new double[3] {
                        halfSize.X, halfSize.Y, halfSize.Z
                    };
                    double maxComponent       = halfComponents[getSubdivisionPlane(bbox)];
                    Vector expansionDirection = Vector.ByCoordinates(maxComponent, maxComponent, maxComponent);
                    bbox = BoundingBox.ByCorners(
                        (Point)center.Translate(-maxComponent, -maxComponent, -maxComponent),
                        (Point)center.Translate(maxComponent, maxComponent, maxComponent)
                        );
                }
            }

            _root = new OctreeNode(0, bbox);
        }
Example #2
0
        internal bool AssureChildren(OctreeNode ot, int maxDepth)
        {
            if (ot.depth >= maxDepth)
            {
                return(false);
            }
            if (ot.isLeaf())
            {
                ot.children = new OctreeNode[8];
                // Half vector size, by scaling total diagonal or maybe faster creating the MidPoint?
                Vector halfSize   = Vector.ByTwoPoints(ot.bbox.MinPoint, ot.bbox.MaxPoint).Scale(0.5);
                int    childDepth = ot.depth + 1;
                for (int i = 0; i < ot.children.Count(); i++)
                {
                    Point min = Point.ByCoordinates(
                        ot.bbox.MinPoint.X + (((i & 4) > 0) ? halfSize.X : 0),
                        ot.bbox.MinPoint.Y + (((i & 2) > 0) ? halfSize.Y : 0),
                        ot.bbox.MinPoint.Z + (((i & 1) > 0) ? halfSize.Z : 0)
                        );
                    Point max = (Point)min.Translate(halfSize);

                    ot.children[i] = new OctreeNode(childDepth, BoundingBox.ByCorners(min, max));
                }
            }
            return(true);
        }