{
     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 varGlynnSim3;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationGlynnSim3 = class(TBaseVariation)
  private
    radius, thickness, contrast, pow,
	  radius1, radius2, gamma: double;
    procedure DoubleCircle(var x: double; var y: 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;

{ TVariationGlynnSim2 }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationGlynnSim3.Prepare;
begin	
	radius1 := radius  + thickness;
	radius2 := sqr(radius) / radius1;
	gamma := radius1 / (radius1 + radius2);	
end;

procedure TVariationGlynnSim3.DoubleCircle(var x: double; var y: double);
var r, Phi, sPhi, cPhi: double;
begin
	Randomize;
	Phi := 2 * Pi * random;
	SinCos(Phi, sPhi, cPhi);
	if (random < gamma) then r := radius1
		else r := radius2;
	x := r * cPhi;
	y := r * sPhi;
end;

procedure TVariationGlynnSim3.CalcFunction;
var r, r2, x, y, px, py, Alpha: double;
begin
	x := FTx^;  y := FTy^;
	r := hypot(x, y);
	Alpha := radius / r;

	if (r < radius1) then
	begin
		DoubleCircle(px, py);
		FPx^ := FPx^ + vvar * px;
		FPy^ := FPy^ + vvar * py;
	end else
	begin
		if (random > contrast * power(Alpha, pow)) then 
		begin
		   FPx^ := FPx^ + vvar * x;
		   FPy^ := FPy^ + vvar * y;
		end else
		begin
		   FPx^ := FPx^ + vvar * sqr(Alpha) * x;
		   FPy^ := FPy^ + vvar * sqr(Alpha) * y;
		end;
	end;	
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationGlynnSim3.Create;
begin
  radius := 1;
  thickness := 0.1;
  contrast := 0.5;
  pow := 1.5;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationGlynnSim3.GetName: string;
begin
  Result := 'GlynnSim3';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim3.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'GlynnSim3_radius';  
  1: Result := 'GlynnSim3_thickness';  
  2: Result := 'GlynnSim3_contrast';  
  3: Result := 'GlynnSim3_pow';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim3.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim3_radius' then begin
    radius := Value;
    Result := True;
  end else if Name = 'GlynnSim3_thickness' then begin
    thickness := Value;
    Result := True;
  end else if Name = 'GlynnSim3_contrast' then begin
 	if Value < 0 then Value := 0;
	if Value > 1 then Value := 1;
    contrast := Value;
    Result := True;
  end else if Name = 'GlynnSim3_pow' then begin
    pow := Value;
    Result := True;
  end 
end;

function TVariationGlynnSim3.ResetVariable(const Name: string): boolean;
begin
  Result := False;
 if Name = 'GlynnSim3_radius' then begin
    radius:= 1;
    Result := True;
  end else if Name = 'GlynnSim3_thickness' then begin
    thickness := 0.1;
    Result := True;
  end else if Name = 'GlynnSim3_contrast' then begin
    contrast := 0.5;
    Result := True;
  end else if Name = 'GlynnSim3_pow' then begin
    pow := 1.5;
    Result := True;	
  end 
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim3.GetNrVariables: integer;
begin
  Result := 4
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationGlynnSim3.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'GlynnSim3_radius' then begin
    Value := radius;
    Result := True;
  end else if Name = 'GlynnSim3_thickness' then begin
    Value := thickness;
    Result := True;
  end else if Name = 'GlynnSim3_contrast' then begin
    Value := contrast;
    Result := True;
  end else if Name = 'GlynnSim3_pow' then begin
    Value := pow;
    Result := True;
  end 
end;

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