C library of triangulation algorithms (for the problems of mobile robot positioning or resection)
zalama.c
Go to the documentation of this file.
1 /**
2  * @file zalama.c
3  * @date 03/09/2012
4  * @author Vincent Pierlot
5  *
6  * The algorithm was implemented after \cite Casanova2002ANewBeacon.
7  * Actually, the algorithm is a little bit different because the paper version didn't work. See comments below.
8 */
9 #include <math.h>
10 #include "const.h"
11 #include "zalama.h"
12 
13 tfloat triangulationZalama(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 alpha12 = alpha2 - alpha1 ;
18  tfloat alpha23 = alpha3 - alpha2 ;
19 
20  tfloat d12 = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) ;
21  tfloat d23 = sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) ) ;
22 
23  tfloat delta12 = atan2( (y2-y1) , (x2-x1) ) ;
24  tfloat ra = d12 / ( 2 * Sin( alpha12 ) ) ;
25  tfloat cax = x1 - ra * Sin( delta12 - alpha12 ) ;
26  tfloat cay = y1 + ra * Cos( delta12 - alpha12 ) ;
27 
28  tfloat delta23 = atan2( (y3-y2) , (x3-x2) ) ;
29  tfloat rb = d23 / ( 2 * Sin( alpha23 ) ) ;
30  tfloat cbx = x2 - rb * Sin( delta23 - alpha23 ) ;
31  tfloat cby = y2 + rb * Cos( delta23 - alpha23 ) ;
32 
33  tfloat d = sqrt( (cbx-cax)*(cbx-cax) + (cby-cay)*(cby-cay) ) ;
34 
35  tfloat d2r = ( rb*rb + d*d - ra*ra ) / ( 2 * d ) ;
36 
37  tfloat gamma = acos( d2r / rb ) ;
38 
39  tfloat phi = atan2( (cay-cby) , (cax-cbx) ) ;
40 
41  tfloat R1x = cbx + rb * Cos( gamma - phi ) ; /* the paper uses 'cax' and 'ra' */
42  tfloat R1y = cby - rb * sin( gamma - phi ) ; /* the paper uses 'cay' and 'ra' */
43  tfloat v1 = sqrt( (R1x-x2)*(R1x-x2) + (R1y-y2)*(R1y-y2) ) ;
44 
45  tfloat R2x = cbx + rb * Cos( phi + gamma ) ; /* the paper uses 'cax' and 'ra' */
46  tfloat R2y = cby + rb * Sin( phi + gamma ) ; /* the paper uses 'cay' and 'ra' */
47  tfloat v2 = sqrt( (R2x-x2)*(R2x-x2) + (R2y-y2)*(R2y-y2) ) ;
48 
49  if( v1 > v2 )
50  {
51  *x = R1x ;
52  *y = R1y ;
53  }
54  else
55  {
56  *x = R2x ;
57  *y = R2y ;
58  }
59 
60  return d;
61 }
62 
double tfloat
Defines the type for float/double.
Definition: const.h:38