More overlap tests.

This commit is contained in:
Бранимир Караџић 2019-02-03 22:07:51 -08:00
parent 55ab61fe7d
commit 70d3888b97
3 changed files with 179 additions and 30 deletions

View File

@ -932,6 +932,33 @@ public:
dde.draw(triangleB);
}
{
Sphere sphereA = { { px+kStepX*4.0f, py, pz+kStepZ*0.0f }, 0.5f };
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*4.0f, 0.0f, kStepZ*0.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
const Disk diskB = { getCenter(triangleB), planeB.normal, 0.5f };
olp = overlap(sphereA, diskB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(sphereA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(diskB);
}
// AABB ---
{
Aabb aabbA, aabbB;
@ -997,6 +1024,34 @@ public:
dde.draw(triangleB);
}
{
Aabb aabbA;
toAabb(aabbA, { px+kStepX*4.0f, py, pz+kStepZ*1.0f }, { 0.5f, 0.5f, 0.5f });
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*4.0f, 0.0f, kStepZ*1.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
const Disk diskB = { getCenter(triangleB), planeB.normal, 0.5f };
olp = overlap(aabbA, diskB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(aabbA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(diskB);
}
// Triangle ---
{
Triangle triangleA =
@ -1060,6 +1115,41 @@ public:
dde.setWireframe(true);
dde.draw(triangleB);
}
{
Triangle triangleA =
{
{ px-0.4f, py+0.0f, pz-0.4f },
{ px+0.0f, py-0.3f, pz-0.5f },
{ px+0.0f, py+0.5f, pz+0.3f },
};
translate(triangleA, {kStepX*4.0f, 0.0f, kStepZ*2.0f});
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*4.0f, 0.0f, kStepZ*2.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
const Disk diskB = { getCenter(triangleB), planeB.normal, 0.5f };
olp = overlap(triangleA, diskB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(triangleA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(diskB);
}
}
dde.pop();

View File

@ -9,14 +9,19 @@
using namespace bx;
Vec3 getCenter(const Aabb& _outAabb)
Vec3 getCenter(const Aabb& _aabb)
{
return mul(add(_outAabb.min, _outAabb.max), 0.5f);
return mul(add(_aabb.min, _aabb.max), 0.5f);
}
Vec3 getExtents(const Aabb& _outAabb)
Vec3 getExtents(const Aabb& _aabb)
{
return mul(sub(_outAabb.max, _outAabb.min), 0.5f);
return mul(sub(_aabb.max, _aabb.min), 0.5f);
}
Vec3 getCenter(const Triangle& _triangle)
{
return bx::mul(bx::add(bx::add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f);
}
void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extent)
@ -826,6 +831,24 @@ bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
return true;
}
void barycentric(float& _outU, float& _outV, float& _outW, const Triangle& _triangle, const Vec3& _pos)
{
const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
const Vec3 v2 = sub(_pos, _triangle.v0);
const float dot00 = dot(v0, v0);
const float dot01 = dot(v0, v1);
const float dot02 = dot(v0, v2);
const float dot11 = dot(v1, v1);
const float dot12 = dot(v1, v2);
const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
_outU = (dot11*dot02 - dot01*dot12)*invDenom;
_outV = (dot00*dot12 - dot01*dot02)*invDenom;
_outW = 1.0f - _outU - _outV;
}
Vec3 closestPoint(const Plane& _plane, const Vec3 _pos)
{
const float dist = distance(_plane, _pos);
@ -862,24 +885,6 @@ bool overlap(const Sphere& _sphere, const Plane& _plane)
return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
}
void barycentric(float& _outU, float& _outV, float& _outW, const Triangle& _triangle, const Vec3& _pos)
{
const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
const Vec3 v2 = sub(_pos, _triangle.v0);
const float dot00 = dot(v0, v0);
const float dot01 = dot(v0, v1);
const float dot02 = dot(v0, v2);
const float dot11 = dot(v1, v1);
const float dot12 = dot(v1, v2);
const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
_outU = (dot11*dot02 - dot01*dot12)*invDenom;
_outV = (dot00*dot12 - dot01*dot02)*invDenom;
_outW = 1.0f - _outU - _outV;
}
bool overlap(const Sphere& _sphere, const Triangle& _triangle)
{
Plane plane;
@ -923,8 +928,15 @@ bool overlap(const Sphere& _sphere, const Cone& _cone)
bool overlap(const Sphere& _sphere, const Disk& _disk)
{
BX_UNUSED(_sphere, _disk);
if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
{
return false;
}
bx::Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center);
return overlap(_sphere, plane);
}
bool overlap(const Sphere& _sphere, const Obb& _obb)
@ -945,6 +957,11 @@ bool overlap(const Aabb& _aabb, const Vec3& _pos)
;
}
bool overlap(const Aabb& _aabb, const Sphere& _sphere)
{
return overlap(_sphere, _aabb);
}
uint32_t overlapTestMask(const Aabb& _aabbA, const Aabb& _aabbB)
{
/// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
@ -1031,8 +1048,15 @@ bool overlap(const Aabb& _aabb, const Cone& _cone)
bool overlap(const Aabb& _aabb, const Disk& _disk)
{
BX_UNUSED(_aabb, _disk);
if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
{
return false;
}
bx::Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center);
return overlap(_aabb, plane);
}
bool overlap(const Aabb& _aabb, const Obb& _obb)
@ -1052,6 +1076,16 @@ bool overlap(const Triangle& _triangle, const bx::Vec3& _pos)
;
}
bool overlap(const Triangle& _triangle, const Sphere& _sphere)
{
return overlap(_sphere, _triangle);
}
bool overlap(const Triangle& _triangle, const Aabb& _aabb)
{
return overlap(_aabb, _triangle);
}
bool overlap(const Triangle& _triangle, const bx::Plane& _plane)
{
const float dist0 = distance(_plane, _triangle.v0);
@ -1092,8 +1126,15 @@ bool overlap(const Triangle& _triangle, const Cone& _cone)
bool overlap(const Triangle& _triangle, const Disk& _disk)
{
BX_UNUSED(_triangle, _disk);
if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
{
return false;
}
bx::Plane plane;
bx::calcPlane(plane, _disk.normal, _disk.center);
return overlap(_triangle, plane);
}
bool overlap(const Triangle& _triangle, const Obb& _obb)

View File

@ -72,6 +72,15 @@ struct Hit
bx::Plane plane;
};
///
bx::Vec3 getCenter(const Aabb& _aabb);
///
bx::Vec3 getExtents(const Aabb& _aabb);
///
bx::Vec3 getCenter(const Triangle& _triangle);
///
void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extent);
@ -183,6 +192,9 @@ bool overlap(const Sphere& _sphere, const Obb& _obb);
///
bool overlap(const Aabb& _aabb, const bx::Vec3& _pos);
///
bool overlap(const Aabb& _aabb, const Sphere& _sphere);
///
bool overlap(const Aabb& _aabbA, const Aabb& _aabbB);
@ -210,6 +222,12 @@ bool overlap(const Aabb& _aabb, const Obb& _obb);
///
bool overlap(const Triangle& _triangle, const bx::Vec3& _pos);
///
bool overlap(const Triangle& _triangle, const Sphere& _sphere);
///
bool overlap(const Triangle& _triangle, const Aabb& _aabb);
///
bool overlap(const Triangle& _triangle, const bx::Plane& _plane);