static protected void TraverseBSPTree(Vector3D origin, BSPTreeNode node, Faces faces)
        {
            if (node != null)
            {
                int sign = (int)Math3D.GetSign(node.Plane.GetDistanceToPlane(origin));

                if (sign > 0)
                {
                    TraverseBSPTree(origin, node.NegativeChild, faces);
                    foreach (Face face in node.Faces)
                    {
                        if (face.Visible == true)
                        {
                            faces.Add(face);
                        }
                    }
                    TraverseBSPTree(origin, node.PositiveChild, faces);
                }
                else                 /*if( sign <= 0 )*/
                {
                    TraverseBSPTree(origin, node.PositiveChild, faces);
                    TraverseBSPTree(origin, node.NegativeChild, faces);
                }
            }
        }
        //------------------------------------------------------------------------

        static public Faces GetVisibleFaces(Vector3D origin, BSPTreeNode node)
        {
            Debug.Assert(node != null);
            Faces faces = new Faces();

            TraverseBSPTree(origin, node, faces);
            return(faces);
        }
Example #3
0
        //------------------------------------------------------------------------

        static public BSPTreeNode       FromFaces(Faces faces)
        {
            //Debug2.Push();

            if (faces.Count == 0)
            {
                //Debug.WriteLine( "faces == 0" );
                //Debug2.Pop();
                return(null);
            }

            /*else if( faces.Count == 1 ) {
             *      Face face = faces[0];
             *
             *      BSPTreeNode node = new BSPTreeNode();
             *      node.Plane = face.GetPlane();
             *      node.Faces.Add( face );
             *      node.PositiveChild	= null;
             *      node.NegativeChild	= null;
             *
             *      return node;
             * }
             * else {*/

            BSPProfiler profilerMinCost = null;

            foreach (Face face in faces)
            {
                BSPProfiler profiler = new BSPProfiler(face.Plane, faces);
                if (profilerMinCost == null || profilerMinCost.GetCost() > profiler.GetCost())
                {
                    profilerMinCost = profiler;
                }
            }

            BSPTreeNode node = new BSPTreeNode();

            node.Plane = profilerMinCost.Plane;
            node.Faces = profilerMinCost.CoplanarFaces;

            node.PositiveChild = BSPTreeNode.FromFaces(profilerMinCost.PositiveFaces);
            node.NegativeChild = BSPTreeNode.FromFaces(profilerMinCost.NegativeFaces);

            return(node);
        }