示例#1
0
        SF2 DrainageMonoDirr()
        {
            SF2 res = new SF2(this, 1);

            List <SP2> fieldcopy = getFieldCopy();

            fieldcopy.Sort((x, y) => y.CompareTo(x));

            for (int i = 0; i < fieldcopy.Count(); i++)
            {
                ResultCheckSlope result = CheckSlope(fieldcopy[i]);

                if (result.nb > 0)
                {
                    float sp = res.at(fieldcopy[i].pos.x, fieldcopy[i].pos.y);
                    for (int ind = 0; ind < result.nb; ind++)
                    {
                        float curr = res.at(result.q[ind].pos.x, result.q[ind].pos.y);
                        float set  = curr + (sp * result.avSlope[ind]);
                        set = Mathf.Clamp(set, 0, 1000);
                        res.setAt(result.q[ind].pos.x, result.q[ind].pos.y, set);
                        if (res.min > set)
                        {
                            res.min = set;
                        }
                        if (res.max < set)
                        {
                            res.max = set;
                        }
                    }
                }
            }

            return(res);
        }
示例#2
0
        public SF2 CalculateRoad()
        {
            if (!needCalculateRoad)
            {
                return(this.Road);
            }
            needCalculateRoad = false;

            var watch = System.Diagnostics.Stopwatch.StartNew();

            SF2 res = new SF2(this, 0);

            Graph graph = new Graph(nx * ny);

            for (int i = 0; i < nx; i++)
            {
                for (int j = 0; j < ny; j++)
                {
                    float slopeij = SlopeMap.at(i, j);
                    for (int k = -1; k < 2; k++)
                    {
                        for (int l = -1; l < 2; l++)
                        {
                            int ind1 = index(i, j);
                            int ind2 = index(i + k, j + l);

                            if (k == 0 && l == 0)
                            {
                                continue;
                            }
                            if ((i + k) <= 0 || (i + k) >= nx - 1 || (j + l) <= 0 || (j + l) >= ny - 1)
                            {
                                continue;
                            }
                            //cost = 1.2^DeltaSlope * dist
                            float deltaSlope = slopeij - SlopeMap.at(i + k, j + l);
                            deltaSlope *= 8;
                            float cost;

                            float baseLength;
                            if ((l + k) % 2 != 0)
                            {
                                baseLength = 1f;
                            }
                            else
                            {
                                baseLength = Mathf.Sqrt(2);
                            }

                            cost = baseLength * Mathf.Pow(10f, deltaSlope);

                            bool isok = graph.AddEdge(ind1, ind2, cost);
                        }
                    }
                }
            }

            int startP = index(1000, 10);
            int dest   = index(10, 1000);
            var path   = graph.FindPath(startP);



            //Debug.Log(path[dest].distance);

            List <(int index, float linkcost)> resultPath = Path(startP, dest);


            List <(int index, float linkcost)> Path(int start, int destination)
            {
                List <(int index, float linkcost)> pathlist = new List <(int index, float linkcost)>();

                pathlist.Add((destination, (float)(path[destination].distance - path[path[destination].prev].distance)));
                for (int i = destination; i != start; i = path[i].prev)
                {
                    float linkcost = (float)(path[path[i].prev].distance - path[path[path[i].prev].prev].distance);
                    pathlist.Add((path[i].prev, linkcost));
                }
                return(pathlist);
            }

            res.max = resultPath[0].linkcost;
            res.min = resultPath[0].linkcost;

            for (int i = 0; i < resultPath.Count; i++)
            {
                int rx;
                int ry;
                var revind = reverseIndex(resultPath[i].index);
                rx = revind.x;
                ry = revind.y;
                res.setAt(rx, ry, resultPath[i].linkcost);

                if (res.max < resultPath[i].linkcost)
                {
                    res.max = resultPath[i].linkcost;
                }
                if (res.min > resultPath[i].linkcost)
                {
                    res.min = resultPath[i].linkcost;
                }
            }

            watch.Stop();
            long elapsedMs = watch.ElapsedMilliseconds;

            Debug.Log(elapsedMs);

            return(res);
        }
示例#3
0
        public Color GetColor(int i, int j)
        {
            switch (currentFilter)
            {
            case Filter.NONE:
                return(new Color(0.5f, 0.5f, 0.5f, 1f));

            case Filter.HEIGHT:
                float res = map(Height(i, j), bdeepth, wdeepth, 0, 1);
                return(Shade(res));

            case Filter.LAPLACIAN:
                float res3 = Laplacien(i, j);                   //map(Laplacien(i, j), 0, 10000, 0, 1);
                res3 = Mathf.Pow(Mathf.Abs(res3), 0.33f);
                res3 = Mathf.InverseLerp(minlap, maxlap, res3); //float res31 = map(res3, 0, 0.0001f, 0, 1);
                return(Shade(res3));

            case Filter.SLOPE:
                float res4 = slope(i, j);
                res4 = Mathf.InverseLerp(0, 5, res4);
                return(Shade(res4));

            case Filter.AV_SLOPE:
                float res5 = averageSlope(i, j);
                res5 = Mathf.InverseLerp(0, 5, res5);
                return(Shade(res5));

            case Filter.STREAM_POWER:
                if (StreamArea == null || !StreamAreaUptoDate)
                {
                    StreamAreaUptoDate = true;
                    StreamArea         = DrainageMonoDirr();
                }
                float slopeij = slope(i, j);
                float res6    = Mathf.Pow(StreamArea.at(i, j), 0.5f) * slopeij;
                //float res6 = StreamArea.at(i, j);
                res6 = Mathf.InverseLerp(1, 10, res6);
                float resok = Mathf.Clamp01(res6);
                return(Shade(resok));

            case Filter.WETNESS_INDEX:
                if (StreamArea == null || !StreamAreaUptoDate)
                {
                    StreamAreaUptoDate = true;
                    StreamArea         = DrainageMonoDirr();
                }
                float slopeij2 = slope(i, j);
                float res7     = Mathf.Log(StreamArea.at(i, j)) / (slopeij2 + 0.05f);
                res7 = Mathf.InverseLerp(1, 30, res7);
                float resokok = Mathf.Clamp01(res7);
                return(Shade(resokok));

                break;

            case Filter.ACCESS:
                break;

            case Filter.ORRIANTATION:
                Vector3 norm = Normal(i, j);
                return(new Color(norm.x, norm.y, norm.z));

            case Filter.ROUTE:
                if (needCalculateRoad)
                {
                    SlopeMap          = getSlopeMap();
                    Road              = CalculateRoad();
                    needCalculateRoad = false;
                    Debug.Log("max :" + Road.max);
                }
                if (Road.at(i, j) > 0)
                {
                    return(Shade(Mathf.InverseLerp(Road.min, Road.max, Road.at(i, j))));
                }
                else
                {
                    return(new Color(0.5f, 0.5f, 0.5f, 1f));
                }

            default:
                return(new Color(0.5f, 0.5f, 0.5f, 1f));
            }
            return(new Color(0.5f, 0.5f, 0.5f, 1f));
        }