Tizen 2.0 Release
[framework/osp/locations.git] / src / FLoc_MathUtils.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FLoc_MathUtils.cpp
20  * @brief               This is the implementation file for _MathUtils class.
21  *
22  * This file contains implementation of _MathUtils class.
23  */
24
25 #include <math.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilMath.h>
28 #include <FLocLocation.h>
29 #include "FLoc_MathUtils.h"
30 #include "FLoc_RegionInfo.h"
31
32 using namespace Tizen::Base::Utility;
33
34
35 namespace Tizen { namespace Locations
36 {
37
38 const double _MathUtils::PI = M_PI;
39 const double _MathUtils::PI2 = 2.0 * _MathUtils::PI;
40 const double _MathUtils::DEG2RAD = _MathUtils::PI / 180.0;
41 const double _MathUtils::RAD2DEG = 180.0 / _MathUtils::PI;
42 const double _MathUtils::HALFPI = 1.5707963267948966;
43 const double _MathUtils::CENTRE_LATITUDE = 0.0;
44
45 double
46 _MathUtils::CalculateOverlapRegion(const _RegionInfo& region, const Location& location)
47 {
48         double overlapArea = 0;
49         double distanceBtwCenters = region.GetCoordinate().GetDistanceTo(location.GetCoordinates());
50         double regionRadius = region.GetRadius();
51         double locationRadius = location.GetHorizontalAccuracy();
52
53         double distanceBtwCentersSqr = distanceBtwCenters * distanceBtwCenters;
54         double regionRadiusSqr = regionRadius * regionRadius;
55         double locationRadiusSqr = locationRadius * locationRadius;
56
57         // The circles are overlapping
58         if (distanceBtwCenters <= Math::Abs(regionRadius - locationRadius))
59         {
60                 overlapArea = locationRadius > regionRadius ? Math::GetPi() * regionRadiusSqr : Math::GetPi() * locationRadiusSqr;
61         }
62         else
63         {
64                 double theta = Math::Acos((regionRadiusSqr + distanceBtwCentersSqr - locationRadiusSqr) / (2 * regionRadius * distanceBtwCenters));
65                 double phi = Math::Acos((locationRadiusSqr + distanceBtwCentersSqr - regionRadiusSqr) / (2 * locationRadius * distanceBtwCenters));
66
67                 overlapArea = regionRadiusSqr * (theta - Math::Sin(theta) * Math::Cos(theta)) + locationRadiusSqr * (phi - Math::Sin(phi) * Math::Cos(phi));
68         }
69
70         return overlapArea;
71 }
72
73 } } // Tizen::Locations