C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
tsukiyama.c
Go to the documentation of this file.
1 /**
2  * @file tsukiyama.c
3  * @date 05/09/2012
4  * @author Vincent Pierlot
5  *
6  * The algorithm was implemented after \cite Tsukiyama2009Mobile.
7  * IT DOES NOT WORK IN MOST OF THE CASES. SOMETIMES THE SOLUTION IS BEACON 2 LOCATION. SEE SECOND ALGORITHM.
8 */
9 #include <math.h>
10 #include "const.h"
11 #include "tsukiyama.h"
12 
13 tfloat triangulationTsukiyamaOriginal(tfloat *x, tfloat *y,
14  tfloat alpha1, tfloat alpha2, tfloat alpha3,
15  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
16 {
17  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
18  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
19 
20  tfloat phi1 = alpha1 - alpha2 ;
21  tfloat phi2 = alpha2 - alpha3 ;
22 
23  tfloat gamma1 = atan2( (y2-y1) , (x2-x1) ) ;
24  tfloat gamma2 = atan2( (y3-y2) , (x3-x2) ) ;
25 
26  tfloat R1 = d12 / ( 2 * Sin( phi1 ) ) ;
27  tfloat R2 = d23 / ( 2 * Sin( phi2 ) ) ;
28 
29  tfloat theta1 = acos( d12 / ( 2 * R1 ) ) ;
30  tfloat theta2 = acos( d23 / ( 2 * R2 ) ) ;
31 
32  tfloat cx1 = x1 + R1 * Cos( gamma1 - theta1 ) ;
33  tfloat cy1 = y1 + R1 * Sin( gamma1 - theta1 ) ;
34 
35  tfloat cx2 = x2 + R2 * Cos( gamma2 - theta2 ) ;
36  tfloat cy2 = y2 + R2 * Sin( gamma2 - theta2 ) ;
37 
38  tfloat d = sqrt( (cx2-cx1)*(cx2-cx1) + (cy2-cy1)*(cy2-cy1) ) ;
39 
40  tfloat phi = acos( ( R1 * R1 + d * d - R2 * R2 ) / ( 2 * R1 * d ) ) ;
41  tfloat sigma = atan2( ( cy2 - cy1 ) , ( cx2 - cx1 ) ) ;
42 
43  *x = cx1 + R1 * Cos( phi - sigma ) ;
44  *y = cy1 - R1 * Sin( phi - sigma ) ; /* in the paper, it's an addition... */
45 
46  return d;
47 }
48 
49 /*
50  * Created by Vincent Pierlot on 05/09/2012.
51  * The algorithm was implemented after Tsukiyama2009Mobile.
52  * Actually, the algorithm is a little bit different because the paper version didn't work. See comments below.
53 */
54 tfloat triangulationTsukiyama(tfloat *x, tfloat *y,
55  tfloat alpha1, tfloat alpha2, tfloat alpha3,
56  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
57 {
58  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
59  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
60 
61  tfloat phi1 = alpha1 - alpha2 ;
62  tfloat phi2 = alpha2 - alpha3 ;
63 
64  tfloat gamma1 = atan2( (y2-y1) , (x2-x1) ) ;
65  tfloat gamma2 = atan2( (y3-y2) , (x3-x2) ) ;
66 
67  tfloat R1 = d12 / ( 2 * Sin( phi1 ) ) ;
68  tfloat R2 = d23 / ( 2 * Sin( phi2 ) ) ;
69 
70  tfloat cx1 = x1 + R1 * Sin( gamma1 + phi1 ) ; /* changed from the paper since theta1 = PI/2 - phi1 */
71  tfloat cy1 = y1 - R1 * Cos( gamma1 + phi1 ) ; /* changed from the paper since theta1 = PI/2 - phi1 */
72 
73  tfloat cx2 = x2 + R2 * Sin( gamma2 + phi2 ) ; /* changed from the paper since theta2 = PI/2 - phi2 */
74  tfloat cy2 = y2 - R2 * Cos( gamma2 + phi2 ) ; /* changed from the paper since theta2 = PI/2 - phi2 */
75 
76  tfloat d = sqrt( (cx2-cx1)*(cx2-cx1) + (cy2-cy1)*(cy2-cy1) ) ;
77 
78  tfloat phi = acos( ( R1*R1 + d*d - R2*R2 ) / ( 2 * R1 * d ) ) ;
79  tfloat sigma = atan2( ( cy2 - cy1 ) , ( cx2 - cx1 ) ) ;
80 
81  /* compute the two possible solutions, like Casanova, and choose the solution that is not beacon 2 location */
82  tfloat R1x = cx1 + R1 * Cos( phi - sigma ) ;
83  tfloat R1y = cy1 - R1 * Sin( phi - sigma ) ;
84  tfloat v1 = sqrt( (R1x-x2)*(R1x-x2) + (R1y-y2)*(R1y-y2) ) ;
85 
86  tfloat R2x = cx1 + R1 * Cos( phi + sigma ) ;
87  tfloat R2y = cy1 + R1 * Sin( phi + sigma ) ;
88  tfloat v2 = sqrt( (R2x-x2)*(R2x-x2) + (R2y-y2)*(R2y-y2) ) ;
89 
90  if( v1 > v2 )
91  {
92  *x = R1x ;
93  *y = R1y ;
94  }
95  else
96  {
97  *x = R2x ;
98  *y = R2y ;
99  }
100 
101  return d;
102 }
103 
double tfloat
Defines the type for float/double.
Definition: const.h:38