示例#1
0
 public void AddTriangle(TopoTriangle triangle)
 {
     if (left == null && triangles == null)
     {
         triangles = new HashSet <TopoTriangle>();
     }
     if (triangles != null)
     {
         triangles.Add(triangle);
         box.Add(triangle.boundingBox);
         if (triangles.Count > nextTrySplit)
         {
             TrySplit();
         }
     }
     else
     {
         if (triangle.boundingBox.maxPoint[dimension] < middlePosition)
         {
             left.AddTriangle(triangle);
         }
         else if (triangle.boundingBox.minPoint[dimension] > middlePosition)
         {
             right.AddTriangle(triangle);
         }
         else
         {
             middle.AddTriangle(triangle);
         }
     }
 }
示例#2
0
 public void PrepareFastSearch()
 {
     tree = new TopoTriangleNode(null);
     foreach (TopoTriangle triangle in triangles)
     {
         tree.AddTriangle(triangle);
     }
 }
示例#3
0
 public void Add(TopoTriangle triangle)
 {
     triangles.Add(triangle);
     if (tree != null)
     {
         tree.AddTriangle(triangle);
     }
 }
示例#4
0
        private void TrySplit()
        {
            int newDim          = 0;
            int parentDimension = -1;

            if (parent != null)
            {
                parentDimension = parent.dimension;
            }
            RHVector3 size = box.Size;

            if (parentDimension != 0)
            {
                newDim = 0;
            }
            if (parentDimension != 1 && size.x < size.y)
            {
                newDim = 1;
            }
            if (parentDimension != 2 && size.z > size[newDim])
            {
                newDim = 2;
            }
            int    loop           = 0;
            int    maxLoop        = 4;
            double bestCenter     = 0;
            double bestPercentage = 3000;
            int    bestDim        = 0;

            for (int dim = 0; dim < 3; dim++)
            {
                if (dim == parentDimension)
                {
                    continue;
                }
                for (loop = 0; loop < maxLoop; loop++)
                {
                    int    count    = 0;
                    double testDist = box.minPoint[newDim] + size[newDim] * (1 + loop) / (maxLoop + 1);
                    foreach (TopoTriangle tri in triangles)
                    {
                        if (tri.boundingBox.maxPoint[newDim] < testDist)
                        {
                            count++;
                        }
                    }
                    double percent = 100.0 * (double)count / triangles.Count;
                    if (Math.Abs(50 - percent) < bestPercentage)
                    {
                        bestPercentage = percent;
                        bestCenter     = testDist;
                        bestDim        = dim;
                    }
                }
            }
            if (bestPercentage < 5)
            {
                nextTrySplit = (nextTrySplit * 3) / 2;
                return; // not effective enough
            }
            left           = new TopoTriangleNode(this);
            right          = new TopoTriangleNode(this);
            middle         = new TopoTriangleNode(this);
            dimension      = newDim;
            middlePosition = bestCenter;
            foreach (TopoTriangle tri in triangles)
            {
                if (tri.boundingBox.maxPoint[dimension] < middlePosition)
                {
                    left.AddTriangle(tri);
                }
                else if (tri.boundingBox.minPoint[dimension] > middlePosition)
                {
                    right.AddTriangle(tri);
                }
                else
                {
                    middle.AddTriangle(tri);
                }
            }
            triangles = null;
        }