From 0d23fbe7a85cb623537505845ca59f1acfae2b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erin=C3=A7=20Arg=C4=B1mak?= Date: Wed, 3 Feb 2021 20:45:08 +0100 Subject: [PATCH 1/2] Spin off of Ex1 to create Open Project template --- Simulations/Demo_2013.vcxproj | 2 +- Simulations/Demo_2015.vcxproj | 2 +- Simulations/Demo_2017.vcxproj | 4 +- ...Simulator.cpp => OpenProjectSimulator.cpp} | 46 +++++++++---------- ...stemSimulator.h => OpenProjectSimulator.h} | 8 ++-- Simulations/main.cpp | 32 +------------ .../PublicMassSpringSystemTests.cpp | 22 ++++----- 7 files changed, 44 insertions(+), 72 deletions(-) rename Simulations/{MassSpringSystemSimulator.cpp => OpenProjectSimulator.cpp} (84%) rename Simulations/{MassSpringSystemSimulator.h => OpenProjectSimulator.h} (93%) diff --git a/Simulations/Demo_2013.vcxproj b/Simulations/Demo_2013.vcxproj index 9af5e825..bcd87298 100644 --- a/Simulations/Demo_2013.vcxproj +++ b/Simulations/Demo_2013.vcxproj @@ -229,7 +229,7 @@ - + diff --git a/Simulations/Demo_2015.vcxproj b/Simulations/Demo_2015.vcxproj index 1e166b05..2cb718d4 100644 --- a/Simulations/Demo_2015.vcxproj +++ b/Simulations/Demo_2015.vcxproj @@ -229,7 +229,7 @@ - + diff --git a/Simulations/Demo_2017.vcxproj b/Simulations/Demo_2017.vcxproj index 20cf9af7..9901b5fb 100644 --- a/Simulations/Demo_2017.vcxproj +++ b/Simulations/Demo_2017.vcxproj @@ -224,14 +224,14 @@ - + - + diff --git a/Simulations/MassSpringSystemSimulator.cpp b/Simulations/OpenProjectSimulator.cpp similarity index 84% rename from Simulations/MassSpringSystemSimulator.cpp rename to Simulations/OpenProjectSimulator.cpp index 68118c6e..d06b18a4 100644 --- a/Simulations/MassSpringSystemSimulator.cpp +++ b/Simulations/OpenProjectSimulator.cpp @@ -1,6 +1,6 @@ -#include "MassSpringSystemSimulator.h" +#include "OpenProjectSimulator.h" -MassSpringSystemSimulator::MassSpringSystemSimulator() { +OpenProjectSimulator::OpenProjectSimulator() { m_iIntegrator = EULER; m_fMass = 10; m_fStiffness = 40; @@ -16,18 +16,18 @@ MassSpringSystemSimulator::MassSpringSystemSimulator() { m_oldtrackmouse = Point2D(); } -const char* MassSpringSystemSimulator::getTestCasesStr() +const char* OpenProjectSimulator::getTestCasesStr() { return "2-point, Complex"; } -const char* MassSpringSystemSimulator::getIntegratorStr() +const char* OpenProjectSimulator::getIntegratorStr() { return "Euler, Leapfrog, Midpoint"; } -void MassSpringSystemSimulator::initUI(DrawingUtilitiesClass* DUC) +void OpenProjectSimulator::initUI(DrawingUtilitiesClass* DUC) { this->DUC = DUC; TwType TW_TYPE_INTEGRATOR = TwDefineEnumFromString("Integrator", getIntegratorStr()); @@ -39,13 +39,13 @@ void MassSpringSystemSimulator::initUI(DrawingUtilitiesClass* DUC) TwAddVarRW(DUC->g_pTweakBar, "Complex: Edge/Initial", TW_TYPE_FLOAT, &m_fComplexInitialRatio, "min=0.01 max=100.00 step=0.01"); } -void MassSpringSystemSimulator::reset() +void OpenProjectSimulator::reset() { (this->springs).clear(); (this->massPoints).clear(); } -void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext) +void OpenProjectSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext) { Vec3 scale = Vec3(0.1f, 0.1f, 0.1f); for (MassPoint &mp : this->massPoints) { @@ -59,7 +59,7 @@ void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateCont this->DUC->endLine(); } -void MassSpringSystemSimulator::notifyCaseChanged(int testCase) +void OpenProjectSimulator::notifyCaseChanged(int testCase) { this->reset(); switch (testCase) { @@ -130,7 +130,7 @@ void MassSpringSystemSimulator::notifyCaseChanged(int testCase) } } -void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed) +void OpenProjectSimulator::externalForcesCalculations(float timeElapsed) { // Apply the mouse deltas to g_vfMovableObjectPos (move along cameras view plane) Point2D mouseDiff; @@ -152,7 +152,7 @@ void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed) } } -void MassSpringSystemSimulator::computeForces() +void OpenProjectSimulator::computeForces() { for (MassPoint& mp : this->massPoints) { mp.force = Vec3(0, 0, 0); @@ -175,7 +175,7 @@ void MassSpringSystemSimulator::computeForces() } } -void MassSpringSystemSimulator::simulateTimestep(float timeStep) +void OpenProjectSimulator::simulateTimestep(float timeStep) { this->computeForces(); // Integrate forces into motion @@ -224,13 +224,13 @@ void MassSpringSystemSimulator::simulateTimestep(float timeStep) } } -void MassSpringSystemSimulator::onClick(int x, int y) +void OpenProjectSimulator::onClick(int x, int y) { m_trackmouse.x = x; m_trackmouse.y = y; } -void MassSpringSystemSimulator::onMouse(int x, int y) +void OpenProjectSimulator::onMouse(int x, int y) { m_oldtrackmouse.x = x; m_oldtrackmouse.y = y; @@ -238,55 +238,55 @@ void MassSpringSystemSimulator::onMouse(int x, int y) m_trackmouse.y = y; } -void MassSpringSystemSimulator::setMass(float mass) +void OpenProjectSimulator::setMass(float mass) { m_fMass = mass; } -void MassSpringSystemSimulator::setStiffness(float stiffness) +void OpenProjectSimulator::setStiffness(float stiffness) { m_fStiffness = stiffness; } -void MassSpringSystemSimulator::setDampingFactor(float damping) +void OpenProjectSimulator::setDampingFactor(float damping) { m_fDamping = damping; } -int MassSpringSystemSimulator::addMassPoint(Vec3 position, Vec3 velocity, bool isFixed) +int OpenProjectSimulator::addMassPoint(Vec3 position, Vec3 velocity, bool isFixed) { int index = (this->massPoints).size(); (this->massPoints).push_back(MassPoint(position, velocity, isFixed)); return index; } -void MassSpringSystemSimulator::addSpring(int masspoint1, int masspoint2, float initialLength) +void OpenProjectSimulator::addSpring(int masspoint1, int masspoint2, float initialLength) { (this->springs).push_back( Spring((this->massPoints)[masspoint1], (this->massPoints)[masspoint2], initialLength) ); } -int MassSpringSystemSimulator::getNumberOfMassPoints() const +int OpenProjectSimulator::getNumberOfMassPoints() const { return massPoints.size(); } -int MassSpringSystemSimulator::getNumberOfSprings() const +int OpenProjectSimulator::getNumberOfSprings() const { return springs.size(); } -Vec3 MassSpringSystemSimulator::getPositionOfMassPoint(int index) const +Vec3 OpenProjectSimulator::getPositionOfMassPoint(int index) const { return (this->massPoints)[index].position; } -Vec3 MassSpringSystemSimulator::getVelocityOfMassPoint(int index) const +Vec3 OpenProjectSimulator::getVelocityOfMassPoint(int index) const { return (this->massPoints)[index].velocity; } -void MassSpringSystemSimulator::applyExternalForce(Vec3 force) +void OpenProjectSimulator::applyExternalForce(Vec3 force) { } diff --git a/Simulations/MassSpringSystemSimulator.h b/Simulations/OpenProjectSimulator.h similarity index 93% rename from Simulations/MassSpringSystemSimulator.h rename to Simulations/OpenProjectSimulator.h index 3b56d69d..f363066e 100644 --- a/Simulations/MassSpringSystemSimulator.h +++ b/Simulations/OpenProjectSimulator.h @@ -1,5 +1,5 @@ -#ifndef MASSSPRINGSYSTEMSIMULATOR_h -#define MASSSPRINGSYSTEMSIMULATOR_h +#ifndef OpenProjectSimulator_h +#define OpenProjectSimulator_h #include "Simulator.h" // Do Not Change @@ -27,10 +27,10 @@ struct Spring { }; -class MassSpringSystemSimulator:public Simulator{ +class OpenProjectSimulator:public Simulator{ public: // Construtors - MassSpringSystemSimulator(); + OpenProjectSimulator(); // UI Functions void initUI(DrawingUtilitiesClass * DUC); diff --git a/Simulations/main.cpp b/Simulations/main.cpp index ade04383..e1dc8c25 100644 --- a/Simulations/main.cpp +++ b/Simulations/main.cpp @@ -20,23 +20,7 @@ using namespace GamePhysics; //#define ADAPTIVESTEP -//#define TEMPLATE_DEMO -#define MASS_SPRING_SYSTEM -//#define RIGID_BODY_SYSTEM -//#define SPH_SYSTEM - -#ifdef TEMPLATE_DEMO -#include "TemplateSimulator.h" -#endif -#ifdef MASS_SPRING_SYSTEM -#include "MassSpringSystemSimulator.h" -#endif -#ifdef RIGID_BODY_SYSTEM -//#include "RigidBodySystemSimulator.h" -#endif -#ifdef SPH_SYSTEM -//#include "SPHSystemSimulator.h" -#endif +#include "OpenProjectSimulator.h" DrawingUtilitiesClass * g_pDUC; Simulator * g_pSimulator; @@ -357,19 +341,7 @@ int main(int argc, char* argv[]) g_pDUC->g_camera.SetViewParams(XMLoadFloat3(&eye), XMLoadFloat3(&lookAt)); g_pDUC-> g_camera.SetButtonMasks(MOUSE_MIDDLE_BUTTON, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON); - -#ifdef TEMPLATE_DEMO - g_pSimulator= new TemplateSimulator(); -#endif -#ifdef MASS_SPRING_SYSTEM - g_pSimulator= new MassSpringSystemSimulator(); -#endif -#ifdef RIGID_BODY_SYSTEM - //g_pSimulator= new RigidBodySystemSimulator(); -#endif -#ifdef SPH_SYSTEM - //g_pSimulator= new SPHSystemSimulator(); -#endif + g_pSimulator= new OpenProjectSimulator(); g_pSimulator->reset(); // Init DXUT and create device diff --git a/SimulatorTester/PublicMassSpringSystemTests.cpp b/SimulatorTester/PublicMassSpringSystemTests.cpp index 3633eade..a4d564c1 100644 --- a/SimulatorTester/PublicMassSpringSystemTests.cpp +++ b/SimulatorTester/PublicMassSpringSystemTests.cpp @@ -1,5 +1,5 @@ #include "CppUnitTest.h" -#include "MassSpringSystemSimulator.h" +#include "OpenProjectSimulator.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; @@ -8,9 +8,9 @@ namespace SimulatorTester TEST_CLASS(PublicMassSpringSystemTests) { public: - void testSceneSetup(MassSpringSystemSimulator* &msss) { + void testSceneSetup(OpenProjectSimulator* &msss) { if (msss) delete msss; - msss = new MassSpringSystemSimulator(); + msss = new OpenProjectSimulator(); msss->setMass(10.0f); msss->setDampingFactor(0.0f); msss->setStiffness(40.0f); @@ -25,20 +25,20 @@ namespace SimulatorTester TEST_METHOD(TestNumberofMassPoints) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); int num = msss->getNumberOfMassPoints(); Assert::AreEqual(2.0f,(float)num,0.0001f,L"Number of Mass Points is not equal to 2",LINE_INFO()); } TEST_METHOD(TestNumberofSprings) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); Assert::AreEqual(1.0f,(float)msss->getNumberOfSprings(),0.0001f,L"Number of Mass Points is not equal to 1",LINE_INFO()); } TEST_METHOD(TestPositionOfMassPointsInitially) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); @@ -50,7 +50,7 @@ namespace SimulatorTester TEST_METHOD(TestPositionOfMassPointsAfter10TimeStepEuler) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); msss->setIntegrator(EULER); for(int i =0; i <10; i++) @@ -65,7 +65,7 @@ namespace SimulatorTester TEST_METHOD(TestPositionOfMassPointsAfter10TimeStepMidPoint) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); msss->setIntegrator(MIDPOINT); for(int i =0; i <10; i++) @@ -80,7 +80,7 @@ namespace SimulatorTester TEST_METHOD(TestVelocityOfMassPointsInitially) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); Assert::AreEqual(-1.0f,(float)msss->getVelocityOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); @@ -92,7 +92,7 @@ namespace SimulatorTester TEST_METHOD(TestVelocityOfMassPointsAfter10TimeStepEuler) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); msss->setIntegrator(EULER); for(int i =0; i <10; i++) @@ -107,7 +107,7 @@ namespace SimulatorTester TEST_METHOD(TestVelocityOfMassPointsAfter10TimeStepMidPoint) { - MassSpringSystemSimulator * msss = NULL; + OpenProjectSimulator * msss = NULL; testSceneSetup(msss); msss->setIntegrator(MIDPOINT); for(int i =0; i <10; i++) From 0cc125174283c16ceebc959e025dd90d68488757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erin=C3=A7=20Arg=C4=B1mak?= Date: Wed, 3 Feb 2021 21:16:09 +0100 Subject: [PATCH 2/2] Clean template and the test file --- Simulations/OpenProjectSimulator.cpp | 242 +----------------- Simulations/OpenProjectSimulator.h | 34 --- .../PublicMassSpringSystemTests.cpp | 124 --------- SimulatorTester/PublicOpenProjectTests.cpp | 24 ++ SimulatorTester/SimulatorTester_2013.vcxproj | 2 +- SimulatorTester/SimulatorTester_2015.vcxproj | 2 +- SimulatorTester/SimulatorTester_2017.vcxproj | 2 +- 7 files changed, 28 insertions(+), 402 deletions(-) delete mode 100644 SimulatorTester/PublicMassSpringSystemTests.cpp create mode 100644 SimulatorTester/PublicOpenProjectTests.cpp diff --git a/Simulations/OpenProjectSimulator.cpp b/Simulations/OpenProjectSimulator.cpp index d06b18a4..9447c181 100644 --- a/Simulations/OpenProjectSimulator.cpp +++ b/Simulations/OpenProjectSimulator.cpp @@ -1,227 +1,40 @@ #include "OpenProjectSimulator.h" OpenProjectSimulator::OpenProjectSimulator() { - m_iIntegrator = EULER; - m_fMass = 10; - m_fStiffness = 40; - m_fDamping = 0; - m_fGravity = 0; - m_fComplexInitialRatio = 0.50; - m_vfMovableObjectFinalPos = Vec3(); - - - m_springColor = Vec3(50, 50, 50); - m_mouse = Point2D(); - m_trackmouse = Point2D(); - m_oldtrackmouse = Point2D(); } const char* OpenProjectSimulator::getTestCasesStr() { - return "2-point, Complex"; -} - - -const char* OpenProjectSimulator::getIntegratorStr() -{ - return "Euler, Leapfrog, Midpoint"; + return "Basic"; } void OpenProjectSimulator::initUI(DrawingUtilitiesClass* DUC) { this->DUC = DUC; - TwType TW_TYPE_INTEGRATOR = TwDefineEnumFromString("Integrator", getIntegratorStr()); - TwAddVarRW(DUC->g_pTweakBar, "Integrator", TW_TYPE_INTEGRATOR, &m_iIntegrator, ""); - TwAddVarRW(DUC->g_pTweakBar, "Mass Spheres", TW_TYPE_FLOAT, &m_fMass, "min=0.1 max=100.0 step=0.1"); - TwAddVarRW(DUC->g_pTweakBar, "Stiffness", TW_TYPE_FLOAT, &m_fStiffness, "min=0.0 max=500 step=0.5"); - TwAddVarRW(DUC->g_pTweakBar, "Damping", TW_TYPE_FLOAT, &m_fDamping, "min=0.00 max=5.00 step=0.05"); - TwAddVarRW(DUC->g_pTweakBar, "Gravity", TW_TYPE_FLOAT, &m_fGravity, "min=0.00 max=100.00 step=0.01"); - TwAddVarRW(DUC->g_pTweakBar, "Complex: Edge/Initial", TW_TYPE_FLOAT, &m_fComplexInitialRatio, "min=0.01 max=100.00 step=0.01"); } void OpenProjectSimulator::reset() { - (this->springs).clear(); - (this->massPoints).clear(); } void OpenProjectSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext) { - Vec3 scale = Vec3(0.1f, 0.1f, 0.1f); - for (MassPoint &mp : this->massPoints) { - this->DUC->drawSphere(mp.position, scale); - } - - this->DUC->beginLine(); - for (Spring& s : this->springs) { - this->DUC->drawLine(s.mp1.position, m_springColor, s.mp2.position, m_springColor); - } - this->DUC->endLine(); } void OpenProjectSimulator::notifyCaseChanged(int testCase) { - this->reset(); - switch (testCase) { - case 0: - addSpring( - addMassPoint({ 0, 0, 0 }, { -1, 0, 0 }, false), - addMassPoint({ 0, 2, 0 }, { 1, 0, 0 }, false), - 1 - ); - break; - case 1: - Vec3 positionOffset = Vec3(0, 2, 0); - float vertexMap[3] = { 0, 1, (1 + sqrt(5)) / 2 }; - vector positions; - for (int i = 0; i < 3; i++) { - Vec3 position; - for (int j = 2; j >= 0; j--) { - position[(i + j) % 3] = vertexMap[j]; - } - positions.push_back(position); - positions.push_back(-position); - position[(i % 3 + 1) % 3] *= -1; - positions.push_back(position); - position[(i % 3 + 1) % 3] *= -1; - position[(i % 3 + 2) % 3] *= -1; - positions.push_back(position); - } - for(Vec3& position : positions) { - addMassPoint(position + positionOffset, { 0, 0, 0 }, false); - } - - float springLength = sqrt(positions[0].squaredDistanceTo(positions[8])) * m_fComplexInitialRatio; - addSpring(10, 0, springLength); - addSpring(10, 3, springLength); - addSpring(10, 5, springLength); - addSpring(10, 7, springLength); - addSpring(10, 8, springLength); - - addSpring(11, 1, springLength); - addSpring(11, 2, springLength); - addSpring(11, 4, springLength); - addSpring(11, 6, springLength); - addSpring(11, 9, springLength); - - addSpring(0, 7, springLength); - addSpring(7, 5, springLength); - addSpring(5, 3, springLength); - addSpring(3, 8, springLength); - addSpring(8, 0, springLength); - - addSpring(1, 6, springLength); - addSpring(6, 4, springLength); - addSpring(4, 2, springLength); - addSpring(2, 9, springLength); - addSpring(9, 1, springLength); - - addSpring(3, 1, springLength); - addSpring(3, 6, springLength); - addSpring(8, 6, springLength); - addSpring(8, 4, springLength); - addSpring(0, 4, springLength); - addSpring(0, 2, springLength); - addSpring(7, 2, springLength); - addSpring(7, 9, springLength); - addSpring(5, 9, springLength); - addSpring(5, 1, springLength); - break; - } } void OpenProjectSimulator::externalForcesCalculations(float timeElapsed) { - // Apply the mouse deltas to g_vfMovableObjectPos (move along cameras view plane) - Point2D mouseDiff; - mouseDiff.x = m_trackmouse.x - m_oldtrackmouse.x; - mouseDiff.y = m_trackmouse.y - m_oldtrackmouse.y; - if (mouseDiff.x != 0 || mouseDiff.y != 0) - { - Mat4 worldViewInv = Mat4(DUC->g_camera.GetWorldMatrix() * DUC->g_camera.GetViewMatrix()); - worldViewInv = worldViewInv.inverse(); - Vec3 inputView = Vec3((float)mouseDiff.x, (float)-mouseDiff.y, 0); - Vec3 inputWorld = worldViewInv.transformVectorNormal(inputView); - // find a proper scale! - float inputScale = 0.005f; - inputWorld = inputWorld * inputScale; - massPoints[0].position = m_vfMovableObjectFinalPos + inputWorld; - } - else { - m_vfMovableObjectFinalPos = massPoints[0].position; - } } void OpenProjectSimulator::computeForces() { - for (MassPoint& mp : this->massPoints) { - mp.force = Vec3(0, 0, 0); - // Gravity - mp.force += Vec3(0, -m_fGravity, 0) * m_fMass; - // Damping - mp.force += mp.velocity * -m_fDamping; - } - - for (Spring& s : this->springs) { - Vec3 mid_X_mp1, mid_X_mp2; - - Vec3 differenceVector = (s.mp1.position - s.mp2.position); - float distance = sqrt(s.mp1.position.squaredDistanceTo(s.mp2.position)); - Vec3 elasticForce = (differenceVector / distance) * ((distance - (double)s.initialLength) * -m_fStiffness); - - // Apply to endpoints - s.mp1.force += elasticForce; - s.mp2.force += -elasticForce; - } } void OpenProjectSimulator::simulateTimestep(float timeStep) { - this->computeForces(); - // Integrate forces into motion - for (MassPoint& mp : this->massPoints) { - if (mp.isFixed) continue; - switch (m_iIntegrator) { - case EULER: - mp.position += mp.velocity * timeStep; - mp.velocity += (mp.force / m_fMass) * timeStep; - break; - case MIDPOINT: - mp.oldPosition = mp.position; - mp.oldVelocity = mp.velocity; - mp.position += mp.velocity * 0.5 *timeStep; - mp.velocity += (mp.force / m_fMass) * 0.5 * timeStep; - break; - case LEAPFROG: // kick-drift-kick version of Leapfrog (synchronised and allows variable time-step) - mp.velocity += (mp.force / m_fMass) * 0.5 * timeStep; - mp.position += mp.velocity * timeStep; - break; - } - } - - //compute force at new point and apply if needed (also, floor collision once we have final state) - this->computeForces(); - for (MassPoint& mp : this->massPoints) { - if (mp.isFixed) continue; - switch (m_iIntegrator) { - case EULER: - break; - case MIDPOINT: - mp.position = mp.oldPosition + mp.velocity * timeStep; - mp.velocity = mp.oldVelocity + (mp.force / m_fMass) *timeStep; - break; - case LEAPFROG: // kick-drift-kick version of Leapfrog (synchronised and allows variable time-step) - mp.velocity += (mp.force / m_fMass) * 0.5 * timeStep; - break; - } - - // Floor collision - float floor_level = -1.0; - if (mp.position.y < floor_level) { - mp.position.y = floor_level + (floor_level - mp.position.y); - mp.velocity.y *= -1; - } - } } void OpenProjectSimulator::onClick(int x, int y) @@ -237,56 +50,3 @@ void OpenProjectSimulator::onMouse(int x, int y) m_trackmouse.x = x; m_trackmouse.y = y; } - -void OpenProjectSimulator::setMass(float mass) -{ - m_fMass = mass; -} - -void OpenProjectSimulator::setStiffness(float stiffness) -{ - m_fStiffness = stiffness; -} - -void OpenProjectSimulator::setDampingFactor(float damping) -{ - m_fDamping = damping; -} - -int OpenProjectSimulator::addMassPoint(Vec3 position, Vec3 velocity, bool isFixed) -{ - int index = (this->massPoints).size(); - (this->massPoints).push_back(MassPoint(position, velocity, isFixed)); - return index; -} - -void OpenProjectSimulator::addSpring(int masspoint1, int masspoint2, float initialLength) -{ - (this->springs).push_back( - Spring((this->massPoints)[masspoint1], (this->massPoints)[masspoint2], initialLength) - ); -} - -int OpenProjectSimulator::getNumberOfMassPoints() const -{ - return massPoints.size(); -} - -int OpenProjectSimulator::getNumberOfSprings() const -{ - return springs.size(); -} - -Vec3 OpenProjectSimulator::getPositionOfMassPoint(int index) const -{ - return (this->massPoints)[index].position; -} - -Vec3 OpenProjectSimulator::getVelocityOfMassPoint(int index) const -{ - return (this->massPoints)[index].velocity; -} - -void OpenProjectSimulator::applyExternalForce(Vec3 force) -{ -} diff --git a/Simulations/OpenProjectSimulator.h b/Simulations/OpenProjectSimulator.h index f363066e..e004379d 100644 --- a/Simulations/OpenProjectSimulator.h +++ b/Simulations/OpenProjectSimulator.h @@ -2,12 +2,6 @@ #define OpenProjectSimulator_h #include "Simulator.h" -// Do Not Change -#define EULER 0 -#define LEAPFROG 1 -#define MIDPOINT 2 -// Do Not Change - struct MassPoint { MassPoint(Vec3 position, Vec3 velocity, bool isFixed) : position(position), velocity(velocity), isFixed(isFixed) { @@ -45,40 +39,12 @@ class OpenProjectSimulator:public Simulator{ // Specific Functions const char* getTestCasesStr(); - const char* getIntegratorStr(); - - void setMass(float mass); - void setStiffness(float stiffness); - void setDampingFactor(float damping); - int addMassPoint(Vec3 position, Vec3 velocity, bool isFixed); - void addSpring(int masspoint1, int masspoint2, float initialLength); - int getNumberOfMassPoints() const; - int getNumberOfSprings() const; - Vec3 getPositionOfMassPoint(int index) const; - Vec3 getVelocityOfMassPoint(int index) const; void applyExternalForce(Vec3 force); - // Do Not Change - void setIntegrator(int integrator) { - m_iIntegrator = integrator; - } - private: // Data Attributes - float m_fMass; - float m_fStiffness; - float m_fDamping; - int m_iIntegrator; - float m_fGravity; - float m_fComplexInitialRatio; - - std::vector massPoints; - std::vector springs; // UI Attributes - Vec3 m_springColor; - Vec3 m_externalForce; - Vec3 m_vfMovableObjectFinalPos; Point2D m_mouse; Point2D m_trackmouse; Point2D m_oldtrackmouse; diff --git a/SimulatorTester/PublicMassSpringSystemTests.cpp b/SimulatorTester/PublicMassSpringSystemTests.cpp deleted file mode 100644 index a4d564c1..00000000 --- a/SimulatorTester/PublicMassSpringSystemTests.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "CppUnitTest.h" -#include "OpenProjectSimulator.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -namespace SimulatorTester -{ - TEST_CLASS(PublicMassSpringSystemTests) - { - public: - void testSceneSetup(OpenProjectSimulator* &msss) { - if (msss) delete msss; - msss = new OpenProjectSimulator(); - msss->setMass(10.0f); - msss->setDampingFactor(0.0f); - msss->setStiffness(40.0f); - msss->applyExternalForce(Vec3(0, 0, 0)); - int p0 = msss->addMassPoint(Vec3(0.0, 0.0f, 0), Vec3(-1.0, 0.0f, 0), false); - int p1 = msss->addMassPoint(Vec3(0.0, 2.0f, 0), Vec3(1.0, 0.0f, 0), false); - msss->addSpring(p0, p1, 1.0); - - //DrawingUtilitiesClass * DUC = new DrawingUtilitiesClass(); - //msss->initUI(DUC); - } - - TEST_METHOD(TestNumberofMassPoints) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - int num = msss->getNumberOfMassPoints(); - Assert::AreEqual(2.0f,(float)num,0.0001f,L"Number of Mass Points is not equal to 2",LINE_INFO()); - } - TEST_METHOD(TestNumberofSprings) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - Assert::AreEqual(1.0f,(float)msss->getNumberOfSprings(),0.0001f,L"Number of Mass Points is not equal to 1",LINE_INFO()); - } - TEST_METHOD(TestPositionOfMassPointsInitially) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(2.0f,(float)msss->getPositionOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - TEST_METHOD(TestPositionOfMassPointsAfter10TimeStepEuler) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - msss->setIntegrator(EULER); - for(int i =0; i <10; i++) - msss->simulateTimestep(0.005);; - Assert::AreEqual(-0.04994f,(float)msss->getPositionOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.00449f,(float)msss->getPositionOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.04994f,(float)msss->getPositionOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(1.9955f,(float)msss->getPositionOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - TEST_METHOD(TestPositionOfMassPointsAfter10TimeStepMidPoint) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - msss->setIntegrator(MIDPOINT); - for(int i =0; i <10; i++) - msss->simulateTimestep(0.005); - Assert::AreEqual(-0.0499164f,(float)msss->getPositionOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0049928f,(float)msss->getPositionOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0499164f,(float)msss->getPositionOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(1.99501f,(float)msss->getPositionOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getPositionOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - TEST_METHOD(TestVelocityOfMassPointsInitially) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - Assert::AreEqual(-1.0f,(float)msss->getVelocityOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(1.0f,(float)msss->getVelocityOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - TEST_METHOD(TestVelocityOfMassPointsAfter10TimeStepEuler) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - msss->setIntegrator(EULER); - for(int i =0; i <10; i++) - msss->simulateTimestep(0.005); - Assert::AreEqual(-0.995508f,(float)msss->getVelocityOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.199592f,(float)msss->getVelocityOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.995508f,(float)msss->getVelocityOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(-0.199592f,(float)msss->getVelocityOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - TEST_METHOD(TestVelocityOfMassPointsAfter10TimeStepMidPoint) - { - OpenProjectSimulator * msss = NULL; - testSceneSetup(msss); - msss->setIntegrator(MIDPOINT); - for(int i =0; i <10; i++) - msss->simulateTimestep(0.005); - Assert::AreEqual(-0.995013f,(float)msss->getVelocityOfMassPoint(0).x,0.0001f,L"Mass Point at index 0, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.199424f,(float)msss->getVelocityOfMassPoint(0).y,0.0001f,L"Mass Point at index 0, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(0).z,0.0001f,L"Mass Point at index 0, Z value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.995013f,(float)msss->getVelocityOfMassPoint(1).x,0.0001f,L"Mass Point at index 1, X value is wrong !!",LINE_INFO()); - Assert::AreEqual(-0.199424f,(float)msss->getVelocityOfMassPoint(1).y,0.0001f,L"Mass Point at index 1, Y value is wrong !!",LINE_INFO()); - Assert::AreEqual(0.0f,(float)msss->getVelocityOfMassPoint(1).z,0.0001f,L"Mass Point at index 1, Z value is wrong !!",LINE_INFO()); - } - - }; -} \ No newline at end of file diff --git a/SimulatorTester/PublicOpenProjectTests.cpp b/SimulatorTester/PublicOpenProjectTests.cpp new file mode 100644 index 00000000..d252413b --- /dev/null +++ b/SimulatorTester/PublicOpenProjectTests.cpp @@ -0,0 +1,24 @@ +#include "CppUnitTest.h" +#include "OpenProjectSimulator.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace SimulatorTester +{ + TEST_CLASS(PublicOpenProjectTests) + { + public: + void testSceneSetup(OpenProjectSimulator* &ops) { + if (ops) delete ops; + ops = new OpenProjectSimulator(); + } + + TEST_METHOD(SampleTest) + { + OpenProjectSimulator * ops = NULL; + testSceneSetup(ops); + int num = 2; + Assert::AreEqual(2.0f,(float)num,0.0001f,L"2 is not equal to 2",LINE_INFO()); + } + }; +} \ No newline at end of file diff --git a/SimulatorTester/SimulatorTester_2013.vcxproj b/SimulatorTester/SimulatorTester_2013.vcxproj index fe72e3c8..9a303a72 100644 --- a/SimulatorTester/SimulatorTester_2013.vcxproj +++ b/SimulatorTester/SimulatorTester_2013.vcxproj @@ -162,7 +162,7 @@ - + diff --git a/SimulatorTester/SimulatorTester_2015.vcxproj b/SimulatorTester/SimulatorTester_2015.vcxproj index 8952a673..a169e0be 100644 --- a/SimulatorTester/SimulatorTester_2015.vcxproj +++ b/SimulatorTester/SimulatorTester_2015.vcxproj @@ -162,7 +162,7 @@ - + diff --git a/SimulatorTester/SimulatorTester_2017.vcxproj b/SimulatorTester/SimulatorTester_2017.vcxproj index f32396d7..8a1bbd36 100644 --- a/SimulatorTester/SimulatorTester_2017.vcxproj +++ b/SimulatorTester/SimulatorTester_2017.vcxproj @@ -163,7 +163,7 @@ - +