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
335
336
337
#pragma once

#include <cstdint>
#include <vector>
#include <tuple>
#include <chrono>
#include <iostream>

using std::tuple;
using std::vector;

using T = float;
using T3 = tuple<T, T, T>;
using TSphere = tuple<T3, T>;
using TCapsule = tuple<T3, T3, T>;
using TSegment = tuple<T3, T3>;
using TTriangle = tuple<T3, T3, T3>;

constexpr T ZERO = (T)0.0;
constexpr T ONE = (T)1.0;
constexpr T EPSILON = (T)1e-6;

#define INLINE_S __forceinline
#define INLINE_L __forceinline

namespace by_copy {

    struct Vector3 {
        T x;
        T y;
        T z;

        INLINE_S Vector3() : x(ZERO), y(ZERO), z(ZERO) { }
        INLINE_S Vector3(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
        INLINE_S Vector3(Vector3 const&) = default;
        INLINE_S Vector3& operator=(Vector3 const&) = default;
    };

    INLINE_S Vector3 operator+(Vector3 a, Vector3 b) {
        return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    }

    INLINE_S Vector3 operator-(Vector3 a, Vector3 b) {
        return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    }

    INLINE_S Vector3 operator*(Vector3 a, T v) {
        return Vector3(a.x * v, a.y * v, a.z * v);
    }

    INLINE_S T dot(Vector3 a, Vector3 b) {
        return a.x * b.x + a.y * b.y + a.z * b.z;
    }

    INLINE_S Vector3 cross(Vector3 a, Vector3 b) {
        return Vector3(
            a.y * b.z - a.z * b.y,
            -(a.x * b.z - a.z * b.x),
            a.x * b.y - a.y * b.x
        );
    }

    INLINE_S T clamp(T value, T min, T max) {
        if (value <= min) {
            return min;
        }
        else if (value >= max) {
            return max;
        }
        else {
            return value;
        }
    }

    INLINE_S T clamp01(T value) {
        return clamp(value, ZERO, ONE);
    }

    INLINE_L T sq_dist_point_segment(Vector3 a, Vector3 b, Vector3 c) {
        auto ab = b - a;
        auto ac = c - a;
        auto bc = c - b;

        auto e = dot(ac, ab);
        if (e <= ZERO) {
            return dot(ac, ac);
        }

        auto f = dot(ab, ab);
        if (e >= f) {
            return dot(bc, bc);
        }

        return dot(ac, ac) - e * e / f;
    }

    INLINE_S bool test_sphere_capsule(Vector3 sphere_center, T sphere_rad, Vector3 capsule_a, Vector3 capsule_b, T capsule_rad) {
        auto dist_sq = sq_dist_point_segment(capsule_a, capsule_b, sphere_center);
        auto rad = sphere_rad + capsule_rad;
        auto result = dist_sq < rad * rad;
        return result;
    }

    INLINE_S bool test_sphere_sphere(Vector3 sphere_center_a, T sphere_rad_a, Vector3 sphere_center_b, T sphere_rad_b) {
        auto diff = sphere_center_b - sphere_center_a;
        auto dist2 = dot(diff, diff);
        auto rad = sphere_rad_a + sphere_rad_b;
        return dist2 <= rad * rad;
    }

    INLINE_L std::tuple<T, T, T, Vector3, Vector3> closest_pt_segment_segment(
        Vector3 p1, Vector3 q1, Vector3 p2, Vector3 q2)
    {
        T s = ZERO;
        T t = ZERO;

        auto d1 = q1 - p1;
        auto d2 = q2 - p2;
        auto r = p1 - p2;
        auto a = dot(d1, d1);
        auto e = dot(d2, d2);
        auto f = dot(d2, r);

        if (a <= EPSILON && e <= EPSILON) {
            auto result = dot(p1 - p2, p1 - p2);
            return std::make_tuple(result, s, t, p1, p2);
        }

        if (a <= EPSILON) {
            t = clamp01(f / e);
        }
        else {
            auto c = dot(d1, r);
            if (e <= EPSILON) {
                s = clamp01(-c / a);
            }
            else {
                auto b = dot(d1, d2);
                auto denom = a * e - b * b;

                if (denom != ZERO) {
                    s = clamp01((b * f - c * e) / denom);
                }
                else {
                    t = (b * s + f) / e;
                    if (t < ZERO) {
                        t = ZERO;
                        s = clamp01(-c / a);
                    }
                    else if (t > ONE) {
                        t = ONE;
                        s = clamp01((b - c) / a);
                    }
                }
            }
        }

        auto c1 = p1 + d1 * s;
        auto c2 = p2 + d2 * t;
        auto result = dot(c1 - c2, c1 - c2);
        return std::make_tuple(result, s, t, c1, c2);
    }

    INLINE_S bool test_capsule_capsule(Vector3 capsule0_a, Vector3 capsule0_b, T capsule0_rad,
        Vector3 capsule1_a, Vector3 capsule1_b, T capsule1_rad)
    {
        auto result = closest_pt_segment_segment(capsule0_a, capsule0_b, capsule1_a, capsule1_b);
        auto dist_sq = std::get<0>(result);
        auto rad = capsule0_rad + capsule1_rad;
        auto overlaps = dist_sq <= rad * rad;
        return overlaps;
    }

    INLINE_L bool test_segment_triangle(Vector3 const& p, Vector3 const& q, Vector3 const& a, Vector3 const& b, Vector3 const& c) {
        T u, v, w, t;

        auto ab = b - a;
        auto ac = c - a;
        auto qp = p - q;

        auto n = cross(ab, ac);
        auto d = dot(qp, n);

        if (d <= ZERO) {
            return false;
        }

        auto ap = p - a;
        t = dot(ap, n);
        if (t <= ZERO || t > d) {
            return false;
        }

        auto e = cross(qp, ap);
        v = dot(ac, e);
        if (v < ZERO || v > d) {
            return false;
        }

        w = -dot(ab, e);
        if (w < ZERO || v + w > d) {
            return false;
        }

        auto inv_d = ONE / d;
        t *= inv_d;
        v *= inv_d;
        w *= inv_d;
        u = ONE - v - w;
        return true;
    }

    INLINE_S Vector3 ToVec3(T3 v) {
        return Vector3(std::get<0>(v), std::get<1>(v), std::get<2>(v));
    }

    struct Sphere {
        Vector3 center;
        T radius;
        Sphere(Vector3 c, T r) : center(c), radius(r) {}
    };

    struct Capsule {
        Vector3 a, b;
        T radius;
        Capsule(Vector3 _a, Vector3 _b, T r) : a(_a), b(_b), radius(r) {}
    };

    struct Segment {
        Vector3 a, b;
        Segment(Vector3 _a, Vector3 _b) : a(_a), b(_b) {}
    };

    struct Triangle {
        Vector3 a, b, c;
        Triangle(Vector3 _a, Vector3 _b, Vector3 _c) : a(_a), b(_b), c(_c) {}
    };

    std::tuple<int64_t, int64_t> run_test(
        vector<TSphere> const& _spheres,
        vector<TCapsule> const& _capsules,
        vector<TSegment> const& _segments,
        vector<TTriangle> const& _triangles) 
    {
        int64_t num_overlaps = 0;
        int64_t milliseconds = 0;

        vector<Sphere> spheres;
        spheres.reserve(_spheres.size());
        for (auto& sphere : _spheres) {
            spheres.emplace_back(ToVec3(std::get<0>(sphere)), std::get<1>(sphere));
        }

        vector<Capsule> capsules;
        capsules.reserve(_capsules.size());
        for (auto& capsule : _capsules) {
            capsules.emplace_back(
                ToVec3(std::get<0>(capsule)),
                ToVec3(std::get<1>(capsule)),
                std::get<2>(capsule));
        }

        vector<Segment> segments;
        segments.reserve(_segments.size());
        for (auto& segment : _segments) {
            segments.emplace_back(
                ToVec3(std::get<0>(segment)),
                ToVec3(std::get<1>(segment)));
        }

        vector<Triangle> triangles;
        triangles.reserve(_triangles.size());
        for (auto const& triangle : _triangles) {
            triangles.emplace_back(
                ToVec3(std::get<0>(triangle)),
                ToVec3(std::get<1>(triangle)),
                ToVec3(std::get<2>(triangle)));
        }

        auto start = std::chrono::high_resolution_clock::now();

        // All sphere-sphere intersections
        for (int i = 0; i < spheres.size(); ++i) {
            auto a = spheres[i];
            for (int j = i + 1; j < spheres.size(); ++j) {
                auto const& b = spheres[j];
                bool overlap = test_sphere_sphere(a.center, a.radius, b.center, b.radius);
                if (overlap) {
                    num_overlaps += 1;
                }
            }
        }

        // All sphere-capsule
        for (int i = 0; i < spheres.size(); ++i) {
            auto s = spheres[i];
            for (int j = 0; j < capsules.size(); ++j) {
                auto const& c = capsules[j];
                bool overlap = test_sphere_capsule(s.center, s.radius, c.a, c.b, c.radius);
                if (overlap) {
                    num_overlaps += 1;
                }
            }
        }

        // All capsule-capsule
        for (int i = 0; i < capsules.size(); ++i) {
            auto c1 = capsules[i];
            for (int j = i + 1; j < capsules.size(); ++j) {
                auto const& c2 = capsules[j];
                bool overlap = test_capsule_capsule(c1.a, c1.b, c1.radius, c2.a, c2.b, c2.radius);
                if (overlap) {
                    num_overlaps += 1;
                }
            }
        }

        // All segment-triangle
        for (int i = 0; i < segments.size(); ++i) {
            auto s = segments[i];
            for (int j = 0; j < triangles.size(); ++j) {
                auto const& t = triangles[j];
                bool overlap = test_segment_triangle(s.a, s.b, t.a, t.b, t.c);
                if (overlap) {
                    num_overlaps += 1;
                }
            }
        }

        auto end = std::chrono::high_resolution_clock::now();
        auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        milliseconds += elapsed_ms.count();

        return std::make_tuple(num_overlaps, milliseconds);
    }

} // namespace by_copy