示例#1
0
        /// <summary>
        /// Creates a new box containing the two given ones.
        /// </summary>
        /// <param name="original">First box.</param>
        /// <param name="additional">Second box.</param>
        /// <param name="result">A JBBox containing the two given boxes.</param>
        public static void CreateMerged(ref TSBBox original, ref TSBBox additional, out TSBBox result)
        {
            FPVector vector;
            FPVector vector2;

            FPVector.Min(ref original.min, ref additional.min, out vector2);
            FPVector.Max(ref original.max, ref additional.max, out vector);
            result.min = vector2;
            result.max = vector;
        }
示例#2
0
        /// <summary>
        /// Expands a bounding box with the volume 0 by all points
        /// given.
        /// </summary>
        /// <param name="points">A array of JVector.</param>
        /// <returns>The resulting bounding box containing all points.</returns>
        #region public static JBBox CreateFromPoints(JVector[] points)

        public static TSBBox CreateFromPoints(FPVector[] points)
        {
            FPVector vector3 = new FPVector(FP.MaxValue);
            FPVector vector2 = new FPVector(FP.MinValue);

            for (int i = 0; i < points.Length; i++)
            {
                FPVector.Min(ref vector3, ref points[i], out vector3);
                FPVector.Max(ref vector2, ref points[i], out vector2);
            }
            return(new TSBBox(vector3, vector2));
        }
示例#3
0
 public void AddPoint(ref FPVector point)
 {
     FPVector.Max(ref this.max, ref point, out this.max);
     FPVector.Min(ref this.min, ref point, out this.min);
 }
示例#4
0
        /// <summary>
        /// Builds the octree.
        /// </summary>
        #region public void BuildOctree()
        public void BuildOctree()
        {
            // create tri and tri bounding box arrays
            triBoxes = new FPBBox[tris.Length];

            // create an infinite size root box
            rootNodeBox = new FPBBox(new FPVector(FP.PositiveInfinity, FP.PositiveInfinity, FP.PositiveInfinity),
                                     new FPVector(FP.NegativeInfinity, FP.NegativeInfinity, FP.NegativeInfinity));


            for (int i = 0; i < tris.Length; i++)
            {
                FPVector.Min(ref positions[tris[i].I1], ref positions[tris[i].I2], out triBoxes[i].min);
                FPVector.Min(ref positions[tris[i].I0], ref triBoxes[i].min, out triBoxes[i].min);

                FPVector.Max(ref positions[tris[i].I1], ref positions[tris[i].I2], out triBoxes[i].max);
                FPVector.Max(ref positions[tris[i].I0], ref triBoxes[i].max, out triBoxes[i].max);

                // get size of the root box
                FPVector.Min(ref rootNodeBox.min, ref triBoxes[i].min, out rootNodeBox.min);
                FPVector.Max(ref rootNodeBox.max, ref triBoxes[i].max, out rootNodeBox.max);
            }

            List <BuildNode> buildNodes = new List <BuildNode>();

            buildNodes.Add(new BuildNode());
            buildNodes[0].box = rootNodeBox;

            FPBBox[] children = new FPBBox[8];
            for (int triNum = 0; triNum < tris.Length; triNum++)
            {
                int    nodeIndex = 0;
                FPBBox box       = rootNodeBox;

                while (box.Contains(ref triBoxes[triNum]) == FPBBox.ContainmentType.Contains)
                {
                    int childCon = -1;
                    for (int i = 0; i < 8; ++i)
                    {
                        CreateAABox(ref box, (EChild)i, out children[i]);
                        if (children[i].Contains(ref triBoxes[triNum]) == FPBBox.ContainmentType.Contains)
                        {
                            // this box contains the tri, it can be the only one that does,
                            // so we can stop our child search now and recurse into it
                            childCon = i;
                            break;
                        }
                    }

                    // no child contains this tri completely, so it belong in this node
                    if (childCon == -1)
                    {
                        buildNodes[nodeIndex].triIndices.Add(triNum);
                        break;
                    }
                    else
                    {
                        // do we already have this child
                        int childIndex = -1;
                        for (int index = 0; index < buildNodes[nodeIndex].nodeIndices.Count; ++index)
                        {
                            if (buildNodes[buildNodes[nodeIndex].nodeIndices[index]].childType == childCon)
                            {
                                childIndex = index;
                                break;
                            }
                        }
                        if (childIndex == -1)
                        {
                            // nope create child
                            BuildNode parentNode = buildNodes[nodeIndex];
                            BuildNode newNode    = new BuildNode();
                            newNode.childType = childCon;
                            newNode.box       = children[childCon];
                            buildNodes.Add(newNode);

                            nodeIndex = buildNodes.Count - 1;
                            box       = children[childCon];
                            parentNode.nodeIndices.Add(nodeIndex);
                        }
                        else
                        {
                            nodeIndex = buildNodes[nodeIndex].nodeIndices[childIndex];
                            box       = children[childCon];
                        }
                    }
                }
            }

            // now convert to the tighter Node from BuildNodes
            nodes         = new Node[buildNodes.Count];
            nodeStackPool = new ArrayResourcePool <ushort>(buildNodes.Count);
            //nodeStack = new UInt16[buildNodes.Count];
            for (int i = 0; i < nodes.Length; i++)
            {
                nodes[i].nodeIndices = new UInt16[buildNodes[i].nodeIndices.Count];
                for (int index = 0; index < nodes[i].nodeIndices.Length; ++index)
                {
                    nodes[i].nodeIndices[index] = (UInt16)buildNodes[i].nodeIndices[index];
                }

                nodes[i].triIndices = new int[buildNodes[i].triIndices.Count];
                buildNodes[i].triIndices.CopyTo(nodes[i].triIndices);
                nodes[i].box = buildNodes[i].box;
            }
            buildNodes.Clear(); buildNodes = null;
        }