/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Matrix33.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/Matrix33.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 83 - (show annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (10 years, 1 month ago) by william
File size: 9542 byte(s)
bulk commit --
** Enable Support for Skeletons & Bones

** A bug has found its way into the code
 -get an unhandled EHAccessViolation
 - I think the problem is in the Textures Class (or one of the sub-classes)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace gr2lib.core.coretypes.implementation
8 {
9 [StructLayout(LayoutKind.Sequential)]
10 public struct Matrix33
11 {
12 public Vector3 right;
13 public Vector3 up;
14 public Vector3 at;
15 public static readonly Matrix33 Identity = new Matrix33(new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, 0f, 1f));
16
17 public Matrix33(Vector3 _right, Vector3 _up, Vector3 _at)
18 {
19 this.right = _right;
20 this.up = _up;
21 this.at = _at;
22 }
23
24 internal Matrix33(native.Matrix33 m)
25 {
26 this.right = new Vector3(m.Right.X, m.Right.Y, m.Right.Z);
27 this.up = new Vector3(m.Up.X, m.Up.Y, m.Up.Z);
28 this.at = new Vector3(m.Back.X, m.Back.Y, m.Back.Z);
29 }
30
31 public static Matrix33 CopyMatrix(Matrix33 m)
32 {
33 Matrix33 copy = new Matrix33();
34 copy.right = new Vector3(m.right.x, m.right.y, m.right.z);
35 copy.up = new Vector3(m.up.x, m.up.y, m.up.z);
36 copy.at = new Vector3(m.at.x, m.at.y, m.at.z);
37 return copy;
38 }
39
40 public float Determinant
41 {
42 get
43 {
44 return (((this.right.x * ((this.up.y * this.at.z) - (this.up.z * this.at.y))) + (this.right.y * ((this.up.z * this.at.x) - (this.up.x * this.at.z)))) + (this.right.z * ((this.up.x * this.at.y) - (this.up.y * this.at.x))));
45 }
46 }
47 public Matrix33 Transpose
48 {
49 get
50 {
51 return new Matrix33(new Vector3(this.right.x, this.up.x, this.at.x), new Vector3(this.right.y, this.up.y, this.at.y), new Vector3(this.right.z, this.up.z, this.at.z));
52 }
53 }
54 public Matrix33 Adjoint
55 {
56 get
57 {
58 Matrix33 matrix = new Matrix33();
59 matrix.right.x = (this.up.y * this.at.z) - (this.up.z * this.at.y);
60 matrix.up.x = (this.up.z * this.at.x) - (this.up.x * this.at.z);
61 matrix.at.x = (this.up.x * this.at.y) - (this.up.y * this.at.x);
62 matrix.right.y = (this.at.y * this.right.z) - (this.at.z * this.right.y);
63 matrix.up.y = (this.at.z * this.right.x) - (this.at.x * this.right.z);
64 matrix.at.y = (this.at.x * this.right.y) - (this.at.y * this.right.x);
65 matrix.right.z = (this.right.y * this.up.z) - (this.right.z * this.up.y);
66 matrix.up.z = (this.right.z * this.up.x) - (this.right.x * this.up.z);
67 matrix.at.z = (this.right.x * this.up.y) - (this.right.y * this.up.x);
68 return matrix;
69 }
70 }
71 public Matrix33 Inverse
72 {
73 get
74 {
75 return (Matrix33)(this.Adjoint * (1f / this.Determinant));
76 }
77 }
78 public bool IsIdentity()
79 {
80 return (this.Equals(Matrix33.Identity));
81 }
82
83 public double Magnitude()
84 {
85 return Math.Sqrt(this.right.LengthSqr() + this.up.LengthSqr() + this.at.LengthSqr());
86 }
87
88 public void SetIdentity()
89 {
90 this.right = new Vector3(1f, 0f, 0f);
91 this.up = new Vector3(0f, 1f, 0f);
92 this.at = new Vector3(0f, 0f, 1f);
93 }
94
95 public void SetRotation(Quaternion q)
96 {
97 float len = q.Magnitude;
98 q = q / len;
99 this.right.Set(1 - 2 * (q.v.y * q.v.y + q.v.z * q.v.z), 2 * (q.v.x * q.v.y - q.v.z * q.n), 2 * (q.v.x * q.v.z + q.v.y * q.n));
100 this.up.Set(2 * (q.v.x * q.v.y + q.v.z * q.n), 1 - 2 * (q.v.x * q.v.x + q.v.z * q.v.z), 2 * (q.v.y * q.v.z - q.v.x * q.n));
101 this.at.Set(2 * (q.v.x * q.v.z - q.v.y * q.n), 2 * (q.v.y * q.v.z + q.v.x * q.n), 1 - 2 * (q.v.x * q.v.x + q.v.y * q.v.y));
102 this.right *= len;
103 this.up *= len;
104 this.at *= len;
105 }
106
107 public void SetRotation(Vector3 axis, float angle)
108 {
109 float len = axis.Length();
110 axis.Normalize();
111 float cos = (float)Math.Cos(angle);
112 float sin = (float)Math.Sin(angle);
113 this.right.Set(axis.x * axis.x + (1 - axis.x * axis.x) * cos, axis.x * axis.y * (1 - cos) - axis.z * sin, axis.x * axis.z * (1 - cos) + axis.y * sin);
114 this.up.Set(axis.x * axis.y * (1 - cos) + axis.z * sin, axis.y * axis.y + (1 - axis.y * axis.y) * cos, axis.y * axis.z * (1 - cos) - axis.x * sin);
115 this.at.Set(axis.x * axis.z * (1 - cos) - axis.y * sin, axis.y * axis.z * (1 - cos) + axis.x * sin, axis.z * axis.z + (1 - axis.z * axis.z) * cos);
116 this.right *= len;
117 this.up *= len;
118 this.at *= len;
119 }
120
121 public void SetRotation(Vector4 raxis)
122 {
123 SetRotation(raxis.V3, raxis.w);
124 }
125
126 public void SetRotationX(float rads)
127 {
128 float sin = (float)Math.Sin((double)rads);
129 float cos = (float)Math.Cos((double)rads);
130 this.right.Set(1f, 0f, 0f);
131 this.up.Set(0f, cos, -sin);
132 this.at.Set(0f, sin, cos);
133 }
134
135 public void SetRotationY(float rads)
136 {
137 float sin = (float)Math.Sin((double)rads);
138 float cos = (float)Math.Cos((double)rads);
139 this.right.Set(cos, 0f, sin);
140 this.up.Set(0f, 1f, 0f);
141 this.at.Set(-sin, 0f, cos);
142 }
143
144 public void SetRotationZ(float rads)
145 {
146 float sin = (float)Math.Sin((double)rads);
147 float cos = (float)Math.Cos((double)rads);
148 this.right.Set(cos, -sin, 0f);
149 this.up.Set(sin, cos, 0f);
150 this.at.Set(0f, 0f, 1f);
151 }
152
153 public override bool Equals(object obj)
154 {
155 if ((obj == null) || (obj.GetType() != base.GetType()))
156 {
157 return false;
158 }
159 Matrix33 matrix = (Matrix33)obj;
160 return (this.right.Equals(matrix.right) && this.up.Equals(matrix.up) && this.at.Equals(matrix.at));
161 }
162
163 public override int GetHashCode()
164 {
165 return (this.right.GetHashCode() ^ this.up.GetHashCode() ^ this.at.GetHashCode());
166 }
167
168 public override string ToString()
169 {
170 StringBuilder builder = new StringBuilder();
171 builder.Append("r:[");
172 builder.Append(this.right.ToString());
173 builder.Append("] u:[");
174 builder.Append(this.up.ToString());
175 builder.Append("] a:[");
176 builder.Append(this.at.ToString());
177 builder.Append("]");
178 return builder.ToString();
179 }
180
181 public Matrix44 ToMatrix44()
182 {
183 Matrix44 matrix = new Matrix44();
184 matrix.right = new Vector4(this.right.x, this.right.y, this.right.z, 0f);
185 matrix.up = new Vector4(this.up.x, this.up.y, this.up.z, 0f);
186 matrix.at = new Vector4(this.at.x, this.at.y, this.at.z, 0f);
187 matrix.pos = new Vector4(0f, 0f, 0f, 1f);
188 return matrix;
189 }
190
191 public Quaternion ToQuaternion()
192 {
193 double mag = this.Magnitude();
194 double m00 = (double)this.right.x / mag;
195 double m11 = (double)this.up.y / mag;
196 double m22 = (double)this.at.z / mag;
197 double qx = Math.Sqrt(0.25 * (1.0 - m00 - m11 + m22)) * mag;
198 double qy = Math.Sqrt(0.25 * (1.0 - m00 + m11 - m22)) * mag;
199 double qz = Math.Sqrt(0.25 * (1.0 + m00 - m11 - m22)) * mag;
200 double[] qw = new double[6];
201 double m01 = (double)this.right.y / mag;
202 qw[0] = (0.5 * m01 - qx * qy) / qz * mag;
203 double m02 = (double)this.right.z / mag;
204 qw[1] = (qx * qz - 0.5 * m02) / qy * mag;
205 double m10 = (double)this.up.x / mag;
206 qw[2] = (qx * qy - 0.5 * m10) / qz * mag;
207 double m12 = (double)this.up.z / mag;
208 qw[3] = (0.5 * m12 - qy * qz) / qx * mag;
209 double m20 = (double)this.at.x / mag;
210 qw[4] = (0.5 * m20 - qx * qz) / qy * mag;
211 double m21 = (double)this.at.y / mag;
212 qw[5] = (qy * qz - 0.5 * m21) / qx * mag;
213 return new Quaternion((float)qx, (float)qy, (float)qz, (float)qw[1]);
214 }
215
216 public static Vector3 operator *(Vector3 vec, Matrix33 xf)
217 {
218 return (Vector3)(((xf.right * vec.x) + (xf.up * vec.y)) + (xf.at * vec.z));
219 }
220
221 public static Matrix33 operator *(Matrix33 a, float s)
222 {
223 return new Matrix33((Vector3)(a.right * s), (Vector3)(a.up * s), (Vector3)(a.at * s));
224 }
225
226 public static Matrix33 operator *(Matrix33 a, Matrix33 b)
227 {
228 Matrix33 matrix = new Matrix33();
229 matrix.right = (Vector3)(((b.right * a.right.x) + (b.up * a.right.y)) + (b.at * a.right.z));
230 matrix.up = (Vector3)(((b.right * a.up.x) + (b.up * a.up.y)) + (b.at * a.up.z));
231 matrix.at = (Vector3)(((b.right * a.at.x) + (b.up * a.at.y)) + (b.at * a.at.z));
232 return matrix;
233 }
234 }
235 }

  ViewVC Help
Powered by ViewVC 1.1.22