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

unit varAffine3D;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationAffine3D = class(TBaseVariation)
  private
    affine3D_a00, affine3D_a01, affine3D_a02,
	  affine3D_a10, affine3D_a11, affine3D_a12,
	  affine3D_a20, affine3D_a21, affine3D_a22,
	  affine3D_bx, affine3D_by, affine3D_bz: double;
	  x0, y0, z0: double;
    affine3D_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 TVariationAffine3D.GetCalcFunction(var f: TCalcFunction); 
begin
  case affine3D_mode of
	0: f := CalcPre;
	1: f := CalcFunction;
	else f := CalcPost;
  end;
end;

procedure TVariationAffine3D.CalcPre;
var  x, y, z, dn: double;
begin
  x := affine3D_a00 * FTx^ - affine3D_a01 * FTy^ + affine3D_a02 * FTz^ + affine3D_bx;
  y := -(affine3D_a10 * FTx^ - affine3D_a11 * FTy^ + affine3D_a12 * FTz^ + affine3D_by);
	z := affine3D_a20 * FTx^ - affine3D_a21 * FTy^ + affine3D_a22 * FTz^ + affine3D_bz;
 
	FTx^ := VVAR * x;
  FTy^ := VVAR * y;
  FTz^ := VVAR * z;

  dn := hypot(x - x0, y - y0, z - z0);
  if (dn <> 0) then color^ := abs(cos(hypot(x - x0, y - y0) / dn))
   else color^ := 0;
end;

procedure TVariationAffine3D.CalcFunction;
var  x, y, z, dn: double;
begin

  x := affine3D_a00 * FTx^ - affine3D_a01 * FTy^ + affine3D_a02 * FTz^ + affine3D_bx;
  y := -(affine3D_a10 * FTx^ - affine3D_a11 * FTy^ + affine3D_a12 * FTz^ + affine3D_by);
	z := affine3D_a20 * FTx^ - affine3D_a21 * FTy^ + affine3D_a22 * FTz^ + affine3D_bz;

  FPx^ := FPx^ + VVAR * x;
  FPy^ := FPy^ + VVAR * y;
  FPz^ := FPz^ + VVAR * z;

  dn := hypot(x - x0, y - y0, z - z0);
  if (dn <> 0) then color^ := abs(cos(hypot(x - x0, y - y0) / dn))
   else color^ := 0;
end;

procedure TVariationAffine3D.CalcPost;
var  x, y, z, dn: double;
begin

  x := affine3D_a00 * FPx^ - affine3D_a01 * FPy^ + affine3D_a02 * FPz^ + affine3D_bx;
  y := -(affine3D_a10 * FPx^ - affine3D_a11 * FPy^ + affine3D_a12 * FPz^ + affine3D_by);
	z := affine3D_a20 * FPx^ - affine3D_a21 * FPy^ + affine3D_a22 * FPz^ + affine3D_bz;

  FPx^ := VVAR * x;
  FPy^ := VVAR * y;
  FPz^ := VVAR * z;
	  
  dn := hypot(x - x0, y - y0, z - z0);
  if (dn <> 0) then color^ := abs(cos(hypot(x - x0, y - y0) / dn))
   else color^ := 0;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationAffine3D.Create;
begin
  affine3D_a00 := 1; 	affine3D_a01 := 0; 		affine3D_a02 := 0; 
  affine3D_a10 := 0; 	affine3D_a11 := 1; 		affine3D_a12 := 0;  
  affine3D_a20 := 0; 	affine3D_a21 := 0; 		affine3D_a22 := 1; 
  affine3D_bx := 0;		affine3D_by := 0;  		affine3D_bz := 0;
  x0 := 0; y0 := 0; z0 := 0;
  affine3D_mode := 1;  // order of applying
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationAffine3D.GetName: string;
begin
  Result := 'affine3D';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationAffine3D.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
  0: Result := 'affine3D_a00';
  1: Result := 'affine3D_a01';
  2: Result := 'affine3D_a02';
  3: Result := 'affine3D_a10';
  4: Result := 'affine3D_a11';
  5: Result := 'affine3D_a12';
  6: Result := 'affine3D_a20';
  7: Result := 'affine3D_a21';
  8: Result := 'affine3D_a22';
  9: Result := 'affine3D_bx';
  10: Result := 'affine3D_by'; 
  11: Result := 'affine3D_bz';
  12: Result := 'affine3D_dc_x0';
  13: Result := 'affine3D_dc_y0';
  14: Result := 'affine3D_dc_z0';
  15: Result := 'affine3D_mode';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationAffine3D.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'affine3D_a00' then begin
    affine3D_a00 := Value;
    Result := True;
  end else if Name = 'affine3D_a01' then begin
    affine3D_a01 := Value;
    Result := True;
  end else if Name = 'affine3D_a02' then begin
    affine3D_a02 := Value;
    Result := True;
  end else if Name = 'affine3D_a10' then begin
    affine3D_a10 := Value;
    Result := True;
  end else if Name = 'affine3D_a11' then begin
    affine3D_a11 := Value;
    Result := True;
  end else if Name = 'affine3D_a12' then begin
    affine3D_a12 := Value;
    Result := True;
  end else if Name = 'affine3D_a20' then begin
    affine3D_a20 := Value;
    Result := True;
  end else if Name = 'affine3D_a21' then begin
    affine3D_a21 := Value;
    Result := True;
  end else if Name = 'affine3D_a22' then begin
    affine3D_a22 := Value;
    Result := True;
  end else if Name = 'affine3D_bx' then begin
    affine3D_bx := Value;
    Result := True;
  end else if Name = 'affine3D_by' then begin
    affine3D_by := Value;
    Result := True;
  end else if Name = 'affine3D_bz' then begin
    affine3D_bz := Value;
    Result := True;
  end else if Name = 'affine3D_dc_x0' then begin
    x0 := Value;
    Result := True;
  end else if Name = 'affine3D_dc_y0' then begin
    y0 := Value;
    Result := True;
  end else if Name = 'affine3D_dc_z0' then begin
    z0 := Value;
    Result := True;
  end else if Name = 'affine3D_mode' then begin
    if (Value < 0) then Value := 0;
    if (Value > 2) then Value := 2;
    affine3D_mode := Round(Value);
    Result := True;
  end
end;

function TVariationAffine3D.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'affine3D_a00' then begin
    affine3D_a00 := 1;
    Result := True;
  end else if Name = 'affine3D_a01' then begin
    affine3D_a01 := 0;
    Result := True;
  end else if Name = 'affine3D_a02' then begin
    affine3D_a02 := 0;
    Result := True;
  end else if Name = 'affine3D_a10' then begin
    affine3D_a10 := 0;
    Result := True;
  end else if Name = 'affine3D_a11' then begin
    affine3D_a11 := 1;
    Result := True;
  end else if Name = 'affine3D_a12' then begin
    affine3D_a12 := 0;
    Result := True;
  end else if Name = 'affine3D_a20' then begin
    affine3D_a20 := 0;
    Result := True;
  end else if Name = 'affine3D_a21' then begin
    affine3D_a21:= 0;
    Result := True;
  end else if Name = 'affine3D_a22' then begin
    affine3D_a22 := 1;
    Result := True;
  end else if Name = 'affine3D_bx' then begin
    affine3D_bx := 0;
    Result := True;
  end else if Name = 'affine3D_by' then begin
    affine3D_by := 0;
    Result := True;	
  end else if Name = 'affine3D_bz' then begin
    affine3D_bz := 0;
    Result := True;		
  end  else if Name = 'affine3D_dc_x0' then begin
    x0 := 0;
    Result := True;
  end else if Name = 'affine3D_dc_y0' then begin
    y0 := 0;
    Result := True;
  end else if Name = 'affine3D_dc_z0' then begin
    z0 := 0;
    Result := True;
  end else if Name = 'affine3D_mode' then begin
    affine3D_mode := 1;
    Result := True;
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationAffine3D.GetNrVariables: integer;
begin
  Result := 16
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationAffine3D.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'affine3D_a00' then begin
    Value := affine3D_a00;
    Result := True;
  end else if Name = 'affine3D_a01' then begin
    Value := affine3D_a01;
    Result := True;
  end else if Name = 'affine3D_a02' then begin
    Value := affine3D_a02;
    Result := True;
  end else if Name = 'affine3D_a10' then begin
    Value := affine3D_a10;
    Result := True;
  end else if Name = 'affine3D_a11' then begin
    Value := affine3D_a11;
    Result := True;
  end else if Name = 'affine3D_a12' then begin
    Value := affine3D_a12;
    Result := True;
  end else if Name = 'affine3D_a20' then begin
    Value := affine3D_a20;
    Result := True;
  end else if Name = 'affine3D_a21' then begin
    Value := affine3D_a21;
    Result := True;
  end else if Name = 'affine3D_a22' then begin
    Value := affine3D_a22;
    Result := True;
  end else if Name = 'affine3D_bx' then begin
    Value := affine3D_bx;
    Result := True;
  end else if Name = 'affine3D_by' then begin
    Value := affine3D_by;
    Result := True;
  end else if Name = 'affine3D_bz' then begin
    Value := affine3D_bz;
    Result := True;	
  end else if Name = 'affine3D_dc_x0' then begin
    Value:= x0;
    Result := True;
  end else if Name = 'affine3D_dc_y0' then begin
    Value := y0;
    Result := True;
  end else if Name = 'affine3D_dc_z0' then begin
    Value := z0;
    Result := True;
  end else if Name = 'affine3D_mode' then begin
    Value := affine3D_mode;
    Result := True;
  end
end;

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