393 lines
12 KiB
C#
393 lines
12 KiB
C#
using System;
|
|
using System.Runtime.Serialization;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Raytheon.Units
|
|
{
|
|
/// <summary>
|
|
/// This class represents an angle.
|
|
///
|
|
/// It has no constructors. Instead, it provides multiple methods for creating objects of this
|
|
/// type. Those methods are of the form FromXXX, where XXX is a unit (e.g.
|
|
/// FromDegrees).
|
|
///
|
|
/// Properties (e.g. Radians) are provided to convert the value to other units and return
|
|
/// as type double.
|
|
/// </summary>
|
|
[Serializable]
|
|
[DataContract]
|
|
public class Angle
|
|
{
|
|
public enum Unit
|
|
{
|
|
Degrees,
|
|
Radians,
|
|
Milliradians
|
|
}
|
|
|
|
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
|
{
|
|
{Unit.Degrees, new List<string>(){ "deg" } },
|
|
{Unit.Radians, new List<string>(){ "rad" } },
|
|
{Unit.Milliradians, new List<string>(){ "mrad" } }
|
|
};
|
|
|
|
private const Unit DefaultUnits = Unit.Degrees;
|
|
private const double DegreesPerRadian = 180 / Math.PI;
|
|
private const int MilliradiansPerRadian = 1000;
|
|
|
|
[DataMember(Name = "Angle", IsRequired = true)]
|
|
private double _angle; // radians
|
|
|
|
private static IDictionary<Unit, UnitInfo<Angle>> _unitInfo = new Dictionary<Unit, UnitInfo<Angle>>()
|
|
{
|
|
{ Unit.Degrees, new UnitInfo<Angle>(UnitAbbreviations[Unit.Degrees], FromDegrees, typeof(Angle).GetProperty("Degrees").GetGetMethod()) },
|
|
{ Unit.Radians, new UnitInfo<Angle>(UnitAbbreviations[Unit.Radians], FromRadians, typeof(Angle).GetProperty("Radians").GetGetMethod()) },
|
|
{ Unit.Milliradians, new UnitInfo<Angle>(UnitAbbreviations[Unit.Milliradians], FromMilliradians, typeof(Angle).GetProperty("Milliradians").GetGetMethod()) }
|
|
};
|
|
|
|
/// <summary>
|
|
/// Prevents a default instance of the <see cref="Angle"/> class from being created.
|
|
/// Use FromXXX methods to create objects of this type.
|
|
/// </summary>
|
|
/// <exception cref="System.Exception">No one should be calling this, it is only here to prevent users from creating default object</exception>
|
|
private Angle()
|
|
{
|
|
throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
|
|
}
|
|
|
|
/// <summary>
|
|
/// ctor used by this class to create objects in FromXXX methods
|
|
/// </summary>
|
|
/// <param name="angle">The angle - radians.</param>
|
|
private Angle(double angle)
|
|
{
|
|
_angle = angle;
|
|
}
|
|
|
|
#region conversion properties
|
|
|
|
/// <summary>
|
|
/// Returns degrees as type double
|
|
/// </summary>
|
|
/// <value>
|
|
/// degrees
|
|
/// </value>
|
|
public double Degrees
|
|
{
|
|
get
|
|
{
|
|
return _angle * DegreesPerRadian;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns milliradians as type double
|
|
/// </summary>
|
|
/// <value>
|
|
/// milliradians
|
|
/// </value>
|
|
public double Milliradians
|
|
{
|
|
get
|
|
{
|
|
return _angle * MilliradiansPerRadian;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns radians as type double
|
|
/// </summary>
|
|
/// <value>
|
|
/// radians
|
|
/// </value>
|
|
public double Radians
|
|
{
|
|
get
|
|
{
|
|
return _angle;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region creation methods
|
|
|
|
/// <summary>
|
|
/// Returns Angle object interpreting passed value as degrees
|
|
/// </summary>
|
|
/// <param name="degrees">degrees</param>
|
|
/// <returns></returns>
|
|
public static Angle FromDegrees(double degrees)
|
|
{
|
|
return new Angle(degrees / DegreesPerRadian);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns Angle object interpreting passed value as milliradians
|
|
/// </summary>
|
|
/// <param name="milliradians">milliradians</param>
|
|
/// <returns></returns>
|
|
public static Angle FromMilliradians(double milliradians)
|
|
{
|
|
return new Angle(milliradians / MilliradiansPerRadian);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns Angle object interpreting passed value as radians
|
|
/// </summary>
|
|
/// <param name="radians">radians</param>
|
|
/// <returns></returns>
|
|
public static Angle FromRadians(double radians)
|
|
{
|
|
return new Angle(radians);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
Angle a = obj as Angle;
|
|
|
|
return (a == null) ? false : (this._angle == a._angle);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return _angle.GetHashCode();
|
|
}
|
|
|
|
#region binary operators
|
|
|
|
// not implementing %, &, |, ^, <<, >>
|
|
|
|
|
|
public static Angle operator +(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot add a null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot add null Angle");
|
|
|
|
return new Angle(left._angle + right._angle);
|
|
}
|
|
|
|
|
|
public static Angle operator -(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot subtract a null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot subtract a null Angle");
|
|
|
|
return new Angle(left._angle - right._angle);
|
|
}
|
|
|
|
|
|
public static Angle operator *(Angle angle, double scalar)
|
|
{
|
|
if (null == angle)
|
|
throw new ArgumentNullException("angle", "Cannot multiply a null Angle");
|
|
|
|
return new Angle(angle._angle * scalar);
|
|
}
|
|
|
|
|
|
public static Angle operator *(double scalar, Angle angle)
|
|
{
|
|
if (null == angle)
|
|
throw new ArgumentNullException("angle", "Cannot multiply a null Angle");
|
|
|
|
return new Angle(scalar * angle._angle);
|
|
}
|
|
|
|
|
|
public static Angle operator /(Angle angle, double scalar)
|
|
{
|
|
if (null == angle)
|
|
throw new ArgumentNullException("angle", "Cannot divide a null Angle");
|
|
|
|
if (0.0 == scalar)
|
|
throw new DivideByZeroException("Cannot divide Angle by 0");
|
|
|
|
return new Angle(angle._angle / scalar);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region comparison operators
|
|
|
|
public static bool operator ==(Angle left, Angle right)
|
|
{
|
|
bool bReturn = false;
|
|
if (object.ReferenceEquals(left, null))
|
|
{
|
|
if (object.ReferenceEquals(right, null))
|
|
{
|
|
//both are null, so we are ==
|
|
bReturn = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!object.ReferenceEquals(left, null) &&
|
|
!object.ReferenceEquals(right, null))
|
|
{
|
|
bReturn = left._angle == right._angle;
|
|
}
|
|
}
|
|
return bReturn;
|
|
}
|
|
|
|
public static bool operator !=(Angle left, Angle right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
|
|
public static bool operator <(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
|
|
|
return (left._angle < right._angle);
|
|
}
|
|
|
|
public static bool operator >(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
|
|
|
return (left._angle > right._angle);
|
|
}
|
|
|
|
public static bool operator <=(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
|
|
|
return (left._angle <= right._angle);
|
|
}
|
|
|
|
public static bool operator >=(Angle left, Angle right)
|
|
{
|
|
if (null == left)
|
|
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
|
|
|
if (null == right)
|
|
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
|
|
|
return (left._angle >= right._angle);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region operators with other classes
|
|
|
|
/// <summary>
|
|
/// Implements the operator / for AngularVelocity = Angle / PrecisionTimeSpan
|
|
/// </summary>
|
|
/// <param name="velocity">The angle.</param>
|
|
/// <param name="time">The time.</param>
|
|
/// <returns>
|
|
/// AngularVelocity
|
|
/// </returns>
|
|
|
|
public static AngularVelocity operator /(Angle angle, PrecisionTimeSpan time)
|
|
{
|
|
if (null == angle)
|
|
throw new ArgumentNullException("angle", "Cannot divide with null Angle");
|
|
|
|
if (null == time)
|
|
throw new ArgumentNullException("time", "Cannot divide with null PrecisionTimeSpan");
|
|
|
|
if (0.0 == time.Milliseconds)
|
|
throw new DivideByZeroException("Cannot divide angle by 0 time");
|
|
|
|
return AngularVelocity.FromRadiansPerSec(angle.Radians / time.Seconds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Implements the operator / for PrecisionTimeSpan = Angle / AngularVelocity
|
|
/// </summary>
|
|
/// <param name="velocity">The angle.</param>
|
|
/// <param name="time">The velocity.</param>
|
|
/// <returns>
|
|
/// PrecisionTimeSpan
|
|
/// </returns>
|
|
|
|
public static PrecisionTimeSpan operator /(Angle angle, AngularVelocity velocity)
|
|
{
|
|
if (null == angle)
|
|
throw new ArgumentNullException("angle", "Cannot divide with null Angle");
|
|
|
|
if (null == velocity)
|
|
throw new ArgumentNullException("velocity", "Cannot divide with null AngularVelocity");
|
|
|
|
if (0.0 == velocity.RadiansPerSec)
|
|
throw new DivideByZeroException("Cannot divide angle by 0 angular velocity");
|
|
|
|
return PrecisionTimeSpan.FromSeconds(angle.Radians / velocity.RadiansPerSec);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region parsing
|
|
|
|
public static Angle Parse(string s)
|
|
{
|
|
return Common.Parse(s, _unitInfo);
|
|
}
|
|
|
|
public static bool TryParse(string s, out Angle value)
|
|
{
|
|
try
|
|
{
|
|
value = Angle.Parse(s);
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
value = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Returns a string appended with default units (e.g. "1.23 m/sec^2").
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A string that represents this instance
|
|
/// </returns>
|
|
public override string ToString()
|
|
{
|
|
return ToString(DefaultUnits);
|
|
}
|
|
|
|
/// <summary>
|
|
/// overload that returns a string appended with specified units (e.g. "1.23 ft/sec^2").
|
|
/// </summary>
|
|
/// <param name="units"></param>
|
|
/// <returns></returns>
|
|
public string ToString(Unit units)
|
|
{
|
|
double value = (double)_unitInfo[units].PropertyGetter.Invoke(this, null);
|
|
|
|
return $"{value} {_unitInfo[units].Abbreviation[0]}";
|
|
}
|
|
}
|
|
}
|