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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include <gpu/vectorTypes.hpp>
#include <stdint.h>
#include <math.h>
#ifdef VS_OPENCL
#define CUDART_PI_F 3.141592654f
#define __device__
#define __host__
#define __restrict__
#else
#include <math_constants.h>
#endif
inline __device__ __host__ float degToRad(float v) { return CUDART_PI_F * (v / 180.0f); }
inline __device__ __host__ float radToDeg(float v) { return v * (180.0f / CUDART_PI_F); }
inline __device__ __host__ float2 operator+(float2 a, float2 b) { return make_float2(a.x + b.x, a.y + b.y); }
inline __device__ __host__ int2 operator+(int2 a, int2 b) { return make_int2(a.x + b.x, a.y + b.y); }
inline __device__ __host__ float2 operator-(float2 a, float2 b) { return make_float2(a.x - b.x, a.y - b.y); }
inline __device__ __host__ float2 operator*(float2 a, float2 b) { return make_float2(a.x * b.x, a.y * b.y); }
inline __device__ __host__ int2 operator-(int2 a, int2 b) { return make_int2(a.x - b.x, a.y - b.y); }
inline __device__ __host__ float2 operator*(float a, float2 v) { return make_float2(a * v.x, a * v.y); }
inline __device__ __host__ float3 operator*(float a, float3 v) { return make_float3(a * v.x, a * v.y, a * v.z); }
inline __device__ __host__ float3 operator+(float3 a, float3 b) { return make_float3(a.x + b.x, a.y + b.y, a.z + b.z); }
inline __device__ __host__ float3 operator-(float3 a, float3 b) { return make_float3(a.x - b.x, a.y - b.y, a.z - b.z); }
inline __device__ __host__ void operator+=(float2 &a, float2 b) {
a.x += b.x;
a.y += b.y;
}
inline __device__ __host__ void operator+=(float3 &a, float3 b) {
a.x += b.x;
a.y += b.y;
a.z += b.z;
}
inline __device__ __host__ void operator-=(float2 &a, float2 b) {
a.x -= b.x;
a.y -= b.y;
}
inline __device__ __host__ void operator+=(float4 &a, float4 b) {
a.x += b.x;
a.y += b.y;
a.z += b.z;
a.w += b.w;
}
inline __device__ __host__ float dot(float3 a, float3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline __device__ __host__ 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);
}
inline __device__ __host__ void operator*=(float2 &v, float a) {
v.x *= a;
v.y *= a;
}
inline __device__ __host__ void operator*=(float2 &v, float2 a) {
v.x *= a.x;
v.y *= a.y;
}
inline __device__ __host__ void operator*=(float3 &v, float a) {
v.x *= a;
v.y *= a;
v.z *= a;
}
inline __device__ __host__ void operator/=(float2 &v, float a) {
v.x /= a;
v.y /= a;
}
inline __device__ __host__ void operator/=(float2 &v, float2 a) {
v.x /= a.x;
v.y /= a.y;
}
inline __device__ __host__ void operator/=(float3 &v, float a) {
v.x /= a;
v.y /= a;
v.z /= a;
}
inline __device__ __host__ void operator/=(float4 &v, float a) {
v.x /= a;
v.y /= a;
v.z /= a;
v.w /= a;
}
inline __device__ __host__ float2 operator*(float2 a, float s) { return make_float2(a.x * s, a.y * s); }
// divide
inline __device__ __host__ float2 operator/(float2 a, float s) { return make_float2(a.x / s, a.y / s); }
inline __device__ __host__ float2 operator/(float2 a, float2 b) { return make_float2(a.x / b.x, a.y / b.y); }
inline __device__ __host__ int2 operator/(int2 a, int2 b) { return make_int2(a.x / b.x, a.y / b.y); }
inline __device__ __host__ bool operator==(float2 a, float2 b) { return (a.x == b.x && a.y == b.y); }
inline __host__ void operator*=(double3 &v, double a) {
v.x *= a;
v.y *= a;
v.z *= a;
}
inline __host__ void operator/=(double3 &v, double a) {
v.x /= a;
v.y /= a;
v.z /= a;
}
inline __device__ __host__ float length(float2 v) {
#ifdef __CUDA_ARCH__
return __fsqrt_rn(v.x * v.x + v.y * v.y);
#else
return sqrtf(v.x * v.x + v.y * v.y);
#endif
}
inline __device__ __host__ float length(float3 v) {
#ifdef __CUDA_ARCH__
return __fsqrt_rn(v.x * v.x + v.y * v.y + v.z * v.z);
#else
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
#endif
}
inline __device__ __host__ float invLength(float2 v) {
#ifdef __CUDA_ARCH__
return rsqrtf(v.x * v.x + v.y * v.y);
#else
return 1.0f / sqrtf(v.x * v.x + v.y * v.y);
#endif
}
inline __device__ __host__ float invLength(float3 v) {
#ifdef __CUDA_ARCH__
return rsqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
#else
return 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
#endif
}
inline __device__ __host__ float clampf(const float input, const float minValue, const float maxValue) {
#ifdef __CUDA_ARCH__
return fmin(fmax(input, minValue), maxValue);
#else
return input < minValue ? minValue : (input > maxValue ? maxValue : input);
#endif
}
inline __device__ __host__ float2 normalize(float2 v) {
float norm = invLength(v);
v.x *= norm;
v.y *= norm;
return v;
}
inline __device__ __host__ float3 normalize(float3 v) {
float norm = invLength(v);
v.x *= norm;
v.y *= norm;
v.z *= norm;
return v;
}
inline __device__ __host__ float2 clampf(const float2 input, const float minValue, const float maxValue) {
float2 output;
output.x = clampf(input.x, minValue, maxValue);
output.y = clampf(input.y, minValue, maxValue);
return output;
}