1/**
2 * Mandelbulber v2, a 3D fractal generator  _%}}i*<.        ____                _______
3 * Copyright (C) 2020 Mandelbulber Team   _>]|=||i=i<,     / __ \___  ___ ___  / ___/ /
4 *                                        \><||i|=>>%)    / /_/ / _ \/ -_) _ \/ /__/ /__
5 * This file is part of Mandelbulber.     )<=i=]=|=i<>    \____/ .__/\__/_//_/\___/____/
6 * The project is licensed under GPLv3,   -<>>=|><|||`        /_/
7 * see also COPYING file in this folder.    ~+{i%+++
8 *
9 * benesiMagTransforms
10 * @reference
11 * http://www.fractalforums.com/new-theories-and-research/
12 * do-m3d-formula-have-to-be-distance-estimation-formulas/
13
14 * This file has been autogenerated by tools/populateUiInformation.php
15 * from the file "fractal_benesi_mag_transforms.cpp" in the folder formula/definition
16 * D O    N O T    E D I T    T H I S    F I L E !
17 */
18
19REAL4 BenesiMagTransformsIteration(REAL4 z, __constant sFractalCl *fractal, sExtendedAuxCl *aux)
20{
21	REAL4 c = aux->const_c;
22	// benesi T1
23	if (fractal->transformCommon.benesiT1Enabled && aux->i >= fractal->transformCommon.startIterations
24			&& aux->i < fractal->transformCommon.stopIterations)
25	{
26		REAL tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
27		z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
28			z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
29
30		REAL tempL = length(z);
31		z = fabs(z) * fractal->transformCommon.scale3D222;
32		// if (tempL < 1e-21f) tempL = 1e-21f;
33		REAL avgScale = length(z) / tempL;
34		aux->DE = aux->DE * avgScale;
35
36		tempXZ = (z.y + z.x) * SQRT_1_2_F;
37		z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
38			z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
39		z = z - fractal->transformCommon.offset200;
40	}
41	// rotation
42	if (fractal->transformCommon.rotationEnabled)
43		z = Matrix33MulFloat4(fractal->transformCommon.rotationMatrix, z);
44
45	//  Benesi pine tree pwr2
46	if (fractal->transformCommon.addCpixelEnabled
47			&& aux->i >= fractal->transformCommon.startIterationsT
48			&& aux->i < fractal->transformCommon.stopIterationsT)
49	{
50		REAL4 zz = z * z;
51		aux->r = native_sqrt(zz.x + zz.y + zz.z); // needed when alternating pwr2s
52		aux->DE = aux->r * aux->DE * 2.0f + 1.0f;
53
54		REAL t = 1.0f;
55		REAL temp = zz.y + zz.z;
56		if (temp > 0.0f) t = 2.0f * z.x / native_sqrt(temp);
57		temp = z.z;
58		z.x = (zz.x - zz.y - zz.z);
59		z.y = (2.0f * t * z.y * temp);
60		z.z = (t * (zz.y - zz.z));
61
62		// swap c.yz then add cPixel
63		REAL4 tempC = c;
64		if (fractal->transformCommon.alternateEnabledFalse) // alternate
65		{
66			tempC = aux->c * fractal->transformCommon.constantMultiplier100;
67			tempC = (REAL4){tempC.x, tempC.z, tempC.y, tempC.w};
68			aux->c = tempC;
69		}
70		else
71		{
72			tempC *= fractal->transformCommon.constantMultiplier100;
73			tempC = (REAL4){tempC.x, tempC.z, tempC.y, tempC.w};
74		}
75		z += tempC;
76	}
77
78	if (fractal->transformCommon.juliaMode)
79	{
80		z.x += fractal->transformCommon.juliaC.x * fractal->transformCommon.constantMultiplier100.x;
81		z.z += fractal->transformCommon.juliaC.y * fractal->transformCommon.constantMultiplier100.y;
82		z.y += fractal->transformCommon.juliaC.z * fractal->transformCommon.constantMultiplier100.z;
83	}
84	// additional transform slot controls
85	bool functionEnabledN[5] = {fractal->transformCommon.functionEnabledAxFalse,
86		fractal->transformCommon.functionEnabledAyFalse,
87		fractal->transformCommon.functionEnabledAzFalse,
88		fractal->transformCommon.functionEnabledBxFalse,
89		fractal->transformCommon.functionEnabledByFalse};
90	int startIterationN[5] = {fractal->transformCommon.startIterationsA,
91		fractal->transformCommon.startIterationsB, fractal->transformCommon.startIterationsC,
92		fractal->transformCommon.startIterationsD, fractal->transformCommon.startIterationsE};
93	int stopIterationN[5] = {fractal->transformCommon.stopIterationsA,
94		fractal->transformCommon.stopIterationsB, fractal->transformCommon.stopIterationsC,
95		fractal->transformCommon.stopIterationsD, fractal->transformCommon.stopIterationsE};
96
97	enumMulti_orderOfTransfCl transfN[5] = {fractal->magTransf.orderOfTransf1,
98		fractal->magTransf.orderOfTransf2, fractal->magTransf.orderOfTransf3,
99		fractal->magTransf.orderOfTransf4, fractal->magTransf.orderOfTransf5};
100
101	for (int f = 0; f < 5; f++)
102	{
103		if (functionEnabledN[f] && aux->i >= startIterationN[f] && aux->i < stopIterationN[f])
104		{
105			REAL tempXZ;
106			REAL tempL;
107			REAL4 temp;
108			REAL avgScale;
109			REAL4 tempV2;
110
111			switch (transfN[f])
112			{
113				case multi_orderOfTransfCl_typeT1:
114				default:
115					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
116					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
117						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
118					z = fabs(z) * fractal->transformCommon.scale3Da222;
119					tempL = length(temp);
120					// if (tempL < 1e-21f) tempL = 1e-21f;
121					avgScale = length(z) / tempL;
122					aux->DE = aux->DE * avgScale;
123					tempXZ = (z.y + z.x) * SQRT_1_2_F;
124					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
125						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
126					z = z - fractal->transformCommon.offsetA200;
127					break;
128
129				case multi_orderOfTransfCl_typeT1Mod:
130					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
131					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
132						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
133					z = fabs(z) * fractal->transformCommon.scale3D333;
134					tempL = length(temp);
135					// if (tempL < 1e-21f) tempL = 1e-21f;
136					avgScale = length(z) / tempL;
137					aux->DE = aux->DE * avgScale;
138					z = (fabs(z + fractal->transformCommon.offset111)
139							 - fabs(z - fractal->transformCommon.offset111) - z);
140					tempXZ = (z.y + z.x) * SQRT_1_2_F;
141					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
142						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
143					break;
144
145				case multi_orderOfTransfCl_typeT2:
146					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
147					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
148						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
149					tempV2 = z;
150					tempV2.x = native_sqrt(z.y * z.y + z.z * z.z);
151					tempV2.y = native_sqrt(z.x * z.x + z.z * z.z); // switching, squared, sqrt
152					tempV2.z = native_sqrt(z.x * z.x + z.y * z.y);
153					z = fabs(tempV2 - fractal->transformCommon.offsetA111);
154					temp = z;
155					tempL = length(temp);
156					z = fabs(z) * fractal->transformCommon.scale3D444;
157					// if (tempL < 1e-21f) tempL = 1e-21f;
158					avgScale = length(z) / tempL;
159					aux->DE = aux->DE * avgScale;
160					tempXZ = (z.y + z.x) * SQRT_1_2_F;
161					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
162						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
163					break;
164
165				case multi_orderOfTransfCl_typeT3:
166					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
167					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
168						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
169
170					tempV2 = z;
171					tempV2.x = (z.y + z.z);
172					tempV2.y = (z.x + z.z); // switching
173					tempV2.z = (z.x + z.y);
174					z = (fabs(tempV2 - fractal->transformCommon.offset222))
175							* fractal->transformCommon.scale3Db222;
176					aux->DE *= length(z) / length(tempV2);
177
178					tempXZ = (z.y + z.x) * SQRT_1_2_F;
179					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
180						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
181					break;
182
183				case multi_orderOfTransfCl_typeT4:
184					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
185					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
186						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
187
188					tempV2 = z;
189					tempV2.x = (z.y * z.y + z.z * z.z);
190					tempV2.y = (z.x * z.x + z.z * z.z); // switching, squared,
191					tempV2.z = (z.x * z.x + z.y * z.y);
192					z = (fabs(tempV2 - fractal->transformCommon.offsetB111))
193							* fractal->transformCommon.scale3Dc222;
194					aux->DE *= length(z) / length(tempV2);
195
196					tempXZ = (z.y + z.x) * SQRT_1_2_F;
197					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
198						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
199					break;
200
201				case multi_orderOfTransfCl_typeT5b:
202					tempXZ = z.x * SQRT_2_3_F - z.z * SQRT_1_3_F;
203					z = (REAL4){(tempXZ - z.y) * SQRT_1_2_F, (tempXZ + z.y) * SQRT_1_2_F,
204						z.x * SQRT_1_3_F + z.z * SQRT_2_3_F, z.w};
205					// if (z.x > -1e-21f && z.x < 1e-21f)
206					//	z.x = (z.x > 0) ? 1e-21f : -1e-21f;
207					// if (z.y > -1e-21f && z.y < 1e-21f)
208					//	z.y = (z.y > 0) ? 1e-21f : -1e-21f;
209					// if (z.z > -1e-21f && z.z < 1e-21f)
210					//	z.z = (z.z > 0) ? 1e-21f : -1e-21f;
211					tempV2 = z;
212					tempV2.x = fabs(native_powr(native_powr(z.y, fractal->transformCommon.int8X)
213																				+ native_powr(z.z, fractal->transformCommon.int8X),
214						fractal->transformCommon.power025.x));
215					tempV2.y = fabs(native_powr(native_powr(z.x, fractal->transformCommon.int8Y)
216																				+ native_powr(z.z, fractal->transformCommon.int8Y),
217						fractal->transformCommon.power025.y));
218					tempV2.z = fabs(native_powr(native_powr(z.x, fractal->transformCommon.int8Z)
219																				+ native_powr(z.y, fractal->transformCommon.int8Z),
220						fractal->transformCommon.power025.z));
221					z = (fabs(tempV2 - fractal->transformCommon.offsetC111))
222							* fractal->transformCommon.scale3Dd222;
223					aux->DE *= length(z) / length(tempV2);
224
225					tempXZ = (z.y + z.x) * SQRT_1_2_F;
226					z = (REAL4){z.z * SQRT_1_3_F + tempXZ * SQRT_2_3_F, (z.y - z.x) * SQRT_1_2_F,
227						z.z * SQRT_2_3_F - tempXZ * SQRT_1_3_F, z.w};
228					break;
229			}
230		}
231	}
232	// analyticDE controls
233	if (fractal->analyticDE.enabled)
234		aux->DE = aux->DE * fractal->analyticDE.scale1 + fractal->analyticDE.offset1;
235	return z;
236}