stub out the Projection classes as well as duplicating the disgusting hack used for...
[moon.git] / class / System.Windows / System.Windows.Media3D / Matrix3D.cs
bloba8b127f2b280058e299d10e3f174668b95e1b7b0
1 //
2 // System.Windows.Media3D.Matrix3D struct
3 //
4 // Contact:
5 // Moonlight List (moonlight-list@lists.ximian.com)
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using Mono;
31 namespace System.Windows.Media3D {
33 public unsafe struct Matrix3D : IFormattable {
35 private double m_11;
36 private double m_12;
37 private double m_13;
38 private double m_14;
39 private double m_21;
40 private double m_22;
41 private double m_23;
42 private double m_24;
43 private double m_31;
44 private double m_32;
45 private double m_33;
46 private double m_34;
47 private double offset_x;
48 private double offset_y;
49 private double offset_z;
50 private double m_44;
52 // we can't have an empty ctor for a struct (CS0568) but the default matrix is identity
53 private bool init;
56 internal unsafe Matrix3D (IntPtr native)
58 if (native == IntPtr.Zero)
59 throw new ArgumentNullException ("native");
61 // FIXME: the generator butchers this pinvoke's name..
62 double *dp = (double*) NativeMethods.matrix3_d_get_matrix_values (native);
63 m_11 = dp [0];
64 m_12 = dp [1];
65 m_13 = dp [2];
66 m_14 = dp [3];
67 m_21 = dp [4];
68 m_22 = dp [5];
69 m_23 = dp [6];
70 m_24 = dp [7];
71 m_31 = dp [8];
72 m_32 = dp [9];
73 m_33 = dp [10];
74 m_34 = dp [11];
75 offset_x = dp [12];
76 offset_y = dp [13];
77 offset_z = dp [14];
78 m_44 = dp [15];
79 init = true;
82 public Matrix3D (double m11, double m12, double m13, double m14,
83 double m21, double m22, double m23, double m24,
84 double m31, double m32, double m33, double m34,
85 double offsetX, double offsetY, double offsetZ, double m44)
87 m_11 = m11;
88 m_12 = m12;
89 m_13 = m13;
90 m_14 = m14;
91 m_21 = m21;
92 m_22 = m22;
93 m_23 = m23;
94 m_24 = m24;
95 m_31 = m31;
96 m_32 = m32;
97 m_33 = m33;
98 m_34 = m34;
99 offset_x = offsetX;
100 offset_y = offsetY;
101 offset_z = offsetZ;
102 m_44 = m44;
103 init = true;
107 public double M11 {
108 get {
109 if (!init) SetIdentity ();
110 return m_11;
112 set {
113 if (!init) SetIdentity ();
114 m_11 = value;
118 public double M12 {
119 get {
120 if (!init) SetIdentity ();
121 return m_12;
123 set {
124 if (!init) SetIdentity ();
125 m_12 = value;
129 public double M13 {
130 get {
131 if (!init) SetIdentity ();
132 return m_13;
134 set {
135 if (!init) SetIdentity ();
136 m_13 = value;
140 public double M14 {
141 get {
142 if (!init) SetIdentity ();
143 return m_14;
145 set {
146 if (!init) SetIdentity ();
147 m_14 = value;
151 public double M21 {
152 get {
153 if (!init) SetIdentity ();
154 return m_21;
156 set {
157 if (!init) SetIdentity ();
158 m_21 = value;
162 public double M22 {
163 get {
164 if (!init) SetIdentity ();
165 return m_22;
167 set {
168 if (!init) SetIdentity ();
169 m_22 = value;
173 public double M23 {
174 get {
175 if (!init) SetIdentity ();
176 return m_23;
178 set {
179 if (!init) SetIdentity ();
180 m_23 = value;
184 public double M24 {
185 get {
186 if (!init) SetIdentity ();
187 return m_24;
189 set {
190 if (!init) SetIdentity ();
191 m_24 = value;
195 public double M31 {
196 get {
197 if (!init) SetIdentity ();
198 return m_31;
200 set {
201 if (!init) SetIdentity ();
202 m_31 = value;
206 public double M32 {
207 get {
208 if (!init) SetIdentity ();
209 return m_32;
211 set {
212 if (!init) SetIdentity ();
213 m_32 = value;
217 public double M33 {
218 get {
219 if (!init) SetIdentity ();
220 return m_33;
222 set {
223 if (!init) SetIdentity ();
224 m_33 = value;
228 public double M34 {
229 get {
230 if (!init) SetIdentity ();
231 return m_34;
233 set {
234 if (!init) SetIdentity ();
235 m_34 = value;
239 public double OffsetX {
240 get {
241 if (!init) SetIdentity ();
242 return offset_x;
244 set {
245 if (!init) SetIdentity ();
246 offset_x = value;
250 public double OffsetY {
251 get {
252 if (!init) SetIdentity ();
253 return offset_y;
255 set {
256 if (!init) SetIdentity ();
257 offset_y = value;
261 public double OffsetZ {
262 get {
263 if (!init) SetIdentity ();
264 return offset_z;
266 set {
267 if (!init) SetIdentity ();
268 offset_z = value;
272 public double M44 {
273 get {
274 if (!init) SetIdentity ();
275 return m_44;
277 set {
278 if (!init) SetIdentity ();
279 m_44 = value;
283 public bool IsIdentity {
284 get {
285 if (!init)
286 return true;
288 return ((m_11 == 1.0 && m_12 == 0.0 && m_13 == 0.0 && m_14 == 0.0) &&
289 (m_21 == 0.0 && m_22 == 1.0 && m_23 == 0.0 && m_24 == 0.0) &&
290 (m_31 == 0.0 && m_32 == 0.0 && m_33 == 1.0 && m_34 == 0.0) &&
291 (offset_x == 0.0 && offset_y == 0.0 && offset_z == 0.0 && m_44 == 1.0));
296 private void SetIdentity ()
298 m_11 = 1.0;
299 m_12 = 0.0;
300 m_13 = 0.0;
301 m_14 = 0.0;
302 m_21 = 0.0;
303 m_22 = 1.0;
304 m_23 = 0.0;
305 m_24 = 0.0;
306 m_31 = 0.0;
307 m_32 = 0.0;
308 m_33 = 1.0;
309 m_34 = 0.0;
310 offset_x = 0.0;
311 offset_y = 0.0;
312 offset_z = 0.0;
313 m_44 = 1.0;
314 init = true;
318 public override int GetHashCode ()
320 if (IsIdentity)
321 return 0;
323 return (m_11.GetHashCode () ^ m_12.GetHashCode () ^ m_13.GetHashCode () ^ m_14.GetHashCode () ^
324 m_21.GetHashCode () ^ m_22.GetHashCode () ^ m_23.GetHashCode () ^ m_24.GetHashCode () ^
325 m_31.GetHashCode () ^ m_32.GetHashCode () ^ m_33.GetHashCode () ^ m_34.GetHashCode () ^
326 offset_x.GetHashCode () ^ offset_y.GetHashCode () ^ offset_z.GetHashCode () ^ m_44.GetHashCode ());
329 public override string ToString ()
331 if (IsIdentity)
332 return "Identity";
334 return String.Format ("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15}",
335 m_11, m_12, m_13, m_14,
336 m_21, m_22, m_23, m_24,
337 m_31, m_32, m_33, m_34,
338 offset_x, offset_y, offset_z, m_44);
341 public string ToString (IFormatProvider provider)
343 return (this as IFormattable).ToString (null, provider);
346 string IFormattable.ToString (string value, IFormatProvider formatProvider)
348 if (IsIdentity)
349 return "Identity";
351 if (String.IsNullOrEmpty (value))
352 value = null;
354 if (formatProvider != null) {
355 ICustomFormatter cp = (ICustomFormatter) formatProvider.GetFormat (typeof (ICustomFormatter));
356 if (cp != null) {
357 string comma = cp.Format (null, ',', formatProvider);
358 return String.Format ("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}{0}{9}{0}{10}{0}{11}{0}{12}{0}{13}{0}{14}{0}{15}{0}{16}",
359 comma,
360 cp.Format (value, m_11, formatProvider),
361 cp.Format (value, m_12, formatProvider),
362 cp.Format (value, m_13, formatProvider),
363 cp.Format (value, m_14, formatProvider),
364 cp.Format (value, m_21, formatProvider),
365 cp.Format (value, m_22, formatProvider),
366 cp.Format (value, m_23, formatProvider),
367 cp.Format (value, m_24, formatProvider),
368 cp.Format (value, m_31, formatProvider),
369 cp.Format (value, m_32, formatProvider),
370 cp.Format (value, m_33, formatProvider),
371 cp.Format (value, m_34, formatProvider),
372 cp.Format (value, offset_x, formatProvider),
373 cp.Format (value, offset_y, formatProvider),
374 cp.Format (value, offset_z, formatProvider),
375 cp.Format (value, m_44, formatProvider));
379 return String.Format ("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15}",
380 m_11.ToString (value, formatProvider), m_12.ToString (value, formatProvider), m_13.ToString (value, formatProvider), m_14.ToString (value, formatProvider),
381 m_21.ToString (value, formatProvider), m_22.ToString (value, formatProvider), m_23.ToString (value, formatProvider), m_24.ToString (value, formatProvider),
382 m_31.ToString (value, formatProvider), m_32.ToString (value, formatProvider), m_33.ToString (value, formatProvider), m_34.ToString (value, formatProvider),
383 offset_x.ToString (value, formatProvider), offset_y.ToString (value, formatProvider), offset_z.ToString (value, formatProvider), m_44.ToString (value, formatProvider));
386 // TODO comparing double is problematic, review MS precision
388 public override bool Equals (object o)
390 if ((o == null) || (!(o is Matrix3D)))
391 return false;
392 return Equals ((Matrix3D)o);
395 public bool Equals (Matrix3D value)
397 return (this == value);
400 public static bool operator == (Matrix3D matrix1, Matrix3D matrix2)
402 if (!matrix1.init) {
403 if (!matrix2.init)
404 return true;
405 matrix1.SetIdentity ();
407 if (!matrix2.init) {
408 matrix2.SetIdentity ();
411 return (matrix1.m_11 == matrix2.m_11 && matrix1.m_12 == matrix2.m_12 && matrix1.m_13 == matrix2.m_13 && matrix1.m_14 == matrix2.m_14 &&
412 matrix1.m_21 == matrix2.m_21 && matrix1.m_22 == matrix2.m_22 && matrix1.m_23 == matrix2.m_23 && matrix1.m_24 == matrix2.m_24 &&
413 matrix1.m_31 == matrix2.m_31 && matrix1.m_32 == matrix2.m_32 && matrix1.m_33 == matrix2.m_33 && matrix1.m_34 == matrix2.m_34 &&
414 matrix1.offset_x == matrix2.offset_x && matrix1.offset_y == matrix2.offset_y && matrix1.offset_z == matrix2.offset_z && matrix1.m_44 == matrix2.m_44);
417 public static bool operator != (Matrix3D matrix1, Matrix3D matrix2)
419 return !(matrix1 == matrix2);
423 public static Matrix3D Identity {
424 get { return new Matrix3D (); }