generated from grellert/template-poo-ii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollisionKernel.cl
225 lines (168 loc) · 7.07 KB
/
CollisionKernel.cl
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
/*
def vector3_det(a:[float], b:[float]):
return a[0] * b[1] - a[1] * b[0]
*/
float vector3_det(float* a_g, float* b_g)
{
return a_g[0] * b_g[1] - a_g[1] * b_g[0];
}
/*
def vector3_is_inside(self_vec3:[float], edges:[[float]]):
count = 0
sx = self_vec3[0]
sy = self_vec3[1]
for e in edges:
(x1, y1) = (e[0][0], e[0][1])
(x2, y2) = (e[1][0], e[1][1])
if (sy < y1) != (sy < y2) and sx < x1 + ((sy-y1)/(y2-y1))*(x2-x1):
count += 1
return count%2==1
*/
bool vector3_is_inside(float* self_vec3, float*** edges, int edges_len)
{
int tid = get_global_id(0);
int count = 0;
float sx = self_vec3[0];
float sy = self_vec3[1];
for(int i = 0; i < edges_len; i++){
float** e = edges[i];
float x1 = e[0][0];
float y1 = e[0][1];
float x2 = e[1][0];
float y2 = e[1][1];
float temp = (x1 + ((sy-y1)/(y2-y1))*(x2-x1));
if (((sy < y1) != (sy < y2)) && (sx < temp)){
count += 1;
}
}
return count%2 == 1;
}
/*
def vector3_get_2d_point_intersection(vec3_pair_1:[[float]], vec3_pair_2:[[float]]):
vlst = (
((vec3_pair_1[0][0],vec3_pair_1[0][1]),(vec3_pair_1[1][0],vec3_pair_1[1][1])),
((vec3_pair_2[0][0],vec3_pair_2[0][1]),(vec3_pair_2[1][0],vec3_pair_2[1][1]))
)
xdiff = (vlst[0][0][0] - vlst[0][1][0], vlst[1][0][0] - vlst[1][1][0])
ydiff = (vlst[0][0][1] - vlst[0][1][1], vlst[1][0][1] - vlst[1][1][1])
div = Vector3.vector3_det(xdiff, ydiff)
if div == 0: return None # Linhas não se cruzam
d = (Vector3.vector3_det(*vlst[0]), Vector3.vector3_det(*vlst[1]))
x = Vector3.vector3_det(d, xdiff) / div
y = Vector3.vector3_det(d, ydiff) / div
for i in range(2):
if x > max(vlst[i][0][0], vlst[i][1][0]) or x < min(vlst[i][0][0], vlst[i][1][0]): return None
if y > max(vlst[i][0][1], vlst[i][1][1]) or y < min(vlst[i][0][1], vlst[i][1][1]): return None
return Vector3(x, y, 0) # Retorna as coordenadas do cruzamento
*/
float* vector3_get_2d_point_intersection(float** vec3_pair_1, float** vec3_pair_2){
float* result;
float*** vlst;
vlst[0][0] = vec3_pair_1[0];
vlst[0][1] = vec3_pair_1[1];
vlst[1][0] = vec3_pair_2[0];
vlst[1][1] = vec3_pair_2[1];
float* xdiff;
xdiff[0] = vlst[0][0][0] - vlst[0][1][0];
xdiff[1] = vlst[1][0][0] - vlst[1][1][0];
float* ydiff;
ydiff[0] = vlst[0][0][1] - vlst[0][1][1];
ydiff[1] = vlst[1][0][1] - vlst[1][1][1];
float div = vector3_det(xdiff, ydiff);
if (div == 0) return result;
//d = (Vector3.vector3_det(*vlst[0]), Vector3.vector3_det(*vlst[1]))
float* d;
d[0] = vector3_det(vlst[0][0], vlst[0][1]); //REVISAR !!!
d[1] = vector3_det(vlst[1][0], vlst[1][1]);
float x = vector3_det(d, xdiff) / div;
float y = vector3_det(d, ydiff) / div;
for (int i = 0; i < 2; i++){
if ((x > max(vlst[i][0][0], vlst[i][1][0])) || (x < min(vlst[i][0][0], vlst[i][1][0]))) return result;
if ((y > max(vlst[i][0][1], vlst[i][1][1])) || (y < min(vlst[i][0][1], vlst[i][1][1]))) return result;
}
result[0] = x;
result[1] = y;
return result;
}
/*
def get_projection_vec3_arrays(transform_len:float, rotation_axis:[float], vec_pair:[[float]]):
pv1 = vec_pair[0]
pv1[0] += transform_len * math.sin(rotation_axis[0])
pv1[1] += transform_len * math.cos(rotation_axis[0])
pv2 = vec_pair[1]
pv2[0] += transform_len * math.sin(rotation_axis[0])
pv2[1] += transform_len * math.cos(rotation_axis[0])
return [pv1, pv2]
*/
float** get_projection_vec3_arrays(float transform_len, float* rotation_axis, float** vec_pair){
float** result;
float* pv1;
float* pv2;
pv1 = vec_pair[0];
pv1[0] += transform_len * sin(rotation_axis[0]);
pv1[1] += transform_len * cos(rotation_axis[0]);
pv2 = vec_pair[1];
pv2[0] += transform_len * sin(rotation_axis[0]);
pv2[1] += transform_len * cos(rotation_axis[0]);
result[0] = pv1;
result[1] = pv2;
return result;
}
/*
def will_collide_2d(pv, vec_pair_1:[[float]], vec_pair_2:[[float]]):
if Vector3.vector3_get_2d_point_intersection([vec_pair_1[0], pv[0]], vec_pair_2): return True
if Vector3.vector3_get_2d_point_intersection([vec_pair_1[1], pv[1]], vec_pair_2): return True
if Vector3.vector3_get_2d_point_intersection([pv[0], pv[1]], vec_pair_2): return True
poly = [vec_pair_1, [vec_pair_1[0], pv[0]], [vec_pair_1[1], pv[1]], [pv[0], pv[1]]]
inside = Vector3.vector3_is_inside(vec_pair_2[0], poly) or Vector3.vector3_is_inside(vec_pair_2[1], poly)
return inside
*/
bool will_collide_2d(float** pv, float** vec_pair_1, float** vec_pair_2){
float** tmp;
tmp[0] = vec_pair_1[0];
tmp[1] = pv[0];
if (vector3_get_2d_point_intersection(tmp, vec_pair_2)) return true;
tmp[0] = vec_pair_1[1];
tmp[1] = pv[1];
if (vector3_get_2d_point_intersection(tmp, vec_pair_2)) return true;
tmp[0] = pv[0];
if (vector3_get_2d_point_intersection(tmp, vec_pair_2)) return true;
float*** poly;
poly[0] = vec_pair_1;
poly[1][0] = vec_pair_1[0];
poly[1][1] = pv[0];
poly[2][0] = vec_pair_1[1];
poly[2][1] = pv[1];
poly[3][0] = pv[0];
poly[3][1] = pv[1];
bool inside = (vector3_is_inside(vec_pair_2[0], poly, 4) || vector3_is_inside(vec_pair_2[1], poly, 4));
return inside;
}
/*
def will_collide(self, ref_position_from:Vector3, transform_len:float, rotation_axis:Vector3, ref_position_other:Vector3, other_polygon):
self_vector_list = self.get_vectors(ref_position_from)
other_vector_list = other_polygon.get_vectors(ref_position_other)
rot_axis_arr = rotation_axis.get_float_array()
last_v = self_vector_list[0]
for v in self_vector_list[1:]:
last_v2 = other_vector_list[0]
vec1_pair = [last_v.get_float_array(), v.get_float_array()]
pv = Vector3.get_projection_vec3_arrays(transform_len, rot_axis_arr, vec1_pair)
for v2 in other_vector_list[1:]:
if Vector3.will_collide_2d(pv, vec1_pair, [last_v2.get_float_array(), v2.get_float_array()]): return True
last_v2 = v2
last_v = v
return False
*/
__kernel void will_collide(
__global float* values, __global bool* ret
/* __global float* transform_len, __global float* rotation_axis,
__global float* ref_position_from, __global float* from_polygon_vec_lst, __global int* from_poly_vec_lst_count,
__global float* ref_position_other,__global float* other_polygon_vec_lst, __global int* other_poly_vec_lst_count */
)
{
int gid = get_global_id(0);
float mov_objs_count = values[0];
/* for (int i = 0; i < *from_poly_vec_lst_count; i++){
} */
}