Skip to content

Commit

Permalink
DTM rework:
Browse files Browse the repository at this point in the history
Use record, so DTM's do not need to be freed anymore.
Point tolerance is now a single like everyone else
  • Loading branch information
ollydev committed Aug 28, 2023
1 parent 6729084 commit 36a4e57
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 478 deletions.
40 changes: 20 additions & 20 deletions Source/forms/simba.dtmeditor.lfm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
object SimbaDTMEditorForm: TSimbaDTMEditorForm
Left = 888
Left = 428
Height = 1146
Top = 135
Top = 44
Width = 1346
Caption = 'DTM Editor'
ClientHeight = 1121
ClientHeight = 1146
ClientWidth = 1346
DesignTimePPI = 120
KeyPreview = True
Expand All @@ -14,10 +14,10 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
Position = poScreenCenter
ShowHint = True
ShowInTaskBar = stAlways
LCLVersion = '2.2.4.0'
LCLVersion = '3.0.0.1'
object PanelMain: TPanel
Left = 0
Height = 1121
Height = 1146
Top = 0
Width = 990
Align = alClient
Expand All @@ -26,14 +26,14 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
end
object PanelRight: TPanel
Left = 990
Height = 1121
Height = 1146
Top = 0
Width = 356
Align = alRight
Anchors = [akTop, akRight]
AutoSize = True
BevelOuter = bvNone
ClientHeight = 1121
ClientHeight = 1146
ClientWidth = 356
Constraints.MinWidth = 356
TabOrder = 1
Expand All @@ -46,7 +46,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = ButtonUpdateImage
Left = 6
Height = 20
Top = 1065
Top = 1090
Width = 344
Anchors = [akLeft, akRight, akBottom]
BevelStyle = bsRaised
Expand All @@ -66,7 +66,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
Left = 184
Height = 30
Hint = 'Clear all drawing from the image'
Top = 1085
Top = 1110
Width = 166
Anchors = [akTop, akLeft, akRight]
AutoSize = True
Expand All @@ -86,7 +86,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
Left = 6
Height = 30
Hint = 'Updates the image from the client'
Top = 1085
Top = 1110
Width = 167
Anchors = [akLeft, akRight, akBottom]
AutoSize = True
Expand All @@ -105,7 +105,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideRight.Side = asrBottom
Left = 185
Height = 30
Top = 1035
Top = 1060
Width = 165
Anchors = [akTop, akLeft, akRight]
AutoSize = True
Expand All @@ -122,7 +122,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = Divider3
Left = 6
Height = 30
Top = 1035
Top = 1060
Width = 166
Anchors = [akLeft, akRight, akBottom]
AutoSize = True
Expand Down Expand Up @@ -151,7 +151,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = ButtonDeletePoints
Left = 6
Height = 730
Height = 755
Top = 6
Width = 344
Anchors = [akTop, akLeft, akRight, akBottom]
Expand All @@ -171,7 +171,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = Divider1
Left = 6
Height = 172
Top = 843
Top = 868
Width = 350
Anchors = [akLeft, akRight, akBottom]
AutoSize = True
Expand Down Expand Up @@ -301,7 +301,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Left = 6
BorderSpacing.Top = 6
OnChange = PointEditChanged
OnEditingDone = PointEditChanged
TabOrder = 3
end
object LabelSize: TLabel
Expand Down Expand Up @@ -365,7 +365,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = FindDTMButton
Left = 6
Height = 20
Top = 1015
Top = 1040
Width = 344
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.Left = 6
Expand All @@ -379,7 +379,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = PanelSelectedPoint
Left = 6
Height = 20
Top = 817
Top = 842
Width = 344
Caption = 'Selected Point'
Anchors = [akLeft, akRight, akBottom]
Expand All @@ -397,7 +397,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = ButtonDeletePoint
Left = 6
Height = 30
Top = 742
Top = 767
Width = 344
Anchors = [akLeft, akRight, akBottom]
AutoSize = True
Expand All @@ -416,7 +416,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Side = asrBottom
Left = 178
Height = 30
Top = 1035
Top = 1060
Width = 1
Anchors = [akTop, akLeft, akBottom]
BevelOuter = bvNone
Expand All @@ -431,7 +431,7 @@ object SimbaDTMEditorForm: TSimbaDTMEditorForm
AnchorSideBottom.Control = Divider2
Left = 6
Height = 30
Top = 778
Top = 803
Width = 344
Anchors = [akLeft, akRight, akBottom]
AutoSize = True
Expand Down
77 changes: 26 additions & 51 deletions Source/forms/simba.dtmeditor.pas
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ TSimbaDTMEditorForm = class(TForm)

procedure DoPaintArea(Sender: TObject; Bitmap: TSimbaImageBoxBitmap; R: TRect);

procedure AddPoint(X, Y, Col, Tol, Size: Integer); overload;
procedure AddPoint(X, Y, Col: Integer; Tol: Single; Size: Integer); overload;
procedure AddPoint(X, Y, Col: Integer); overload;
procedure EditPoint(Index: Integer; X, Y, Col, Tol, Size: Integer);
procedure EditPoint(Index: Integer; X, Y, Col: Integer; Tol: Single; Size: Integer);
procedure OffsetPoint(Index: Integer; X, Y: Integer);

function GetPointAt(X, Y: Integer): Integer;
Expand All @@ -124,7 +124,7 @@ implementation

uses
math,
simba.windowhandle, simba.colormath, simba.colormath_distance;
simba.windowhandle, simba.colormath;

procedure TSimbaDTMEditorForm.ClientImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
Expand Down Expand Up @@ -286,28 +286,28 @@ procedure TSimbaDTMEditorForm.DoPaintArea(Sender: TObject; Bitmap: TSimbaImageBo
end;
end;

procedure TSimbaDTMEditorForm.AddPoint(X, Y, Col, Tol, Size: Integer);
procedure TSimbaDTMEditorForm.AddPoint(X, Y, Col: Integer; Tol: Single; Size: Integer);
begin
ListBox.ItemIndex := ListBox.Items.Add('%d, %d, %d, %d, %d', [X, Y, Col, Tol, Size]);
ListBox.ItemIndex := ListBox.Items.Add('%d, %d, %d, %f, %d', [X, Y, Col, Tol, Size]);
DrawDTM();
end;

procedure TSimbaDTMEditorForm.AddPoint(X, Y, Col: Integer);
begin
ListBox.ItemIndex := ListBox.Items.Add('%d, %d, %d, 0, 1', [X, Y, Col]);
ListBox.ItemIndex := ListBox.Items.Add('%d, %d, %d, 0, 0', [X, Y, Col]);
DrawDTM();
end;

procedure TSimbaDTMEditorForm.EditPoint(Index: Integer; X, Y, Col, Tol, Size: Integer);
procedure TSimbaDTMEditorForm.EditPoint(Index: Integer; X, Y, Col: Integer; Tol: Single; Size: Integer);
begin
ListBox.Items[Index] := Format('%d, %d, %d, %d, %d', [X, Y, Col, Tol, Size]);
ListBox.Items[Index] := Format('%d, %d, %d, %f, %d', [X, Y, Col, Tol, Size]);

if ListBox.ItemIndex = Index then
begin
EditPointX.Text := IntToStr(X);
EditPointY.Text := IntToStr(Y);
EditPointColor.Text := IntToStr(Col);
EditPointTolerance.Text := IntToStr(Tol);
EditPointTolerance.Text := Format('%f', [Tol]);
EditPointSize.Text := IntToStr(Size);
end;

Expand All @@ -318,8 +318,9 @@ procedure TSimbaDTMEditorForm.OffsetPoint(Index: Integer; X, Y: Integer);
var
Point: TDTMPoint;
begin
Point := GetPoint(Index); EditPoint(Index, Point.X + X, Point.Y + Y, Point.Color, Point.Tolerance, Point.AreaSize);
Point := GetPoint(Index); EditPoint(Index, Point.X + X, Point.Y + Y, Point.Color, Point.Tolerance, Point.AreaSize);
Point := GetPoint(Index);

EditPoint(Index, Point.X + X, Point.Y + Y, Point.Color, Point.Tolerance, Point.AreaSize);
end;

function TSimbaDTMEditorForm.GetPoint(Index: Integer): TDTMPoint;
Expand All @@ -328,12 +329,11 @@ function TSimbaDTMEditorForm.GetPoint(Index: Integer): TDTMPoint;
begin
Items := ListBox.Items[Index].Split(', ');

Result.x := StrToInt(Items[0]);
Result.y := StrToInt(Items[1]);
Result.X := StrToInt(Items[0]);
Result.Y := StrToInt(Items[1]);
Result.Color := StrToInt(Items[2]);
Result.Tolerance := StrToInt(Items[3]);
Result.Tolerance := StrToFloat(Items[3]);
Result.AreaSize := StrToInt(Items[4]);
// Result.bp := False;
end;

function TSimbaDTMEditorForm.GetPoints: TDTMPointArray;
Expand All @@ -346,15 +346,8 @@ function TSimbaDTMEditorForm.GetPoints: TDTMPointArray;
end;

function TSimbaDTMEditorForm.GetDTM: TDTM;
var
Points: TDTMPointArray;
i: Integer;
begin
Points := GetPoints();

Result := TDTM.Create();
for i := 0 to High(Points) do
Result.AddPoint(Points[i]);
Result.Points := GetPoints();
end;

procedure TSimbaDTMEditorForm.LoadDTMClick(Sender: TObject);
Expand All @@ -375,18 +368,14 @@ procedure TSimbaDTMEditorForm.LoadDTMClick(Sender: TObject);
Value := Copy(Value, 1, Pos(#39, Value) - 1);
end;

DTM := TDTM.Create();

try
DTM.LoadFromString(Value);
DTM.FromString(Value);
for I := 0 to DTM.PointCount - 1 do
with DTM.Points[I] do
AddPoint(X, Y, Color, Tolerance, AreaSize);
except
ShowMessage('Invalid DTM String: ' + Value);
end;

DTM.Free();
end;
end;

Expand All @@ -397,35 +386,20 @@ procedure TSimbaDTMEditorForm.PointFlash(Sender: TObject);
end;

procedure TSimbaDTMEditorForm.FindDTMClick(Sender: TObject);
var
DTM: TDTM;
begin
ListBox.ClearSelection();

DTM := GetDTM();
try
FDebugColor := [];
FDebugDTM := FImageBox.FindDTM(DTM);
FDebugColor := [];
FDebugDTM := FImageBox.FindDTM(GetDTM());

FImageBox.Paint();
finally
DTM.Free();
end;
FImageBox.Paint();
end;

procedure TSimbaDTMEditorForm.ButtonPrintDTMClick(Sender: TObject);
var
DTM: TDTM;
begin
DTM := GetDTM();

try
FDTMString := DTM.SaveToString();
FDTMString := GetDTM().ToString();

SimbaDebugLn([EDebugLn.FOCUS], 'DTM := TDTM.CreateFromString(' + #39 + FDTMString + #39 + ');');
finally
DTM.Free();
end;
SimbaDebugLn([EDebugLn.FOCUS], 'DTM := TDTM.CreateFromString(' + #39 + FDTMString + #39 + ');');
end;

procedure TSimbaDTMEditorForm.ListBoxSelectionChange(Sender: TObject; User: boolean);
Expand All @@ -444,7 +418,7 @@ procedure TSimbaDTMEditorForm.ListBoxSelectionChange(Sender: TObject; User: bool
EditPointX.Text := IntToStr(X);
EditPointY.Text := IntToStr(Y);
EditPointColor.Text := IntToStr(Color);
EditPointTolerance.Text := IntToStr(Tolerance);
EditPointTolerance.Text := Format('%f', [Tolerance]);
EditPointSize.Text := IntToStr(AreaSize);
end;
end else
Expand Down Expand Up @@ -551,7 +525,8 @@ procedure TSimbaDTMEditorForm.ButtonDeletePointsClick(Sender: TObject);

procedure TSimbaDTMEditorForm.PointEditChanged(Sender: TObject);
var
X, Y, Col, Tol, Size: Integer;
X, Y, Col, Size: Integer;
Tol: Single;
Point: TDTMPoint;
begin
if (ListBox.ItemIndex > -1) and (TEdit(Sender).Text <> '') then
Expand All @@ -561,7 +536,7 @@ procedure TSimbaDTMEditorForm.PointEditChanged(Sender: TObject);
X := StrToIntDef(EditPointX.Text, Point.X);
Y := StrToIntDef(EditPointY.Text, Point.Y);
Col := StrToIntDef(EditPointColor.Text, Point.Color);
Tol := StrToIntDef(EditPointTolerance.Text, Point.Tolerance);
Tol := StrToFloatDef(EditPointTolerance.Text, Point.Tolerance);
Size := StrToIntDef(EditPointSize.Text, Point.AreaSize);

EditPoint(ListBox.ItemIndex, X, Y, Col, Tol, Size);
Expand Down
5 changes: 5 additions & 0 deletions Source/generics.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ generic procedure MoveElement<T>(var Arr: array of T; AFrom, ATo: Integer);
var
Item: T;
begin
if (Length(Arr) = 0) then
SimbaException('MoveElement: Empty array');
AFrom := EnsureRange(AFrom, Low(Arr), High(Arr));
ATo := EnsureRange(ATo, Low(Arr), High(Arr));

Item := Arr[AFrom];
if (ATo > AFrom) then
Move(Arr[AFrom + 1], Arr[AFrom], (ATo - AFrom) * SizeOf(T))
Expand Down
Loading

0 comments on commit 36a4e57

Please sign in to comment.