Example #1
0
        void Combine(BinaryMap source, RectangleC area, Point at, CombineFunction function)
        {
            int shift      = (int)((uint)area.X & WordMask) - (int)((uint)at.X & WordMask);
            int vectorSize = (area.Width >> WordShift) + 2;

            Parallel.For(0, area.Height,
                         () => new CombineLocals {
                Vector = new uint[vectorSize], SrcVector = new uint[vectorSize]
            },
                         delegate(int y, ParallelLoopState state, CombineLocals locals)
            {
                LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
                source.LoadLine(locals.SrcVector, new Point(area.X, area.Y + y), area.Width);
                if (shift >= 0)
                {
                    ShiftLeft(locals.SrcVector, shift);
                }
                else
                {
                    ShiftRight(locals.SrcVector, -shift);
                }
                function(locals.Vector, locals.SrcVector);
                SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
                return(locals);
            }, locals => { });
        }
Example #2
0
        public void AddTwoIntegersUsingParameters()
        {
            IFunction add    = new CombineFunction(new CombineFunction(new AddIntegerFunction(), new ParameterFunction(0, 0)), new ParameterFunction(1, 0));
            IFunction result = add.Bind(new IFunction[] { new ConstantFunction(1), new ConstantFunction(2) });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ConstantFunction));
            Assert.AreEqual(3, result.Value);
        }
Example #3
0
 public static CombiningFunc GetFunction(CombineFunction combineFunc)
 {
     CombiningFunc ret;
     switch(combineFunc)
     {
     case CombineFunction.D1:
         ret = new CombiningFunc(D1);
         break;
     case CombineFunction.D2MinusD1:
         ret = new CombiningFunc(D2MinusD1);
         break;
     case CombineFunction.D3MinusD1:
         ret = new CombiningFunc(D3MinusD1);
         break;
     default:
         ret = new CombiningFunc(D2MinusD1);
         break;
     }
     return ret;
 }
Example #4
0
        public static float Noise3D(Vector3 input, float seed, DistanceFunction distanceFunction = DistanceFunction.EUCLIDIAN, CombineFunction combineFunction = CombineFunction.d0)
        {
            var randomDiff   = Vector3.zero;
            var featurePoint = Vector3.zero;

            int cubeX, cubeY, cubeZ;

            var distanceArray = new float[3];

            for (var i = 0; i < distanceArray.Length; i++)
            {
                distanceArray[i] = Mathf.Infinity;
            }

            var evalCubeX = Mathf.FloorToInt(input.x);
            var evalCubeY = Mathf.FloorToInt(input.y);
            var evalCubeZ = Mathf.FloorToInt(input.z);

            for (sbyte i = -1; i < 2; ++i)
            {
                for (sbyte j = -1; j < 2; ++j)
                {
                    for (sbyte k = -1; k < 2; ++k)
                    {
                        cubeX = evalCubeX + i;
                        cubeY = evalCubeY + j;
                        cubeZ = evalCubeZ + k;

                        var lastRandom = LCGRandom(Hash((uint)(cubeX + seed), (uint)(cubeY), (uint)(cubeZ)));

                        uint numberFeaturePoints = 1;

                        for (uint l = 0; l < numberFeaturePoints; ++l)
                        {
                            lastRandom   = LCGRandom(lastRandom);
                            randomDiff.x = (float)lastRandom / 0x100000000;

                            lastRandom   = LCGRandom(lastRandom);
                            randomDiff.y = (float)lastRandom / 0x100000000;

                            lastRandom   = LCGRandom(lastRandom);
                            randomDiff.z = (float)lastRandom / 0x100000000;

                            featurePoint.x = randomDiff.x + (float)cubeX;
                            featurePoint.y = randomDiff.y + (float)cubeY;
                            featurePoint.z = randomDiff.z + (float)cubeZ;

                            switch (distanceFunction)
                            {
                            case DistanceFunction.EUCLIDIAN:
                                Insert(distanceArray, EuclidianDistanceFunc3(input, featurePoint));
                                break;

                            case DistanceFunction.MANHATTAN:
                                Insert(distanceArray, ManhattanDistanceFunc3(input, featurePoint));
                                break;

                            case DistanceFunction.CHEBYSHEV:
                                Insert(distanceArray, ChebyshevDistanceFunc3(input, featurePoint));
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
            }

            var combine = 0f;

            switch (combineFunction)
            {
            case CombineFunction.d0:
                combine = CombineFunc_D0(distanceArray);
                break;

            case CombineFunction.d1:
                combine = CombineFunc_D1_D0(distanceArray);
                break;

            case CombineFunction.d2:
                combine = CombineFunc_D2_D0(distanceArray);
                break;

            default:
                combine = 0.0f;
                break;
            }

            return(Mathf.Clamp(combine * 2.0f - 1.0f, -1.0f, 1.0f));
        }
Example #5
0
        public static float CellNoise(Vector3 position, float seed, DistanceFunction distanceFunction = DistanceFunction.EUCLIDIAN, CombineFunction combineFunction = CombineFunction.d0)
        {
            var gain      = 1.0f;
            var value     = 0.0f;
            var octaves   = 4;
            var frequency = 2.0f;
            var amplitude = 0.5f;

            for (byte i = 0; i < octaves; i++)
            {
                value += Noise3D(new Vector3(position.x * gain * frequency, position.y * gain * frequency, position.z * gain * frequency), seed, distanceFunction, combineFunction) * amplitude / gain;
                gain  *= 2f;
            }

            return(value);
        }
Example #6
0
 void Combine(BinaryMap source, RectangleC area, Point at, CombineFunction function)
 {
     int shift = (int)((uint)area.X & WordMask) - (int)((uint)at.X & WordMask);
     int vectorSize = (area.Width >> WordShift) + 2;
     Parallel.For(0, area.Height,
         () => new CombineLocals { Vector = new uint[vectorSize], SrcVector = new uint[vectorSize] },
         delegate(int y, ParallelLoopState state, CombineLocals locals)
         {
             LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
             source.LoadLine(locals.SrcVector, new Point(area.X, area.Y + y), area.Width);
             if (shift >= 0)
                 ShiftLeft(locals.SrcVector, shift);
             else
                 ShiftRight(locals.SrcVector, -shift);
             function(locals.Vector, locals.SrcVector);
             SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
             return locals;
         }, locals => { });
 }
Example #7
0
        public static float Noise3D(Vector3 input, float seed, 
								DistanceFunction distanceFunction = DistanceFunction.EUCLIDIAN, 
								CombineFunction combineFunction = CombineFunction.d0)
        {
            uint lastRandom, numberFeaturePoints;
            Vector3 randomDiff = Vector3.zero;
            Vector3 featurePoint = Vector3.zero;
            int cubeX, cubeY, cubeZ;

            float[] distanceArray = new float[3];
            for (int i = 0; i < distanceArray.Length; i++)
            distanceArray[i] = Mathf.Infinity;

            int evalCubeX = Mathf.FloorToInt(input.x);
            int evalCubeY = Mathf.FloorToInt(input.y);
            int evalCubeZ = Mathf.FloorToInt(input.z);

            for (int i = -1; i < 2; ++i) {
            for (int j = -1; j < 2; ++j) {
                for (int k = -1; k < 2; ++k) {
                    cubeX = evalCubeX + i;
                    cubeY = evalCubeY + j;
                    cubeZ = evalCubeZ + k;

                    lastRandom = lcgRandom(hash((uint)(cubeX + seed), (uint)(cubeY), (uint)(cubeZ)));

                    numberFeaturePoints = 1;

                    for (uint l = 0; l < numberFeaturePoints; ++l) {
                        lastRandom = lcgRandom(lastRandom);
                        randomDiff.x = (float)lastRandom / 0x100000000;

                        lastRandom = lcgRandom(lastRandom);
                        randomDiff.y = (float)lastRandom / 0x100000000;

                        lastRandom = lcgRandom(lastRandom);
                        randomDiff.z = (float)lastRandom / 0x100000000;

                        featurePoint.x = randomDiff.x + (float)cubeX;
                        featurePoint.y = randomDiff.y + (float)cubeY;
             						featurePoint.z = randomDiff.z + (float)cubeZ;

                        switch(distanceFunction) {
                        case DistanceFunction.EUCLIDIAN:
                            insert(distanceArray, EuclidianDistanceFunc3(input, featurePoint));
                            break;
                        case DistanceFunction.MANHATTAN:
                            insert(distanceArray, ManhattanDistanceFunc3(input, featurePoint));
                            break;
                        case DistanceFunction.CHEBYSHEV:
                            insert(distanceArray, ChebyshevDistanceFunc3(input, featurePoint));
                            break;
                        }
                    }
                }
            }
            }

            float combine = 0f;
            switch(combineFunction) {
            case CombineFunction.d0:
            combine = CombineFunc_D0(distanceArray);
            break;
            case CombineFunction.d1:
            combine = CombineFunc_D1_D0(distanceArray);
            break;
            case CombineFunction.d2:
            combine = CombineFunc_D2_D0(distanceArray);
            break;
            }
            return Mathf.Clamp(combine * 2f - 1f, -1f, 1f);
        }