2 // System.Windows.Media3D.Matrix3D struct
5 // Moonlight List (moonlight-list@lists.ximian.com)
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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.
31 namespace System
.Windows
.Media3D
{
33 public unsafe struct Matrix3D
: IFormattable
{
47 private double offset_x
;
48 private double offset_y
;
49 private double offset_z
;
52 // we can't have an empty ctor for a struct (CS0568) but the default matrix is identity
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
);
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
)
109 if (!init
) SetIdentity ();
113 if (!init
) SetIdentity ();
120 if (!init
) SetIdentity ();
124 if (!init
) SetIdentity ();
131 if (!init
) SetIdentity ();
135 if (!init
) SetIdentity ();
142 if (!init
) SetIdentity ();
146 if (!init
) SetIdentity ();
153 if (!init
) SetIdentity ();
157 if (!init
) SetIdentity ();
164 if (!init
) SetIdentity ();
168 if (!init
) SetIdentity ();
175 if (!init
) SetIdentity ();
179 if (!init
) SetIdentity ();
186 if (!init
) SetIdentity ();
190 if (!init
) SetIdentity ();
197 if (!init
) SetIdentity ();
201 if (!init
) SetIdentity ();
208 if (!init
) SetIdentity ();
212 if (!init
) SetIdentity ();
219 if (!init
) SetIdentity ();
223 if (!init
) SetIdentity ();
230 if (!init
) SetIdentity ();
234 if (!init
) SetIdentity ();
239 public double OffsetX
{
241 if (!init
) SetIdentity ();
245 if (!init
) SetIdentity ();
250 public double OffsetY
{
252 if (!init
) SetIdentity ();
256 if (!init
) SetIdentity ();
261 public double OffsetZ
{
263 if (!init
) SetIdentity ();
267 if (!init
) SetIdentity ();
274 if (!init
) SetIdentity ();
278 if (!init
) SetIdentity ();
283 public bool IsIdentity
{
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 ()
318 public override int GetHashCode ()
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 ()
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
)
351 if (String
.IsNullOrEmpty (value))
354 if (formatProvider
!= null) {
355 ICustomFormatter cp
= (ICustomFormatter
) formatProvider
.GetFormat (typeof (ICustomFormatter
));
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}",
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
)))
392 return Equals ((Matrix3D
)o
);
395 public bool Equals (Matrix3D
value)
397 return (this == value);
400 public static bool operator == (Matrix3D matrix1
, Matrix3D matrix2
)
405 matrix1
.SetIdentity ();
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 (); }