Пример #1
0
    // Draw an OBB only if bound to a node located at the level_to_be_drawn recursivity level
    protected void Draw(OBBTreeNode node, int level_to_be_drawn, int current_level)
    {
        if (node != null)
        {
            if (current_level == level_to_be_drawn)
            {
                DrawOBB(node.obb, Color.green);
            }
            else
            {
                if (current_level == level_to_be_drawn - 1)
                {
                    DrawOBB(node.obb, Color.gray);
                }

                current_level++;

                foreach (OBBTreeNode child in node.childrenNodes)
                {
                    if (child != null)
                    {
                        Draw(child, level_to_be_drawn, current_level);
                    }
                }
            }
        }
    }
Пример #2
0
    // Recursive method that splits an OBB and compute the OBBs of the sub-spaces
    // If OBB can not be split, stores triangle ids contained in leaf node
    protected void DivideRecursively(OBBTreeNode node, int[] triangles)
    {
        int[] side1;
        int[] side2;

        Vector3[] axes = { Vector3.right, Vector3.up, Vector3.forward };

        // First try to split longest axis, then medium, and then smallest
        if (Split(triangles, node.obb.orientation * axes[(int)node.obb.longAxis], out side1, out side2) ||
            Split(triangles, node.obb.orientation * axes[(int)node.obb.medAxis], out side1, out side2) ||
            Split(triangles, node.obb.orientation * axes[(int)node.obb.shortAxis], out side1, out side2))
        {
            triangles = null;

            // Create node for sub-space 1
            node.childrenNodes[0] = new OBBTreeNode();

            // Compute OBB of triangles contained in sub-space 1
            node.childrenNodes[0].obb = ComputeOBB(side1);

            // Call same method sub-space 1
            DivideRecursively(node.childrenNodes[0], side1);

            // Create node for sub-space 2
            node.childrenNodes[1] = new OBBTreeNode();

            // Compute OBB of triangles contained in sub-space 2
            node.childrenNodes[1].obb = ComputeOBB(side2);

            // Call same method on sub-space 2
            DivideRecursively(node.childrenNodes[1], side2);
        }
        else
        {
            // Store triangles ids contained in OBB
            node.obb.containedTriangles = new HashSet <int>(triangles);
        }
    }