Esempio n. 1
0
            public void Build(SimpleLinkedList triangles, int depth)
            {
                if (triangles.Count < SplitSize || depth >= 8)
                {
                    this.triangles = new int[triangles.Count];
                    SimpleLinkedList.Node rover = triangles.first;
                    int i = 0;
                    while (rover != null)
                    {
                        this.triangles[i] = rover.val;
                        rover             = rover.next;
                        i++;
                    }
                    if (triangles.Count >= SplitSize)
                    {
                        //Vector size;
                        //Utils.sub(out size, max, min);
                        //logger.WriteLine("New leaf " + depth + " size: " + triangles.Count + " " + size);
                    }
                }
                else
                {
                    this.triangles = null;

                    float[] xl = new float[3] {
                        min.x, mid.x, max.x
                    };
                    float[] yl = new float[3] {
                        min.y, mid.y, max.y
                    };
                    float[] zl = new float[3] {
                        min.z, mid.z, max.z
                    };

                    Vector boxhalfsize = new Vector(
                        mid.x - min.x,
                        mid.y - min.y,
                        mid.z - min.z);

                    // allocate children
                    //SimpleLinkedList[, ,] childTris = new SimpleLinkedList[2, 2, 2];
                    children = new Node[2, 2, 2];

                    Vector vertex0;
                    Vector vertex1;
                    Vector vertex2;

                    //foreach (int triangle in triangles)
                    for (int x = 0; x < 2; x++)
                    {
                        for (int y = 0; y < 2; y++)
                        {
                            for (int z = 0; z < 2; z++)
                            {
                                SimpleLinkedList.Node rover     = triangles.GetFirst();
                                SimpleLinkedList      childTris = new SimpleLinkedList(this.logger);

                                children[x, y, z] = new Node(tree,
                                                             new Vector(xl[x], yl[y], zl[z]),
                                                             new Vector(xl[x + 1], yl[y + 1], zl[z + 1]), this.logger);
                                children[x, y, z].parent = this;
                                int c = 0;
                                while (rover != null)
                                {
                                    c++;
                                    SimpleLinkedList.Node next = rover.next;
                                    int triangle = rover.val;
                                    tree.tc.GetTriangleVertices(triangle,
                                                                out vertex0.x, out vertex0.y, out vertex0.z,
                                                                out vertex1.x, out vertex1.y, out vertex1.z,
                                                                out vertex2.x, out vertex2.y, out vertex2.z);

                                    if (Utils.TestTriangleBoxIntersect(vertex0, vertex1, vertex2,
                                                                       children[x, y, z].mid, boxhalfsize))
                                    {
                                        childTris.Steal(rover, triangles);
                                    }
                                    rover = next;
                                }
                                if (c == 0)
                                {
                                    children[x, y, z] = null; // drop that
                                }
                                else
                                {
                                    //logger.WriteLine(depth + " of " + c + " stole " + childTris.RealCount + "(" + childTris.Count + ")" + " left is " + triangles.RealCount + "(" + triangles.Count + ")");
                                    children[x, y, z].Build(childTris, depth + 1);
                                    triangles.StealAll(childTris);
                                }

                                /*if (depth == 0)
                                 * {
                                 *  logger.WriteLine("Post tris: " + triangles.Count);
                                 *  logger.WriteLine("count: " + c);
                                 * }*/
                            }
                        }
                    }
                }
            }
Esempio n. 2
0
            public void Build(SimpleLinkedList triangles, int depth)
            {
                if (triangles.Count < SplitSize || depth >= 10)
                {
                    this.triangles = new int[triangles.Count];
                    SimpleLinkedList.Node rover = triangles.first;
                    int i = 0;
                    while (rover != null)
                    {
                        this.triangles[i] = rover.val;
                        rover             = rover.next;
                        i++;
                    }
                    if (triangles.Count >= SplitSize)
                    {
                        Vector size;
                        Utils.sub(out size, ref max, ref min);
                        PathGraph.Log("New leaf " + depth + " size: " + triangles.Count + " " + size);
                    }
                }
                else
                {
                    this.triangles = null;

                    var xl = new float[3] {
                        min.x, mid.x, max.x
                    };
                    var yl = new float[3] {
                        min.y, mid.y, max.y
                    };

                    var boxhalfsize = new Vector(
                        mid.x - min.x,
                        mid.y - min.y,
                        1E10f);


                    children = new Node[2, 2];

                    Vector vertex0;
                    Vector vertex1;
                    Vector vertex2;

                    // if (depth <= 3)
                    //     PathGraph.Log(depth + " Pre tris: " + triangles.Count);

                    int ugh = 0;
                    //foreach (int triangle in triangles)
                    for (int x = 0; x < 2; x++)
                    {
                        for (int y = 0; y < 2; y++)
                        {
                            SimpleLinkedList.Node rover = triangles.GetFirst();
                            var childTris = new SimpleLinkedList();

                            children[x, y] = new Node(tree,
                                                      new Vector(xl[x], yl[y], 0),
                                                      new Vector(xl[x + 1], yl[y + 1], 0));
                            children[x, y].parent = this;
                            int c = 0;
                            while (rover != null)
                            {
                                c++;
                                SimpleLinkedList.Node next = rover.next;
                                int triangle = rover.val;
                                tree.tc.GetTriangleVertices(triangle,
                                                            out vertex0.x, out vertex0.y, out vertex0.z,
                                                            out vertex1.x, out vertex1.y, out vertex1.z,
                                                            out vertex2.x, out vertex2.y, out vertex2.z);

                                if (Utils.TestTriangleBoxIntersect(vertex0, vertex1, vertex2,
                                                                   children[x, y].mid, boxhalfsize))
                                {
                                    childTris.Steal(rover, triangles);

                                    ugh++;
                                }
                                rover = next;
                            }
                            if (c == 0)
                            {
                                children[x, y] = null; // drop that
                            }
                            else
                            {
                                //PathGraph.Log(depth + " of " + c + " stole " + childTris.RealCount + "(" + childTris.Count + ")" + " left is " + triangles.RealCount + "(" + triangles.Count + ")");
                                children[x, y].Build(childTris, depth + 1);
                                triangles.StealAll(childTris);
                            }

                            /*if (depth == 0)
                             * {
                             *  PathGraph.Log("Post tris: " + triangles.Count);
                             *  PathGraph.Log("count: " + c);
                             * }*/
                        }
                    }
                }
            }