//------------------------------------------------------------------------------
// <copyright file="LqrSMACGeodetic.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declaration of Geodetic functions
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LQRSMACGEODETIC_H__
#define __LQRSMACGEODETIC_H__

#include "LqrSMACSharedLib.h"
#include <string>

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC Geodetic
{
public:

	typedef std::string string;

	struct SHARED_LIB_PUBLIC BearingAndDistance
	{
		BearingAndDistance();
		BearingAndDistance(double _bearing, double _distance);

		string ToString() const;

		double bearing;				// degrees bearing
		double distance;			// distance in kilometers
	};

	enum Hemisphere
	{
		NORTH,
		SOUTH,
		EAST,
		WEST
	};

	enum HemisphereOrientation
	{
		LATITUDE,
		LONGITUDE
	};

	struct SHARED_LIB_PUBLIC Location
	{
		Location();
		Location(double _lat, double _lon);
		Location(const string& latstr, const string& lonstr);

		BearingAndDistance CalculateBearingAndDistanceFrom(const Location& from) const;
		BearingAndDistance CalculateBearingAndDistanceTo(const Location& to) const;
		void MoveLocation(const BearingAndDistance& offset);
		string ToString(bool useDMS = false) const;

		double lat;
		double lon;
		bool valid;
	};

	static BearingAndDistance CalculateBearingAndDistance(const Location& from, const Location& to);
	static double CalculateCentralAngle(const Location& from, const Location& to);
	static double CalculateCentralAngleRad(const Location& from, const Location& to);
	static double CalculateInitialBearing(const Location& from, const Location& to);
	static double CalculateInitialBearing(double flat, double flon, double tlat, double tlon);
	static double HaversineCARad(double flat, double flon, double tlat, double tlon);
	static double LawOfCosinesCARad(double flat, double flon, double tlat, double tlon);
	static Location OffsetLocation(const Location& loc, const BearingAndDistance& offset);
	static double DegreesToRadians(double deg);
	static double RadianstoDegrees(double rad);
	static double RadianstoDegrees(double rad, HemisphereOrientation orientation);
	static double StringToAngle(const string& str, HemisphereOrientation orientation = LATITUDE);
	static string AngleToString(double angle, bool useDMS = false, HemisphereOrientation orientation = LATITUDE);
	static void AngleToDMS(double angle, double& degrees, double& minutes, double& seconds);
	static double DMSToAngle(double degrees, double minutes, double seconds, Hemisphere hemi);
	static void StringToDMS(const string& str, double& degrees, double& minutes, double& seconds, Hemisphere& hemi, HemisphereOrientation orientation = LATITUDE);
};

}
}

#endif
