GLS.FileTIN.pas 4.5 KB

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