C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
fontllagunes.c
Go to the documentation of this file.
1 /**
2  * @file fontllagunes.c
3  * @date 18/03/2011
4  * @author Vincent Pierlot
5  *
6  * The first algorithm was implemented after \cite Font-Llagunes2009Consistent.
7  * The second algorithm was implemented after \cite Font-Llagunes2009New.
8 */
9 #include <math.h>
10 #include "const.h"
11 #include "fontllagunes.h"
12 
13 tfloat triangulationFontLlagunes(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 cot_alpha = Cot( alpha2 - alpha1 ) ;
18  tfloat cot_beta = Cot( alpha3 - alpha2 ) ;
19  cot_alpha = adjust_value_to_bounds( cot_alpha , COT_MAX ) ;
20  cot_beta = adjust_value_to_bounds( cot_beta , COT_MAX ) ;
21 
22  tfloat phi = atan2( (y3-y2) , (x3-x2) ) ;
23  tfloat sin_phi = Sin(phi) ;
24  tfloat cos_phi = Cos(phi) ;
25 
26  tfloat x3r = cos_phi * ( x3 - x2 ) + sin_phi * ( y3 - y2 ) ;
27  tfloat x1r = cos_phi * ( x1 - x2 ) + sin_phi * ( y1 - y2 ) ;
28  tfloat y1r = -sin_phi * ( x1 - x2 ) + cos_phi * ( y1 - y2 ) ;
29 
30  tfloat eta = ( x3r - x1r - y1r*cot_alpha ) / ( x3r*cot_beta - y1r + x1r*cot_alpha ) ;
31 
32  tfloat k = ( 1 - eta*cot_beta ) / ( 1 + eta*eta ) ;
33 
34  tfloat xr = x3r * k ;
35  tfloat yr = xr * (-eta) ;
36 
37  *x = x2 + cos_phi * xr - sin_phi * yr ;
38  *y = y2 + sin_phi * xr + cos_phi * yr ;
39 
40  return eta ;
41 }
42 
43 /* TODO: handle singularities correctly */
44 tfloat triangulationFontLlagunes2(tfloat *x, tfloat *y,
45  tfloat alpha1, tfloat alpha2, tfloat alpha3,
46  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
47 {
48  tfloat theta = 0.0 ; /* first guess */
49 
50  tfloat alpha12 = alpha2 - alpha1 ; /* alpha */
51  tfloat alpha23 = alpha3 - alpha2 ; /* beta */
52 
53  tfloat mA = Cot( theta ) ;
54  tfloat mB = Cot( theta - alpha12 ) ;
55  tfloat mC = Cot( theta - alpha12 - alpha23 ) ;
56  mA = adjust_value_to_bounds( mA , COT_MAX ) ;
57  mB = adjust_value_to_bounds( mB , COT_MAX ) ;
58  mC = adjust_value_to_bounds( mC , COT_MAX ) ;
59 
60  tfloat xPAB = ( mA * x1 - mB * x2 - y1 + y2 ) / ( mA - mB ) ;
61  tfloat yPAB = mA * ( xPAB - x1 ) + y1 ;
62  tfloat xPBC = ( mB * x2 - mC * x3 - y2 + y3 ) / ( mB - mC ) ;
63  tfloat yPBC = mB * ( xPBC - x2 ) + y2 ;
64  tfloat xPAC = ( mA * x1 - mC * x3 - y1 + y3 ) / ( mA - mC ) ;
65  tfloat yPAC = mA * ( xPAC - x1 ) + y1 ;
66 
67  tfloat Ap = ( ( xPAB - xPBC ) * ( yPBC - yPAC ) - ( xPBC - xPAC ) * ( yPAB - yPBC ) )/* / 2*/ ;
68  Ap = ( Ap < 0.0 ) ? -Ap : Ap ;
69 
70  tfloat cot_12 = Cot( alpha12 ) ;
71  tfloat cot_23 = Cot( alpha23 ) ;
72  tfloat cot_31 = ( 1.0 - cot_12 * cot_23 ) / ( cot_12 + cot_23 ) ; /* optimization like ToTal */
73  cot_12 = adjust_value_to_bounds( cot_12 , COT_MAX ) ;
74  cot_23 = adjust_value_to_bounds( cot_23 , COT_MAX ) ;
75  cot_31 = adjust_value_to_bounds( cot_31 , COT_MAX ) ;
76 
77  tfloat xCAB = ( x1 + x2 + ( y1 - y2 ) * cot_12 )/* / 2*/ ;
78  tfloat yCAB = ( y1 + y2 + ( x2 - x1 ) * cot_12 )/* / 2*/ ;
79  tfloat xCBC = ( x2 + x3 + ( y2 - y3 ) * cot_23 )/* / 2*/ ;
80  tfloat yCBC = ( y2 + y3 + ( x3 - x2 ) * cot_23 )/* / 2*/ ;
81  tfloat xCAC = ( x1 + x3 - ( y1 - y3 ) * cot_31 )/* / 2*/ ;
82  tfloat yCAC = ( y1 + y3 - ( x3 - x1 ) * cot_31 )/* / 2*/ ;
83 
84  tfloat Ac = ( ( xCAB - xCBC ) * ( yCBC - yCAC ) - ( xCBC - xCAC ) * ( yCAB - yCBC ) )/* / 2*/ ;
85  Ac = ( Ac < 0.0 ) ? -Ac : Ac ;
86 
87  tfloat delta_theta = asin( sqrt( Ap / Ac )/* / 2*/ ) ;
88  delta_theta = ( delta_theta < 0.0 ) ? -delta_theta : delta_theta ;
89 
90  tfloat S = ( xPBC - xPAB ) * ( yCBC - yCAB ) - ( xCBC - xCAB ) * ( yPBC - yPAB ) ;
91  if ( S < 0.0 ) delta_theta = -delta_theta ;
92 
93  theta += delta_theta ;
94 
95  mA = Cot( theta ) ;
96  mB = Cot( theta - alpha12 ) ;
97  mA = adjust_value_to_bounds( mA , COT_MAX ) ;
98  mB = adjust_value_to_bounds( mB , COT_MAX ) ;
99 
100  *x = ( mA * x1 - mB * x2 - y1 + y2 ) / ( mA - mB ) ;
101  *y = mA * ( (*x) - x1 ) + y1 ;
102 
103  return Ac ;
104 }
double tfloat
Defines the type for float/double.
Definition: const.h:38