public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            double RRe = R.Real;
            double RIm = R.Imaginary;
            double r0, i0, r1, i1;
            double dr, di;
            double r, i;
            double aspect;
            int    x, y;
            int    n, N;

            N = Palette.Length;

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int size = Width * Height;

            double[] ColorIndex = new double[size];
            double   Conv       = 1e-10;
            double   Div        = 1e10;

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 == null || Row3 == null || Row4 == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }
                    else if (Row2.Length != c || Row3.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row2.Length.ToString() + ", " + Row3.Length.ToString() + " and " + Row4.Length.ToString() +
                                                         ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        j  = Offset[x];
                        z  = Row2[x];
                        z2 = Row3[x];
                        z3 = Row4[x];

                        z       = R2 * z * z2 / (2 * z2 * z2 - z * z3);
                        Row[x] -= z;

                        Mod = z.Magnitude;

                        if (Mod > Conv && Mod < Div)
                        {
                            if (x != x2)
                            {
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                ColorIndex[j++] = N;
                            }
                            else
                            {
                                ColorIndex[j++] = n;
                            }
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }
            }

            Node.Expression.Preview(new GraphBitmap(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette)));

            double[] Boundary = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
            FractalGraph.Smooth(ColorIndex, Boundary, Width, Height, N, Palette, Node, Variables);

            return(new FractalGraph(FractalGraph.ToBitmap(ColorIndex, Width, Height, Palette),
                                    r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
        }
示例#2
0
        public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            byte[]  reds;
            byte[]  greens;
            byte[]  blues;
            double  RRe = R.Real;
            double  RIm = R.Imaginary;
            double  r0, i0, r1, i1;
            double  dr, di;
            double  r, i;
            double  aspect;
            int     x, y;
            int     n, N;
            SKColor cl;

            N      = Palette.Length;
            reds   = new byte[N];
            greens = new byte[N];
            blues  = new byte[N];

            for (x = 0; x < N; x++)
            {
                cl        = Palette[x];
                reds[x]   = cl.Red;
                greens[x] = cl.Green;
                blues[x]  = cl.Blue;
            }

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int    size = Width * Height * 4;
            double Conv = 1e-10;
            double Div  = 1e10;

            byte[] rgb = new byte[size];

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 == null || Row3 == null || Row4 == null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }
                    else if (Row2.Length != c || Row3.Length != c)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Length returned: " +
                                                         Row2.Length.ToString() + ", " + Row3.Length.ToString() + " and " + Row4.Length.ToString() +
                                                         ". Expected: " + c.ToString(), Node);
                    }

                    for (x = x2 = 0; x < c; x++)
                    {
                        j  = Offset[x];
                        z  = Row2[x];
                        z2 = Row3[x];
                        z3 = Row4[x];

                        z       = R2 * z * z2 / (2 * z2 * z2 - z * z3);
                        Row[x] -= z;

                        Mod = z.Magnitude;

                        if (Mod > Conv && Mod < Div)
                        {
                            if (x != x2)
                            {
                                Offset[x2] = j;
                            }

                            x2++;
                        }
                        else
                        {
                            if (n >= N)
                            {
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                                rgb[j++] = 0;
                            }
                            else
                            {
                                rgb[j++] = blues[n];
                                rgb[j++] = greens[n];
                                rgb[j++] = reds[n];
                            }

                            rgb[j++] = 255;
                        }
                    }

                    if (x2 < x)
                    {
                        Array.Resize <Complex>(ref Row, x2);
                        Array.Resize <int>(ref Offset, x2);
                        c = x2;
                    }
                }
            }

            using (SKData Data = SKData.Create(new MemoryStream(rgb)))
            {
                SKImage Bitmap = SKImage.FromPixelData(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width * 4);
                return(new FractalGraph(Bitmap, r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State));
            }
        }
示例#3
0
        public static FractalGraph CalcHalley(double rCenter, double iCenter, double rDelta, Complex R,
                                              ILambdaExpression f, ScriptNode fDef, Variables Variables, SKColor[] Palette, int Width, int Height,
                                              ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
        {
            byte[]  reds;
            byte[]  greens;
            byte[]  blues;
            double  RRe = R.Real;
            double  RIm = R.Imaginary;
            double  r0, i0, r1, i1;
            double  dr, di;
            double  r, i;
            double  aspect;
            int     x, y;
            int     n, N;
            SKColor cl;

            N      = Palette.Length;
            reds   = new byte[N];
            greens = new byte[N];
            blues  = new byte[N];

            for (x = 0; x < N; x++)
            {
                cl        = Palette[x];
                reds[x]   = cl.Red;
                greens[x] = cl.Green;
                blues[x]  = cl.Blue;
            }

            Variables v = new Variables();

            Variables.CopyTo(v);

            string ParameterName;

            if (!(f is IDifferentiable Differentiable) ||
                !(Differentiable.Differentiate(ParameterName = Differentiable.DefaultVariableName, v) is ILambdaExpression fPrim))
            {
                throw new ScriptRuntimeException("Lambda expression not differentiable.", Node);
            }

            if (!(fPrim is IDifferentiable Differentiable2) ||
                !(Differentiable2.Differentiate(ParameterName, v) is ILambdaExpression fBis))
            {
                throw new ScriptRuntimeException("Lambda expression not twice differentiable.", Node);
            }

            int    size = Width * Height * 4;
            double Conv = 1e-10;
            double Div  = 1e10;

            byte[] rgb = new byte[size];

            Complex[]  Row;
            Complex[]  Row2;
            Complex[]  Row3;
            Complex[]  Row4;
            int[]      Offset;
            IElement[] P = new IElement[1];
            int        j, c, x2;
            IElement   Obj, Obj2, Obj3;
            double     Mod;
            Complex    z, z2, z3;
            Complex    R2 = R * 2;

            rDelta *= 0.5;
            r0      = rCenter - rDelta;
            r1      = rCenter + rDelta;

            aspect = ((double)Width) / Height;

            i0 = iCenter - rDelta / aspect;
            i1 = iCenter + rDelta / aspect;

            dr = (r1 - r0) / Width;
            di = (i1 - i0) / Height;

            for (y = 0, i = i0; y < Height; y++, i += di)
            {
                Row    = new Complex[Width];
                Offset = new int[Width];

                c = Width;
                for (x = 0, x2 = y * Width * 4, r = r0; x < Width; x++, r += dr, x2 += 4)
                {
                    Row[x]    = new Complex(r, i);
                    Offset[x] = x2;
                }

                n = 0;
                while (n < N && c > 0)
                {
                    n++;
                    P[0] = Expression.Encapsulate(Row);
                    Obj  = f.Evaluate(P, v);
                    Obj2 = fPrim.Evaluate(P, v);
                    Obj3 = fBis.Evaluate(P, v);
                    Row2 = Obj.AssociatedObjectValue as Complex[];
                    Row3 = Obj2.AssociatedObjectValue as Complex[];
                    Row4 = Obj3.AssociatedObjectValue as Complex[];

                    if (Row2 is null || Row3 is null || Row4 is null)
                    {
                        throw new ScriptRuntimeException("Lambda expression (and its first and second derivative) must be able to accept complex vectors, " +
                                                         "and return complex vectors of equal length. Type returned: " +
                                                         Obj.GetType().FullName + ", " + Obj2.GetType().FullName + " and " + Obj3.GetType().FullName,
                                                         Node);
                    }