PointH.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. namespace VisualMath.Accord.Imaging
  2. {
  3. using System.Drawing;
  4. /// <summary>
  5. /// Represents an ordered pair of real x- and y-coordinates and scalar w that defines
  6. /// a point in a two-dimensional plane using homogeneous coordinates.
  7. /// </summary>
  8. ///
  9. /// <remarks>
  10. /// <para>
  11. /// In mathematics, homogeneous coordinates are a system of coordinates used in
  12. /// projective geometry much as Cartesian coordinates are used in Euclidean geometry.</para>
  13. /// <para>
  14. /// They have the advantage that the coordinates of a point, even those at infinity,
  15. /// can be represented using finite coordinates. Often formulas involving homogeneous
  16. /// coordinates are simpler and more symmetric than their Cartesian counterparts.</para>
  17. /// <para>
  18. /// Homogeneous coordinates have a range of applications, including computer graphics,
  19. /// where they allow affine transformations and, in general, projective transformations
  20. /// to be easily represented by a matrix.</para>
  21. ///
  22. /// <para>
  23. /// References:
  24. /// <list type="bullet">
  25. /// <item><description>
  26. /// http://alumnus.caltech.edu/~woody/docs/3dmatrix.html</description></item>
  27. /// <item><description>
  28. /// http://simply3d.wordpress.com/2009/05/29/homogeneous-coordinates/</description></item>
  29. /// </list></para>
  30. /// </remarks>
  31. ///
  32. public struct PointH
  33. {
  34. private float px, py, pw;
  35. /// <summary>
  36. /// The first coordinate.
  37. /// </summary>
  38. public float X
  39. {
  40. get { return px; }
  41. set { px = value; }
  42. }
  43. /// <summary>
  44. /// The second coordinate.
  45. /// </summary>
  46. public float Y
  47. {
  48. get { return py; }
  49. set { py = value; }
  50. }
  51. /// <summary>
  52. /// The inverse scaling factor for X and Y.
  53. /// </summary>
  54. public float W
  55. {
  56. get { return pw; }
  57. set { pw = value; }
  58. }
  59. /// <summary>
  60. /// Creates a new point.
  61. /// </summary>
  62. public PointH(float x, float y)
  63. {
  64. px = x;
  65. py = y;
  66. pw = 1;
  67. }
  68. /// <summary>
  69. /// Creates a new point.
  70. /// </summary>
  71. public PointH(float x, float y, float w)
  72. {
  73. px = x;
  74. py = y;
  75. pw = w;
  76. }
  77. /// <summary>
  78. /// Transforms a point using a projection matrix.
  79. /// </summary>
  80. public void Transform(float[,] matrix)
  81. {
  82. px = matrix[0, 0] * px + matrix[0, 1] * py + matrix[0, 2] * pw;
  83. py = matrix[1, 0] * px + matrix[1, 1] * py + matrix[1, 2] * pw;
  84. pw = matrix[2, 0] * px + matrix[2, 1] * py + matrix[2, 2] * pw;
  85. }
  86. /// <summary>
  87. /// Normalizes the point to have unit scale.
  88. /// </summary>
  89. public void Normalize()
  90. {
  91. px = px / pw;
  92. py = py / pw;
  93. pw = 1;
  94. }
  95. /// <summary>
  96. /// Gets whether this point is normalized (w = 1).
  97. /// </summary>
  98. public bool IsNormalized
  99. {
  100. get { return pw == 1f; }
  101. }
  102. /// <summary>
  103. /// Gets whether this point is at infinity (w = 0).
  104. /// </summary>
  105. public bool IsAtInfinity
  106. {
  107. get { return pw == 0f; }
  108. }
  109. /// <summary>
  110. /// Gets whether this point is at the origin.
  111. /// </summary>
  112. public bool IsEmpty
  113. {
  114. get { return px == 0 && py == 0; }
  115. }
  116. /// <summary>
  117. /// Converts the point to a array representation.
  118. /// </summary>
  119. public double[] ToArray()
  120. {
  121. return new double[] { px, py, pw };
  122. }
  123. /// <summary>
  124. /// Multiplication by scalar.
  125. /// </summary>
  126. public static PointH operator *(PointH a, float b)
  127. {
  128. return new PointH(b * a.X, b * a.Y, b * a.W);
  129. }
  130. /// <summary>
  131. /// Multiplication by scalar.
  132. /// </summary>
  133. public static PointH operator *(float b, PointH a)
  134. {
  135. return a * b;
  136. }
  137. /// <summary>
  138. /// Subtraction.
  139. /// </summary>
  140. public static PointH operator -(PointH a, PointH b)
  141. {
  142. return new PointH(a.X - b.X, a.Y - b.Y, a.W - b.W);
  143. }
  144. /// <summary>
  145. /// Addition.
  146. /// </summary>
  147. public static PointH operator +(PointH a, PointH b)
  148. {
  149. return new PointH(a.X + b.X, a.Y + b.Y, a.W + b.W);
  150. }
  151. /// <summary>
  152. /// Equality
  153. /// </summary>
  154. public static bool operator ==(PointH a, PointH b)
  155. {
  156. return (a.px / a.pw == b.px / b.pw && a.py / a.pw == b.py / b.pw);
  157. }
  158. /// <summary>
  159. /// Inequality
  160. /// </summary>
  161. public static bool operator !=(PointH a, PointH b)
  162. {
  163. return (a.px / a.pw != b.px / b.pw || a.py / a.pw != b.py / b.pw);
  164. }
  165. /// <summary>
  166. /// PointF Conversion
  167. /// </summary>
  168. public static implicit operator PointF(PointH a)
  169. {
  170. return new PointF((float)(a.px / a.pw), (float)(a.py / a.pw));
  171. }
  172. /// <summary>
  173. /// Converts to a Integer point by computing the ceiling of the point coordinates.
  174. /// </summary>
  175. public static Point Ceiling(PointH point)
  176. {
  177. return new Point(
  178. (int)System.Math.Ceiling(point.px / point.pw),
  179. (int)System.Math.Ceiling(point.py / point.pw));
  180. }
  181. /// <summary>
  182. /// Converts to a Integer point by rounding the point coordinates.
  183. /// </summary>
  184. public static Point Round(PointH point)
  185. {
  186. return new Point(
  187. (int)System.Math.Round(point.px / point.pw),
  188. (int)System.Math.Round(point.py / point.pw));
  189. }
  190. /// <summary>
  191. /// Converts to a Integer point by truncating the point coordinates.
  192. /// </summary>
  193. public static Point Truncate(PointH point)
  194. {
  195. return new Point(
  196. (int)System.Math.Truncate(point.px / point.pw),
  197. (int)System.Math.Truncate(point.py / point.pw));
  198. }
  199. /// <summary>
  200. /// Compares two objects for equality.
  201. /// </summary>
  202. public override bool Equals(object obj)
  203. {
  204. if (obj is PointH)
  205. {
  206. PointH p = (PointH)obj;
  207. if (px / pw == p.px / p.pw &&
  208. py / pw == p.py / p.pw)
  209. return true;
  210. }
  211. return false;
  212. }
  213. /// <summary>
  214. /// Returns the hash code for this instance.
  215. /// </summary>
  216. public override int GetHashCode()
  217. {
  218. return px.GetHashCode() ^ py.GetHashCode() ^ pw.GetHashCode();
  219. }
  220. /// <summary>
  221. /// Returns the empty point.
  222. /// </summary>
  223. public static readonly PointH Empty = new PointH(0, 0, 1);
  224. }
  225. }