Skip to content
Snippets Groups Projects
Commit 613402e1 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

REFAC: Add parameterized tests for pointSet to sphere and capsule

parent 4ac63786
No related branches found
No related tags found
No related merge requests found
......@@ -24,16 +24,77 @@
#include "imstkPointSet.h"
#include "imstkPointSetToCapsuleCD.h"
#include "imstkCapsule.h"
#include <iostream>
using namespace imstk;
TEST(imstkPointSetToCapsuleCDTest, IntersectionTestAB)
struct pointSetCapsuleCDTestData
{
auto capsule = std::make_shared<Capsule>();
// Capsule parameters
Vec3d capsulePos;
double capsuleRadius;
double capsuleLength;
Vec3d point;// point position to be tested against capsule
};
std::ostream&
operator<<(std::ostream& stream, pointSetCapsuleCDTestData const& d)
{
return stream << "[pointSetCapsuleCDTestData: Capsule (pos=" << d.capsulePos.transpose()
<< " rad=" << d.capsuleRadius << " len=" << d.capsuleLength << "), Point (pos=" << d.capsulePos.transpose() << "]";
}
struct pointSetCapsuleCDValidationData
{
// size of collision data
size_t sizeA;
size_t sizeB;
// collision element type on both sides
CollisionElementType elementTypeA;
CollisionElementType elementTypeB;
// contact directions
Vec3d dirA;
Vec3d dirB;
// contact depths
double depthA;
double depthB;
size_t contactIndexA; // index of the contact point side A
Vec3d contactPointB; // position of the contact point side B
};
std::ostream&
operator<<(std::ostream& stream, pointSetCapsuleCDValidationData const& v)
{
return stream << "[pointSetCapsuleCDValidationData: " << v.sizeA << ", "
<< v.sizeB << ", "
<< (int)v.elementTypeA << ", "
<< (int)v.elementTypeB << ", ("
<< v.dirA.transpose() << "), ("
<< v.dirB.transpose() << "), "
<< v.depthA << ", "
<< v.depthB << ", "
<< v.contactIndexA << ", ("
<< v.contactPointB.transpose() << ") ]";
}
class imstkPointSetToCapsuleCDIntersectionTests :
public ::testing::TestWithParam<std::tuple<pointSetCapsuleCDTestData, pointSetCapsuleCDValidationData>> {};
TEST_P(imstkPointSetToCapsuleCDIntersectionTests, IntersectionTestAB)
{
const pointSetCapsuleCDTestData& testData = std::get<0>(GetParam());
const pointSetCapsuleCDValidationData& valData = std::get<1>(GetParam());
auto capsule = std::make_shared<Capsule>(testData.capsulePos, testData.capsuleRadius, testData.capsuleLength);
auto pointSet = std::make_shared<PointSet>();
auto verticesPtr = std::make_shared<VecDataArray<double, 3>>(1);
(*verticesPtr)[0] = Vec3d(0.25, 0.0, 0.0);
(*verticesPtr)[0] = testData.point;
pointSet->initialize(verticesPtr);
PointSetToCapsuleCD m_pointSetToCapsuleCD;
......@@ -45,33 +106,46 @@ TEST(imstkPointSetToCapsuleCDTest, IntersectionTestAB)
std::shared_ptr<CollisionData> colData = m_pointSetToCapsuleCD.getCollisionData();
// Should be one element on side A, 0 on side B (default CD data is not generated for the sphere)
EXPECT_EQ(1, colData->elementsA.getSize());
EXPECT_EQ(1, colData->elementsB.getSize());
EXPECT_EQ(valData.sizeA, colData->elementsA.getSize());
EXPECT_EQ(valData.sizeB, colData->elementsB.getSize());
// That element should be a point directional element
EXPECT_EQ(CollisionElementType::PointIndexDirection, colData->elementsA[0].m_type);
EXPECT_EQ(CollisionElementType::PointDirection, colData->elementsB[0].m_type);
EXPECT_EQ(valData.elementTypeA, colData->elementsA[0].m_type);
EXPECT_EQ(valData.elementTypeB, colData->elementsB[0].m_type);
EXPECT_EQ(Vec3d(1.0, 0.0, 0.0), colData->elementsA[0].m_element.m_PointIndexDirectionElement.dir);
EXPECT_EQ(Vec3d(-1.0, 0.0, 0.0), colData->elementsB[0].m_element.m_PointDirectionElement.dir);
EXPECT_EQ(valData.dirA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.dir);
EXPECT_EQ(valData.dirB, colData->elementsB[0].m_element.m_PointDirectionElement.dir);
// Should have depth
EXPECT_NEAR(0.25, colData->elementsA[0].m_element.m_PointIndexDirectionElement.penetrationDepth, 1.0e-4);
EXPECT_NEAR(0.25, colData->elementsB[0].m_element.m_PointDirectionElement.penetrationDepth, 1.0e-4);
EXPECT_NEAR(valData.depthA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.penetrationDepth, 1.0e-4);
EXPECT_NEAR(valData.depthB, colData->elementsB[0].m_element.m_PointDirectionElement.penetrationDepth, 1.0e-4);
// The contact point on A should be the point
EXPECT_EQ(0, colData->elementsA[0].m_element.m_PointIndexDirectionElement.ptIndex);
EXPECT_EQ(valData.contactIndexA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.ptIndex);
// The contact point on B should be the nearest point on the surface of the sphere
EXPECT_NEAR(0.5, colData->elementsB[0].m_element.m_PointDirectionElement.pt[0], 1.0e-4);
EXPECT_LE((valData.contactPointB - colData->elementsB[0].m_element.m_PointDirectionElement.pt).norm(), 1.0e-12);
}
TEST(imstkPointSetToCapsuleCDTest, NonIntersectionTestAB)
INSTANTIATE_TEST_SUITE_P(PointSetToCapsuleCDTests, imstkPointSetToCapsuleCDIntersectionTests,
::testing::Values(std::make_tuple(pointSetCapsuleCDTestData{ Vec3d(0.0, 0.0, 0.0), 0.5, 1.0, Vec3d(0.25, 0.0, 0.0) },
pointSetCapsuleCDValidationData{ 1, 1, CollisionElementType::PointIndexDirection,
CollisionElementType::PointDirection,
Vec3d(1.0, 0.0, 0.0), Vec3d(-1.0, 0.0, 0.0),
0.25, 0.25,
0, Vec3d(0.5, 0.0, 0.0) })
));
class imstkPointSetToCapsuleCDNonIntersectionTests : public ::testing::TestWithParam<pointSetCapsuleCDTestData> {};
TEST_P(imstkPointSetToCapsuleCDNonIntersectionTests, NonIntersectionTestAB)
{
auto capsule = std::make_shared<Capsule>();
const pointSetCapsuleCDTestData testData = GetParam();
auto capsule = std::make_shared<Capsule>(testData.capsulePos, testData.capsuleRadius, testData.capsuleLength);
auto pointSet = std::make_shared<PointSet>();
auto verticesPtr = std::make_shared<VecDataArray<double, 3>>(1);
(*verticesPtr)[0] = Vec3d(5.0, 5.0, 5.0);
(*verticesPtr)[0] = testData.point;
pointSet->initialize(verticesPtr);
PointSetToCapsuleCD m_pointSetToCapsuleCD;
......@@ -82,17 +156,10 @@ TEST(imstkPointSetToCapsuleCDTest, NonIntersectionTestAB)
std::shared_ptr<CollisionData> colData = m_pointSetToCapsuleCD.getCollisionData();
// Should be no elements
// check if there are no contacts
EXPECT_EQ(0, colData->elementsA.getSize());
EXPECT_EQ(0, colData->elementsB.getSize());
}
int
imstkPointSetToCapsuleCDTest(int argc, char* argv[])
{
// Init Google Test & Mock
::testing::InitGoogleTest(&argc, argv);
// Run tests with gtest
return RUN_ALL_TESTS();
}
INSTANTIATE_TEST_SUITE_P(PointSetToCapsuleCDTests, imstkPointSetToCapsuleCDNonIntersectionTests,
::testing::Values(pointSetCapsuleCDTestData{ Vec3d(0.0, 0.0, 0.0), 0.5, 1.0, Vec3d(5.0, 5.0, 5.0) }));
\ No newline at end of file
......@@ -19,6 +19,8 @@
=========================================================================*/
#include <tuple>
#include "gtest/gtest.h"
#include "imstkPointSet.h"
......@@ -27,12 +29,70 @@
using namespace imstk;
TEST(imstkPointSetToSphereCDTest, IntersectionTestAB)
struct pointSetSphereCDTestData
{
Vec3d spherePos; // sphere position
double sphereRadius; // sphere position
Vec3d point; // point position to be tested against sphere
};
std::ostream&
operator<<(std::ostream& stream, pointSetSphereCDTestData const& d)
{
return stream << "[pointSetSphereCDTestData: Sphere (pos=" << d.spherePos.transpose()
<< " rad=" << d.sphereRadius << "), Point (pos=" << d.point.transpose() << "]";
}
struct pointSetSphereCDValidationData
{
// size of collision data
size_t sizeA;
size_t sizeB;
// collision element type on both sides
CollisionElementType elementTypeA;
CollisionElementType elementTypeB;
// contact directions
Vec3d dirA;
Vec3d dirB;
// contact depths
double depthA;
double depthB;
size_t contactIndexA; // index of the contact point side A
Vec3d contactPointB; // position of the contact point side B
};
std::ostream&
operator<<(std::ostream& stream, pointSetSphereCDValidationData const& v)
{
auto sphere = std::make_shared<Sphere>(Vec3d(0.0, -2.0, 0.0), 2.1);
return stream << "[pointSetCapsuleCDValidationData: " << v.sizeA << ", "
<< v.sizeB << ", "
<< static_cast<int>(v.elementTypeA) << ", "
<< static_cast<int>(v.elementTypeB) << ", ("
<< v.dirA.transpose() << "), ("
<< v.dirB.transpose() << "), "
<< v.depthA << ", "
<< v.depthB << ", "
<< v.contactIndexA << ", ("
<< v.contactPointB.transpose() << ") ]";
}
class imstkPointSetToSphereCDIntersectionTests :
public ::testing::TestWithParam<std::tuple<pointSetSphereCDTestData, pointSetSphereCDValidationData>> {};
TEST_P(imstkPointSetToSphereCDIntersectionTests, IntersectionTestAB)
{
const pointSetSphereCDTestData& testData = std::get<0>(GetParam());
const pointSetSphereCDValidationData& valData = std::get<1>(GetParam());
auto sphere = std::make_shared<Sphere>(testData.spherePos, testData.sphereRadius);
auto pointSet = std::make_shared<PointSet>();
auto verticesPtr = std::make_shared<VecDataArray<double, 3>>(1);
(*verticesPtr)[0] = Vec3d(0.0, 0.0, 0.0);
(*verticesPtr)[0] = testData.point;
pointSet->initialize(verticesPtr);
PointSetToSphereCD m_pointSetToSphereCD;
......@@ -44,33 +104,46 @@ TEST(imstkPointSetToSphereCDTest, IntersectionTestAB)
std::shared_ptr<CollisionData> colData = m_pointSetToSphereCD.getCollisionData();
// Should be one element on side A, 0 on side B (default CD data is not generated for the sphere)
EXPECT_EQ(1, colData->elementsA.getSize());
EXPECT_EQ(1, colData->elementsB.getSize());
EXPECT_EQ(valData.sizeA, colData->elementsA.getSize());
EXPECT_EQ(valData.sizeB, colData->elementsB.getSize());
// That element should be a point directional element
EXPECT_EQ(CollisionElementType::PointIndexDirection, colData->elementsA[0].m_type);
EXPECT_EQ(CollisionElementType::PointDirection, colData->elementsB[0].m_type);
EXPECT_EQ(valData.elementTypeA, colData->elementsA[0].m_type);
EXPECT_EQ(valData.elementTypeB, colData->elementsB[0].m_type);
// That element should have 0.5 depth
EXPECT_EQ(Vec3d(0.0, 1.0, 0.0), colData->elementsA[0].m_element.m_PointIndexDirectionElement.dir);
EXPECT_EQ(Vec3d(0.0, -1.0, 0.0), colData->elementsB[0].m_element.m_PointDirectionElement.dir);
EXPECT_EQ(valData.dirA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.dir);
EXPECT_EQ(valData.dirB, colData->elementsB[0].m_element.m_PointDirectionElement.dir);
// Should have depths of 0.5
EXPECT_NEAR(0.1, colData->elementsA[0].m_element.m_PointIndexDirectionElement.penetrationDepth, 0.00001);
EXPECT_NEAR(0.1, colData->elementsB[0].m_element.m_PointDirectionElement.penetrationDepth, 0.00001);
EXPECT_NEAR(valData.depthA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.penetrationDepth, 0.00001);
EXPECT_NEAR(valData.depthB, colData->elementsB[0].m_element.m_PointDirectionElement.penetrationDepth, 0.00001);
// The contact point on A should be the point
EXPECT_EQ(0, colData->elementsA[0].m_element.m_PointIndexDirectionElement.ptIndex);
EXPECT_EQ(valData.contactIndexA, colData->elementsA[0].m_element.m_PointIndexDirectionElement.ptIndex);
// The contact point on B should be the nearest point on the surface of the sphere
EXPECT_NEAR(0.1, colData->elementsB[0].m_element.m_PointDirectionElement.pt[1], 0.00001);
EXPECT_LE((valData.contactPointB - colData->elementsB[0].m_element.m_PointDirectionElement.pt).norm(), 1.0e-12);
}
TEST(imstkPointSetToSphereCDTest, NonIntersectionTestAB)
INSTANTIATE_TEST_SUITE_P(PointSetToSphereCDTests, imstkPointSetToSphereCDIntersectionTests,
::testing::Values(std::make_tuple(pointSetSphereCDTestData{ Vec3d(0.0, -2.0, 0.0), 2.1, Vec3d(0.0, 0.0, 0.0) },
pointSetSphereCDValidationData{ 1, 1, CollisionElementType::PointIndexDirection,
CollisionElementType::PointDirection,
Vec3d(0.0, 1.0, 0.0), Vec3d(0.0, -1.0, 0.0),
0.1, 0.1,
0, Vec3d(0.0, 0.1, 0.0) })));
class imstkPointSetToSphereCDNonIntersectionTests : public ::testing::TestWithParam<pointSetSphereCDTestData> {};
TEST_P(imstkPointSetToSphereCDNonIntersectionTests, NonIntersectionTestAB)
{
auto sphere = std::make_shared<Sphere>(Vec3d(0.0, -8.0, 0.0), 2.1);
const pointSetSphereCDTestData testData = GetParam();
auto sphere = std::make_shared<Sphere>(testData.spherePos, testData.sphereRadius);
auto pointSet = std::make_shared<PointSet>();
auto verticesPtr = std::make_shared<VecDataArray<double, 3>>(1);
(*verticesPtr)[0] = Vec3d(0.0, 0.0, 0.0);
(*verticesPtr)[0] = testData.point;
pointSet->initialize(verticesPtr);
PointSetToSphereCD m_pointSetToSphereCD;
......@@ -86,12 +159,8 @@ TEST(imstkPointSetToSphereCDTest, NonIntersectionTestAB)
EXPECT_EQ(0, colData->elementsB.getSize());
}
int
imstkPointSetToSphereCDTest(int argc, char* argv[])
{
// Init Google Test & Mock
::testing::InitGoogleTest(&argc, argv);
// Run tests with gtest
return RUN_ALL_TESTS();
}
INSTANTIATE_TEST_SUITE_P(PointSetToSphereCDTests, imstkPointSetToSphereCDNonIntersectionTests,
::testing::Values(pointSetSphereCDTestData{ Vec3d(0.0, -8.0, 0.0), 2.1, Vec3d(0.0, 0.0, 0.0) },
pointSetSphereCDTestData{ Vec3d(0.0, 0.0, 0.0), 1.0, Vec3d(0.0, 5.0, 0.0) },
pointSetSphereCDTestData{ Vec3d(0.0, 0.0, 0.0), 0.1, Vec3d(2.0, 0.0, 0.0) },
pointSetSphereCDTestData{ Vec3d(0.0, 0.0, 7.4), 5.0, Vec3d(0.0, 0.0, -10.0) }));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment