1 | unit UTrack;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, fgl;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TTrack = class;
|
---|
12 | TTrackPoints = class;
|
---|
13 | TTrackLinks = class;
|
---|
14 |
|
---|
15 | { TTrackPoint }
|
---|
16 |
|
---|
17 | TTrackPoint = class
|
---|
18 | Track: TTrack;
|
---|
19 | Position: TPoint;
|
---|
20 | NeighPoints: TTrackPoints;
|
---|
21 | NeighLinks: TTrackLinks;
|
---|
22 | procedure Connect(TrackPoint: TTrackPoint);
|
---|
23 | procedure Disconnect(TrackPoint: TTrackPoint);
|
---|
24 | constructor Create;
|
---|
25 | destructor Destroy; override;
|
---|
26 | end;
|
---|
27 |
|
---|
28 | { TTrackPoints }
|
---|
29 |
|
---|
30 | TTrackPoints = class(TFPGObjectList<TTrackPoint>)
|
---|
31 | end;
|
---|
32 |
|
---|
33 | { TTrackLink }
|
---|
34 |
|
---|
35 | TTrackLink = class
|
---|
36 | Track: TTrack;
|
---|
37 | Points: TTrackPoints;
|
---|
38 | constructor Create;
|
---|
39 | destructor Destroy; override;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | { TTrackLinks }
|
---|
43 |
|
---|
44 | TTrackLinks = class(TFPGObjectList<TTrackLink>)
|
---|
45 | function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
|
---|
46 | end;
|
---|
47 |
|
---|
48 | { TTrack }
|
---|
49 |
|
---|
50 | TTrack = class
|
---|
51 | public
|
---|
52 | Points: TTrackPoints;
|
---|
53 | Links: TTrackLinks;
|
---|
54 | function AddNew: TTrackPoint;
|
---|
55 | constructor Create;
|
---|
56 | destructor Destroy; override;
|
---|
57 | procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);
|
---|
58 | end;
|
---|
59 |
|
---|
60 |
|
---|
61 | implementation
|
---|
62 |
|
---|
63 | { TTrackLinks }
|
---|
64 |
|
---|
65 | function TTrackLinks.SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
|
---|
66 | var
|
---|
67 | I: Integer;
|
---|
68 | begin
|
---|
69 | I := 0;
|
---|
70 | while (I < 0) and
|
---|
71 | ((Items[I].Points.First <> Point1) or (Items[I].Points.Last <> Point2))
|
---|
72 | and ((Items[I].Points.First <> Point2) or (Items[I].Points.Last <> Point1)) do
|
---|
73 | Inc(I);
|
---|
74 | if I < 0 then Result := Items[I]
|
---|
75 | else Result := nil;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | { TTrackLink }
|
---|
79 |
|
---|
80 | constructor TTrackLink.Create;
|
---|
81 | begin
|
---|
82 | Points := TTrackPoints.Create;
|
---|
83 | Points.OwnsObjects := False;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | destructor TTrackLink.Destroy;
|
---|
87 | begin
|
---|
88 | Points.Free;
|
---|
89 | inherited;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | { TTrackPoints }
|
---|
93 |
|
---|
94 | function TTrack.AddNew: TTrackPoint;
|
---|
95 | begin
|
---|
96 | Result := TTrackPoint.Create;
|
---|
97 | Result.Track := Self;
|
---|
98 | Points.Add(Result);
|
---|
99 | end;
|
---|
100 |
|
---|
101 | { TTrackPoint }
|
---|
102 |
|
---|
103 | procedure TTrackPoint.Connect(TrackPoint: TTrackPoint);
|
---|
104 | var
|
---|
105 | NewLink: TTrackLink;
|
---|
106 | begin
|
---|
107 | if NeighPoints.IndexOf(TrackPoint) = -1 then begin
|
---|
108 | NeighPoints.Add(TrackPoint);
|
---|
109 | TrackPoint.NeighPoints.Add(Self);
|
---|
110 | // Add new link
|
---|
111 | NewLink := TTrackLink.Create;
|
---|
112 | NewLink.Points.Add(TrackPoint);
|
---|
113 | NewLink.Points.Add(Self);
|
---|
114 | NeighLinks.Add(NewLink);
|
---|
115 | TrackPoint.NeighLinks.Add(NewLink);
|
---|
116 | Track.Links.Add(NewLink);
|
---|
117 | end;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TTrackPoint.Disconnect(TrackPoint: TTrackPoint);
|
---|
121 | var
|
---|
122 | Index: Integer;
|
---|
123 | Link: TTrackLink;
|
---|
124 | begin
|
---|
125 | Index := NeighPoints.IndexOf(TrackPoint);
|
---|
126 | if NeighPoints.IndexOf(TrackPoint) <> -1 then begin
|
---|
127 | NeighPoints.Delete(Index);
|
---|
128 | TrackPoint.NeighPoints.Remove(Self);
|
---|
129 | // Remove link
|
---|
130 | Link := NeighLinks.SearchPoints(Self, TrackPoint);
|
---|
131 | NeighLinks.Remove(Link);
|
---|
132 | TrackPoint.NeighLinks.Remove(Link);
|
---|
133 | Track.Links.Remove(Link);
|
---|
134 | end;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | constructor TTrackPoint.Create;
|
---|
138 | begin
|
---|
139 | NeighPoints := TTrackPoints.Create;
|
---|
140 | NeighPoints.OwnsObjects := False;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | destructor TTrackPoint.Destroy;
|
---|
144 | var
|
---|
145 | I: Integer;
|
---|
146 | begin
|
---|
147 | // Disconnect from all before destruction
|
---|
148 | for I := NeighPoints.Count - 1 downto 0 do
|
---|
149 | NeighPoints[I].Disconnect(Self);
|
---|
150 | if Assigned(Track) then Track.Points.Remove(Self);
|
---|
151 | NeighPoints.Free;
|
---|
152 | inherited;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | { TTrack }
|
---|
156 |
|
---|
157 | constructor TTrack.Create;
|
---|
158 | begin
|
---|
159 | Points := TTrackPoints.Create;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | destructor TTrack.Destroy;
|
---|
163 | begin
|
---|
164 | Points.Free;
|
---|
165 | inherited;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | procedure TTrack.RemoveTrackBetween(TP1, TP2: TTrackPoint);
|
---|
169 | var
|
---|
170 | Index1, Index2: Integer;
|
---|
171 | Temp: Integer;
|
---|
172 | I: Integer;
|
---|
173 | begin
|
---|
174 | Index1 := Points.IndexOf(TP1);
|
---|
175 | Index2 := Points.IndexOf(TP2);
|
---|
176 | if (Index1 = -1) then
|
---|
177 | raise Exception.Create('TrackPoint1 not found');
|
---|
178 | if (Index2 = -1) then
|
---|
179 | raise Exception.Create('TrackPoint2 not found');
|
---|
180 | if Index1 > Index2 then begin
|
---|
181 | Temp := Index1;
|
---|
182 | Index1 := Index2;
|
---|
183 | Index2 := Temp;
|
---|
184 | end;
|
---|
185 | for I := 1 to Index2 - Index1 - 1 do
|
---|
186 | Points.Delete(Index1 + 1);
|
---|
187 | end;
|
---|
188 |
|
---|
189 |
|
---|
190 | end.
|
---|
191 |
|
---|