(********************************************
  Version: 7.0
  Date: 2004/08/19
  This is shareware.
  Copyright (C) Michal Pohanka
  All rights reserved.

  Internet: http://www.volny.cz/profipohanka
 ********************************************)

unit UPohTypes;

interface

uses
  SysUtils, Graphics, Types;

type

  // Records
  TXYInteger = packed record
    x, y: integer;
  end;

  TXZInteger = packed record
    x, z: integer;
  end;

  TXYZInteger = packed record
    x, y, z: integer;
  end;

  TXYSingle = packed record
    x, y: single;
  end;

  TXYDouble = packed record
    x, y: double;
  end;

  TXYZDouble = packed record
    x, y, z: double;
  end;

  TRGB = packed record
    r, g, b: byte;
  end;

  TRGBA = packed record
    r, g, b, a: byte;
  end;

  TBGR = packed record
    b, g, r: byte;
  end;

  TBGRA = packed record
    b, g, r, a: byte;
  end;

  // Styles
  TPohLineStyle = (plsNone, plsLine, plsLineZColor);
  TPohPointStyle = (ppsNone, ppsBox, ppsCircle, ppsCrossPlus, ppsCrossX, ppsBarAutoWidth, ppsBarZWidth);
  TPohAxis2DCaptionPos = (pcpCenter, pcpEnd);
  TPohArrowStyle = (pasNone, pasArrow, pasFilled, pasSharp);
  TPohLegendPos = (plpLeft, plpRight, plpTop, plpBottom, plpLeftTop, plpLeftBottom, plpRightTop, plpRightBottom);

	TDirection = (diHorizonal, diVertical);
  TGradientShape = (gsRectangle, gsRoundRect, gsEllipse);
  TScaleCaptionAlignHoriz = (scaLeft, scaRight, scaCenter, scaLabels);
  TScaleCaptionAlignVert = (scaTop, scaBottom);

  TPohBtnViewStyle = (pbvIconText, pbvIconOnly, pbvTextOnly);
  TPohBtnStyle = (pbsSolid, pbsOwnerDraw, pbsHorizontal, pbsVertical, pbsAutoHV, pbsGradient);
  TPohGradFill = (pgfSolid, pgfOwnerDraw, pgfHorizontal, pgfVertical, pgfAutoHV);
  TPohLabelBorder = (plbNone, plbLowered, plbRaised, plbOutlined);
  TPohLabelFill = (plfSolid, plfOwnerDraw, plfHorizontal, plfVertical, plfAutoHV, plfGradRect, plfBitmap, plfBitmapFit);
  TPohLabelBackground = (plbSolid, plbOwnerDraw, plbHorizontal, plbVertical, plbAutoHV, plbGradient, plbTransparent);
  TPohCheckStyle = (pcsSolid, pcsOwnerDraw, pcsHorizontal, pcsVertical, pcsAutoHV, pcsGradient, pcsTransparent);
  TPohShadowStyle = (ssNone, ssSolid, ssGradient);

  TPohAxisPosition = (apZero, apMinimum, apMaximum);
  TPohDragType = (pdNone, pdRotate, pdMove, pdZoom);

  // Arrays
  TByteArray = packed array of byte;
  TSingleArray = packed array of single;
  TSingleArrayAlgn = array of single;
  TDoubleArray = packed array of double;
  TDoubleArrayAlgn = array of double;
  TIntegerArray = packed array of integer;
  TStringArray = packed array of string;
  TMetafileArray = packed array of TMetafile;

  TArr2Double = packed array[0..1] of double;
  TArr3Double = packed array[0..2] of double;

  TArrXYInteger = packed array of TXYInteger;
  TArrXZInteger = packed array of TXZInteger;
  TArrXYZInteger = packed array of TXYZInteger;
  TArrXYSingle = packed array of TXYSingle;
  TArrXYDouble = packed array of TXYDouble;
  TArrXYZDouble = packed array of TXYZDouble;

  TArrRGBA = packed array of TRGBA;
  TArrRGB = packed array of TRGB;
  TArrBGRA = packed array of TBGRA;
  TArrBGR = packed array of TBGR;

  TArr1000RGB = packed array[0..1000] of tRGB;
  PArr1000RGB = ^TArr1000RGB;

  TPohColorArray = packed array of TColor;
  TPoh6ColorArray = packed array[0..5] of TColor;
  TPoh256ColorArray = packed array[0..255] of TColor;

  // 2D Arrays
  TArrArrXYZDouble = packed array of TArrXYZDouble;
  TDoubleMatrixAlgn = array of TDoubleArrayAlgn;

  // Exceptions
  EPohArrayRange = class(Exception);

  // Events
  TPaintCanvasEvent = procedure(Sender: TObject; Canvas: TCanvas) of object;
  TPaintCanvasRectEvent = procedure(Sender: TObject; Canvas: TCanvas; PaintRect: TRect) of object;
  TPaintRectEvent = procedure(Sender: TObject; PaintRect: TRect) of object;
  TPohProgressEvent = procedure(Sender: TObject; progress: double) of object;
  TValidateCharEvent = procedure(Sender: TObject; ch: Char; var isValid: boolean) of object;

  TOptimizedFunc1D = procedure(Sender: TObject; x: double; var y: double) of object;
  TOptimizedFuncND = procedure(Sender: TObject; var p: TDoubleArrayAlgn; var value: double) of object;

  function XYInteger(x, y: integer): TXYInteger;
  function XZInteger(x, z: integer): TXZInteger;
  function XYZInteger(x, y, z: integer): TXYZInteger;
  function XYSingle(x, y: single): TXYSingle;
  function XYDouble(x, y: double): TXYDouble;
  function XYZDouble(x, y, z: double): TXYZDouble;

  function IsEqualCA256(var a, b: TPoh256ColorArray): boolean;
  function ArrayInsert(var arr: TDoubleArray; idx: integer; count: integer=1): boolean; overload // returns true if OK
  procedure AllocDoubleMatrixAlgn(var a: TDoubleMatrixAlgn; columns, rows: integer);
  procedure FreeDoubleMatrixAlgn(var a: TDoubleMatrixAlgn);
implementation

function XYInteger(x, y: integer): TXYInteger;
begin
  result.x:=x;
  result.y:=y;
end;

function XZInteger(x, z: integer): TXZInteger;
begin
  result.x:=x;
  result.z:=z;
end;

function XYZInteger(x, y, z: integer): TXYZInteger;
begin
  result.x:=x;
  result.y:=y;
  result.z:=z;
end;

function XYSingle(x, y: single): TXYSingle;
begin
  result.x:=x;
  result.y:=y;
end;

function XYDouble(x, y: double): TXYDouble;
begin
  result.x:=x;
  result.y:=y;
end;

function XYZDouble(x, y, z: double): TXYZDouble;
begin
  result.x:=x;
  result.y:=y;
  result.z:=z;
end;

function ArrayInsert(var arr: TDoubleArray; idx: integer; count: integer): boolean;
var
  nold, i: integer;
begin
  if count<1 then begin
    result:=true;
    exit;
  end;
  nold:=Length(arr);
  if idx<0 then idx:=0;
  if idx>nold then idx:=nold;
  SetLength(arr, nold+count);
  for i:=nold-1 downto idx do arr[i+count]:=arr[i];
  //Move(arr[idx], arr[idx+count], SizeOf(arr[0])*count);
  result:=(nold+count)=Length(arr);
end;

procedure AllocDoubleMatrixAlgn(var a: TDoubleMatrixAlgn; columns, rows: integer);
var
  c: integer;
begin
  SetLength(a, columns);
  for c:=0 to columns-1 do SetLength(a[c], rows);
end;

procedure FreeDoubleMatrixAlgn(var a: TDoubleMatrixAlgn);
var
  i: integer;
begin
  for i:=0 to High(a) do SetLength(a[i], 0);
  SetLength(a, 0);
end;

function IsEqualCA256(var a, b: TPoh256ColorArray): boolean;
var
  i: integer;
begin
  i:=0;
  while (i<255) and (a[i]=b[i]) do Inc(i);
  result:=i>255;
end;

end.
