Commit dc431620 authored by Andrew Wilson's avatar Andrew Wilson
Browse files

TEST: Bend functor tests

parent 54f42185
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "gtest/gtest.h"
#include "imstkPbdBendConstraint.h"
using namespace imstk;
///
/// \brief Test that two connecting line segments unfold
///
TEST(imstkPbdBendConstraintTest, TestConvergence1)
{
PbdBendConstraint constraint;
// Straight line upon initialization
VecDataArray<double, 3> vertices(3);
vertices[0] = Vec3d(0.0, 0.0, 0.0);
vertices[1] = Vec3d(0.5, 0.0, 0.0);
vertices[2] = Vec3d(1.0, 0.0, 0.0);
DataArray<double> invMasses(3);
invMasses[0] = 1.0;
invMasses[1] = 0.0; // Center doesn't move
invMasses[2] = 1.0;
constraint.initConstraint(vertices, 0, 1, 2, 1e20);
// Modify it so the line segments look like \/
vertices[0][1] = 0.1;
vertices[2][1] = 0.1;
for (int i = 0; i < 500; i++)
{
constraint.projectConstraint(invMasses, 0.01, PbdConstraint::SolverType::xPBD, vertices);
}
// Should resolve back to a flat line
EXPECT_NEAR(vertices[0][1], 0.0, IMSTK_DOUBLE_EPS);
EXPECT_NEAR(vertices[2][1], 0.0, IMSTK_DOUBLE_EPS);
}
\ No newline at end of file
......@@ -25,34 +25,27 @@
using namespace imstk;
///
/// \brief TODO
///
class imstkPbdPointEdgeConstraintTest : public ::testing::Test
{
protected:
PbdPointEdgeConstraint m_constraint;
};
///
/// \brief Test that a point and edge meet on touching
///
TEST_F(imstkPbdPointEdgeConstraintTest, TestConvergence1)
TEST(imstkPbdPointEdgeConstraintTest, TestConvergence1)
{
PbdPointEdgeConstraint constraint;
Vec3d a = Vec3d(-0.5, 0.0, 0.0);
Vec3d b = Vec3d(0.5, 0.0, 0.0);
Vec3d x = Vec3d(0.0, -1.0, 0.0);
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_NEAR(x[1], a[1], IMSTK_DOUBLE_EPS);
......@@ -63,22 +56,24 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestConvergence1)
///
/// \brief Test that a point and edge meet on touching
///
TEST_F(imstkPbdPointEdgeConstraintTest, TestConvergence2)
TEST(imstkPbdPointEdgeConstraintTest, TestConvergence2)
{
PbdPointEdgeConstraint constraint;
Vec3d a = Vec3d(-0.5, 0.0, 0.0);
Vec3d b = Vec3d(0.5, 0.0, 0.0);
Vec3d x = Vec3d(0.0, 1.0, 0.0);
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_NEAR(x[1], a[1], IMSTK_DOUBLE_EPS);
......@@ -89,8 +84,10 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestConvergence2)
///
/// \brief Test that a point not within bounds of edge does not move (left)
///
TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence1)
TEST(imstkPbdPointEdgeConstraintTest, TestNoConvergence1)
{
PbdPointEdgeConstraint constraint;
Vec3d a = Vec3d(-0.5, 0.0, 0.0);
const Vec3d aInit = a;
Vec3d b = Vec3d(0.5, 0.0, 0.0);
......@@ -100,14 +97,14 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence1)
const Vec3d xInit = x;
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_EQ(xInit[0], x[0]);
......@@ -126,8 +123,10 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence1)
///
/// \brief Test that a point not within bounds of edge does not move (right)
///
TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence2)
TEST(imstkPbdPointEdgeConstraintTest, TestNoConvergence2)
{
PbdPointEdgeConstraint constraint;
Vec3d a = Vec3d(-0.5, 0.0, 0.0);
const Vec3d aInit = a;
Vec3d b = Vec3d(0.5, 0.0, 0.0);
......@@ -137,14 +136,14 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence2)
const Vec3d xInit = x;
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_EQ(xInit[0], x[0]);
......@@ -158,17 +157,4 @@ TEST_F(imstkPbdPointEdgeConstraintTest, TestNoConvergence2)
EXPECT_EQ(bInit[0], b[0]);
EXPECT_EQ(bInit[1], b[1]);
EXPECT_EQ(bInit[2], b[2]);
}
///
/// \brief TODO
///
int
imstkPbdPointEdgeConstraintTest(int argc, char* argv[])
{
// Init Google Test & Mock
::testing::InitGoogleTest(&argc, argv);
// Run tests with gtest
return RUN_ALL_TESTS();
}
}
\ No newline at end of file
......@@ -25,44 +25,24 @@
using namespace imstk;
///
/// \brief TODO
///
class imstkPbdPointPointConstraintTest : public ::testing::Test
{
protected:
PbdPointPointConstraint m_constraint;
};
///
/// \brief Test that two points meet
///
TEST_F(imstkPbdPointPointConstraintTest, TestConvergence1)
TEST(imstkPbdPointPointConstraintTest, TestConvergence1)
{
PbdPointPointConstraint constraint;
Vec3d a = Vec3d(0.0, 0.0, 0.0);
Vec3d b = Vec3d(0.0, -1.0, 0.0);
m_constraint.initConstraint(
constraint.initConstraint(
{ &a, 1.0, nullptr },
{ &b, 1.0, nullptr },
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
ASSERT_EQ(a[1], b[1]);
}
///
/// \brief TODO
///
int
imstkPbdPointPointConstraintTest(int argc, char* argv[])
{
// Init Google Test & Mock
::testing::InitGoogleTest(&argc, argv);
// Run tests with gtest
return RUN_ALL_TESTS();
}
}
\ No newline at end of file
......@@ -25,20 +25,13 @@
using namespace imstk;
///
/// \brief TODO
///
class imstkPbdPointTriangleConstraintTest : public ::testing::Test
{
protected:
PbdPointTriangleConstraint m_constraint;
};
///
/// \brief Test that a point below a triangle, and the triangle, meet on y axes
///
TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence1)
TEST(imstkPbdPointTriangleConstraintTest, TestConvergence1)
{
PbdPointTriangleConstraint constraint;
Vec3d a = Vec3d(0.5, 0.0, -0.5);
Vec3d b = Vec3d(-0.5, 0.0, -0.5);
Vec3d c = Vec3d(0.0, 0.0, 0.5);
......@@ -47,7 +40,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence1)
x[1] -= 1.0;
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
......@@ -55,7 +48,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence1)
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_NEAR(x[1], a[1], 0.00000001);
......@@ -66,8 +59,10 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence1)
///
/// \brief Test that a point above a triangle, and the triangle, meet on y axes
///
TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence2)
TEST(imstkPbdPointTriangleConstraintTest, TestConvergence2)
{
PbdPointTriangleConstraint constraint;
Vec3d a = Vec3d(0.5, 0.0, -0.5);
Vec3d b = Vec3d(-0.5, 0.0, -0.5);
Vec3d c = Vec3d(0.0, 0.0, 0.5);
......@@ -76,7 +71,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence2)
x[1] += 1.0;
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &x, 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
......@@ -84,7 +79,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence2)
1.0, 1.0);
for (int i = 0; i < 3; i++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
EXPECT_NEAR(x[1], a[1], 0.00000001);
......@@ -95,8 +90,10 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestConvergence2)
///
/// \brief Test that a point not within the triangle does not move at all
///
TEST_F(imstkPbdPointTriangleConstraintTest, TestNoConvergence1)
TEST(imstkPbdPointTriangleConstraintTest, TestNoConvergence1)
{
PbdPointTriangleConstraint constraint;
Vec3d a = Vec3d(0.5, 0.0, -0.5);
const Vec3d aInit = a;
Vec3d b = Vec3d(-0.5, 0.0, -0.5);
......@@ -120,7 +117,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestNoConvergence1)
c = cInit;
Vec3d zeroVelocity = Vec3d::Zero();
m_constraint.initConstraint(
constraint.initConstraint(
{ &testPts[i], 1.0, &zeroVelocity },
{ &a, 1.0, &zeroVelocity },
{ &b, 1.0, &zeroVelocity },
......@@ -128,7 +125,7 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestNoConvergence1)
1.0, 1.0);
for (int j = 0; j < 3; j++)
{
m_constraint.solvePosition();
constraint.solvePosition();
}
// Test they haven't moved
......@@ -148,17 +145,4 @@ TEST_F(imstkPbdPointTriangleConstraintTest, TestNoConvergence1)
EXPECT_EQ(cInit[1], c[1]);
EXPECT_EQ(cInit[2], c[2]);
}
}
///
/// \brief TODO
///
int
imstkPbdPointTriangleConstraintTest(int argc, char* argv[])
{
// Init Google Test & Mock
::testing::InitGoogleTest(&argc, argv);
// Run tests with gtest
return RUN_ALL_TESTS();
}
}
\ No newline at end of file
......@@ -57,6 +57,6 @@ imstk_add_library(DynamicalModels
#-----------------------------------------------------------------------------
# Testing
#-----------------------------------------------------------------------------
#if( ${PROJECT_NAME}_BUILD_TESTING )
# add_subdirectory( Testing )
#endif()
if( ${PROJECT_NAME}_BUILD_TESTING )
add_subdirectory(Testing)
endif()
include(imstkAddTest)
imstk_add_test( DynamicalModels )
\ No newline at end of file
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "gtest/gtest.h"
#include "imstkLineMesh.h"
#include "imstkPbdConstraintFunctor.h"
#include "imstkVecDataArray.h"
using namespace imstk;
///
/// \brief Test that the correct constraint was generated
///
TEST(imstkPbdConstraintFunctorTest, TestGeneration1)
{
// Create mesh for generation
auto lineMesh = std::make_shared<LineMesh>();
auto vertices = std::make_shared<VecDataArray<double, 3>>(3);
(*vertices)[0] = Vec3d(-0.5, 0.0, 0.0);
(*vertices)[1] = Vec3d(0.0, 0.0, 0.0);
(*vertices)[2] = Vec3d(0.5, 0.0, 0.0);
auto indices = std::make_shared<VecDataArray<int, 2>>(2);
(*indices)[0] = Vec2i(0, 1);
(*indices)[1] = Vec2i(1, 2);
lineMesh->initialize(vertices, indices);
// Create functor
PbdBendConstraintFunctor constraintFunctor;
constraintFunctor.setStiffness(1e20);
constraintFunctor.setStride(1);
constraintFunctor.setGeometry(lineMesh);
// Fill container
PbdConstraintContainer container;
constraintFunctor(container);
// Check that constraint got generated
EXPECT_EQ(container.getConstraints().size(), 1);
// Check that correct constraint type got generated
std::shared_ptr<PbdBendConstraint> constraint =
std::dynamic_pointer_cast<PbdBendConstraint>(container.getConstraints()[0]);
EXPECT_NE(constraint, nullptr);
// Check constraint generated between correct elements and with correct values
EXPECT_EQ(constraint->getStiffness(), 1e20);
EXPECT_EQ(constraint->getVertexIds().size(), 3);
EXPECT_EQ(constraint->getVertexIds()[0], 0);
EXPECT_EQ(constraint->getVertexIds()[1], 1);
EXPECT_EQ(constraint->getVertexIds()[2], 2);
}
///
/// \brief Test that the correct constraint was generated with differing stride
///
TEST(imstkPbdConstraintFunctorTest, TestGeneration2)
{
// Create mesh for generation
auto lineMesh = std::make_shared<LineMesh>();
auto vertices = std::make_shared<VecDataArray<double, 3>>(5);
(*vertices)[0] = Vec3d(-1.0, 0.0, 0.0);
(*vertices)[1] = Vec3d(-0.5, 0.0, 0.0);
(*vertices)[2] = Vec3d(0.0, 0.0, 0.0);
(*vertices)[3] = Vec3d(0.5, 0.0, 0.0);
(*vertices)[4] = Vec3d(1.0, 0.0, 0.0);
auto indices = std::make_shared<VecDataArray<int, 2>>(4);
(*indices)[0] = Vec2i(0, 1);
(*indices)[1] = Vec2i(1, 2);
(*indices)[2] = Vec2i(2, 3);
(*indices)[3] = Vec2i(3, 4);
lineMesh->initialize(vertices, indices);
// Create functor
PbdBendConstraintFunctor constraintFunctor;
constraintFunctor.setStiffness(1e20);
constraintFunctor.setStride(2);
constraintFunctor.setGeometry(lineMesh);
// Fill container
PbdConstraintContainer container;
constraintFunctor(container);
// Check that constraint got generated
EXPECT_EQ(container.getConstraints().size(), 1);
// Check that correct constraint type got generated
std::shared_ptr<PbdBendConstraint> constraint =
std::dynamic_pointer_cast<PbdBendConstraint>(container.getConstraints()[0]);
EXPECT_NE(constraint, nullptr);
// Check constraint generated between correct elements and with correct values
EXPECT_EQ(constraint->getStiffness(), 1e20);
EXPECT_EQ(constraint->getVertexIds().size(), 3);
EXPECT_EQ(constraint->getVertexIds()[0], 0);
EXPECT_EQ(constraint->getVertexIds()[1], 2);
EXPECT_EQ(constraint->getVertexIds()[2], 4);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment