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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations) (download)
Sat Jul 24 02:23:18 2010 UTC (9 years, 3 months ago) by william
File size: 13583 byte(s)
continue to add xml documentation

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.ComponentModel;
7 using gr2lib.core.ui.typeeditors;
8 using System.Drawing.Design;
9
10 namespace gr2lib.core.coretypes.implementation
11 {
12 /// <summary>
13 /// IVector4 inteface
14 /// </summary>
15 public interface IVector4
16 {
17 /// <summary>
18 /// x
19 /// </summary>
20 float x { get; set; }
21 /// <summary>
22 /// y
23 /// </summary>
24 float y { get; set; }
25 /// <summary>
26 /// z
27 /// </summary>
28 float z { get; set; }
29 /// <summary>
30 /// w
31 /// </summary>
32 float w { get; set; }
33 }
34 /// <summary>
35 /// Vector4 class
36 /// </summary>
37 [StructLayout(LayoutKind.Sequential)]
38 [Editor(typeof(UIVectorEditor<Vector4>), typeof(UITypeEditor))]
39 public struct Vector4 : IVector4
40 {
41 #region Implicit Conversion Operators
42 /// <summary>
43 /// convert string to vector4
44 /// </summary>
45 /// <param name="vector">the stirng to convert</param>
46 /// <returns></returns>
47 public static implicit operator Vector4(string vector)
48 {
49 if (vector != null)
50 {
51 float x, y, z, w;
52 string[] _matrix = vector.Split(',');
53 x = Convert.ToSingle(_matrix[0].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
54 y = Convert.ToSingle(_matrix[1].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
55 z = Convert.ToSingle(_matrix[2].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
56 w = Convert.ToSingle(_matrix[3].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
57 return new Vector4(x, y, z,w);
58 }
59 else
60 {
61 return new Vector4();
62 }
63 }
64 /// <summary>
65 /// converts vector4 to string
66 /// </summary>
67 /// <param name="vector">the vector to convert</param>
68 /// <returns></returns>
69 public static implicit operator string(Vector4 vector)
70 {
71 if (vector != null) { return vector.ToString(); } else { return new Vector4().ToString(); }
72 }
73 #endregion
74 /// <summary>
75 /// Empty Vector
76 /// </summary>
77 public static readonly Vector4 Empty;
78 /// <summary>
79 /// Invalid Vector
80 /// </summary>
81 public static readonly Vector4 Invalid;
82 /// <summary>
83 /// OutOfWorld Vector
84 /// </summary>
85 public static readonly Vector4 OutOfWorld;
86 /// <summary>
87 /// Zero Vector
88 /// </summary>
89 public static readonly Vector4 Zero;
90 /// <summary>
91 /// Origin Vector
92 /// </summary>
93 public static readonly Vector4 Origin;
94 /// <summary>
95 /// UnitX Vector
96 /// </summary>
97 public static readonly Vector4 UnitX;
98 /// <summary>
99 /// UnitY Vector
100 /// </summary>
101 public static readonly Vector4 UnitY;
102 /// <summary>
103 /// UnitZ Vector
104 /// </summary>
105 public static readonly Vector4 UnitZ;
106 /// <summary>
107 /// UnitW Vector
108 /// </summary>
109 public static readonly Vector4 UnitW;
110 private float _x;
111 private float _y;
112 private float _z;
113 private float _w;
114
115 #region IVector4 Members
116 /// <summary>
117 /// x
118 /// </summary>
119 public float x { get { return _x; } set { _x = value; } }
120 /// <summary>
121 /// y
122 /// </summary>
123 public float y { get { return _y; } set { _y = value; } }
124 /// <summary>
125 /// z
126 /// </summary>
127 public float z { get { return _z; } set { _z = value; } }
128 /// <summary>
129 /// w
130 /// </summary>
131 public float w { get { return _w; } set { _w = value; } }
132 #endregion
133
134 /// <summary>
135 /// Gets a Vector3 from this instance
136 /// </summary>
137 public Vector3 V3
138 {
139 get
140 {
141 return new Vector3(this);
142 }
143 set
144 {
145 this.x = value.x;
146 this.y = value.y;
147 this.z = value.z;
148 }
149 }
150 //public Vector4()
151 //{
152 // this.x = 0f;
153 // this.y = 0f;
154 // this.z = 0f;
155 // this.w = 0f;
156 //}
157
158 /// <summary>
159 /// default constructor from a Vector4
160 /// </summary>
161 /// <param name="v"></param>
162 public Vector4(Vector4 v)
163 {
164 this._x = v.x;
165 this._y = v.y;
166 this._z = v.z;
167 this._w = v.w;
168 }
169 /// <summary>
170 /// default constructor from a Vector3
171 /// </summary>
172 /// <param name="v"></param>
173 public Vector4(Vector3 v)
174 {
175 this._x = v.x;
176 this._y = v.y;
177 this._z = v.z;
178 this._w = 1f;
179 }
180 /// <summary>
181 /// default constructor from a Vector2
182 /// </summary>
183 /// <param name="v"></param>
184 public Vector4(Vector2 v)
185 {
186 this._x = v.x;
187 this._y = v.y;
188 this._z = 0f;
189 this._w = 1f;
190 }
191
192 /// <summary>
193 /// Creates a World Vector from a Vector2
194 /// </summary>
195 /// <param name="v"></param>
196 /// <returns></returns>
197 public static Vector4 CreateWorldVector4FromVector2(Vector2 v)
198 {
199 return new Vector4(v.x, 0f, v.y, 1f);
200 }
201
202 /// <summary>
203 /// default constructor speciying x,y,z,w
204 /// </summary>
205 /// <param name="_x"></param>
206 /// <param name="_y"></param>
207 /// <param name="_z"></param>
208 /// <param name="_w"></param>
209 public Vector4(float _x, float _y, float _z, float _w)
210 {
211 this._x = _x;
212 this._y = _y;
213 this._z = _z;
214 this._w = _w;
215 }
216 /// <summary>
217 /// default constructor speciying x,y,z
218 /// </summary>
219 /// <param name="_x"></param>
220 /// <param name="_y"></param>
221 /// <param name="_z"></param>
222 public Vector4(float _x, float _y, float _z)
223 : this(_x, _y, _z, 1f)
224 {
225 }
226 /// <summary>
227 /// Set this instance from x,y,z,w
228 /// </summary>
229 /// <param name="_x"></param>
230 /// <param name="_y"></param>
231 /// <param name="_z"></param>
232 /// <param name="_w"></param>
233 public void Set(float _x, float _y, float _z, float _w)
234 {
235 this.x = _x;
236 this.y = _y;
237 this.z = _z;
238 this.w = _w;
239 }
240 /// <summary>
241 /// Set this instance from x,y,z
242 /// </summary>
243 /// <param name="_x"></param>
244 /// <param name="_y"></param>
245 /// <param name="_z"></param>
246 public void Set(float _x, float _y, float _z)
247 {
248 this.x = _x;
249 this.y = _y;
250 this.z = _z;
251 this.w = 1f;
252 }
253 /// <summary>
254 /// Test if instances are equal
255 /// </summary>
256 /// <param name="obj"></param>
257 /// <returns></returns>
258 public override bool Equals(object obj)
259 {
260 if ((obj == null) || (obj.GetType() != base.GetType()))
261 {
262 return false;
263 }
264 Vector4 vector = (Vector4)obj;
265 return (this.x == vector.x && this.y == vector.y && this.z == vector.z && this.w == vector.w);
266 }
267 /// <summary>
268 /// Get hash for this instance
269 /// </summary>
270 /// <returns></returns>
271 public override int GetHashCode()
272 {
273 return (this.x.GetHashCode() ^ this.y.GetHashCode() ^ this.z.GetHashCode() ^ this.w.GetHashCode());
274 }
275 /// <summary>
276 /// operator -(Vector4 vec)
277 /// </summary>
278 /// <param name="vec"></param>
279 /// <returns></returns>
280 public static Vector4 operator -(Vector4 vec)
281 {
282 return new Vector4(-vec.x, -vec.y, -vec.z, vec.w);
283 }
284 /// <summary>
285 /// operator +(Vector4 a, Vector4 b)
286 /// </summary>
287 /// <param name="a"></param>
288 /// <param name="b"></param>
289 /// <returns></returns>
290 public static Vector4 operator +(Vector4 a, Vector4 b)
291 {
292 return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
293 }
294 /// <summary>
295 /// operator -(Vector4 a, Vector4 b)
296 /// </summary>
297 /// <param name="a"></param>
298 /// <param name="b"></param>
299 /// <returns></returns>
300 public static Vector4 operator -(Vector4 a, Vector4 b)
301 {
302 return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
303 }
304 /// <summary>
305 /// operator *(Vector4 vec, float scaler)
306 /// </summary>
307 /// <param name="vec"></param>
308 /// <param name="scaler"></param>
309 /// <returns></returns>
310 public static Vector4 operator *(Vector4 vec, float scaler)
311 {
312 return new Vector4(vec.x * scaler, vec.y * scaler, vec.z * scaler, vec.w * scaler);
313 }
314 /// <summary>
315 /// operator /(Vector4 vec, float scaler)
316 /// </summary>
317 /// <param name="vec"></param>
318 /// <param name="scaler"></param>
319 /// <returns></returns>
320 public static Vector4 operator /(Vector4 vec, float scaler)
321 {
322 return new Vector4(vec.x / scaler, vec.y / scaler, vec.z / scaler, vec.w / scaler);
323 }
324 /// <summary>
325 /// operator *(Vector4 a, Vector4 b)
326 /// </summary>
327 /// <param name="a"></param>
328 /// <param name="b"></param>
329 /// <returns></returns>
330 public static float operator *(Vector4 a, Vector4 b)
331 {
332 return ((((a.x * b.x) + (a.y * b.y)) + (a.z * b.z)) + (a.w * b.w));
333 }
334 /// <summary>
335 /// Get Length
336 /// </summary>
337 /// <returns></returns>
338 public float Length()
339 {
340 return (float)Math.Sqrt((double)this.LengthSqr());
341 }
342 /// <summary>
343 /// Get Length Sqr
344 /// </summary>
345 /// <returns></returns>
346 public float LengthSqr()
347 {
348 return ((((this.x * this.x) + (this.y * this.y)) + (this.z * this.z)) + (this.w * this.w));
349 }
350 /// <summary>
351 /// Normalize
352 /// </summary>
353 /// <returns></returns>
354 public Vector4 Normalize()
355 {
356 float len = this.Length();
357 if (Math.Abs(len) < 1E-05f)
358 {
359 this.x = this.y = this.z = this.w = 0f;
360 return this;
361 }
362 len = 1f / len;
363 this.x *= len;
364 this.y *= len;
365 this.z *= len;
366 this.w *= len;
367 return this;
368 }
369 /// <summary>
370 /// CrossProduct
371 /// </summary>
372 /// <param name="a"></param>
373 /// <param name="b"></param>
374 /// <returns></returns>
375 public static Vector4 CrossProduct(Vector4 a, Vector4 b)
376 {
377 return new Vector4((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x), 0f);
378 }
379 /// <summary>
380 /// IsSimilarTo
381 /// </summary>
382 /// <param name="v"></param>
383 /// <returns></returns>
384 public bool IsSimilarTo(Vector4 v)
385 {
386 Vector4 vector = this - v;
387 return (vector.LengthSqr() < 9.999999E-11f);
388 }
389 /// <summary>
390 /// Gets string representation
391 /// </summary>
392 /// <returns></returns>
393 public override string ToString()
394 {
395 StringBuilder builder = new StringBuilder();
396 builder.Append("(");
397 builder.Append(this.x.ToString("0.000000"));
398 builder.Append(", ");
399 builder.Append(this.y.ToString("0.000000"));
400 builder.Append(", ");
401 builder.Append(this.z.ToString("0.000000"));
402 builder.Append(", ");
403 builder.Append(this.w.ToString("0.000000"));
404 builder.Append(")");
405 return builder.ToString();
406 }
407
408 static Vector4()
409 {
410 Empty = new Vector4(0f, 0f, 0f, 0f);
411 Invalid = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
412 OutOfWorld = new Vector4(-20000f, -20000f, -20000f, 1f);
413 Zero = Empty;
414 Origin = Empty;
415 UnitX = new Vector4(1f, 0f, 0f, 0f);
416 UnitY = new Vector4(0f, 1f, 0f, 0f);
417 UnitZ = new Vector4(0f, 0f, 1f, 0f);
418 UnitW = new Vector4(0f, 0f, 0f, 1f);
419 }
420 }
421 }

  ViewVC Help
Powered by ViewVC 1.1.22