C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
total.c
Go to the documentation of this file.
1 /**
2  * @file total.c
3  * @brief Implementation of the ToTal algorithm
4  * @date 15/02/2011
5  * The algorithm was implemented after \cite Pierlot2014ANewThree (published earlier in \cite Pierlot2011ANewThreeObject ). This algorithm was used develop in the context of the Eurobot contest.
6 
7  * @author Vincent Pierlot
8 */
9 #include <math.h>
10 #include "const.h"
11 #include "total.h"
12 
13 /*
14  * Version with mathematical approximation of the limit for the pseudosingularities
15 */
17  tfloat alpha1, tfloat alpha2, tfloat alpha3,
18  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
19 {
20  tfloat cot_12 = Cot( alpha2 - alpha1 ) ;
21  tfloat cot_23 = Cot( alpha3 - alpha2 ) ;
22  cot_12 = adjust_value_to_bounds( cot_12 , COT_MAX ) ;
23  cot_23 = adjust_value_to_bounds( cot_23 , COT_MAX ) ;
24  tfloat cot_31 = ( 1.0 - cot_12 * cot_23 ) / ( cot_12 + cot_23 ) ;
25  cot_31 = adjust_value_to_bounds( cot_31 , COT_MAX ) ;
26 
27  tfloat x1_ = x1 - x2 , y1_ = y1 - y2 , x3_ = x3 - x2 , y3_ = y3 - y2 ;
28 
29  tfloat c12x = x1_ + cot_12 * y1_ ;
30  tfloat c12y = y1_ - cot_12 * x1_ ;
31 
32  tfloat c23x = x3_ - cot_23 * y3_ ;
33  tfloat c23y = y3_ + cot_23 * x3_ ;
34 
35  tfloat c31x = (x3_ + x1_) + cot_31 * (y3_ - y1_) ;
36  tfloat c31y = (y3_ + y1_) - cot_31 * (x3_ - x1_) ;
37  tfloat k31 = (x3_ * x1_) + (y3_ * y1_) + cot_31 * ( (y3_ * x1_) - (x3_ * y1_) ) ;
38 
39  tfloat D = (c12x - c23x) * (c23y - c31y) - (c23x - c31x) * (c12y - c23y) ;
40  tfloat invD = 1.0 / D ;
41  tfloat K = k31 * invD ;
42 
43  *x = K * (c12y - c23y) + x2 ;
44  *y = K * (c23x - c12x) + y2 ;
45 
46  return invD ; /* return 1/D */
47 }
48 
49 /*
50  * Version with exact limits for the pseudosingularities (3 cases)
51 */
52 tfloat triangulationPierlot2(tfloat *x, tfloat *y,
53  tfloat alpha1, tfloat alpha2, tfloat alpha3,
54  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
55 {
56  tfloat alpha_12 = alpha2 - alpha1 ;
57  tfloat alpha_23 = alpha3 - alpha2 ;
58  tfloat alpha_31 = alpha1 - alpha3 ;
59 
60  tfloat x1_ , y1_ , x2_ , y2_ , x3_ , y3_ ;
61 
62  tfloat cot_12 , cot_23 , cot_31 , c12x , c12y , c23x , c23y , c31x , c31y , k , D , invD , K ;
63 
64  if ( alpha_12 == PI || alpha_12 == 0.0 )
65  {
66  x1_ = x1 - x3 , y1_ = y1 - y3 , x2_ = x2 - x3 , y2_ = y2 - y3 ;
67 
68  cot_23 = cot( alpha_23 ) ;
69 
70  c12x = (y1_ - y2_) ;
71  c12y = (x2_ - x1_) ;
72  k = (y1_ * x2_) - (x1_ * y2_) ;
73 
74  c23x = x2_ + cot_23 * y2_ ;
75  c23y = y2_ - cot_23 * x2_ ;
76 
77  c31x = x1_ + cot_23 * y1_ ;
78  c31y = y1_ - cot_23 * x1_ ;
79 
80  D = (c31x - c23x) * (c12y) + (c12x) * (c23y - c31y) ;
81  invD = 1.0 / D ;
82  K = k * invD ;
83 
84  *x = K * ( c23y - c31y ) + x3 ;
85  *y = K * ( c31x - c23x ) + y3 ;
86 
87  return invD ;
88  }
89 
90  if ( alpha_23 == PI || alpha_23 == 0.0 )
91  {
92  x2_ = x2 - x1 , y2_ = y2 - y1 , x3_ = x3 - x1 , y3_ = y3 - y1 ;
93 
94  cot_31 = cot( alpha_31 ) ;
95 
96  c12x = x2_ + cot_31 * y2_ ;
97  c12y = y2_ - cot_31 * x2_ ;
98 
99  c23x = (y2_ - y3_) ;
100  c23y = (x3_ - x2_) ;
101  k = (y2_ * x3_) - (x2_ * y3_) ;
102 
103  c31x = x3_ + cot_31 * y3_ ;
104  c31y = y3_ - cot_31 * x3_ ;
105 
106  D = (c12x - c31x) * (c23y) + (c23x) * (c31y - c12y) ;
107  invD = 1.0 / D ;
108  K = k * invD ;
109 
110  *x = K * ( c31y - c12y ) + x1 ;
111  *y = K * ( c12x - c31x ) + y1 ;
112 
113  return invD ;
114  }
115 
116  if ( alpha_31 == PI || alpha_31 == 0.0 )
117  {
118  x1_ = x1 - x2 , y1_ = y1 - y2 , x3_ = x3 - x2 , y3_ = y3 - y2 ;
119 
120  cot_12 = cot( alpha_12 ) ;
121 
122  c12x = x1_ + cot_12 * y1_ ;
123  c12y = y1_ - cot_12 * x1_ ;
124 
125  c23x = x3_ + cot_12 * y3_ ;
126  c23y = y3_ - cot_12 * x3_ ;
127 
128  c31x = (y3_ - y1_) ;
129  c31y = (x1_ - x3_) ;
130  k = (y3_ * x1_) - (x3_ * y1_) ;
131 
132  D = (c23x - c12x) * (c31y) + (c31x) * (c12y - c23y) ;
133  invD = 1.0 / D ;
134  K = k * invD ;
135 
136  *x = K * (c12y - c23y) + x2 ;
137  *y = K * (c23x - c12x) + y2 ;
138 
139  return invD ;
140  }
141 
142  x1_ = x1 - x2 , y1_ = y1 - y2 , x3_ = x3 - x2 , y3_ = y3 - y2 ;
143 
144  cot_12 = cot( alpha_12 ) ;
145  cot_23 = cot( alpha_23 ) ;
146  cot_31 = ( 1.0 - cot_12*cot_23 ) / (cot_12 + cot_23) ;
147 
148  c12x = x1_ + cot_12 * y1_ ;
149  c12y = y1_ - cot_12 * x1_ ;
150 
151  c23x = x3_ - cot_23 * y3_ ;
152  c23y = y3_ + cot_23 * x3_ ;
153 
154  c31x = (x3_ + x1_) + cot_31 * (y3_ - y1_) ;
155  c31y = (y3_ + y1_) - cot_31 * (x3_ - x1_) ;
156  k = (x3_ * x1_) + (y3_ * y1_) + cot_31 * ( (y3_ * x1_) - (x3_ * y1_) ) ;
157 
158  D = (c12x - c23x) * (c23y - c31y) - (c23x - c31x) * (c12y - c23y) ;
159  invD = 1.0 / D ;
160  K = k * invD ;
161 
162  *x = K * (c12y - c23y) + x2 ;
163  *y = K * (c23x - c12x) + y2 ;
164 
165  return invD ; /* return 1/D */
166 }
167 
#define PI
The value of PI.
Definition: const.h:12
double tfloat
Defines the type for float/double.
Definition: const.h:38
tfloat triangulationPierlot(tfloat *x, tfloat *y, tfloat alpha1, tfloat alpha2, tfloat alpha3, tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
Definition: total.c:16