/[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 83 - (show annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (9 years, 11 months ago) by william
File size: 6182 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 Vector4
11 {
12 public static readonly Vector4 Empty;
13 public static readonly Vector4 Invalid;
14 public static readonly Vector4 OutOfWorld;
15 public static readonly Vector4 Zero;
16 public static readonly Vector4 Origin;
17 public static readonly Vector4 UnitX;
18 public static readonly Vector4 UnitY;
19 public static readonly Vector4 UnitZ;
20 public static readonly Vector4 UnitW;
21 public float x;
22 public float y;
23 public float z;
24 public float w;
25 public Vector3 V3
26 {
27 get
28 {
29 return new Vector3(this);
30 }
31 set
32 {
33 this.x = value.x;
34 this.y = value.y;
35 this.z = value.z;
36 }
37 }
38 public Vector4(Vector4 v)
39 {
40 this.x = v.x;
41 this.y = v.y;
42 this.z = v.z;
43 this.w = v.w;
44 }
45
46 public Vector4(Vector3 v)
47 {
48 this.x = v.x;
49 this.y = v.y;
50 this.z = v.z;
51 this.w = 1f;
52 }
53
54 public Vector4(Vector2 v)
55 {
56 this.x = v.x;
57 this.y = v.y;
58 this.z = 0f;
59 this.w = 1f;
60 }
61
62 public static Vector4 CreateWorldVector4FromVector2(Vector2 v)
63 {
64 return new Vector4(v.x, 0f, v.y, 1f);
65 }
66
67 public Vector4(float _x, float _y, float _z, float _w)
68 {
69 this.x = _x;
70 this.y = _y;
71 this.z = _z;
72 this.w = _w;
73 }
74
75 public Vector4(float _x, float _y, float _z)
76 : this(_x, _y, _z, 1f)
77 {
78 }
79
80 public void Set(float _x, float _y, float _z, float _w)
81 {
82 this.x = _x;
83 this.y = _y;
84 this.z = _z;
85 this.w = _w;
86 }
87
88 public void Set(float _x, float _y, float _z)
89 {
90 this.x = _x;
91 this.y = _y;
92 this.z = _z;
93 this.w = 1f;
94 }
95
96 public override bool Equals(object obj)
97 {
98 if ((obj == null) || (obj.GetType() != base.GetType()))
99 {
100 return false;
101 }
102 Vector4 vector = (Vector4)obj;
103 return (this.x == vector.x && this.y == vector.y && this.z == vector.z && this.w == vector.w);
104 }
105
106 public override int GetHashCode()
107 {
108 return (this.x.GetHashCode() ^ this.y.GetHashCode() ^ this.z.GetHashCode() ^ this.w.GetHashCode());
109 }
110
111 public static Vector4 operator -(Vector4 vec)
112 {
113 return new Vector4(-vec.x, -vec.y, -vec.z, vec.w);
114 }
115
116 public static Vector4 operator +(Vector4 a, Vector4 b)
117 {
118 return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
119 }
120
121 public static Vector4 operator -(Vector4 a, Vector4 b)
122 {
123 return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
124 }
125
126 public static Vector4 operator *(Vector4 vec, float scaler)
127 {
128 return new Vector4(vec.x * scaler, vec.y * scaler, vec.z * scaler, vec.w * scaler);
129 }
130
131 public static Vector4 operator /(Vector4 vec, float scaler)
132 {
133 return new Vector4(vec.x / scaler, vec.y / scaler, vec.z / scaler, vec.w / scaler);
134 }
135
136 public static float operator *(Vector4 a, Vector4 b)
137 {
138 return ((((a.x * b.x) + (a.y * b.y)) + (a.z * b.z)) + (a.w * b.w));
139 }
140
141 public float Length()
142 {
143 return (float)Math.Sqrt((double)this.LengthSqr());
144 }
145
146 public float LengthSqr()
147 {
148 return ((((this.x * this.x) + (this.y * this.y)) + (this.z * this.z)) + (this.w * this.w));
149 }
150
151 public Vector4 Normalize()
152 {
153 float len = this.Length();
154 if (Math.Abs(len) < 1E-05f)
155 {
156 this.x = this.y = this.z = this.w = 0f;
157 return this;
158 }
159 len = 1f / len;
160 this.x *= len;
161 this.y *= len;
162 this.z *= len;
163 this.w *= len;
164 return this;
165 }
166
167 public static Vector4 CrossProduct(Vector4 a, Vector4 b)
168 {
169 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);
170 }
171
172 public bool IsSimilarTo(Vector4 v)
173 {
174 Vector4 vector = this - v;
175 return (vector.LengthSqr() < 9.999999E-11f);
176 }
177
178 public override string ToString()
179 {
180 StringBuilder builder = new StringBuilder();
181 builder.Append("(");
182 builder.Append(this.x.ToString("0.000000"));
183 builder.Append(", ");
184 builder.Append(this.y.ToString("0.000000"));
185 builder.Append(", ");
186 builder.Append(this.z.ToString("0.000000"));
187 builder.Append(", ");
188 builder.Append(this.w.ToString("0.000000"));
189 builder.Append(")");
190 return builder.ToString();
191 }
192
193 static Vector4()
194 {
195 Empty = new Vector4(0f, 0f, 0f, 0f);
196 Invalid = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
197 OutOfWorld = new Vector4(-20000f, -20000f, -20000f, 1f);
198 Zero = Empty;
199 Origin = Empty;
200 UnitX = new Vector4(1f, 0f, 0f, 0f);
201 UnitY = new Vector4(0f, 1f, 0f, 0f);
202 UnitZ = new Vector4(0f, 0f, 1f, 0f);
203 UnitW = new Vector4(0f, 0f, 0f, 1f);
204 }
205 }
206 }

  ViewVC Help
Powered by ViewVC 1.1.22