{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit DocoFastLineSeriesClass;

interface
uses Classes, Series, XMLClasses, SysUtils, Chart, Graphics, TeEngine;

type

  TDouble = class
  public
    value : double;
    constructor Create(AValue : double);
  end;

  TDocoFastLineSeries = class(TFastLineSeries)
  private
    FeOrdo : double;
    FeAbsc : double;
    tpsVisu : double;
    freqVisu : double;
    index : integer;
    myTempyValues : TMemoryStream;
    myTempxValues : TMemoryStream;
  public
    numDataOrdo : integer;
    numDataAbsc : integer;
    constructor Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; windowVisu : double);reintroduce;
    procedure majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);virtual;abstract;
    procedure ClearSerie;virtual;abstract;
    procedure AfterDraw;virtual;abstract;
    destructor destroy;override;
  end;

  TSweepSerie = class(TDocoFastLineSeries)
  private
    indexDefil : integer;
    NDefil : double;
    dNDefil : double;
  public
    constructor Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
    procedure majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);override;
    procedure ClearSerie;override;
    procedure AfterDraw;override;
  end;

  TOscilloSerie = class(TDocoFastLineSeries)
  private
    indexOscillo : integer;
    NOscillo : double;
    dNOscillo : double;
  public
    constructor Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
    procedure majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);override;
    procedure ClearSerie;override;
    procedure AfterDraw;override;
  end;

  TXYSerie = class(TDocoFastLineSeries)
  private
    dx : double;
    dy : double;
    x1 : single;
    y1 : single;
    x2 : single;
    y2 : single;
    timeSerie : TList;//array of single;
  public
    constructor Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
    procedure majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);override;
    procedure ClearSerie;override;
    procedure AfterDraw;override;
  end;

  TFrequencySerie = class(TDocoFastLineSeries)
  private
    nbPoints : integer;
  public
    constructor Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; nbPoints : integer);
    procedure majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);override;
    procedure ClearSerie;override;
    procedure AfterDraw;override;
  end;

implementation

uses UExecute;

constructor TDouble.Create(AValue : double);
begin
  inherited Create;

  Self.value := AValue;
end;


//*********************//
//serie ancêtre - Debut//
//*********************//
constructor TDocoFastLineSeries.Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; windowVisu : double);
var InfoCanaux : TInfoCanaux;
    indexData : integer;
begin
  inherited Create(AOwner);

  Self.tpsVisu := windowVisu;
  Self.freqVisu := windowVisu;
  
  index := 0;

  infoCanaux := ExecuteForm.DocDOCOMETRE.RendreListeInfoCanaux;

  indexData := infoCanaux.Nom.IndexOf(nomCanalOrdo);
  numDataOrdo := strtoint(infoCanaux.NumCanal[indexData]);
  FeOrdo := ExecuteForm.DocDOCOMETRE.EXPERIENCE.FEG / strtoint(infoCanaux.RappFreq[indexData]);

  //Attention : si graphe temporel , pas de num canal associé en abscisse
  // => numDataAbsc = 0
  indexData := infoCanaux.Nom.IndexOf(nomCanalAbsc);
  if indexData > -1 then
  begin
    numDataAbsc := strtoint(infoCanaux.NumCanal[indexData]);
    FeAbsc := ExecuteForm.DocDOCOMETRE.EXPERIENCE.FEG / strtoint(infoCanaux.RappFreq[indexData]);
  end else
  begin
    numDataAbsc := 0;
    FeAbsc := 0;// ou FEG ?
  end;

  infoCanaux.Free;

end;

destructor TDocoFastLineSeries.destroy;
begin
  self.myTempyValues.Free;
  self.myTempxValues.Free;

  inherited destroy;
end;

//*********************//
//serie ancêtre - Fin  //
//*********************//


//*******************************//
//Graphes type défilement - Debut//
//*******************************//
constructor TSweepSerie.Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
begin
  inherited Create(AOwner,nomCanalOrdo,nomCanalAbsc,tpsVisu);
  myTempyValues := TMemoryStream.Create;
end;

procedure TSweepSerie.ClearSerie;
var  i : integer;
begin
  Self.Clear;
  if ParentChart.ChartWidth > 0
  then for i := 0 to ParentChart.ChartWidth - 1
       do AddXY(i*tpsVisu/ParentChart.ChartWidth,0.0);
  index := 0;
  NDefil := 0.0;
  dNDefil := tpsVisu/ParentChart.ChartWidth*FeOrdo;
  indexDefil := 0;
end;

procedure TSweepSerie.AfterDraw;
var x1,y1,x2,y2 : integer;
begin
  if (indexDefil > 0) and (indexDefil < self.Count) then
  begin
    x1 := Self.CalcXPos(indexDefil - 1);
    y1 := Self.CalcYPos(indexDefil - 1);
    x2 := Self.CalcXPos(indexDefil);
    y2 := Self.CalcYPos(indexDefil);
    Self.ParentChart.Canvas.Pen.Mode := pmBlack;
    Self.ParentChart.Canvas.MoveTo(x1,y1);
    Self.ParentChart.Canvas.LineTo(x2,y2);
  end;
end;

procedure TSweepSerie.majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);
var signal,x : single;
    fin : boolean;
begin
  fin := false;

  if nbdataOrdo = - 1
  then myTempyValues.LoadFromStream(yValues)
  else begin
    myTempyValues.SetSize(0);
    if (yValues.Position + nbdataOrdo*4) < yValues.Size
    then myTempyValues.CopyFrom(yValues,nbDataOrdo*4)
    else if yValues.Position < yValues.Size
         then myTempyValues.CopyFrom(yValues,yValues.Size -yValues.Position)
         else fin := true;
    myTempyValues.Position := 0;
  end;

  while (not fin) do
  begin
    //if (index*4) < yValues.Size then
    if (index*4) < myTempyValues.Size then
    begin
      //yValues.Position := index*4;
      myTempyValues.Position := index*4;
      //yValues.Read(signal,4);
      myTempyValues.Read(signal,4);
      NDefil := NDefil + dNDefil;
      index := trunc(NDefil);
      if indexDefil = Self.Count then
      begin
        Self.Delete(0);
        x := Self.XValues[Self.Count-1] + dNDefil/FeOrdo;
        Self.AddXY(x,signal);
      end else
      begin
        Self.YValues[indexDefil] := signal;
        inc(indexDefil);
      end;
    end else
    begin
      //NDefil := NDefil - yValues.Size div 4;
      NDefil := NDefil - myTempyValues.Size div 4;
      index := trunc(NDefil);
      fin := true;
    end;
  end;
  Self.Repaint;
end;
//*******************************//
//Graphes type défilement - Fin  //
//*******************************//


//****************************//
//Graphes type oscillo - Debut//
//****************************//
constructor TOscilloSerie.Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
begin
  inherited Create(AOwner,nomCanalOrdo,nomCanalAbsc,tpsVisu);
  myTempyValues := TMemoryStream.Create;
end;

procedure TOscilloSerie.ClearSerie;
var i : integer;
begin
  Self.Clear;
  if ParentChart.ChartWidth > 0
  then for i := 0 to ParentChart.ChartWidth
       do AddXY(i*tpsVisu/ParentChart.ChartWidth,0.0);
  index := 0;
  NOscillo := 0.0;
  dNOscillo := tpsVisu/ParentChart.ChartWidth*FeOrdo;
  indexOscillo := 0;
end;

procedure TOscilloSerie.AfterDraw;
var x1,y1,x2,y2 : integer;
begin
  if (indexOscillo > 0) and (indexOscillo < self.Count) then
  begin
    x1 := Self.CalcXPos(indexOscillo - 1);
    y1 := Self.CalcYPos(indexOscillo - 1);
    x2 := Self.CalcXPos(indexOscillo);
    y2 := Self.CalcYPos(indexOscillo);
    Self.ParentChart.Canvas.Pen.Mode := pmBlack;
    Self.ParentChart.Canvas.MoveTo(x1,y1);
    Self.ParentChart.Canvas.LineTo(x2,y2);
  end;
end;

procedure TOscilloSerie.majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);
var signal : single;
    fin : boolean;
begin
  fin := false;
  if nbdataOrdo = -1
  then myTempyValues.LoadFromStream(yValues)
  else begin
    myTempyValues.SetSize(0);
    if (yValues.Position + nbdataOrdo*4) < yValues.Size
    then myTempyValues.CopyFrom(yValues,nbDataOrdo*4)
    else if yValues.Position < yValues.Size
         then myTempyValues.CopyFrom(yValues,yValues.Size -yValues.Position)
         else fin := true;
    myTempyValues.Position := 0;
  end;

  while(not fin) do
  begin
    if (index*4) < myTempyValues.Size then
    begin
      myTempyValues.Position := index*4;
      myTempyValues.Read(signal,4);
      NOscillo := NOscillo + dNOscillo;
      index := trunc(NOscillo);
      if indexOscillo = Self.Count then indexOscillo := 0;
      Self.YValues[indexOscillo] := signal;
      inc(indexOscillo);
    end else
    begin
      NOscillo := NOscillo - myTempyValues.Size div 4;
      index := trunc(NOscillo);
      fin := true;
    end;
  end;
  Self.Repaint;


end;
//****************************//
//Graphes type oscillo - Fin  //
//****************************//


//******************************//
//Graphes type frequence - Debut//
//*****************************//
constructor TFrequencySerie.Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; nbPoints : integer);
var InfoCanaux : TInfoCanaux;
    RF : double;
begin
  inherited Create(AOwner,nomCanalOrdo,nomCanalAbsc,freqVisu);
  myTempyValues := TMemoryStream.Create;

  infoCanaux := ExecuteForm.DocDOCOMETRE.RendreListeInfoCanaux;
  Self.nbPoints := nbPoints; //strtoint(infoCanaux.TailleTampon[infoCanaux.Nom.IndexOf(nomCanalOrdo)]);
  RF := strtofloat(infoCanaux.RappFreq[infoCanaux.Nom.IndexOf(nomCanalOrdo)]);
  Self.freqVisu := RF * ExecuteForm.DocDOCOMETRE.EXPERIENCE.FEG / 2;

  InfoCanaux.Free;
end;

procedure TFrequencySerie.ClearSerie;
var i : integer;
begin
  Self.Clear;

  if ParentChart.ChartWidth > 0
  then for i := 0 to nbPoints
       do AddXY(i*freqVisu/nbPoints,0.0);

end;

procedure TFrequencySerie.AfterDraw;
begin
  //
end;

procedure TFrequencySerie.majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);
var signal : single;
    fin : boolean;
begin
  fin := false;

  if nbdataOrdo = -1
  then myTempyValues.LoadFromStream(yValues)
  else begin
    myTempyValues.SetSize(0);
    if (yValues.Position + nbdataOrdo*4) < yValues.Size
    then myTempyValues.CopyFrom(yValues,nbDataOrdo*4)
    else if yValues.Position < yValues.Size
         then myTempyValues.CopyFrom(yValues,yValues.Size -yValues.Position)
         else fin := true;
    myTempyValues.Position := 0;
  end;

  index := 0;
  while(not fin) do
  begin
    if (index*4) < myTempyValues.Size then
    begin
      myTempyValues.Position := index*4;
      myTempyValues.Read(signal,4);
      Self.YValues[index] := signal;
      inc(index);
      if index = nbPoints then fin := true;
    end else fin := true;
  end;
  Self.Repaint;
end;
//******************************//
//Graphes type frequence - Fin  //
//******************************//


//************************//
//Graphes type XY - Debut//
//************************//
constructor TXYSerie.Create(AOwner : TComponent;nomCanalOrdo,nomCanalAbsc : string; tpsVisu : double);
begin
  inherited Create(AOwner,nomCanalOrdo,nomCanalAbsc,tpsVisu);
  timeSerie := TList.Create;
  myTempyValues := TMemoryStream.Create;
  myTempxValues := TMemoryStream.Create;
end;

procedure TXYSerie.ClearSerie;
begin
  Self.Clear;
  timeSerie.Clear;
  Self.XValues.Order := loNone ;

  index := 0;
  dx := (Self.ParentChart.BottomAxis.Maximum - Self.ParentChart.BottomAxis.Minimum)/Self.ParentChart.ChartWidth;
  dy := (Self.ParentChart.LeftAxis.Maximum - Self.ParentChart.LeftAxis.Minimum)/Self.ParentChart.ChartHeight;
  x1 := 0; y1 := 0; x2 := 0; y2 := 0;
end;

procedure TXYSerie.AfterDraw;
begin
  //
end;

procedure TXYSerie.majSerie(xValues,yValues : TMemoryStream;nbDataAbsc,nbdataOrdo : integer);
var dxi,dyi : single;
    EndTime,BeginTime : TDouble;
    fin : boolean;
begin
  if Self.Count > 0 then
  begin
    x1 := Self.XValue[Self.Count - 1];
    y1 := Self.YValue[Self.Count - 1];
  end;

  fin := false;

  if nbdataOrdo = - 1
  then myTempyValues.LoadFromStream(yValues)
  else begin
    myTempyValues.SetSize(0);
    if (yValues.Position + nbdataOrdo*4) < yValues.Size
    then myTempyValues.CopyFrom(yValues,nbDataOrdo*4)
    else if yValues.Position < yValues.Size
         then myTempyValues.CopyFrom(yValues,yValues.Size -yValues.Position)
         else fin := true;
    myTempyValues.Position := 0;
  end;

  if nbdataAbsc = - 1
  then myTempxValues.LoadFromStream(xValues)
  else begin
    myTempxValues.SetSize(0);
    if (xValues.Position + nbdataOrdo*4) < xValues.Size
    then myTempxValues.CopyFrom(xValues,nbDataOrdo*4)
    else if xValues.Position < xValues.Size
         then myTempxValues.CopyFrom(xValues,xValues.Size -xValues.Position)
         else fin := true;
    myTempxValues.Position := 0;
  end;


  while (not fin) do
  begin
    //xValues.Read(x2,4);
    myTempxValues.Read(x2,4);
    //yValues.Read(y2,4);
    myTempyValues.Read(y2,4);
    dxi := abs(x2-x1); dyi := abs(y2-y1);

    if (dxi >= dx) and (dyi < dy) then
    begin
      Self.AddXY(x2,y1);        
      x1 := x2;
      timeSerie.Add(TDouble.Create(index / FeOrdo));
    end;

    if (dxi < dx) and (dyi >= dy) then
    begin
      Self.AddXY(x1,y2);
      y1 := y2;
      timeSerie.Add(TDouble.Create(index / FeOrdo))
    end;

    if (dxi >= dx) and (dyi >= dy) then
    begin
      Self.AddXY(x2,y2);
      x1 := x2;
      y1 := y2;
      timeSerie.Add(TDouble.Create(index / FeOrdo))
    end;   

    if timeSerie.Count > 2 then
    begin
      EndTime := timeSerie.Last;
      BeginTime := timeSerie.First;
      while ((EndTime.value - BeginTime.value) > tpsVisu) do
      begin
        Self.Delete(0);
        timeSerie.Delete(0);
        BeginTime := timeSerie.First;
      end;
    end; 

    inc(index);

    fin := ((xValues.Position = xValues.Size) or (yValues.Position = yValues.Size));
  end;
  Self.Repaint;
end;
//************************//
//Graphes type XY - Fin  //
//***********************//

end.
