Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __GEOMETRY_H__ 00020 #define __GEOMETRY_H__ 00021 00022 #define _USE_MATH_DEFINES 00023 #include <cmath> 00024 #include <cstdlib> 00025 00026 using namespace std; 00027 namespace wiselib 00028 { 00029 template<typename NodePosition_P> 00030 class Geometry 00031 { 00032 public: 00033 typedef NodePosition_P NodePosition; 00034 typedef typename NodePosition::Float Number; 00035 00036 Geometry() {} 00037 // -------------------------------------------------------------------- 00038 ~Geometry() {} 00039 // -------------------------------------------------------------------- 00040 inline NodePosition trilateration(Number x1, Number y1, Number R1, Number x2, Number y2, Number R2, Number x3, Number y3, Number R3) 00041 { 00042 Number x_one, x_two, y_one, y_two; 00043 poly_points(x1, y1, R1, x2, y2, R2, x_one, y_one, x_two, y_two); 00044 if ( fabs((R3*R3) - ((x3-x_one)*(x3-x_one) + (y3-y_one)*(y3-y_one))) < fabs((R3*R3) - ((x3-x_two)*(x3-x_two) + (y3-y_two)*(y3-y_two))) ) 00045 { 00046 return NodePosition(x_one, y_one, 0); 00047 } 00048 else 00049 { 00050 return NodePosition(x_two, y_two, 0); 00051 } 00052 } 00053 // -------------------------------------------------------------------- 00054 inline void poly_points(Number x1, Number y1, Number R1, Number x2, Number y2, Number R2, Number& x_one, Number& y_one, Number& x_two, Number& y_two) 00055 { 00056 Number ALPHA, BETA, a,b,c,d; 00057 if ( x1 == x2 ) 00058 { 00059 ALPHA = BETA = (y1*y1 - y2*y2 - R1*R1 + R2*R2)/(2*(y1-y2)); 00060 a = 1; 00061 b = (-2*x1); 00062 c = (x1*x1 + y1*y1 + ALPHA*ALPHA - 2*y1*ALPHA - R1*R1); 00063 d = b*b -4*a*c; 00064 y_one = ALPHA; 00065 y_two = ALPHA; 00066 x_one = (-b-sqrt(d))/(2*a); 00067 x_two = (-b+sqrt(d))/(2*a); 00068 } 00069 else 00070 { 00071 ALPHA = (x1*x1 - x2*x2 + y1*y1 - y2*y2 -R1*R1 + R2*R2)/(2*(x1-x2)); 00072 BETA = - (y1-y2)/(x1-x2); 00073 a = (BETA*BETA+1); 00074 b = (2*ALPHA*BETA -2*x1*BETA -2*y1); 00075 c = (x1*x1 + ALPHA*ALPHA - 2*x1*ALPHA + y1*y1 - R1*R1); 00076 d = b*b - 4*a*c; 00077 y_one = (-b-sqrt(d))/(2*a); 00078 y_two = (-b+sqrt(d))/(2*a); 00079 x_one = ALPHA + BETA*y_one; 00080 x_two = ALPHA + BETA*y_two; 00081 } 00082 } 00083 // -------------------------------------------------------------------- 00084 }; 00085 00086 } 00087 #endif