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

interface

uses
  BaseVariation, XFormMan;

type
  TVariationHypertile3D = class(TBaseVariation)
  private
    hypertile3D_p, hypertile3D_q, hypertile3D_n: integer;
	  cx, cy, c2x, c2y, c2, s2x, s2y, s2z: 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;

///////////////////////////////////////////////////////////////////////////////
procedure TVariationHypertile3D.Prepare;
var 	
	t, pa, qa, r: double;
begin
	pa := PI2 / hypertile3D_p;
  qa := PI2 / hypertile3D_q;
	t := cos(pa);
	r := -(t - 1) / (t + cos(qa));
	if (r > 0) then
        r := 1 / sqrt(1 + r)
    else
        r := 1;

	t := hypertile3D_n * pa;
	SinCos(t, pa, qa);
	
	cx := r * qa;
	cy := r * pa;
	c2x := 2 * cx;
	c2y := 2 * cy;
	c2 := sqr(cx) + sqr(cy);
	
  s2x := 1 + sqr(cx) - sqr(cy);
  s2y := 1 + sqr(cy) - sqr(cx);
  s2z := 1 - sqr(cy) - sqr(cx);
end;

procedure TVariationHypertile3D.CalcFunction;
var 	
	r2, x2cx, y2cy, vr: double;
begin
  r2 := sqr(FTx^) + sqr(FTy^) + sqr(FTz^);
  
  x2cx := c2x * FTx^;
  y2cy := c2y * FTy^;

  vr := vvar / (c2 * r2 + x2cx - y2cy + 1);

  FPx^ := FPx^ + vr * (FTx^ * s2x - cx * ( y2cy - r2 - 1));
  FPy^ := FPy^ + vr * (FTy^ * s2y + cy * (-x2cx - r2 - 1));
  FPz^ := FPz^ + vr * (FTz^ * s2z);
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationHypertile3D.Create;
begin
  hypertile3D_p := 3; 
  hypertile3D_q := 7;
  hypertile3D_n := 0; 
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationHypertile3D.GetName: string;
begin
  Result := 'hypertile3D';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile3D.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'hypertile3D_p';
  1: Result := 'hypertile3D_q';
  2: Result := 'hypertile3D_n';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile3D.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'hypertile3D_p' then begin
    if Value < 3 then Value := 3;
	hypertile3D_p := Round(Value);
    Result := True;
  end else if Name = 'hypertile3D_q' then begin
    if Value < 3 then Value := 3;
    hypertile3D_q := Round(Value);
    Result := True;
  end else if Name = 'hypertile3D_n' then begin
    hypertile3D_n := Round(Value);
    Result := True;
  end;
end;

function TVariationHypertile3D.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'hypertile3D_p' then begin
    hypertile3D_p := 3;
    Result := True;
  end else if Name = 'hypertile3D_q' then begin
    hypertile3D_q := 7;
    Result := True;
  end else if Name = 'hypertile3D_n' then begin
    hypertile3D_n := 0;
    Result := True;
  end;
end;

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

///////////////////////////////////////////////////////////////////////////////
function TVariationHypertile3D.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'hypertile3D_p' then begin
    Value := hypertile3D_p;
    Result := True;
  end else if Name = 'hypertile3D_q' then begin
    Value := hypertile3D_q;
    Result := True;
  end else if Name = 'hypertile3D_n' then begin
    Value := hypertile3D_n;
    Result := True;
  end;
end;

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