Exemple #1
0
        private float getClipped(Layer2DObject layer, int xph, int v)
        {
            int x = xph;
            int y = v;

            if (xph < layer.Resolution.X)
            {
                x = JryMath.Abs(xph);
            }
            if (v < layer.Resolution.Y)
            {
                y = JryMath.Abs(v);
            }

            if (xph >= layer.Resolution.X)
            {
                x = 2 * layer.Resolution.X - xph - 2;
            }
            if (v >= layer.Resolution.Y)
            {
                y = 2 * layer.Resolution.Y - v - 2;
            }



            return(layer[x, y].Value);
        }
Exemple #2
0
        public ILayerMasked Do(Vector2 size)
        {
            //int maxDim = (int)JryMath.Max(size.X, size.Y);
//CONSTANT - There is a problem with values that are not 2^n wher n is element of N

            var            maxDim        = 256;
            int            gradSize      = 8;
            IInterpolation interpolation = new LinearClipped();

            Layer2DObject map   = new Layer2DObject(size);
            int           hgrid = (int)size.X;
            int           vgrid = (int)size.Y;


            float gain       = _noiseParameters.Amplitude;
            float lacunarity = _noiseParameters.Lacunarity;

            var gradients = setupGradient(gradSize);

            //set up the random numbers table
            int[] permutations = getPermutaions(maxDim);

            int maxDimMinOne   = maxDim - 1;
            int gradSizeMinOne = gradSize - 1;

            for (int i = 0; i < vgrid; i++)
            {
                for (int j = 0; j < hgrid; j++)
                {
                    float pixel_value = 0.0f;

                    float amplitude = 1.0f;
                    float frequency = 1.0f / maxDim;

                    for (int k = _noiseParameters.FromDepth; k < _noiseParameters.ToDepth; k++)
                    {
                        int x = JryMath.Floor(j * frequency);
                        int y = JryMath.Floor(i * frequency);

                        float fracX = j * frequency - x;
                        float fracY = i * frequency - y;

                        // following two lines solved the bug.
                        x += k;
                        y += k;
                        IntVector4 v              = getIndices(permutations, maxDimMinOne, gradSizeMinOne, x, y);
                        Vector2[]  grads          = getGrads(gradients, v);
                        float      interpolatedxy = biInterpolate(interpolation, grads, fracX, fracY);

                        pixel_value += interpolatedxy * amplitude;
                        amplitude   *= gain;
                        frequency   *= lacunarity;
                    }

                    //put it in the map
                    map[j, i] = pixel_value;
                }
            }
            return(map);
        }
Exemple #3
0
        private void square(Layer2DObject layer, int size, float ap, int depth, IRandom2 rand)
        {
            int squareSize     = (int)(size * JryMath.Pow(.5f, depth));
            int squareHalfSize = (int)(squareSize / 2);

            for (int iy = 0; iy < JryMath.Pow(2, depth); iy++)
            {
                for (int ix = 0; ix < JryMath.Pow(2, depth); ix++)
                {
                    var xCorL = ix * squareSize;
                    var xCorR = (ix + 1) * squareSize;
                    var yCorT = iy * squareSize;
                    var yCorB = (iy + 1) * squareSize;

                    var tl  = layer[xCorL, yCorT].Value;
                    var bl  = layer[xCorL, yCorB].Value;
                    var tr  = layer[xCorR, yCorT].Value;
                    var br  = layer[xCorR, yCorB].Value;
                    int xph = xCorL + squareHalfSize;
                    int yph = yCorT + squareHalfSize;
                    var c   = (tl + tr + br + bl) / 4 + rnd(rand, xph, yph) * ap;
                    var b   = (br + bl + c + getClipped(layer, xph, yph + squareSize)) / 4 + rnd(rand, xph, yph + squareSize) * ap;
                    var t   = (tr + tl + c + getClipped(layer, xph, yph - squareSize)) / 4 + rnd(rand, xph, yph - squareSize) * ap;
                    var r   = (tr + br + c + getClipped(layer, xph + squareSize, yph)) / 4 + rnd(rand, xph + squareSize, yph) * ap;
                    var l   = (tl + bl + c + getClipped(layer, xph - squareSize, yph)) / 4 + rnd(rand, xph - squareSize, yph) * ap;

                    layer[xph, yCorT] = t;
                    layer[xph, yCorB] = b;
                    layer[xCorL, yph] = l;
                    layer[xCorR, yph] = r;
                }
            }
        }
Exemple #4
0
        public ILayerMasked Do(INoiseParameters param, Vector2 size)
        {
            var dt = DateTime.Now.Millisecond;

            System.Diagnostics.Debug.Print(dt + "Rand" + _rand.NextD(1, 2));

            var           pow      = (int)JryMath.Max(JryMath.Ceil(JryMath.Log(size.X, 2)), JryMath.Ceil(JryMath.Log(size.Y, 2)));
            int           sizePow2 = (int)JryMath.Pow(2, pow) + 1;
            Layer2DObject layer    = new Layer2DObject(sizePow2, sizePow2);

            setupBaseDS(sizePow2, layer, _rand);
            var par = param.BaseAmplitude * param.Amplitude;

            ds(layer, sizePow2, par, param, _rand);


            var croped = new Layer2DObject(size);

            for (int y = 0; y < size.Y; y++)
            {
                for (int x = 0; x < size.X; x++)
                {
                    croped[x, y] = layer[x, y];
                }
            }
            return(croped);
        }
Exemple #5
0
        // TODO create new generator that crates gradient primitives!
        private static Layer2DObject getCanyonProfile(List <Vector2> oldpoints, Vector2 resolution)
        {
            // polygon has to be centered
            var oldcenter         = oldpoints.Aggregate((a, b) => a + b) / oldpoints.Count;
            var maskCenter        = resolution * .5f;
            var ptTranslateVector = oldcenter - maskCenter;
            //var ptTranslateVector = Vector2.Zero;
            var points        = oldpoints.Select(r => r - ptTranslateVector).ToList();
            var center        = points.Aggregate((a, b) => a + b) / points.Count;
            var v             = new VerySpecificQuadraticVoronoiAreaDrawer();
            var canyonProfile = new Layer2DObject(resolution);

            var segs = points.Select((t, i) => new LineSegment(t, points[(i + 1) % points.Count])).ToList();

            v.PrintToLayer(canyonProfile, new List <IArea>()
            {
                new Area()
                {
                    Center   = center,
                    Points   = points,
                    Segments = segs
                }
            }, Vector2.Zero);
            return(canyonProfile);
        }
Exemple #6
0
        private void ds(Layer2DObject layer, int sizePow2, float ba, INoiseParameters np, IRandom2 rand)
        {
            int size = sizePow2 - 1;
            int log  = (int)JryMath.Log(size, 2);

            // HACK
            var x = np as DsNoiseParameters;

            for (int l = 0; l < log; l++)
            {
                if (l == 0 && x != null)
                {
                    hackeddiamond(layer, ba, x.HillProbability);
                }
                else
                {
                    diamond(layer, size, ba, l, rand);
                }


                square(layer, size, ba, l, rand);
                ba *= np.Amplitude;
                if (l >= np.ToDepth)
                {
                    ba *= np.Amplitude;
                }
            }
        }
        public ILayerMasked GetCachedZoomedHeightmap()
        {
            if (HeightmapLayer == null)
            {
                return(null);
            }
            if (Zoom == 1)
            {
                return(HeightmapLayer);
            }
            if (Zoom == _lastZoom && _cachedZoomedLayer != null && _version == HeightmapVersion)
            {
                return(_cachedZoomedLayer);
            }
            _lastZoom = Zoom;
            _version  = HeightmapVersion;
            Layer2DObject l = new Layer2DObject(GetRecalculatedSize());

            float xScale = (float)HeightmapLayer.Resolution.X / l.Resolution.X;
            float yScale = (float)HeightmapLayer.Resolution.Y / l.Resolution.Y;

            for (int y = 0; y < l.Resolution.Y; y++)
            {
                for (int x = 0; x < l.Resolution.X; x++)
                {
                    var oldX = (int)(x * xScale);
                    var oldY = (int)(y * yScale);
                    l[x, y] = HeightmapLayer[oldX, oldY];
                }
            }
            _cachedZoomedLayer = l;
            return(l);
        }
Exemple #8
0
        public async Task SaveLayers()
        {
            Blur b  = new Blur();
            var  dr = new BallDrainageSimulator();

            var vl = new VisualLogger();

            var file   = "SavedFiles/rvrnw.bmp";
            var i      = Image.FromFile(file);
            var bitmap = new Bitmap(i);

            var layer = new LayerConverter().LoadLayer(bitmap);



            ILayer blured = new Layer2DObject(layer.Resolution);

            //b.Process(layer, blured, 3);
            blured = layer;
            vl.Log(blured, "base");
            var drainagemap = dr.GetDrainageMap(blured, 10);

            //logger shit
            drainagemap[0, 0] = 0;
            vl.Log(drainagemap, "drainage");
        }
Exemple #9
0
        public ILayerMasked RenderBitmapToHeightMap(Bitmap bitmap)
        {
            int hei   = bitmap.Height;
            int wid   = bitmap.Width;
            var layer = new Layer2DObject(wid, hei);

            BitmapData dataOut = bitmap.LockBits(new Rectangle(0, 0, wid, hei), ImageLockMode.WriteOnly, bitmap.PixelFormat);

            unsafe
            {
                int dO = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8;  // pixel size in bytes

                for (int yo = 0; yo < hei; yo++)
                {
                    byte *optr = (byte *)dataOut.Scan0 + yo * dataOut.Stride;

                    for (int xo = 0; xo < wid; xo++)
                    {
                        //COLOR here

                        var color = toGrayScale(optr[0], optr[1], optr[2]);

                        layer[xo, yo] = color;

                        optr += dO;
                    }
                }
            }
            bitmap.UnlockBits(dataOut);

            return(layer);
        }
        public override Task <ILayerMasked> GenerateLayer()
        {
            var pts    = _layerLocalParameters.Polygon.Points.ToList();
            var bounds = new GeometryUtils().GetBoundsForPoints(pts);
            var mask   = obtainBluredMask(pts,
                                          _layerLocalParameters.BlurSize,
                                          _layerLocalParameters.ExtendSize);


            var size  = mask.Resolution;
            int count = (int)JryMath.Max(size.X, size.Y) / 100 + 1;
            var areas = _voronoiAreGenerator.GenerateAreas(size, bounds.GetSize(), count);

            _areaLoggerWithFill.LogAreas(size, areas, "loggedAreasFilled");

            var layer     = new Layer2DObject(new Vector2(size.X + _layerLocalParameters.ExtendSize, size.Y + _layerLocalParameters.ExtendSize));
            var layerSize = layer.Resolution;
            var deltaX    = (layerSize.X - size.X) / 2;
            var deltaY    = (layerSize.Y - size.Y) / 2;

            var translateVector = new Vector2(deltaX, deltaY);

            _voronoiAreaDrawer.PrintToLayer(layer, areas, translateVector);
            layer.Mask = mask;
            _logger.Log(layer, "mountain");
            applyOffset(_layerLocalParameters.ExtendSize, _layerLocalParameters.Polygon.Points.ToList(), layer);
            _layUtils.Normalize(layer);
            layer.Mask = mask;
            return(Task.FromResult((ILayerMasked)layer));
        }
Exemple #11
0
        public Layer2DObject[] Generate(IWorld world)
        {
            int x = (int)world.Parameters.BitmapResolution.X;
            int y = (int)world.Parameters.BitmapResolution.Y;

            Layer2DObject layer = new Layer2DObject(world.Parameters.BitmapResolution);

            return(new Layer2DObject[] { layer });
        }
Exemple #12
0
        private void hackeddiamond(Layer2DObject layer, float ap, float he)
        {
            var s  = layer.Resolution.X - 1;
            var tl = layer[0, 0].Value;
            var bl = layer[0, s].Value;
            var tr = layer[s, 0].Value;
            var br = layer[s, s].Value;
            var c  = (tl + tr + br + bl) / 4 + (he - .5f) * ap;

            //var c = he-.5f * ap ;
            layer[s / 2, s / 2] = c;
        }
        private ILayerMasked drawSiteToLayer(IntVector2 size, IEnumerable <IArea> areas)
        {
            var layer = new Layer2DObject(size.X, size.Y);

            _layerUtility.IterateValues(layer, (x, y, va) => layer[x, y] = 0);
            var layerDrawer = new LayerDrawer(layer);

            foreach (var area in areas)
            {
                layerDrawer.DrawArea(area);
            }
            return(layer);
        }
Exemple #14
0
        public ILayer LoadLayer(Bitmap m)
        {
            var l = new Layer2DObject(m.Size.Width, m.Size.Height);

            for (int y = 0; y < l.Resolution.Y; y++)
            {
                for (int x = 0; x < l.Resolution.X; x++)
                {
                    l[x, y] = (float)m.GetPixel(x, y).R / 256;
                }
            }

            return(l);
        }
        public void BeginPaint(Vector2 vector2)
        {
            if (!CanPaint())
            {
                return;
            }

            _beforeUpdateLayer = new Layer2DObject(HeightmapLayer.Resolution);

            utils.CloneFromTo(HeightmapLayer, _beforeUpdateLayer);
            LayerPainter.InitializeSource(this.HeightmapLayer);
            var rec = zoomManager.RecalcZoomDiv(vector2);

            LayerPainter.BeginBrushpath(rec, SelectedPluginTool, BrushSize, (float)BrushStrenght / 100, (float)BrushFade / 100);
        }
        public async Task Compose(IWorld w)
        {
            var param  = w.Parameters;
            var fields = w.Fields;

            var pars = await generateFields(param, fields);

            IErosionDescriptor bs;

            ILayerMasked    baseLayer;
            IBlendModeAlpha ba;

            if (w.UseBase)
            {
                var bsd = _describer.DescribeBaseLayer(param, w.BaseField);
                //baseLayer = await bsd.LayerGlobalParameters.Profile
                baseLayer = await bsd.LayerGlobalParameters.Generator.GenerateLayer();

                //_layerUtils.Normalize(baseLayer);
                //HACK baselayers mask extend it size
                var x = new Layer2DObject(w.Parameters.BitmapResolution);
                _layerUtils.IterateValues(x, (v, val) => x[v] = baseLayer[v]);
                baseLayer = x;

                ba = bsd.AlphaBlend;
                bs = bsd;
            }
            else
            {
                baseLayer      = new Layer2DObject(w.Parameters.BitmapResolution);
                baseLayer.Mask = new Mask(baseLayer.Resolution);
                _layerUtils.IterateValues(baseLayer.Mask, (v, val) => baseLayer[v] = 1);

                setUpDefaulValue(baseLayer);
                bs = new ErosionDescriptor();
                ba = new AlphaBlend();
            }

            bs.HydraulicErosionParams.Strenght = param.ErosionStrength * strengthMultpilier;

            ILayerMasked merged = merge(ba, baseLayer, pars);

            //_layerUtils.FitInto(merged, 0, 1);
            postProcessWholeLayer(merged, bs, w.Parameters.RiverAmount);
            ComposedLayer = merged;
        }
Exemple #17
0
        private ILayer slightlyNoiseLayer(ILayer input)
        {
            var noise = getNoise(input);

            new VisualLogger().Log(noise, "noise");
            ILayer baseLayer = new Layer2DObject(input.Resolution);

            _layerUtility.IterateValues(baseLayer,
                                        (vv, val) => baseLayer[vv] = 0);

            ILayer layerNoised = new Layer2DObject(input.Resolution);

            _layerUtility.IterateValues(input,
                                        (vv, val) => layerNoised[vv] = val + .05f * noise[vv]);
            new VisualLogger().Log(layerNoised, "noised");
            return(layerNoised);
        }
Exemple #18
0
        private void setupBaseDS(int sizePow2, Layer2DObject layer, IRandom2 rand)
        {
            int blockSize = sizePow2;
            int xstart    = 0;
            int ystart    = 0;
            int xend      = xstart + blockSize - 1;
            int yend      = ystart + blockSize - 1;

            var tl = rnd(rand, xstart, ystart);
            var tr = rnd(rand, xend, ystart);
            var bl = rnd(rand, xstart, yend);
            var br = rnd(rand, xend, yend);

            layer[xend, ystart]   = tr;
            layer[xstart, yend]   = bl;
            layer[xstart, ystart] = tl;
            layer[xend, yend]     = br;
        }
Exemple #19
0
        private static ILayer printMountainProfile(int countOfCells, ISegmendDivider sd)
        {
            var l = new Layer2DObject(res);

            // TODO change rand approach

            var rnd = new Rand(seed);


            var c  = new VoronoiConverter(sd);
            var g  = new VoronoiGenerator(c);
            var gg = new VoronoiAreaGenerator(g, rnd);

            var vd = new VoronoiAreaDrawer();


            var areas = gg.GenerateAreas(res, res, countOfCells);

            vd.PrintToLayer(l, areas, Vector2.Zero);
            return(l);
        }
Exemple #20
0
        public override async Task <ILayerMasked> GenerateLayer()
        {
            var layers = await generateLayers();

            var x      = layers.Min(r => r.Layer.Resolution.X);
            var y      = layers.Min(r => r.Layer.Resolution.Y);
            var size   = new Vector2(x, y);
            var merged = new Layer2DObject(x, y);

            // move to layerUtils class
            foreach (var layerWithInfluence in layers)
            {
                var inf   = layerWithInfluence.Influence;
                var layer = layerWithInfluence.Layer;
                // TODO use paralallel and atomic addition
                // disable cache
                for (int i = 0; i < size.Y; i++)
                {
                    for (int j = 0; j < size.X; j++)
                    {
                        var influencedValue = inf * layer[j, i];
                        var mergedValue     = merged[j, i];
                        merged[j, i] =
                            mergedValue.HasValue ?
                            mergedValue + influencedValue :
                            influencedValue;
                    }
                }
            }
            merged.Mask = obtainBluredMask(_layerLocalParameters.Polygon.Points.ToArray()
                                           , _layerLocalParameters.BlurSize
                                           , _layerLocalParameters.ExtendSize);

            applyOffset(_layerLocalParameters.ExtendSize
                        , _layerLocalParameters.Polygon.Points.ToArray()
                        , merged);
            _logger.Log(merged, "combined");
            return(merged);
        }
Exemple #21
0
        public ILayer GetDrainageMap(ILayer input, int riverCount)
        {
            var    layerNoised  = slightlyNoiseLayer(input);
            var    localMaximae = getLocalMaximaRecord(layerNoised, riverCount);
            uint   i            = 0;
            ILayer finalLayer   = new Layer2DObject(input.Resolution);

            foreach (var max in localMaximae)
            {
                ILayer layer = new Layer2DObject(input.Resolution);
                drawOnLayer(layerNoised, layer, max, ++i);
                _layerUtility.IterateValues(layer, (vv, val) =>
                {
                    if (val.HasValue && val.Value == riverMark)
                    {
                        finalLayer[vv] = 1;
                    }
                    ;
                });
            }
            return(finalLayer);
        }
Exemple #22
0
        private static void printVoronoi(int countOfCells, ISegmendDivider sd)
        {
            var l = new Layer2DObject(res);

            // TODO change rand approach

            var rnd = new Rand(seed);


            var c  = new VoronoiConverter(sd);
            var g  = new VoronoiGenerator(c);
            var gg = new VoronoiAreaGenerator(g, rnd);

            var vd = new VoronoiAreaDrawer();

            var areas = gg.GenerateAreas(res, res * .5f, countOfCells);



            vd.PrintToLayer(l, areas, new Vector2());
            var vl = new VisualLogger();

            vl.Log(l, "profile");
        }
            public ILayerMasked Process(ILayer inImage, int radial, float ignoreLevel)
            {
                //everything that is above ignorelevel is ignored
                var rct    = inImage.Resolution;
                var source = new float?[rct.X * rct.Y];

                var width  = inImage.Resolution.X;
                var height = inImage.Resolution.Y;

                var _greyScale = new float?[width * height];

                int xSize = inImage.Resolution.X;
                int ySize = inImage.Resolution.Y;

                for (int y = 0; y < ySize; y++)
                {
                    for (int x = 0; x < xSize; x++)
                    {
                        var i   = y * xSize + x;
                        var val = inImage[x, y];
                        source[i] = nullToZero(val);
                    }
                }

                Parallel.For(0, source.Length, _pOptions, i =>
                {
                    _greyScale[i] = nullToZero(source[i]);
                });

                var newGrayScale = new float?[width * height];
                var dest         = new float?[width * height];

                Parallel.Invoke(
                    () => gaussBlur_4(_greyScale, newGrayScale, radial, width, height));

                Parallel.For(0, dest.Length, _pOptions, i =>
                {
                    var val = newGrayScale[i];
                    if (val > 1)
                    {
                        val = 1;
                    }
                    if (val < 0)
                    {
                        val = 0;
                    }


                    dest[i] = val;
                });

                var image = new Layer2DObject(width, height);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        var i   = y * width + x;
                        var val = dest[i];
                        if (val.HasValue && val.Value < ignoreLevel)
                        {
                            image[x, y] = val;
                        }
                        else
                        {
                            image[x, y] = inImage[x, y];
                        }
                    }
                }

                return(image);
            }