Esempio n. 1
0
 public kd_node(int d, double cv, kd_node hi_c, kd_node lo_c, List <Tri> t_list)
 {
     dim    = d;
     cutval = cv;
     hi     = hi_c;
     lo     = lo_c;
     tris   = t_list;
 }
Esempio n. 2
0
        public static void PrintKdtree(kd_node root)
        {
            //if (root.tris == null)
            //    return;

            if (root.tris != null)
            {
                if (root.tris.Count > 0)
                {
                    System.Console.WriteLine("bucket node with {0} tris", root.tris.Count);
                }
            }
            if (root.hi != null)
            {
                PrintKdtree(root.hi);
            }

            if (root.lo != null)
            {
                PrintKdtree(root.lo);
            }
        }
Esempio n. 3
0
 public kd_node(int d, double cv, kd_node hi_c, kd_node lo_c, List<Tri> t_list)
 {
     dim = d;
     cutval = cv;
     hi = hi_c;
     lo = lo_c;
     tris = t_list;
 }
Esempio n. 4
0
        public static kd_node build_kdtree(List<Tri> tris)
        {
            // given a list of triangles, build a kd-tree

            // this should never happen...
            if (tris.Count == 0)
            {
                System.Console.WriteLine("kdtree build ERROR (tris.Count==0)");
                return new kd_node(0, 0, null, null, tris);
            }

            // if the triangles in this node are contained within
            // a rectangle smaller than XX,
            // OR if only one triangle remains,
            // return a bucket node
            if (tris.Count==1)
            {
                kd_node bucket_node = new kd_node(0, 0, null, null, tris);
                //System.Console.WriteLine("(tris.Count<=1)returning bucket node with {0} triangles", tris.Count);
                return bucket_node;
            }

            sp spr = kdtree.spread(tris);
            //System.Console.WriteLine("cuttting along dim={0} spread={1}", spr.d, spr.val);
            //Console.ReadKey();

            // if the max spread is 0, return a bucket node (?when does this happen?)
            if (spr.val == 0.0)
            {
                kd_node bucket_node = new kd_node(0, 0, null, null, tris);
                // System.Console.WriteLine("(spr.val==0) returning bucket node with {0} triangles", tris.Count);
                //foreach (Tri tr in tris)
                //    Console.WriteLine(tr);
                return bucket_node;
            }

            //return null;

            // otherwise, select at which triangle to cut
            double cv=spr.start+spr.val/2;
            //System.Console.WriteLine("cutvalue={0}",cv);

            // build lists of triangles lower and higher than cutval
            List<Tri> tris_hi = new List<Tri>();
            List<Tri> tris_lo = new List<Tri>();
            foreach (Tri t in tris)
            {
                // choose which triangles go into which list here.
                t.calc_bbox(); // this is probably not needed.
                if (spr.d == 0)
                {
                    if (t.bb.maxx > cv)
                        tris_hi.Add(t);
                    else
                        tris_lo.Add(t);
                }
                else if (spr.d == 1)
                {
                    if (t.bb.minx > cv)
                        tris_hi.Add(t);
                    else
                        tris_lo.Add(t);
                }
                else if (spr.d == 2)
                {
                    if (t.bb.maxy > cv)
                        tris_hi.Add(t);
                    else
                        tris_lo.Add(t);
                }
                else if (spr.d == 3)
                {
                    if (t.bb.miny > cv)
                        tris_hi.Add(t);
                    else
                        tris_lo.Add(t);
                }

            }

            if (tris_hi.Count == 0)
                Console.WriteLine("hi-list=0!");

            if (tris_lo.Count == 0)
                Console.WriteLine("lo-list=0!");

            kd_node node = new kd_node();
            node.dim = spr.d;
            node.cutval = cv;
            //System.Console.WriteLine("hi_count={0}  lo_count={1}", tris_hi.Count, tris_lo.Count);
            // System.Console.ReadKey();

            node.hi = build_kdtree(tris_hi);
            node.lo = build_kdtree(tris_lo);

            return node;
        }
Esempio n. 5
0
        public static void search_kdtree(List<Tri> tlist, Point p, Cutter c, kd_node node)
        {
            ns+=1;

            if (node.tris != null)
            {
                if (node.tris.Count > 0)
                {   // add all triangles of a bucket node
                    foreach (Tri t in node.tris)
                    {
                        // check that t belongs
                        if ((p.x + c.R) < t.bb.minx)
                            return;
                        else if ((p.x - c.R) > t.bb.maxx)
                            return;
                        else if ((p.y + c.R) < t.bb.miny)
                            return;
                        else if ((p.y - c.R) > t.bb.maxy)
                            return;
                        else
                            tlist.Add(t);
                    }
                    return;
                }
            }

            switch (node.dim)
            {
                case 0: // cut along xplus
                    if (node.cutval <= p.x - c.R)
                        search_kdtree(tlist, p, c, node.hi);
                    else
                    {
                        search_kdtree(tlist, p, c, node.hi);
                        search_kdtree(tlist, p, c, node.lo);
                    }
                    break;
                case 1: // cut along xminus
                    if (node.cutval >= p.x + c.R)
                        search_kdtree(tlist, p, c, node.lo);
                    else
                    {
                        search_kdtree(tlist, p, c, node.hi);
                        search_kdtree(tlist, p, c, node.lo);
                    }
                    break;
                case 2: // cut along yplus
                    if (node.cutval <= p.y - c.R)
                        search_kdtree(tlist, p, c, node.hi);
                    else
                    {
                        search_kdtree(tlist, p, c, node.hi);
                        search_kdtree(tlist, p, c, node.lo);
                    }
                    break;
                case 3: // cut along yminus
                    if (node.cutval >= p.y + c.R)
                        search_kdtree(tlist, p, c, node.lo);
                    {
                        search_kdtree(tlist, p, c, node.hi);
                        search_kdtree(tlist, p, c, node.lo);
                    }
                    break;
            }
            return;
        }
Esempio n. 6
0
        public static void PrintKdtree(kd_node root)
        {
            //if (root.tris == null)
            //    return;

            if (root.tris != null)
            {
                if (root.tris.Count > 0)
                    System.Console.WriteLine("bucket node with {0} tris", root.tris.Count);
            }
            if (root.hi != null)
                PrintKdtree(root.hi);

            if (root.lo != null)
                PrintKdtree(root.lo);
        }
Esempio n. 7
0
        static public kd_node build_kdtree(List <Tri> tris)
        {
            // given a list of triangles, build a kd-tree

            // this should never happen...
            if (tris.Count == 0)
            {
                System.Console.WriteLine("kdtree build ERROR (tris.Count==0)");
                return(new kd_node(0, 0, null, null, tris));
            }

            // if the triangles in this node are contained within
            // a rectangle smaller than XX,
            // OR if only one triangle remains,
            // return a bucket node
            if (tris.Count == 1)
            {
                kd_node bucket_node = new kd_node(0, 0, null, null, tris);
                //System.Console.WriteLine("(tris.Count<=1)returning bucket node with {0} triangles", tris.Count);
                return(bucket_node);
            }

            sp spr = kdtree.spread(tris);

            //System.Console.WriteLine("cuttting along dim={0} spread={1}", spr.d, spr.val);
            //Console.ReadKey();

            // if the max spread is 0, return a bucket node (?when does this happen?)
            if (spr.val == 0.0)
            {
                kd_node bucket_node = new kd_node(0, 0, null, null, tris);
                // System.Console.WriteLine("(spr.val==0) returning bucket node with {0} triangles", tris.Count);
                //foreach (Tri tr in tris)
                //    Console.WriteLine(tr);
                return(bucket_node);
            }

            //return null;

            // otherwise, select at which triangle to cut
            double cv = spr.start + spr.val / 2;
            //System.Console.WriteLine("cutvalue={0}",cv);


            // build lists of triangles lower and higher than cutval
            List <Tri> tris_hi = new List <Tri>();
            List <Tri> tris_lo = new List <Tri>();

            foreach (Tri t in tris)
            {
                // choose which triangles go into which list here.
                t.calc_bbox(); // this is probably not needed.
                if (spr.d == 0)
                {
                    if (t.bb.maxx > cv)
                    {
                        tris_hi.Add(t);
                    }
                    else
                    {
                        tris_lo.Add(t);
                    }
                }
                else if (spr.d == 1)
                {
                    if (t.bb.minx > cv)
                    {
                        tris_hi.Add(t);
                    }
                    else
                    {
                        tris_lo.Add(t);
                    }
                }
                else if (spr.d == 2)
                {
                    if (t.bb.maxy > cv)
                    {
                        tris_hi.Add(t);
                    }
                    else
                    {
                        tris_lo.Add(t);
                    }
                }
                else if (spr.d == 3)
                {
                    if (t.bb.miny > cv)
                    {
                        tris_hi.Add(t);
                    }
                    else
                    {
                        tris_lo.Add(t);
                    }
                }
            }

            if (tris_hi.Count == 0)
            {
                Console.WriteLine("hi-list=0!");
            }

            if (tris_lo.Count == 0)
            {
                Console.WriteLine("lo-list=0!");
            }

            kd_node node = new kd_node();

            node.dim    = spr.d;
            node.cutval = cv;
            //System.Console.WriteLine("hi_count={0}  lo_count={1}", tris_hi.Count, tris_lo.Count);
            // System.Console.ReadKey();

            node.hi = build_kdtree(tris_hi);
            node.lo = build_kdtree(tris_lo);

            return(node);
        }
Esempio n. 8
0
        public static void search_kdtree(List <Tri> tlist, Point p, Cutter c, kd_node node)
        {
            ns += 1;

            if (node.tris != null)
            {
                if (node.tris.Count > 0)
                {   // add all triangles of a bucket node
                    foreach (Tri t in node.tris)
                    {
                        // check that t belongs
                        if ((p.x + c.R) < t.bb.minx)
                        {
                            return;
                        }
                        else if ((p.x - c.R) > t.bb.maxx)
                        {
                            return;
                        }
                        else if ((p.y + c.R) < t.bb.miny)
                        {
                            return;
                        }
                        else if ((p.y - c.R) > t.bb.maxy)
                        {
                            return;
                        }
                        else
                        {
                            tlist.Add(t);
                        }
                    }
                    return;
                }
            }

            switch (node.dim)
            {
            case 0:     // cut along xplus
                if (node.cutval <= p.x - c.R)
                {
                    search_kdtree(tlist, p, c, node.hi);
                }
                else
                {
                    search_kdtree(tlist, p, c, node.hi);
                    search_kdtree(tlist, p, c, node.lo);
                }
                break;

            case 1:     // cut along xminus
                if (node.cutval >= p.x + c.R)
                {
                    search_kdtree(tlist, p, c, node.lo);
                }
                else
                {
                    search_kdtree(tlist, p, c, node.hi);
                    search_kdtree(tlist, p, c, node.lo);
                }
                break;

            case 2:     // cut along yplus
                if (node.cutval <= p.y - c.R)
                {
                    search_kdtree(tlist, p, c, node.hi);
                }
                else
                {
                    search_kdtree(tlist, p, c, node.hi);
                    search_kdtree(tlist, p, c, node.lo);
                }
                break;

            case 3:     // cut along yminus
                if (node.cutval >= p.y + c.R)
                {
                    search_kdtree(tlist, p, c, node.lo);
                }
                {
                    search_kdtree(tlist, p, c, node.hi);
                    search_kdtree(tlist, p, c, node.lo);
                }
                break;
            }
            return;
        }