Esempio n. 1
0
 public void ResetTransformLib()
 {
     for (int i = 0; i < polygon_lib.Count; i++)
     {
         polygon_lib[i].trans = Mat3x3.Eye();
     }
 }
Esempio n. 2
0
        public int AddCanvasFitPolygon(IntRect canvas, int pattern_handle)
        {
            Ngon B = polygon_lib[pattern_handle].GetTransformedPoly()[0];

            Ngon C = GeomUtility.CanFitInsidePolygon(canvas, B);

            polygon_lib.Add(new PolyRef()
            {
                poly = new Ngons()
                {
                    C
                }, trans = Mat3x3.Eye()
            });
            return(polygon_lib.Count - 1);
        }
Esempio n. 3
0
        public int AddMinkowskiSum(int subj_handle, int pattern_handle, NFPQUALITY quality, bool flip_pattern, int set_at = -1)
        {
            Ngons A = polygon_lib[subj_handle].GetTransformedPoly();
            Ngons B = polygon_lib[pattern_handle].GetTransformedPoly();

            Ngons   C    = GeomUtility.MinkowskiSum(B[0], A, quality, flip_pattern);
            PolyRef pref = new PolyRef()
            {
                poly = C, trans = Mat3x3.Eye()
            };

            if (set_at < 0)
            {
                polygon_lib.Add(pref);
            }
            else
            {
                polygon_lib[set_at] = pref;
            }

            return(set_at < 0 ? polygon_lib.Count - 1 : set_at);
        }
Esempio n. 4
0
        /// <summary>
        /// Append a set triangulated polygons to the nester and get handles for each point to the correp. polygon island
        /// </summary>
        /// <param name="points"></param>
        /// <param name="tris"></param>
        /// <returns></returns>
        public int[] AddPolygons(IntPoint[] points, int[] tris, double miter_distance = 0.0)
        {
            // from points to clusters of tris
            int[] poly_map = new int[points.Length];
            for (int i = 0; i < poly_map.Length; i++)
            {
                poly_map[i] = -1;
            }

            HashSet <int>[] graph = new HashSet <int> [points.Length];
            for (int i = 0; i < graph.Length; i++)
            {
                graph[i] = new HashSet <int>();
            }
            for (int i = 0; i < tris.Length; i += 3)
            {
                int t1 = tris[i];
                int t2 = tris[i + 1];
                int t3 = tris[i + 2];

                graph[t1].Add(t2);
                graph[t1].Add(t3);
                graph[t2].Add(t1);
                graph[t2].Add(t3);
                graph[t3].Add(t1);
                graph[t3].Add(t2);
            }

            if (graph.Any(p => p.Count == 0))
            {
                throw new Exception("No singular vertices should exist on mesh");
            }

            int[] clust_ids = new int[points.Length];

            HashSet <int> unmarked  = new HashSet <int>(Enumerable.Range(0, points.Length));
            int           clust_cnt = 0;

            while (unmarked.Count > 0)
            {
                Queue <int> open  = new Queue <int>();
                int         first = unmarked.First();
                unmarked.Remove(first);
                open.Enqueue(first);
                while (open.Count > 0)
                {
                    int c = open.Dequeue();
                    clust_ids[c] = clust_cnt;
                    foreach (int n in graph[c])
                    {
                        if (unmarked.Contains(n))
                        {
                            unmarked.Remove(n);
                            open.Enqueue(n);
                        }
                    }
                }

                clust_cnt++;
            }

            Ngons[] clusters = new Ngons[clust_cnt];
            for (int i = 0; i < tris.Length; i += 3)
            {
                int clust = clust_ids[tris[i]];
                if (clusters[clust] == null)
                {
                    clusters[clust] = new Ngons();
                }

                IntPoint p1 = points[tris[i]];
                IntPoint p2 = points[tris[i + 1]];
                IntPoint p3 = points[tris[i + 2]];

                clusters[clust].Add(new Ngon()
                {
                    p1, p2, p3
                });
            }

            List <Ngons> fulls = new List <Ngons>();

            for (int i = 0; i < clust_cnt; i++)
            {
                Ngons cl = clusters[i];

                Clipper c = new Clipper();
                foreach (Ngon n in cl)
                {
                    c.AddPath(n, PolyType.ptSubject, true);
                }

                Ngons full = new Ngons();
                c.Execute(ClipType.ctUnion, full, PolyFillType.pftNonZero);
                full = Clipper.SimplifyPolygons(full, PolyFillType.pftNonZero);

                if (miter_distance > 0.00001)
                {
                    Ngons         full_miter = new Ngons();
                    ClipperOffset co         = new ClipperOffset();
                    co.AddPaths(full, JoinType.jtMiter, EndType.etClosedPolygon);
                    co.Execute(ref full_miter, miter_distance);
                    full_miter = Clipper.SimplifyPolygons(full_miter, PolyFillType.pftNonZero);
                    fulls.Add(full_miter);
                }
                else
                {
                    fulls.Add(full);
                }
            }

            for (int i = 0; i < clust_ids.Length; i++)
            {
                clust_ids[i] += polygon_lib.Count;
            }

            for (int i = 0; i < fulls.Count; i++)
            {
                polygon_lib.Add(new PolyRef()
                {
                    poly = fulls[i], trans = Mat3x3.Eye()
                });
            }

            return(clust_ids);
        }