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

Contents of /trunk/gr2lib/core/coretypes/implementation/Vector3.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: 18431 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 /// IVector3 inteface
14 /// </summary>
15 public interface IVector3
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 }
30 /// <summary>
31 /// Vector3 class
32 /// </summary>
33 [StructLayout(LayoutKind.Sequential)]
34 [Editor(typeof(UIVectorEditor<Vector3>), typeof(UITypeEditor))]
35 public struct Vector3 : IVector3
36 {
37 #region Implicit Conversion Operators
38 /// <summary>
39 /// Converts string to Vector3
40 /// </summary>
41 /// <param name="vector">the string to convert</param>
42 /// <returns></returns>
43 public static implicit operator Vector3(string vector)
44 {
45 if (vector != null)
46 {
47 float x, y, z;
48 string[] _matrix = vector.Split(',');
49 x = Convert.ToSingle(_matrix[0].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
50 y = Convert.ToSingle(_matrix[1].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
51 z = Convert.ToSingle(_matrix[2].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
52 return new Vector3(x, y, z);
53 }
54 else
55 {
56 return new Vector3();
57 }
58 }
59 /// <summary>
60 /// converts vector3 to string
61 /// </summary>
62 /// <param name="vector">the vector to convert</param>
63 /// <returns></returns>
64 public static implicit operator string(Vector3 vector)
65 {
66 if (vector != null) { return vector.ToString(); } else { return new Vector3().ToString(); }
67 }
68 #endregion
69 /// <summary>
70 /// Empty
71 /// </summary>
72 public static readonly Vector3 Empty;
73 /// <summary>
74 /// Invalid
75 /// </summary>
76 public static readonly Vector3 Invalid;
77 /// <summary>
78 /// OutOfWorld
79 /// </summary>
80 public static readonly Vector3 OutOfWorld;
81 /// <summary>
82 /// Zero
83 /// </summary>
84 public static readonly Vector3 Zero;
85 /// <summary>
86 /// Origin
87 /// </summary>
88 public static readonly Vector3 Origin;
89 /// <summary>
90 /// UnitX
91 /// </summary>
92 public static readonly Vector3 UnitX;
93 /// <summary>
94 /// UnitY
95 /// </summary>
96 public static readonly Vector3 UnitY;
97 /// <summary>
98 /// UnitZ
99 /// </summary>
100 public static readonly Vector3 UnitZ;
101 private float _x;
102 private float _y;
103 private float _z;
104
105 #region IVector3 Members
106 /// <summary>
107 /// x
108 /// </summary>
109 public float x { get { return _x; } set { _x = value; } }
110 /// <summary>
111 /// y
112 /// </summary>
113 public float y { get { return _y; } set { _y = value; } }
114 /// <summary>
115 /// z
116 /// </summary>
117 public float z { get { return _z; } set { _z = value; } }
118 #endregion
119
120 //public Vector3()
121 //{
122 // this.x = 0f;
123 // this.y = 0f;
124 // this.z = 0f;
125 //}
126 /// <summary>
127 /// default constructor from vector4
128 /// </summary>
129 /// <param name="v"></param>
130 public Vector3(Vector4 v)
131 {
132 this._x = v.x;
133 this._y = v.y;
134 this._z = v.z;
135 }
136 /// <summary>
137 /// default constructor from Vector3
138 /// </summary>
139 /// <param name="v"></param>
140 public Vector3(Vector3 v)
141 {
142 this._x = v.x;
143 this._y = v.y;
144 this._z = v.z;
145 }
146 /// <summary>
147 /// default constructor from Vector2
148 /// </summary>
149 /// <param name="v"></param>
150 public Vector3(Vector2 v)
151 {
152 this._x = v.x;
153 this._y = v.y;
154 this._z = 0f;
155 }
156 /// <summary>
157 /// Vector4 P4
158 /// </summary>
159 public Vector4 P4
160 {
161 get
162 {
163 return new Vector4(this.x, this.y, this.z, 1f);
164 }
165 }
166 /// <summary>
167 /// Vector4 V4
168 /// </summary>
169 public Vector4 V4
170 {
171 get
172 {
173 return new Vector4(this.x, this.y, this.z, 0f);
174 }
175 }
176 /// <summary>
177 /// CreateWorldVector3FromVector2
178 /// </summary>
179 /// <param name="v"></param>
180 /// <returns></returns>
181 public static Vector3 CreateWorldVector3FromVector2(Vector2 v)
182 {
183 return new Vector3(v.x, 0f, v.y);
184 }
185 /// <summary>
186 /// default constrcutor from x,y,z
187 /// </summary>
188 /// <param name="_x"></param>
189 /// <param name="_y"></param>
190 /// <param name="_z"></param>
191 public Vector3(float _x, float _y, float _z)
192 {
193 this._x = _x;
194 this._y = _y;
195 this._z = _z;
196 }
197 /// <summary>
198 /// operator ==(Vector3 a, Vector3 b)
199 /// </summary>
200 /// <param name="a"></param>
201 /// <param name="b"></param>
202 /// <returns></returns>
203 public static bool operator ==(Vector3 a, Vector3 b)
204 {
205 return (((a.x == b.x) && (a.y == b.y)) && (a.z == b.z));
206 }
207 /// <summary>
208 /// operator !=(Vector3 a, Vector3 b)
209 /// </summary>
210 /// <param name="a"></param>
211 /// <param name="b"></param>
212 /// <returns></returns>
213 public static bool operator !=(Vector3 a, Vector3 b)
214 {
215 if ((a.x == b.x) && (a.y == b.y))
216 {
217 return (a.z != b.z);
218 }
219 return true;
220 }
221 /// <summary>
222 /// Test if instances are equal
223 /// </summary>
224 /// <param name="obj"></param>
225 /// <returns></returns>
226 public override bool Equals(object obj)
227 {
228 if ((obj == null) || (obj.GetType() != base.GetType()))
229 {
230 return false;
231 }
232 Vector3 vector = (Vector3)obj;
233 return (((this.x == vector.x) && (this.y == vector.y)) && (this.z == vector.z));
234 }
235 /// <summary>
236 /// Get Hash Code
237 /// </summary>
238 /// <returns></returns>
239 public override int GetHashCode()
240 {
241 return ((this.x.GetHashCode() ^ this.y.GetHashCode()) ^ this.z.GetHashCode());
242 }
243 /// <summary>
244 /// get string representation
245 /// </summary>
246 /// <returns></returns>
247 public override string ToString()
248 {
249 StringBuilder builder = new StringBuilder();
250 builder.Append("(");
251 builder.Append(this.x.ToString("0.000000"));
252 builder.Append(", ");
253 builder.Append(this.y.ToString("0.000000"));
254 builder.Append(", ");
255 builder.Append(this.z.ToString("0.000000"));
256 builder.Append(")");
257 return builder.ToString();
258 }
259 /// <summary>
260 /// Tryparse
261 /// </summary>
262 /// <param name="values"></param>
263 /// <param name="vector3"></param>
264 /// <returns></returns>
265 public static bool TryParse(string values, out Vector3 vector3)
266 {
267 vector3 = new Vector3();
268 if (values == null)
269 {
270 return false;
271 }
272 string[] strArray = values.Replace(" ", "").Replace("f", "").Split(new char[] { ',' });
273 if (strArray.Length != 3)
274 {
275 return false;
276 }
277 if (!float.TryParse(strArray[0], out vector3._x))
278 {
279 return false;
280 }
281 if (!float.TryParse(strArray[1], out vector3._y))
282 {
283 return false;
284 }
285 if (!float.TryParse(strArray[2], out vector3._z))
286 {
287 return false;
288 }
289 return true;
290 }
291 /// <summary>
292 /// Set: x,y,z
293 /// </summary>
294 /// <param name="_x"></param>
295 /// <param name="_y"></param>
296 /// <param name="_z"></param>
297 public void Set(float _x, float _y, float _z)
298 {
299 this.x = _x;
300 this.y = _y;
301 this.z = _z;
302 }
303 /// <summary>
304 /// operator -(Vector3 vec)
305 /// </summary>
306 /// <param name="vec"></param>
307 /// <returns></returns>
308 public static Vector3 operator -(Vector3 vec)
309 {
310 return new Vector3(-vec.x, -vec.y, -vec.z);
311 }
312 /// <summary>
313 /// operator +(Vector3 vec, float scaler)
314 /// </summary>
315 /// <param name="vec"></param>
316 /// <param name="scaler"></param>
317 /// <returns></returns>
318 public static Vector3 operator +(Vector3 vec, float scaler)
319 {
320 return new Vector3(vec.x + scaler, vec.y + scaler, vec.z + scaler);
321 }
322 /// <summary>
323 /// operator -(Vector3 vec, float scaler)
324 /// </summary>
325 /// <param name="vec"></param>
326 /// <param name="scaler"></param>
327 /// <returns></returns>
328 public static Vector3 operator -(Vector3 vec, float scaler)
329 {
330 return new Vector3(vec.x - scaler, vec.y - scaler, vec.z - scaler);
331 }
332 /// <summary>
333 /// operator +(Vector3 a, Vector3 b)
334 /// </summary>
335 /// <param name="a"></param>
336 /// <param name="b"></param>
337 /// <returns></returns>
338 public static Vector3 operator +(Vector3 a, Vector3 b)
339 {
340 return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
341 }
342 /// <summary>
343 /// operator -(Vector3 a, Vector3 b)
344 /// </summary>
345 /// <param name="a"></param>
346 /// <param name="b"></param>
347 /// <returns></returns>
348 public static Vector3 operator -(Vector3 a, Vector3 b)
349 {
350 return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
351 }
352 /// <summary>
353 /// operator *(Vector3 vec, float scaler)
354 /// </summary>
355 /// <param name="vec"></param>
356 /// <param name="scaler"></param>
357 /// <returns></returns>
358 public static Vector3 operator *(Vector3 vec, float scaler)
359 {
360 return new Vector3(vec.x * scaler, vec.y * scaler, vec.z * scaler);
361 }
362 /// <summary>
363 /// operator /(Vector3 vec, float scaler)
364 /// </summary>
365 /// <param name="vec"></param>
366 /// <param name="scaler"></param>
367 /// <returns></returns>
368 public static Vector3 operator /(Vector3 vec, float scaler)
369 {
370 return new Vector3(vec.x / scaler, vec.y / scaler, vec.z / scaler);
371 }
372 /// <summary>
373 /// operator *(Vector3 a, Vector3 b)
374 /// </summary>
375 /// <param name="a"></param>
376 /// <param name="b"></param>
377 /// <returns></returns>
378 public static float operator *(Vector3 a, Vector3 b)
379 {
380 return (((a.x * b.x) + (a.y * b.y)) + (a.z * b.z));
381 }
382 /// <summary>
383 /// Multiply(Vector3 b)
384 /// </summary>
385 /// <param name="b"></param>
386 /// <returns></returns>
387 public Vector3 Multiply(Vector3 b)
388 {
389 return new Vector3(this.x * b.x, this.y * b.y, this.z * b.z);
390 }
391 /// <summary>
392 /// operator /(Vector3 a, Vector3 b)
393 /// </summary>
394 /// <param name="a"></param>
395 /// <param name="b"></param>
396 /// <returns></returns>
397 public static Vector3 operator /(Vector3 a, Vector3 b)
398 {
399 return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
400 }
401 /// <summary>
402 /// Min(Vector3 a, Vector3 b)
403 /// </summary>
404 /// <param name="a"></param>
405 /// <param name="b"></param>
406 /// <returns></returns>
407 public static Vector3 Min(Vector3 a, Vector3 b)
408 {
409 return new Vector3(Math.Min(a.x, b.x), Math.Min(a.y, b.y), Math.Min(a.z, b.z));
410 }
411 /// <summary>
412 /// Max(Vector3 a, Vector3 b)
413 /// </summary>
414 /// <param name="a"></param>
415 /// <param name="b"></param>
416 /// <returns></returns>
417 public static Vector3 Max(Vector3 a, Vector3 b)
418 {
419 return new Vector3(Math.Max(a.x, b.x), Math.Max(a.y, b.y), Math.Max(a.z, b.z));
420 }
421 /// <summary>
422 /// Floor(Vector3 v)
423 /// </summary>
424 /// <param name="v"></param>
425 /// <returns></returns>
426 public static Vector3 Floor(Vector3 v)
427 {
428 return new Vector3(MathUtils.Floor(v.x), MathUtils.Floor(v.y), MathUtils.Floor(v.z));
429 }
430 /// <summary>
431 /// Lerp(Vector3 a, Vector3 b, float t)
432 /// </summary>
433 /// <param name="a"></param>
434 /// <param name="b"></param>
435 /// <param name="t"></param>
436 /// <returns></returns>
437 public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
438 {
439 return new Vector3(a.x + ((b.x - a.x) * t), a.y + ((b.y - a.y) * t), a.z + ((b.z - a.z) * t));
440 }
441 /// <summary>
442 /// Clamp(Vector3 a, Vector3 clampMin, Vector3 clampMax)
443 /// </summary>
444 /// <param name="a"></param>
445 /// <param name="clampMin"></param>
446 /// <param name="clampMax"></param>
447 /// <returns></returns>
448 public static Vector3 Clamp(Vector3 a, Vector3 clampMin, Vector3 clampMax)
449 {
450 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));
451 }
452 /// <summary>
453 /// CrossProduct(Vector3 a, Vector3 b)
454 /// </summary>
455 /// <param name="a"></param>
456 /// <param name="b"></param>
457 /// <returns></returns>
458 public static Vector3 CrossProduct(Vector3 a, Vector3 b)
459 {
460 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));
461 }
462 /// <summary>
463 /// DotProduct(Vector3 a, Vector3 b)
464 /// </summary>
465 /// <param name="a"></param>
466 /// <param name="b"></param>
467 /// <returns></returns>
468 public static float DotProduct(Vector3 a, Vector3 b)
469 {
470 return a.x * b.x + a.y * b.y + a.z * b.z;
471 }
472 /// <summary>
473 /// Normalize()
474 /// </summary>
475 /// <returns></returns>
476 public Vector3 Normalize()
477 {
478 float len = this.Length();
479 if (Math.Abs(len) < 1E-05f)
480 {
481 this.x = this.y = this.z = 0f;
482 return this;
483 }
484 len = 1f / len;
485 this.x *= len;
486 this.y *= len;
487 this.z *= len;
488 return this;
489 }
490 /// <summary>
491 /// Length
492 /// </summary>
493 /// <returns></returns>
494 public float Length()
495 {
496 return (float)Math.Sqrt((double)this.LengthSqr());
497 }
498 /// <summary>
499 /// LengthSqr
500 /// </summary>
501 /// <returns></returns>
502 public float LengthSqr()
503 {
504 return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
505 }
506 /// <summary>
507 /// operator *(Matrix33 xf, Vector3 vec)
508 /// </summary>
509 /// <param name="xf"></param>
510 /// <param name="vec"></param>
511 /// <returns></returns>
512 public static Vector3 operator *(Matrix33 xf, Vector3 vec)
513 {
514 Vector3 vector = new Vector3();
515 vector = (Vector3)(xf.right * vec.x);
516 vector += (Vector3)(xf.up * vec.y);
517 vector += (Vector3)(xf.at * vec.z);
518 return vector;
519 }
520 /// <summary>
521 /// operator *(Matrix44 xf, Vector3 vec)
522 /// </summary>
523 /// <param name="xf"></param>
524 /// <param name="vec"></param>
525 /// <returns></returns>
526 public static Vector3 operator *(Matrix44 xf, Vector3 vec)
527 {
528 Vector3 vector = new Vector3();
529 vector = (Vector3)(xf.right.V3 * vec.x);
530 vector += (Vector3)(xf.up.V3 * vec.y);
531 vector += (Vector3)(xf.at.V3 * vec.z);
532 return (vector + xf.pos.V3);
533 }
534 /// <summary>
535 /// IsSimilarTo(Vector3 v)
536 /// </summary>
537 /// <param name="v"></param>
538 /// <returns></returns>
539 public bool IsSimilarTo(Vector3 v)
540 {
541 Vector3 vector = this - v;
542 return (vector.LengthSqr() < 9.999999E-11f);
543 }
544
545 static Vector3()
546 {
547 Empty = new Vector3(0f, 0f, 0f);
548 Invalid = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
549 OutOfWorld = new Vector3(-20000f, -20000f, -20000f);
550 Zero = Empty;
551 Origin = new Vector3(0f, 0f, 0f);
552 UnitX = new Vector3(1f, 0f, 0f);
553 UnitY = new Vector3(0f, 1f, 0f);
554 UnitZ = new Vector3(0f, 0f, 1f);
555 }
556 }
557 }

  ViewVC Help
Powered by ViewVC 1.1.22