GLFilePLY.pas 3.2 KB

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