From cceb38c7eec32a80095133a3c2e252003244f3d5 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 27 Feb 2024 14:33:46 -0500 Subject: [PATCH] =?UTF-8?q?=C3=89tapes=201,=202=20et=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 36 +++++----- labo01/CMakeLists.txt | 13 ++-- labo_physique/ParticleSimApplication.cpp | 2 +- labo_physique/ParticleSystem.cpp | 83 +++++++++++++++++------- 4 files changed, 86 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index efbc4f1..c60e7bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,19 +6,19 @@ project(GTI320-labos) #-------------------------------------------------- include(FetchContent) FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.11.0 + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.11.0 ) # For Windows: Prevent overriding the parent project's compiler/linker settings set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # No need for GMock -set( BUILD_GMOCK OFF CACHE BOOL "" FORCE) -set( INSTALL_GTEST OFF CACHE BOOL "" FORCE) -set( GTEST_FORCE_SHARED_CRT ON CACHE BOOL "" FORCE) -set( GTEST_DISABLE_PTHREADS ON CACHE BOOL "" FORCE) +set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) +set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) +set(GTEST_FORCE_SHARED_CRT ON CACHE BOOL "" FORCE) +set(GTEST_DISABLE_PTHREADS ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) #-------------------------------------------------- @@ -26,22 +26,22 @@ FetchContent_MakeAvailable(googletest) #-------------------------------------------------- include(FetchContent) FetchContent_Declare( - nanogui - GIT_REPOSITORY https://github.com/mitsuba-renderer/nanogui.git - GIT_PROGRESS TRUE + nanogui + GIT_REPOSITORY https://github.com/mitsuba-renderer/nanogui.git + GIT_PROGRESS TRUE ) -set( NANOGUI_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) -set( NANOGUI_BUILD_SHARED OFF CACHE BOOL "" FORCE) -set( NANOGUI_BUILD_PYTHON OFF CACHE BOOL "" FORCE) -set( NANOGUI_USE_OPENGL ON CACHE BOOL "" FORCE) +set(NANOGUI_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +set(NANOGUI_BUILD_SHARED OFF CACHE BOOL "" FORCE) +set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL "" FORCE) +set(NANOGUI_USE_OPENGL ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(nanogui) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) -include_directories(${CMAKE_SOURCE_DIR}/labo01/src ${COMMON_INCLUDES}) +include_directories(${CMAKE_SOURCE_DIR}/labo01 ${COMMON_INCLUDES}) add_subdirectory(labo01) add_subdirectory(labo_physique) -if( MSVC ) - set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT labo_physique) -endif() +if (MSVC) + set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT labo_physique) +endif () diff --git a/labo01/CMakeLists.txt b/labo01/CMakeLists.txt index e206da3..5144fc1 100644 --- a/labo01/CMakeLists.txt +++ b/labo01/CMakeLists.txt @@ -3,14 +3,17 @@ cmake_minimum_required(VERSION 3.15) project(labo01) # Setup language requirements -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") # Add .cpp and .h files -set(HEADERS DenseStorage.h MatrixBase.h Matrix.h Math3D.h Vector.h Operators.h) -set(SOURCE main.cpp) -add_executable(labo01 ${SOURCE} ${HEADERS}) +file(GLOB_RECURSE MAIN_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "*.h") +add_executable(labo01 main.cpp ${MAIN_SOURCES} ${TESTS_SOURCES}) + +# Add .cpp and .h files +#set(HEADERS DenseStorage.h MatrixBase.h Matrix.h Math3D.h Vector.h Operators.h) +#set(SOURCE main.cpp) +#add_executable(labo01 ${SOURCE} ${HEADERS}) # Add linking information for Google Test target_link_libraries(labo01 gtest) diff --git a/labo_physique/ParticleSimApplication.cpp b/labo_physique/ParticleSimApplication.cpp index 6ed1f45..16fc4ab 100644 --- a/labo_physique/ParticleSimApplication.cpp +++ b/labo_physique/ParticleSimApplication.cpp @@ -314,7 +314,7 @@ void ParticleSimApplication::initGui() m_stiffness = std::exp(value); onStiffnessSliderChanged(); }); - m_sliderStiffness->set_value(std::logf(300.f)); + m_sliderStiffness->set_value(std::log(300.f)); // Curseur du nombre maximum d'itération pour Jacobi et Gauss-Seidel Widget* panelMaxIter = new Widget(panelSimControl); diff --git a/labo_physique/ParticleSystem.cpp b/labo_physique/ParticleSystem.cpp index ab57702..c25b8b7 100644 --- a/labo_physique/ParticleSystem.cpp +++ b/labo_physique/ParticleSystem.cpp @@ -8,64 +8,93 @@ using namespace gti320; * Étant donné une particule p, la force est stockée dans le vecteur p.f * Les forces prisent en compte sont : la gravité et la force des ressorts. */ -void ParticleSystem::computeForces() -{ +void ParticleSystem::computeForces() { // TODO // // Calcul de la force gravitationnelle sur chacune des particules - for (Particle& p : m_particles) - { + for (Particle &p: m_particles) { + p.f(0) = 0; // x + p.f(1) = -p.m * 9.81f; // y } // TODO // - // Pour chaque ressort, ajouter la force exercée à chacune des exptrémités sur + // Pour chaque ressort, ajouter la force exercée à chacune des extrémités sur // les particules appropriées. Pour un ressort s, les deux particules // auxquelles le ressort est attaché sont m_particles[s.index0] et // m_particles[s.index1]. On rappelle que les deux forces sont de même // magnitude mais dans des directions opposées. - for (const Spring& s : m_springs) - { + for (const Spring &s: m_springs) { + Particle &p0 = m_particles[s.index0]; + Particle &p1 = m_particles[s.index1]; + + p0.f = p0.f + (s.k * p0.x); + p1.f = p1.f + (-s.k * p1.x); } } /** - * Assemble les données du système dans les vecteurs trois vecteurs d'état _pos, + * Assemble les données du système dans les trois vecteurs d'état _pos, * _vel et _force. */ -void ParticleSystem::pack(Vector& _pos, - Vector& _vel, - Vector& _force) -{ +void ParticleSystem::pack(Vector &_pos, + Vector &_vel, + Vector &_force) { // TODO // // Copier les données des particules dans les vecteurs. Attention, si on a // changé de modèle, il est possible que les vecteurs n'aient pas la bonne // taille. Rappel : la taille de ces vecteurs doit être 2 fois le nombre de // particules. + int required_size = (int) this->m_particles.size() * 2; + if (_pos.size() != required_size) { + // Si un des vecteurs n'est pas la bonne grandeur, un changement de modèle a eu lieu et on doit redimensionner tous les vecteurs. + _pos.resize(required_size); + _vel.resize(required_size); + _force.resize(required_size); + } + for (int i = 0; i < m_particles.size(); i++) { + Particle &p = m_particles[i]; + int ix = i * 2; + int iy = i * 2 + 1; + + _pos(ix) = p.x(0); + _pos(iy) = p.x(1); + _vel(ix) = p.v(0); + _vel(iy) = p.v(1); + _force(ix) = p.f(0); + _force(iy) = p.f(1); + } } /** * Copie les données des vecteurs d'états dans le particules du système. */ -void ParticleSystem::unpack(const Vector& _pos, - const Vector& _vel) -{ +void ParticleSystem::unpack(const Vector &_pos, + const Vector &_vel) { // TODO // // Mise à jour des propriétés de chacune des particules à partir des valeurs // contenues dans le vecteur d'état. -} + for (int i = 0; i < _pos.size(); i += 2) { + int ix = i; + int iy = i + 1; + Particle &p = m_particles[i / 2]; + p.x(0) = _pos(ix); + p.x(1) = _pos(iy); + p.v(0) = _vel(ix); + p.v(1) = _vel(iy); + } +} /** * Construction de la matirce de masses. */ -void ParticleSystem::buildMassMatrix(Matrix& M) -{ +void ParticleSystem::buildMassMatrix(Matrix &M) { const int numParticles = m_particles.size(); const int dim = 2 * numParticles; M.resize(dim, dim); @@ -75,8 +104,16 @@ void ParticleSystem::buildMassMatrix(Matrix& M) // // Inscrire la masse de chacune des particules dans la matrice de masses M // - for (int i = 0; i < numParticles; ++i) - { + for (int i = 0; i < numParticles; ++i) { + int j = i * 2; + int k = j + 1; + float m = m_particles[i].m; + if (m_particles[i].fixed) { + m = 10000000; + } + + M(j, j) = m; + M(k, k) = m; } } @@ -84,8 +121,7 @@ void ParticleSystem::buildMassMatrix(Matrix& M) /** * Construction de la matrice de rigidité. */ -void ParticleSystem::buildDfDx(Matrix& dfdx) -{ +void ParticleSystem::buildDfDx(Matrix &dfdx) { const int numParticles = m_particles.size(); const int numSprings = m_springs.size(); const int dim = 2 * numParticles; @@ -93,8 +129,7 @@ void ParticleSystem::buildDfDx(Matrix& dfdx) dfdx.setZero(); // Pour chaque ressort... - for (const Spring& spring : m_springs) - { + for (const Spring &spring: m_springs) { // TODO // // Calculer le coefficients alpha et le produit dyadique tel que décrit au cours.