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

unit varPostSinusoidal;

interface

uses
  BaseVariation, XFormMan;

{$ifdef CPUX86}
  {$define _ASM_}
{$endif}

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

{ TVariationPostSinusoidal }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationPostSinusoidal.GetCalcFunction(var f: TCalcFunction);
begin
	if use3D = 0 then
		f := CalcFlat
	else f := CalcFunction;	
end;

procedure TVariationPostSinusoidal.CalcFlat;
{$ifndef _ASM_}
begin
  FPx^ := vvar * sin(FPx^);
  FPy^ := vvar * sin(FPy^);
{$else}  
asm    // AV: added inline asm code
    fld     qword ptr [eax + vvar]
    mov     edx, [eax + FTx]
    fld     qword ptr [edx + 16]      // FPx
    fsin
    fmul    st, st(1)
    fstp    qword ptr [edx + 16]

    fld     qword ptr [edx + 24]  // FPy
    fsin
    fmulp
    fstp    qword ptr [edx + 24]

    fwait
{$endif}  
end;

procedure TVariationPostSinusoidal.CalcFunction;
{$ifndef _ASM_}
begin
  FPx^ := vvar * sin(FPx^);
  FPy^ := vvar * sin(FPy^);
  FPz^ := vvar * sin(FPz^);  // AV: changed from linear scale
{$else}  
asm    // AV: added inline asm code
    fld     qword ptr [eax + vvar]
    mov     edx, [eax + FTx]
    fld     qword ptr [edx + 16]      // FPx
    fsin
    fmul    st, st(1)
    fstp    qword ptr [edx + 16]

    fld     qword ptr [edx + 24]  // FPy
    fsin
    fmul    st, st(1)
    fstp    qword ptr [edx + 24]

    fld    qword ptr [edx + 40] // FPz
    fsin
    fmulp
    fstp    qword ptr [edx + 40]

    fwait
{$endif}  
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationPostSinusoidal.Create;
begin
	use3D := 0;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationPostSinusoidal.GetName: string;
begin
  Result := 'post_sinusoidal';
end;

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

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

///////////////////////////////////////////////////////////////////////////////
function TVariationPostSinusoidal.GetNrVariables: integer;
begin
  Result := 1;
end;

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

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