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

Annotation of /trunk/gr2lib/core/coretypes/implementation/Vector3.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 83 - (hide annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (10 years ago) by william
File size: 9971 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 william 83 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 Vector3
11     {
12     #region Implicit Conversion Operators
13     public static implicit operator Vector3(string vector)
14     {
15     if (vector != null)
16     {
17     float x, y, z;
18     string[] _matrix = vector.Split(',');
19     x = Convert.ToSingle(_matrix[0].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
20     y = Convert.ToSingle(_matrix[1].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
21     z = Convert.ToSingle(_matrix[2].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
22     return new Vector3(x, y, z);
23     }
24     else
25     {
26     return new Vector3();
27     }
28     }
29     public static implicit operator string(Vector3 vector)
30     {
31     if (vector != null) { return vector.ToString(); } else { return new Vector3().ToString(); }
32     }
33     #endregion
34     public static readonly Vector3 Empty;
35     public static readonly Vector3 Invalid;
36     public static readonly Vector3 OutOfWorld;
37     public static readonly Vector3 Zero;
38     public static readonly Vector3 Origin;
39     public static readonly Vector3 UnitX;
40     public static readonly Vector3 UnitY;
41     public static readonly Vector3 UnitZ;
42     public float x;
43     public float y;
44     public float z;
45     public Vector3(Vector4 v)
46     {
47     this.x = v.x;
48     this.y = v.y;
49     this.z = v.z;
50     }
51    
52     public Vector3(Vector3 v)
53     {
54     this.x = v.x;
55     this.y = v.y;
56     this.z = v.z;
57     }
58    
59     public Vector3(Vector2 v)
60     {
61     this.x = v.x;
62     this.y = v.y;
63     this.z = 0f;
64     }
65    
66     public Vector4 P4
67     {
68     get
69     {
70     return new Vector4(this.x, this.y, this.z, 1f);
71     }
72     }
73     public Vector4 V4
74     {
75     get
76     {
77     return new Vector4(this.x, this.y, this.z, 0f);
78     }
79     }
80     public static Vector3 CreateWorldVector3FromVector2(Vector2 v)
81     {
82     return new Vector3(v.x, 0f, v.y);
83     }
84    
85     public Vector3(float _x, float _y, float _z)
86     {
87     this.x = _x;
88     this.y = _y;
89     this.z = _z;
90     }
91    
92     public static bool operator ==(Vector3 a, Vector3 b)
93     {
94     return (((a.x == b.x) && (a.y == b.y)) && (a.z == b.z));
95     }
96    
97     public static bool operator !=(Vector3 a, Vector3 b)
98     {
99     if ((a.x == b.x) && (a.y == b.y))
100     {
101     return (a.z != b.z);
102     }
103     return true;
104     }
105    
106     public override bool Equals(object obj)
107     {
108     if ((obj == null) || (obj.GetType() != base.GetType()))
109     {
110     return false;
111     }
112     Vector3 vector = (Vector3)obj;
113     return (((this.x == vector.x) && (this.y == vector.y)) && (this.z == vector.z));
114     }
115    
116     public override int GetHashCode()
117     {
118     return ((this.x.GetHashCode() ^ this.y.GetHashCode()) ^ this.z.GetHashCode());
119     }
120    
121     public override string ToString()
122     {
123     StringBuilder builder = new StringBuilder();
124     builder.Append("(");
125     builder.Append(this.x.ToString("0.000000"));
126     builder.Append(", ");
127     builder.Append(this.y.ToString("0.000000"));
128     builder.Append(", ");
129     builder.Append(this.z.ToString("0.000000"));
130     builder.Append(")");
131     return builder.ToString();
132     }
133    
134     public static bool TryParse(string values, out Vector3 vector3)
135     {
136     vector3 = new Vector3();
137     if (values == null)
138     {
139     return false;
140     }
141     string[] strArray = values.Replace(" ", "").Replace("f", "").Split(new char[] { ',' });
142     if (strArray.Length != 3)
143     {
144     return false;
145     }
146     if (!float.TryParse(strArray[0], out vector3.x))
147     {
148     return false;
149     }
150     if (!float.TryParse(strArray[1], out vector3.y))
151     {
152     return false;
153     }
154     if (!float.TryParse(strArray[2], out vector3.z))
155     {
156     return false;
157     }
158     return true;
159     }
160    
161     public void Set(float _x, float _y, float _z)
162     {
163     this.x = _x;
164     this.y = _y;
165     this.z = _z;
166     }
167    
168     public static Vector3 operator -(Vector3 vec)
169     {
170     return new Vector3(-vec.x, -vec.y, -vec.z);
171     }
172    
173     public static Vector3 operator +(Vector3 vec, float scaler)
174     {
175     return new Vector3(vec.x + scaler, vec.y + scaler, vec.z + scaler);
176     }
177    
178     public static Vector3 operator -(Vector3 vec, float scaler)
179     {
180     return new Vector3(vec.x - scaler, vec.y - scaler, vec.z - scaler);
181     }
182    
183     public static Vector3 operator +(Vector3 a, Vector3 b)
184     {
185     return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
186     }
187    
188     public static Vector3 operator -(Vector3 a, Vector3 b)
189     {
190     return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
191     }
192    
193     public static Vector3 operator *(Vector3 vec, float scaler)
194     {
195     return new Vector3(vec.x * scaler, vec.y * scaler, vec.z * scaler);
196     }
197    
198     public static Vector3 operator /(Vector3 vec, float scaler)
199     {
200     return new Vector3(vec.x / scaler, vec.y / scaler, vec.z / scaler);
201     }
202    
203     public static float operator *(Vector3 a, Vector3 b)
204     {
205     return (((a.x * b.x) + (a.y * b.y)) + (a.z * b.z));
206     }
207    
208     public Vector3 Multiply(Vector3 b)
209     {
210     return new Vector3(this.x * b.x, this.y * b.y, this.z * b.z);
211     }
212    
213     public static Vector3 operator /(Vector3 a, Vector3 b)
214     {
215     return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
216     }
217    
218     public static Vector3 Min(Vector3 a, Vector3 b)
219     {
220     return new Vector3(Math.Min(a.x, b.x), Math.Min(a.y, b.y), Math.Min(a.z, b.z));
221     }
222    
223     public static Vector3 Max(Vector3 a, Vector3 b)
224     {
225     return new Vector3(Math.Max(a.x, b.x), Math.Max(a.y, b.y), Math.Max(a.z, b.z));
226     }
227    
228     public static Vector3 Floor(Vector3 v)
229     {
230     return new Vector3(MathUtils.Floor(v.x), MathUtils.Floor(v.y), MathUtils.Floor(v.z));
231     }
232    
233     public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
234     {
235     return new Vector3(a.x + ((b.x - a.x) * t), a.y + ((b.y - a.y) * t), a.z + ((b.z - a.z) * t));
236     }
237    
238     public static Vector3 Clamp(Vector3 a, Vector3 clampMin, Vector3 clampMax)
239     {
240     return new Vector3(MathUtils.Clamp(a.x, clampMin.x, clampMax.x), MathUtils.Clamp(a.y, clampMin.y, clampMax.y), MathUtils.Clamp(a.z, clampMin.z, clampMax.z));
241     }
242    
243     public static Vector3 CrossProduct(Vector3 a, Vector3 b)
244     {
245     return new Vector3((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x));
246     }
247    
248     public static float DotProduct(Vector3 a, Vector3 b)
249     {
250     return a.x * b.x + a.y * b.y + a.z * b.z;
251     }
252    
253     public Vector3 Normalize()
254     {
255     float len = this.Length();
256     if (Math.Abs(len) < 1E-05f)
257     {
258     this.x = this.y = this.z = 0f;
259     return this;
260     }
261     len = 1f / len;
262     this.x *= len;
263     this.y *= len;
264     this.z *= len;
265     return this;
266     }
267    
268     public float Length()
269     {
270     return (float)Math.Sqrt((double)this.LengthSqr());
271     }
272    
273     public float LengthSqr()
274     {
275     return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
276     }
277    
278     public static Vector3 operator *(Matrix33 xf, Vector3 vec)
279     {
280     Vector3 vector = new Vector3();
281     vector = (Vector3)(xf.right * vec.x);
282     vector += (Vector3)(xf.up * vec.y);
283     vector += (Vector3)(xf.at * vec.z);
284     return vector;
285     }
286    
287     public static Vector3 operator *(Matrix44 xf, Vector3 vec)
288     {
289     Vector3 vector = new Vector3();
290     vector = (Vector3)(xf.right.V3 * vec.x);
291     vector += (Vector3)(xf.up.V3 * vec.y);
292     vector += (Vector3)(xf.at.V3 * vec.z);
293     return (vector + xf.pos.V3);
294     }
295    
296     public bool IsSimilarTo(Vector3 v)
297     {
298     Vector3 vector = this - v;
299     return (vector.LengthSqr() < 9.999999E-11f);
300     }
301    
302     static Vector3()
303     {
304     Empty = new Vector3(0f, 0f, 0f);
305     Invalid = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
306     OutOfWorld = new Vector3(-20000f, -20000f, -20000f);
307     Zero = Empty;
308     Origin = new Vector3(0f, 0f, 0f);
309     UnitX = new Vector3(1f, 0f, 0f);
310     UnitY = new Vector3(0f, 1f, 0f);
311     UnitZ = new Vector3(0f, 0f, 1f);
312     }
313     }
314     }

  ViewVC Help
Powered by ViewVC 1.1.22