2014-08-08 00:00:58 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "3d/CCOBB.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-08-08 00:00:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#define ROTATE(a, i, j, k, l) \
|
|
|
|
g = a.m[i + 4 * j]; \
|
|
|
|
h = a.m[k + 4 * l]; \
|
|
|
|
a.m[i + 4 * j] = (float)(g - s * (h + g * tau)); \
|
|
|
|
a.m[k + 4 * l] = (float)(h + s * (g - h * tau));
|
2014-08-08 00:00:58 +08:00
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
static Mat4 _getConvarianceMatrix(const Vec3* vertPos, int vertCount)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Mat4 Cov;
|
|
|
|
|
|
|
|
double S1[3];
|
|
|
|
double S2[3][3];
|
|
|
|
|
|
|
|
S1[0] = S1[1] = S1[2] = 0.0;
|
|
|
|
S2[0][0] = S2[1][0] = S2[2][0] = 0.0;
|
|
|
|
S2[0][1] = S2[1][1] = S2[2][1] = 0.0;
|
|
|
|
S2[0][2] = S2[1][2] = S2[2][2] = 0.0;
|
|
|
|
|
|
|
|
// get center of mass
|
2021-12-25 10:04:45 +08:00
|
|
|
for (i = 0; i < vertCount; i++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2014-08-11 10:40:34 +08:00
|
|
|
S1[0] += vertPos[i].x;
|
|
|
|
S1[1] += vertPos[i].y;
|
|
|
|
S1[2] += vertPos[i].z;
|
|
|
|
|
|
|
|
S2[0][0] += vertPos[i].x * vertPos[i].x;
|
|
|
|
S2[1][1] += vertPos[i].y * vertPos[i].y;
|
|
|
|
S2[2][2] += vertPos[i].z * vertPos[i].z;
|
|
|
|
S2[0][1] += vertPos[i].x * vertPos[i].y;
|
|
|
|
S2[0][2] += vertPos[i].x * vertPos[i].z;
|
|
|
|
S2[1][2] += vertPos[i].y * vertPos[i].z;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
float n = (float)vertCount;
|
2014-08-08 00:00:58 +08:00
|
|
|
// now get covariances
|
2021-12-25 10:04:45 +08:00
|
|
|
Cov.m[0] = (float)(S2[0][0] - S1[0] * S1[0] / n) / n;
|
|
|
|
Cov.m[5] = (float)(S2[1][1] - S1[1] * S1[1] / n) / n;
|
|
|
|
Cov.m[10] = (float)(S2[2][2] - S1[2] * S1[2] / n) / n;
|
|
|
|
Cov.m[4] = (float)(S2[0][1] - S1[0] * S1[1] / n) / n;
|
|
|
|
Cov.m[9] = (float)(S2[1][2] - S1[1] * S1[2] / n) / n;
|
|
|
|
Cov.m[8] = (float)(S2[0][2] - S1[0] * S1[2] / n) / n;
|
|
|
|
Cov.m[1] = Cov.m[4];
|
|
|
|
Cov.m[2] = Cov.m[8];
|
|
|
|
Cov.m[6] = Cov.m[9];
|
2014-08-08 00:00:58 +08:00
|
|
|
|
|
|
|
return Cov;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static float& _getElement(Vec3& point, int index)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
if (index == 0)
|
|
|
|
return point.x;
|
|
|
|
if (index == 1)
|
|
|
|
return point.y;
|
|
|
|
if (index == 2)
|
|
|
|
return point.z;
|
|
|
|
|
|
|
|
CC_ASSERT(0);
|
|
|
|
return point.x;
|
|
|
|
}
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
int n = 3;
|
2021-12-25 10:04:45 +08:00
|
|
|
int j, iq, ip, i;
|
2014-08-11 10:40:34 +08:00
|
|
|
double tresh, theta, tau, t, sm, s, h, g, c;
|
2014-08-08 00:00:58 +08:00
|
|
|
int nrot;
|
|
|
|
Vec3 b;
|
|
|
|
Vec3 z;
|
|
|
|
Mat4 v;
|
|
|
|
Vec3 d;
|
|
|
|
|
|
|
|
v = Mat4::IDENTITY;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ip = 0; ip < n; ip++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
_getElement(b, ip) = a.m[ip + 4 * ip];
|
|
|
|
_getElement(d, ip) = a.m[ip + 4 * ip];
|
|
|
|
_getElement(z, ip) = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nrot = 0;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (i = 0; i < 50; i++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2014-08-11 10:40:34 +08:00
|
|
|
sm = 0.0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ip = 0; ip < n; ip++)
|
|
|
|
for (iq = ip + 1; iq < n; iq++)
|
|
|
|
sm += fabs(a.m[ip + 4 * iq]);
|
|
|
|
if (fabs(sm) < FLT_EPSILON)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
v.transpose();
|
|
|
|
*vout = v;
|
|
|
|
*dout = d;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
if (i < 3)
|
2021-12-25 10:04:45 +08:00
|
|
|
tresh = 0.2 * sm / (n * n);
|
|
|
|
else
|
2014-08-11 10:40:34 +08:00
|
|
|
tresh = 0.0;
|
2014-08-08 00:00:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ip = 0; ip < n; ip++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (iq = ip + 1; iq < n; iq++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
g = 100.0 * fabs(a.m[ip + iq * 4]);
|
2014-08-08 00:00:58 +08:00
|
|
|
float dmip = _getElement(d, ip);
|
|
|
|
float dmiq = _getElement(d, iq);
|
2014-08-11 10:40:34 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (i > 3 && fabs(dmip) + g == fabs(dmip) && fabs(dmiq) + g == fabs(dmiq))
|
2014-08-11 10:40:34 +08:00
|
|
|
{
|
|
|
|
a.m[ip + 4 * iq] = 0.0;
|
|
|
|
}
|
|
|
|
else if (fabs(a.m[ip + 4 * iq]) > tresh)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2014-08-11 10:40:34 +08:00
|
|
|
h = dmiq - dmip;
|
|
|
|
if (fabs(h) + g == fabs(h))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
t = (a.m[ip + 4 * iq]) / h;
|
2014-08-11 10:40:34 +08:00
|
|
|
}
|
2014-08-08 00:00:58 +08:00
|
|
|
else
|
|
|
|
{
|
2014-08-11 10:40:34 +08:00
|
|
|
theta = 0.5 * h / (a.m[ip + 4 * iq]);
|
2021-12-25 10:04:45 +08:00
|
|
|
t = 1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
|
|
|
|
if (theta < 0.0)
|
|
|
|
t = -t;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
c = 1.0 / sqrt(1 + t * t);
|
|
|
|
s = t * c;
|
|
|
|
tau = s / (1.0 + c);
|
|
|
|
h = t * a.m[ip + 4 * iq];
|
2014-08-08 00:00:58 +08:00
|
|
|
_getElement(z, ip) -= (float)h;
|
|
|
|
_getElement(z, iq) += (float)h;
|
|
|
|
_getElement(d, ip) -= (float)h;
|
|
|
|
_getElement(d, iq) += (float)h;
|
2021-12-25 10:04:45 +08:00
|
|
|
a.m[ip + 4 * iq] = 0.0;
|
|
|
|
for (j = 0; j < ip; j++)
|
|
|
|
{
|
|
|
|
ROTATE(a, j, ip, j, iq);
|
|
|
|
}
|
|
|
|
for (j = ip + 1; j < iq; j++)
|
|
|
|
{
|
|
|
|
ROTATE(a, ip, j, j, iq);
|
|
|
|
}
|
|
|
|
for (j = iq + 1; j < n; j++)
|
|
|
|
{
|
|
|
|
ROTATE(a, ip, j, iq, j);
|
|
|
|
}
|
|
|
|
for (j = 0; j < n; j++)
|
|
|
|
{
|
|
|
|
ROTATE(v, j, ip, j, iq);
|
|
|
|
}
|
2014-08-08 00:00:58 +08:00
|
|
|
nrot++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 10:40:34 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ip = 0; ip < n; ip++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
_getElement(b, ip) += _getElement(z, ip);
|
|
|
|
_getElement(d, ip) = _getElement(b, ip);
|
|
|
|
_getElement(z, ip) = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v.transpose();
|
|
|
|
*vout = v;
|
|
|
|
*dout = d;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
static Mat4 _getOBBOrientation(const Vec3* vertPos, int num)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
Mat4 Cov;
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
if (num <= 0)
|
2014-08-08 00:00:58 +08:00
|
|
|
return Mat4::IDENTITY;
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
Cov = _getConvarianceMatrix(vertPos, num);
|
2014-08-08 00:00:58 +08:00
|
|
|
|
|
|
|
// now get eigenvectors
|
|
|
|
Mat4 Evecs;
|
|
|
|
Vec3 Evals;
|
2014-08-11 10:40:34 +08:00
|
|
|
_getEigenVectors(&Evecs, &Evals, Cov);
|
2014-08-08 00:00:58 +08:00
|
|
|
|
|
|
|
Evecs.transpose();
|
|
|
|
|
|
|
|
return Evecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
OBB::OBB()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
OBB::OBB(const AABB& aabb)
|
|
|
|
{
|
|
|
|
reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
_center = (aabb._min + aabb._max);
|
|
|
|
_center.scale(0.5f);
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_xAxis.set(1.0f, 0.0f, 0.0f);
|
|
|
|
_yAxis.set(0.0f, 1.0f, 0.0f);
|
|
|
|
_zAxis.set(0.0f, 0.0f, 1.0f);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
_extents = aabb._max - aabb._min;
|
|
|
|
_extents.scale(0.5f);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-23 13:53:23 +08:00
|
|
|
computeExtAxis();
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
OBB::OBB(const Vec3* verts, int num)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!verts)
|
|
|
|
return;
|
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
Mat4 matTransform = _getOBBOrientation(verts, num);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
// For matTransform is orthogonal, so the inverse matrix is just rotate it;
|
|
|
|
matTransform.transpose();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
Vec3 vecMax = matTransform * Vec3(verts[0].x, verts[0].y, verts[0].z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
Vec3 vecMin = vecMax;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-11 10:40:34 +08:00
|
|
|
for (int i = 1; i < num; i++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
Vec3 vect = matTransform * Vec3(verts[i].x, verts[i].y, verts[i].z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
vecMax.x = vecMax.x > vect.x ? vecMax.x : vect.x;
|
|
|
|
vecMax.y = vecMax.y > vect.y ? vecMax.y : vect.y;
|
|
|
|
vecMax.z = vecMax.z > vect.z ? vecMax.z : vect.z;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
vecMin.x = vecMin.x < vect.x ? vecMin.x : vect.x;
|
|
|
|
vecMin.y = vecMin.y < vect.y ? vecMin.y : vect.y;
|
|
|
|
vecMin.z = vecMin.z < vect.z ? vecMin.z : vect.z;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
matTransform.transpose();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_xAxis.set(matTransform.m[0], matTransform.m[1], matTransform.m[2]);
|
|
|
|
_yAxis.set(matTransform.m[4], matTransform.m[5], matTransform.m[6]);
|
|
|
|
_zAxis.set(matTransform.m[8], matTransform.m[9], matTransform.m[10]);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_center = 0.5f * (vecMax + vecMin);
|
2014-08-08 00:00:58 +08:00
|
|
|
_center *= matTransform;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
_xAxis.normalize();
|
|
|
|
_yAxis.normalize();
|
|
|
|
_zAxis.normalize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
_extents = 0.5f * (vecMax - vecMin);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-23 13:53:23 +08:00
|
|
|
computeExtAxis();
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OBB::containPoint(const Vec3& point) const
|
|
|
|
{
|
|
|
|
Vec3 vd = point - _center;
|
|
|
|
|
|
|
|
float d = vd.dot(_xAxis);
|
|
|
|
if (d > _extents.x || d < -_extents.x)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
d = vd.dot(_yAxis);
|
|
|
|
if (d > _extents.y || d < -_extents.y)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
d = vd.dot(_zAxis);
|
|
|
|
if (d > _extents.z || d < -_extents.z)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBB::set(const Vec3& center, const Vec3& xAxis, const Vec3& yAxis, const Vec3& zAxis, const Vec3& extents)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_center = center;
|
|
|
|
_xAxis = xAxis;
|
|
|
|
_yAxis = yAxis;
|
|
|
|
_zAxis = zAxis;
|
2014-08-08 00:00:58 +08:00
|
|
|
_extents = extents;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBB::reset()
|
|
|
|
{
|
|
|
|
memset(this, 0, sizeof(OBB));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBB::getCorners(Vec3* verts) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
verts[0] = _center - _extentX + _extentY + _extentZ; // left top front
|
|
|
|
verts[1] = _center - _extentX - _extentY + _extentZ; // left bottom front
|
|
|
|
verts[2] = _center + _extentX - _extentY + _extentZ; // right bottom front
|
|
|
|
verts[3] = _center + _extentX + _extentY + _extentZ; // right top front
|
|
|
|
|
|
|
|
verts[4] = _center + _extentX + _extentY - _extentZ; // right top back
|
|
|
|
verts[5] = _center + _extentX - _extentY - _extentZ; // right bottom back
|
|
|
|
verts[6] = _center - _extentX - _extentY - _extentZ; // left bottom back
|
|
|
|
verts[7] = _center - _extentX + _extentY - _extentZ; // left top back
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float OBB::projectPoint(const Vec3& point, const Vec3& axis) const
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
float dot = axis.dot(point);
|
|
|
|
float ret = dot * point.length();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void OBB::getInterval(const OBB& box, const Vec3& axis, float& min, float& max) const
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
Vec3 corners[8];
|
|
|
|
box.getCorners(corners);
|
|
|
|
float value;
|
|
|
|
min = max = projectPoint(axis, corners[0]);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 1; i < 8; i++)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
value = projectPoint(axis, corners[i]);
|
2021-12-25 10:04:45 +08:00
|
|
|
min = MIN(min, value);
|
|
|
|
max = MAX(max, value);
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 OBB::getEdgeDirection(int index) const
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
Vec3 corners[8];
|
|
|
|
getCorners(corners);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
Vec3 tmpLine;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (index)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 0: // edge with x axis
|
|
|
|
tmpLine = corners[5] - corners[6];
|
|
|
|
tmpLine.normalize();
|
|
|
|
break;
|
|
|
|
case 1: // edge with y axis
|
|
|
|
tmpLine = corners[7] - corners[6];
|
|
|
|
tmpLine.normalize();
|
|
|
|
break;
|
|
|
|
case 2: // edge with z axis
|
|
|
|
tmpLine = corners[1] - corners[6];
|
|
|
|
tmpLine.normalize();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(0, "Invalid index!");
|
|
|
|
break;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
return tmpLine;
|
|
|
|
}
|
|
|
|
|
2014-08-14 13:15:37 +08:00
|
|
|
Vec3 OBB::getFaceDirection(int index) const
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
|
|
|
Vec3 corners[8];
|
|
|
|
getCorners(corners);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-14 13:15:37 +08:00
|
|
|
Vec3 faceDirection, v0, v1;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (index)
|
2014-08-08 00:00:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 0: // front and back
|
|
|
|
v0 = corners[2] - corners[1];
|
|
|
|
v1 = corners[0] - corners[1];
|
|
|
|
Vec3::cross(v0, v1, &faceDirection);
|
|
|
|
faceDirection.normalize();
|
|
|
|
break;
|
|
|
|
case 1: // left and right
|
|
|
|
v0 = corners[5] - corners[2];
|
|
|
|
v1 = corners[3] - corners[2];
|
|
|
|
Vec3::cross(v0, v1, &faceDirection);
|
|
|
|
faceDirection.normalize();
|
|
|
|
break;
|
|
|
|
case 2: // top and bottom
|
|
|
|
v0 = corners[1] - corners[2];
|
|
|
|
v1 = corners[5] - corners[2];
|
|
|
|
Vec3::cross(v0, v1, &faceDirection);
|
|
|
|
faceDirection.normalize();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CCASSERT(0, "Invalid index!");
|
|
|
|
break;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
2014-08-14 13:15:37 +08:00
|
|
|
return faceDirection;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OBB::intersects(const OBB& box) const
|
|
|
|
{
|
|
|
|
float min1, max1, min2, max2;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2014-08-14 13:15:37 +08:00
|
|
|
getInterval(*this, getFaceDirection(i), min1, max1);
|
|
|
|
getInterval(box, getFaceDirection(i), min2, max2);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (max1 < min2 || max2 < min1)
|
|
|
|
return false;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2014-08-14 13:15:37 +08:00
|
|
|
getInterval(*this, box.getFaceDirection(i), min1, max1);
|
|
|
|
getInterval(box, box.getFaceDirection(i), min2, max2);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (max1 < min2 || max2 < min1)
|
|
|
|
return false;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
Vec3 axis;
|
2014-10-21 17:22:42 +08:00
|
|
|
Vec3::cross(getEdgeDirection(i), box.getEdgeDirection(j), &axis);
|
2014-08-08 00:00:58 +08:00
|
|
|
getInterval(*this, axis, min1, max1);
|
|
|
|
getInterval(box, axis, min2, max2);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (max1 < min2 || max2 < min1)
|
|
|
|
return false;
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-08 00:00:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBB::transform(const Mat4& mat)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec4 newcenter = mat * Vec4(_center.x, _center.y, _center.z, 1.0f); // center;
|
|
|
|
_center.x = newcenter.x;
|
|
|
|
_center.y = newcenter.y;
|
|
|
|
_center.z = newcenter.z;
|
2014-08-08 00:00:58 +08:00
|
|
|
|
|
|
|
_xAxis = mat * _xAxis;
|
|
|
|
_yAxis = mat * _yAxis;
|
|
|
|
_zAxis = mat * _zAxis;
|
|
|
|
|
|
|
|
_xAxis.normalize();
|
|
|
|
_yAxis.normalize();
|
|
|
|
_zAxis.normalize();
|
|
|
|
|
|
|
|
Vec3 scale, trans;
|
|
|
|
Quaternion quat;
|
|
|
|
mat.decompose(&scale, &quat, &trans);
|
|
|
|
|
|
|
|
_extents.x *= scale.x;
|
|
|
|
_extents.y *= scale.y;
|
|
|
|
_extents.z *= scale.z;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-23 13:53:23 +08:00
|
|
|
computeExtAxis();
|
2014-08-08 00:00:58 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|