2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FLoc_MathUtils.cpp
20 * @brief This is the implementation file for _MathUtils class.
22 * This file contains implementation of _MathUtils class.
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"
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Base;
36 namespace Tizen { namespace Locations
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;
47 _MathUtils::CalculateOverlapRegion(const _RegionInfo& region, const Location& location)
49 double overlapArea = 0;
50 double distanceBtwCenters = region.GetCoordinate().GetDistanceTo(location.GetCoordinates());
51 double regionRadius = region.GetRadius();
52 double locationRadius = location.GetHorizontalAccuracy();
54 double distanceBtwCentersSqr = distanceBtwCenters * distanceBtwCenters;
55 double regionRadiusSqr = regionRadius * regionRadius;
56 double locationRadiusSqr = locationRadius * locationRadius;
58 // The circles are overlapping
59 if (distanceBtwCenters <= Math::Abs(regionRadius - locationRadius))
61 overlapArea = locationRadius > regionRadius ? Math::GetPi() * regionRadiusSqr : Math::GetPi() * locationRadiusSqr;
65 double theta = Math::Acos((regionRadiusSqr + distanceBtwCentersSqr - locationRadiusSqr) / (2 * regionRadius * distanceBtwCenters));
66 double phi = Math::Acos((locationRadiusSqr + distanceBtwCentersSqr - regionRadiusSqr) / (2 * locationRadius * distanceBtwCenters));
68 overlapArea = regionRadiusSqr * (theta - Math::Sin(theta) * Math::Cos(theta)) + locationRadiusSqr * (phi - Math::Sin(phi) * Math::Cos(phi));
75 _MathUtils::GetShortestDistance(const Location& location, const Tizen::Base::Collection::IList& regionList)
77 double minDistance = Double::GetMaxValue();
78 int count = regionList.GetCount();
80 for (int i = 0; i < count; i++)
82 const _RegionInfo* pRegionInfo = static_cast<const _RegionInfo*> (regionList.GetAt(i));
86 Coordinates regionCoordinate = pRegionInfo->GetCoordinate();
87 double distance = abs(regionCoordinate.GetDistanceTo(location.GetCoordinates()) - pRegionInfo->GetRadius());
89 if (minDistance > distance)
91 minDistance = distance;
96 SysLog(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);
100 } } // Tizen::Locations