GXS.FileGTS.pas 3.4 KB

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