/***************************************************************************
**
**  This file is part of QGpCoreMath.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This file is distributed in the hope that it will be useful, but WITHOUT
**  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
**  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
**  License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
**  See http://www.geopsy.org for more information.
**
**  Created: 2004-10-07
**  Copyright: 2004-2019
**    Marc Wathelet
**    Marc Wathelet (ULg, Liège, Belgium)
**    Marc Wathelet (LGIT, Grenoble, France)
**
***************************************************************************/

#include <math.h>

#include "Angle.h"
#include "Point2D.h"

namespace QGpCoreMath {

  /*!
    \fn Angle::Angle()
    Init a 0 radian angle
  */

  /*!
    \fn void Angle::set(double dx, double dy)
    Set cosine and sine with compution of distance (sqrt(dx*dx+dy*dy))
  */

  /*!
    \fn void Angle::set(double dx, double dy, double r)
    Set cosine and sine without recompution of distance (sqrt(dx*dx+dy*dy))
  */

  /*!
    \fn Angle& Angle::operator=(const Angle& o)
  */

  /*!
    \fn void Angle::initDegrees()
    From angle in radians, compute angle in degrees
  */

  /*!
    \fn void Angle::initRadians()
    From cosine and sine of angle, compute angle in radians from -pi/2 to pi/2
  */

  /*!
    \fn void Angle::setDegrees(double degrees)
    Can access angle in degrees, radians, sine or cosine.
  */

  /*!
    \fn void Angle::setRadians(double radians)
    Degrees are not set, only angle in radians, sine or cosine.
    To be able to call degrees(), call initDegrees() before.
  */

  /*!
    \fn void Angle::chSign()
    Reverse angle sign.
  */

  /*!
    \fn void Angle::mirror()
    Rotate angle by 180 degrees
  */

  /*!
    \fn double Angle::geographicToMath(double angle)

    Converts a geographical azimuth in degrees (0 in north direction, clockwize) to
    an angle in radians in mathematical sense (0 in east direction, anti-clockwize).

    \sa mathToGeographic(double angle)
  */

  /*!
    \fn double Angle::mathToGeographic(double angle)

    Converts an angle in radians in mathematical sense (0 in east direction, anti-clockwize) to
    geographical azimuth in degrees (0 in north direction, clockwize).

    \sa geographicToMath(double angle)
  */

  /*!
    Converts from degrees to D.MMSSsss... (degrees, minutes, seconds).
  */
  double Angle::degreesToDMS(double angle)
  {
    double sign;
    if(angle<0.0) {
      sign=-1.0;
      angle=-angle;
    } else {
      sign=1.0;
    }
    double d=floor(angle);
    angle=(angle-d)*60.0;
    double m=floor(angle);
    angle=(angle-m)*60.0;
    d+=0.01*m+0.0001*angle;
    return sign*d;
  }

  /*!
    Converts from D.MMSSsss... (degrees, minutes, seconds) to degrees.
  */
  double Angle::DMSToDegrees(double angle)
  {
    double sign;
    if(angle<0.0) {
      sign=-1.0;
      angle=-angle;
    } else {
      sign=1.0;
    }
    double d=floor(angle);
    angle=(angle-d)*100.0;
    double m=floor(angle);
    angle=(angle-m)*100.0;
    d+=m/60.0+angle/3600.0;
    return sign*d;
  }

  /*!
    Converts from degrees to D.MMmm... (degrees, minutes).
  */
  double Angle::degreesToDM(double angle)
  {
    double sign;
    if(angle<0.0) {
      sign=-1.0;
      angle=-angle;
    } else {
      sign=1.0;
    }
    double d=floor(angle);
    angle=(angle-d)*60.0;
    d+=0.01*angle;
    return sign*d;
  }

  /*!
    Converts from D.MMSSsss... (degrees, minutes) to degrees.
  */
  double Angle::DMToDegrees(double angle)
  {
    double sign;
    if(angle<0.0) {
      sign=-1.0;
      angle=-angle;
    } else {
      sign=1.0;
    }
    double d=floor(angle);
    angle=(angle-d)*100.0;
    d+=angle/60.0;
    return sign*d;
  }

  /*!
    Maintains degrees and radians between 0 and 360, 0 and 2*pi, respectivelly.
  */
  void Angle::normalize()
  {
    while(_deg>=360.0) _deg-=360.0;
    while(_deg<0.0) _deg+=360.0;
    while(_rad>=2.0*M_PI) _rad-=2.0*M_PI;
    while(_rad<0.0) _rad+=2.0*M_PI;
  }

  void Angle::operator+=(const Angle& a)
  {
    _deg+=a._deg;
    setRadians(_rad+a._rad);
  }

  /*!
    Remove M_PI ambiguities, returned angle is between -M_PI to M_PI
  */
  double Angle::aroundZero(double angle)
  {
    if(angle<=0.5*M_PI) {
      return angle;
    } else if(angle<1.5*M_PI) {
      return angle-M_PI;
    } else {
      return angle-2*M_PI;
    }
  }

  double Angle::canonicalRadians(double angle)
  {
    TRACE;
    if(angle<0.0) {
      angle+=2.0*M_PI;
      while(angle<0.0) {
        angle+=2.0*M_PI;
      }
    } else if(angle>2.0*M_PI) {
      angle-=2.0*M_PI;
      while(angle>2.0*M_PI) {
        angle-=2.0*M_PI;
      }
    }
    return angle;
  }

  double Angle::canonicalDegrees(double angle)
  {
    TRACE;
    if(angle<0.0) {
      angle+=360.0;
      while(angle<0.0) {
        angle+=360.0;
      }
    } else if(angle>360.0) {
      angle-=360.0;
      while(angle>360.0) {
        angle-=360.0;
      }
    }
    return angle;
  }

  /*!
    Return an angle difference \a a1 - \a a2 between -M_PI and M_PI
  */
  double Angle::differenceRadians(double a1, double a2)
  {
    TRACE;
    double diff=a1-a2;
    if(diff<-M_PI) {
      diff+=2*M_PI;
    } else if(diff>M_PI) {
      diff-=2*M_PI;
    }
    return diff;
  }

  /*!
    Return an angle difference \a a1 - \a a2 between -180 and 180
  */
  double Angle::differenceDegrees(double a1, double a2)
  {
    TRACE;
    double diff=a1-a2;
    if(diff<-180.0) {
      diff+=360.0;
    } else if(diff>180.0) {
      diff-=360.0;
    }
    return diff;
  }

} // namespace QGpCoreMath
