C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
cohen.c
Go to the documentation of this file.
1 /**
2  * @file cohen.c
3  * @date 28/08/2012
4  * @author Vincent Pierlot
5 
6  * The algorithm was implemented after \cite Esteves2003Generalized (same as \cite Cohen1992AComprehensive).
7  * Unlike what is written in the paper, this method seems to work in all cases and beacon configurations.
8  * Maybe it is due to the use of atan2() since it is not mentioned in the paper.
9 */
10 #include <math.h>
11 #include "const.h"
12 #include "cohen.h"
13 
14 /* Trigonometric version */
15 tfloat triangulationCohenTrigo(tfloat *x, tfloat *y,
16  tfloat alpha1, tfloat alpha2, tfloat alpha3,
17  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
18 {
19  tfloat alpha12 = alpha2 - alpha1 ;
20  tfloat alpha31 = alpha1 - alpha3 ;
21 
22  tfloat L12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
23  tfloat L31 = sqrt( (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) ) ;
24 
25  tfloat phi = atan2( (y1-y2) , (x1-x2) ) ;
26  tfloat sigma = atan2( (x1-x3) , (y1-y3) ) + HALFPI + phi ; /* different from the paper */
27 
28  tfloat gamma = sigma - alpha31 ;
29 
30  tfloat sin_alpha12 = Sin( alpha12 ) ;
31  tfloat sin_alpha31 = Sin( alpha31 ) ;
32 
33  tfloat p = ( L31 * sin_alpha12 ) / ( L12 * sin_alpha31 ) ;
34  tfloat tau = atan( ( sin_alpha12 - p * Sin( gamma ) ) / ( p * Cos( gamma ) - Cos( alpha12 ) ) ) ;
35 
36  tfloat L1 = ( L12 * Sin( tau + alpha12 ) ) / sin_alpha12 ;
37 
38  *x = x1 - L1 * Cos( phi + tau ) ;
39  *y = y1 - L1 * Sin( phi + tau ) ;
40 
41  return tau;
42 }
43 
44 /*
45 Note: Cohen 2 = Kortenkamp
46 */
47 
48 /* Geometric Circle Intersection version */
49 tfloat triangulationCohenGeometricOriginal(tfloat *x, tfloat *y,
50  tfloat alpha1, tfloat alpha2, tfloat alpha3,
51  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
52 {
53  tfloat alpha12 = alpha2 - alpha1 ; /* alpha */
54  tfloat alpha23 = alpha3 - alpha2 ; /* beta */
55 
56  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
57  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
58 
59  tfloat ra = d12 / ( 2 * Sin( alpha12 ) ) ;
60  tfloat rb = d23 / ( 2 * Sin( alpha23 ) ) ;
61 
62  tfloat la = d12 / ( 2 * adjust_value_to_bounds( Tan( alpha12 ) , COT_MAX ) ) ;
63  tfloat lb = d23 / ( 2 * adjust_value_to_bounds( Tan( alpha23 ) , COT_MAX ) ) ;
64 
65  tfloat cax = ( x1 + x2 ) / 2 - la * ( y2 - y1 ) / d12 ;
66  tfloat cay = ( y1 + y2 ) / 2 + la * ( x2 - x1 ) / d12 ;
67 
68  tfloat cbx = ( x2 + x3 ) / 2 - lb * ( y3 - y2 ) / d23 ;
69  tfloat cby = ( y2 + y3 ) / 2 + lb * ( x3 - x2 ) / d23 ;
70 
71  tfloat d = sqrt( (cbx-cax)*(cbx-cax) + (cby-cay)*(cby-cay) ) ;
72 
73  tfloat c2m = ( rb*rb + d*d - ra*ra ) / ( 2 * d ) ; /* different from the paper; taken from Casanova */
74 
75  /*tfloat phi = atan2( (cay-cby) , (cax-cbx) ) ;
76  tfloat mx = cbx + c2m * cos(phi) ; printf("mx = %f\n", mx);
77  tfloat my = cby + c2m * sin(phi) ; printf("my = %f\n", my);*/
78 
79  tfloat mx = cbx + c2m * ( cax - cbx ) / d ; /* optimized */
80  tfloat my = cby + c2m * ( cay - cby ) / d ; /* optimized */
81 
82  *x = 2 * mx - x2 ;
83  *y = 2 * my - y2 ;
84 
85  return d;
86 }
87 
88 tfloat triangulationCohenGeometric(tfloat *x, tfloat *y,
89  tfloat alpha1, tfloat alpha2, tfloat alpha3,
90  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
91 {
92  tfloat alpha12 = alpha2 - alpha1 ; /* alpha */
93  tfloat alpha23 = alpha3 - alpha2 ; /* beta */
94 
95  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
96  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
97 
98  tfloat ra = d12 / ( Sin( alpha12 ) ) ; /* x2 */
99  tfloat rb = d23 / ( Sin( alpha23 ) ) ; /* x2 */
100 
101  tfloat la = adjust_value_to_bounds( Cot( alpha12 ) , COT_MAX ) ; /* optimized */
102  tfloat lb = adjust_value_to_bounds( Cot( alpha23 ) , COT_MAX ) ; /* optimized */
103 
104  tfloat cax = ( x1 + x2 ) - la * ( y2 - y1 ) ; /* optimized */ /* x2 */
105  tfloat cay = ( y1 + y2 ) + la * ( x2 - x1 ) ; /* optimized */ /* x2 */
106 
107  tfloat cbx = ( x2 + x3 ) - lb * ( y3 - y2 ) ; /* optimized */ /* x2 */
108  tfloat cby = ( y2 + y3 ) + lb * ( x3 - x2 ) ; /* optimized */ /* x2 */
109 
110  tfloat d = (cbx-cax)*(cbx-cax) + (cby-cay)*(cby-cay) ; /* x4 */
111 
112  tfloat c2m = ( rb*rb + d - ra*ra ) / ( 2 * d ) ; /* different from the paper; taken from Casanova */ /* unchanged */
113 
114  *x = cbx + c2m * ( cax - cbx ) - x2 ; /* optimized */
115  *y = cby + c2m * ( cay - cby ) - y2 ; /* optimized */
116 
117  return d;
118 }
119 
double tfloat
Defines the type for float/double.
Definition: const.h:38