diff --git a/nodeTest.cpp b/nodeTest.cpp index 465f108..32ca4ae 100644 --- a/nodeTest.cpp +++ b/nodeTest.cpp @@ -17,44 +17,12 @@ namespace vb01{ nodeC = new Node(); nodeD = new Node(); - parentA = new Node(); - childA0 = new Node(); - childA1 = new Node(); - childA2 = new Node(); - parentA->attachChild(childA0); - parentA->attachChild(childA1); - parentA->attachChild(childA2); - parentB = new Node(); - childB0 = new Node(); - childB1 = new Node(); - childB2 = new Node(); - parentB->attachChild(childB0); - parentB->attachChild(childB1); - parentB->attachChild(childB2); - parentC = new Node(); - childC0 = new Node(); - childC1 = new Node(); - childC2 = new Node(); - parentC->attachChild(childC0); - parentC->attachChild(childC1); - parentC->attachChild(childC2); - parentD = new Node(); - childD0 = new Node(); - childD1 = new Node(); - childD2 = new Node(); - parentD->attachChild(childD0); - parentD->attachChild(childD1); - parentD->attachChild(childD2); - parentA->attachChild(parentB); - parentB->attachChild(parentC); - parentC->attachChild(parentD); - - rootNode->attachChild(nodeA); - nodeA->attachChild(nodeB); - /* - nodeA->attachChild(nodeC); - nodeA->attachChild(nodeD); - */ + firstChainNode = new Node(); + rootNode->attachChild(firstChainNode); + secondChainNode = new Node(); + firstChainNode->attachChild(secondChainNode); + thirdChainNode = new Node(); + secondChainNode->attachChild(thirdChainNode); } void NodeTest::tearDown(){ @@ -67,6 +35,92 @@ namespace vb01{ */ } + void NodeTest::testLocalToGlobalPosition(){ + firstChainNode->setPosition(Vector3::VEC_ZERO); + secondChainNode->setPosition(Vector3::VEC_ZERO); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + + CPPUNIT_ASSERT(firstChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + CPPUNIT_ASSERT(secondChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + + Vector3 pos0 = Vector3(1, 1, 1); + thirdChainNode->setPosition(pos0); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos0); + + Vector3 pos1 = Vector3(1, 2, 3); + secondChainNode->setPosition(pos1); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + CPPUNIT_ASSERT(secondChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos1); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos1); + + Vector3 pos2 = Vector3(1, 2, 1); + Vector3 pos22 = Vector3(1, 2, 2); + secondChainNode->setPosition(pos2); + thirdChainNode->setPosition(pos22); + CPPUNIT_ASSERT(secondChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos2); + CPPUNIT_ASSERT(secondChainNode->localToGlobalPosition(pos22) == pos2 + pos22); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos2 + pos22); + + Vector3 pos3 = Vector3(1, 2, 1); + firstChainNode->setPosition(pos3); + secondChainNode->setPosition(Vector3::VEC_ZERO); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + CPPUNIT_ASSERT(firstChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos3); + CPPUNIT_ASSERT(secondChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos3); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalPosition(Vector3::VEC_ZERO) == pos3); + } + + void NodeTest::testGlobalToLocalPosition(){ + firstChainNode->setPosition(Vector3::VEC_ZERO); + secondChainNode->setPosition(Vector3::VEC_ZERO); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + + CPPUNIT_ASSERT(firstChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + CPPUNIT_ASSERT(secondChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + CPPUNIT_ASSERT(thirdChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == Vector3::VEC_ZERO); + + Vector3 pos0 = Vector3(1, 1, 1); + thirdChainNode->setPosition(pos0); + CPPUNIT_ASSERT(thirdChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos0); + + Vector3 pos1 = Vector3(1, 2, 3); + secondChainNode->setPosition(pos1); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + CPPUNIT_ASSERT(secondChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos1); + CPPUNIT_ASSERT(thirdChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos1); + + Vector3 pos2 = Vector3(1, 2, 3); + secondChainNode->setPosition(pos2); + thirdChainNode->setPosition(pos2); + CPPUNIT_ASSERT(secondChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos2); + CPPUNIT_ASSERT(thirdChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos2 * 2); + + Vector3 pos3 = Vector3(1, 2, 3); + firstChainNode->setPosition(pos3); + secondChainNode->setPosition(Vector3::VEC_ZERO); + thirdChainNode->setPosition(Vector3::VEC_ZERO); + CPPUNIT_ASSERT(firstChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos3); + CPPUNIT_ASSERT(secondChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos3); + CPPUNIT_ASSERT(thirdChainNode->globalToLocalPosition(Vector3::VEC_ZERO) == -pos3); + } + + void NodeTest::testGlobalToLocalOrientation(){ + } + + void NodeTest::testLocalToGlobalOrientation(){ + firstChainNode->setOrientation(Quaternion::QUAT_W); + secondChainNode->setOrientation(Quaternion::QUAT_W); + thirdChainNode->setOrientation(Quaternion::QUAT_W); + + CPPUNIT_ASSERT(firstChainNode->localToGlobalOrientation(Quaternion::QUAT_W) == Quaternion::QUAT_W); + CPPUNIT_ASSERT(secondChainNode->localToGlobalOrientation(Quaternion::QUAT_W) == Quaternion::QUAT_W); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalOrientation(Quaternion::QUAT_W) == Quaternion::QUAT_W); + + Quaternion q0 = Quaternion(.2, Vector3(0, 1, 0)); + thirdChainNode->setOrientation(q0); + CPPUNIT_ASSERT(thirdChainNode->localToGlobalOrientation(Quaternion::QUAT_W) == q0); + } void NodeTest::testDetachChild(){ nodeA->dettachChild(nodeB); CPPUNIT_ASSERT(nodeA->getChild(0) != nodeB); @@ -117,6 +171,7 @@ namespace vb01{ vector parentCDescendants; vector parentDDescendants; + /* parentA->getDescendants(parentADescendants); parentB->getDescendants(parentBDescendants); parentC->getDescendants(parentCDescendants); @@ -126,5 +181,6 @@ namespace vb01{ CPPUNIT_ASSERT(parentDDescendants[0] == childD0); CPPUNIT_ASSERT(parentDDescendants[1] == childD1); CPPUNIT_ASSERT(parentDDescendants[2] == childD2); + */ } } diff --git a/nodeTest.h b/nodeTest.h index 5f0bf00..0ac445c 100644 --- a/nodeTest.h +++ b/nodeTest.h @@ -9,10 +9,16 @@ namespace vb01{ class NodeTest : public CppUnit::TestFixture{ CPPUNIT_TEST_SUITE(NodeTest); - //CPPUNIT_TEST(testDetachChild); + /* + CPPUNIT_TEST(testDetachChild); CPPUNIT_TEST(testAdjustDir); CPPUNIT_TEST(testAdjustUp); CPPUNIT_TEST(testGetDescendants); + */ + CPPUNIT_TEST(testLocalToGlobalPosition); + CPPUNIT_TEST(testGlobalToLocalPosition); + CPPUNIT_TEST(testLocalToGlobalOrientation); + CPPUNIT_TEST(testGlobalToLocalOrientation); CPPUNIT_TEST_SUITE_END(); public: @@ -24,28 +30,19 @@ namespace vb01{ void testAdjustUp(); void testDetachChild(); void testGetDescendants(); + void testLocalToGlobalPosition(); + void testGlobalToLocalPosition(); + void testLocalToGlobalOrientation(); + void testGlobalToLocalOrientation(); Node *rootNode = nullptr, - *parentA = nullptr, - *childA0 = nullptr, - *childA1 = nullptr, - *childA2 = nullptr, - *parentB = nullptr, - *childB0 = nullptr, - *childB1 = nullptr, - *childB2 = nullptr, - *parentC = nullptr, - *childC0 = nullptr, - *childC1 = nullptr, - *childC2 = nullptr, - *parentD = nullptr, - *childD0 = nullptr, - *childD1 = nullptr, - *childD2 = nullptr, *nodeA = nullptr, *nodeB = nullptr, *nodeC = nullptr, - *nodeD = nullptr; + *nodeD = nullptr, + *firstChainNode = nullptr, + *secondChainNode = nullptr, + *thirdChainNode = nullptr; }; } diff --git a/quaternion.h b/quaternion.h index 8bd526e..17f279b 100755 --- a/quaternion.h +++ b/quaternion.h @@ -17,6 +17,8 @@ namespace vb01{ *this = this->fromAngle(angle, axis); } Quaternion(){} + inline bool operator==(Quaternion q){return w == q.w && x == q.x && y == q.y && z == q.z;} + inline bool operator!=(Quaternion q){return w != q.w || x != q.x || y != q.y || z != q.z;} inline Quaternion operator- (){return Quaternion(w, -x, -y, -z);} inline Quaternion operator* (Quaternion q){ float a1 = this->w, a2 = q.w,