Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EstimateFlameSize.cs
1using System;
2using System.Collections.Generic;
3using SkiaSharp;
10
12{
43 {
47 public EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, ScriptNode DimX, ScriptNode DimY, ScriptNode Seed,
49 : base(new ScriptNode[] { N, FlameFunctions, DimX, DimY, Seed },
50 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar,
51 ArgumentType.Scalar }, Start, Length, Expression)
52 {
53 }
54
58 public EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, ScriptNode DimX, ScriptNode DimY,
60 : base(new ScriptNode[] { N, FlameFunctions, DimX, DimY },
61 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar }, Start, Length, Expression)
62 {
63 }
64
69 : base(new ScriptNode[] { N, FlameFunctions, DimX },
70 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar }, Start, Length, Expression)
71 {
72 }
73
78 : base(new ScriptNode[] { N, FlameFunctions },
79 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Vector }, Start, Length, Expression)
80 {
81 }
82
86 public override string[] DefaultArgumentNames
87 {
88 get
89 {
90 return new string[]
91 {
92 "N", "FlameFunctions", "DimX", "DimY", "Seed"
93 };
94 }
95 }
96
100 public override string FunctionName => nameof(EstimateFlameSize);
101
106 {
107 long N;
108 int dimx, dimy;
109 int i, c;
110 int Seed;
111
112 i = 0;
113 c = Arguments.Length;
114 N = (long)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
115 if (N <= 0)
116 throw new ScriptRuntimeException("N in calls to EstimateFlameSize() must be a positive integer.", this);
117
118 object Obj = Arguments[i++].AssociatedObjectValue;
119
120 if (!(Obj is Array FlameArray))
121 throw new ScriptRuntimeException("The second parameter to EstimateFlameSize must be an array, containing flame definitions.", this);
122
123 List<FlameFunction> FlameFunctions = new List<FlameFunction>();
124 double Weight;
125 FlameFunction CurrentFunction = null;
126
127 foreach (object FlameItem in FlameArray)
128 {
129 if (FlameItem is DoubleMatrix M)
130 {
131 CurrentFunction = new FlameFunction(M, this);
132 FlameFunctions.Add(CurrentFunction);
133 }
134 else if (FlameItem is SKColor || FlameItem is string)
135 {
136 if (CurrentFunction is null)
137 {
138 M = new DoubleMatrix(new double[,] { { 1, 0 }, { 0, 1 } });
139 CurrentFunction = new FlameFunction(M, this);
140 FlameFunctions.Add(CurrentFunction);
141 }
142
143 SKColor cl = Graph.ToColor(FlameItem);
144 cl.ToHsl(out float H, out float S, out float L);
145
146 CurrentFunction.SetColorHsl(H, S, L);
147 CurrentFunction = null;
148 }
149 else if (FlameItem is IFlameVariation Var)
150 {
151 if (CurrentFunction is null)
152 {
153 M = new DoubleMatrix(new double[,] { { 1, 0 }, { 0, 1 } });
154 CurrentFunction = new FlameFunction(M, this);
155 FlameFunctions.Add(CurrentFunction);
156 }
157
158 CurrentFunction.Add(Var);
159 }
160 else if (FlameItem is ILambdaExpression Lambda)
161 {
162 if (CurrentFunction is null)
163 {
164 M = new DoubleMatrix(new double[,] { { 1, 0 }, { 0, 1 } });
165 CurrentFunction = new FlameFunction(M, this);
166 FlameFunctions.Add(CurrentFunction);
167 }
168
169 CurrentFunction.Add(Lambda);
170 }
171 else
172 {
173 try
174 {
175 Weight = Expression.ToDouble(FlameItem);
176
177 if (CurrentFunction is null)
178 {
179 M = new DoubleMatrix(new double[,] { { 1, 0 }, { 0, 1 } });
180 CurrentFunction = new FlameFunction(M, this);
181 FlameFunctions.Add(CurrentFunction);
182 }
183
184 CurrentFunction.SetWeight(Weight);
185 }
186 catch (Exception)
187 {
188 throw new ScriptRuntimeException("Invalid flame variation definition.", this);
189 }
190 }
191 }
192
193 if (i < c)
194 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
195 else
196 dimx = 320;
197
198 if (i < c)
199 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
200 else
201 dimy = 200;
202
203 if (i < c)
204 Seed = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
205 else
206 {
207 lock (gen)
208 {
209 Seed = gen.Next();
210 }
211
212 Variables.ConsoleOut?.WriteLine("Seed = " + Seed.ToString(), Variables);
213 }
214
215 if (i < c)
216 {
217 throw new ScriptRuntimeException("Parameter mismatch in call to EstimateFlameSize(N,FlameFunctions[dimx[,dimy[,Seed]]]).",
218 this);
219 }
220
221 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
222 throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
223
224 FlameFunction[] Functions = FlameFunctions.ToArray();
225
226 FlameFractalRgba.EstimateSize(out double xc, out double yc, out double dr, Functions, dimx, dimy, Seed, N, Variables, this);
227
228 return new DoubleVector(xc, yc, dr);
229 }
230
231 private static readonly Random gen = new Random();
232
233 }
234}
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
Estimates the size of a flame fractal.
EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, ScriptNode DimX, ScriptNode DimY, ScriptNode Seed, int Start, int Length, Expression Expression)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
EstimateFlameSize(ScriptNode N, ScriptNode FlameFunctions, int Start, int Length, Expression Expression)
TODO
Calculates a flame fractal in RGBA space. Intensity is calculated along the A-axis....
static void EstimateSize(out double xCenter, out double yCenter, out double rDelta, FlameFunction[] Functions, int Width, int Height, int Seed, long N, Variables Variables, ScriptNode Node)
TODO
Base class for graphs.
Definition: Graph.cs:79
static SKColor ToColor(object Object)
Converts an object to a color.
Definition: Graph.cs:828
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
TextWriter ConsoleOut
Console out interface. Can be used by functions and script to output data to the console.
Definition: Variables.cs:219
Basic interface for all types of elements.
Definition: IElement.cs:20
Base interface for lambda expressions.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9