Programmation delphi

tarekbouakkaz Messages postés 1 Date d'inscription samedi 24 avril 2010 Statut Membre Dernière intervention 29 mai 2010 - 29 mai 2010 à 13:48
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 29 mai 2010 à 15:04
salut a tous;
je cherche a importer un fichier de points (*.txt) contient des coordonnées XYZ séparé par un espace.
mais je veux avoir les coordonnées comme des float pas des string pour faire des calcule sur le fichier.
merci a tous.

1 réponse

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
29 mai 2010 à 15:04
type
  PFloat = ^TFloat;
  TFloat = Double;
const
  SieOfFloat = SizeOf(TFloat);

type
  PFloatPoint3D = ^TFloatPoint3D;
  TFloatPoint3D = packed record
    case integer of
      0: (X, Y, Z: TFloat);
      1: (XYZ: array[0..2] of TFloat);
  end;
const
  SizeOfFloatPoint3D = SizeOf(TFloatPoint3D);

type
  TFloatPoint3DFile = class
  private
    fFPList: TList;
    function GetCount: integer;
    function GetFloatPoint3D(index: integer): TFloatPoint3D;
    procedure SetFloatPoint3D(index: integer; const Value: TFloatPoint3D);
  protected
    procedure ClearAll;
    procedure AddFloatPoint(v: TFloatPoint3D);
  public
    property FloatPoints[index: integer]: TFloatPoint3D read GetFloatPoint3D write SetFloatPoint3D;
    property Count: integer read GetCount;
  public
    procedure LoadFromTextStream(Stream: TStream);
    procedure LoadFromTextFile(FileName: string);
    procedure SaveToTextStream(Stream: TStream);
    procedure SaveToTextFile(FileName: string);
    procedure LoadFromRawStream(Stream: TStream);
    procedure LoadFromRawFile(FileName: string);
    procedure SaveToRawStream(Stream: TStream);
    procedure SaveToRawFile(FileName: string);
    constructor Create;
    destructor Destroy; override;
  end;


{ TFloatPoint3DFile }

procedure TFloatPoint3DFile.AddFloatPoint(v: TFloatPoint3D);
var ptr : PFloatPoint3D;
begin
  new(ptr);
  ptr^ := v;
  fFPList.Add(ptr);
end;

procedure TFloatPoint3DFile.ClearAll;
begin
  while fFPList.Count > 0 do
  begin
    Dispose(fFPList.First);
    fFPList.Delete(0);
  end;
end;

constructor TFloatPoint3DFile.Create;
begin
  fFPList := TList.Create;
end;

destructor TFloatPoint3DFile.Destroy;
begin
  ClearAll;
  fFPList.Free;
  inherited;
end;

function TFloatPoint3DFile.GetCount: integer;
begin
  result := fFPList.Count;
end;

function TFloatPoint3DFile.GetFloatPoint3D(index: integer): TFloatPoint3D;
begin
  result := PFloatPoint3D(fFPList.Items[index])^;
end;

procedure TFloatPoint3DFile.LoadFromRawFile(FileName: string);
var Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    LoadFromRawStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TFloatPoint3DFile.LoadFromRawStream(Stream: TStream);
var R: integer;
    V: TFloatPoint3D;
begin
  ClearAll;
  repeat
    R := Stream.Read(V, SizeOfFloatPoint3D);
    if R = SizeOfFloatPoint3D then
      AddFloatPoint(V);
  until R < SizeOfFloatPoint3D;
end;

procedure TFloatPoint3DFile.LoadFromTextFile(FileName: string);
var Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    LoadFromTextStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TFloatPoint3DFile.LoadFromTextStream(Stream: TStream);
var N,I,S,L: integer;
    C: PAnsiChar;
    Vs: AnsiString;
    V: TFloatPoint3D;
begin
  ClearAll;
  S := Stream.Size;
  C := AllocMem(S);
  try
    Stream.Read(C[0], S);
    N := 0;
    while N < S do
    begin
      I := 0;
      while I < 3 do
      begin
        L := N;
        while C[N] in ['0'..'9','.',','] do
          inc(N);
        Vs := copy(C^, L, N-L);
        V.XYZ[I] := StrToFloatDef(Vs, 0);
        inc(I);
      end;
      AddFloatPoint(V);
    end;
  finally
    FreeMem(C);
  end;
end;

procedure TFloatPoint3DFile.SaveToRawFile(FileName: string);
var Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    SaveToRawStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TFloatPoint3DFile.SaveToRawStream(Stream: TStream);
var N, C: integer;
begin
  C := fFPList.Count-1;
  for N := 0 to C do
  begin
    Write(PFloatPoint3D(fFPList.Items[N])^, SizeOfFloatPoint3D);
  end;
end;

procedure TFloatPoint3DFile.SaveToTextFile(FileName: string);
var Stream : TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    SaveToTextStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TFloatPoint3DFile.SaveToTextStream(Stream: TStream);
var N: integer;
    V: TFloatPoint3D;
begin
  with TStringList.Create do
  try
    for N := 0 to fFPList.Count-1 do
    begin
      V := PFloatPoint3D(fFPList.Items[N])^;
      Add(format('%.6f %.6f %.6f',[V.X, V.Y, V.Z]));
    end;
    SaveToStream(Stream);
  finally
    Free;
  end;
end;

procedure TFloatPoint3DFile.SetFloatPoint3D(index: integer; const Value: TFloatPoint3D);
begin
  PFloatPoint3D(fFPList.Items[index])^ := Value;
end;



________________________________________________________
besoin de câbles audio, vidèo, informatique pas cher ?
0
Rejoignez-nous