agpl_1.0.0_b5da3320/src/agpl-cv.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
 

with Ada.Numerics.Elementary_Functions;

package body Agpl.Cv is

   -------------------
   -- Inner_Product --
   -------------------

   function Inner_Product (L, R : Float_Array_3) return Float is
   begin
      return L (1) * R (1) + L (2) * R (2) + L (3) * R (3);
   end Inner_Product;

   -------------------
   -- Cross_Product --
   -------------------

   function Cross_Product (L, R : Float_Array_3) return Float_Array_3 is
   begin
      return
        (L (2) * R (3) - L (3) * R (2),
         L (3) * R (1) - L (1) * R (3),
         L (1) * R (2) - L (2) * R (1));
   end Cross_Product;

   --------------
   -- Distance --
   --------------

   function Distance (L, R : Point2D) return Float is
      use Ada.Numerics.Elementary_Functions;
   begin
      return Sqrt ((L (1) - R (1)) * (L (1) - R (1)) +
                   (L (2) - R (2)) * (L (2) - R (2)));
   end Distance;

   function Distance (Line : Line2D; Point : Point2D) return Float is
      use Ada.Numerics.Elementary_Functions;
      P : constant Point2D := Normalize (Point);
   begin
      return
        abs (Line (1) * P (1) + Line (2) * P (2) + Line (3)) /
        sqrt (Line (1) * Line (1) + Line (2) * Line (2));
   end Distance;

   ---------------------
   -- Signed_Distance --
   ---------------------

   function Signed_Distance (Line : Line2D; Point : Point2D) return Float is
      use Ada.Numerics.Elementary_Functions;
      P : constant Point2D := Normalize (Point);
   begin
      return
        (Line (1) * P (1) + Line (2) * P (2) + Line (3)) /
        sqrt (Line (1) * Line (1) + Line (2) * Line (2));
   end Signed_Distance;

   ---------------
   -- Normalize --
   ---------------

   function Normalize (Point : Point2D) return Point2D is
   begin
      if Point (3) = 0.0 or else Point (3) = 1.0 then
         return Point;
      else
         return (Point (1) / Point (3), Point (2) / Point (3), 1.0);
      end if;
   end Normalize;

end Agpl.Cv;