{  Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina }

unit varPostCircleCrop;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationPostCircleCrop = class(TBaseVariation)

  const
    sx : string = 'post_circlecrop_x';
    sy : string = 'post_circlecrop_y';
    sradius : string = 'post_circlecrop_radius';
    szero : string = 'post_circlecrop_zero';
    sarea : string = 'post_circlecrop_scatter_area'; 
	
  private
    x0, y0, radius, scatter_area, ca: double;
    zero: byte;
  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;

{ TVariationPostCircleCrop }

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

procedure TVariationPostCircleCrop.Prepare;
begin
  ca := max(-1.0, min(scatter_area, 1.0));
end;

procedure TVariationPostCircleCrop.CalcFunction;
var
    x, y, rad, ang, rdc, sn, cn: double;
begin
  x := FPx^ - x0;
  y := FPy^ - y0;
  rad := Hypot(x, y);

  if (rad > radius) then
  begin
    if (zero = 1) then
    begin
       FPx^ := 0;
       FPy^ := 0;
    end else
    begin
      ang := arctan2(y, x);
      SinCos(ang, sn, cn);
      rdc := radius + (random * 0.5 * ca);

      FPx^ := vvar * rdc * cn + x0;
      FPy^ := vvar * rdc * sn + y0;
    end;
  end else
  begin
      FPx^ := vvar * x + x0;
      FPy^ := vvar * y + y0;
  end;
end;

constructor TVariationPostCircleCrop.Create;
begin
  x0 := 0;
  y0 := 0;
  radius := 1;
  scatter_area := 0;
  zero := 0;
end;

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

class function TVariationPostCircleCrop.GetName: string;
begin
  Result := 'post_circlecrop';
end;

function TVariationPostCircleCrop.GetNrVariables: integer;
begin
  Result := 5;
end;

function TVariationPostCircleCrop.GetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = sx then begin
    Value := x0;
    Result := True;
  end
  else if Name = sy then begin
    Value := y0;
    Result := True;
  end
  else if Name = sradius then begin
    Value := radius;
    Result := True;
  end
  else if Name = sarea then begin
    Value := scatter_area;
    Result := True;
  end
  else if Name = szero then begin
    Value := zero;
    Result := True;
  end;
end;

function TVariationPostCircleCrop.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
	  0: Result := sradius;
    1: Result := sx;
    2: Result := sy;
    3: Result := sarea;
    4: Result := szero;
  else
    Result := '';
  end;
end;

function TVariationPostCircleCrop.ResetVariable(const Name: string): boolean;
begin
Result := False;
  if Name = sx then begin
    x0 := 0;
    Result := True;
  end
  else if Name = sy then begin
    y0 := 0;
    Result := True;
  end
  else if Name = sradius then begin
    radius := 1;
    Result := True;
  end
  else if Name = sarea then begin
    scatter_area := 0;
    Result := True;
  end
  else if Name = szero then begin
    zero := 0;
    Result := True;
  end;
end;

function TVariationPostCircleCrop.SetVariable(const Name: string;
  var value: double): boolean;
begin
Result := False;
  if Name = sx then begin
    x0 := Value;
    Result := True;
  end
  else if Name = sy then begin
    y0 := Value;
    Result := True;
  end
  else if Name = sradius then begin
    radius := Value;
    Result := True;
  end
  else if Name = sarea then begin
    scatter_area := Value;
    Result := True;
  end
  else if Name = szero then begin
    if Value < 0 then Value := 0;
    if Value > 1 then Value := 1;
    zero := Round(Value);
    Result := True;
  end;
end;
///////////////////////////////////////////////////////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationPostCircleCrop), false, false);

end.
