0

I am trying to implement the Trilateration process in 2D. The wikipedia article relating to this: Tilateration

I have found a nice question here at this site, where the algorithm is well explained:artifical intelligence

After all, I tried to implement the algorithm in c++. Unfortunately I faced some problems... Let's see my implementation. It is only a function: The first inputs are three vector, each representing a 2D point with X,Y coordinates. The other (r1,r2,r3) input variables stand for the distance/radius of each point.

#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h> 
#include <vector>
using namespace std;

std::vector<double> trilateration(double point1[], double point2[], double point3[], double r1, double r2, double r3) {
    std::vector<double> resultPose;
    //unit vector in a direction from point1 to point 2
    double p2p1Distance = pow(pow(point2[0]-point1[0],2) + pow(point2[1]-point1[1],2),0.5);
    double exx = (point2[0]-point1[0])/p2p1Distance;
    double exy = (point2[1]-point1[1])/p2p1Distance;
    //signed magnitude of the x component
    double ix = exx*(point3[0]-point1[0]);
    double iy = exy*(point3[1]-point1[1]);
    //the unit vector in the y direction. 
    double eyx = (point3[0]-point1[0]-ix*exx)/pow(pow(point3[0]-point1[0]-ix*exx,2) + pow(point3[1]-point1[1]-iy*exy,2),0.5);
    double eyy = (point3[1]-point1[1]-iy*exy)/pow(pow(point3[0]-point1[0]-ix*exx,2) + pow(point3[1]-point1[1]-iy*exy,2),0.5);
    //the signed magnitude of the y component
    double jx = eyx*(point3[0]-point1[0]);
    double jy = eyy*(point3[1]-point1[1]);
    //coordinates
    double x = (pow(r1,2) - pow(r2,2) + pow(p2p1Distance,2))/ (2 * p2p1Distance);
    double y = (pow(r1,2) - pow(r3,2) + pow(iy,2) + pow(jy,2))/2*jy - ix*x/jx;
    //result coordinates
    double finalX = point1[0]+ x*exx + y*eyx;
    double finalY = point1[1]+ x*exy + y*eyy;
    resultPose.push_back(finalX);
    resultPose.push_back(finalY);
    return resultPose;
}

As I mentioned I followed this article. I am of the opinion that the problem lies at the part where the y coordinate is calculated. I am also not sure about last part, where I calculate finalX, finalY...

My main function is the following:

int main(int argc, char* argv[]){
    std::vector<double> finalPose;
    double p1[] = {4.0,4.0};
    double p2[] = {9.0,7.0};
    double p3[] = {9.0,1.0};
    double r1,r2,r3;
    r1 = 4;
    r2 = 3;
    r3 = 3.25;
    finalPose = trilateration(p1,p2,p3,r1,r2,r3);
    cout<<"X:::  "<<finalPose[0]<<endl;
    cout<<"Y:::  "<<finalPose[1]<<endl; 
    //x = 8, y = 4.1

}

The result should be around X~8 and Y~4.1, but I got X = 13.5542 and Y=-5.09038

So my problem is and question is: I have problem with dividing the calculations for x and y. I think I could solve the algorithm till x, after that I have problems with calculating y.

The calculation is the following for y: y = (r12 - r32 + i2 + j2) / 2j - ix / j

I do not know which i and j I should use here since I have two i (ix,iy) and two j(jx,jy). As you can see I used iy and jy but at the end of the line I used ix due to multiplication with x. Thanks in advance!

Community
  • 1
  • 1
mirind4
  • 1,423
  • 4
  • 21
  • 29

2 Answers2

3

I used a couple of auxiliary variables but it works just fine...

#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h> 
#include <vector>
using namespace std;

struct point 
{
    float x,y;
};

float norm (point p) // get the norm of a vector
{
    return pow(pow(p.x,2)+pow(p.y,2),.5);
}

point trilateration(point point1, point point2, point point3, double r1, double r2, double r3) {
    point resultPose;
    //unit vector in a direction from point1 to point 2
    double p2p1Distance = pow(pow(point2.x-point1.x,2) + pow(point2.y-   point1.y,2),0.5);
    point ex = {(point2.x-point1.x)/p2p1Distance, (point2.y-point1.y)/p2p1Distance};
    point aux = {point3.x-point1.x,point3.y-point1.y};
    //signed magnitude of the x component
    double i = ex.x * aux.x + ex.y * aux.y;
    //the unit vector in the y direction. 
    point aux2 = { point3.x-point1.x-i*ex.x, point3.y-point1.y-i*ex.y};
    point ey = { aux2.x / norm (aux2), aux2.y / norm (aux2) };
    //the signed magnitude of the y component
    double j = ey.x * aux.x + ey.y * aux.y;
    //coordinates
    double x = (pow(r1,2) - pow(r2,2) + pow(p2p1Distance,2))/ (2 * p2p1Distance);
    double y = (pow(r1,2) - pow(r3,2) + pow(i,2) + pow(j,2))/(2*j) - i*x/j;
    //result coordinates
    double finalX = point1.x+ x*ex.x + y*ey.x;
    double finalY = point1.y+ x*ex.y + y*ey.y;
    resultPose.x = finalX;
    resultPose.y = finalY;
    return resultPose;
}

int main(int argc, char* argv[]){
    point finalPose;
    point p1 = {4.0,4.0};
    point p2 = {9.0,7.0};
    point p3 = {9.0,1.0};
    double r1,r2,r3;
    r1 = 4;
    r2 = 3;
    r3 = 3.25;
    finalPose = trilateration(p1,p2,p3,r1,r2,r3);
    cout<<"X:::  "<<finalPose.x<<endl;
    cout<<"Y:::  "<<finalPose.y<<endl; 
}

$ the output is:

X:::  8.02188
Y:::  4.13021
2

It's a little unclear, and perhaps incorrect, in the linked SO answer that the values of i and j are scalar values and computed a bit differently than the other vector quantities. More explicitly you should have:

i = ex · (P3 - P1) = exx (P3x - P1x) + exy (P3y - P1y) = ix + iy

j = ey · (P3 - P1) = eyx (P3x - P1x) + eyy (P3y - P1y) = jx + jy

Note that · is the dot product of two vectors here. Thus, in your code there should be no ix, iy, jx or jy.

Also, in your calculation of y you should change the denominator of /2*j to:

 / (2*j)

otherwise you are multiplying by j instead of dividing. Making these changes gives me the result of [7.05, 5.74] which is closer to your expected values.

Community
  • 1
  • 1
uesp
  • 6,194
  • 20
  • 15
  • waohh, you are a genius! I appreciate you help, it works! For me i got the right coordinates (8.02,4.13) – mirind4 Apr 15 '15 at 19:21
  • I would like to ask an additional question, if no problem. If I want to expand it to the 3D case, where spheres intersect each other, the only thing I should add is: z = ±sqrt(r12 - x2 - y2)? The following question is about it: http://goo.gl/y30gPn . What is your advice? Thanks in advance! – mirind4 Apr 15 '15 at 19:25
  • For the 3D case it looks like you would just need to add a z component to each of the vectors (`exz`, `eyz`) and compute `ez` and `z` as on the Wikipedia page. Then the calculation for the final coordinates would add the term `±z * ez` where `ez` is a vector (so you would need `ezx`, `ezy` and `ezz` like the other terms). – uesp Apr 15 '15 at 21:25
  • Thanks! As i see the "ez" can be calculated with the cross product of ex and ey. I tried to do this, what do you think it is good? "ezx=exx*eyx", "ezy=exy*eyy", "ezz=exz*eyz" . After these steps the final Z coordinate should be: point1[2] + x * exz + y * eyz + z * ezz (where z = ±sqrt(r12 - x2 - y2)) Am I right? By the way i am really grateful, I like this site and helpful people like you very much! – mirind4 Apr 15 '15 at 21:53
  • Because to the best of my knowledge, if we want to get a 3D coordinates, we should have 4 sphere for the clear and unequivocal solution. But I have really no clue where to put the data of the 4th point to this equations...Do you know how to handle the data of the 4th point/sphere? – mirind4 Apr 15 '15 at 22:18
  • The Wikipedia article only mentions 3 spheres (Trilateration, Tri = 3) and says there can be 0, 1 or 2 solutions to the problem. – uesp Apr 16 '15 at 00:29