-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec4.bf
334 lines (277 loc) · 7.17 KB
/
vec4.bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System;
namespace Quickmaths {
public struct vec4 : IFormattable {
public static readonly vec4 X = vec4(1,0,0,0);
public static readonly vec4 Y = vec4(0,1,0,0);
public static readonly vec4 Z = vec4(0,0,1,0);
public static readonly vec4 W = vec4(0,0,0,1);
public float x;
public float y;
public float z;
public float w;
public float r {
get { return x; }
set mut { x = value; }}
public float g {
get { return y; }
set mut { y = value; }}
public float b {
get { return z; }
set mut { z = value; }}
public float a {
get { return w; }
set mut { w = value; }}
//////////////////////////////////////////////////////////////////////////
// Constructors
//////////////////////////////////////////////////////////////////////////
public this(float x, float y, float z, float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public this(float x) {
this.x = x;
this.y = x;
this.z = x;
this.w = x;
}
public this(vec3 xyz, float w) {
this.x = xyz.x;
this.y = xyz.y;
this.z = xyz.z;
this.w = w;
}
public this(float x, vec3 yzw) {
this.x = x;
this.y = yzw.x;
this.z = yzw.y;
this.w = yzw.z;
}
public this(vec2 xy, vec2 zw) {
this.x = xy.x;
this.y = xy.y;
this.z = zw.x;
this.w = zw.y;
}
public this(vec2 xy, float z, float w) {
this.x = xy.x;
this.y = xy.y;
this.z = z;
this.w = w;
}
public this(float x, float y, vec2 zw) {
this.x = x;
this.y = y;
this.z = zw.x;
this.w = zw.y;
}
public this(float x, vec2 yz, float w) {
this.x = x;
this.y = yz.x;
this.z = yz.y;
this.w = w;
}
//////////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////////
public float dot(vec4 vec) {
return this.x*vec.x + this.y+vec.y + this.z*vec.z + this.w*vec.w;
}
public vec4 cross(vec4 vec) {
return vec4(
this.y*vec.z + this.z*vec.y,
this.z*vec.w + this.w*vec.z,
this.w*vec.x + this.x*vec.w,
this.x*vec.y + this.y*vec.x );
}
public float magnitude() {
return Math.Sqrt(magnitude2());
}
public float magnitude2() {
return x*x + y*y + z*z + w*w;
}
public vec4 normal() {
float mag = magnitude();
if (mag == 0)
return this;
return this / magnitude();
}
public vec4 trim(float length) {
return this.normal() * length;
}
public vec4 clampLength(float min, float max) {
return this.normal() * Math.Clamp(this.magnitude(), min, max);
}
public vec4 abs() {
return vec4(
Math.Abs(this.x),
Math.Abs(this.y),
Math.Abs(this.z),
Math.Abs(this.w) );
}
public vec4 floor() {
return vec4(
Math.Floor(this.x),
Math.Floor(this.y),
Math.Floor(this.z),
Math.Floor(this.w) );
}
public vec4 ceil() {
return vec4(
Math.Ceiling(this.x),
Math.Ceiling(this.y),
Math.Ceiling(this.z),
Math.Ceiling(this.w) );
}
public vec4 clamp(vec4 min, vec4 max) {
return vec4(
Math.Clamp(this.x, min.x, max.x),
Math.Clamp(this.y, min.y, max.y),
Math.Clamp(this.z, min.z, max.z),
Math.Clamp(this.w, min.w, max.w) );
}
/*
public static vec4 fromHex(StringView hex, float a=1) {
StringView hexStr = hex;
if (hexStr[0] == '#')
hexStr = hexStr.Substring(1);
try {
if (hexStr.Length == 8)
return vec4(
int.Parse(hexStr.Substring(0,2), System.Globalization.NumberStyles.HexNumber) / 255f,
int.Parse(hexStr.Substring(2,2), System.Globalization.NumberStyles.HexNumber) / 255f,
int.Parse(hexStr.Substring(4,2), System.Globalization.NumberStyles.HexNumber) / 255f,
int.Parse(hexStr.Substring(6,2), System.Globalization.NumberStyles.HexNumber) / 255f );
else if (hex.Length == 6)
return vec4(
int.Parse(hexStr.Substring(0,2), System.Globalization.NumberStyles.HexNumber) / 255f,
int.Parse(hexStr.Substring(2,2), System.Globalization.NumberStyles.HexNumber) / 255f,
int.Parse(hexStr.Substring(4,2), System.Globalization.NumberStyles.HexNumber) / 255f,
a );
else
return vec4(0,0,0,a);
} catch(System.FormatException) {
return vec4(0,0,0,a);
}
}
public string toHex() {
return ((int) Math.min(this.x * 255, 255)).ToString("X") +
((int) Math.min(this.y * 255, 255)).ToString("X") +
((int) Math.min(this.z * 255, 255)).ToString("X") +
((int) Math.min(this.w * 255, 255)).ToString("X");
}
*/
//////////////////////////////////////////////////////////////////////////
// Overrides
//////////////////////////////////////////////////////////////////////////
public void ToString(String outString, String format, IFormatProvider formatProvider) {
outString.Append("(");
x.ToString(outString, format, formatProvider);
outString.Append(",");
y.ToString(outString, format, formatProvider);
outString.Append(",");
z.ToString(outString, format, formatProvider);
outString.Append(",");
w.ToString(outString, format, formatProvider);
outString.Append(")");
}
//////////////////////////////////////////////////////////////////////////
// Casting
//////////////////////////////////////////////////////////////////////////
public static implicit operator vec4(vec3 v) {
return vec4(v,0);
}
public static explicit operator vec4(float[4] v) {
return vec4( v[0], v[1], v[2], v[3] );
}
public static explicit operator float[4](vec4 v) {
return float[4]( v.x, v.y, v.z, v.w );
}
//////////////////////////////////////////////////////////////////////////
// Operators
//////////////////////////////////////////////////////////////////////////
public static bool operator==(vec4 a, vec4 b) {
return a.x == b.x
&& a.y == b.y
&& a.z == b.z
&& a.w == b.w;
}
public static bool operator!=(vec4 a, vec4 b) {
return a.x != b.x
|| a.y != b.y
|| a.z != b.z
|| a.w != b.w;
}
public static vec4 operator+(vec4 a, vec4 b) {
return vec4(
a.x + b.x,
a.y + b.y,
a.z + b.z,
a.w + b.w );
}
public static vec4 operator-(vec4 a, vec4 b) {
return vec4(
a.x - b.x,
a.y - b.y,
a.z - b.z,
a.w - b.w );
}
public static vec4 operator-(vec4 a) {
return vec4(
-a.x,
-a.y,
-a.z,
-a.w );
}
public static vec4 operator*(vec4 a, vec4 b) {
return vec4(
a.x * b.x,
a.y * b.y,
a.z * b.z,
a.w * b.w );
}
public static vec4 operator*(float a, vec4 b) {
return vec4(
a * b.x,
a * b.y,
a * b.z,
a * b.w );
}
public static vec4 operator*(vec4 a, float b) {
return vec4(
a.x * b,
a.y * b,
a.z * b,
a.w * b );
}
public static vec4 operator/(vec4 a, vec4 b) {
return vec4(
a.x / b.x,
a.y / b.y,
a.z / b.z,
a.w / b.w );
}
public static vec4 operator/(float a, vec4 b) {
return vec4(
a / b.x,
a / b.y,
a / b.z,
a / b.w );
}
public static vec4 operator/(vec4 a, float b) {
return vec4(
a.x / b,
a.y / b,
a.z / b,
a.w / b );
}
public static vec4 operator%(vec4 a, vec4 b) {
return vec4(
a.x % b.x,
a.y % b.y,
a.z % b.z,
a.w % b.w );
}
}
}