agpl_1.0.0_b5da3320/src/agpl-gdk/agpl-gdk-drawer_figures.adb

  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
with Agpl.Cv;
with Agpl.Gdk.Drawer_Point;
with Agpl.Gdk.Drawer_Segment;
with Agpl.Transf2D;

with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;

package body Agpl.Gdk.Drawer_Figures is

   package Transform is new Transf2D (Float);

   function "+" (P : in Cv.Point2D) return Transform.Pose;
   pragma Inline ("+");
   function "+" (P : in Transform.Pose) return Cv.Point2D;
   pragma Inline ("+");

   function "+" (P : in Cv.Point2D) return Transform.Pose is
   begin
      return (P (P'First), P (P'First + 1), 0.0, 1.0);
   end "+";

   function "+" (P : in Transform.Pose) return Cv.Point2D is
      use Transform;
   begin
      return (P (X), P (Y), 1.0);
   end "+";

   ----------------
   -- Draw_Arrow --
   ----------------

   procedure Draw_Arrow (X1, Y1, X2, Y2 : in     Float;
                         Size           : in     Float;
                         Gc             : in     Gdk_GC;
                         Draw           : in out Drawer.Object)
   is
   begin
      Draw_Segment (X1, Y1, X2, Y2, Gc, Draw);

      --  Arrow
      declare
         H  : constant Float := Size;
         P1 : Cv.Point2D := (-H,   H, 1.0);
         P2 : Cv.Point2D := (-H, -H, 1.0);
         P5 : Cv.Point2D := (0.0, 0.0, 1.0);
         A  : constant Float := Arctan (Y2 - Y1, X2 - X1);
      begin
         --  Transform the points to reflect the true pose:
         declare
            use Transform;
            T : constant Transformation :=
                  Get_Translation (-X2, -Y2) * Get_Rotation (-A);
         begin
            P1 := +(T * (+P1));
            P2 := +(T * (+P2));
            P5 := +(T * (+P5));
         end;

         Draw_Segment (P1 (1), P1 (2), P5 (1), P5 (2), Gc, Draw);
         Draw_Segment (P2 (1), P2 (2), P5 (1), P5 (2), Gc, Draw);
      end;
   end Draw_Arrow;

   ----------------------
   -- Draw_Grid_Points --
   ----------------------

   procedure Draw_Grid_Points
     (X1, Y1, X2, Y2 : in     Float;
      Gc             : in     Gdk_GC;
      Draw           : in out Drawer.Object;
      Every_X        : in     Float := 1.0;
      Every_Y        : in     Float := 1.0)
   is
      Xmin : constant Float := Float'Min (X1, X2);
      Xmax : constant Float := Float'Max (X1, X2);
      Ymin : constant Float := Float'Min (Y1, Y2);
      Ymax : constant Float := Float'Max (Y1, Y2);

      C, R : Float;
   begin
      C := 0.0;
      while C <= Xmax loop
         R := 0.0;
         while R <= Ymax loop
            if C >= Xmin and then R >= Ymin then
               Draw.Draw (Drawer_Point.Create (Gc, C, R));
            end if;
            R := R + Every_Y;
         end loop;
         C := C + Every_X;
      end loop;

      C := 0.0;
      while C >= Xmin loop
         R := 0.0;
         while R >= Ymin loop
            if C <= Xmax and then R <= Ymax then
               Draw.Draw (Drawer_Point.Create (Gc, C, R));
            end if;
            R := R - Every_Y;
         end loop;
         C := C - Every_X;
      end loop;

      C := 0.0;
      while C <= Xmax loop
         R := 0.0;
         while R >= Ymin loop
            if C >= Xmin and then R <= Ymax then
               Draw.Draw (Drawer_Point.Create (Gc, C, R));
            end if;
            R := R - Every_Y;
         end loop;
         C := C + Every_X;
      end loop;

      C := 0.0;
      while C >= Xmin loop
         R := 0.0;
         while R <= Ymax loop
            if C <= Xmax and then R >= Ymin then
               Draw.Draw (Drawer_Point.Create (Gc, C, R));
            end if;
            R := R + Every_Y;
         end loop;
         C := C - Every_X;
      end loop;
   end Draw_Grid_Points;

   ---------------
   -- Draw_Plus --
   ---------------

   procedure Draw_Plus
     (X, Y : in     Float;
      Gc   : in     Gdk_GC;
      Draw : in out Drawer.Object;
      Size : in     Float := 1.0)
   is
      H    : constant Float := Size / 2.0;
   begin
      Draw.Draw (Drawer_Segment.Create (Gc, X,     Y - H, X,     Y + H));
      Draw.Draw (Drawer_Segment.Create (Gc, X - H, Y,     X + H, Y));
   end Draw_Plus;

   ----------------
   -- Draw_Robot --
   ----------------

   procedure Draw_Robot (X, Y, A : in     Float;
                         Gc      : in     Gdk_GC;
                         Draw    : in out Drawer.Object;
                         Size    : in     Float := 0.5)
   is
      --  Points for a robot looking along the X axis.
      H : constant Float := Size / 2.0;
      P1 : Cv.Point2D := (-H, H, 1.0);
      P2 : Cv.Point2D := (-H, -H, 1.0);
      P3 : Cv.Point2D := (H, H, 1.0);
      P4 : Cv.Point2D := (H, -H, 1.0);
      P5 : Cv.Point2D := (H, 0.0, 1.0);
   begin
      --  Transform the points to reflect the true pose:
      declare
         use Transform;
         T : constant Transformation :=
               Get_Translation (-X, -Y) * Get_Rotation (-A);
      begin
         P1 := +(T * (+P1));
         P2 := +(T * (+P2));
         P3 := +(T * (+P3));
         P4 := +(T * (+P4));
         P5 := +(T * (+P5));
      end;

      --  Arrow
      Draw_Segment (P1 (1), P1 (2), P5 (1), P5 (2), Gc, Draw);
      Draw_Segment (P2 (1), P2 (2), P5 (1), P5 (2), Gc, Draw);

      --  Base
      Draw_Segment (P1 (1), P1 (2), P2 (1), P2 (2), Gc, Draw);

      --  Box
      Draw_Segment (P1 (1), P1 (2), P3 (1), P3 (2), Gc, Draw);
      Draw_Segment (P2 (1), P2 (2), P4 (1), P4 (2), Gc, Draw);
      Draw_Segment (P3 (1), P3 (2), P4 (1), P4 (2), Gc, Draw);
   end Draw_Robot;

   ------------------
   -- Draw_Segment --
   ------------------

   procedure Draw_Segment (X1, Y1, X2, Y2 : in     Float;
                           Gc             : in     Gdk_GC;
                           Draw           : in out Drawer.Object)
   is
   begin
      Draw.Draw (Drawer_Segment.Create (Gc, X1, Y1, X2, Y2));
   end Draw_Segment;

   -----------------
   -- Draw_Vector --
   -----------------

   procedure Draw_Vector (X, Y : in Float;
                          A, L : in Float;
                          Gc   : in Gdk_GC;
                          Draw : in out Drawer.Object)
   is
      X2 : constant Float := X + L * Cos (A);
      Y2 : constant Float := Y + L * Sin (A);
   begin
      Draw_Segment (X, Y, X2, Y2, Gc, Draw);
   end Draw_Vector;

end Agpl.Gdk.Drawer_Figures;