Updates to kd-tree ray intersection - ATOM-15673 (#1026)
* updates to kd-tree ray intersection * update tests for kd-tree * add one more test for kd-tree intersection * updates to ModelKdTree following review feedback * improve api doc comment for RayIntersection in ModelKdTree * updates following review feedback * update .clang-format to stack parameters if they do not all fit on one line
This commit is contained in:
committed by
GitHub
parent
0f699cfd47
commit
4a5b7edbfe
@@ -13,6 +13,7 @@ AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortLambdasOnASingleLine: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
BinPackParameters: false
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterClass: true
|
||||
|
||||
@@ -35,7 +35,15 @@ namespace AZ
|
||||
ModelKdTree() = default;
|
||||
|
||||
bool Build(const ModelAsset* model);
|
||||
bool RayIntersection(const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distance, AZ::Vector3& normal) const;
|
||||
//! Return if a ray intersected the model.
|
||||
//! @param raySrc The starting point of the ray.
|
||||
//! @param rayDir The direction and length of the ray (magnitude is encoded in the direction).
|
||||
//! @param[out] The normalized distance of the intersection (in the range 0.0-1.0) - to calculate the actual
|
||||
//! distance, multiply distanceNormalized by the magnitude of rayDir.
|
||||
//! @param[out] The surface normal of the intersection with the model.
|
||||
//! @return Return true if there was an intersection with the model, false otherwise.
|
||||
bool RayIntersection(
|
||||
const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distanceNormalized, AZ::Vector3& normal) const;
|
||||
void GetPenetratedBoxes(const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, AZStd::vector<AZ::Aabb>& outBoxes);
|
||||
|
||||
enum ESplitAxis
|
||||
@@ -53,8 +61,14 @@ namespace AZ
|
||||
private:
|
||||
|
||||
void BuildRecursively(ModelKdTreeNode* pNode, const AZ::Aabb& boundbox, AZStd::vector<ObjectIdTriangleIndices>& indices);
|
||||
bool RayIntersectionRecursively(ModelKdTreeNode* pNode, const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distance, AZ::Vector3& normal) const;
|
||||
void GetPenetratedBoxesRecursively(ModelKdTreeNode* pNode, const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, AZStd::vector<AZ::Aabb>& outBoxes);
|
||||
bool RayIntersectionRecursively(
|
||||
ModelKdTreeNode* pNode,
|
||||
const AZ::Vector3& raySrc,
|
||||
const AZ::Vector3& rayDir,
|
||||
float& distanceNormalized,
|
||||
AZ::Vector3& normal) const;
|
||||
void GetPenetratedBoxesRecursively(
|
||||
ModelKdTreeNode* pNode, const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, AZStd::vector<AZ::Aabb>& outBoxes);
|
||||
void ConstructMeshList(const ModelAsset* model, const AZ::Transform& matParent);
|
||||
|
||||
static const int s_MinimumVertexSizeInLeafNode = 3 * 10;
|
||||
|
||||
@@ -140,8 +140,9 @@ namespace AZ
|
||||
bool Model::LocalRayIntersection(const AZ::Vector3& rayStart, const AZ::Vector3& dir, float& distance, AZ::Vector3& normal) const
|
||||
{
|
||||
AZ_PROFILE_FUNCTION(Debug::ProfileCategory::AzRender);
|
||||
float firstHit;
|
||||
const int result = Intersect::IntersectRayAABB2(rayStart, dir.GetReciprocal(), m_aabb, firstHit, distance);
|
||||
float start;
|
||||
float end;
|
||||
const int result = Intersect::IntersectRayAABB2(rayStart, dir.GetReciprocal(), m_aabb, start, end);
|
||||
if (Intersect::ISECT_RAY_AABB_NONE != result)
|
||||
{
|
||||
if (ModelAsset* modelAssetPtr = m_modelAsset.Get())
|
||||
@@ -164,7 +165,9 @@ namespace AZ
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Model::RayIntersection(const AZ::Transform& modelTransform, const AZ::Vector3& nonUniformScale, const AZ::Vector3& rayStart, const AZ::Vector3& dir, float& distanceFactor, AZ::Vector3& normal) const
|
||||
bool Model::RayIntersection(
|
||||
const AZ::Transform& modelTransform, const AZ::Vector3& nonUniformScale, const AZ::Vector3& rayStart, const AZ::Vector3& dir,
|
||||
float& distanceFactor, AZ::Vector3& normal) const
|
||||
{
|
||||
AZ_PROFILE_FUNCTION(Debug::ProfileCategory::AzRender);
|
||||
const AZ::Vector3 clampedScale = nonUniformScale.GetMax(AZ::Vector3(AZ::MinTransformScale));
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
*/
|
||||
|
||||
#include <AzCore/std/numeric.h>
|
||||
#include <AzCore/std/limits.h>
|
||||
#include <Atom/RPI.Reflect/Model/ModelKdTree.h>
|
||||
#include <AzCore/Math/IntersectSegment.h>
|
||||
|
||||
@@ -191,10 +192,10 @@ namespace AZ
|
||||
|
||||
if (ModelLodAsset* lodAssetPtr = model->GetLodAssets()[0].Get())
|
||||
{
|
||||
AZ_Warning("ModelKdTree", lodAssetPtr->GetMeshes().size() <= std::numeric_limits<AZ::u8>::max() + 1,
|
||||
AZ_Warning("ModelKdTree", lodAssetPtr->GetMeshes().size() <= AZStd::numeric_limits<AZ::u8>::max() + 1,
|
||||
"KdTree generation doesn't support models with greater than 256 meshes. RayIntersection results will be incorrect "
|
||||
"unless the meshes are merged or broken up into multiple models");
|
||||
const size_t size = AZStd::min<size_t>(lodAssetPtr->GetMeshes().size(), std::numeric_limits<AZ::u8>::max() + 1);
|
||||
const size_t size = AZStd::min<size_t>(lodAssetPtr->GetMeshes().size(), AZStd::numeric_limits<AZ::u8>::max() + 1);
|
||||
m_meshes.reserve(size);
|
||||
AZStd::transform(
|
||||
lodAssetPtr->GetMeshes().begin(), AZStd::next(lodAssetPtr->GetMeshes().begin(), size),
|
||||
@@ -204,20 +205,42 @@ namespace AZ
|
||||
}
|
||||
}
|
||||
|
||||
bool ModelKdTree::RayIntersection(const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distance, AZ::Vector3& normal) const
|
||||
bool ModelKdTree::RayIntersection(
|
||||
const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distanceNormalized, AZ::Vector3& normal) const
|
||||
{
|
||||
return RayIntersectionRecursively(m_pRootNode.get(), raySrc, rayDir, distance, normal);
|
||||
float closestDistanceNormalized = AZStd::numeric_limits<float>::max();
|
||||
if (RayIntersectionRecursively(m_pRootNode.get(), raySrc, rayDir, closestDistanceNormalized, normal))
|
||||
{
|
||||
distanceNormalized = closestDistanceNormalized;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ModelKdTree::RayIntersectionRecursively(ModelKdTreeNode* pNode, const AZ::Vector3& raySrc, const AZ::Vector3& rayDir, float& distance, AZ::Vector3& normal) const
|
||||
bool ModelKdTree::RayIntersectionRecursively(
|
||||
ModelKdTreeNode* pNode,
|
||||
const AZ::Vector3& raySrc,
|
||||
const AZ::Vector3& rayDir,
|
||||
float& distanceNormalized,
|
||||
AZ::Vector3& normal) const
|
||||
{
|
||||
using Intersect::IntersectRayAABB2;
|
||||
using Intersect::IntersectSegmentTriangleCCW;
|
||||
using Intersect::ISECT_RAY_AABB_NONE;
|
||||
|
||||
if (!pNode)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
float start, end;
|
||||
if (AZ::Intersect::IntersectRayAABB2(raySrc, rayDir.GetReciprocal(), pNode->GetBoundBox(), start, end) == Intersect::ISECT_RAY_AABB_NONE)
|
||||
if (IntersectRayAABB2(raySrc, rayDir.GetReciprocal(), pNode->GetBoundBox(), start, end) == ISECT_RAY_AABB_NONE)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (start > distanceNormalized)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -235,17 +258,13 @@ namespace AZ
|
||||
return false;
|
||||
}
|
||||
|
||||
AZ::Vector3 intersectionNormal;
|
||||
float hitDistanceNormalized;
|
||||
const float maxDist(FLT_MAX);
|
||||
float nearestDist = maxDist;
|
||||
|
||||
float nearestDistanceNormalized = distanceNormalized;
|
||||
for (AZ::u32 i = 0; i < nVBuffSize; ++i)
|
||||
{
|
||||
const auto& [first, second, third] = pNode->GetVertexIndex(i);
|
||||
const AZ::u32 nObjIndex = pNode->GetObjIndex(i);
|
||||
|
||||
AZStd::array_view<float> positionBuffer = m_meshes[nObjIndex].m_vertexData;
|
||||
const AZStd::array_view<float> positionBuffer = m_meshes[nObjIndex].m_vertexData;
|
||||
|
||||
if (positionBuffer.empty())
|
||||
{
|
||||
@@ -258,25 +277,23 @@ namespace AZ
|
||||
AZ::Vector3{positionBuffer[third * 3 + 0], positionBuffer[third * 3 + 1], positionBuffer[third * 3 + 2]},
|
||||
};
|
||||
|
||||
const AZ::Vector3 rayEnd = raySrc + rayDir * distance;
|
||||
|
||||
if (AZ::Intersect::IntersectSegmentTriangleCCW(raySrc, rayEnd, trianglePoints[0], trianglePoints[1], trianglePoints[2],
|
||||
intersectionNormal, hitDistanceNormalized) != Intersect::ISECT_RAY_AABB_NONE)
|
||||
float hitDistanceNormalized;
|
||||
AZ::Vector3 intersectionNormal;
|
||||
const AZ::Vector3 rayEnd = raySrc + rayDir;
|
||||
if (IntersectSegmentTriangleCCW(raySrc, rayEnd, trianglePoints[0], trianglePoints[1], trianglePoints[2],
|
||||
intersectionNormal, hitDistanceNormalized) != ISECT_RAY_AABB_NONE)
|
||||
{
|
||||
float hitDistance = hitDistanceNormalized * distance;
|
||||
|
||||
if (nearestDist > hitDistance)
|
||||
if (nearestDistanceNormalized > hitDistanceNormalized)
|
||||
{
|
||||
normal = intersectionNormal;
|
||||
nearestDistanceNormalized = hitDistanceNormalized;
|
||||
}
|
||||
|
||||
nearestDist = AZStd::GetMin(nearestDist, hitDistance);
|
||||
}
|
||||
}
|
||||
|
||||
if (nearestDist < maxDist)
|
||||
if (nearestDistanceNormalized < distanceNormalized)
|
||||
{
|
||||
distance = AZStd::GetMin(distance, nearestDist);
|
||||
distanceNormalized = nearestDistanceNormalized;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -284,8 +301,8 @@ namespace AZ
|
||||
}
|
||||
|
||||
// running both sides to find the closest intersection
|
||||
const bool bFoundChild0 = RayIntersectionRecursively(pNode->GetChild(0), raySrc, rayDir, distance, normal);
|
||||
const bool bFoundChild1 = RayIntersectionRecursively(pNode->GetChild(1), raySrc, rayDir, distance, normal);
|
||||
const bool bFoundChild0 = RayIntersectionRecursively(pNode->GetChild(0), raySrc, rayDir, distanceNormalized, normal);
|
||||
const bool bFoundChild1 = RayIntersectionRecursively(pNode->GetChild(1), raySrc, rayDir, distanceNormalized, normal);
|
||||
|
||||
return bFoundChild0 || bFoundChild1;
|
||||
}
|
||||
@@ -311,5 +328,5 @@ namespace AZ
|
||||
GetPenetratedBoxesRecursively(pNode->GetChild(0), raySrc, rayDir, outBoxes);
|
||||
GetPenetratedBoxesRecursively(pNode->GetChild(1), raySrc, rayDir, outBoxes);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace RPI
|
||||
} // namespace AZ
|
||||
|
||||
@@ -1074,13 +1074,13 @@ namespace UnitTest
|
||||
}
|
||||
};
|
||||
|
||||
class KdTreeIntersectsFixture
|
||||
class KdTreeIntersectsParameterizedFixture
|
||||
: public ModelTests
|
||||
, public ::testing::WithParamInterface<KdTreeIntersectParams>
|
||||
{
|
||||
};
|
||||
|
||||
TEST_P(KdTreeIntersectsFixture, KdTreeIntersects)
|
||||
TEST_P(KdTreeIntersectsParameterizedFixture, KdTreeIntersects)
|
||||
{
|
||||
TwoSeparatedPlanesMesh mesh;
|
||||
|
||||
@@ -1090,7 +1090,10 @@ namespace UnitTest
|
||||
float distance = AZStd::numeric_limits<float>::max();
|
||||
AZ::Vector3 normal;
|
||||
|
||||
EXPECT_THAT(kdTree.RayIntersection(AZ::Vector3(GetParam().xpos, GetParam().ypos, GetParam().zpos), AZ::Vector3::CreateAxisZ(-1.0f), distance, normal), testing::Eq(GetParam().expectedShouldIntersect));
|
||||
EXPECT_THAT(
|
||||
kdTree.RayIntersection(
|
||||
AZ::Vector3(GetParam().xpos, GetParam().ypos, GetParam().zpos), AZ::Vector3::CreateAxisZ(-1.0f), distance, normal),
|
||||
testing::Eq(GetParam().expectedShouldIntersect));
|
||||
EXPECT_THAT(distance, testing::FloatEq(GetParam().expectedDistance));
|
||||
}
|
||||
|
||||
@@ -1119,5 +1122,63 @@ namespace UnitTest
|
||||
KdTreeIntersectParams{0.778f, 0.111f, 1.0f, 0.5f, true},
|
||||
KdTreeIntersectParams{0.778f, 0.778f, 1.0f, 0.5f, true},
|
||||
};
|
||||
INSTANTIATE_TEST_CASE_P(KdTreeIntersectsPlane, KdTreeIntersectsFixture, ::testing::ValuesIn(intersectTestData));
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(KdTreeIntersectsPlane, KdTreeIntersectsParameterizedFixture, ::testing::ValuesIn(intersectTestData));
|
||||
|
||||
class KdTreeIntersectsFixture
|
||||
: public ModelTests
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
ModelTests::SetUp();
|
||||
|
||||
m_mesh = AZStd::make_unique<TwoSeparatedPlanesMesh>();
|
||||
m_kdTree = AZStd::make_unique<AZ::RPI::ModelKdTree>();
|
||||
ASSERT_TRUE(m_kdTree->Build(m_mesh->GetModel().Get()));
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
m_kdTree.reset();
|
||||
m_mesh.reset();
|
||||
|
||||
ModelTests::TearDown();
|
||||
}
|
||||
|
||||
AZStd::unique_ptr<TwoSeparatedPlanesMesh> m_mesh;
|
||||
AZStd::unique_ptr<AZ::RPI::ModelKdTree> m_kdTree;
|
||||
};
|
||||
|
||||
TEST_F(KdTreeIntersectsFixture, KdTreeIntersectionReturnsNormalizedDistance)
|
||||
{
|
||||
float t = AZStd::numeric_limits<float>::max();
|
||||
AZ::Vector3 normal;
|
||||
|
||||
constexpr float rayLength = 100.0f;
|
||||
EXPECT_THAT(
|
||||
m_kdTree->RayIntersection(
|
||||
AZ::Vector3::CreateZero(), AZ::Vector3::CreateAxisZ(-rayLength), t, normal), testing::Eq(true));
|
||||
EXPECT_THAT(t, testing::FloatEq(0.005f));
|
||||
}
|
||||
|
||||
TEST_F(KdTreeIntersectsFixture, KdTreeIntersectionHandlesInvalidStartingNormalizedDistance)
|
||||
{
|
||||
float t = -0.5f; // invalid starting distance
|
||||
AZ::Vector3 normal;
|
||||
|
||||
constexpr float rayLength = 10.0f;
|
||||
EXPECT_THAT(
|
||||
m_kdTree->RayIntersection(AZ::Vector3::CreateAxisZ(0.75f), AZ::Vector3::CreateAxisZ(-rayLength), t, normal), testing::Eq(true));
|
||||
EXPECT_THAT(t, testing::FloatEq(0.025f));
|
||||
}
|
||||
|
||||
TEST_F(KdTreeIntersectsFixture, KdTreeIntersectionDoesNotScaleRayByStartingDistance)
|
||||
{
|
||||
float t = 10.0f; // starting distance (used to check it is not read from initially by RayIntersection)
|
||||
AZ::Vector3 normal;
|
||||
|
||||
EXPECT_THAT(
|
||||
m_kdTree->RayIntersection(AZ::Vector3::CreateAxisZ(5.0f), -AZ::Vector3::CreateAxisZ(), t, normal), testing::Eq(false));
|
||||
}
|
||||
} // namespace UnitTest
|
||||
|
||||
@@ -140,9 +140,16 @@ namespace AZ
|
||||
AZ::Vector3 nonUniformScale = AZ::Vector3::CreateOne();
|
||||
AZ::NonUniformScaleRequestBus::EventResult(nonUniformScale, GetEntityId(), &AZ::NonUniformScaleRequests::GetScale);
|
||||
|
||||
float t;
|
||||
AZ::Vector3 ignoreNormal;
|
||||
constexpr float rayLength = 1000.0f;
|
||||
if (m_controller.GetModel()->RayIntersection(transform, nonUniformScale, src, dir * rayLength, t, ignoreNormal))
|
||||
{
|
||||
distance = rayLength * t;
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_controller.GetModel()->RayIntersection(transform, nonUniformScale, src, dir, distance, ignoreNormal);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EditorMeshComponent::SupportsEditorRayIntersect()
|
||||
|
||||
@@ -485,16 +485,12 @@ namespace AZ
|
||||
m_transformInterface->GetWorldTM(), m_cachedNonUniformScale, ray.m_startWorldPosition,
|
||||
ray.m_endWorldPosition - ray.m_startWorldPosition, t, normal))
|
||||
{
|
||||
// note: this is a temporary workaround to handle cases where model->RayIntersection
|
||||
// returns negative distances, follow-up ATOM-15673
|
||||
const auto absT = AZStd::abs(t);
|
||||
|
||||
// fill in ray result structure after successful intersection
|
||||
const auto intersectionLine = (ray.m_endWorldPosition - ray.m_startWorldPosition);
|
||||
result.m_uv = AZ::Vector2::CreateZero();
|
||||
result.m_worldPosition = ray.m_startWorldPosition + intersectionLine * absT;
|
||||
result.m_worldPosition = ray.m_startWorldPosition + intersectionLine * t;
|
||||
result.m_worldNormal = normal;
|
||||
result.m_distance = intersectionLine.GetLength() * absT;
|
||||
result.m_distance = intersectionLine.GetLength() * t;
|
||||
result.m_entityAndComponent = m_entityComponentIdPair;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user