Menu

[r7533]: / trunk / Source / GLS.FilePLY.pas  Maximize  Restore  History

Download this file

111 lines (97 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
//
// The graphics engine GLXEngine. The unit of GLScene for Delphi
//
unit GLS.FilePLY;
(* PLY (Stanford Triangle Format) vector file format implementation *)
interface
{$I Stage.Defines.inc}
uses
System.Classes,
System.SysUtils,
Stage.VectorGeometry,
GLS.VectorLists,
GLS.VectorFileObjects,
GLS.ApplicationFileIO,
Stage.Utils;
type
(* The PLY vector file aka Stanford Triangle Format.
This is a format for storing graphical objects that are described as a
collection of polygons. The format is extensible, supports variations and
subformats. This importer only works for the simplest variant (triangles
without specified normals, and will ignore most header specifications. *)
TGLPLYVectorFile = class(TGLVectorFile)
public
class function Capabilities: TGLDataFileCapabilities; override;
procedure LoadFromStream(aStream: TStream); override;
end;
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------
// ------------------ TGLPLYVectorFile ------------------
// ------------------
class function TGLPLYVectorFile.Capabilities: TGLDataFileCapabilities;
begin
Result := [dfcRead];
end;
procedure TGLPLYVectorFile.LoadFromStream(aStream: TStream);
var
i, nbVertices, nbFaces: Integer;
sl: TStringList;
mesh: TGLMeshObject;
fg: TFGVertexIndexList;
p: PChar;
begin
sl := TStringList.Create;
try
sl.LoadFromStream(aStream{$IFDEF Unicode}, TEncoding.ASCII{$ENDIF});
mesh := TGLMeshObject.CreateOwned(Owner.MeshObjects);
mesh.Mode := momFaceGroups;
if sl[0] <> 'ply' then
raise Exception.Create('Not a valid ply file !');
nbVertices := 0;
nbFaces := 0;
i := 0;
while i < sl.Count do
begin
if sl[i] = 'end_header' then
Break;
if Copy(sl[i], 1, 14) = 'element vertex' then
nbVertices := StrToIntDef(Copy(sl[i], 16, MaxInt), 0);
if Copy(sl[i], 1, 12) = 'element face' then
nbFaces := StrToIntDef(Copy(sl[i], 14, MaxInt), 0);
Inc(i);
end;
Inc(i);
// vertices
mesh.Vertices.Capacity := nbVertices;
while (i < sl.Count) and (nbVertices > 0) do
begin
p := PChar(sl[i]);
mesh.Vertices.Add(ParseFloat(p), ParseFloat(p), ParseFloat(p));
// AffineVectorMake(GLStrToFloatDef(tl[0]), GLStrToFloatDef(tl[1]), GLStrToFloatDef(tl[2])));}
Dec(nbVertices);
Inc(i);
end;
// faces
fg := TFGVertexIndexList.CreateOwned(mesh.FaceGroups);
fg.Mode := fgmmTriangles;
fg.VertexIndices.Capacity := nbFaces * 3;
while (i < sl.Count) and (nbFaces > 0) do
begin
p := PChar(sl[i]);
ParseInteger(p); // skip index
fg.VertexIndices.Add(ParseInteger(p), ParseInteger(p), ParseInteger(p));
Dec(nbFaces);
Inc(i);
end;
mesh.BuildNormals(fg.VertexIndices, momTriangles);
finally
sl.Free;
end;
end;
// ------------------------------------------------------------------
initialization
// ------------------------------------------------------------------
RegisterVectorFileFormat('ply', 'Stanford triangle format', TGLPLYVectorFile);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.