C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
madsen.c
Go to the documentation of this file.
1 /**
2  * @file madsen.c
3  * @date 02/10/2012
4  * @author Vincent Pierlot
5  *
6  * The algorithm was implemented after \cite Madsen1998Optimal.
7 */
8 #include <math.h>
9 #include "const.h"
10 #include "madsen.h"
11 
12 tfloat triangulationMadsen(tfloat *x, tfloat *y,
13  tfloat alpha1, tfloat alpha2, tfloat alpha3,
14  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
15 {
16  tfloat alpha12 = alpha2 - alpha1 ;
17  tfloat alpha23 = alpha3 - alpha2 ;
18 
19  tfloat alpha13 = alpha12 + alpha23 ;
20 
21  tfloat L12_2 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ;
22  tfloat L23_2 = (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ;
23  tfloat L13_2 = (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) ;
24  tfloat L12 = sqrt( L12_2 ) ;
25  tfloat L23 = sqrt( L23_2 ) ;
26  tfloat L13 = sqrt( L13_2 ) ;
27 
28  tfloat cosA123 = ( L12_2 + L23_2 - L13_2 ) / ( 2 * L12 * L23 ) ;
29  tfloat cosA213 = ( L12_2 + L13_2 - L23_2 ) / ( 2 * L12 * L13 ) ;
30  tfloat cosA132 = ( L13_2 + L23_2 - L12_2 ) / ( 2 * L13 * L23 ) ;
31 
32  tfloat A123 = acos( cosA123 ) ;
33  tfloat A213 = acos( cosA213 ) ;
34  tfloat A132 = acos( cosA132 ) ;
35 
36  tfloat sinA123 = Sin( A123 ) ;
37 
38  tfloat sin_alpha12 = Sin( alpha12 ) ;
39  tfloat sin_alpha13 = Sin( alpha13 ) ;
40  tfloat cos_alpha13 = Cos( alpha13 ) ;
41 
42  /* valid for topology 1, I haven't implemented the second one */
43  tfloat Ap12 = - atan( (L23*sin_alpha12*(sin_alpha13*cosA123 - cos_alpha13*sinA123)) / (L12*Sin(alpha23) + L23*sin_alpha12*(cos_alpha13*cosA123 + sin_alpha13*sinA123) ) ) ;
44  tfloat A23p = A123 - Ap12 - alpha13 ;
45 
46  tfloat tau = atan2( (y3-y1) , (x3-x1) ) ;
47  tfloat cos_tau = Cos( tau ) ;
48  tfloat sin_tau = Sin( tau ) ;
49 
50  tfloat T1 = Tan( A23p + A132 ) ;
51  tfloat T2 = Tan( Ap12 + A213 ) ;
52 
53  tfloat x_ = L13 * T1 / ( T2 + T1 ) ;
54  tfloat y_ = (x_) * T2 ;
55 
56  *x = x_ * cos_tau - y_ * sin_tau + x1 ;
57  *y = x_ * sin_tau + y_ * cos_tau + y1 ;
58 
59  return tau;
60 }
61 
double tfloat
Defines the type for float/double.
Definition: const.h:38