osp-locations changed to replace log macros with secure log macros to avoid privacy...
[platform/framework/native/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 <FBaseDouble.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseUtilMath.h>
29 #include <FLocLocation.h>
30 #include "FLoc_MathUtils.h"
31 #include "FLoc_RegionInfo.h"
32
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Base;
35
36 namespace Tizen { namespace Locations
37 {
38
39 const double _MathUtils::PI = M_PI;
40 const double _MathUtils::PI2 = 2.0 * _MathUtils::PI;
41 const double _MathUtils::DEG2RAD = _MathUtils::PI / 180.0;
42 const double _MathUtils::RAD2DEG = 180.0 / _MathUtils::PI;
43 const double _MathUtils::HALFPI = 1.5707963267948966;
44 const double _MathUtils::CENTRE_LATITUDE = 0.0;
45
46 double
47 _MathUtils::CalculateOverlapRegion(const _RegionInfo& region, const Location& location)
48 {
49         double overlapArea = 0;
50         double distanceBtwCenters = region.GetCoordinate().GetDistanceTo(location.GetCoordinates());
51         double regionRadius = region.GetRadius();
52         double locationRadius = location.GetHorizontalAccuracy();
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         return overlapArea;
70 }
71
72 double
73 _MathUtils::GetShortestDistance(const Location& location, const Tizen::Base::Collection::IList& regionList)
74 {
75         double minDistance = Double::GetMaxValue();
76         int count = regionList.GetCount();
77
78         for (int i = 0; i < count; i++)
79         {
80                 const _RegionInfo* pRegionInfo = static_cast<const _RegionInfo*> (regionList.GetAt(i));
81                 if (pRegionInfo)
82                 {
83                         Coordinates regionCoordinate = pRegionInfo->GetCoordinate();
84                         double distance = abs(regionCoordinate.GetDistanceTo(location.GetCoordinates()) - pRegionInfo->GetRadius());
85
86                         if (minDistance > distance)
87                         {
88                                 minDistance = distance;
89                         }
90                 }
91         }
92
93         SysSecureLog(NID_LOC, "Shortest distance from location (lat: %lf and lon: %lf) to the nearest region boundary is (%lf) meters", location.GetCoordinates().GetLatitude(), location.GetCoordinates().GetLongitude(), minDistance);
94         return minDistance;
95 }
96 } } // Tizen::Locations