-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectors_math.cpp
272 lines (199 loc) · 5.74 KB
/
vectors_math.cpp
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
#pragma once
//#ifndef _VEC_MATH
//#define _VEC_MATH
#include "vectors_math.h"
#include <math.h>
/*float fmax ( float a, float b ) {
return a >= b ? a : b;
} ;
float fmin ( float a, float b ) {
return a <= b ? a : b;
} ; */
float rsqrtf(float x) {
return 1.0f / sqrtf(x);
}
float2 make_float2 ( float x, float y ) {
return float2 ( x,y );
} ;
float3 operator / (float3 &a, float3 &b) {
return make_float3( a.x / b.x, a.y / b.y, a.z / b.z );
} ;
float4 fminf1 ( const float4 &v0, const float4 &v1 ) {
return make_float4 ( fminf1 ( v0.x, v1.x ), fminf1 ( v0.y, v1.y ), fminf1 ( v0.z, v1.z ), 1.0f );
} ;
float4 fmaxf1 ( const float4 &v0, const float4 &v1 ) {
return make_float4 ( fmaxf1 ( v0.x, v1.x ), fmaxf1 ( v0.y, v1.y ), fmaxf1 ( v0.z, v1.z ), 1.0f );
} ;
float3 fminf1 ( const float3 &v0, const float3 &v1 ) {
return make_float3 ( fminf1 ( v0.x, v1.x ), fminf1 ( v0.y, v1.y ), fminf1 ( v0.z, v1.z ) );
} ;
float3 fmaxf1 ( const float3 &v0, const float3 &v1 ) {
return make_float3 ( fmaxf1 ( v0.x, v1.x ), fmaxf1 ( v0.y, v1.y ), fmaxf1 ( v0.z, v1.z ) );
} ;
float4 operator-(float4 &a, float4 &b)
{
return make_float4( a.x - b.x, a.y - b.y, a.z - b.z, 1.0f );
}
float3 make_float3 ( float x, float y, float z ) {
return float3 ( x,y,z );
} ;
float3 operator-(float3 &a) {
return make_float3(-a.x, -a.y, -a.z);
} ;
float3 operator+(float3 a, float3 b) {
return make_float3(a.x + b.x, a.y + b.y, a.z + b.z );
}
void operator*=(float3 &a, float b) {
a.x *= b; a.y *= b; a.z *= b;
}
float3 operator*(float b, float3 a) {
return make_float3(b * a.x, b * a.y, b * a.z );
}
float dot(float3 a, float3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
} ;
float3 cross(float3 a, float3 b) {
return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
float3 normalize(float3 v) {
float invLen = rsqrtf(dot(v, v));
return invLen * v;
}
float4 make_float4 ( float x, float y, float z, float w ) {
return float4 ( x,y,z,w );
} ;
float4 operator-(float4 &a) {
return make_float4(-a.x, -a.y, -a.z,-a.w);
} ;
float4 operator+(float4 a, float4 b) {
return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w );
}
void operator*=(float4 &a, float b) {
a.x *= b; a.y *= b; a.z *= b; a.w *= b;
}
float4 operator*(float b, float4 a) {
return make_float4(b * a.x, b * a.y, b * a.z, b * a.w );
}
float dot(float4 a, float4 b) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
} ;
float4 normalize(float4 v) {
const float invLen = rsqrtf(dot(v, v));
return invLen * v;
}
float imin1(const int a, const int b) {
return a < b ? a : b;
}
float imax1(const int a, const int b) {
return a > b ? a : b;
}
float fminf1(const float a, const float b)
{
return a < b ? a : b;
}
float fmaxf1(const float a, const float b)
{
return a > b ? a : b;
}
float clamp(const float f, const float a, const float b) {
return fmaxf1(a, fminf1(f, b));
}
float3 operator-(float3 a, float3 b)
{
return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
}
float3 operator+(float3 a, float b)
{
return make_float3(a.x + b, a.y + b, a.z + b);
}
void operator+=(float3 &a, float b)
{
a.x += b; a.y += b; a.z += b;
}
float3 operator*(float3 a, float b)
{
return make_float3(a.x * b, a.y * b, a.z * b);
}
float length(float3 v)
{
return sqrtf(dot(v, v));
}
void operator+=(float3 &a, float3 b)
{
a.x += b.x; a.y += b.y; a.z += b.z;
}
float3 operator/(float3 a, float b)
{
return make_float3(a.x / b, a.y / b, a.z / b);
}
void operator/=(float3 &a, float3 b)
{
a.x /= b.x; a.y /= b.y; a.z /= b.z;
}
void operator/=(float3 &a, float b)
{
a.x /= b; a.y /= b; a.z /= b;
}
float3 get_float3( float4 &v ) {
return make_float3( v.x, v.y, v.z );
} ;
float3 operator/(float b, float3 a) {
return make_float3( b/a.x, b/a.y, b/a.z );
};
float4 operator*(float4 a, float b) {
return make_float4(b * a.x, b * a.y, b * a.z, b * a.w );
} ;
float4 lerp(const float4& a, const float4& b, const float t) { return (float4)(a * ((float)1 - t) + b * t); };
float fminf1(float a, float b, float c) { return fminf1(fminf1(a, b), c); };
float fmaxf1(float a, float b, float c) { return fmaxf1(fmaxf1(a, b), c); };
int3 make_int3 ( int x, int y, int z ) {
return int3( x, y, z );
} ;
int3 operator-(int3 &a, int3 &b) {
return int3(a.x - b.x, a.y - b.y, a.z - b.z );
} ;
int3 operator*(int3 a, float3 b) {
return int3(a.x * b.x, a.y * b.y, a.z * b.z );
} ;
float3 operator*(float3 a, float3 b) {
return float3(a.x * b.x, a.y * b.y, a.z * b.z );
} ;
int3 min3i ( const int3 &v0, const int a ) {
return int3( min(v0.x,a), min(v0.y,a), min(v0.z,a) );
} ;
int3 max3i ( const int3 &v0, const int a ) {
return int3( max(v0.x,a), max(v0.y,a), max(v0.z,a) );
} ;
int3 clamp(const int3 i3, const int a, const int b) {
return max3i(min3i(i3, b), a);
} ;
int3 max3i ( const int3 &v0, const int3 &a ) {
return int3( max(v0.x,a.x), max(v0.y,a.y), max(v0.z,a.z) );
} ;
int3 clamp(const int3 i3, const int3 &a, const int b) {
return max3i(min3i(i3, b), a);
} ;
int3 make_int3 ( const float3 & a ) {
return int3( a.x, a.y, a.z );
} ;
float lerp(const float a, const float b, const float t) { return (float)(a * ((float)1 - t) + b * t); };
float fminf1 ( const float3& a ) {
return fminf1 ( a.x, a.y, a.z );
};
float fmaxf1 ( const float3& a ) {
return fmaxf1 ( a.x, a.y, a.z );
};
float fsumf ( const float3& a ) {
return a.x + a.y + a.z;
};
float3 make_float3 ( const float4& v ) {
return float3 ( v.x, v.y, v.z );
} ;
float4 make_float4 ( const float3& v ) {
return float4 ( v.x, v.y, v.z, 1.0f );
} ;
float4& operator+=(float4 &a, float4 &b) {
a = a + b;
return a;
}
//#endif /* _VEC_MATH */