LZScene

Форк
0
/
GLFileTIN.pas 
160 строк · 5.4 Кб
1
//
2
// This unit is part of the GLScene Engine https://github.com/glscene
3
//
4
{
5
   TIN (Triangular Irregular Network) vector file format implementation.
6
   History : 
7
       08/04/13 - PW - Fixed a bug in VertArr: decremented element numbers by one
8
       17/11/05 - PW - Added support for ascii TIN files with materials
9
       05/06/03 - SG - Separated from GLVectorFileObjects.pas
10
	 
11
}
12
unit GLFileTIN;
13

14
interface
15

16
{$I GLScene.inc}
17

18
uses
19
  Classes, SysUtils,
20
  GLVectorFileObjects, GLApplicationFileIO;
21

22
type
23
   // TGLTINVectorFile
24
   //
25
   { The TIN vector file (triangle irregular network).
26
      It is a simple text format, with one triangle record per line, no materials,
27
      no texturing (there may be more, but I never saw anything in this files).
28
      This format is encountered in the DEM/DTED world and used in place of grids. }
29
   TGLTINVectorFile = class(TGLVectorFile)
30
      public
31
          
32
         class function Capabilities : TGLDataFileCapabilities; override;
33
         procedure LoadFromStream(aStream : TStream); override;
34
   end;
35

36
// ------------------------------------------------------------------
37
// ------------------------------------------------------------------
38
// ------------------------------------------------------------------
39
implementation
40
// ------------------------------------------------------------------
41
// ------------------------------------------------------------------
42
// ------------------------------------------------------------------
43

44
uses
45
  GLVectorGeometry, GLUtils, GLTypes;
46
// ------------------
47
// ------------------ TGLTINVectorFile ------------------
48
// ------------------
49

50
// Capabilities
51
//
52
class function TGLTINVectorFile.Capabilities : TGLDataFileCapabilities;
53
begin
54
   Result:=[dfcRead];
55
end;
56

57
// LoadFromStream
58
//
59
procedure TGLTINVectorFile.LoadFromStream(aStream : TStream);
60
var
61
   i, j : Integer;
62
   sl, tl : TStringList;
63
   mesh : TGLMeshObject;
64
   v1, v2, v3, n : TAffineVector;
65
   ActiveTin : Boolean;
66
   Id_Tin : Integer;
67
   Tnam, S : string;
68
   Id_Mat, NVert, NTri : Integer;
69

70
   VertArr :  TGLPoint3DArray;
71
   n1, n2, n3 : Integer;
72

73

74
begin
75
  sl := TStringList.Create;
76
  tl := TStringList.Create;
77
  try
78
    sl.LoadFromStream(aStream);
79
    mesh      := TGLMeshObject.CreateOwned(Owner.MeshObjects);
80
    mesh.Mode := momTriangles;
81
    if sl[0]<>'TIN' then    // the file with single TIN described by vertices only
82
    begin
83
      for i := 0 to sl.Count - 1 do
84
      begin
85
        tl.CommaText := Copy(sl[i], 0, MaxInt);
86
        Trim(tl.CommaText);
87
        if tl.Count = 9 then
88
        begin
89
            SetVector(v1, GLUtils.StrToFloatDef(tl[0],0), GLUtils.StrToFloatDef(tl[1],0), GLUtils.StrToFloatDef(tl[2],0));
90
            SetVector(v2, GLUtils.StrToFloatDef(tl[3],0), GLUtils.StrToFloatDef(tl[4],0), GLUtils.StrToFloatDef(tl[5],0));
91
            SetVector(v3, GLUtils.StrToFloatDef(tl[6],0), GLUtils.StrToFloatDef(tl[7],0), GLUtils.StrToFloatDef(tl[8],0));
92
            mesh.Vertices.Add(v1, v2, v3);
93
            n := CalcPlaneNormal(v1, v2, v3);
94
            mesh.Normals.Add(n, n, n);
95
        end;
96
      end
97
    end
98
    else  // the file with multiple TINs described by triangles and materials
99
    while i < sl.Count - 1  do
100
    begin
101
      Inc(i);
102
      tl.DelimitedText := sl[i];
103
      if (tl.CommaText = 'BEGT') then // the beginning of new tin
104
      begin
105
        repeat
106
          Inc(i); tl.DelimitedText := sl[i];
107
          if (tl[0] = 'ACTIVETIN') then
108
            ActiveTin := True;
109
          if (tl[0] = 'ID') then
110
            Id_Tin := StrToInt(tl[1]);
111
          if (tl[0] = 'TNAM') then
112
            Tnam := tl[1];
113
          if (tl[0] = 'MAT') then
114
            Id_Mat := StrToInt(tl[1]);
115
          if (tl[0] = 'VERT') then
116
            NVert := StrToInt(tl[1]);
117
        until tl[0]='VERT';
118
        SetLength(VertArr, NVert);
119
        j := 0;
120
        repeat
121
          Inc(i);
122
          tl.DelimitedText := sl[i];
123
          VertArr[j].X := StrToFloat(tl[0]);
124
          VertArr[j].Y := StrToFloat(tl[1]);
125
          VertArr[j].Z := StrToFloat(tl[2]);
126
          Inc(j);
127
        until (j = NVert);
128
        Inc(i);  tl.DelimitedText := sl[i];
129
        NTri := StrToInt(tl[1]);
130
        j := 0;
131
        repeat
132
          Inc(i); Inc(j);
133
          tl.DelimitedText := sl[i];
134
          n1 := StrToInt(tl[0]); n2 := StrToInt(tl[1]); n3 := StrToInt(tl[2]);
135
          SetVector(v1, VertArr[n1-1].X, VertArr[n1-1].Y, VertArr[n1-1].Z);
136
          SetVector(v2, VertArr[n2-1].X, VertArr[n2-1].Y, VertArr[n2-1].Z);
137
          SetVector(v3, VertArr[n3-1].X, VertArr[n3-1].Y, VertArr[n3-1].Z);
138
          mesh.Vertices.Add(v1, v2, v3);
139
          n := CalcPlaneNormal(v1, v2, v3);
140
          mesh.Normals.Add(n, n, n);
141
        until (j = NTri);
142
        Inc(i); tl.DelimitedText := sl[i]; //tl.DelimitedText = 'ENDT';
143
      end;
144
    end;
145
  finally
146
    tl.Free;
147
    sl.Free;
148
  end;
149
end;
150

151
// ------------------------------------------------------------------
152
// ------------------------------------------------------------------
153
// ------------------------------------------------------------------
154
initialization
155
// ------------------------------------------------------------------
156
// ------------------------------------------------------------------
157
// ------------------------------------------------------------------
158

159
   RegisterVectorFileFormat('tin', 'Triangular Irregular Network', TGLTINVectorFile);
160

161
end.
162

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.