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

unit varPower;

interface
uses
  BaseVariation, XFormMan;

type
  TVariationPower = class(TBaseVariation)
  private
    use3D: byte;
	  procedure Calc3D;
  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;

    procedure CalcFunction; override;
    procedure GetCalcFunction(var f: TCalcFunction); override;
  end;


implementation

uses
  Math;
  
////////////////////////

procedure TVariationPower.GetCalcFunction(var f: TCalcFunction);
begin
	if use3D <> 0 then f := Calc3D // AV: added optional 3d-support
	else f := CalcFunction;
end;

procedure TVariationPower.Calc3D; // AV
var
  r, FSinA, FCosA: double;
begin
  r := sqrt(sqr(FTx^) + sqr(FTy^) + sqr(FTz^)) + 1E-300;
  FSinA := FTx^ / r;
  FCosA := FTy^ / r;
  r := vvar * Math.Power(r, FSinA);
  FPx^ := FPx^ + r * FCosA;
  FPy^ := FPy^ + r * FSinA;
  // transform the plane into 3d-shape
  r := vvar * Math.Power(r, FCosA);
  FPz^ := FPz^ + r * FCosA; //FSinA;
end;

procedure TVariationPower.CalcFunction;
var
  r, FSinA, FCosA: double;
begin
  r := sqrt(sqr(FTx^) + sqr(FTy^)) + 1E-300;
  FSinA := FTx^ / r;
  FCosA := FTy^ / r;
  r := vvar * Math.Power(r, FSinA);
  FPx^ := FPx^ + r * FCosA;
  FPy^ := FPy^ + r * FSinA;
end;

constructor TVariationPower.Create;
begin
  inherited Create;
  use3D := 0;
end;

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

class function TVariationPower.GetName: string;
begin
   Result := 'power';
end;

function TVariationPower.GetNrVariables: integer;
begin
  Result := 1;
end;

function TVariationPower.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
  0: Result := 'power_use3D';
  else
    Result := '';
  end;
end;

function TVariationPower.GetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = 'power_use3D' then begin
    Value := use3D;
    Result := True;
  end;
end;

function TVariationPower.SetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = 'power_use3D' then begin
    if (Value > 1) then Value := 1;
    if (Value < 0) then Value := 0;
    use3D := Round(Value);
    Result := True;
  end;
end;

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

end.
