Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BVTree"/> class.
        /// </summary>
        /// <param name="verts">A set of vertices.</param>
        /// <param name="polys">A set of polygons composed of the vertices in <c>verts</c>.</param>
        /// <param name="nvp">The maximum number of vertices per polygon.</param>
        /// <param name="cellSize">The size of a cell.</param>
        /// <param name="cellHeight">The height of a cell.</param>
        public BVTree(PolyVertex[] verts, PolyMesh.Polygon[] polys, int nvp, float cellSize, float cellHeight)
        {
            nodes = new Node[polys.Length * 2];
            var items = new List <Node>();

            for (int i = 0; i < polys.Length; i++)
            {
                PolyMesh.Polygon p = polys[i];

                Node temp;
                temp.Index      = i;
                temp.Bounds.Min = temp.Bounds.Max = verts[p.Vertices[0]];

                for (int j = 1; j < nvp; j++)
                {
                    int vi = p.Vertices[j];
                    if (vi == PolyMesh.NullId)
                    {
                        break;
                    }

                    var v = verts[vi];
                    PolyVertex.ComponentMin(ref temp.Bounds.Min, ref v, out temp.Bounds.Min);
                    PolyVertex.ComponentMax(ref temp.Bounds.Max, ref v, out temp.Bounds.Max);
                }

                temp.Bounds.Min.Y = (int)Math.Floor((float)temp.Bounds.Min.Y * cellHeight / cellSize);
                temp.Bounds.Max.Y = (int)Math.Ceiling((float)temp.Bounds.Max.Y * cellHeight / cellSize);

                items.Add(temp);
            }

            Subdivide(items, 0, items.Count, 0);
        }
Example #2
0
        /// <summary>
        /// Calculates the bounding box for a set of bounding boxes.
        /// </summary>
        /// <param name="items">The list of all the bounding boxes.</param>
        /// <param name="minIndex">The first bounding box in the list to get the extends of.</param>
        /// <param name="maxIndex">The last bounding box in the list to get the extends of.</param>
        /// <param name="bounds">The extends of all the bounding boxes.</param>
        private static void CalcExtends(List <Node> items, int minIndex, int maxIndex, out PolyBounds bounds)
        {
            bounds = items[minIndex].Bounds;

            for (int i = minIndex + 1; i < maxIndex; i++)
            {
                Node it = items[i];
                PolyVertex.ComponentMin(ref it.Bounds.Min, ref bounds.Min, out bounds.Min);
                PolyVertex.ComponentMax(ref it.Bounds.Max, ref bounds.Max, out bounds.Max);
            }
        }