Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NovaSmoothFractal.cs
1using System;
2using System.Numerics;
3using System.Text;
4using SkiaSharp;
10
12{
32 {
37 ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
38 : base(new ScriptNode[] { r, i, dr, R, p, Palette, DimX, DimY },
39 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
40 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar,
42 {
43 }
44
49 ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
50 : base(new ScriptNode[] { r, i, dr, R, p, Palette, DimX },
51 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
52 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar },
54 {
55 }
56
61 ScriptNode Palette, int Start, int Length, Expression Expression)
62 : base(new ScriptNode[] { r, i, dr, R, p, Palette },
63 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
64 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector },
66 {
67 }
68
74 : base(new ScriptNode[] { r, i, dr, R, p },
75 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
76 ArgumentType.Scalar, ArgumentType.Scalar },
78 {
79 }
80
84 public override string[] DefaultArgumentNames
85 {
86 get
87 {
88 return new string[] { "r", "i", "dr", "R", "p", "Palette", "DimX", "DimY" };
89 }
90 }
91
96 {
97 string ColorExpression = null;
98 SKColor[] Palette;
99 object Obj;
100 double rc, ic;
101 double dr;
102 double Rr, Ri, pr, pi;
103 int dimx, dimy;
104 int i, c;
105
106 rc = Expression.ToDouble(Arguments[0].AssociatedObjectValue);
107 ic = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
108 dr = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
109
110 if ((Obj = Arguments[3].AssociatedObjectValue) is Complex)
111 {
112 Complex z = (Complex)Obj;
113 Rr = z.Real;
114 Ri = z.Imaginary;
115 }
116 else
117 {
118 Rr = Expression.ToDouble(Arguments[3].AssociatedObjectValue);
119 Ri = 0;
120 }
121
122 if ((Obj = Arguments[4].AssociatedObjectValue) is Complex)
123 {
124 Complex z = (Complex)Obj;
125 pr = z.Real;
126 pi = z.Imaginary;
127 }
128 else
129 {
130 pr = Expression.ToDouble(Arguments[4].AssociatedObjectValue);
131 pi = 0;
132 }
133
134 c = Arguments.Length;
135 i = 5;
136
137 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
138 {
139 ColorExpression = this.Arguments[i].SubExpression;
141 }
142 else
143 {
144 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
145 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
146
147 if (i < c && this.Arguments[i] is null)
148 i++;
149 }
150
151 if (i < c)
152 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
153 else
154 dimx = 320;
155
156 if (i < c)
157 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
158 else
159 dimy = 200;
160
161 if (i < c)
162 {
163 throw new ScriptRuntimeException("Parameter mismatch in call to NovaSmoothFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
164 this);
165 }
166
167 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
168 throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
169
170 return CalcNova(rc, ic, dr, Rr, Ri, pr, pi, Palette, dimx, dimy, this, Variables, this.FractalZoomScript,
171 new object[] { Palette, dimx, dimy, Rr, Ri, pr, pi, ColorExpression });
172 }
173
174 private string FractalZoomScript(double r, double i, double Size, object State)
175 {
176 object[] Parameters = (object[])State;
177 int DimX = (int)Parameters[1];
178 int DimY = (int)Parameters[2];
179 double Rr = (double)Parameters[3];
180 double Ri = (double)Parameters[4];
181 double pr = (double)Parameters[5];
182 double pi = (double)Parameters[6];
183 string ColorExpression = (string)Parameters[7];
184
185 StringBuilder sb = new StringBuilder();
186
187 sb.Append("NovaSmoothFractal(");
188 sb.Append(Expression.ToString(r));
189 sb.Append(',');
190 sb.Append(Expression.ToString(i));
191 sb.Append(',');
192 sb.Append(Expression.ToString(Size / 4));
193 sb.Append(',');
194 sb.Append(Expression.ToString(Rr));
195
196 if (Ri != 0)
197 {
198 sb.Append('+');
199 sb.Append(Expression.ToString(Ri));
200 sb.Append("*i");
201 }
202
203 sb.Append(',');
204 sb.Append(Expression.ToString(pr));
205
206 if (pi != 0)
207 {
208 sb.Append('+');
209 sb.Append(Expression.ToString(pi));
210 sb.Append("*i");
211 }
212
213 if (!string.IsNullOrEmpty(ColorExpression))
214 {
215 sb.Append(',');
216 sb.Append(ColorExpression);
217 }
218
219 sb.Append(',');
220 sb.Append(DimX.ToString());
221 sb.Append(',');
222 sb.Append(DimY.ToString());
223 sb.Append(')');
224
225 return sb.ToString();
226 }
227
231 public static FractalGraph CalcNova(double rCenter, double iCenter, double rDelta, double Rr, double Ri,
232 double pr, double pi, SKColor[] Palette, int Width, int Height, ScriptNode Node, Variables Variables,
233 FractalZoomScript FractalZoomScript, object State)
234 {
235 double r0, i0, r1, i1;
236 double dr, di;
237 double r, i;
238 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4;
239 double aspect;
240 double Temp;
241 int x, y;
242 int n, N;
243 int index;
244 double lnz;
245 double argz;
246 double amp;
247 double phi;
248
249 N = Palette.Length;
250
251 rDelta *= 0.5;
252 r0 = rCenter - rDelta;
253 r1 = rCenter + rDelta;
254
255 aspect = ((double)Width) / Height;
256
257 i0 = iCenter - rDelta / aspect;
258 i1 = iCenter + rDelta / aspect;
259
260 dr = (r1 - r0) / Width;
261 di = (i1 - i0) / Height;
262
263 int Size = Width * Height;
264 double[] ColorIndex = new double[Size];
265 double Conv = 1e-10;
266 double Div = 1e10;
267
268 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
269 {
270 for (x = 0, r = r0; x < Width; x++, r += dr)
271 {
272 zr = r;
273 zi = i;
274
275 n = 0;
276 do
277 {
278 // f: z->z^p-1 = exp(p*ln(z))-1
279 // exp(a+ib)=exp(a)*(cos(b)+i*sin(b))
280 // ln(z)=ln|z|+i*arg(z)
281 // exp(p*ln(z))-1 =
282 // = exp((pr+i*pi)*(ln|z|+i*arg(z)))-1 =
283 // = exp(pr*ln|z|-pi*arg(z)+i*(pi*ln|z|+pr*arg(z)))-1 =
284 // = exp(pr*ln|z|-pi*arg(z))*(cos(pi*ln|z|+pr*arg(z))+i*sin(pi*ln|z|+pr*arg(z)))-1
285
286 lnz = Math.Log(Math.Sqrt(zr * zr + zi * zi));
287 argz = Math.Atan2(zi, zr);
288 amp = Math.Exp(pr * lnz - pi * argz);
289 phi = pi * lnz + pr * argz;
290
291 zr2 = amp * Math.Cos(phi) - 1;
292 zi2 = amp * Math.Sin(phi);
293
294 // f': z->p*z^(p-1) = p*exp((p-1)*ln(z)) =
295 // = (pr+i*pi)*exp((pr-1+i*pi)*(ln|z|+i*arg(z))) =
296 // = (pr+i*pi)*exp((pr-1)*ln|z|-pi*arg(z)+i*(pi*ln|z|+(pr-1)*arg(z))) =
297 // = (pr+i*pi)*exp((pr-1)*ln|z|-pi*arg(z))(sin(pi*ln|z|+(pr-1)*arg(z))+i*cos(pi*ln|z|+(pr-1)*arg(z))) =
298
299 amp = Math.Exp((pr - 1) * lnz - pi * argz);
300 phi = pi * lnz + (pr - 1) * argz;
301
302 zr3 = amp * Math.Cos(phi);
303 zi3 = amp * Math.Sin(phi);
304
305 Temp = pr * zr3 - pi * zi3;
306 zi3 = pr * zi3 + pi * zr3;
307 zr3 = Temp;
308
309 // f/f':
310
311 Temp = 1.0 / (zr3 * zr3 + zi3 * zi3);
312 zr4 = (zr2 * zr3 + zi2 * zi3) * Temp;
313 zi4 = (zi2 * zr3 - zr2 * zi3) * Temp;
314
315 Temp = Rr * zr4 - Ri * zi4;
316 zi4 = Ri * zr4 + Rr * zi4;
317 zr4 = Temp;
318
319 zr -= zr4;
320 zi -= zi4;
321
322 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
323 }
324 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
325
326 if (Temp < Conv && n < N)
327 ColorIndex[index++] = n;
328 else
329 ColorIndex[index++] = N;
330 }
331 }
332
333 Variables.Preview(Node.Expression, new GraphBitmap(Variables, FractalGraph.ToPixels(ColorIndex, Width, Height, Palette)));
334
335 double[] Boundary = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
336 FractalGraph.Smooth(ColorIndex, Boundary, Width, Height, N, Palette, Node, Variables);
337
338 return new FractalGraph(Variables, FractalGraph.ToPixels(ColorIndex, Width, Height, Palette),
339 r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State);
340 }
341
345 public override string FunctionName => nameof(NovaSmoothFractal);
346 }
347}
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
NovaSmoothFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
NovaSmoothFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
NovaSmoothFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, int Start, int Length, Expression Expression)
TODO
NovaSmoothFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcNova(double rCenter, double iCenter, double rDelta, double Rr, double Ri, double pr, double pi, SKColor[] Palette, int Width, int Height, ScriptNode Node, Variables Variables, FractalZoomScript FractalZoomScript, object State)
TODO
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:22
static PixelInformation ToPixels(double[] ColorIndex, int Width, int Height, SKColor[] Palette)
TODO
static SKColor[] ToPalette(ObjectVector Vector)
TODO
static double[] FindBoundaries(double[] ColorIndex, int Width, int Height)
TODO
static void Smooth(double[] ColorIndex, double[] Boundary, int Width, int Height, int N, SKColor[] Palette, ScriptNode Node, Variables Variables)
TODO
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:13
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
void Preview(Expression Expression, IElement Result)
Reports a preview of the final result.
Definition: Variables.cs:455
Basic interface for all types of elements.
Definition: IElement.cs:20
delegate string FractalZoomScript(double r, double i, double Size, object State)
Generates new script when zoomed.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9