using System; using System.Runtime.Serialization; using System.Diagnostics.CodeAnalysis; using System.Collections.Generic; namespace Raytheon.Units { /// /// 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. /// [Serializable] [DataContract] public class Angle { public enum Unit { Degrees, Radians, Milliradians } public static IDictionary> UnitAbbreviations = new Dictionary>() { {Unit.Degrees, new List(){ "deg" } }, {Unit.Radians, new List(){ "rad" } }, {Unit.Milliradians, new List(){ "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> _unitInfo = new Dictionary>() { { Unit.Degrees, new UnitInfo(UnitAbbreviations[Unit.Degrees], FromDegrees, typeof(Angle).GetProperty("Degrees").GetGetMethod()) }, { Unit.Radians, new UnitInfo(UnitAbbreviations[Unit.Radians], FromRadians, typeof(Angle).GetProperty("Radians").GetGetMethod()) }, { Unit.Milliradians, new UnitInfo(UnitAbbreviations[Unit.Milliradians], FromMilliradians, typeof(Angle).GetProperty("Milliradians").GetGetMethod()) } }; /// /// Prevents a default instance of the class from being created. /// Use FromXXX methods to create objects of this type. /// /// No one should be calling this, it is only here to prevent users from creating default object private Angle() { throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object"); } /// /// ctor used by this class to create objects in FromXXX methods /// /// The angle - radians. private Angle(double angle) { _angle = angle; } #region conversion properties /// /// Returns degrees as type double /// /// /// degrees /// public double Degrees { get { return _angle * DegreesPerRadian; } } /// /// Returns milliradians as type double /// /// /// milliradians /// public double Milliradians { get { return _angle * MilliradiansPerRadian; } } /// /// Returns radians as type double /// /// /// radians /// public double Radians { get { return _angle; } } #endregion #region creation methods /// /// Returns Angle object interpreting passed value as degrees /// /// degrees /// public static Angle FromDegrees(double degrees) { return new Angle(degrees / DegreesPerRadian); } /// /// Returns Angle object interpreting passed value as milliradians /// /// milliradians /// public static Angle FromMilliradians(double milliradians) { return new Angle(milliradians / MilliradiansPerRadian); } /// /// Returns Angle object interpreting passed value as radians /// /// radians /// 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 /// /// Implements the operator / for AngularVelocity = Angle / PrecisionTimeSpan /// /// The angle. /// The time. /// /// AngularVelocity /// 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); } /// /// Implements the operator / for PrecisionTimeSpan = Angle / AngularVelocity /// /// The angle. /// The velocity. /// /// PrecisionTimeSpan /// 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 /// /// Returns a string appended with default units (e.g. "1.23 m/sec^2"). /// /// /// A string that represents this instance /// public override string ToString() { return ToString(DefaultUnits); } /// /// overload that returns a string appended with specified units (e.g. "1.23 ft/sec^2"). /// /// /// public string ToString(Unit units) { double value = (double)_unitInfo[units].PropertyGetter.Invoke(this, null); return $"{value} {_unitInfo[units].Abbreviation[0]}"; } } }