Matrix.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // System.Drawing.Drawing2D.Matrix.cs
  3. //
  4. // Authors:
  5. // Stefan Maierhofer <[email protected]>
  6. // Dennis Hayes ([email protected])
  7. // Duncan Mak ([email protected])
  8. // Ravindra ([email protected])
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Runtime.InteropServices;
  33. namespace System.Drawing.Drawing2D
  34. {
  35. public sealed class Matrix : MarshalByRefObject, IDisposable
  36. {
  37. internal IntPtr nativeMatrix;
  38. // constructors
  39. internal Matrix (IntPtr ptr)
  40. {
  41. nativeMatrix = ptr;
  42. }
  43. public Matrix ()
  44. {
  45. Status status = GDIPlus.GdipCreateMatrix (out nativeMatrix);
  46. GDIPlus.CheckStatus (status);
  47. }
  48. public Matrix (Rectangle rect, Point[] plgpts)
  49. {
  50. if (plgpts == null)
  51. throw new ArgumentNullException ("plgpts");
  52. if (plgpts.Length != 3)
  53. throw new ArgumentException ("plgpts");
  54. Status status = GDIPlus.GdipCreateMatrix3I (ref rect, plgpts, out nativeMatrix);
  55. GDIPlus.CheckStatus (status);
  56. }
  57. public Matrix (RectangleF rect, PointF[] plgpts)
  58. {
  59. if (plgpts == null)
  60. throw new ArgumentNullException ("plgpts");
  61. if (plgpts.Length != 3)
  62. throw new ArgumentException ("plgpts");
  63. Status status = GDIPlus.GdipCreateMatrix3 (ref rect, plgpts, out nativeMatrix);
  64. GDIPlus.CheckStatus (status);
  65. }
  66. public Matrix (float m11, float m12, float m21, float m22, float dx, float dy)
  67. {
  68. Status status = GDIPlus.GdipCreateMatrix2 (m11, m12, m21, m22, dx, dy, out nativeMatrix);
  69. GDIPlus.CheckStatus (status);
  70. }
  71. // properties
  72. public float[] Elements {
  73. get {
  74. float [] retval = new float [6];
  75. IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
  76. try {
  77. Status status = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
  78. GDIPlus.CheckStatus (status);
  79. Marshal.Copy (tmp, retval, 0, 6);
  80. }
  81. finally {
  82. Marshal.FreeHGlobal (tmp);
  83. }
  84. return retval;
  85. }
  86. }
  87. public bool IsIdentity {
  88. get {
  89. bool retval;
  90. Status status = GDIPlus.GdipIsMatrixIdentity (nativeMatrix, out retval);
  91. GDIPlus.CheckStatus (status);
  92. return retval;
  93. }
  94. }
  95. public bool IsInvertible {
  96. get {
  97. bool retval;
  98. Status status = GDIPlus.GdipIsMatrixInvertible (nativeMatrix, out retval);
  99. GDIPlus.CheckStatus (status);
  100. return retval;
  101. }
  102. }
  103. public float OffsetX {
  104. get {
  105. return this.Elements [4];
  106. }
  107. }
  108. public float OffsetY {
  109. get {
  110. return this.Elements [5];
  111. }
  112. }
  113. public Matrix Clone()
  114. {
  115. IntPtr retval;
  116. Status status = GDIPlus.GdipCloneMatrix (nativeMatrix, out retval);
  117. GDIPlus.CheckStatus (status);
  118. return new Matrix (retval);
  119. }
  120. public void Dispose ()
  121. {
  122. if (nativeMatrix != IntPtr.Zero) {
  123. Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
  124. GDIPlus.CheckStatus (status);
  125. nativeMatrix = IntPtr.Zero;
  126. }
  127. GC.SuppressFinalize (this);
  128. }
  129. public override bool Equals (object obj)
  130. {
  131. Matrix m = obj as Matrix;
  132. if (m != null) {
  133. bool retval;
  134. Status status = GDIPlus.GdipIsMatrixEqual (nativeMatrix, m.nativeMatrix, out retval);
  135. GDIPlus.CheckStatus (status);
  136. return retval;
  137. } else
  138. return false;
  139. }
  140. ~Matrix()
  141. {
  142. Dispose ();
  143. }
  144. public override int GetHashCode ()
  145. {
  146. return base.GetHashCode ();
  147. }
  148. public void Invert ()
  149. {
  150. Status status = GDIPlus.GdipInvertMatrix (nativeMatrix);
  151. GDIPlus.CheckStatus (status);
  152. }
  153. public void Multiply (Matrix matrix)
  154. {
  155. Multiply (matrix, MatrixOrder.Prepend);
  156. }
  157. public void Multiply (Matrix matrix, MatrixOrder order)
  158. {
  159. if (matrix == null)
  160. throw new ArgumentNullException ("matrix");
  161. Status status = GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
  162. GDIPlus.CheckStatus (status);
  163. }
  164. public void Reset()
  165. {
  166. Status status = GDIPlus.GdipSetMatrixElements (nativeMatrix, 1, 0, 0, 1, 0, 0);
  167. GDIPlus.CheckStatus (status);
  168. }
  169. public void Rotate (float angle)
  170. {
  171. Rotate (angle, MatrixOrder.Prepend);
  172. }
  173. public void Rotate (float angle, MatrixOrder order)
  174. {
  175. Status status = GDIPlus.GdipRotateMatrix (nativeMatrix, angle, order);
  176. GDIPlus.CheckStatus (status);
  177. }
  178. public void RotateAt (float angle, PointF point)
  179. {
  180. RotateAt (angle, point, MatrixOrder.Prepend);
  181. }
  182. public void RotateAt (float angle, PointF point, MatrixOrder order)
  183. {
  184. if ((order < MatrixOrder.Prepend) || (order > MatrixOrder.Append))
  185. throw new ArgumentException ("order");
  186. angle *= (float) (Math.PI / 180.0); // degrees to radians
  187. float cos = (float) Math.Cos (angle);
  188. float sin = (float) Math.Sin (angle);
  189. float e4 = -point.X * cos + point.Y * sin + point.X;
  190. float e5 = -point.X * sin - point.Y * cos + point.Y;
  191. float[] m = this.Elements;
  192. Status status;
  193. if (order == MatrixOrder.Prepend)
  194. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  195. cos * m[0] + sin * m[2],
  196. cos * m[1] + sin * m[3],
  197. -sin * m[0] + cos * m[2],
  198. -sin * m[1] + cos * m[3],
  199. e4 * m[0] + e5 * m[2] + m[4],
  200. e4 * m[1] + e5 * m[3] + m[5]);
  201. else
  202. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  203. m[0] * cos + m[1] * -sin,
  204. m[0] * sin + m[1] * cos,
  205. m[2] * cos + m[3] * -sin,
  206. m[2] * sin + m[3] * cos,
  207. m[4] * cos + m[5] * -sin + e4,
  208. m[4] * sin + m[5] * cos + e5);
  209. GDIPlus.CheckStatus (status);
  210. }
  211. public void Scale (float scaleX, float scaleY)
  212. {
  213. Scale (scaleX, scaleY, MatrixOrder.Prepend);
  214. }
  215. public void Scale (float scaleX, float scaleY, MatrixOrder order)
  216. {
  217. Status status = GDIPlus.GdipScaleMatrix (nativeMatrix, scaleX, scaleY, order);
  218. GDIPlus.CheckStatus (status);
  219. }
  220. public void Shear (float shearX, float shearY)
  221. {
  222. Shear (shearX, shearY, MatrixOrder.Prepend);
  223. }
  224. public void Shear (float shearX, float shearY, MatrixOrder order)
  225. {
  226. Status status = GDIPlus.GdipShearMatrix (nativeMatrix, shearX, shearY, order);
  227. GDIPlus.CheckStatus (status);
  228. }
  229. public void TransformPoints (Point[] pts)
  230. {
  231. if (pts == null)
  232. throw new ArgumentNullException ("pts");
  233. Status status = GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  234. GDIPlus.CheckStatus (status);
  235. }
  236. public void TransformPoints (PointF[] pts)
  237. {
  238. if (pts == null)
  239. throw new ArgumentNullException ("pts");
  240. Status status = GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  241. GDIPlus.CheckStatus (status);
  242. }
  243. public void TransformVectors (Point[] pts)
  244. {
  245. if (pts == null)
  246. throw new ArgumentNullException ("pts");
  247. Status status = GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  248. GDIPlus.CheckStatus (status);
  249. }
  250. public void TransformVectors (PointF[] pts)
  251. {
  252. if (pts == null)
  253. throw new ArgumentNullException ("pts");
  254. Status status = GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  255. GDIPlus.CheckStatus (status);
  256. }
  257. public void Translate (float offsetX, float offsetY)
  258. {
  259. Translate (offsetX, offsetY, MatrixOrder.Prepend);
  260. }
  261. public void Translate (float offsetX, float offsetY, MatrixOrder order)
  262. {
  263. Status status = GDIPlus.GdipTranslateMatrix (nativeMatrix, offsetX, offsetY, order);
  264. GDIPlus.CheckStatus (status);
  265. }
  266. public void VectorTransformPoints (Point[] pts)
  267. {
  268. TransformVectors (pts);
  269. }
  270. internal IntPtr NativeObject
  271. {
  272. get{
  273. return nativeMatrix;
  274. }
  275. set {
  276. nativeMatrix = value;
  277. }
  278. }
  279. }
  280. }