Debian Codegraph @

deps/gnatcoll-slim/testsuite/tests/geometry/test.adb

raw
1 ------------------------------------------------------------------------------
2 -- G N A T C O L L --
3 -- --
4 -- Copyright (C) 2010-2018, AdaCore --
5 -- --
6 -- This library is free software; you can redistribute it and/or modify it --
7 -- under terms of the GNU General Public License as published by the Free --
8 -- Software Foundation; either version 3, or (at your option) any later --
9 -- version. This library is distributed in the hope that it will be useful, --
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11 -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
12 -- --
13 -- As a special exception under Section 7 of GPL version 3, you are granted --
14 -- additional permissions described in the GCC Runtime Library Exception, --
15 -- version 3.1, as published by the Free Software Foundation. --
16 -- --
17 -- You should have received a copy of the GNU General Public License and --
18 -- a copy of the GCC Runtime Library Exception along with this program; --
19 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
20 -- <http://www.gnu.org/licenses/>. --
21 -- --
22 ------------------------------------------------------------------------------
23
24 with Ada.Text_IO;
25 with GNATCOLL.Geometry;
26 with Test_Assert;
27
28 function Test return Integer is
29
30 package IO renames Ada.Text_IO;
31 package A renames Test_Assert;
32
33 generic
34 type Coordinate is digits <>;
35 with package G is new GNATCOLL.Geometry (Coordinate);
36 package Asserts is
37 use type G.Point;
38
39 Tolerance : constant G.Distance_Type := 1.0E-5;
40 -- Tolerance when comparing distances
41
42 procedure Put (P : G.Point);
43 -- Put image of a point on stdout
44
45 procedure Put (C : G.Circle);
46 -- Put image of a circle on stdout
47
48 procedure Assert (P1, P2 : G.Point; Error : String);
49 -- Check if two points are equals
50
51 procedure Assert_Inside
52 (P : G.Point;
53 S : G.Segment;
54 On_Line : Boolean := True;
55 On_Segment : Boolean := True;
56 Error : String := "");
57
58 procedure Assert_Inside_Triangle
59 (P : G.Point;
60 T : G.Triangle;
61 Result : Boolean := True;
62 Error : String := "");
63
64 procedure Assert_Intersection
65 (S1, S2 : G.Segment;
66 On_Line, On_Segment : G.Point;
67 Error : String);
68
69 procedure Assert_Intersect_Triangle
70 (T1, T2 : G.Triangle; Result : Boolean; Error : String);
71 procedure Assert_Intersect_Rectangle
72 (R1, R2 : G.Rectangle; Result : Boolean; Error : String);
73 procedure Assert_Same_Side
74 (P1, P2 : G.Point; S : G.Segment; Result : Boolean; Error : String);
75 procedure Assert (D1, D2 : Coordinate'Base; Error : String);
76 procedure Assert_Distance
77 (P : G.Point; S : G.Segment; On_Line, On_Segment : Coordinate;
78 Error : String);
79 procedure Assert_Area
80 (P : G.Polygon; D : Coordinate; Error : String);
81 procedure Assert
82 (C1, C2 : G.Circle; Error : String);
83 end Asserts;
84
85 package body Asserts is
86
87 ---------
88 -- Put --
89 ---------
90
91 procedure Put (P : G.Point) is
92 begin
93 if P = G.No_Point then
94 IO.Put ("(No point)");
95 elsif P = G.Infinity_Points then
96 IO.Put ("(Infinity of points)");
97 else
98 IO.Put ("(" & P.X'Img & "," & P.Y'Img & ")");
99 end if;
100 end Put;
101
102 ---------
103 -- Put --
104 ---------
105
106 procedure Put (C : G.Circle) is
107 begin
108 IO.Put ("(c="); Put (C.Center);
109 IO.Put (" r=" & C.Radius'Img);
110 end Put;
111
112 ------------
113 -- Assert --
114 ------------
115
116 procedure Assert (P1, P2 : G.Point; Error : String) is
117 Success : constant Boolean := P1 = P2;
118 begin
119 A.Assert (Success, Error);
120 if not Success then
121 IO.Put ("expected: "); Put (P1); IO.New_Line;
122 IO.Put ("got: "); Put (P2); IO.New_Line;
123 end if;
124 end Assert;
125
126 procedure Assert_Inside
127 (P : G.Point;
128 S : G.Segment;
129 On_Line : Boolean := True;
130 On_Segment : Boolean := True;
131 Error : String := "")
132 is
133 SR : constant G.Segment := (S (2), S (1));
134 begin
135 A.Assert (G.Inside (P, G.To_Line (S)) = On_Line, Error & " (line test)");
136 A.Assert (G.Inside (P, G.To_Line (SR)) = On_Line, Error & " (reverse line test)");
137 A.Assert (G.Inside (P, S) = On_Segment,
138 Error & " (segment test)");
139 A.Assert (G.Inside (P, SR) = On_Segment,
140 Error & " (reverse segment test)");
141 end Assert_Inside;
142
143 procedure Assert_Inside_Triangle
144 (P : G.Point; T : G.Triangle;
145 Result : Boolean := True;
146 Error : String := "")
147 is
148 T2 : constant G.Triangle := (T (3), T (2), T (1));
149 begin
150 A.Assert (G.Inside (P, T) = Result, Error & " (triangle test)");
151 A.Assert (G.Inside (P, T2) = Result, Error & " (reverse triangle test)");
152 A.Assert (G.Inside (P, G.Polygon (T)) = Result, Error & " (polygon test)");
153 A.Assert (G.Inside (P, G.Polygon (T2)) = Result, Error & " (reverse polygon test)");
154 end Assert_Inside_Triangle;
155
156 procedure Assert_Intersection
157 (S1, S2 : G.Segment;
158 On_Line : G.Point;
159 On_Segment : G.Point;
160 Error : String)
161 is
162 begin
163 A.Assert (G.Intersection (G.To_Line (S1), G.To_Line (S2)) = On_Line,
164 Error & " (line test)");
165 A.Assert (G.Intersection (G.To_Line (S2), G.To_Line (S1)) = On_Line,
166 Error & " (reverse line test)");
167 A.Assert (G.Intersection (S1, S2) = On_Segment,
168 Error & " (segment test)");
169 A.Assert (G.Intersection (S2, S1) = On_Segment,
170 Error & " (reverse segment test)");
171 end Assert_Intersection;
172
173 procedure Assert_Intersect_Triangle
174 (T1, T2 : G.Triangle; Result : Boolean; Error : String)
175 is
176 T3 : constant G.Triangle := (T1 (2), T1 (3), T1 (1));
177 begin
178 A.Assert (G.Intersect (T1, T2) = Result, Error & " (test1)");
179 A.Assert (G.Intersect (T2, T1) = Result, Error & " (test2)");
180 A.Assert (G.Intersect (T3, T2) = Result, Error & " (test3)");
181 A.Assert (G.Intersect (T2, T3) = Result, Error & " (test4)");
182 end Assert_Intersect_Triangle;
183
184 procedure Assert_Intersect_Rectangle
185 (R1, R2 : G.Rectangle; Result : Boolean; Error : String)
186 is
187 begin
188 A.Assert (G.Intersect (R1, R2) = Result, Error);
189 end Assert_Intersect_Rectangle;
190
191 procedure Assert_Same_Side
192 (P1, P2 : G.Point; S : G.Segment; Result : Boolean; Error : String) is
193 begin
194 A.Assert (G.Same_Side (P1, P2, S) = Result, Error & " (segment test)");
195 A.Assert (G.Same_Side (P1, P2, G.To_Line (S)) = Result, Error & " (line test)");
196 end Assert_Same_Side;
197
198 procedure Assert (D1, D2 : Coordinate'Base; Error : String) is
199 Success : constant Boolean := abs (D1 - D2) <= Tolerance;
200 begin
201 A.Assert (Success, Error);
202 if not Success then
203 IO.Put_Line ("expected: " & D1'Img);
204 IO.Put_Line ("got: " & D2'Img);
205 end if;
206 end Assert;
207
208 procedure Assert_Distance
209 (P : G.Point;
210 S : G.Segment;
211 On_Line, On_Segment : Coordinate;
212 Error : String)
213 is
214 SR : constant G.Segment := (S (2), S (1));
215 begin
216 A.Assert (G.Distance (P, G.To_Line (S)) = On_Line, Error & " (line test)");
217 A.Assert (G.Distance (P, G.To_Line (SR)) = On_Line,
218 Error & " (reverse line test)");
219 A.Assert (G.Distance (P, S) = On_Segment,
220 Error & " (segment test)");
221 A.Assert (G.Distance (P, SR) = On_Segment,
222 Error & " (reverse segment test)");
223 end Assert_Distance;
224
225 procedure Assert_Area
226 (P : G.Polygon; D : Coordinate; Error : String) is
227 begin
228 Assert (G.Area (P), abs (D), Error & " (poly test)");
229 if P'Length = 3 then
230 Assert (G.Area (G.Triangle (P)), D,
231 Error & " (triangle test)");
232 end if;
233 end Assert_Area;
234
235 procedure Assert (C1, C2 : G.Circle; Error : String) is
236 use type G.Circle;
237 Success : constant Boolean := C1 = C2;
238 begin
239 A.Assert (Success, Error);
240 if not Success then
241 IO.Put ("expected: "); Put (C1); IO.New_Line;
242 IO.Put ("got: "); Put (C2); IO.New_Line;
243 end if;
244 end Assert;
245 end Asserts;
246
247 generic
248 type Coordinate is digits <>;
249 procedure Tests;
250
251 procedure Tests is
252 package G is new GNATCOLL.Geometry (Coordinate);
253 package Assertions is new Asserts (Coordinate, G);
254 use Assertions, G, G.Coordinate_Elementary_Functions;
255 begin
256 Assert_Inside ((2.0, 0.0), ((0.0, 0.0), (10.0, 0.0)), True, True,
257 "Point on vertical line");
258 Assert_Inside ((-1.0, 0.0), ((0.0, 0.0), (10.0, 0.0)), True, False,
259 "Point on vertical line, but not on segment");
260 Assert_Inside ((0.0, 0.0), ((0.0, 0.0), (10.0, 0.0)), True, True,
261 "Point on vertical line at one end");
262 Assert_Inside ((0.0, 2.0), ((0.0, 0.0), (0.0, 10.0)), True, True,
263 "Point on horizontal line");
264 Assert_Inside ((0.0, -1.0), ((0.0, 0.0), (0.0, 10.0)), True, False,
265 "Point on horizontal line, but not on segment");
266 Assert_Inside ((0.0, 0.0), ((0.0, 0.0), (0.0, 10.0)), True, True,
267 "Point on horizontal line at one end");
268 Assert_Inside ((0.0, 0.0), ((-2.0, 2.0), (2.0, 2.0)), False, False,
269 "Not on line nor segment");
270 Assert_Intersection
271 (((0.0, 0.0), (10.0, 0.0)), ((0.0, 0.0), (10.0, 0.0)),
272 Infinity_Points, Infinity_Points,
273 "Intersection of same horizontal line is infinite");
274 Assert_Intersection
275 (((0.0, 0.0), (10.0, 0.0)), ((0.0, 0.0), (5.0, 0.0)),
276 Infinity_Points, Infinity_Points,
277 "Intersection of overlapping horizontal line is infinite");
278 Assert_Intersection
279 (((0.0, 0.0), (0.0, 10.0)), ((0.0, 0.0), (0.0, 10.0)),
280 Infinity_Points, Infinity_Points,
281 "Intersection of same vertical line is infinite");
282 Assert_Intersection
283 (((0.0, 0.0), (0.0, 10.0)), ((0.0, 0.0), (0.0, 5.0)),
284 Infinity_Points, Infinity_Points,
285 "Intersection of overlapping vertical vectors is infinite");
286 Assert_Intersection
287 (((0.0, 0.0), (10.0, 0.0)), ((-2.0, 2.0), (2.0, 2.0)),
288 No_Point, No_Point,
289 "No intersection of parallel vectors");
290 Assert_Intersection
291 (((0.0, 0.0), (10.0, 0.0)), ((11.0, 0.0), (20.0, 0.0)),
292 Infinity_Points, No_Point,
293 "Intersection of aligned vectors");
294 Assert_Intersection
295 (((0.0, 0.0), (0.0, 10.0)), ((-2.0, 2.0), (2.0, 2.0)),
296 Point'(0.0, 2.0), Point'(0.0, 2.0),
297 "Simple segment intersection");
298 Assert_Intersection
299 (((0.0, 0.0), (0.0, 10.0)), ((-2.0, 0.0), (2.0, 0.0)),
300 Point'(0.0, 0.0), Point'(0.0, 0.0),
301 "Intersection at one end of the vectors");
302 Assert_Intersection
303 (((0.0, 0.0), (10.0, 0.0)), ((-2.0, 2.0), (2.0, 2.0)),
304 No_Point, No_Point,
305 "Parallel lines have no intersection");
306 Assert_Distance
307 ((0.0, 0.0), ((-2.0, 2.0), (2.0, 2.0)), 2.0, 2.0,
308 "Simple distance to line");
309 Assert_Distance
310 ((0.0, 2.0), ((-2.0, 2.0), (2.0, 2.0)), 0.0, 0.0,
311 "Distance when point on line");
312 Assert_Distance
313 ((-3.0, 2.0), ((-2.0, 2.0), (2.0, 2.0)), 0.0, 1.0,
314 "Distance when point on line, not on segment");
315 Assert_Distance
316 ((-3.0, 0.0), ((-2.0, 2.0), (2.0, 2.0)), 2.0, Sqrt (5.0),
317 "Distance when orthogonal projection not on segment");
318 Assert_Area
319 (((0.0, 0.0), (4.0, 0.0), (0.0, 3.0)), 6.0,
320 "Area of a counter-clockwise triangle");
321 Assert_Area
322 (((0.0, 0.0), (0.0, 3.0), (4.0, 0.0)), 6.0,
323 "Area of clockwise triangle");
324 Assert_Area
325 (((0.0, 0.0), (0.0, 3.0), (2.0, 3.0), (2.0, 4.0),
326 (3.0, 4.0), (3.0, 0.0)), 10.0,
327 "Area for two squares (non-convex)");
328 Assert
329 (To_Circle ((0.0, 0.0), (1.0, 0.0), (0.0, 1.0)),
330 ((0.5, 0.5), Sqrt (0.5)),
331 "Circle from point");
332 Assert_Inside_Triangle
333 ((0.0, 0.0), ((0.0, 0.0), (0.0, 1.0), (1.0, 0.0)),
334 True, "Point on boundary of triangle");
335 Assert_Inside_Triangle
336 ((0.1, 0.1), ((0.0, 0.0), (0.0, 1.0), (1.0, 0.0)),
337 True, "Point inside triangle");
338 Assert_Inside_Triangle
339 ((0.5, 1.0), ((0.0, 0.0), (0.0, 1.0), (1.0, 0.0)),
340 False, "Point outside triangle");
341 A.Assert
342 (G.Inside ((2.5, 3.5),
343 Poly => ((0.0, 0.0), (0.0, 3.0), (2.0, 3.0), (2.0, 4.0),
344 (3.0, 4.0), (3.0, 0.0))),
345 "Point inside non-convex polygon");
346 A.Assert
347 (G.Inside ((2.0, 2.5),
348 Poly => ((0.0, 0.0), (0.0, 3.0), (2.0, 3.0), (2.0, 4.0),
349 (3.0, 4.0), (3.0, 0.0))),
350 "Point inside non-convex polygon, vertically below vertex");
351 Assert_Same_Side
352 ((0.0, 0.0), (4.0, 0.0), ((-2.0, 2.0), (2.0, 2.0)), True,
353 "Same side of horizontal line");
354 Assert_Same_Side
355 ((0.0, 0.0), (0.0, 4.0), ((-2.0, 2.0), (-2.0, -2.0)), True,
356 "Same side of vertical line");
357 Assert_Same_Side
358 ((0.0, 0.0), (4.0, 0.0), ((2.0, 2.0), (2.0, -2.0)), False,
359 "Not same side of vertical line");
360 Assert
361 (Centroid (((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0))),
362 (0.5, 0.5),
363 "Centroid of rectangle");
364 Assert
365 (Centroid (((0.0, 0.0), (1.0, 0.0), (0.0, 1.0))),
366 (1.0 / 3.0, 1.0 / 3.0),
367 "Centroid of triangle");
368 Assert_Intersect_Triangle
369 (((0.0, 0.0), (1.0, 0.0), (0.0, 1.0)),
370 ((0.5, 0.0), (1.5, 0.0), (0.5, 1.0)),
371 True, "Intersection of two triangles");
372 Assert_Intersect_Triangle
373 (((0.0, 0.0), (1.0, 0.0), (0.0, 1.0)),
374 ((1.0, 0.0), (2.0, 0.0), (1.0, 1.0)),
375 True, "Intersection with one common vertex");
376 Assert_Intersect_Triangle
377 (((0.0, 0.0), (1.0, 0.0), (0.0, 1.0)),
378 ((1.1, 0.0), (2.1, 0.0), (1.1, 1.0)),
379 False, "No Intersection");
380 Assert_Intersect_Rectangle
381 (((0.0, 0.0), (1.0, 1.0)),
382 ((0.5, 0.0), (1.5, 2.0)),
383 True, "Intersection of rectangles");
384 Assert_Intersect_Rectangle
385 (((0.0, 0.0), (1.0, 1.0)),
386 ((0.5, 1.0), (1.5, 2.0)),
387 True, "Intersection of rectangles with common edge");
388 Assert_Intersect_Rectangle
389 (((0.0, 0.0), (1.0, 1.0)),
390 ((0.5, 1.1), (1.5, 2.0)),
391 False, "No intersection of rectangles");
392
393 -- From the documentation of Boost geometry
394 -- http://geometrylibrary.geodan.nl/
395
396 declare
397 P1 : constant Point := (1.0, 1.0);
398 P2 : constant Point := (2.0, 3.0);
399 P3 : constant Point := (3.7, 2.0);
400 P : constant Polygon :=
401 ((2.0, 1.3), (4.1, 3.0), (5.3, 2.6), (2.9, 0.7));
402 begin
403 Assert (2.23607, Distance (P1, P2), "Distance P1-P2");
404 Assert (3.015, Area (P), "Area of polygon");
405 A.Assert (Inside (P3, P), "Point inside polygon");
406 Assert (1.04403, Distance (P1, P), "Distance point-polygon");
407 end;
408 end Tests;
409
410 begin
411 declare
412 subtype Coordinate is Float;
413 procedure Float_Tests is new Tests (Coordinate);
414 begin
415 Float_Tests;
416 end;
417
418 -- Check we can instantiate with more restricted types
419
420 declare
421 type Coordinate is digits 1 range -500.0 .. 500.0;
422 procedure Float_Tests is new Tests (Coordinate);
423 begin
424 Float_Tests;
425 end;
426
427 declare
428 type Coordinate is digits 1 range -500.0 .. -200.0;
429 package Geom is new GNATCOLL.Geometry (Coordinate);
430 package Assertions is new Asserts (Coordinate, Geom);
431 use Geom, Assertions;
432 P1 : constant Point := (-401.0, -401.0);
433 P2 : constant Point := (-402.0, -403.0);
434 begin
435 Assert (2.23607, Distance (P1, P2), "Distance P1-P2");
436 end;
437
438 return A.Report;
439 end Test;
440
441