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

unit varBent2;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationBent2 = class(TBaseVariation)
  private
    b2x, b2y, b2z, vvarx, vvary, vvarz: 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;

{ TVariationBent2 }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationBent2.Prepare;
begin
	vvarx := vvar * b2x;
	vvary := vvar * b2y;
  vvarz := vvar * b2z;
end;

procedure TVariationBent2.CalcFunction;
begin
  if(FTx^ < 0.0) then
	  FPx^ := FPx^ + vvarx * FTx^
	else 
	  FPx^ := FPx^ + vvar * FTx^;
  if (FTy^ < 0) then
	  FPy^ := FPy^ + vvary * FTy^
	else   
    FPy^ := FPy^ + vvar * FTy^;
  // AV: added 3D-support
  if (FTz^ < 0) then
	  FPz^ := FPz^ + vvarz * FTz^
	else
    FPz^ := FPz^ + vvar * FTz^;
end;

///////////////////////////////////////////////////////////////////////////////
class function TVariationBent2.GetName: string;
begin
  Result := 'bent2';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBent2.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'bent2_x';
  1: Result := 'bent2_y';
  2: Result := 'bent2_z';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBent2.GetNrVariables: integer;
begin
  Result := 3;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBent2.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'bent2_x' then begin
    b2x := Value;
    Result := True;
  end else if Name = 'bent2_y' then begin
    b2y := Value;
    Result := True;
  end else if Name = 'bent2_z' then begin
    b2z := Value;
    Result := True;
  end;
end;

function TVariationBent2.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'bent2_x' then begin
    b2x := 1;
    Result := True;
  end else if Name = 'bent2_y' then begin
    b2y := 1;
    Result := True;
  end else if Name = 'bent2_z' then begin
    b2z := 1;
    Result := True;
  end;
end;


///////////////////////////////////////////////////////////////////////////////
function TVariationBent2.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'bent2_x' then begin
    Value := b2x;
    Result := True;
  end else if Name = 'bent2_y' then begin
    Value := b2y;
    Result := True;
  end else if Name = 'bent2_z' then begin
    Value := b2z;
    Result := True;
  end;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationBent2.Create;
begin
  b2x  := 2;
  b2y := 0.5;
  b2z  := 1;
end;

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

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