This is really only a half intersection test.
| 178 | |
| 179 | // This is really only a half intersection test. |
| 180 | bool Obb::halfIntersect(const Obb& a, Obb b) |
| 181 | { |
| 182 | // Translate both boxes so that this box is at the origin. |
| 183 | b.m_p -= a.m_p; |
| 184 | |
| 185 | // Rotate the clip box by the inverse of this box's rotation to |
| 186 | // bring it to the same relative location as this box unrotated. |
| 187 | b.m_p = math::rotate(b.m_p, a.m_quat.inverse()); |
| 188 | |
| 189 | // BOX3D representation of this OBB (translated to 0, 0, 0) |
| 190 | BOX3D box(-a.m_hx, -a.m_hy, -a.m_hz, a.m_hx, a.m_hy, a.m_hz); |
| 191 | |
| 192 | // If any of the clip box corners are in this box, we're done. |
| 193 | // While we're at it, store the min/max of corner points. |
| 194 | Eigen::Vector3d pmin((std::numeric_limits<double>::max)(), |
| 195 | (std::numeric_limits<double>::max)(), |
| 196 | (std::numeric_limits<double>::max)()); |
| 197 | Eigen::Vector3d pmax((std::numeric_limits<double>::lowest)(), |
| 198 | (std::numeric_limits<double>::lowest)(), |
| 199 | (std::numeric_limits<double>::lowest)()); |
| 200 | for (size_t i = 0; i < 8; ++i) |
| 201 | { |
| 202 | Eigen::Vector3d corner = b.corner(i); |
| 203 | if (box.contains(corner.x(), corner.y(), corner.z())) |
| 204 | return true; |
| 205 | |
| 206 | pmax.x() = (std::max)(pmax.x(), corner.x()); |
| 207 | pmin.x() = (std::min)(pmin.x(), corner.x()); |
| 208 | pmax.y() = (std::max)(pmax.y(), corner.y()); |
| 209 | pmin.y() = (std::min)(pmin.y(), corner.y()); |
| 210 | pmax.z() = (std::max)(pmax.z(), corner.z()); |
| 211 | pmin.z() = (std::min)(pmin.z(), corner.z()); |
| 212 | } |
| 213 | |
| 214 | // If the clip box surrounds this box, we're done. |
| 215 | if (pmax.x() >= a.m_hx && pmin.x() <= -a.m_hx && |
| 216 | pmax.y() >= a.m_hy && pmin.y() <= -a.m_hy && |
| 217 | pmax.z() >= a.m_hz && pmin.z() <= -a.m_hz) |
| 218 | return true; |
| 219 | |
| 220 | // If any of the segments that make up the clip region intersect |
| 221 | // this normalized box, we're done. |
| 222 | for (size_t i = 0; i < 12; ++i) |
| 223 | { |
| 224 | Segment s = b.segment(i); |
| 225 | if (a.intersectNormalized(s)) |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | // No intersection. |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // Note that this is just here to support the above. The box we're |
| 234 | // testing is treated as centered at the origin with faces parallel to |