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

interface

uses
  BaseVariation, XFormMan;

type
  TVariationProjective = class(TBaseVariation)
  private
    pr_A, pr_B, pr_C, pr_A1, pr_B1, pr_C1, pr_A2, pr_B2, pr_C2: double;
    pr_mode: byte;
	procedure CalcPre;
    procedure CalcPost;

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

implementation

uses
  Math;

///////////////////////////////////////////////////////////////////////////////
procedure TVariationProjective.GetCalcFunction(var f: TCalcFunction);
begin
  case pr_mode of
	0: f := CalcPre;
	1: f := CalcFunction;
	else f := CalcPost;
  end;
end;

procedure TVariationProjective.CalcPost;
var  x, y, denom: double;
begin
   x := FPx^;
   y := FPy^;

  denom := pr_A * x + pr_B * y + pr_C;
  if (abs(denom) < 1E-20) then denom := 1E-20;
  
  FPx^ := VVAR * (pr_A1 * x + pr_B1 * y + pr_C1) / denom;
  FPy^ := VVAR * (pr_A2 * x + pr_B2 * y + pr_C2) / denom;
  //FPz^ := VVAR * FPz^;     
end;

procedure TVariationProjective.CalcFunction;
var  x, y, denom: double;
begin
   x := FTx^;
   y := FTy^;

  denom := pr_A * x + pr_B * y + pr_C;
  if (abs(denom) < 1E-20) then denom := 1E-20;
    
  FPx^ := FPx^ + VVAR * (pr_A1 * x + pr_B1 * y + pr_C1) / denom;
  FPy^ := FPy^ + VVAR * (pr_A2 * x + pr_B2 * y + pr_C2) / denom;
  //FPz^ := FPz^ + VVAR * FTz^;   
end;

procedure TVariationProjective.CalcPre;
var  x, y, denom: double;
begin
   x := FTx^;
   y := FTy^;

  denom := pr_A * x + pr_B * y + pr_C;
  if (abs(denom) < 1E-20) then denom := 1E-20;

  FTx^ := VVAR * (pr_A1 * x + pr_B1 * y + pr_C1) / denom;
  FTy^ := VVAR * (pr_A2 * x + pr_B2 * y + pr_C2) / denom;
  //FTz^ := VVAR * FTz^;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationProjective.Create;
begin
  pr_A := 0; 	pr_B := 0; 		pr_C := 1;  // denominator coefs
  pr_A1 := 1; 	pr_B1 := 0; 	pr_C1 := 0;  // x-enumerator coefs
  pr_A2 := 0; 	pr_B2 := 1; 	pr_C2 := 0;  // y-enumerator coefs
  pr_mode := 1;  // order of applying
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationProjective.GetName: string;
begin
  Result := 'projective';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationProjective.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'pr_A';
  1: Result := 'pr_B';
  2: Result := 'pr_C';
  3: Result := 'pr_A1';
  4: Result := 'pr_B1';
  5: Result := 'pr_C1';
  6: Result := 'pr_A2';
  7: Result := 'pr_B2';
  8: Result := 'pr_C2';
  9: Result := 'projective_mode';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationProjective.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'pr_A' then begin
    pr_A := Value;
    Result := True;
  end else if Name = 'pr_B' then begin
    pr_B := Value;
    Result := True;
  end else if Name = 'pr_C' then begin
    pr_C := Value;
    Result := True;
  end else if Name = 'pr_A1' then begin
    pr_A1 := Value;
    Result := True;
  end else if Name = 'pr_B1' then begin
    pr_B1 := Value;
    Result := True;
  end else if Name = 'pr_C1' then begin
    pr_C1 := Value;
    Result := True;
  end else if Name = 'pr_A2' then begin
    pr_A2 := Value;
    Result := True;
  end else if Name = 'pr_B2' then begin
    pr_B2 := Value;
    Result := True;
  end else if Name = 'pr_C2' then begin
    pr_C2 := Value;
    Result := True;
  end else if Name = 'projective_mode' then begin
    if (Value < 0) then Value := 0;
    if (Value > 2) then Value := 2;
    pr_mode := Round(Value);
    Result := True;
  end
end;
function TVariationProjective.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'pr_A' then begin
    pr_A := 0;
    Result := True;
  end else if Name = 'pr_B' then begin
    pr_B := 0;
    Result := True;
  end else if Name = 'pr_C' then begin
    pr_C := 1;
    Result := True;
  end else if Name = 'pr_A1' then begin
    pr_A1 := 1;
    Result := True;
  end else if Name = 'pr_B1' then begin
    pr_B1 := 0;
    Result := True;
  end else if Name = 'pr_C1' then begin
    pr_C1 := 0;
    Result := True;
  end else if Name = 'pr_A2' then begin
    pr_A2 := 0;
    Result := True;
  end else if Name = 'pr_B2' then begin
    pr_B2:= 1;
    Result := True;
  end else if Name = 'pr_C2' then begin
    pr_C2 := 0;
    Result := True;
  end else if Name = 'projective_mode' then begin
    pr_mode := 1;
    Result := True;
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationProjective.GetNrVariables: integer;
begin
  Result := 10
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationProjective.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'pr_A' then begin
    Value := pr_A;
    Result := True;
  end else if Name = 'pr_B' then begin
    Value := pr_B;
    Result := True;
  end else if Name = 'pr_C' then begin
    Value := pr_C;
    Result := True;
  end else if Name = 'pr_A1' then begin
    Value := pr_A1;
    Result := True;
  end else if Name = 'pr_B1' then begin
    Value := pr_B1;
    Result := True;
  end else if Name = 'pr_C1' then begin
    Value := pr_C1;
    Result := True;
  end else if Name = 'pr_A2' then begin
    Value := pr_A2;
    Result := True;
  end else if Name = 'pr_B2' then begin
    Value := pr_B2;
    Result := True;
  end else if Name = 'pr_C2' then begin
    Value := pr_C2;
    Result := True;
  end else if Name = 'projective_mode' then begin
    Value := pr_mode;
    Result := True;
  end
end;

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