C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
mcgillem.c
Go to the documentation of this file.
1 /**
2  * @file mcgillem.c
3  * @date 04/09/2012
4  * @author Vincent Pierlot
5  *
6  * The algorithm was implemented after \cite Mcgillem1989ABeacon.
7 */
8 #include <math.h>
9 #include "const.h"
10 #include "mcgillem.h"
11 
12 tfloat triangulationMcGillemTrigo(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 d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
17  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
18 
19  tfloat phi1 = alpha1 - alpha2 ;
20  tfloat phi2 = alpha2 - alpha3 ;
21 
22  tfloat sin_phi1 = Sin( phi1 ) ;
23  tfloat sin_phi2 = Sin( phi2 ) ;
24 
25  tfloat Phi1 = HALFPI - atan2( (y2-y1) , (x2-x1) ) ;
26  tfloat Phi2 = HALFPI - atan2( (y3-y2) , (x3-x2) ) ;
27 
28  tfloat w = QUARTPI + atan( ( d12 * sin_phi2 ) / ( d23 * sin_phi1 ) ) ;
29 
30  tfloat v = HALFPI - ( Phi1 - Phi2 + phi1 + phi2 ) / 2 ;
31 
32  tfloat gamma = v + atan( tan(v) / tan(w) ) ;
33 
34  tfloat dAP = d12 * Sin( gamma + phi1 ) / sin_phi1 ;
35 
36  *x = x1 + dAP * Sin( Phi1 + gamma ) ;
37  *y = y1 + dAP * Cos( Phi1 + gamma ) ;
38 
39  return 0;
40 }
41 
42 tfloat triangulationMcGillemGeometric(tfloat *x, tfloat *y,
43  tfloat alpha1, tfloat alpha2, tfloat alpha3,
44  tfloat x1, tfloat y1, tfloat x2, tfloat y2, tfloat x3, tfloat y3)
45 {
46  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
47  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
48 
49  tfloat phi1 = alpha1 - alpha2 ;
50  tfloat phi2 = alpha2 - alpha3 ;
51 
52  tfloat sigma1 = -atan2( (y2-y1) , (x2-x1) ) ;
53  tfloat sigma2 = -atan2( (y3-y2) , (x3-x2) ) ;
54 
55  tfloat R1 = d12 / ( 2 * Sin( phi1 ) ) ;
56  tfloat R2 = d23 / ( 2 * Sin( phi2 ) ) ;
57 
58  tfloat xn = x1 - R1 * Sin( sigma1 - phi1 ) ;
59  tfloat yn = y1 - R1 * Cos( sigma1 - phi1 ) ;
60 
61  tfloat xm = x2 - R2 * Sin( sigma2 - phi2 ) ;
62  tfloat ym = y2 - R2 * Cos( sigma2 - phi2 ) ;
63 
64  tfloat m = ( ym - yn ) / ( xn - xm ) ;
65  tfloat n = ( R2*R2 - R1*R1 - xm*xm + xn*xn - ym*ym + yn*yn ) / ( 2*( xn - xm ) ) ;
66 
67  *y = ( ( 2*m*xn + 2*yn - 2*m*n ) / ( 1 + m*m ) ) - y2 ;
68  *x = m * (*y) + n ;
69 
70  return n;
71 }
72 
double tfloat
Defines the type for float/double.
Definition: const.h:38