/// <summary>
        /// Calculates the contours.
        /// </summary>
        public void CalculateContours()
        {
            if (this.Data == null)
            {
                return;
            }

            double[] actualContourLevels = this.ContourLevels;

            this.segments = new List <ContourSegment>();
            Conrec.RendererDelegate renderer = (startX, startY, endX, endY, contourLevel) =>
                                               this.segments.Add(new ContourSegment(new DataPoint(startX, startY), new DataPoint(endX, endY), contourLevel));

            if (actualContourLevels == null)
            {
                double max = this.Data[0, 0];
                double min = this.Data[0, 0];
                for (int i = 0; i < this.Data.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < this.Data.GetUpperBound(1); j++)
                    {
                        max = Math.Max(max, this.Data[i, j]);
                        min = Math.Min(min, this.Data[i, j]);
                    }
                }

                double actualStep = this.ContourLevelStep;
                if (double.IsNaN(actualStep))
                {
                    double range   = max - min;
                    double step    = range / 20;
                    double stepExp = Math.Round(Math.Log(Math.Abs(step), 10));
                    actualStep = Math.Pow(10, Math.Floor(stepExp));
                }

                max = Math.Round(actualStep * (int)Math.Ceiling(max / actualStep), 14);
                min = Math.Round(actualStep * (int)Math.Floor(min / actualStep), 14);

                actualContourLevels = ArrayHelper.CreateVector(min, max, actualStep);
            }

            Conrec.Contour(this.Data, this.ColumnCoordinates, this.RowCoordinates, actualContourLevels, renderer);

            this.JoinContourSegments();

            if (this.ContourColors != null && this.ContourColors.Length > 0)
            {
                foreach (var c in this.contours)
                {
                    // get the index of the contour's level
                    var index = IndexOf(actualContourLevels, c.ContourLevel);
                    if (index >= 0)
                    {
                        // clamp the index to the range of the ContourColors array
                        index   = index % this.ContourColors.Length;
                        c.Color = this.ContourColors[index];
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Contour()
        {
            var x        = ArrayHelper.CreateVector(0, 10, 100);
            var y        = ArrayHelper.CreateVector(0, 10, 100);
            var z        = ArrayHelper.CreateVector(-1, 1, 20);
            var data     = ArrayHelper.Evaluate((x1, y1) => Math.Sin(x1 * y1), x, y);
            int segments = 0;

            Conrec.Contour(data, x, y, z, (x1, y1, x2, y2, elev) => { segments++; });
            Assert.AreEqual(134068, segments);
        }
Ejemplo n.º 3
0
        private void CreateContours(double[,] points, double[] contourX, double[] contourY, double[] contourZ, int gridSize, int steps, ContourType contourType)
        {
            Dictionary <Vector2, List <Vector2> >[] contours = new Dictionary <Vector2, List <Vector2> > [contourZ.Length];

            for (int heightCount = 0; heightCount < contourZ.Length; ++heightCount) //Loop through the different Z height segments
            {
                contours[heightCount] = new Dictionary <Vector2, List <Vector2> >();
            }

            Conrec.Contour(points, contourX, contourY, contourZ, contours); //Get contours for the points. Returns an array of contours for the different heights.

            foreach (Dictionary <Vector2, List <Vector2> > contourList in contours)
            {
                List <List <Vector2> > contourChains = Chains.Process(contourList);
                contourChains = Chains.Simplify(contourChains);

                foreach (List <Vector2> chains in contourChains)
                {
                    List <OSMWayND>  wayPaths = new List <OSMWayND>();
                    List <OSMWayTag> wayTags  = new List <OSMWayTag>();

                    foreach (Vector2 node in chains)
                    {
                        osmNodes.Add(CreateNode(unindexedNodeOffset++, new Vector3((node.x - gridSize) - ((steps * gridSize) / 2), 0, (node.y - gridSize) - ((steps * gridSize) / 2))));
                        wayPaths.Add(new OSMWayND {
                            @ref = (uint)unindexedNodeOffset - 1
                        });
                    }

                    wayPaths.Add(new OSMWayND {
                        @ref = (uint)(unindexedNodeOffset - chains.Count)
                    });                                                                               //Back to the first chain

                    switch (contourType)
                    {
                    case ContourType.Ground:
                        wayTags.Add(new OSMWayTag {
                            k = "natural", v = "coastline"
                        });
                        break;

                    case ContourType.Water:
                        wayTags.Add(new OSMWayTag {
                            k = "natural", v = "water"
                        });
                        break;
                    }

                    osmWays.Add(new OSMWay {
                        changeset = 50000000, id = (uint)unindexedWayOffset++, timestamp = DateTime.Now, user = "******", nd = wayPaths.ToArray(), tag = wayTags.ToArray(), version = 1
                    });
                }
            }
        }
Ejemplo n.º 4
0
        private GMapOverlay Buildroutes(double maxValue, IList <DataPoint> dataPoints)
        {
            double[] xArray = new Double[dataPoints.Count];
            double[] yArray = new Double[dataPoints.Count];
            double[] zArray = new Double[dataPoints.Count];

            int max = ( int)maxValue / 1 + 1;

            for (int index = 0; index < max; index++)
            {
                zArray[index] = index;
            }

            Double[,] array = new Double[dataPoints.Count, 3];
            int counter = 0;

            foreach (DataPoint dataPoint1 in dataPoints)
            {
                array[counter, 0] = dataPoint1.Lat;
                array[counter, 1] = dataPoint1.Lon;
                array[counter, 2] = dataPoint1.Value;
                //zArray[counter] = dataPoint1.Value;

                counter++;
            }
            int upperBound = array.GetUpperBound(0);

            double topLeftLat     = this.viewArea.LocationTopLeft.Lat;
            double rightBottomLat = this.viewArea.LocationRightBottom.Lat;
            double increment      = (topLeftLat - rightBottomLat) / 10;

            for (int index = 0; index < dataPoints.Count; index++)
            {
                xArray[index] = topLeftLat - index * increment;
            }

            double topLeftLng     = this.viewArea.LocationTopLeft.Lng;
            double rightBottomLng = this.viewArea.LocationRightBottom.Lng;

            increment = (topLeftLng - rightBottomLng) / 10;
            for (int index = 0; index < dataPoints.Count; index++)
            {
                yArray[index] = topLeftLng + index * increment;
            }

            GMapOverlay        routes = new GMapOverlay("routes");
            List <PointLatLng> points = null;// new List<PointLatLng>();

            IDictionary <double, IList <Controller.DataPointBase> > results = Conrec.Contour(array, xArray, yArray, zArray);

            foreach (double key in results.Keys)
            {
                points = new List <PointLatLng>();
                IList <DataPointBase> list = results[key];
                foreach (DataPointBase dpoint in list)
                {
                    points.Add(new PointLatLng(dpoint.Lat, dpoint.Lon));
                }

                GMapRoute route = new GMapRoute(points, key.ToString());
                route.Stroke = new Pen(Color.Red, 1);
                routes.Routes.Add(route);
            }

            return(routes);
            //
        }
Ejemplo n.º 5
0
    public static Pixbuf Contour(ManagedDNN network, NeuralNetworkOptions opts, double threshold, ManagedArray x, int width, int height, int f1 = 0, int f2 = 0)
    {
        InitializeContour(11, width, height);

        var m = Rows(x);

        var xplot = new double[width];
        var yplot = new double[height];
        var data  = new double[height, width];

        minx = double.MaxValue;
        maxx = double.MinValue;

        miny = double.MaxValue;
        maxy = double.MinValue;

        f1 = f1 >= 0 && f1 < Cols(x) ? f1 : 0;
        f2 = f2 >= 0 && f2 < Cols(x) ? f2 : 0;

        for (var j = 0; j < m; j++)
        {
            minx = Math.Min(x[f1, j], minx);
            maxx = Math.Max(x[f1, j], maxx);

            miny = Math.Min(x[f2, j], miny);
            maxy = Math.Max(x[f2, j], maxy);
        }

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        minx = minx - 8 * deltax;
        maxx = maxx + 8 * deltax;
        miny = miny - 8 * deltay;
        maxy = maxy + 8 * deltay;

        deltax = (maxx - minx) / width;
        deltay = (maxy - miny) / height;

        // For predict
        for (var i = 0; i < width; i++)
        {
            xplot[i] = minx + i * deltax;
        }

        for (var i = 0; i < height; i++)
        {
            yplot[i] = miny + i * deltay;
        }

        var xx = new ManagedArray(2, height);

        for (var i = 0; i < width; i++)
        {
            for (var j = 0; j < height; j++)
            {
                xx[f1, j] = xplot[i];
                xx[f2, j] = yplot[j];
            }

            var p = network.Predict(xx, opts);

            for (var j = 0; j < height; j++)
            {
                data[i, j] = p[j];
            }

            ManagedOps.Free(p);
        }

        var z = new double[] { 0.6, 0.8, 1 };

        Conrec.Contour(data, xplot, yplot, z, ContourLine);

        Points(ContourGraph, network, opts, threshold, x, width, height, f1, f2);

        ManagedOps.Free(xx);

        var border = new Color(128, 128, 128);

        // Plot bounding box
        var cw = ContourGraph.Width - 1;
        var ch = ContourGraph.Height;

        Common.Line(ContourGraph, 0, 1, cw, 1, border);
        Common.Line(ContourGraph, cw, 1, cw, ch, border);
        Common.Line(ContourGraph, 0, ch, cw, ch, border);
        Common.Line(ContourGraph, 0, 1, 0, ch, border);

        return(ContourGraph);
    }
Ejemplo n.º 6
0
        private void ComputeWireTerrainContoursMesh()
        {
            if (sourceTerrain != null && 0 < cellCountX && 0 < cellCountZ && 0 < heightStep)
            {
                lineSegments.Clear();
                int vertexCountX = cellCountX + 1;
                int vertexCountZ = cellCountZ + 1;

                var   terrainData     = sourceTerrain.terrainData;
                float normalizedX     = 0;
                float normalizedZ     = 0;
                float normalizedXStep = 1f / (float)cellCountX;
                float normalizedZStep = 1f / (float)cellCountZ;

                double[,] heightsData = new double[vertexCountX, vertexCountZ];
                double[] xs = new double[vertexCountX];
                double[] ys = new double[vertexCountZ];

                Vector3 terrainSize = terrainData.size;
                float   h;
                for (int i = 0; i < vertexCountZ; i++)
                {
                    normalizedX = 0;
                    for (int j = 0; j < vertexCountX; j++)
                    {
                        h = terrainData.GetInterpolatedHeight(normalizedX, normalizedZ);
                        heightsData[j, i] = h;
                        xs[j]             = normalizedX * terrainSize.x;
                        normalizedX      += normalizedXStep;
                    }
                    ys[i]        = normalizedZ * terrainSize.z;
                    normalizedZ += normalizedZStep;
                }

                var terrainHeight = terrainSize.y;
                contoursLevelsCount = (int)(terrainHeight / heightStep) + 1;
                double[] zs = new double[contoursLevelsCount];
                for (int i = 0; i < contoursLevelsCount; i++)
                {
                    zs[i] = heightStart + i * heightStep;
                }

                Conrec.Contour(heightsData, xs, ys, zs, CollectLineSegment);

                if (optimize)
                {
                    Vector3[] vertices;
                    int[]     indices;
                    WireTerrainUtils.Optimize(lineSegments, terrainSize.x / cellCountX, terrainSize.z / cellCountZ, terrainSize, out vertices, out indices);

                    if (drawBound)
                    {
                        WireTerrainUtils.AddBounds(terrainSize, ref vertices, ref indices);
                    }

                    WireTerrainUtils.GenerateMesh(transform, vertices, indices, material, terrainHeight, generateUV, flatten, InvokeCleanup);
                }
                else
                {
                    if (drawBound)
                    {
                        lineSegments.Add(Vector3.zero);
                        lineSegments.Add(new Vector3(terrainSize.x, 0, 0));
                        lineSegments.Add(new Vector3(terrainSize.x, 0, 0));
                        lineSegments.Add(new Vector3(terrainSize.x, 0, terrainSize.z));
                        lineSegments.Add(new Vector3(terrainSize.x, 0, terrainSize.z));
                        lineSegments.Add(new Vector3(0, 0, terrainSize.z));
                        lineSegments.Add(new Vector3(0, 0, terrainSize.z));
                        lineSegments.Add(Vector3.zero);
                    }

                    WireTerrainUtils.GenerateMesh(transform, lineSegments, material, terrainHeight, generateUV, flatten, InvokeCleanup);
                }
            }
        }
        private void AddCountours()
        {
            var gridSize = 16;
            var steps    = (1920 * 9 / gridSize);
            var data     = new double[steps + 2, steps + 2];

            var x = new double[steps + 2];
            var y = new double[steps + 2];

            for (var i = 0; i < steps + 2; i += 1)
            {
                for (var j = 0; j < steps + 2; j += 1)
                {
                    if (i == 0 || i == steps + 1 || j == 0 || j == steps + 1)
                    {
                        data[i, j] = 0;
                    }
                    else
                    {
                        var pos        = new Vector3((i - 1 - steps / 2) * gridSize, 0, (j - 1 - steps / 2) * gridSize);
                        var waterLevel = Singleton <TerrainManager> .instance.SampleRawHeightSmoothWithWater(pos, false, 0f);

                        var groundLevel = Singleton <TerrainManager> .instance.SampleRawHeightSmooth(pos);

                        data[i, j] = waterLevel - groundLevel;
                    }
                }
                x[i] = i * gridSize;
                y[i] = i * gridSize;
            }

            var z      = new double[] { 1.6 };
            var result = new Dictionary <Vector2, List <Vector2> > [z.Length];

            for (var i = 0; i < z.Length; i += 1)
            {
                result[i] = new Dictionary <Vector2, List <Vector2> >();
            }

            Conrec.Contour(data, x, y, z, result);

            var chains = Process(result[0]);

            chains = Simplify(chains);

            foreach (var chain in chains)
            {
                if (chain.Count == 0)
                {
                    continue;
                }
                var nds = new List <osmWayND>();
                foreach (var node in chain)
                {
                    nds.Add(new osmWayND {
                        @ref = AddNode(new Vector3(node.x - gridSize - steps * gridSize / 2, 0, node.y - gridSize - steps * gridSize / 2))
                    });
                }
                nds.Add(new osmWayND {
                    @ref = nds[0].@ref
                });
                var tags = new List <osmWayTag>();
                tags.Add(new osmWayTag {
                    k = "natural", v = "water"
                });

                wayCount += 1;
                ways.Add(new osmWay {
                    changeset = 50000000, id = (uint)wayCount, timestamp = DateTime.Now, user = "******", version = 1, nd = nds.ToArray(), tag = tags.ToArray()
                });
            }
        }