-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRecognition.pas
More file actions
374 lines (336 loc) · 9.06 KB
/
Recognition.pas
File metadata and controls
374 lines (336 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
unit Recognition;
{$MODE Delphi}
interface
uses
Shapes, Support, Actions, Graphics, Types, Undo, Picture, Controls, SysUtils;
type
TReco = record
point: TPoint;
time: TDateTime;
end;
TRecognition = class(TAction)
private
_n: Integer;
_points: array of TReco;
_color: TColor;
_width: Integer;
_BGcolor: TColor;
_fill: boolean;
_zoom: Real;
_mousedown: Boolean;
procedure addPoint(p: TPoint);
function recognize(): TShape;
function tryCircleAndEllipse(var odchylka: Real): TShape;
function trySegmentLine(var odchylka: Real): TShape;
function tryPolyLinePolygonAndRectangle(var odchylka: Real): TShape;
function isRectangle(points: TPoints): Boolean;
public
procedure zoom(a: Real); override;
procedure changeShape(shape: TShape); override;
function changeWidth(width: Integer): TUndoStrategy; override;
function changeColor(color: TColor): TUndoStrategy; override;
function delete(): TUndoStrategy; override; //neunduuje sa;
function changeBGColor(color: TColor): TUndoStrategy; override;
function changeBGMode(fill: boolean): TUndoStrategy; override;
function getActualColor(): TColor; override;
function getActualWidth(): Integer; override;
function getActualBGColor(): TColor; override;
function getActualBGMode(): Boolean; override;
function mouseDown(args: TMouseArgs): TUndoStrategy; override;
function mouseMove(args: TMouseArgs): TUndoStrategy; override;
function mouseUp(args: TMouseArgs): TUndoStrategy; override;
procedure draw(C: TCanvas); override;
constructor Create(pic: TVectorPicture; color: TColor = clBlack; width: Integer = 1; backGroundColor: TColor = clWhite; hasBGColor: boolean = false);
destructor Destroy(); override;
end;
implementation
//------------------TRecognition-----------------
procedure TRecognition.changeShape(shape: TShape);
begin
end;
function TRecognition.delete(): TUndoStrategy;
begin
Result := nil;
end;
function TRecognition.tryCircleAndEllipse(var odchylka: Real): TShape;
var
ellipse: TGeneralEllipseEquation;
i: Integer;
p: TPoint;
minx, maxx, miny, maxy, a, b: Integer;
begin
p := NilPoint;
minx := MaxInt;
miny := MaxInt;
maxx := - MaxInt;
maxy := - MaxInt;
for i := 0 to _n - 1 do
begin
if _points[i].point.X > maxx then maxx := _points[i].point.X;
if _points[i].point.Y > maxy then maxy := _points[i].point.Y;
if _points[i].point.X < minx then minx := _points[i].point.X;
if _points[i].point.Y < miny then miny := _points[i].point.Y;
p.X := p.X + _points[i].point.X;
p.Y := p.Y + _points[i].point.Y;
end;
p.X := round(p.X / _n);
p.Y := round(p.Y / _n);
a := round((maxx - minx) / 2);
b := round((maxy - miny) / 2);
ellipse := TGeneralEllipseEquation.Create(p.X, p.Y, a, b);
if (abs(a - b) < 5) then
Result := TCircle.Create(p, round((a+b)/2), _color, _width)
else
Result := TEllipse.Create(p, a, b, _color, _width);
Result.backgroundColor := _BGcolor;
Result.bgColorMode := _fill;
odchylka := 0;
for i := 0 to _n - 1 do
odchylka := odchylka + ellipse.pointDistance(_points[i].point);
odchylka := odchylka / _n;
end;
function TRecognition.trySegmentLine(var odchylka: Real): TShape;
var
line: TGeneralLineEquation;
i: Integer;
begin
line := TGeneralLineEquation.Create(_points[0].point, _points[_n-1].point);
odchylka := 0;
for i := 0 to _n - 1 do
odchylka := odchylka + line.pointDistance(_points[i].point);
odchylka := odchylka / _n;
Result := TSegmentLine.Create(_points[0].point, _points[_n-1].point, _color, _width);
Result.zoom := _zoom;
Result.backgroundColor := _BGcolor;
Result.bgColorMode := _fill;
line.Free;
end;
function TRecognition.isRectangle(points: TPoints): Boolean;
var
rec: TRect;
begin
Result := false;
if Length(points) = 4 then
begin
rec := Rect(points[0].X, points[0].Y, points[2].X, points[2].Y);
if (TSupport.distance(Point(rec.Left, rec.Bottom), points[1]) < 50) and
(TSupport.distance(Point(rec.Right, rec.Top), points[3]) < 50) then
Result := true;
if (TSupport.distance(Point(rec.Left, rec.Bottom), points[3]) < 50) and
(TSupport.distance(Point(rec.Right, rec.Top), points[1]) < 50) then
Result := true;
end;
end;
function TRecognition.tryPolyLinePolygonAndRectangle(var odchylka: Real): TShape;
var
i, m, j: Integer;
poi: TPoints;
begin
SetLength(poi, 5);
poi[0] := _points[0].point;
m := 1;
for i := 0 to _n - 2 do
begin
if (_points[i + 1].time - _points[i].time) > 0.000000185185185188 then
begin
poi[m] := _points[i].point;
m := m + 1;
if (length(poi) < (m + 3)) then
SetLength(poi, Length(poi) + 5);
end;
end;
poi[m] := _points[_n - 1].point;
m := m + 1;
i := 0;
while i <= m - 2 do
begin
if TSupport.distance(poi[i], poi[i+1]) < 20 then
begin
for j := i to m - 2 do
poi[j] := poi[j+1];
m := m - 1;
end
else
i := i + 1;
end;
SetLength(poi, m);
if Length(poi) < 2 then
begin
Result := nil;
odchylka := MaxInt;
Exit;
end;
if (TSupport.distance(poi[0], poi[m - 1])) < 15 then
begin
SetLength(poi, m - 1);
if isRectangle(poi) then
Result := TRectangle.Create(Rect(poi[0].X, poi[0].Y, poi[2].X, poi[2].Y), _color, _width)
else
Result := TPolygon.Create(poi, _color, _width);
end
else
Result := TPolyLine.Create(poi, _color, _width);
Result.backgroundColor := _BGcolor;
Result.bgColorMode := _fill;
odchylka := 0;
for i := 0 to _n - 1 do
odchylka := odchylka + Result.distance(_points[i].point);
odchylka := odchylka / _n;
end;
function TRecognition.recognize(): TShape;
var
s: TShape;
odchylka, min: Real;
i: Integer;
begin
if _n < 5 then
begin
Result := nil;
Exit;
end;
if not(hasPicture) then
Exit;
for i := 0 to _n - 1 do
begin
_points[i].point.X := round(_points[i].point.X / _zoom);
_points[i].point.Y := round(_points[i].point.Y / _zoom);
end;
Result := tryPolyLinePolygonAndRectangle(odchylka);
min := odchylka;
//segment line
s := trySegmentLine(odchylka);
if odchylka < min then
begin
if Result <> nil then
Result.Free;
min := odchylka;
Result := s;
end;
//ellipse & circle
s := tryCircleAndEllipse(odchylka);
if (odchylka < min) then
begin
min := odchylka;
Result.Free;
Result := s;
end;
if min > 20 then
FreeAndNil(Result);
//
end;
procedure TRecognition.zoom(a: Real);
begin
_zoom := a;
_mousedown := false;
_n := 0;
end;
function TRecognition.changeWidth(width: Integer): TUndoStrategy;
begin
Result := nil;
_width := width;
end;
function TRecognition.changeColor(color: TColor): TUndoStrategy;
begin
Result := nil;
_color := color;
end;
function TRecognition.changeBGColor(color: TColor): TUndoStrategy;
begin
Result := nil;
_BGcolor := color;
end;
function TRecognition.changeBGMode(fill: boolean): TUndoStrategy;
begin
Result := nil;
_fill := fill;
end;
function TRecognition.getActualColor(): TColor;
begin
Result := _color;
end;
function TRecognition.getActualWidth(): Integer;
begin
Result := _width;
end;
function TRecognition.getActualBGColor(): TColor;
begin
Result := _BGcolor;
end;
function TRecognition.getActualBGMode(): Boolean;
begin
Result := _fill;
end;
function TRecognition.mouseUp(args: TMouseArgs): TUndoStrategy;
var
s: TShape;
begin
Result := nil;
if (args.button = mbLeft) and _mousedown then
begin
_mousedown := false;
s := recognize();
if s <> nil then
begin
_picture.addShape(s);
Result := TUndoCreate.Create(s, _picture, _picture.getZIndex(s));
end;
SetLength(_points, 100);
_n := 0;
end;
end;
procedure TRecognition.draw(C: TCanvas);
var
i: Integer;
begin
C.MoveTo(_points[0].point.X, _points[0].point.Y);
C.Pen.Color := clGray;
C.Pen.Width := round(_width * _zoom);
for i := 0 to _n - 1 do
if ((i mod 2) = 0) then
C.LineTo(_points[i].point.X, _points[i].point.Y);
end;
function TRecognition.mouseMove(args: TMouseArgs): TUndoStrategy;
begin
Result := nil;
if _mousedown then
addPoint(args.point);
end;
function TRecognition.mouseDown(args: TMouseArgs): TUndoStrategy;
begin
Result := nil;
if (args.button = mbLeft) then
begin
_mousedown := true;
addPoint(args.point);
end;
end;
procedure TRecognition.addPoint(p: TPoint);
begin
_points[_n].point := p;
_points[_n].time := Time();
_n := _n + 1;
if (_n > Length(_points) - 1) then
SetLength(_points, Length(_points) + 50);
end;
constructor TRecognition.Create(pic: TVectorPicture; color: TColor = clBlack; width: Integer = 1; backGroundColor: TColor = clWhite; hasBGColor: boolean = false);
begin
_group := nil;
_shape := nil;
_picture := pic;
_color := color;
_width := width;
_mousedown := false;
_BGcolor := backGroundColor;
_fill := hasBGColor;
if (hasPicture) then
_zoom := _picture.zoom
else
_zoom := 1;
SetLength(_points, 100);
_n := 0;
end;
destructor TRecognition.Destroy();
begin
end;
//-----------------------------------------------
end.