{
     Apophysis Copyright (C) 2001-2004 Mark Townsend
     Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
     Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov

     Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
     Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
     Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}

unit varJuliaN3Dx;

interface

uses
  BaseVariation, XFormMan;

const
  j3power = 'julian3Dx_power';
  j3dist = 'julian3Dx_dist';
  j3a = 'julian3Dx_a';
  j3b = 'julian3Dx_b';
  j3c = 'julian3Dx_c';
  j3d = 'julian3Dx_d';
  j3e = 'julian3Dx_e';
  j3f = 'julian3Dx_f';
    
type
  TVariationJulian3Dx = class(TBaseVariation)
  private
    jdist, ja, jb, jc, jd, je, jf: double;
	jpower, absN: integer;	
	cN, pi2: double;

  public
    constructor Create;

    class function GetName: string; override;
    class function GetInstance: TBaseVariation; override;

    function GetNrVariables: integer; override;
    function GetVariableNameAt(const Index: integer): string; override;

    function SetVariable(const Name: string; var value: double): boolean; override;
    function GetVariable(const Name: string; var value: double): boolean; override;
	function ResetVariable(const Name: string): boolean; override;

    procedure Prepare; override;
    procedure CalcFunction; override;
  end;

implementation

uses
  Math;

{ TVariationJulian3Dx }

{ Based on standart Julia3D by Joel Faber, 
 with additional pre-affine parameters and JuliaN_dist variable }

//////////////////////////////////////////
procedure TVariationJulian3Dx.Prepare;
var ca, sa: double;
begin
	absN := abs(jpower);
	cN := (jdist / jpower - 1.0) / 2.0;
	pi2 := pi * 2;
end;

//////////////////////////////////////////
procedure TVariationJulian3Dx.CalcFunction;
var	x, y, z, r, r2, alpha, sina, cosa: double;
begin
    x := ja * FTx^ + jb * FTy^ + je;
    y := jc * FTx^ + jd * FTy^ + jf;
	  z := FTz^ / absN;
	
	  r2 := sqr(x) + sqr(y); // AV: replaced FTx^ * FTx^ + FTy^ * FTy^;
    r := vvar * Math.power(r2 + z * z, cN); 
	  FPz^ := FPz^ + r * z;
	
    alpha := (arctan2(y, x) + pi2 * random(absN))/ jpower;
    SinCos(alpha, sina, cosa);
	  r := r * sqrt(r2);
	  FPx^ := FPx^ + r * cosa;
    FPy^ := FPy^ + r * sina;
end;

///////////////////////////////////////////////////////////////////////////////

constructor TVariationJulian3Dx.Create;
begin
  jpower := 2 + random(5);
  jdist := 1;
  ja := 1;
  jb := 0;
  jc := 0;
  jd := 1;
  je := 0;
  jf := 0;
end;

///////////////////////////////////////////////////////////////////////////////
class function TVariationJulian3Dx.GetInstance: TBaseVariation;
begin
  Result := TVariationJulian3Dx.Create;
end;

///////////////////////////////////////////////////////////////////////////////
class function TVariationJulian3Dx.GetName: string;
begin
  Result := 'julian3Dx';
end;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
function TVariationJulian3Dx.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
    0: Result := j3power;
    1: Result := j3dist;
	2: Result := j3a;
	3: Result := j3b;
	4: Result := j3c;
	5: Result := j3d;
	6: Result := j3e;
	7: Result := j3f;	
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationJulian3Dx.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = j3power then begin
    jpower := Round(Value);
    if jpower = 0 then jpower := 1;
    Value := jpower; 
    Result := True;
  end else if Name = j3dist then begin
    jdist := value;
    Result := True;
  end else if Name = j3a then begin
	ja := Value;
    Result := True;
  end else if Name = j3b then begin
	jb := Value;
    Result := True;
  end else if Name = j3c then begin
	jc := Value;
    Result := True;
  end else if Name = j3d then begin
	jd := Value;
    Result := True;
  end else if Name = j3e then begin
	je := Value;
    Result := True;
  end else if Name = j3f then begin
	jf := Value;
    Result := True;
  end;
end;

function TVariationJulian3Dx.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = j3power then begin
	jpower := 2;
    Result := True;
  end else if Name = j3dist then begin
    jdist := 1;
    Result := True;
  end else if Name = j3a then begin
	ja := 1;
    Result := True;
  end else if Name = j3b then begin
	jb := 0;
    Result := True;
  end else if Name = j3c then begin
	jc := 0;
    Result := True;
  end else if Name = j3d then begin
	jd := 1;
    Result := True;
  end else if Name = j3e then begin
	je := 0;
    Result := True;
  end else if Name = j3f then begin
	jf := 0;
    Result := True;
  end;
end;

/////////////////////////////////////////////////////////////////////

function TVariationJulian3Dx.GetNrVariables: integer;
begin
  Result := 8;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationJulian3Dx.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = j3power then begin
    Value := jpower; 
    Result := True;
  end else if Name = j3dist then begin
    Value := jdist;
    Result := True;
  end else if Name = j3a then begin
	Value := ja;
    Result := True;
  end else if Name = j3b then begin
	Value := jb;
    Result := True;
  end else if Name = j3c then begin
	Value := jc;
    Result := True;
  end else if Name = j3d then begin
	Value := jd;
    Result := True;
  end else if Name = j3e then begin
	Value := je;
    Result := True;
  end else if Name = j3f then begin
	Value := jf;
    Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationJulian3Dx), true, false);
end.