1 #include "geometry.hpp"
11 const glm::dmat4 &b_m,
15 glm::dvec3 a_corners[8] = {
16 glm::dvec3(a_m * glm::dvec4(a_box.min.x, a_box.min.y, a_box.min.z, 1.0)),
17 glm::dvec3(a_m * glm::dvec4(a_box.min.x, a_box.min.y, a_box.max.z, 1.0)),
18 glm::dvec3(a_m * glm::dvec4(a_box.min.x, a_box.max.y, a_box.min.z, 1.0)),
19 glm::dvec3(a_m * glm::dvec4(a_box.min.x, a_box.max.y, a_box.max.z, 1.0)),
20 glm::dvec3(a_m * glm::dvec4(a_box.max.x, a_box.min.y, a_box.min.z, 1.0)),
21 glm::dvec3(a_m * glm::dvec4(a_box.max.x, a_box.min.y, a_box.max.z, 1.0)),
22 glm::dvec3(a_m * glm::dvec4(a_box.max.x, a_box.max.y, a_box.min.z, 1.0)),
23 glm::dvec3(a_m * glm::dvec4(a_box.max.x, a_box.max.y, a_box.max.z, 1.0)),
26 glm::dvec3 b_corners[8] = {
27 glm::dvec3(b_m * glm::dvec4(b_box.min.x, b_box.min.y, b_box.min.z, 1.0)),
28 glm::dvec3(b_m * glm::dvec4(b_box.min.x, b_box.min.y, b_box.max.z, 1.0)),
29 glm::dvec3(b_m * glm::dvec4(b_box.min.x, b_box.max.y, b_box.min.z, 1.0)),
30 glm::dvec3(b_m * glm::dvec4(b_box.min.x, b_box.max.y, b_box.max.z, 1.0)),
31 glm::dvec3(b_m * glm::dvec4(b_box.max.x, b_box.min.y, b_box.min.z, 1.0)),
32 glm::dvec3(b_m * glm::dvec4(b_box.max.x, b_box.min.y, b_box.max.z, 1.0)),
33 glm::dvec3(b_m * glm::dvec4(b_box.max.x, b_box.max.y, b_box.min.z, 1.0)),
34 glm::dvec3(b_m * glm::dvec4(b_box.max.x, b_box.max.y, b_box.max.z, 1.0)),
37 glm::dvec3 axes[15] = {
44 normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[0]))),
45 normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[1]))),
46 normalize(cross(glm::dvec3(a_m[0]), glm::dvec3(b_m[2]))),
47 normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[0]))),
48 normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[1]))),
49 normalize(cross(glm::dvec3(a_m[1]), glm::dvec3(b_m[2]))),
50 normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[0]))),
51 normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[1]))),
52 normalize(cross(glm::dvec3(a_m[2]), glm::dvec3(b_m[2]))),
55 depth = std::numeric_limits<double>::infinity();
59 for (const glm::dvec3 &axis : axes) {
60 if (any(isnan(axis))) {
61 // can result from the cross products if A and B have parallel axes
65 double a_min = std::numeric_limits<double>::infinity();
66 double a_max = -std::numeric_limits<double>::infinity();
67 for (const glm::dvec3 &corner : a_corners) {
68 double val = dot(corner, axis);
69 a_min = std::min(a_min, val);
70 a_max = std::max(a_max, val);
73 double b_min = std::numeric_limits<double>::infinity();
74 double b_max = -std::numeric_limits<double>::infinity();
75 for (const glm::dvec3 &corner : b_corners) {
76 double val = dot(corner, axis);
77 b_min = std::min(b_min, val);
78 b_max = std::max(b_max, val);
81 if (a_max < b_min || b_max < a_min) return false;
83 double overlap = std::min(a_max, b_max) - std::max(a_min, b_min);
84 if (overlap < depth) {
92 normal = axes[min_axis];