GLS.FileGTS.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileGTS;
  5. (* GTS (GNU Triangulated Surface) vector file format implementation. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.Classes,
  10. GLS.VectorGeometry,
  11. GLS.VectorLists,
  12. GLS.VectorFileObjects,
  13. GLS.ApplicationFileIO;
  14. type
  15. (* The GTS vector file (GNU Triangulated Surface library).
  16. It is a simple text format, with indexed vertices. The first line contains
  17. the number of vertices, the number of edges and the number of faces separated
  18. by spaces.
  19. Following lines contain the x/y/z coordinates of vertices, then the edges
  20. (two indices) and the faces (three indices).
  21. http://gts.sourceforge.net/ *)
  22. TGLGTSVectorFile = class(TGLVectorFile)
  23. public
  24. class function Capabilities: TGLDataFileCapabilities; override;
  25. procedure LoadFromStream(aStream: TStream); override;
  26. end;
  27. // ------------------------------------------------------------------
  28. implementation
  29. // ------------------------------------------------------------------
  30. uses
  31. {$IFDEF Unicode}
  32. Sysutils,
  33. {$ENDIF}
  34. GLS.Utils;
  35. // ------------------
  36. // ------------------ TGLGTSVectorFile ------------------
  37. // ------------------
  38. class function TGLGTSVectorFile.Capabilities: TGLDataFileCapabilities;
  39. begin
  40. Result := [dfcRead];
  41. end;
  42. procedure TGLGTSVectorFile.LoadFromStream(aStream: TStream);
  43. var
  44. i, nv, ne, nf, k, ei: Integer;
  45. sl: TStringList;
  46. mesh: TGLMeshObject;
  47. fg: TFGVertexIndexList;
  48. vertIndices: array [0 .. 5] of Integer;
  49. pEdge, pTri, p: PChar;
  50. begin
  51. sl := TStringList.Create;
  52. try
  53. sl.LoadFromStream(aStream{$IFDEF Unicode}, TEncoding.ASCII{$ENDIF});
  54. mesh := TGLMeshObject.CreateOwned(Owner.MeshObjects);
  55. mesh.Mode := momFaceGroups;
  56. if sl.Count > 0 then
  57. begin
  58. p := PChar(sl[0]);
  59. nv := ParseInteger(p);
  60. ne := ParseInteger(p);
  61. nf := ParseInteger(p);
  62. if (nv or nf or ne) = 0 then
  63. Exit;
  64. for i := 1 to nv do
  65. begin
  66. p := PChar(sl[i]);
  67. mesh.Vertices.Add(ParseFloat(p), ParseFloat(p), ParseFloat(p));
  68. end;
  69. fg := TFGVertexIndexList.CreateOwned(mesh.FaceGroups);
  70. for i := 1 + nv + ne to nv + ne + nf do
  71. begin
  72. pTri := PChar(sl[i]);
  73. for k := 0 to 2 do
  74. begin
  75. ei := ParseInteger(pTri);
  76. pEdge := PChar(sl[nv + ei]);
  77. vertIndices[k * 2 + 0] := ParseInteger(pEdge);
  78. vertIndices[k * 2 + 1] := ParseInteger(pEdge);
  79. end;
  80. if (vertIndices[0] = vertIndices[2]) or (vertIndices[0] = vertIndices[3])
  81. then
  82. fg.VertexIndices.Add(vertIndices[0] - 1)
  83. else
  84. fg.VertexIndices.Add(vertIndices[1] - 1);
  85. if (vertIndices[2] = vertIndices[4]) or (vertIndices[2] = vertIndices[5])
  86. then
  87. fg.VertexIndices.Add(vertIndices[2] - 1)
  88. else
  89. fg.VertexIndices.Add(vertIndices[3] - 1);
  90. if (vertIndices[4] = vertIndices[0]) or (vertIndices[4] = vertIndices[1])
  91. then
  92. fg.VertexIndices.Add(vertIndices[4] - 1)
  93. else
  94. fg.VertexIndices.Add(vertIndices[5] - 1);
  95. end;
  96. mesh.BuildNormals(fg.VertexIndices, momTriangles);
  97. end;
  98. finally
  99. sl.Free;
  100. end;
  101. end;
  102. // ------------------------------------------------------------------
  103. initialization
  104. // ------------------------------------------------------------------
  105. RegisterVectorFileFormat('gts', 'GNU Triangulated Surface', TGLGTSVectorFile);
  106. end.