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

interface

uses
  BaseVariation, XFormMan;

{$ifndef Apo7X64}
  {$define _ASM_}
{$endif}

type
  TVariationDCBubble = class(TBaseVariation)
  private
    centerx, centery, scale, bdcs, cden: double;
    style: byte;

    procedure CalcBubble;
    procedure CalcNewStyle;
  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;
    procedure GetCalcFunction(var f: TCalcFunction); override;
  end;

implementation

uses
  Math;

{ TVariationDCBubble }

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

procedure TVariationDCBubble.Prepare;
begin
	if (scale = 0) then
    bdcs := 10E-6
  else
    bdcs := 1 / scale;
  cden := bdcs /(2 * pi);
end;

procedure TVariationDCBubble.GetCalcFunction(var f: TCalcFunction);
begin
	if style = 0 then
    f := CalcFunction
	else
    f := CalcNewStyle;
end;

procedure TVariationDCBubble.CalcBubble;
{$ifndef _ASM_}
var
  r: double;
begin
  r := (sqr(FTx^) + sqr(FTy^))/4 + 1;
  FPz^ := FPz^ + VVAR * (2 / r - 1);  // AV: fixed inversion

  r := VVAR / r;

  FPx^ := FPx^ + r * FTx^;
  FPy^ := FPy^ + r * FTy^;
{$else}
 asm      // AV: added asm-code for speed
    mov     edx, [eax + FTx]
    fld     qword ptr [edx + 8]
    fld     qword ptr [edx]
    fld     st(1)
    fmul    st, st
    fld     st(1)
    fmul    st, st
    faddp
    fld1
    fadd    st, st
    fadd    st, st
    fdivp   st(1), st

    fld     qword ptr [eax + vvar]
    fld1
    fadd    st(2), st
    fdivr   st(2), st

    fld     st(2)
    fadd    st, st
    fsubrp  st(1), st
    fmul    st, st(1)
    fadd    qword ptr [edx + 40]
    fstp    qword ptr [edx + 40]

    fmulp

    fmul    st(2), st
    fmulp
    fadd    qword ptr [edx + 16]
    fstp    qword ptr [edx + 16]
    fadd    qword ptr [edx + 24]
    fstp    qword ptr [edx + 24]
    fwait
{$endif}
end;

procedure TVariationDCBubble.CalcFunction;
begin
  CalcBubble;
  color^ := fmod(abs(bdcs * (sqr(FPx^ + centerx) + sqr(FPy^ + centery))), 1.0);
end;

procedure TVariationDCBubble.CalcNewStyle;
begin
  CalcBubble;
  color^ := fmod(abs(cden * arctan2(FPy^ + centery, FPx^ - centerx)), 1.0);
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationDCBubble.Create;
begin
  centerx  := 0;
  centery := 0;
  scale := 1.0;
  style := 0;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationDCBubble.GetName: string;
begin
  Result := 'dc_bubble';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationDCBubble.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'dc_bubble_centerx';
  1: Result := 'dc_bubble_centery';
  2: Result := 'dc_bubble_scale';
  3: Result := 'dc_bubble_style';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationDCBubble.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'dc_bubble_centerx' then begin
    centerx := Value;
    Result := True;
  end else if Name = 'dc_bubble_centery' then begin
    centery := Value;
    Result := True;
  end
  else if Name = 'dc_bubble_scale' then begin
    scale := Value;
    Result := True;
  end
  else if Name = 'dc_bubble_style' then begin
    if (Value < 0) then Value := 0;
    if (Value > 1) then Value := 1;
    style := Round(Value);
    Result := True;
  end
end;

function TVariationDCBubble.ResetVariable(const Name: string): boolean;
begin
  Result := False;
 if Name = 'dc_bubble_centerx' then begin
    centerx:= 0;
    Result := True;
  end else if Name = 'dc_bubble_centery' then begin
    centery := 0;
    Result := True;
  end else if Name = 'dc_bubble_scale' then begin
    scale := 1;
    Result := True;
  end else if Name = 'dc_bubble_style' then begin
    style := 0;
    Result := True;
  end
end;

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

///////////////////////////////////////////////////////////////////////////////
function TVariationDCBubble.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'dc_bubble_centerx' then begin
    Value := centerx;
    Result := True;
  end else if Name = 'dc_bubble_centery' then begin
    Value := centery;
    Result := True;
  end
  else if Name = 'dc_bubble_scale' then begin
    Value := scale;
    Result := True;
  end
  else if Name = 'dc_bubble_style' then begin
    Value := style;
    Result := True;
  end
end;

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