{
     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

     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 varModulus;

interface

uses
  BaseVariation, XFormMan;

const
  modx = 'modulus_x';
  mody = 'modulus_y';
  modz = 'modulus_z';
  mod3D = 'modulus_use3D';
type
  TVariationModulus = class(TBaseVariation)
  private
      mx, my, mz, xrange, yrange, zrange: double;
	  m3D: byte;
  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;

{ TVariationModulus }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationModulus.Prepare;
begin
    xrange := 2.0 * mx;
    yrange := 2.0 * my;
    zrange := 2.0 * mz;
end;

//////////////////////////////////////////
procedure TVariationModulus.CalcFunction;
begin
   if (FTx^ > mx) then
        FPx^ := FPx^ + VVAR * (-mx + fmod(FTx^ + mx, xrange))
   else if (FTx^ < -mx) then
        FPx^ := FPx^ + VVAR * (mx - fmod(mx - FTx^, xrange))
   else
        FPx^ := FPx^ + VVAR * FTx^;

   if (FTy^ > my) then
        FPy^ := FPy^ + VVAR * (-my + fmod(FTy^ + my, yrange))
   else if (FTy^ < -my) then
        FPy^ := FPy^ + VVAR * (my - fmod(my - FTy^, yrange))
   else
        FPy^ := FPy^ + VVAR * FTy^;
		
		
	if (m3D <> 0) then 
	begin
		if (FTz^ > mz) then
			FPz^ := FPz^ + VVAR * (-mz + fmod(FTz^ + mz, zrange))
		else if (FTz^ < -mz) then
			FPz^ := FPz^ + VVAR * (mz - fmod(mz - FTz^, zrange))
		else
			FPz^ := FPz^ + VVAR * FTz^;
	end;
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationModulus.Create;
begin
  mx := 1;
  my := 1;
  mz := 1;
  m3D := 0;
end;

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

///////////////////////////////////////////////////////////////////////////////
class function TVariationModulus.GetName: string;
begin
  Result := 'modulus';
end;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
function TVariationModulus.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
    0: Result := modx;
    1: Result := mody;
    2: Result := modz;
    3: Result := mod3D;
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationModulus.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = modx then begin
    mx := Value;
    Result := True;
  end
  else if Name = mody then begin
    my := value;
    Result := True;
  end else if Name = modz then begin
    mz := value;
    Result := True;
  end else if Name = mod3D then begin
    if (value < 0) then Value := 0;
	if (value > 1) then Value := 1;
    m3D := Round(Value);  
    Result := True;
  end;
end;

function TVariationModulus.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = modx then begin
    mx := 1;
    Result := True;
  end
  else if Name = mody then begin
    my := 1;
    Result := True;
  end else if Name = modz then begin
    mz := 1;
    Result := True;
  end else if Name = mod3D then begin
    m3D := 0;
    Result := True;
  end;
end;

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

function TVariationModulus.GetNrVariables: integer;
begin
  Result := 4;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationModulus.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = modx then begin
    Value := mx;
    Result := true;
  end
  else if Name = mody then begin
    Value := my;
    Result := true;
  end else if Name = modz then begin
    Value := mz;
    Result := true;
  end else if Name = mod3D then begin
    Value := m3D;
    Result := true;
  end;
end;

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