GLS.FileTIN.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileTIN;
  5. (* TIN (Triangular Irregular Network) vector file format implementation *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.Classes,
  10. System.SysUtils,
  11. GLS.VectorTypes,
  12. GLS.VectorFileObjects,
  13. GLS.ApplicationFileIO,
  14. GLS.VectorGeometry,
  15. GLS.VectorTypesExt,
  16. GLS.Utils;
  17. type
  18. (*The TIN vector file (triangle irregular network).
  19. It is a simple text format, with one triangle record per line, no materials,
  20. no texturing (there may be more, but I never saw anything in this files).
  21. This format is encountered in the DEM/DTED world and used in place of grids. *)
  22. TGLTINVectorFile = class(TGLVectorFile)
  23. public
  24. class function Capabilities : TGLDataFileCapabilities; override;
  25. procedure LoadFromStream(aStream : TStream); override;
  26. end;
  27. // ------------------------------------------------------------------
  28. implementation
  29. // ------------------------------------------------------------------
  30. // ------------------
  31. // ------------------ TGLTINVectorFile ------------------
  32. // ------------------
  33. class function TGLTINVectorFile.Capabilities : TGLDataFileCapabilities;
  34. begin
  35. Result:=[dfcRead];
  36. end;
  37. procedure TGLTINVectorFile.LoadFromStream(aStream : TStream);
  38. var
  39. i, j : Integer;
  40. sl, tl : TStringList;
  41. mesh : TGLMeshObject;
  42. v1, v2, v3, n : TAffineVector;
  43. ActiveTin : Boolean;
  44. Id_Tin : Integer;
  45. Tnam: string;
  46. Id_Mat, NVert, NTri : Integer;
  47. VertArr : TPoint3DArray;
  48. n1, n2, n3 : Integer;
  49. begin
  50. sl := TStringList.Create;
  51. tl := TStringList.Create;
  52. i := 0;
  53. try
  54. sl.LoadFromStream(aStream);
  55. mesh := TGLMeshObject.CreateOwned(Owner.MeshObjects);
  56. mesh.Mode := momTriangles;
  57. if sl[0]<>'TIN' then // the file with single TIN described by vertices only
  58. begin
  59. for i := 0 to sl.Count - 1 do
  60. begin
  61. tl.CommaText := Copy(sl[i], 0, MaxInt);
  62. Trim(tl.CommaText);
  63. if tl.Count = 9 then
  64. begin
  65. SetVector(v1, GLStrToFloatDef(tl[0],0), GLStrToFloatDef(tl[1],0), GLStrToFloatDef(tl[2],0));
  66. SetVector(v2, GLStrToFloatDef(tl[3],0), GLStrToFloatDef(tl[4],0), GLStrToFloatDef(tl[5],0));
  67. SetVector(v3, GLStrToFloatDef(tl[6],0), GLStrToFloatDef(tl[7],0), GLStrToFloatDef(tl[8],0));
  68. mesh.Vertices.Add(v1, v2, v3);
  69. n := CalcPlaneNormal(v1, v2, v3);
  70. mesh.Normals.Add(n, n, n);
  71. end;
  72. end
  73. end
  74. else // the file with multiple TINs described by triangles and materials
  75. while i < sl.Count - 1 do
  76. begin
  77. Inc(i);
  78. tl.DelimitedText := sl[i];
  79. if (tl.CommaText = 'BEGT') then // the beginning of new tin
  80. begin
  81. repeat
  82. Inc(i); tl.DelimitedText := sl[i];
  83. if (tl[0] = 'ACTIVETIN') then
  84. ActiveTin := True;
  85. if (tl[0] = 'ID') then
  86. Id_Tin := StrToInt(tl[1]);
  87. if (tl[0] = 'TNAM') then
  88. Tnam := tl[1];
  89. if (tl[0] = 'MAT') then
  90. Id_Mat := StrToInt(tl[1]);
  91. if (tl[0] = 'VERT') then
  92. NVert := StrToInt(tl[1]);
  93. until tl[0]='VERT';
  94. SetLength(VertArr, NVert);
  95. j := 0;
  96. repeat
  97. Inc(i);
  98. tl.DelimitedText := sl[i];
  99. VertArr[j].X := GLStrToFloatDef(tl[0]);
  100. VertArr[j].Y := GLStrToFloatDef(tl[1]);
  101. VertArr[j].Z := GLStrToFloatDef(tl[2]);
  102. Inc(j);
  103. until (j = NVert);
  104. Inc(i);
  105. tl.DelimitedText := sl[i];
  106. NTri := StrToInt(tl[1]);
  107. j := 0;
  108. repeat
  109. Inc(i); Inc(j);
  110. tl.DelimitedText := sl[i];
  111. n1 := StrToInt(tl[0]); n2 := StrToInt(tl[1]); n3 := StrToInt(tl[2]);
  112. SetVector(v1, VertArr[n1-1].X, VertArr[n1-1].Y, VertArr[n1-1].Z);
  113. SetVector(v2, VertArr[n2-1].X, VertArr[n2-1].Y, VertArr[n2-1].Z);
  114. SetVector(v3, VertArr[n3-1].X, VertArr[n3-1].Y, VertArr[n3-1].Z);
  115. mesh.Vertices.Add(v1, v2, v3);
  116. n := CalcPlaneNormal(v1, v2, v3);
  117. mesh.Normals.Add(n, n, n);
  118. until (j = NTri);
  119. Inc(i); tl.DelimitedText := sl[i]; //tl.DelimitedText = 'ENDT';
  120. end;
  121. end;
  122. finally
  123. tl.Free;
  124. sl.Free;
  125. end;
  126. end;
  127. // ------------------------------------------------------------------
  128. initialization
  129. // ------------------------------------------------------------------
  130. RegisterVectorFileFormat('tin', 'Triangular Irregular Network', TGLTINVectorFile);
  131. end.