GXS.FileTIN.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.FileTIN;
  5. (* TIN (Triangular Irregular Network) vector file format implementation *)
  6. interface
  7. uses
  8. System.Classes,
  9. System.SysUtils,
  10. Stage.VectorTypes,
  11. Stage.VectorGeometry,
  12. Stage.VectorTypesExt,
  13. GXS.ApplicationFileIO,
  14. GXS.VectorFileObjects,
  15. GXS.ImageUtils;
  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. TgxTINVectorFile = class(TgxVectorFile)
  22. public
  23. class function Capabilities: TDataFileCapabilities; override;
  24. procedure LoadFromStream(aStream: TStream); override;
  25. end;
  26. // ------------------------------------------------------------------
  27. implementation
  28. // ------------------------------------------------------------------
  29. // ------------------
  30. // ------------------ TgxTINVectorFile ------------------
  31. // ------------------
  32. class function TgxTINVectorFile.Capabilities: TDataFileCapabilities;
  33. begin
  34. Result := [dfcRead];
  35. end;
  36. procedure TgxTINVectorFile.LoadFromStream(aStream: TStream);
  37. var
  38. i, j: Integer;
  39. sl, tl: TStringList;
  40. mesh: TgxMeshObject;
  41. v1, v2, v3, n: TAffineVector;
  42. ActiveTin: Boolean;
  43. Id_Tin: Integer;
  44. Tnam: string;
  45. Id_Mat, NVert, NTri: Integer;
  46. VertArr: TPoint3DArray;
  47. n1, n2, n3: Integer;
  48. begin
  49. sl := TStringList.Create;
  50. tl := TStringList.Create;
  51. try
  52. sl.LoadFromStream(aStream);
  53. mesh := TgxMeshObject.CreateOwned(Owner.MeshObjects);
  54. mesh.Mode := momTriangles;
  55. if sl[0] <> 'TIN' then
  56. // 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),
  65. StrToFloatDef(tl[1], 0),
  66. StrToFloatDef(tl[2], 0));
  67. SetVector(v2, StrToFloatDef(tl[3], 0),
  68. StrToFloatDef(tl[4], 0),
  69. StrToFloatDef(tl[5], 0));
  70. SetVector(v3, StrToFloatDef(tl[6], 0),
  71. StrToFloatDef(tl[7], 0),
  72. StrToFloatDef(tl[8], 0));
  73. mesh.Vertices.Add(v1, v2, v3);
  74. n := CalcPlaneNormal(v1, v2, v3);
  75. mesh.Normals.Add(n, n, n);
  76. end;
  77. end
  78. end
  79. else // the file with multiple TINs described by triangles and materials
  80. while i < sl.Count - 1 do
  81. begin
  82. Inc(i);
  83. tl.DelimitedText := sl[i];
  84. if (tl.CommaText = 'BEGT') then // the beginning of new tin
  85. begin
  86. repeat
  87. Inc(i);
  88. tl.DelimitedText := sl[i];
  89. if (tl[0] = 'ACTIVETIN') then
  90. ActiveTin := True;
  91. if (tl[0] = 'ID') then
  92. Id_Tin := StrToInt(tl[1]);
  93. if (tl[0] = 'TNAM') then
  94. Tnam := tl[1];
  95. if (tl[0] = 'MAT') then
  96. Id_Mat := StrToInt(tl[1]);
  97. if (tl[0] = 'VERT') then
  98. NVert := StrToInt(tl[1]);
  99. until tl[0] = 'VERT';
  100. SetLength(VertArr, NVert);
  101. j := 0;
  102. repeat
  103. Inc(i);
  104. tl.DelimitedText := sl[i];
  105. VertArr[j].X := StrToFloat(tl[0]);
  106. VertArr[j].Y := StrToFloat(tl[1]);
  107. VertArr[j].Z := StrToFloat(tl[2]);
  108. Inc(j);
  109. until (j = NVert);
  110. Inc(i);
  111. tl.DelimitedText := sl[i];
  112. NTri := StrToInt(tl[1]);
  113. j := 0;
  114. repeat
  115. Inc(i);
  116. Inc(j);
  117. tl.DelimitedText := sl[i];
  118. n1 := StrToInt(tl[0]);
  119. n2 := StrToInt(tl[1]);
  120. n3 := StrToInt(tl[2]);
  121. SetVector(v1, VertArr[n1 - 1].X, VertArr[n1 - 1].Y,
  122. VertArr[n1 - 1].Z);
  123. SetVector(v2, VertArr[n2 - 1].X, VertArr[n2 - 1].Y,
  124. VertArr[n2 - 1].Z);
  125. SetVector(v3, VertArr[n3 - 1].X, VertArr[n3 - 1].Y,
  126. VertArr[n3 - 1].Z);
  127. mesh.Vertices.Add(v1, v2, v3);
  128. n := CalcPlaneNormal(v1, v2, v3);
  129. mesh.Normals.Add(n, n, n);
  130. until (j = NTri);
  131. Inc(i);
  132. tl.DelimitedText := sl[i]; // tl.DelimitedText = 'ENDT';
  133. end;
  134. end;
  135. finally
  136. tl.Free;
  137. sl.Free;
  138. end;
  139. end;
  140. // ------------------------------------------------------------------
  141. initialization
  142. // ------------------------------------------------------------------
  143. RegisterVectorFileFormat('tin', 'Triangular Irregular Network',
  144. TgxTINVectorFile);
  145. end.