fVertextReduction.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. unit fVertextReduction;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Lion Example
  23. *
  24. * The Initial Developer(s) of the Original Code is:
  25. * Christian-W. Budde <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. *
  31. * ***** END LICENSE BLOCK ***** *)
  32. interface
  33. {$I GR32.inc}
  34. uses
  35. {$IFDEF FPC} LCLIntf, {$ENDIF}
  36. System.Classes,
  37. Vcl.ComCtrls,
  38. Vcl.Controls,
  39. Vcl.Forms,
  40. GR32,
  41. GR32_Image,
  42. GR32_Polygons,
  43. GR32_Paths;
  44. type
  45. TFrmLineSimplification = class(TForm)
  46. PaintBox32: TPaintBox32;
  47. procedure FormCreate(Sender: TObject);
  48. procedure FormDestroy(Sender: TObject);
  49. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  50. procedure PaintBox32PaintBuffer(Sender: TObject);
  51. procedure PaintBox32MouseDown(Sender: TObject; Button: TMouseButton;
  52. Shift: TShiftState; X, Y: Integer);
  53. procedure PaintBox32MouseUp(Sender: TObject; Button: TMouseButton;
  54. Shift: TShiftState; X, Y: Integer);
  55. procedure PaintBox32MouseMove(Sender: TObject; Shift: TShiftState;
  56. X, Y: Integer);
  57. private
  58. FPoints: TArrayOfFloatPoint;
  59. FLastEpsilon: TFloat;
  60. FRenderer: TPolygonRenderer32VPR;
  61. end;
  62. var
  63. FrmLineSimplification: TFrmLineSimplification;
  64. implementation
  65. {$IFDEF FPC}
  66. {$R *.lfm}
  67. {$ELSE}
  68. {$R *.dfm}
  69. {$ENDIF}
  70. uses
  71. GR32_VectorUtils;
  72. { TFrmLineSimplification }
  73. procedure TFrmLineSimplification.FormCreate(Sender: TObject);
  74. begin
  75. FRenderer := TPolygonRenderer32VPR.Create(PaintBox32.Buffer);
  76. FRenderer.Color := clBlack32;
  77. end;
  78. procedure TFrmLineSimplification.FormDestroy(Sender: TObject);
  79. begin
  80. FRenderer.Free;
  81. end;
  82. procedure TFrmLineSimplification.FormKeyDown(Sender: TObject; var Key: Word;
  83. Shift: TShiftState);
  84. begin
  85. case Key of
  86. 13:
  87. begin
  88. FLastEpsilon := 2 * FLastEpsilon;
  89. if Length(FPoints) > 0 then
  90. FPoints := VertexReduction(FPoints, FLastEpsilon);
  91. PaintBox32.Invalidate;
  92. end;
  93. 27:
  94. Close;
  95. end;
  96. end;
  97. procedure TFrmLineSimplification.PaintBox32MouseDown(Sender: TObject;
  98. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  99. begin
  100. SetLength(FPoints, 1);
  101. FPoints[0] := FloatPoint(X, Y);
  102. PaintBox32.OnMouseMove := PaintBox32MouseMove;
  103. end;
  104. procedure TFrmLineSimplification.PaintBox32MouseMove(Sender: TObject;
  105. Shift: TShiftState; X, Y: Integer);
  106. var
  107. Index: Integer;
  108. begin
  109. Index := Length(FPoints) - 1;
  110. if (FPoints[Index].X <> X) and (FPoints[Index].Y <> Y) then
  111. begin
  112. Index := Length(FPoints);
  113. SetLength(FPoints, Index + 1);
  114. FPoints[Index] := FloatPoint(X, Y);
  115. PaintBox32.Invalidate;
  116. end;
  117. end;
  118. procedure TFrmLineSimplification.PaintBox32MouseUp(Sender: TObject;
  119. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  120. var
  121. Index: Integer;
  122. begin
  123. Index := Length(FPoints) - 1;
  124. if (FPoints[Index].X <> X) and (FPoints[Index].Y <> Y) then
  125. begin
  126. Index := Length(FPoints);
  127. SetLength(FPoints, Index + 1);
  128. FPoints[Index] := FloatPoint(X, Y);
  129. end;
  130. FLastEpsilon := 1;
  131. if ssShift in Shift then
  132. FLastEpsilon := 5
  133. else if ssCtrl in Shift then
  134. FLastEpsilon := 0.5;
  135. FPoints := VertexReduction(FPoints, FLastEpsilon);
  136. PaintBox32.Invalidate;
  137. PaintBox32.OnMouseMove := nil;
  138. end;
  139. procedure TFrmLineSimplification.PaintBox32PaintBuffer(Sender: TObject);
  140. var
  141. Index: Integer;
  142. begin
  143. with PaintBox32.Buffer do
  144. begin
  145. Clear($FFFFFFFF);
  146. FRenderer.PolygonFS(BuildPolyline(FPoints, 2));
  147. for Index := 0 to High(FPoints) do
  148. with FPoints[Index] do
  149. FillRectS(Round(X - 4), Round(Y - 4), Round(X + 4), Round(Y + 4),
  150. clBlack32);
  151. end;
  152. end;
  153. end.