Пример #1
0
        void PolygonIntersect()
        {
            //相交检测
            BeginSample("PolygonIntersect - SegmentIntersect");

            this.SortByXcoordinate(segments);

            List <Segment> removeList = new List <Segment> (segments.Count / 2);
            Dictionary <Segment, intersectCls>     SegPointsdict = new Dictionary <Segment, intersectCls> (segments.Count / 2);
            Dictionary <KInt2, HashSet <Segment> > dic           = new Dictionary <KInt2, HashSet <Segment> >();

            for (int i = 0; i < segments.Count; ++i)
            {
                Segment firstline = segments [i];
                for (int j = i + 1; j < segments.Count; ++j)
                {
                    Segment secondline = segments [j];

                    if (firstline.MinX > secondline.MaxX)
                    {
                        break;
                    }

                    long maxminy = KMath.Max(segments[i].MinY, segments[j].MinY);
                    long minmaxy = KMath.Min(segments[i].MaxY, segments[j].MaxY);
                    if (maxminy > minmaxy)
                    {
                        continue;
                    }

                    KInt2 result;
                    if (!firstline.isConnect(secondline) && Segment.Intersect(firstline.start, firstline.end, secondline.start, secondline.end, out result))
                    {
                        if (dic.ContainsKey(result))
                        {
                            dic[result].Add(firstline);
                            dic[result].Add(secondline);
                        }
                        else
                        {
                            HashSet <Segment> hashdata = new HashSet <Segment>();
                            hashdata.Add(firstline);
                            hashdata.Add(secondline);
                            dic[result] = hashdata;
                        }

                        if (firstline.start != result && firstline.end != result)
                        {
                            if (SegPointsdict.ContainsKey(firstline))
                            {
                                SegPointsdict [firstline].points.TryAdd(result);
                            }
                            else
                            {
                                intersectCls data = new intersectCls();
                                data.points.Add(result);
                                data.intersectseg         = secondline;
                                SegPointsdict [firstline] = data;
                            }

                            removeList.Add(firstline);
                        }

                        if (secondline.start != result && secondline.end != result)
                        {
                            if (SegPointsdict.ContainsKey(secondline))
                            {
                                SegPointsdict [secondline].points.TryAdd(result);
                            }
                            else
                            {
                                intersectCls data = new intersectCls();
                                data.points.Add(result);
                                data.intersectseg          = firstline;
                                SegPointsdict [secondline] = data;
                            }
                            removeList.Add(secondline);
                        }

                        // LogMgr.LogError("first =>" + firstline.uid + " => " + secondline.uid);
                    }
                }

                ShowProgress("相交检测", (float)i / segments.Count);
            }

            EndSample();

            //remove old segments  即使有重复也无所谓,减少多次检查的开销
            for (int i = 0; i < removeList.Count; ++i)
            {
                segments.Remove(removeList [i]);
            }

            BeginSample("PolygonIntersect - SortAndAdd2Segments");
            SortAndAdd2Segments(SegPointsdict);
            EndSample();


            DefineIDforSegments(segments);
            //
            BeginSample("Build - TriangleTree");
            tree = new TriangleTree();
            tree.Build(ObsTriangles, 2);
            EndSample();
            //
            List <NavmeshTriangle> testtriangles = new List <NavmeshTriangle> (10);
            List <KInt2>           nearestidxs   = new List <KInt2>(10);

            removeList.Clear();

            BeginSample("Build - Test Triangle");
            for (int i = 0; i < segments.Count; ++i)
            {
                Segment seg = segments [i];
                testtriangles.Clear();
                nearestidxs.Clear();

                tree.ball_queryPoints((seg.start + seg.end) / 2, Seekrange, nearestidxs);
                for (int k = 0; k < nearestidxs.Count; ++k)
                {
                    List <NavmeshTriangle> list = point2triangle [nearestidxs [k]];
                    for (int v = 0; v < list.Count; ++v)
                    {
                        testtriangles.Add(list [v]);
                    }
                }

                ShowProgress("获取邻近三角形", (float)i / segments.Count);

                TriangleCheck(testtriangles, dic, removeList, seg);
            }

            EndSample();
            //
#if UNITY_EDITOR
            willremoveList.AddRange(removeList);
#endif
            for (int i = 0; i < removeList.Count; ++i)
            {
                segments.Remove(removeList [i]);
            }
        }
Пример #2
0
        void Add2Rvo()
        {
            BeginSample("Add2Rvo");
            float firsttime = Time.realtimeSinceStartup;

            if (ExactMode == ExactType.ZERO)
            {
                BeginSample("ZERO COST");
                int[] uses = new int[20];

                for (int i = 0; i < ObsTriangles.Length; i++)
                {
                    NavmeshTriangle node = ObsTriangles [i];

                    uses [0] = uses [1] = uses [2] = 0;

                    if (node != null)
                    {
                        for (int j = 0; j < node.connections.Count; j++)
                        {
                            NavmeshTriangle other = node.connections [j];
                            if (other != null)
                            {
                                int a = node.SharedEdge(other);
                                if (a != -1)
                                {
                                    uses [a] = 1;
                                }
                            }
                        }

                        for (int j = 0; j < 3; j++)
                        {
                            if (uses [j] == 0)
                            {
                                var v1 = node.GetVertex(j);
                                var v2 = node.GetVertex((j + 1) % 3);

                                Simulator.Instance.addObstacle(v1, v2);
                            }
                        }
                    }

                    ShowProgress("插入障碍点", (float)i / (ObsTriangles.Length));
                }
                EndSample();
            }


            if (ExactMode >= ExactType.ONE)
            {
                BeginSample("Buildsegments");
                Buildsegments();
                EndSample();

                BeginSample("RemoveUnused");
                RemoveUnused();
                EndSample();

                BeginSample("PushSegmentstoObstacles");
                PushSegmentstoObstacles();
                EndSample();
            }

            ShowProgress("准备构建ObstacleTree", 0);
            float time = Time.realtimeSinceStartup;

            BeginSample("processObstacles");
            if (processObstacles)
            {
                Simulator.Instance.processObstacles();
            }
            EndSample();
            LogMgr.LogFormat("Add2Rvo cost :{0}  processObstacles cost :{1} ", Time.realtimeSinceStartup - firsttime, Time.realtimeSinceStartup - time);
            ShowProgress("构建ObstacleTree 完成", 1);
            this.ClearProgressBar();
            EndSample();

#if !UNITY_EDITOR
            ObsTriangles   = null;
            segments       = null;
            tree           = null;
            point2triangle = null;
#else
            if (ObstacleDebug >= DebugMode.ShowAll && UnityEditor.EditorUtility.DisplayDialog("tips", "export kdtree asset or not ?", "OK", "NO"))
            {
                string assetpath = UnityEditor.EditorUtility.SaveFilePanelInProject("save", "kdtree", "asset", "please enter a filename");
                if (!string.IsNullOrEmpty(assetpath))
                {
                    KdtreeAsset   node = ScriptableObject.CreateInstance <KdtreeAsset> ();
                    ScriptCommand cmd  = ScriptCommand.Create((int)FrameWorkCmdDefine.GET_KDTREE);
                    cmd.Excute();

                    ScriptCommand obscmd = ScriptCommand.Create((int)FrameWorkCmdDefine.GET_OBSTACLES);
                    obscmd.Excute();

                    KdTree           tree      = cmd.ReturnParams.ReadObject() as KdTree;
                    IList <Obstacle> obstacles = obscmd.ReturnParams.ReadObject() as IList <Obstacle>;
                    node.CreateKdtree(tree, obstacles);

                    cmd.Release(true);
                    obscmd.Release(true);

                    UnityEditor.AssetDatabase.CreateAsset(node, assetpath);
                }
            }
#endif
        }