GLFileGTS.pas 3.5 KB

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