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  * knotv1
10  * Based on DarkBeam formula from this thread:
11  * http://www.fractalforums.com/new-theories-and-research/not-fractal-but-funny-trefoil-knot-routine
12  */
13 
14 #include "all_fractal_definitions.h"
15 
cFractalKnotV1()16 cFractalKnotV1::cFractalKnotV1() : cAbstractFractal()
17 {
18 	nameInComboBox = "Knot V1";
19 	internalName = "knot_v1";
20 	internalID = fractal::knotV1;
21 	DEType = analyticDEType;
22 	DEFunctionType = customDEFunction;
23 	cpixelAddition = cpixelDisabledByDefault;
24 	defaultBailout = 100.0;
25 	DEAnalyticFunction = analyticFunctionCustomDE;
26 	coloringFunction = coloringFunctionDefault;
27 }
28 
FormulaCode(CVector4 & z,const sFractal * fractal,sExtendedAux & aux)29 void cFractalKnotV1::FormulaCode(CVector4 &z, const sFractal *fractal, sExtendedAux &aux)
30 {
31 	double polyfoldOrder = fractal->transformCommon.int2;
32 
33 	if (fractal->transformCommon.functionEnabledAxFalse) z.x = fabs(z.x);
34 	if (fractal->transformCommon.functionEnabledAyFalse) z.y = fabs(z.y);
35 	if (fractal->transformCommon.functionEnabledAzFalse) z.z = fabs(z.z);
36 	z += fractal->transformCommon.offset000;
37 
38 	CVector4 zc = z;
39 	zc.z *= fractal->transformCommon.scaleA1;
40 	double mobius =
41 		(1.0 * fractal->transformCommon.intA1 + fractal->transformCommon.intB1 / polyfoldOrder)
42 		* atan2(zc.y, zc.x);
43 
44 	zc.x = sqrt(zc.x * zc.x + zc.y * zc.y) - fractal->transformCommon.offsetA2;
45 	double temp = zc.x;
46 	double c = cos(mobius);
47 	double s = sin(mobius);
48 	zc.x = c * zc.x + s * zc.z;
49 	zc.z = -s * temp + c * zc.z;
50 
51 	double m = 1.0 * polyfoldOrder / M_PI_2x;
52 	double angle1 = floor(0.5 + m * (M_PI_2 - atan2(zc.x, zc.z))) / m;
53 
54 	temp = zc.x;
55 	c = cos(angle1);
56 	s = sin(angle1);
57 	zc.x = c * zc.x + s * zc.z;
58 	zc.z = -s * temp + c * zc.z;
59 
60 	zc.x -= fractal->transformCommon.offset05;
61 
62 	double len = sqrt(zc.x * zc.x + zc.z * zc.z);
63 
64 	if (fractal->transformCommon.functionEnabledCFalse) len = min(len, max(fabs(zc.x), fabs(zc.z)));
65 
66 	aux.DE0 = len - fractal->transformCommon.offset01;
67 
68 	if (fractal->transformCommon.functionEnabledJFalse)
69 	{
70 		if (fractal->transformCommon.functionEnabledDFalse)
71 			aux.DE0 = min(aux.dist, aux.DE0);
72 		if (fractal->transformCommon.functionEnabledKFalse) aux.DE0 /= aux.DE;
73 		if (fractal->transformCommon.functionEnabledEFalse) z = zc;
74 	}
75 	double colorDist = aux.dist;
76 	aux.dist = aux.DE0;
77 
78 	// aux.color
79 	if (fractal->foldColor.auxColorEnabled)
80 	{
81 		double colorAdd = 0.0;
82 		double ang = (M_PI - 2.0 * fabs(atan(zc.x / zc.z))) * 2.0 / M_PI;
83 
84 		if (fmod(ang, 2.0) < 1.0) colorAdd += fractal->foldColor.difs0000.x;
85 		colorAdd += fractal->foldColor.difs0000.y * fabs(ang);
86 		colorAdd += fractal->foldColor.difs0000.z * fabs(ang * zc.x);
87 		colorAdd += fractal->foldColor.difs0000.w * angle1;
88 
89 		colorAdd += fractal->foldColor.difs1;
90 		if (fractal->foldColor.auxColorEnabledA)
91 		{
92 			if (colorDist != aux.dist) aux.color += colorAdd;
93 		}
94 		else
95 			aux.color += colorAdd;
96 	}
97 
98 	// DE tweak
99 	if (fractal->analyticDE.enabledFalse) aux.dist = aux.dist * fractal->analyticDE.scale1;
100 }
101