CurveKey.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // MIT License - Copyright (C) The Mono.Xna Team
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. using System;
  5. using System.ComponentModel;
  6. using System.Runtime.Serialization;
  7. namespace CommonLang.Geometry
  8. {
  9. /// <summary>
  10. /// Key point on the <see cref="Curve"/>.
  11. /// </summary>
  12. // TODO : [TypeConverter(typeof(ExpandableObjectConverter))]
  13. public class CurveKey : IEquatable<CurveKey>, IComparable<CurveKey>
  14. {
  15. #region Private Fields
  16. private CurveContinuity _continuity;
  17. private readonly float _position;
  18. private float _tangentIn;
  19. private float _tangentOut;
  20. private float _value;
  21. #endregion
  22. #region Properties
  23. /// <summary>
  24. /// Gets or sets the indicator whether the segment between this point and the next point on the curve is discrete or continuous.
  25. /// </summary>
  26. public CurveContinuity Continuity
  27. {
  28. get { return this._continuity; }
  29. set { this._continuity = value; }
  30. }
  31. /// <summary>
  32. /// Gets a position of the key on the curve.
  33. /// </summary>
  34. public float Position
  35. {
  36. get { return this._position; }
  37. }
  38. /// <summary>
  39. /// Gets or sets a tangent when approaching this point from the previous point on the curve.
  40. /// </summary>
  41. public float TangentIn
  42. {
  43. get { return this._tangentIn; }
  44. set { this._tangentIn = value; }
  45. }
  46. /// <summary>
  47. /// Gets or sets a tangent when leaving this point to the next point on the curve.
  48. /// </summary>
  49. public float TangentOut
  50. {
  51. get { return this._tangentOut; }
  52. set { this._tangentOut = value; }
  53. }
  54. /// <summary>
  55. /// Gets a value of this point.
  56. /// </summary>
  57. public float Value
  58. {
  59. get { return this._value; }
  60. set { this._value = value; }
  61. }
  62. #endregion
  63. #region Constructors
  64. /// <summary>
  65. /// Creates a new instance of <see cref="CurveKey"/> class with position: 0 and value: 0.
  66. /// </summary>
  67. public CurveKey() : this(0, 0)
  68. {
  69. // This parameterless constructor is needed for correct serialization of CurveKeyCollection and CurveKey.
  70. }
  71. /// <summary>
  72. /// Creates a new instance of <see cref="CurveKey"/> class.
  73. /// </summary>
  74. /// <param name="position">Position on the curve.</param>
  75. /// <param name="value">Value of the control point.</param>
  76. public CurveKey(float position, float value)
  77. : this(position, value, 0, 0, CurveContinuity.Smooth)
  78. {
  79. }
  80. /// <summary>
  81. /// Creates a new instance of <see cref="CurveKey"/> class.
  82. /// </summary>
  83. /// <param name="position">Position on the curve.</param>
  84. /// <param name="value">Value of the control point.</param>
  85. /// <param name="tangentIn">Tangent approaching point from the previous point on the curve.</param>
  86. /// <param name="tangentOut">Tangent leaving point toward next point on the curve.</param>
  87. public CurveKey(float position, float value, float tangentIn, float tangentOut)
  88. : this(position, value, tangentIn, tangentOut, CurveContinuity.Smooth)
  89. {
  90. }
  91. /// <summary>
  92. /// Creates a new instance of <see cref="CurveKey"/> class.
  93. /// </summary>
  94. /// <param name="position">Position on the curve.</param>
  95. /// <param name="value">Value of the control point.</param>
  96. /// <param name="tangentIn">Tangent approaching point from the previous point on the curve.</param>
  97. /// <param name="tangentOut">Tangent leaving point toward next point on the curve.</param>
  98. /// <param name="continuity">Indicates whether the curve is discrete or continuous.</param>
  99. public CurveKey(float position, float value, float tangentIn, float tangentOut, CurveContinuity continuity)
  100. {
  101. this._position = position;
  102. this._value = value;
  103. this._tangentIn = tangentIn;
  104. this._tangentOut = tangentOut;
  105. this._continuity = continuity;
  106. }
  107. #endregion
  108. /// <summary>
  109. ///
  110. /// Compares whether two <see cref="CurveKey"/> instances are not equal.
  111. /// </summary>
  112. /// <param name="value1"><see cref="CurveKey"/> instance on the left of the not equal sign.</param>
  113. /// <param name="value2"><see cref="CurveKey"/> instance on the right of the not equal sign.</param>
  114. /// <returns><c>true</c> if the instances are not equal; <c>false</c> otherwise.</returns>
  115. public static bool operator !=(CurveKey value1, CurveKey value2)
  116. {
  117. return !(value1 == value2);
  118. }
  119. /// <summary>
  120. /// Compares whether two <see cref="CurveKey"/> instances are equal.
  121. /// </summary>
  122. /// <param name="value1"><see cref="CurveKey"/> instance on the left of the equal sign.</param>
  123. /// <param name="value2"><see cref="CurveKey"/> instance on the right of the equal sign.</param>
  124. /// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
  125. public static bool operator ==(CurveKey value1, CurveKey value2)
  126. {
  127. if (object.Equals(value1, null))
  128. return object.Equals(value2, null);
  129. if (object.Equals(value2, null))
  130. return object.Equals(value1, null);
  131. return (value1._position == value2._position)
  132. && (value1._value == value2._value)
  133. && (value1._tangentIn == value2._tangentIn)
  134. && (value1._tangentOut == value2._tangentOut)
  135. && (value1._continuity == value2._continuity);
  136. }
  137. /// <summary>
  138. /// Creates a copy of this key.
  139. /// </summary>
  140. /// <returns>A copy of this key.</returns>
  141. public CurveKey Clone()
  142. {
  143. return new CurveKey(this._position, this._value, this._tangentIn, this._tangentOut, this._continuity);
  144. }
  145. #region Inherited Methods
  146. public int CompareTo(CurveKey other)
  147. {
  148. return this._position.CompareTo(other._position);
  149. }
  150. public bool Equals(CurveKey other)
  151. {
  152. return (this == other);
  153. }
  154. public override bool Equals(object obj)
  155. {
  156. return (obj as CurveKey) != null && Equals((CurveKey)obj);
  157. }
  158. public override int GetHashCode()
  159. {
  160. return this._position.GetHashCode() ^ this._value.GetHashCode() ^ this._tangentIn.GetHashCode() ^
  161. this._tangentOut.GetHashCode() ^ this._continuity.GetHashCode();
  162. }
  163. #endregion
  164. }
  165. }