This commit is contained in:
william 2024-02-27 13:20:47 -05:00
commit cd8cf3d2b2
25 changed files with 4386 additions and 0 deletions

47
CMakeLists.txt Normal file
View File

@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.15)
project(GTI320-labos)
#--------------------------------------------------
# Add google test and setup build
#--------------------------------------------------
include(FetchContent)
FetchContent_Declare(
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)
FetchContent_MakeAvailable(googletest)
#--------------------------------------------------
# Add nanogui
#--------------------------------------------------
include(FetchContent)
FetchContent_Declare(
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)
FetchContent_MakeAvailable(nanogui)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${CMAKE_SOURCE_DIR}/labo01/src ${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()

22
labo01/CMakeLists.txt Normal file
View File

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.15)
project(labo01)
# Setup language requirements
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Add .cpp and .h files
file(GLOB_RECURSE TESTS_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "tests/*.cpp")
file(GLOB_RECURSE MAIN_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "src/*.h")
add_executable(labo01 main.cpp ${MAIN_SOURCES} ${TESTS_SOURCES})
# Add linking information for Google Test
target_link_libraries(labo01 gtest)
# Set labo01 as the startup project for Visual Studio
if( MSVC )
set_property(TARGET labo01 PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/labo01)
endif()

28
labo01/main.cpp Normal file
View File

@ -0,0 +1,28 @@
/**
* @file main.cpp
*
* @brief Execute unit tests for a simple linear algebra library.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <gtest/gtest.h>
int main(int argc, char** argv)
{
// Executer tous les tests unitaires.
//
// Les tests sont écrites dans les fichiers:
// tests/Tests1a.cpp
// tests/Tests1b.cpp
// tests/TestsSupplementaire1a.cpp
// tests/TestsSupplementaire1b.cpp
//
::testing::InitGoogleTest(&argc, argv);
const int ret = RUN_ALL_TESTS();
return ret;
}

220
labo01/src/DenseStorage.h Normal file
View File

@ -0,0 +1,220 @@
#pragma once
/**
* @file DenseStorage.h
*
* @brief Stockage dense pour des données à taille fixe ou dynamique.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <cstring>
#include <cassert>
namespace gti320
{
enum SizeType { Dynamic = -1 };
/**
* Stockage à taille fixe.
*
* Le nombre de données à stocker est connu au moment de la compilation.
* Ce nombre est donné par le paramètre de patron : _Size
*
* Un tampon (tableau) de taille `_Size_` est alloué sur la pile d'exécution.
*/
template<typename _Scalar, int _Size>
class DenseStorage
{
private:
// TODO déclarer une variable m_data et allouer la mémoire pour y stocker _Size éléments
_Scalar* m_data; // <-- Ceci n'est pas bon, à modifier
public:
/**
* Constructeur par défaut
*/
DenseStorage() { }
/**
* Constructeur de copie
*/
DenseStorage(const DenseStorage& other)
{
memcpy(m_data, other.m_data, sizeof(m_data));
}
/**
* Constructeur avec taille spécifiée.
* Doit être la même que la taille spécifiée dans le patron
*
*/
explicit DenseStorage(int _size)
{
assert(_size > 0 && _size == _Size);
}
/**
* Constructeur avec taille (_size) et données initiales (_data).
*/
explicit DenseStorage(const _Scalar* _data, int _size)
{
assert(_size >= 0 && _size == _Size);
memcpy(m_data, _data, sizeof(_Scalar) * _size);
}
/**
* Opérateur de copie
*/
DenseStorage& operator=(const DenseStorage& other)
{
if (this != &other)
{
assert(other.size() == _Size);
memcpy(m_data, other.m_data, sizeof(m_data));
}
return *this;
}
static int size() { return _Size; }
/**
* Redimensionne le stockage pour qu'il contienne `size` élément.
*/
void resize(int size)
{
// Ne rien faire. Invalide pour les matrices à taille fixe.
}
/**
* Mets tous les éléments à zéro.
*/
void setZero()
{
memset(m_data, 0, sizeof(_Scalar) * _Size);
}
/**
* Accès au tampon de données (en lecteur seulement)
*/
const _Scalar* data() const
{
return m_data;
}
/**
* Accès au tampon de données (pour lecture et écriture)
*/
_Scalar* data()
{
return m_data;
}
};
/**
* Stockage à taille dynamique.
*
* Le nombre de données à stocker est déterminé à l'exécution.
* Un tampon de la taille demandée doit être alloué sur le tas via
* l'opérateur `new []` et la mémoire doit être libérée avec `delete[]`
*/
template<typename _Scalar>
class DenseStorage<_Scalar, Dynamic>
{
private:
_Scalar* m_data;
int m_size;
public:
/**
* Constructeur par défaut
*/
DenseStorage() : m_data(nullptr), m_size(0) {}
/**
* Constructeur avec taille spécifiée
*/
explicit DenseStorage(int _size) : m_data(nullptr), m_size(_size)
{
// TODO allouer un tampon pour stocker _size éléments de type _Scalar.
// TODO initialiser ce tampon à zéro.
}
/**
* Constructeur de copie
*/
DenseStorage(const DenseStorage& other)
: m_data(nullptr)
, m_size(other.m_size)
{
// TODO allouer un tampon pour stocker _size éléments de type _Scalar.
// TODO copier other.m_data dans m_data.
}
/**
* Opérateur de copie
*/
DenseStorage& operator=(const DenseStorage& other)
{
// TODO implémenter !
return *this;
}
/**
* Destructeur
*/
~DenseStorage()
{
// TODO libérer la mémoire allouée
}
/**
* Retourne la taille du tampon
*/
inline int size() const { return m_size; }
/**
* Redimensionne le tampon alloué pour le stockage.
* La mémoire qui n'est plus utilisée doit être libérée.
*
* Note : Toutes opérations de redimensionnement entraînent une réallocation de mémoire.
* Il nest pas pertinent de copier les données car le résultat serait de toute façon incohérent.
*/
void resize(int _size)
{
// TODO redimensionner la mémoire allouée
}
/**
* Met tous les éléments à zéro.
*/
void setZero()
{
// TODO implémenter !
}
/**
* Accès au tampon de données (en lecteur seulement)
*/
const _Scalar* data() const { return m_data; }
/**
* Accès au tampon de données (pour lecture et écriture)
*/
_Scalar* data() { return m_data; }
};
}

110
labo01/src/Math3D.h Normal file
View File

@ -0,0 +1,110 @@
#pragma once
/**
* @file Math3D.h
*
* @brief Fonctions pour l'intinialisation et la manipulation de matrices de
* rotation, des matrices de transformations en coordonnées homogènes et les
* vecteurs 3D.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
#include "Operators.h"
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
namespace gti320 {
// Deux types de vecteurs 3D considérés ici
typedef Vector<double, 3> Vector3d;
typedef Vector<float, 3> Vector3f;
// Dans le cadre de ce projet, nous considérons seulement deux
// cas :
//
// - les rotations
// - les translations
//
// Deux types de matrices en coordonnées homogèes :
typedef Matrix<double, 4, 4, ColumnStorage> Matrix4d;
typedef Matrix<float, 4, 4, ColumnStorage> Matrix4f;
//
// Deux types de matrices pour les rotations
typedef Matrix<double, 3, 3, ColumnStorage> Matrix3d;
typedef Matrix<float, 3, 3, ColumnStorage> Matrix3f;
/**
* Initialise et retourne la matrice identité
*/
template<>
inline void Matrix4d::setIdentity()
{
// TODO affecter la valeur 0.0 partout, sauf sur la diagonale principale où c'est 1.0.
// Note: ceci est une redéfinition d'une fonction membre!
}
/**
* Calcul de la matrice inverse, SPÉCIALISÉ pour le cas d'une matrice de
* transformation en coordonnées homogènes.
*
* TODO (vous pouvez supposer qu'il s'agit d'une matrice de transformation
* en coordonnées homogènes)
*/
template<>
inline Matrix4d Matrix4d::inverse() const
{
// TODO : implémenter
return Matrix4d(); // Pas bon, à changer
}
/**
* Calcul de la matrice inverse, SPÉCIALISÉ pour le cas d'une matrice de rotation.
*
* (vous pouvez supposer qu'il s'agit d'une matrice de rotation)
*/
template<>
inline Matrix3d Matrix3d::inverse() const
{
// TODO : implémenter
return Matrix3d();
}
/**
* Multiplication d'une matrice 4x4 avec un vecteur 3D la matrice
* représente une transformation en coordonnées homogène.
*/
template <typename _Scalar>
Vector<_Scalar, 3> operator*(const Matrix<_Scalar, 4, 4, ColumnStorage>& A, const Vector<_Scalar, 3>& v)
{
// TODO : implémenter
return Vector<_Scalar, 3>(); // pas bon, à changer
}
/**
* Initialise et retourne la matrice de rotation définie par les angles
* d'Euler XYZ exprimés en radians.
*
* La matrice doit correspondre au produit : Rz*Ry*Rx.
*/
template<typename _Scalar>
static Matrix<_Scalar, 3, 3> makeRotation(_Scalar x, _Scalar y, _Scalar z)
{
// TODO : implémenter
return Matrix<_Scalar, 3, 3>(); // pas bon, à changer
}
}

350
labo01/src/Matrix.h Normal file
View File

@ -0,0 +1,350 @@
#pragma once
/**
* @file Matrix.h
*
* @brief Implémentation de matrices simples.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "MatrixBase.h"
namespace gti320
{
enum StorageType
{
ColumnStorage = 0,
RowStorage = 1
};
// Déclaration avancée
template <typename _Scalar, int _RowsAtCompile, int _ColsAtCompile, int _StorageType> class SubMatrix;
/**
* Classe Matrix spécialisé pour le cas générique. (defaut par colonne)
*
* (le cas d'un stockage par ligne fait l'objet d'une spécialisation de patron, voir plus bas)
*/
template <typename _Scalar = double, int _RowsAtCompile = Dynamic, int _ColsAtCompile = Dynamic, int _StorageType = ColumnStorage>
class Matrix : public MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile> {
public:
/**
* Constructeur par défaut
*/
Matrix() : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>() { }
/**
* Constructeur de copie
*/
Matrix(const Matrix& other) : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>(other) { }
/**
* Constructeur avec spécification du nombre de ligne et de colonnes
*/
explicit Matrix(int _rows, int _cols) : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>(_rows, _cols) { }
/**
* Destructeur
*/
~Matrix() { }
/**
* Opérateur de copie à partir d'une sous-matrice.
*
* Exemple :
* Matrix<...> A(...);
* Matrix<...> B(...);
* B = A.block(i,j,m,n);
*/
template<typename _OtherScalar, int OtherRows, int _OtherCols, int _OtherStorage>
Matrix& operator= (const SubMatrix<_OtherScalar, OtherRows, _OtherCols, _OtherStorage>& submatrix)
{
// TODO copier les données de la sous-matrice.
// Note : si les dimensions ne correspondent pas, la matrice doit être redimensionnée.
// Vous pouvez présumer qu'il s'agit d'un stockage par colonnes.
return *this;
}
/**
* Accesseur à une entrée de la matrice (lecture seule)
*/
_Scalar operator()(int i, int j) const
{
// TODO implementer
return (double)(i + j);
}
/**
* Accesseur à une entrée de la matrice (lecture ou écriture)
*/
_Scalar& operator()(int i, int j)
{
// TODO implementer
// Indice : l'implémentation est identique à celle de la fonction précédente.
_Scalar x = (double)(i + j);
return x;
}
/**
* Crée une sous-matrice pour un block de taille (rows, cols) à partir de l'index (i,j).
*/
SubMatrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType> block(int i, int j, int rows, int cols) const
{
return SubMatrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType>(*this, i, j, rows, cols);
}
/**
* Calcule l'inverse de la matrice
*/
Matrix inverse() const
{
// Do nothing.
return *this;
}
/**
* Retourne la transposée de la matrice
*/
template<typename _OtherScalar, int _OtherRows, int _OtherCols, int _OtherStorage>
Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage> transpose() const
{
// TODO calcule et retourne la transposée de la matrice.
return Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>(); // pas bon, à changer
}
/**
* Affecte l'identité à la matrice
*/
inline void setIdentity()
{
// TODO affecter la valeur 0.0 partour, sauf sur la diagonale principale où c'est 1.0..
// Votre implémentation devrait aussi fonctionner pour des matrices qui ne sont pas carrées.
}
};
/**
* Classe Matrix spécialisée pour un stockage par lignes
*/
template <typename _Scalar, int _RowsAtCompile, int _ColsAtCompile>
class Matrix< _Scalar, _RowsAtCompile, _ColsAtCompile, RowStorage> : public MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile> {
public:
/**
* Constructeur par défaut
*/
Matrix() : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>() { }
/**
* Constructeur de copie
*/
Matrix(const Matrix& other) : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>(other) { }
/**
* Constructeur avec spécification du nombre de ligne et de colonnes
*/
explicit Matrix(int rows, int cols) : MatrixBase<_Scalar, _RowsAtCompile, _ColsAtCompile>(rows, cols) { }
/**
* Destructeur
*/
~Matrix() { }
/**
* Opérateur de copie à partir d'une sous-matrice.
*
* Exemple :
* Matrix<...> A(...);
* Matrix<...> B(...);
* B = A.block(i,j,m,n);
*/
template<typename _OtherScalar, int OtherRows, int _OtherCols, int _OtherStorage>
Matrix& operator= (const SubMatrix<_OtherScalar, OtherRows, _OtherCols, _OtherStorage>& submatrix)
{
// TODO copier les données de la sous-matrice.
// Note : si les dimensions ne correspondent pas, la matrice doit être redimensionnée.
// Vous pouvez présumer qu'il s'agit d'un stockage par lignes.
return *this;
}
/**
* Accesseur à une entrée de la matrice (lecture seule)
*/
_Scalar operator()(int i, int j) const
{
// TODO implementer
return 0.0;
}
/**
* Accesseur à une entrée de la matrice (lecture ou écriture)
*/
_Scalar& operator()(int i, int j)
{
// TODO implementer
_Scalar x = 0.0;
return x;
}
/**
* Crée une sous-matrice pour un block de taille (rows, cols) à partir de l'index (i,j).
*/
SubMatrix<_Scalar, _RowsAtCompile, _ColsAtCompile, RowStorage> block(int i, int j, int rows, int cols) const {
return SubMatrix<_Scalar, _RowsAtCompile, _ColsAtCompile, RowStorage>(*this, i, j, rows, cols);
}
/**
* Calcule l'inverse de la matrice
*/
Matrix inverse() const
{
// Do nothing.
return *this;
}
/**
* Retourne la transposée de la matrice
*/
Matrix<_Scalar, _ColsAtCompile, _RowsAtCompile, ColumnStorage> transpose() const
{
// TODO calcule et retourne la transposée de la matrice.
// Optimisez cette fonction en tenant compte du type de stockage utilisé.
return Matrix<_Scalar, _ColsAtCompile, _RowsAtCompile, ColumnStorage>();
}
/**
* Affecte l'identité à la matrice
*/
inline void setIdentity()
{
// TODO affecter la valeur 0.0 partour, sauf sur la diagonale principale où c'est 1.0..
// Votre implémentation devrait aussi fonctionner pour des matrices qui ne sont pas carrées.
}
};
/**
* Classe pour accéder à une sous-matrice.
*
* Un sous-matrice ne copie pas les données. Au lieu de cela, elle conserve une
* référence à la matrice originale.
*/
template <typename _Scalar, int _RowsAtCompile, int _ColsAtCompile, int _StorageType>
class SubMatrix
{
private:
// Référence à la matrice originale
Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType>& m_matrix;
// Constructeur par défaut (privé)
SubMatrix() {}
// (i,j) est le coin supérieur gauche de la sous-matrice
int m_i; // Décalage en ligne
int m_j; // Décalage en colonne
// la sous-matrice est de dimension : m_rows x m_cols
int m_rows; // Hauteur de la sous-matrice (nombre de lignes)
int m_cols; // Largeur de la sous-matrice (nombre de colonnes)
public:
/**
* Constructeur à partir d'une référence en lecture seule à une matrice.
*/
SubMatrix(const Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType>& _matrix, int _i, int _j, int _rows, int _cols) :
m_matrix(const_cast<Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType>&>(_matrix)),
m_i(_i), m_j(_j), m_rows(_rows), m_cols(_cols)
{
}
/**
* Constructeur à partir d'une référence en lecture et écriture à une matrice.
*/
explicit SubMatrix(Matrix<_Scalar, _RowsAtCompile, _ColsAtCompile, _StorageType>& _matrix, int _i, int _j, int _rows, int _cols) :
m_matrix(_matrix),
m_i(_i), m_j(_j), m_rows(_rows), m_cols(_cols)
{
}
/**
* Constructeur de copie
*/
SubMatrix(const SubMatrix& other) :
m_matrix(other.m_matrix),
m_i(other.m_i), m_j(other.m_j), m_rows(other.m_rows), m_cols(other.m_cols)
{
}
/**
* Destructeur
*/
~SubMatrix() { }
/**
* Opérateur de copie (à partir d'une matrice)
*
* Copies toutes les entrées de la matrice dans la sous-matrice.
*
* Note : la taille de la matrice doit correspondre à la taille de la
* sous-matrice.
*/
template<typename _OtherScalar, int _OtherRows, int _OtherCols, int _OtherStorage>
SubMatrix& operator= (const Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>& matrix)
{
// TODO Cpopie les valeurs de la matrice dans la sous-matrice.
// Note les dimensions de la matrice doivent correspondre à celle de
// la sous-matrice.
return *this;
}
/**
* Accesseur aux entrées de la sous-matrice (lecture seule)
*
* Note : il faut s'assurer que les indices respectent la taille de la
* sous-matrice
*/
_Scalar operator()(int i, int j) const
{
// TODO implémenter
return 0.0;
}
/**
* Accesseur aux entrées de la sous-matrice (lecture et écriture)
*
* Note : il faut s'assurer que les indices respectent la taille de la
* sous-matrice
*/
_Scalar& operator()(int i, int j)
{
// TODO implémenter
_Scalar x = 0.0;
return x;
}
/**
* Retourne la transposée de la sous-matrice sous la forme d'une matrice.
*/
template<typename _OtherScalar, int _OtherRows, int _OtherCols, int _OtherStorage>
Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage> transpose() const
{
// TODO implémenter
return Matrix<_OtherScalar, _OtherRows, _OtherCols, _OtherStorage>();
}
inline int rows() const { return m_rows; }
inline int cols() const { return m_cols; }
};
}

357
labo01/src/MatrixBase.h Normal file
View File

@ -0,0 +1,357 @@
#pragma once
/**
* @file MatrixBase.h
*
* @brief Classe contenant les éléments de base des matrices et des vecteurs.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "DenseStorage.h"
namespace gti320
{
/**
* Classe de base pour les matrices et vecteurs à taille fixe.
*/
template <typename _Scalar, int _Rows, int _Cols>
class MatrixBase
{
protected:
DenseStorage<_Scalar, _Rows* _Cols> m_storage;
public:
typedef _Scalar Scalar;
/**
* Constructeur par défaut
*/
MatrixBase() : m_storage() { }
/**
* Constructeur de copie
*/
MatrixBase(const MatrixBase& other) : m_storage(other.m_storage) { }
explicit MatrixBase(int _rows, int _cols) : m_storage() { }
/**
* Destructeur
*/
~MatrixBase() { }
/**
* Redimensionne la matrice
*/
void resize(int _rows, int _cols)
{
// Ne rien faire.
}
/**
* Opérateur de copie
*/
MatrixBase& operator=(const MatrixBase& other)
{
if (this != &other)
{
m_storage = other.m_storage;
}
return *this;
}
inline void setZero() { m_storage.setZero(); }
static inline int cols() { return _Cols; }
static inline int rows() { return _Rows; }
/**
* Accès à la donnée membre de stockage (en lecture seule)
*/
const DenseStorage<_Scalar, _Rows* _Cols>& storage() const
{
return m_storage;
}
/**
* Nombre d'éléments stockés dans le tampon.
*/
inline int size() const
{
return m_storage.size();
}
/**
* Accès au tampon de données ((lecture seule))
*/
const _Scalar* data() const
{
return m_storage.data();
}
};
/**
* Classe de base pour les matrices avec un nombre de lignes dynamique et un
* nombre fixe de colonnes.
*/
template <typename _Scalar, int _Cols>
class MatrixBase<_Scalar, Dynamic, _Cols>
{
protected:
DenseStorage<_Scalar, Dynamic> m_storage;
int m_rows;
public:
typedef _Scalar Scalar;
/**
* Constructeur par défaut
*/
MatrixBase() : m_storage(), m_rows(0) { }
explicit MatrixBase(int _rows, int _cols) : m_storage(_rows* _Cols), m_rows(_rows) { }
/**
* Constructeur de copie
*/
MatrixBase(const MatrixBase& other) : m_storage(other.m_storage), m_rows(other.m_rows) { }
/**
* Destructeur
*/
~MatrixBase() { }
/**
* Opérateur de copie
*/
MatrixBase& operator=(const MatrixBase& other)
{
if (this != &other)
{
m_storage = other.m_storage;
m_rows = other.m_rows;
}
return *this;
}
/**
* Redimensionne la matrice
*/
void resize(int _rows, int _cols)
{
assert(_cols == _Cols);
m_storage.resize(_rows * _Cols);
m_rows = _rows;
}
inline void setZero() { m_storage.setZero(); }
static inline int cols() { return _Cols; }
inline int rows() const { return m_rows; }
/**
* Accès à la donnée membre de stockage (en lecture seule)
*/
const DenseStorage<_Scalar, Dynamic>& storage() const
{
return m_storage;
}
/**
* Nombre d'éléments stockés dans le tampon.
*/
inline int size() const
{
return m_storage.size();
}
/**
* Accès au tampon de données ((lecture seule))
*/
const _Scalar* data() const
{
return m_storage.data();
}
};
/**
* Classe de base pour les matrices avec un nombre fixe de lignes et un
* nombre de colonnes dynamique.
*/
template <typename _Scalar, int _Rows>
class MatrixBase<_Scalar, _Rows, Dynamic>
{
protected:
DenseStorage<_Scalar, Dynamic> m_storage;
int m_cols;
public:
typedef _Scalar Scalar;
/**
* Constructeur par défaut
*/
MatrixBase() : m_storage() { }
explicit MatrixBase(int _rows, int _cols) : m_storage(_rows* _cols), m_cols(_cols) { }
/**
* Constructeur de copie
*/
MatrixBase(const MatrixBase& other) : m_storage(other.m_storage), m_cols(other.m_cols) { }
/**
* Destructeur
*/
~MatrixBase() { }
/**
* Opérateur de copie
*/
MatrixBase& operator=(const MatrixBase& other)
{
if (this != &other) {
m_storage = other.m_storage;
m_cols = other.m_cols;
}
return *this;
}
/**
* Redimensionne la matrice
*/
void resize(int _rows, int _cols)
{
assert(_rows == _Rows);
m_storage.resize(_Rows * _cols);
m_cols = _cols;
}
inline void setZero() { m_storage.setZero(); }
inline int cols() const { return m_cols; }
static inline int rows() { return _Rows; }
/**
* Accès à la donnée membre de stockage (en lecture seule)
*/
const DenseStorage<_Scalar, Dynamic>& storage() const
{
return m_storage;
}
/**
* Nombre d'éléments stockés dans le tampon.
*/
inline int size() const
{
return m_storage.size();
}
/**
* Accès au tampon de données ((lecture seule))
*/
const _Scalar* data() const
{
return m_storage.data();
}
};
/**
* Classe de base pour les matrices avec un nombre de lignes et de colonnes
* dynamiques.
*/
template <typename _Scalar>
class MatrixBase<_Scalar, Dynamic, Dynamic>
{
protected:
DenseStorage<_Scalar, Dynamic> m_storage;
int m_cols;
int m_rows;
public:
typedef _Scalar Scalar;
/**
* Constructeur par défaut
*/
MatrixBase() : m_storage(), m_rows(0), m_cols(0) { }
explicit MatrixBase(int _rows, int _cols) : m_storage(_rows* _cols), m_rows(_rows), m_cols(_cols) { }
/**
* Constructeur de copie
*/
MatrixBase(const MatrixBase& other) : m_storage(other.m_storage), m_rows(other.m_rows), m_cols(other.m_cols) { }
/**
* Destructeur
*/
~MatrixBase() { }
/**
* Opérateur de copie
*/
MatrixBase& operator=(const MatrixBase& other)
{
if (this != &other)
{
resize(other.m_rows, other.m_cols);
m_storage = other.m_storage;
}
return *this;
}
/**
* Redimensionne la matrice
*/
void resize(int _rows, int _cols)
{
m_storage.resize(_rows * _cols);
m_rows = _rows;
m_cols = _cols;
}
inline void setZero() { m_storage.setZero(); }
inline int cols() const { return m_cols; }
inline int rows() const { return m_rows; }
/**
* Accès à la donnée membre de stockage (en lecture seule)
*/
const DenseStorage<_Scalar, Dynamic>& storage() const
{
return m_storage;
}
/**
* Nombre d'éléments stockés dans le tampon.
*/
inline int size() const
{
return m_storage.size();
}
/**
* Accès au tampon de données ((lecture seule))
*/
const _Scalar* data() const
{
return m_storage.data();
}
};
}

179
labo01/src/Operators.h Normal file
View File

@ -0,0 +1,179 @@
#pragma once
/**
* @file Operators.h
*
* @brief Opérateurs arithmétiques pour les matrices et les vecteurs.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
/**
* Implémentation de divers opérateurs arithmétiques pour les matrices et les vecteurs.
*/
namespace gti320 {
/**
* Multiplication : Matrice * Matrice (générique)
*/
template <typename _Scalar, int RowsA, int ColsA, int StorageA, int RowsB, int ColsB, int StorageB>
Matrix<_Scalar, RowsA, ColsB> operator*(const Matrix<_Scalar, RowsA, ColsA, StorageA>& A, const Matrix<_Scalar, RowsB, ColsB, StorageB>& B)
{
// TODO implémenter
return Matrix<_Scalar, RowsA, ColsB>();
}
/**
* Multiplication : Matrice (colonne) * Matrice (ligne)
*
* Spécialisation de l'opérateur de multiplication pour le cas les matrices
* ont un stockage à taille dynamique et la matrice de gauche utilise un
* stockage par colonnes et celle de droite un stockage par lignes.
*/
template <typename _Scalar>
Matrix<_Scalar, Dynamic, Dynamic> operator*(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, RowStorage>& B)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic>();
}
/**
* Multiplication : Matrice (ligne) * Matrice (colonne)
*
* Spécialisation de l'opérateur de multiplication pour le cas les matrices
* ont un stockage à taille dynamique et la matrice de gauche utilise un
* stockage par lignes et celle de droite un stockage par colonnes.
*/
template <typename _Scalar>
Matrix<_Scalar, Dynamic, Dynamic> operator*(const Matrix<_Scalar, Dynamic, Dynamic, RowStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& B)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic>();
}
/**
* Addition : Matrice + Matrice (générique)
*/
template <typename _Scalar, int Rows, int Cols, int StorageA, int StorageB>
Matrix<_Scalar, Rows, Cols> operator+(const Matrix<_Scalar, Rows, Cols, StorageA>& A, const Matrix<_Scalar, Rows, Cols, StorageB>& B)
{
// TODO : implémenter
return Matrix<_Scalar, Rows, Cols>();
}
/**
* Addition : Matrice (colonne) + Matrice (colonne)
*
* Spécialisation de l'opérateur d'addition pour le cas les deux matrices
* sont stockées par colonnes.
*/
template <typename _Scalar>
Matrix<_Scalar, Dynamic, Dynamic> operator+(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& B)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic>();
}
/**
* Addition : Matrice (ligne) + Matrice (ligne)
*
* Spécialisation de l'opérateur d'addition pour le cas les deux matrices
* sont stockées par lignes.
*/
template <typename _Scalar>
Matrix<_Scalar, Dynamic, Dynamic, RowStorage> operator+(const Matrix<_Scalar, Dynamic, Dynamic, RowStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, RowStorage>& B)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic, RowStorage>();
}
/**
* Multiplication : Scalaire * Matrice (colonne)
*
* Spécialisation de l'opérateur de multiplication par un scalaire pour le
* cas d'une matrice stockée par colonnes.
*/
template <typename _Scalar, int _Rows, int _Cols>
Matrix<_Scalar, _Rows, _Cols, ColumnStorage> operator*(const _Scalar& a, const Matrix<_Scalar, _Rows, _Cols, ColumnStorage>& A)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic>();
}
/**
* Multiplication : Scalaire * Matrice (ligne)
*
* Spécialisation de l'opérateur de multiplication par un scalaire pour le
* cas d'une matrice stockée par lignes.
*/
template <typename _Scalar, int _Rows, int _Cols>
Matrix<_Scalar, _Rows, _Cols, RowStorage> operator*(const _Scalar& a, const Matrix<_Scalar, _Rows, _Cols, RowStorage>& A)
{
// TODO : implémenter
return Matrix<_Scalar, Dynamic, Dynamic, RowStorage>();
}
/**
* Multiplication : Matrice (ligne) * Vecteur
*
* Spécialisation de l'opérateur de multiplication matrice*vecteur pour le
* cas la matrice est représentée par lignes.
*/
template <typename _Scalar, int _Rows, int _Cols>
Vector<_Scalar, _Rows> operator*(const Matrix<_Scalar, _Rows, _Cols, RowStorage>& A, const Vector<_Scalar, _Cols>& v)
{
// TODO : implémenter
return Vector<_Scalar, _Rows>();
}
/**
* Multiplication : Matrice (colonne) * Vecteur
*
* Spécialisation de l'opérateur de multiplication matrice*vecteur pour le
* cas la matrice est représentée par colonnes.
*/
template <typename _Scalar, int _Rows, int _Cols>
Vector<_Scalar, _Rows> operator*(const Matrix<_Scalar, _Rows, _Cols, ColumnStorage>& A, const Vector<_Scalar, _Cols>& v)
{
// TODO : implémenter
return Vector<_Scalar, _Rows>();
}
/**
* Multiplication : Scalaire * Vecteur
*/
template <typename _Scalar, int _Rows>
Vector<_Scalar, _Rows> operator*(const _Scalar& a, const Vector<_Scalar, _Rows>& v)
{
// TODO : implémenter
return Vector<_Scalar, _Rows>();
}
/**
* Addition : Vecteur + Vecteur
*/
template <typename _Scalar, int _RowsA, int _RowsB>
Vector<_Scalar, _RowsA> operator+(const Vector<_Scalar, _RowsA>& a, const Vector<_Scalar, _RowsB>& b)
{
// TODO : implémenter
return Vector<_Scalar, _RowsA>();
}
/**
* Soustraction : Vecteur - Vecteur
*/
template <typename _Scalar, int _RowsA, int _RowsB>
Vector<_Scalar, _RowsA> operator-(const Vector<_Scalar, _RowsA>& a, const Vector<_Scalar, _RowsB>& b)
{
// TODO : implémenter
return Vector<_Scalar, _RowsA>();
}
}

104
labo01/src/Vector.h Normal file
View File

@ -0,0 +1,104 @@
#pragma once
/**
* @file Vector.h
*
* @brief Implémentation de vecteurs simples
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <cmath>
#include "MatrixBase.h"
namespace gti320 {
/**
* Classe vecteur générique.
*
* Cette classe réutilise la classe `MatrixBase` et ses spécialisations de
* templates pour les manipulation bas niveau.
*/
template <typename _Scalar = double, int _Rows = Dynamic>
class Vector : public MatrixBase<_Scalar, _Rows, 1> {
public:
/**
* Constructeur par défaut
*/
Vector() : MatrixBase<_Scalar, _Rows, 1>() { }
/**
* Contructeur à partir d'un taille (rows).
*/
explicit Vector(int rows) : MatrixBase<_Scalar, _Rows, 1>(rows, 1) { }
/**
* Constructeur de copie
*/
Vector(const Vector& other) : MatrixBase<_Scalar, _Rows, 1>(other) { }
/**
* Destructeur
*/
~Vector() { }
/**
* Opérateur de copie
*/
Vector& operator=(const Vector& other)
{
// TODO implémenter
this->m_storage = other.m_storage;
return *this;
}
/**
* Accesseur à une entrée du vecteur (lecture seule)
*/
_Scalar operator()(int i) const
{
// TODO implémenter
return (double)i;
}
/**
* Accesseur à une entrée du vecteur (lecture et écriture)
*/
_Scalar& operator()(int i)
{
// TODO implémenter
_Scalar x = (double)i;
return x;
}
/**
* Modifie le nombre de lignes du vecteur
*/
void resize(int _rows)
{
MatrixBase<_Scalar, _Rows, 1>::resize(_rows, 1);
}
/**
* Produit scalaire de *this et other.
*/
inline _Scalar dot(const Vector& other) const
{
// TODO implémenter
return 0.0;
}
/**
* Retourne la norme euclidienne du vecteur
*/
inline _Scalar norm() const
{
// TODO implémenter
return 0.0;
}
};
}

398
labo01/tests/Tests1a.cpp Normal file
View File

@ -0,0 +1,398 @@
/**
* @file Tests1a.cpp
*
* @brief Tests unitaires de la partie 1b
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
#include <gtest/gtest.h>
#include <chrono>
using namespace gti320;
TEST(TestLabo1a, DotAndNorm) {
{
Vector<double,3> u; u(0) = 81.74804972455644; u(1) = -74.09304417781078; u(2) = -91.38202093790602;
Vector<double,3> v; v(0) = 81.1462118959985; v(1) = 29.763442976156085; v(2) = 39.00787276684184;
EXPECT_NEAR(u.dot(v), 863.662223794679, 1e-4) << "(u=(81.74804972455644, -74.09304417781078, -91.38202093790602), v= (81.1462118959985, 29.763442976156085, 39.00787276684184))";
EXPECT_NEAR(u.norm(), 143.25919370148452, 1e-4) << "(u=(81.74804972455644, -74.09304417781078, -91.38202093790602))";
u = v;
EXPECT_NEAR(u.norm(), 94.82712892764025, 1e-4) << "u=(81.74804972455644, -74.09304417781078, -91.38202093790602)";
}
{
Vector<double,3> u; u(0) = 13.087936541928485; u(1) = -26.541934227528046; u(2) = -48.13859302157102;
Vector<double,3> v; v(0) = -66.28108277028942; v(1) = 11.919000480625243; v(2) = -96.21367167640467;
EXPECT_NEAR(u.dot(v), 3447.7548518990116, 1e-4) << "(u=(13.087936541928485, -26.541934227528046, -48.13859302157102), v= (-66.28108277028942, 11.919000480625243, -96.21367167640467))";
EXPECT_NEAR(u.norm(), 56.50745520336602, 1e-4) << "(u=(13.087936541928485, -26.541934227528046, -48.13859302157102))";
u = v;
EXPECT_NEAR(u.norm(), 117.44068768154457, 1e-4) << "u=(13.087936541928485, -26.541934227528046, -48.13859302157102)";
}
{
Vector<double,3> u; u(0) = -79.56972852806103; u(1) = -64.04761691711548; u(2) = 75.76398532227091;
Vector<double,3> v; v(0) = 68.1521534060019; v(1) = -38.02548546805114; v(2) = 44.66242384133133;
EXPECT_NEAR(u.dot(v), 396.3966055984092, 1e-4) << "(u=(-79.56972852806103, -64.04761691711548, 75.76398532227091), v= (68.1521534060019, -38.02548546805114, 44.66242384133133))";
EXPECT_NEAR(u.norm(), 127.17554954748255, 1e-4) << "(u=(-79.56972852806103, -64.04761691711548, 75.76398532227091))";
u = v;
EXPECT_NEAR(u.norm(), 89.91877258025103, 1e-4) << "u=(-79.56972852806103, -64.04761691711548, 75.76398532227091)";
}
{
Vector<double,3> u; u(0) = -27.829925590795995; u(1) = 97.52374799957443; u(2) = -71.29949583706774;
Vector<double,3> v; v(0) = 15.413123773966177; v(1) = -25.9678191712148; v(2) = -70.46329734352945;
EXPECT_NEAR(u.dot(v), 2062.5724349077673, 1e-4) << "(u=(-27.829925590795995, 97.52374799957443, -71.29949583706774), v= (15.413123773966177, -25.9678191712148, -70.46329734352945))";
EXPECT_NEAR(u.norm(), 123.97178827819569, 1e-4) << "(u=(-27.829925590795995, 97.52374799957443, -71.29949583706774))";
u = v;
EXPECT_NEAR(u.norm(), 76.66138721353245, 1e-4) << "u=(-27.829925590795995, 97.52374799957443, -71.29949583706774)";
}
{
Vector<double,3> u; u(0) = 46.94969707999718; u(1) = 92.7292896206543; u(2) = 61.3890905084975;
Vector<double,3> v; v(0) = 91.40236621394195; v(1) = -23.911909695536067; v(2) = -66.18706904017846;
EXPECT_NEAR(u.dot(v), -1989.1849652004407, 1e-4) << "(u=(46.94969707999718, 92.7292896206543, 61.3890905084975), v= (91.40236621394195, -23.911909695536067, -66.18706904017846))";
EXPECT_NEAR(u.norm(), 120.71294728783312, 1e-4) << "(u=(46.94969707999718, 92.7292896206543, 61.3890905084975))";
u = v;
EXPECT_NEAR(u.norm(), 115.3555377211011, 1e-4) << "u=(46.94969707999718, 92.7292896206543, 61.3890905084975)";
}
{
Vector<double,3> u; u(0) = 9.836648455487577; u(1) = 27.373779624683323; u(2) = 33.47163213808042;
Vector<double,3> v; v(0) = -17.904556153169437; v(1) = -10.39159035574184; v(2) = 36.045191146582226;
EXPECT_NEAR(u.dot(v), 745.913449426867, 1e-4) << "(u=(9.836648455487577, 27.373779624683323, 33.47163213808042), v= (-17.904556153169437, -10.39159035574184, 36.045191146582226))";
EXPECT_NEAR(u.norm(), 44.344488065198725, 1e-4) << "(u=(9.836648455487577, 27.373779624683323, 33.47163213808042))";
u = v;
EXPECT_NEAR(u.norm(), 41.566983123113395, 1e-4) << "u=(9.836648455487577, 27.373779624683323, 33.47163213808042)";
}
{
Vector<double,3> u; u(0) = 77.34524968701723; u(1) = -86.53838659054598; u(2) = 52.511893628372974;
Vector<double,3> v; v(0) = 39.390038592378005; v(1) = -53.13512176642368; v(2) = 60.23922457130902;
EXPECT_NEAR(u.dot(v), 10808.13583201148, 1e-4) << "(u=(77.34524968701723, -86.53838659054598, 52.511893628372974), v= (39.390038592378005, -53.13512176642368, 60.23922457130902))";
EXPECT_NEAR(u.norm(), 127.39183245121873, 1e-4) << "(u=(77.34524968701723, -86.53838659054598, 52.511893628372974))";
u = v;
EXPECT_NEAR(u.norm(), 89.4632912562147, 1e-4) << "u=(77.34524968701723, -86.53838659054598, 52.511893628372974)";
}
{
Vector<double,3> u; u(0) = 72.58764125601218; u(1) = 13.004884623782146; u(2) = 6.688195585248067;
Vector<double,3> v; v(0) = -41.68110389567836; v(1) = 65.83906651972734; v(2) = 64.45916748876758;
EXPECT_NEAR(u.dot(v), -1738.1880334803595, 1e-4) << "(u=(72.58764125601218, 13.004884623782146, 6.688195585248067), v= (-41.68110389567836, 65.83906651972734, 64.45916748876758))";
EXPECT_NEAR(u.norm(), 74.04609812391155, 1e-4) << "(u=(72.58764125601218, 13.004884623782146, 6.688195585248067))";
u = v;
EXPECT_NEAR(u.norm(), 101.12903329655835, 1e-4) << "u=(72.58764125601218, 13.004884623782146, 6.688195585248067)";
}
{
Vector<double,3> u; u(0) = 44.123038783178856; u(1) = 59.032307289173275; u(2) = -96.45920641599031;
Vector<double,3> v; v(0) = -23.433056105942327; v(1) = -95.82287577132844; v(2) = -87.71884659805113;
EXPECT_NEAR(u.dot(v), 1770.7072393377675, 1e-4) << "(u=(44.123038783178856, 59.032307289173275, -96.45920641599031), v= (-23.433056105942327, -95.82287577132844, -87.71884659805113))";
EXPECT_NEAR(u.norm(), 121.39206875965131, 1e-4) << "(u=(44.123038783178856, 59.032307289173275, -96.45920641599031))";
u = v;
EXPECT_NEAR(u.norm(), 132.006544110677, 1e-4) << "u=(44.123038783178856, 59.032307289173275, -96.45920641599031)";
}
{
Vector<double,3> u; u(0) = -7.594864170286073; u(1) = -92.12702535387345; u(2) = 10.61833019724412;
Vector<double,3> v; v(0) = -57.88532008253067; v(1) = -61.95047768934079; v(2) = -50.9801175870505;
EXPECT_NEAR(u.dot(v), 5605.620650217189, 1e-4) << "(u=(-7.594864170286073, -92.12702535387345, 10.61833019724412), v= (-57.88532008253067, -61.95047768934079, -50.9801175870505))";
EXPECT_NEAR(u.norm(), 93.04740565161417, 1e-4) << "(u=(-7.594864170286073, -92.12702535387345, 10.61833019724412))";
u = v;
EXPECT_NEAR(u.norm(), 98.9320188623685, 1e-4) << "u=(-7.594864170286073, -92.12702535387345, 10.61833019724412)";
}
{
Vector<double,3> u; u(0) = 28.01623276831728; u(1) = -35.47111447521442; u(2) = -80.09707278049716;
Vector<double,3> v; v(0) = 62.23810927998369; v(1) = -69.45561657190204; v(2) = -17.998932897185853;
EXPECT_NEAR(u.dot(v), 5649.007321253708, 1e-4) << "(u=(28.01623276831728, -35.47111447521442, -80.09707278049716), v= (62.23810927998369, -69.45561657190204, -17.998932897185853))";
EXPECT_NEAR(u.norm(), 91.97092110361056, 1e-4) << "(u=(28.01623276831728, -35.47111447521442, -80.09707278049716))";
u = v;
EXPECT_NEAR(u.norm(), 94.98224310663369, 1e-4) << "u=(28.01623276831728, -35.47111447521442, -80.09707278049716)";
}
{
Vector<double,3> u; u(0) = 17.056114318144637; u(1) = -47.87236586884536; u(2) = -66.233990996889;
Vector<double,3> v; v(0) = -16.5274983107699; v(1) = -61.98159750757431; v(2) = -95.73085862430288;
EXPECT_NEAR(u.dot(v), 9025.947640683207, 1e-4) << "(u=(17.056114318144637, -47.87236586884536, -66.233990996889), v= (-16.5274983107699, -61.98159750757431, -95.73085862430288))";
EXPECT_NEAR(u.norm(), 83.48422613218729, 1e-4) << "(u=(17.056114318144637, -47.87236586884536, -66.233990996889))";
u = v;
EXPECT_NEAR(u.norm(), 115.23573197124973, 1e-4) << "u=(17.056114318144637, -47.87236586884536, -66.233990996889)";
}
{
Vector<double,3> u; u(0) = 53.68514451573557; u(1) = 40.487633535881685; u(2) = -34.39102876903675;
Vector<double,3> v; v(0) = 5.723467714467617; v(1) = -11.984939376053532; v(2) = 71.57117734380691;
EXPECT_NEAR(u.dot(v), -2639.3830610897676, 1e-4) << "(u=(53.68514451573557, 40.487633535881685, -34.39102876903675), v= (5.723467714467617, -11.984939376053532, 71.57117734380691))";
EXPECT_NEAR(u.norm(), 75.52540016977055, 1e-4) << "(u=(53.68514451573557, 40.487633535881685, -34.39102876903675))";
u = v;
EXPECT_NEAR(u.norm(), 72.79306478576713, 1e-4) << "u=(53.68514451573557, 40.487633535881685, -34.39102876903675)";
}
{
Vector<double,3> u; u(0) = -19.41880304735801; u(1) = 80.15388147841654; u(2) = 18.762371448279836;
Vector<double,3> v; v(0) = 56.544323419883455; v(1) = 37.75922627098413; v(2) = -45.767718603712495;
EXPECT_NEAR(u.dot(v), 1069.8145305211428, 1e-4) << "(u=(-19.41880304735801, 80.15388147841654, 18.762371448279836), v= (56.544323419883455, 37.75922627098413, -45.767718603712495))";
EXPECT_NEAR(u.norm(), 84.57991020455952, 1e-4) << "(u=(-19.41880304735801, 80.15388147841654, 18.762371448279836))";
u = v;
EXPECT_NEAR(u.norm(), 81.96159921441485, 1e-4) << "u=(-19.41880304735801, 80.15388147841654, 18.762371448279836)";
}
{
Vector<double,3> u; u(0) = 14.53101118226931; u(1) = -87.12123268098249; u(2) = -12.733552633138217;
Vector<double,3> v; v(0) = -96.36370344463752; v(1) = -81.8797705320039; v(2) = -25.593956628649764;
EXPECT_NEAR(u.dot(v), 6059.106481886422, 1e-4) << "(u=(14.53101118226931, -87.12123268098249, -12.733552633138217), v= (-96.36370344463752, -81.8797705320039, -25.593956628649764))";
EXPECT_NEAR(u.norm(), 89.23790020217884, 1e-4) << "(u=(14.53101118226931, -87.12123268098249, -12.733552633138217))";
u = v;
EXPECT_NEAR(u.norm(), 129.01670736710366, 1e-4) << "u=(14.53101118226931, -87.12123268098249, -12.733552633138217)";
}
{
Vector<double,3> u; u(0) = -68.15128360202914; u(1) = -35.22142681790423; u(2) = -80.20820520692742;
Vector<double,3> v; v(0) = -71.59083714955358; v(1) = -22.11220916087268; v(2) = -41.412146034892785;
EXPECT_NEAR(u.dot(v), 8979.424909853704, 1e-4) << "(u=(-68.15128360202914, -35.22142681790423, -80.20820520692742), v= (-71.59083714955358, -22.11220916087268, -41.412146034892785))";
EXPECT_NEAR(u.norm(), 110.98874963801407, 1e-4) << "(u=(-68.15128360202914, -35.22142681790423, -80.20820520692742))";
u = v;
EXPECT_NEAR(u.norm(), 85.61053438078382, 1e-4) << "u=(-68.15128360202914, -35.22142681790423, -80.20820520692742)";
}
{
Vector<double,3> u; u(0) = 32.15880264107108; u(1) = -27.044272757999167; u(2) = -21.93384224298498;
Vector<double,3> v; v(0) = 94.98651607465004; v(1) = 96.25601423462368; v(2) = -4.364871517549744;
EXPECT_NEAR(u.dot(v), 547.2171237254174, 1e-4) << "(u=(32.15880264107108, -27.044272757999167, -21.93384224298498), v= (94.98651607465004, 96.25601423462368, -4.364871517549744))";
EXPECT_NEAR(u.norm(), 47.399100327501664, 1e-4) << "(u=(32.15880264107108, -27.044272757999167, -21.93384224298498))";
u = v;
EXPECT_NEAR(u.norm(), 135.3022934606082, 1e-4) << "u=(32.15880264107108, -27.044272757999167, -21.93384224298498)";
}
{
Vector<double,3> u; u(0) = -38.55884639660741; u(1) = -32.44768663392304; u(2) = -86.51014461651837;
Vector<double,3> v; v(0) = -32.975270310868936; v(1) = -48.59524328395069; v(2) = -92.16520328196806;
EXPECT_NEAR(u.dot(v), 10821.51667331416, 1e-4) << "(u=(-38.55884639660741, -32.44768663392304, -86.51014461651837), v= (-32.975270310868936, -48.59524328395069, -92.16520328196806))";
EXPECT_NEAR(u.norm(), 100.1181408382185, 1e-4) << "(u=(-38.55884639660741, -32.44768663392304, -86.51014461651837))";
u = v;
EXPECT_NEAR(u.norm(), 109.2853641523316, 1e-4) << "u=(-38.55884639660741, -32.44768663392304, -86.51014461651837)";
}
{
Vector<double,3> u; u(0) = -93.94403064987613; u(1) = -22.806019440495206; u(2) = -6.186803379242534;
Vector<double,3> v; v(0) = 99.58719003758503; v(1) = 11.426147026760503; v(2) = 89.17338366240898;
EXPECT_NEAR(u.dot(v), -10167.905155829267, 1e-4) << "(u=(-93.94403064987613, -22.806019440495206, -6.186803379242534), v= (99.58719003758503, 11.426147026760503, 89.17338366240898))";
EXPECT_NEAR(u.norm(), 96.87038739221866, 1e-4) << "(u=(-93.94403064987613, -22.806019440495206, -6.186803379242534))";
u = v;
EXPECT_NEAR(u.norm(), 134.1642933468604, 1e-4) << "u=(-93.94403064987613, -22.806019440495206, -6.186803379242534)";
}
{
Vector<double,3> u; u(0) = 49.70172009239852; u(1) = 35.23095832735339; u(2) = -95.645812626239;
Vector<double,3> v; v(0) = -26.17330847186834; v(1) = -13.328976428902024; v(2) = 43.288466758804674;
EXPECT_NEAR(u.dot(v), -5910.811645163534, 1e-4) << "(u=(49.70172009239852, 35.23095832735339, -95.645812626239), v= (-26.17330847186834, -13.328976428902024, 43.288466758804674))";
EXPECT_NEAR(u.norm(), 113.40018905513546, 1e-4) << "(u=(49.70172009239852, 35.23095832735339, -95.645812626239))";
u = v;
EXPECT_NEAR(u.norm(), 52.31247502588592, 1e-4) << "u=(49.70172009239852, 35.23095832735339, -95.645812626239)";
}
}
// Test les matrice avec redimensionnement dynamique
TEST(TestLabo1a, StaticMatrixTests)
{
// Crée une matrice ŕ taille fixe
Matrix<double, 3, 5> M;
EXPECT_EQ(M.cols(), 5);
EXPECT_EQ(M.rows(), 3);
// Redimensionne la matrice (la taille ne doit pas changer).
M.resize(100, 1000);
EXPECT_EQ(M.cols(), 5);
EXPECT_EQ(M.rows(), 3);
// Test - stockage par colonnes
Matrix<double, 100, 99, ColumnStorage> ColM;
ColM.setZero();
ColM(0, 0) = 1.0;
ColM(90, 17) = 99.0;
ColM(10, 33) = 7.2;
EXPECT_EQ(ColM(0, 0), 1.0);
EXPECT_EQ(ColM(90, 17), 99.0);
EXPECT_EQ(ColM(10, 33), 7.2);
// Test - stockage par lignes
Matrix<double, 5, 4, RowStorage> RowM;
RowM.setZero();
RowM(0, 0) = 2.1;
RowM(3, 3) = -0.2;
RowM(4, 3) = 1.2;
EXPECT_EQ(RowM.rows(), 5);
EXPECT_EQ(RowM.cols(), 4);
EXPECT_DOUBLE_EQ(RowM(0, 0), 2.1);
EXPECT_DOUBLE_EQ(RowM(3, 3), -0.2);
EXPECT_DOUBLE_EQ(RowM(4, 3), 1.2);
EXPECT_DOUBLE_EQ(RowM(3, 2), 0.0);
// Transposée
const auto RowMT = RowM.transpose();
EXPECT_EQ(RowMT.rows(), 4);
EXPECT_EQ(RowMT.cols(), 5);
EXPECT_DOUBLE_EQ(RowMT(0, 0), 2.1);
EXPECT_DOUBLE_EQ(RowMT(3, 3), -0.2);
EXPECT_DOUBLE_EQ(RowMT(3, 4), 1.2);
EXPECT_DOUBLE_EQ(RowMT(2, 3), 0.0);
}
// Test les matrice avec redimensionnement dynamique
TEST(TestLabo1a, DynamicMatrixTests)
{
// Crée une matrice ŕ taille dynamique
// (note : les valeurs par défaut du patron de la classe `Matrix` mettent le
// le nombre de ligne et de colonnes ŕ `Dynamic`)
Matrix<double> M(3, 5);
EXPECT_EQ(M.cols(), 5);
EXPECT_EQ(M.rows(), 3);
// Redimensionne la matrice
M.resize(100, 1000);
EXPECT_EQ(M.cols(), 1000);
EXPECT_EQ(M.rows(), 100);
// Test - stockage par colonnes
Matrix<double, Dynamic, Dynamic, ColumnStorage> ColM(100, 100);
ColM.setZero();
ColM(0, 0) = 1.0;
ColM(99, 99) = 99.0;
ColM(10, 33) = 5.0;
EXPECT_EQ(ColM(0, 0), 1.0);
EXPECT_EQ(ColM(10, 33), 5.0);
EXPECT_EQ(ColM(99, 99), 99.0);
// Test - stockage par lignes
Matrix<double, Dynamic, Dynamic, RowStorage> RowM(5, 4);
RowM.setZero();
RowM(0, 0) = 2.1;
RowM(3, 3) = -0.2;
RowM(4, 3) = 1.2;
EXPECT_EQ(RowM.rows(), 5);
EXPECT_EQ(RowM.cols(), 4);
EXPECT_DOUBLE_EQ(RowM(0, 0), 2.1);
EXPECT_DOUBLE_EQ(RowM(3, 3), -0.2);
EXPECT_DOUBLE_EQ(RowM(4, 3), 1.2);
EXPECT_DOUBLE_EQ(RowM(3, 2), 0.0);
// Transposée
const auto RowMT = RowM.transpose();
EXPECT_EQ(RowMT.rows(), 4);
EXPECT_EQ(RowMT.cols(), 5);
EXPECT_DOUBLE_EQ(RowMT(0, 0), 2.1);
EXPECT_DOUBLE_EQ(RowMT(3, 3), -0.2);
EXPECT_DOUBLE_EQ(RowMT(3, 4), 1.2);
EXPECT_DOUBLE_EQ(RowMT(2, 3), 0.0);
}
/**
* Test pour les vecteurs ŕ taille dynamique
*/
TEST(TestLabo1a, DynamicVectorSizeTest)
{
Vector<double> v(5);
v.setZero();
EXPECT_EQ(v.rows(), 5);
v.resize(3);
EXPECT_EQ(v.rows(), 3);
v(0) = 1.0;
v(1) = 2.0;
v(2) = 3.0;
EXPECT_DOUBLE_EQ(v.norm(), 3.7416573867739413855837487323165);
Vector<double, Dynamic> v2(3);
v2.setZero();
v2(1) = 2.0;
EXPECT_DOUBLE_EQ(v2.dot(v), 4.0);
EXPECT_DOUBLE_EQ(v2(0), 0.0);
EXPECT_DOUBLE_EQ(v2(1), 2.0);
EXPECT_DOUBLE_EQ(v2(2), 0.0);
}
TEST(TestLabo1a, Transpose) {
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 2);
m(0, 0) = 96.65562704596559; m(0, 1) = -5.374125981819006; m(1, 0) = 64.81364389648456; m(1, 1) = -69.97868420900502; m(2, 0) = -48.298014749653696; m(2, 1) = 81.5535983882881;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), 96.65562704596559); EXPECT_DOUBLE_EQ(mtr(0, 1), 64.81364389648456); EXPECT_DOUBLE_EQ(mtr(0, 2), -48.298014749653696); EXPECT_DOUBLE_EQ(mtr(1, 0), -5.374125981819006); EXPECT_DOUBLE_EQ(mtr(1, 1), -69.97868420900502); EXPECT_DOUBLE_EQ(mtr(1, 2), 81.5535983882881);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), 96.65562704596559); EXPECT_DOUBLE_EQ(mtc(0, 1), 64.81364389648456); EXPECT_DOUBLE_EQ(mtc(0, 2), -48.298014749653696); EXPECT_DOUBLE_EQ(mtc(1, 0), -5.374125981819006); EXPECT_DOUBLE_EQ(mtc(1, 1), -69.97868420900502); EXPECT_DOUBLE_EQ(mtc(1, 2), 81.5535983882881);
}
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 11);
m(0, 0) = -93.11774348806081; m(0, 1) = -98.38480859022877; m(0, 2) = -70.06261616604155; m(0, 3) = 84.87535684986423; m(0, 4) = -99.9818642210925; m(0, 5) = -25.11185567441916; m(0, 6) = 8.85590979917103; m(0, 7) = 32.717509759851936; m(0, 8) = 12.677927494307738; m(0, 9) = -76.92757015087707; m(0, 10) = -80.99502606313347; m(1, 0) = -14.108330840223957; m(1, 1) = -94.81883611699344; m(1, 2) = -45.060719301725285; m(1, 3) = -41.383939879963094; m(1, 4) = 80.36741308691853; m(1, 5) = -87.50961508738959; m(1, 6) = -32.65290161505921; m(1, 7) = 18.529868291798238; m(1, 8) = -46.169755352790595; m(1, 9) = 83.67078184672366; m(1, 10) = -30.02053885691531; m(2, 0) = 5.8022044043901815; m(2, 1) = -39.507965223996266; m(2, 2) = 38.099939917456425; m(2, 3) = -11.891042873050296; m(2, 4) = 53.89070563872062; m(2, 5) = 23.280098882479578; m(2, 6) = -5.0318917624474295; m(2, 7) = 31.50255873280196; m(2, 8) = 22.629046207699318; m(2, 9) = 3.5102931218199416; m(2, 10) = -95.03021938861154;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), -93.11774348806081); EXPECT_DOUBLE_EQ(mtr(0, 1), -14.108330840223957); EXPECT_DOUBLE_EQ(mtr(0, 2), 5.8022044043901815); EXPECT_DOUBLE_EQ(mtr(1, 0), -98.38480859022877); EXPECT_DOUBLE_EQ(mtr(1, 1), -94.81883611699344); EXPECT_DOUBLE_EQ(mtr(1, 2), -39.507965223996266); EXPECT_DOUBLE_EQ(mtr(2, 0), -70.06261616604155); EXPECT_DOUBLE_EQ(mtr(2, 1), -45.060719301725285); EXPECT_DOUBLE_EQ(mtr(2, 2), 38.099939917456425); EXPECT_DOUBLE_EQ(mtr(3, 0), 84.87535684986423); EXPECT_DOUBLE_EQ(mtr(3, 1), -41.383939879963094); EXPECT_DOUBLE_EQ(mtr(3, 2), -11.891042873050296); EXPECT_DOUBLE_EQ(mtr(4, 0), -99.9818642210925); EXPECT_DOUBLE_EQ(mtr(4, 1), 80.36741308691853); EXPECT_DOUBLE_EQ(mtr(4, 2), 53.89070563872062); EXPECT_DOUBLE_EQ(mtr(5, 0), -25.11185567441916); EXPECT_DOUBLE_EQ(mtr(5, 1), -87.50961508738959); EXPECT_DOUBLE_EQ(mtr(5, 2), 23.280098882479578); EXPECT_DOUBLE_EQ(mtr(6, 0), 8.85590979917103); EXPECT_DOUBLE_EQ(mtr(6, 1), -32.65290161505921); EXPECT_DOUBLE_EQ(mtr(6, 2), -5.0318917624474295); EXPECT_DOUBLE_EQ(mtr(7, 0), 32.717509759851936); EXPECT_DOUBLE_EQ(mtr(7, 1), 18.529868291798238); EXPECT_DOUBLE_EQ(mtr(7, 2), 31.50255873280196); EXPECT_DOUBLE_EQ(mtr(8, 0), 12.677927494307738); EXPECT_DOUBLE_EQ(mtr(8, 1), -46.169755352790595); EXPECT_DOUBLE_EQ(mtr(8, 2), 22.629046207699318); EXPECT_DOUBLE_EQ(mtr(9, 0), -76.92757015087707); EXPECT_DOUBLE_EQ(mtr(9, 1), 83.67078184672366); EXPECT_DOUBLE_EQ(mtr(9, 2), 3.5102931218199416); EXPECT_DOUBLE_EQ(mtr(10, 0), -80.99502606313347); EXPECT_DOUBLE_EQ(mtr(10, 1), -30.02053885691531); EXPECT_DOUBLE_EQ(mtr(10, 2), -95.03021938861154);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), -93.11774348806081); EXPECT_DOUBLE_EQ(mtc(0, 1), -14.108330840223957); EXPECT_DOUBLE_EQ(mtc(0, 2), 5.8022044043901815); EXPECT_DOUBLE_EQ(mtc(1, 0), -98.38480859022877); EXPECT_DOUBLE_EQ(mtc(1, 1), -94.81883611699344); EXPECT_DOUBLE_EQ(mtc(1, 2), -39.507965223996266); EXPECT_DOUBLE_EQ(mtc(2, 0), -70.06261616604155); EXPECT_DOUBLE_EQ(mtc(2, 1), -45.060719301725285); EXPECT_DOUBLE_EQ(mtc(2, 2), 38.099939917456425); EXPECT_DOUBLE_EQ(mtc(3, 0), 84.87535684986423); EXPECT_DOUBLE_EQ(mtc(3, 1), -41.383939879963094); EXPECT_DOUBLE_EQ(mtc(3, 2), -11.891042873050296); EXPECT_DOUBLE_EQ(mtc(4, 0), -99.9818642210925); EXPECT_DOUBLE_EQ(mtc(4, 1), 80.36741308691853); EXPECT_DOUBLE_EQ(mtc(4, 2), 53.89070563872062); EXPECT_DOUBLE_EQ(mtc(5, 0), -25.11185567441916); EXPECT_DOUBLE_EQ(mtc(5, 1), -87.50961508738959); EXPECT_DOUBLE_EQ(mtc(5, 2), 23.280098882479578); EXPECT_DOUBLE_EQ(mtc(6, 0), 8.85590979917103); EXPECT_DOUBLE_EQ(mtc(6, 1), -32.65290161505921); EXPECT_DOUBLE_EQ(mtc(6, 2), -5.0318917624474295); EXPECT_DOUBLE_EQ(mtc(7, 0), 32.717509759851936); EXPECT_DOUBLE_EQ(mtc(7, 1), 18.529868291798238); EXPECT_DOUBLE_EQ(mtc(7, 2), 31.50255873280196); EXPECT_DOUBLE_EQ(mtc(8, 0), 12.677927494307738); EXPECT_DOUBLE_EQ(mtc(8, 1), -46.169755352790595); EXPECT_DOUBLE_EQ(mtc(8, 2), 22.629046207699318); EXPECT_DOUBLE_EQ(mtc(9, 0), -76.92757015087707); EXPECT_DOUBLE_EQ(mtc(9, 1), 83.67078184672366); EXPECT_DOUBLE_EQ(mtc(9, 2), 3.5102931218199416); EXPECT_DOUBLE_EQ(mtc(10, 0), -80.99502606313347); EXPECT_DOUBLE_EQ(mtc(10, 1), -30.02053885691531); EXPECT_DOUBLE_EQ(mtc(10, 2), -95.03021938861154);
}
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 19);
m(0, 0) = 50.864090811708934; m(0, 1) = 86.72545584739655; m(0, 2) = -59.41096188367827; m(0, 3) = -81.01855448213378; m(0, 4) = -44.75527142333435; m(0, 5) = 6.881484559867502; m(0, 6) = 75.66386662485408; m(0, 7) = -56.002568995946575; m(0, 8) = -44.600865729883445; m(0, 9) = -18.413408652944696; m(0, 10) = -97.11569037525811; m(0, 11) = 53.19666636708692; m(0, 12) = -64.58909530248837; m(0, 13) = -13.336152508799714; m(0, 14) = -69.12350514623267; m(0, 15) = 56.52730125536351; m(0, 16) = 89.92552663330136; m(0, 17) = 94.33106257780301; m(0, 18) = 76.55814809986282; m(1, 0) = 14.747374583026797; m(1, 1) = -12.087041962746213; m(1, 2) = -63.3966872965287; m(1, 3) = -11.386504830855642; m(1, 4) = -76.29669292020178; m(1, 5) = -66.38097797841678; m(1, 6) = -20.191218640024687; m(1, 7) = -48.05245125934678; m(1, 8) = -83.87380388512868; m(1, 9) = -21.25569067138788; m(1, 10) = 4.889708648539596; m(1, 11) = -85.80217867074691; m(1, 12) = -85.26796006798745; m(1, 13) = 3.8169098134350605; m(1, 14) = 73.4920467291056; m(1, 15) = 98.24187210950205; m(1, 16) = -9.878913280704495; m(1, 17) = -63.027946780158196; m(1, 18) = 55.64310458734482; m(2, 0) = -75.20410363410058; m(2, 1) = 45.30781974344674; m(2, 2) = -43.85391331821933; m(2, 3) = 67.45134132003074; m(2, 4) = 55.35009554124747; m(2, 5) = -24.1599830304106; m(2, 6) = 89.85022708733555; m(2, 7) = 63.770995127367826; m(2, 8) = -7.388429976036122; m(2, 9) = 8.073425560498507; m(2, 10) = 76.76129649290999; m(2, 11) = -50.19697292034915; m(2, 12) = -25.310526646715076; m(2, 13) = 14.496228473119118; m(2, 14) = 46.35572803496012; m(2, 15) = -43.2905212948518; m(2, 16) = 72.28250601709692; m(2, 17) = 59.72857548888601; m(2, 18) = 39.776820197984875;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), 50.864090811708934); EXPECT_DOUBLE_EQ(mtr(0, 1), 14.747374583026797); EXPECT_DOUBLE_EQ(mtr(0, 2), -75.20410363410058); EXPECT_DOUBLE_EQ(mtr(1, 0), 86.72545584739655); EXPECT_DOUBLE_EQ(mtr(1, 1), -12.087041962746213); EXPECT_DOUBLE_EQ(mtr(1, 2), 45.30781974344674); EXPECT_DOUBLE_EQ(mtr(2, 0), -59.41096188367827); EXPECT_DOUBLE_EQ(mtr(2, 1), -63.3966872965287); EXPECT_DOUBLE_EQ(mtr(2, 2), -43.85391331821933); EXPECT_DOUBLE_EQ(mtr(3, 0), -81.01855448213378); EXPECT_DOUBLE_EQ(mtr(3, 1), -11.386504830855642); EXPECT_DOUBLE_EQ(mtr(3, 2), 67.45134132003074); EXPECT_DOUBLE_EQ(mtr(4, 0), -44.75527142333435); EXPECT_DOUBLE_EQ(mtr(4, 1), -76.29669292020178); EXPECT_DOUBLE_EQ(mtr(4, 2), 55.35009554124747); EXPECT_DOUBLE_EQ(mtr(5, 0), 6.881484559867502); EXPECT_DOUBLE_EQ(mtr(5, 1), -66.38097797841678); EXPECT_DOUBLE_EQ(mtr(5, 2), -24.1599830304106); EXPECT_DOUBLE_EQ(mtr(6, 0), 75.66386662485408); EXPECT_DOUBLE_EQ(mtr(6, 1), -20.191218640024687); EXPECT_DOUBLE_EQ(mtr(6, 2), 89.85022708733555); EXPECT_DOUBLE_EQ(mtr(7, 0), -56.002568995946575); EXPECT_DOUBLE_EQ(mtr(7, 1), -48.05245125934678); EXPECT_DOUBLE_EQ(mtr(7, 2), 63.770995127367826); EXPECT_DOUBLE_EQ(mtr(8, 0), -44.600865729883445); EXPECT_DOUBLE_EQ(mtr(8, 1), -83.87380388512868); EXPECT_DOUBLE_EQ(mtr(8, 2), -7.388429976036122); EXPECT_DOUBLE_EQ(mtr(9, 0), -18.413408652944696); EXPECT_DOUBLE_EQ(mtr(9, 1), -21.25569067138788); EXPECT_DOUBLE_EQ(mtr(9, 2), 8.073425560498507); EXPECT_DOUBLE_EQ(mtr(10, 0), -97.11569037525811); EXPECT_DOUBLE_EQ(mtr(10, 1), 4.889708648539596); EXPECT_DOUBLE_EQ(mtr(10, 2), 76.76129649290999); EXPECT_DOUBLE_EQ(mtr(11, 0), 53.19666636708692); EXPECT_DOUBLE_EQ(mtr(11, 1), -85.80217867074691); EXPECT_DOUBLE_EQ(mtr(11, 2), -50.19697292034915); EXPECT_DOUBLE_EQ(mtr(12, 0), -64.58909530248837); EXPECT_DOUBLE_EQ(mtr(12, 1), -85.26796006798745); EXPECT_DOUBLE_EQ(mtr(12, 2), -25.310526646715076); EXPECT_DOUBLE_EQ(mtr(13, 0), -13.336152508799714); EXPECT_DOUBLE_EQ(mtr(13, 1), 3.8169098134350605); EXPECT_DOUBLE_EQ(mtr(13, 2), 14.496228473119118); EXPECT_DOUBLE_EQ(mtr(14, 0), -69.12350514623267); EXPECT_DOUBLE_EQ(mtr(14, 1), 73.4920467291056); EXPECT_DOUBLE_EQ(mtr(14, 2), 46.35572803496012); EXPECT_DOUBLE_EQ(mtr(15, 0), 56.52730125536351); EXPECT_DOUBLE_EQ(mtr(15, 1), 98.24187210950205); EXPECT_DOUBLE_EQ(mtr(15, 2), -43.2905212948518); EXPECT_DOUBLE_EQ(mtr(16, 0), 89.92552663330136); EXPECT_DOUBLE_EQ(mtr(16, 1), -9.878913280704495); EXPECT_DOUBLE_EQ(mtr(16, 2), 72.28250601709692); EXPECT_DOUBLE_EQ(mtr(17, 0), 94.33106257780301); EXPECT_DOUBLE_EQ(mtr(17, 1), -63.027946780158196); EXPECT_DOUBLE_EQ(mtr(17, 2), 59.72857548888601); EXPECT_DOUBLE_EQ(mtr(18, 0), 76.55814809986282); EXPECT_DOUBLE_EQ(mtr(18, 1), 55.64310458734482); EXPECT_DOUBLE_EQ(mtr(18, 2), 39.776820197984875);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), 50.864090811708934); EXPECT_DOUBLE_EQ(mtc(0, 1), 14.747374583026797); EXPECT_DOUBLE_EQ(mtc(0, 2), -75.20410363410058); EXPECT_DOUBLE_EQ(mtc(1, 0), 86.72545584739655); EXPECT_DOUBLE_EQ(mtc(1, 1), -12.087041962746213); EXPECT_DOUBLE_EQ(mtc(1, 2), 45.30781974344674); EXPECT_DOUBLE_EQ(mtc(2, 0), -59.41096188367827); EXPECT_DOUBLE_EQ(mtc(2, 1), -63.3966872965287); EXPECT_DOUBLE_EQ(mtc(2, 2), -43.85391331821933); EXPECT_DOUBLE_EQ(mtc(3, 0), -81.01855448213378); EXPECT_DOUBLE_EQ(mtc(3, 1), -11.386504830855642); EXPECT_DOUBLE_EQ(mtc(3, 2), 67.45134132003074); EXPECT_DOUBLE_EQ(mtc(4, 0), -44.75527142333435); EXPECT_DOUBLE_EQ(mtc(4, 1), -76.29669292020178); EXPECT_DOUBLE_EQ(mtc(4, 2), 55.35009554124747); EXPECT_DOUBLE_EQ(mtc(5, 0), 6.881484559867502); EXPECT_DOUBLE_EQ(mtc(5, 1), -66.38097797841678); EXPECT_DOUBLE_EQ(mtc(5, 2), -24.1599830304106); EXPECT_DOUBLE_EQ(mtc(6, 0), 75.66386662485408); EXPECT_DOUBLE_EQ(mtc(6, 1), -20.191218640024687); EXPECT_DOUBLE_EQ(mtc(6, 2), 89.85022708733555); EXPECT_DOUBLE_EQ(mtc(7, 0), -56.002568995946575); EXPECT_DOUBLE_EQ(mtc(7, 1), -48.05245125934678); EXPECT_DOUBLE_EQ(mtc(7, 2), 63.770995127367826); EXPECT_DOUBLE_EQ(mtc(8, 0), -44.600865729883445); EXPECT_DOUBLE_EQ(mtc(8, 1), -83.87380388512868); EXPECT_DOUBLE_EQ(mtc(8, 2), -7.388429976036122); EXPECT_DOUBLE_EQ(mtc(9, 0), -18.413408652944696); EXPECT_DOUBLE_EQ(mtc(9, 1), -21.25569067138788); EXPECT_DOUBLE_EQ(mtc(9, 2), 8.073425560498507); EXPECT_DOUBLE_EQ(mtc(10, 0), -97.11569037525811); EXPECT_DOUBLE_EQ(mtc(10, 1), 4.889708648539596); EXPECT_DOUBLE_EQ(mtc(10, 2), 76.76129649290999); EXPECT_DOUBLE_EQ(mtc(11, 0), 53.19666636708692); EXPECT_DOUBLE_EQ(mtc(11, 1), -85.80217867074691); EXPECT_DOUBLE_EQ(mtc(11, 2), -50.19697292034915); EXPECT_DOUBLE_EQ(mtc(12, 0), -64.58909530248837); EXPECT_DOUBLE_EQ(mtc(12, 1), -85.26796006798745); EXPECT_DOUBLE_EQ(mtc(12, 2), -25.310526646715076); EXPECT_DOUBLE_EQ(mtc(13, 0), -13.336152508799714); EXPECT_DOUBLE_EQ(mtc(13, 1), 3.8169098134350605); EXPECT_DOUBLE_EQ(mtc(13, 2), 14.496228473119118); EXPECT_DOUBLE_EQ(mtc(14, 0), -69.12350514623267); EXPECT_DOUBLE_EQ(mtc(14, 1), 73.4920467291056); EXPECT_DOUBLE_EQ(mtc(14, 2), 46.35572803496012); EXPECT_DOUBLE_EQ(mtc(15, 0), 56.52730125536351); EXPECT_DOUBLE_EQ(mtc(15, 1), 98.24187210950205); EXPECT_DOUBLE_EQ(mtc(15, 2), -43.2905212948518); EXPECT_DOUBLE_EQ(mtc(16, 0), 89.92552663330136); EXPECT_DOUBLE_EQ(mtc(16, 1), -9.878913280704495); EXPECT_DOUBLE_EQ(mtc(16, 2), 72.28250601709692); EXPECT_DOUBLE_EQ(mtc(17, 0), 94.33106257780301); EXPECT_DOUBLE_EQ(mtc(17, 1), -63.027946780158196); EXPECT_DOUBLE_EQ(mtc(17, 2), 59.72857548888601); EXPECT_DOUBLE_EQ(mtc(18, 0), 76.55814809986282); EXPECT_DOUBLE_EQ(mtc(18, 1), 55.64310458734482); EXPECT_DOUBLE_EQ(mtc(18, 2), 39.776820197984875);
}
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 12);
m(0, 0) = -32.75375661346618; m(0, 1) = -13.6098421772042; m(0, 2) = -46.86521220477553; m(0, 3) = 59.00702831249865; m(0, 4) = -4.564740481250126; m(0, 5) = 16.232920948792312; m(0, 6) = -65.0828541930055; m(0, 7) = -57.88248367433246; m(0, 8) = -81.72734394349992; m(0, 9) = 28.080964462881383; m(0, 10) = 36.6937632974539; m(0, 11) = 41.35837714070166; m(1, 0) = -47.68004182743719; m(1, 1) = -64.19046526329765; m(1, 2) = -75.66374889412242; m(1, 3) = -82.35155489863575; m(1, 4) = -75.40948179339736; m(1, 5) = -21.839837867220055; m(1, 6) = -7.047883213897393; m(1, 7) = -28.576424866142986; m(1, 8) = 36.45450614900494; m(1, 9) = -9.122226758045997; m(1, 10) = 39.94779111587877; m(1, 11) = 9.684112064820866; m(2, 0) = 34.000255287178476; m(2, 1) = 64.99966038549962; m(2, 2) = -11.973673265362649; m(2, 3) = 80.2812674443403; m(2, 4) = 95.91223898270886; m(2, 5) = -27.634369350449873; m(2, 6) = -75.21066302899243; m(2, 7) = -20.700089950120855; m(2, 8) = 86.68513293601455; m(2, 9) = 75.43854998281424; m(2, 10) = -88.06922362937304; m(2, 11) = -92.34886576586136;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), -32.75375661346618); EXPECT_DOUBLE_EQ(mtr(0, 1), -47.68004182743719); EXPECT_DOUBLE_EQ(mtr(0, 2), 34.000255287178476); EXPECT_DOUBLE_EQ(mtr(1, 0), -13.6098421772042); EXPECT_DOUBLE_EQ(mtr(1, 1), -64.19046526329765); EXPECT_DOUBLE_EQ(mtr(1, 2), 64.99966038549962); EXPECT_DOUBLE_EQ(mtr(2, 0), -46.86521220477553); EXPECT_DOUBLE_EQ(mtr(2, 1), -75.66374889412242); EXPECT_DOUBLE_EQ(mtr(2, 2), -11.973673265362649); EXPECT_DOUBLE_EQ(mtr(3, 0), 59.00702831249865); EXPECT_DOUBLE_EQ(mtr(3, 1), -82.35155489863575); EXPECT_DOUBLE_EQ(mtr(3, 2), 80.2812674443403); EXPECT_DOUBLE_EQ(mtr(4, 0), -4.564740481250126); EXPECT_DOUBLE_EQ(mtr(4, 1), -75.40948179339736); EXPECT_DOUBLE_EQ(mtr(4, 2), 95.91223898270886); EXPECT_DOUBLE_EQ(mtr(5, 0), 16.232920948792312); EXPECT_DOUBLE_EQ(mtr(5, 1), -21.839837867220055); EXPECT_DOUBLE_EQ(mtr(5, 2), -27.634369350449873); EXPECT_DOUBLE_EQ(mtr(6, 0), -65.0828541930055); EXPECT_DOUBLE_EQ(mtr(6, 1), -7.047883213897393); EXPECT_DOUBLE_EQ(mtr(6, 2), -75.21066302899243); EXPECT_DOUBLE_EQ(mtr(7, 0), -57.88248367433246); EXPECT_DOUBLE_EQ(mtr(7, 1), -28.576424866142986); EXPECT_DOUBLE_EQ(mtr(7, 2), -20.700089950120855); EXPECT_DOUBLE_EQ(mtr(8, 0), -81.72734394349992); EXPECT_DOUBLE_EQ(mtr(8, 1), 36.45450614900494); EXPECT_DOUBLE_EQ(mtr(8, 2), 86.68513293601455); EXPECT_DOUBLE_EQ(mtr(9, 0), 28.080964462881383); EXPECT_DOUBLE_EQ(mtr(9, 1), -9.122226758045997); EXPECT_DOUBLE_EQ(mtr(9, 2), 75.43854998281424); EXPECT_DOUBLE_EQ(mtr(10, 0), 36.6937632974539); EXPECT_DOUBLE_EQ(mtr(10, 1), 39.94779111587877); EXPECT_DOUBLE_EQ(mtr(10, 2), -88.06922362937304); EXPECT_DOUBLE_EQ(mtr(11, 0), 41.35837714070166); EXPECT_DOUBLE_EQ(mtr(11, 1), 9.684112064820866); EXPECT_DOUBLE_EQ(mtr(11, 2), -92.34886576586136);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), -32.75375661346618); EXPECT_DOUBLE_EQ(mtc(0, 1), -47.68004182743719); EXPECT_DOUBLE_EQ(mtc(0, 2), 34.000255287178476); EXPECT_DOUBLE_EQ(mtc(1, 0), -13.6098421772042); EXPECT_DOUBLE_EQ(mtc(1, 1), -64.19046526329765); EXPECT_DOUBLE_EQ(mtc(1, 2), 64.99966038549962); EXPECT_DOUBLE_EQ(mtc(2, 0), -46.86521220477553); EXPECT_DOUBLE_EQ(mtc(2, 1), -75.66374889412242); EXPECT_DOUBLE_EQ(mtc(2, 2), -11.973673265362649); EXPECT_DOUBLE_EQ(mtc(3, 0), 59.00702831249865); EXPECT_DOUBLE_EQ(mtc(3, 1), -82.35155489863575); EXPECT_DOUBLE_EQ(mtc(3, 2), 80.2812674443403); EXPECT_DOUBLE_EQ(mtc(4, 0), -4.564740481250126); EXPECT_DOUBLE_EQ(mtc(4, 1), -75.40948179339736); EXPECT_DOUBLE_EQ(mtc(4, 2), 95.91223898270886); EXPECT_DOUBLE_EQ(mtc(5, 0), 16.232920948792312); EXPECT_DOUBLE_EQ(mtc(5, 1), -21.839837867220055); EXPECT_DOUBLE_EQ(mtc(5, 2), -27.634369350449873); EXPECT_DOUBLE_EQ(mtc(6, 0), -65.0828541930055); EXPECT_DOUBLE_EQ(mtc(6, 1), -7.047883213897393); EXPECT_DOUBLE_EQ(mtc(6, 2), -75.21066302899243); EXPECT_DOUBLE_EQ(mtc(7, 0), -57.88248367433246); EXPECT_DOUBLE_EQ(mtc(7, 1), -28.576424866142986); EXPECT_DOUBLE_EQ(mtc(7, 2), -20.700089950120855); EXPECT_DOUBLE_EQ(mtc(8, 0), -81.72734394349992); EXPECT_DOUBLE_EQ(mtc(8, 1), 36.45450614900494); EXPECT_DOUBLE_EQ(mtc(8, 2), 86.68513293601455); EXPECT_DOUBLE_EQ(mtc(9, 0), 28.080964462881383); EXPECT_DOUBLE_EQ(mtc(9, 1), -9.122226758045997); EXPECT_DOUBLE_EQ(mtc(9, 2), 75.43854998281424); EXPECT_DOUBLE_EQ(mtc(10, 0), 36.6937632974539); EXPECT_DOUBLE_EQ(mtc(10, 1), 39.94779111587877); EXPECT_DOUBLE_EQ(mtc(10, 2), -88.06922362937304); EXPECT_DOUBLE_EQ(mtc(11, 0), 41.35837714070166); EXPECT_DOUBLE_EQ(mtc(11, 1), 9.684112064820866); EXPECT_DOUBLE_EQ(mtc(11, 2), -92.34886576586136);
}
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 17);
m(0, 0) = 41.19222807334356; m(0, 1) = -9.647827035990119; m(0, 2) = 79.08578232010518; m(0, 3) = -81.30050511004372; m(0, 4) = 5.2008331502779725; m(0, 5) = -94.16930980955891; m(0, 6) = -13.738166181380976; m(0, 7) = 31.708028058804274; m(0, 8) = -98.07812519655093; m(0, 9) = -51.718134217817926; m(0, 10) = -81.49405897174573; m(0, 11) = 86.09136014594006; m(0, 12) = -23.14506976740256; m(0, 13) = 76.3616884219812; m(0, 14) = 60.28344358729666; m(0, 15) = -35.34123633864937; m(0, 16) = -87.33936780992902; m(1, 0) = 25.563530352835954; m(1, 1) = -43.62388501728416; m(1, 2) = 0.5379451108277209; m(1, 3) = 40.11963964000961; m(1, 4) = -63.945471933511236; m(1, 5) = 25.47334071885645; m(1, 6) = 20.998951793092573; m(1, 7) = 23.964236092014318; m(1, 8) = -53.69611952912465; m(1, 9) = -48.45849352784126; m(1, 10) = 0.03386109792266723; m(1, 11) = -43.90344163766924; m(1, 12) = -6.698621092523396; m(1, 13) = -99.37831178796779; m(1, 14) = -9.64088536276411; m(1, 15) = 3.118955974716698; m(1, 16) = -81.5481380106742; m(2, 0) = 62.07279507925867; m(2, 1) = 90.29145840318458; m(2, 2) = -25.71415288702252; m(2, 3) = 74.7395459171091; m(2, 4) = 24.394997421168824; m(2, 5) = -58.2301634354434; m(2, 6) = -4.768291835922895; m(2, 7) = 49.704487303266575; m(2, 8) = 18.65157355854312; m(2, 9) = 98.9073219636636; m(2, 10) = -45.19000115648366; m(2, 11) = -45.249286237965315; m(2, 12) = 97.09143831741096; m(2, 13) = -31.80834068260583; m(2, 14) = -78.03982967131924; m(2, 15) = -14.844621355572116; m(2, 16) = -66.64707457523824;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), 41.19222807334356); EXPECT_DOUBLE_EQ(mtr(0, 1), 25.563530352835954); EXPECT_DOUBLE_EQ(mtr(0, 2), 62.07279507925867); EXPECT_DOUBLE_EQ(mtr(1, 0), -9.647827035990119); EXPECT_DOUBLE_EQ(mtr(1, 1), -43.62388501728416); EXPECT_DOUBLE_EQ(mtr(1, 2), 90.29145840318458); EXPECT_DOUBLE_EQ(mtr(2, 0), 79.08578232010518); EXPECT_DOUBLE_EQ(mtr(2, 1), 0.5379451108277209); EXPECT_DOUBLE_EQ(mtr(2, 2), -25.71415288702252); EXPECT_DOUBLE_EQ(mtr(3, 0), -81.30050511004372); EXPECT_DOUBLE_EQ(mtr(3, 1), 40.11963964000961); EXPECT_DOUBLE_EQ(mtr(3, 2), 74.7395459171091); EXPECT_DOUBLE_EQ(mtr(4, 0), 5.2008331502779725); EXPECT_DOUBLE_EQ(mtr(4, 1), -63.945471933511236); EXPECT_DOUBLE_EQ(mtr(4, 2), 24.394997421168824); EXPECT_DOUBLE_EQ(mtr(5, 0), -94.16930980955891); EXPECT_DOUBLE_EQ(mtr(5, 1), 25.47334071885645); EXPECT_DOUBLE_EQ(mtr(5, 2), -58.2301634354434); EXPECT_DOUBLE_EQ(mtr(6, 0), -13.738166181380976); EXPECT_DOUBLE_EQ(mtr(6, 1), 20.998951793092573); EXPECT_DOUBLE_EQ(mtr(6, 2), -4.768291835922895); EXPECT_DOUBLE_EQ(mtr(7, 0), 31.708028058804274); EXPECT_DOUBLE_EQ(mtr(7, 1), 23.964236092014318); EXPECT_DOUBLE_EQ(mtr(7, 2), 49.704487303266575); EXPECT_DOUBLE_EQ(mtr(8, 0), -98.07812519655093); EXPECT_DOUBLE_EQ(mtr(8, 1), -53.69611952912465); EXPECT_DOUBLE_EQ(mtr(8, 2), 18.65157355854312); EXPECT_DOUBLE_EQ(mtr(9, 0), -51.718134217817926); EXPECT_DOUBLE_EQ(mtr(9, 1), -48.45849352784126); EXPECT_DOUBLE_EQ(mtr(9, 2), 98.9073219636636); EXPECT_DOUBLE_EQ(mtr(10, 0), -81.49405897174573); EXPECT_DOUBLE_EQ(mtr(10, 1), 0.03386109792266723); EXPECT_DOUBLE_EQ(mtr(10, 2), -45.19000115648366); EXPECT_DOUBLE_EQ(mtr(11, 0), 86.09136014594006); EXPECT_DOUBLE_EQ(mtr(11, 1), -43.90344163766924); EXPECT_DOUBLE_EQ(mtr(11, 2), -45.249286237965315); EXPECT_DOUBLE_EQ(mtr(12, 0), -23.14506976740256); EXPECT_DOUBLE_EQ(mtr(12, 1), -6.698621092523396); EXPECT_DOUBLE_EQ(mtr(12, 2), 97.09143831741096); EXPECT_DOUBLE_EQ(mtr(13, 0), 76.3616884219812); EXPECT_DOUBLE_EQ(mtr(13, 1), -99.37831178796779); EXPECT_DOUBLE_EQ(mtr(13, 2), -31.80834068260583); EXPECT_DOUBLE_EQ(mtr(14, 0), 60.28344358729666); EXPECT_DOUBLE_EQ(mtr(14, 1), -9.64088536276411); EXPECT_DOUBLE_EQ(mtr(14, 2), -78.03982967131924); EXPECT_DOUBLE_EQ(mtr(15, 0), -35.34123633864937); EXPECT_DOUBLE_EQ(mtr(15, 1), 3.118955974716698); EXPECT_DOUBLE_EQ(mtr(15, 2), -14.844621355572116); EXPECT_DOUBLE_EQ(mtr(16, 0), -87.33936780992902); EXPECT_DOUBLE_EQ(mtr(16, 1), -81.5481380106742); EXPECT_DOUBLE_EQ(mtr(16, 2), -66.64707457523824);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), 41.19222807334356); EXPECT_DOUBLE_EQ(mtc(0, 1), 25.563530352835954); EXPECT_DOUBLE_EQ(mtc(0, 2), 62.07279507925867); EXPECT_DOUBLE_EQ(mtc(1, 0), -9.647827035990119); EXPECT_DOUBLE_EQ(mtc(1, 1), -43.62388501728416); EXPECT_DOUBLE_EQ(mtc(1, 2), 90.29145840318458); EXPECT_DOUBLE_EQ(mtc(2, 0), 79.08578232010518); EXPECT_DOUBLE_EQ(mtc(2, 1), 0.5379451108277209); EXPECT_DOUBLE_EQ(mtc(2, 2), -25.71415288702252); EXPECT_DOUBLE_EQ(mtc(3, 0), -81.30050511004372); EXPECT_DOUBLE_EQ(mtc(3, 1), 40.11963964000961); EXPECT_DOUBLE_EQ(mtc(3, 2), 74.7395459171091); EXPECT_DOUBLE_EQ(mtc(4, 0), 5.2008331502779725); EXPECT_DOUBLE_EQ(mtc(4, 1), -63.945471933511236); EXPECT_DOUBLE_EQ(mtc(4, 2), 24.394997421168824); EXPECT_DOUBLE_EQ(mtc(5, 0), -94.16930980955891); EXPECT_DOUBLE_EQ(mtc(5, 1), 25.47334071885645); EXPECT_DOUBLE_EQ(mtc(5, 2), -58.2301634354434); EXPECT_DOUBLE_EQ(mtc(6, 0), -13.738166181380976); EXPECT_DOUBLE_EQ(mtc(6, 1), 20.998951793092573); EXPECT_DOUBLE_EQ(mtc(6, 2), -4.768291835922895); EXPECT_DOUBLE_EQ(mtc(7, 0), 31.708028058804274); EXPECT_DOUBLE_EQ(mtc(7, 1), 23.964236092014318); EXPECT_DOUBLE_EQ(mtc(7, 2), 49.704487303266575); EXPECT_DOUBLE_EQ(mtc(8, 0), -98.07812519655093); EXPECT_DOUBLE_EQ(mtc(8, 1), -53.69611952912465); EXPECT_DOUBLE_EQ(mtc(8, 2), 18.65157355854312); EXPECT_DOUBLE_EQ(mtc(9, 0), -51.718134217817926); EXPECT_DOUBLE_EQ(mtc(9, 1), -48.45849352784126); EXPECT_DOUBLE_EQ(mtc(9, 2), 98.9073219636636); EXPECT_DOUBLE_EQ(mtc(10, 0), -81.49405897174573); EXPECT_DOUBLE_EQ(mtc(10, 1), 0.03386109792266723); EXPECT_DOUBLE_EQ(mtc(10, 2), -45.19000115648366); EXPECT_DOUBLE_EQ(mtc(11, 0), 86.09136014594006); EXPECT_DOUBLE_EQ(mtc(11, 1), -43.90344163766924); EXPECT_DOUBLE_EQ(mtc(11, 2), -45.249286237965315); EXPECT_DOUBLE_EQ(mtc(12, 0), -23.14506976740256); EXPECT_DOUBLE_EQ(mtc(12, 1), -6.698621092523396); EXPECT_DOUBLE_EQ(mtc(12, 2), 97.09143831741096); EXPECT_DOUBLE_EQ(mtc(13, 0), 76.3616884219812); EXPECT_DOUBLE_EQ(mtc(13, 1), -99.37831178796779); EXPECT_DOUBLE_EQ(mtc(13, 2), -31.80834068260583); EXPECT_DOUBLE_EQ(mtc(14, 0), 60.28344358729666); EXPECT_DOUBLE_EQ(mtc(14, 1), -9.64088536276411); EXPECT_DOUBLE_EQ(mtc(14, 2), -78.03982967131924); EXPECT_DOUBLE_EQ(mtc(15, 0), -35.34123633864937); EXPECT_DOUBLE_EQ(mtc(15, 1), 3.118955974716698); EXPECT_DOUBLE_EQ(mtc(15, 2), -14.844621355572116); EXPECT_DOUBLE_EQ(mtc(16, 0), -87.33936780992902); EXPECT_DOUBLE_EQ(mtc(16, 1), -81.5481380106742); EXPECT_DOUBLE_EQ(mtc(16, 2), -66.64707457523824);
}
{
Matrix<double, 3, -1, ColumnStorage> m; m.resize(3, 11);
m(0, 0) = -44.385915446696366; m(0, 1) = 28.793505461441015; m(0, 2) = 71.2114986465838; m(0, 3) = 51.14773880111491; m(0, 4) = -7.958682467368476; m(0, 5) = -83.09905041318346; m(0, 6) = -22.191719125506793; m(0, 7) = -2.2844026553076873; m(0, 8) = -85.89049887253955; m(0, 9) = 7.359151344016439; m(0, 10) = -12.836173824817394; m(1, 0) = 60.864624834962456; m(1, 1) = 55.31797716120735; m(1, 2) = 54.86505134841502; m(1, 3) = 1.2088001265842365; m(1, 4) = 51.71783583087824; m(1, 5) = -97.7984372019131; m(1, 6) = -21.276052450577225; m(1, 7) = -38.9853136735308; m(1, 8) = -83.47696130822689; m(1, 9) = 3.197398523431062; m(1, 10) = -81.03476411018644; m(2, 0) = 78.17495315633076; m(2, 1) = -81.81693970564498; m(2, 2) = -30.34183030075195; m(2, 3) = -15.609525205781651; m(2, 4) = -27.0093291175411; m(2, 5) = 42.95413233280735; m(2, 6) = -72.61359152573652; m(2, 7) = 78.95917814822454; m(2, 8) = -83.12124753315223; m(2, 9) = 74.1286028999458; m(2, 10) = -87.09605709830836;
Matrix<double, -1, 3, RowStorage> mtr = m.transpose<double, -1, 3, RowStorage>();
EXPECT_DOUBLE_EQ(mtr(0, 0), -44.385915446696366); EXPECT_DOUBLE_EQ(mtr(0, 1), 60.864624834962456); EXPECT_DOUBLE_EQ(mtr(0, 2), 78.17495315633076); EXPECT_DOUBLE_EQ(mtr(1, 0), 28.793505461441015); EXPECT_DOUBLE_EQ(mtr(1, 1), 55.31797716120735); EXPECT_DOUBLE_EQ(mtr(1, 2), -81.81693970564498); EXPECT_DOUBLE_EQ(mtr(2, 0), 71.2114986465838); EXPECT_DOUBLE_EQ(mtr(2, 1), 54.86505134841502); EXPECT_DOUBLE_EQ(mtr(2, 2), -30.34183030075195); EXPECT_DOUBLE_EQ(mtr(3, 0), 51.14773880111491); EXPECT_DOUBLE_EQ(mtr(3, 1), 1.2088001265842365); EXPECT_DOUBLE_EQ(mtr(3, 2), -15.609525205781651); EXPECT_DOUBLE_EQ(mtr(4, 0), -7.958682467368476); EXPECT_DOUBLE_EQ(mtr(4, 1), 51.71783583087824); EXPECT_DOUBLE_EQ(mtr(4, 2), -27.0093291175411); EXPECT_DOUBLE_EQ(mtr(5, 0), -83.09905041318346); EXPECT_DOUBLE_EQ(mtr(5, 1), -97.7984372019131); EXPECT_DOUBLE_EQ(mtr(5, 2), 42.95413233280735); EXPECT_DOUBLE_EQ(mtr(6, 0), -22.191719125506793); EXPECT_DOUBLE_EQ(mtr(6, 1), -21.276052450577225); EXPECT_DOUBLE_EQ(mtr(6, 2), -72.61359152573652); EXPECT_DOUBLE_EQ(mtr(7, 0), -2.2844026553076873); EXPECT_DOUBLE_EQ(mtr(7, 1), -38.9853136735308); EXPECT_DOUBLE_EQ(mtr(7, 2), 78.95917814822454); EXPECT_DOUBLE_EQ(mtr(8, 0), -85.89049887253955); EXPECT_DOUBLE_EQ(mtr(8, 1), -83.47696130822689); EXPECT_DOUBLE_EQ(mtr(8, 2), -83.12124753315223); EXPECT_DOUBLE_EQ(mtr(9, 0), 7.359151344016439); EXPECT_DOUBLE_EQ(mtr(9, 1), 3.197398523431062); EXPECT_DOUBLE_EQ(mtr(9, 2), 74.1286028999458); EXPECT_DOUBLE_EQ(mtr(10, 0), -12.836173824817394); EXPECT_DOUBLE_EQ(mtr(10, 1), -81.03476411018644); EXPECT_DOUBLE_EQ(mtr(10, 2), -87.09605709830836);
Matrix<double, -1, 3, ColumnStorage> mtc = m.transpose<double, -1, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(mtc(0, 0), -44.385915446696366); EXPECT_DOUBLE_EQ(mtc(0, 1), 60.864624834962456); EXPECT_DOUBLE_EQ(mtc(0, 2), 78.17495315633076); EXPECT_DOUBLE_EQ(mtc(1, 0), 28.793505461441015); EXPECT_DOUBLE_EQ(mtc(1, 1), 55.31797716120735); EXPECT_DOUBLE_EQ(mtc(1, 2), -81.81693970564498); EXPECT_DOUBLE_EQ(mtc(2, 0), 71.2114986465838); EXPECT_DOUBLE_EQ(mtc(2, 1), 54.86505134841502); EXPECT_DOUBLE_EQ(mtc(2, 2), -30.34183030075195); EXPECT_DOUBLE_EQ(mtc(3, 0), 51.14773880111491); EXPECT_DOUBLE_EQ(mtc(3, 1), 1.2088001265842365); EXPECT_DOUBLE_EQ(mtc(3, 2), -15.609525205781651); EXPECT_DOUBLE_EQ(mtc(4, 0), -7.958682467368476); EXPECT_DOUBLE_EQ(mtc(4, 1), 51.71783583087824); EXPECT_DOUBLE_EQ(mtc(4, 2), -27.0093291175411); EXPECT_DOUBLE_EQ(mtc(5, 0), -83.09905041318346); EXPECT_DOUBLE_EQ(mtc(5, 1), -97.7984372019131); EXPECT_DOUBLE_EQ(mtc(5, 2), 42.95413233280735); EXPECT_DOUBLE_EQ(mtc(6, 0), -22.191719125506793); EXPECT_DOUBLE_EQ(mtc(6, 1), -21.276052450577225); EXPECT_DOUBLE_EQ(mtc(6, 2), -72.61359152573652); EXPECT_DOUBLE_EQ(mtc(7, 0), -2.2844026553076873); EXPECT_DOUBLE_EQ(mtc(7, 1), -38.9853136735308); EXPECT_DOUBLE_EQ(mtc(7, 2), 78.95917814822454); EXPECT_DOUBLE_EQ(mtc(8, 0), -85.89049887253955); EXPECT_DOUBLE_EQ(mtc(8, 1), -83.47696130822689); EXPECT_DOUBLE_EQ(mtc(8, 2), -83.12124753315223); EXPECT_DOUBLE_EQ(mtc(9, 0), 7.359151344016439); EXPECT_DOUBLE_EQ(mtc(9, 1), 3.197398523431062); EXPECT_DOUBLE_EQ(mtc(9, 2), 74.1286028999458); EXPECT_DOUBLE_EQ(mtc(10, 0), -12.836173824817394); EXPECT_DOUBLE_EQ(mtc(10, 1), -81.03476411018644); EXPECT_DOUBLE_EQ(mtc(10, 2), -87.09605709830836);
}
}
TEST(TestLabo1a, AffectationBySubMatrix)
{
{
Matrix<double, 4, 4, ColumnStorage> m;
m.setIdentity();
EXPECT_DOUBLE_EQ(m(0, 0), 1); EXPECT_DOUBLE_EQ(m(0, 1), 0); EXPECT_DOUBLE_EQ(m(0, 2), 0); EXPECT_DOUBLE_EQ(m(0, 3), 0); EXPECT_DOUBLE_EQ(m(1, 0), 0); EXPECT_DOUBLE_EQ(m(1, 1), 1); EXPECT_DOUBLE_EQ(m(1, 2), 0); EXPECT_DOUBLE_EQ(m(1, 3), 0); EXPECT_DOUBLE_EQ(m(2, 0), 0); EXPECT_DOUBLE_EQ(m(2, 1), 0); EXPECT_DOUBLE_EQ(m(2, 2), 1); EXPECT_DOUBLE_EQ(m(2, 3), 0); EXPECT_DOUBLE_EQ(m(3, 0), 0); EXPECT_DOUBLE_EQ(m(3, 1), 0); EXPECT_DOUBLE_EQ(m(3, 2), 0); EXPECT_DOUBLE_EQ(m(3, 3), 1);
Matrix<double, 4, 1, RowStorage> c;
c(0, 0) = 1.0; c(1, 0) = 2.0; c(2, 0) = 3.0; c(3, 0) = 4.0;
m.block(0, 0, 4, 1) = c;
m.block(0, 1, 4, 1) = c;
m.block(0, 2, 4, 1) = c;
m.block(0, 3, 4, 1) = c;
EXPECT_DOUBLE_EQ(m(0, 0), 1); EXPECT_DOUBLE_EQ(m(0, 1), 1); EXPECT_DOUBLE_EQ(m(0, 2), 1); EXPECT_DOUBLE_EQ(m(0, 3), 1); EXPECT_DOUBLE_EQ(m(1, 0), 2); EXPECT_DOUBLE_EQ(m(1, 1), 2); EXPECT_DOUBLE_EQ(m(1, 2), 2); EXPECT_DOUBLE_EQ(m(1, 3), 2); EXPECT_DOUBLE_EQ(m(2, 0), 3); EXPECT_DOUBLE_EQ(m(2, 1), 3); EXPECT_DOUBLE_EQ(m(2, 2), 3); EXPECT_DOUBLE_EQ(m(2, 3), 3); EXPECT_DOUBLE_EQ(m(3, 0), 4); EXPECT_DOUBLE_EQ(m(3, 1), 4); EXPECT_DOUBLE_EQ(m(3, 2), 4); EXPECT_DOUBLE_EQ(m(3, 3), 4);
Matrix<double, 2, 2, ColumnStorage> b;
b(0, 0) = 0.0; b(1, 0) = 1.0; b(0, 1) = 2.0; b(1, 1) = 3.0;
m.block(0, 0, 2, 2) = b;
m.block(2, 0, 2, 2) = b;
m.block(0, 2, 2, 2) = b;
m.block(2, 2, 2, 2) = b;
EXPECT_DOUBLE_EQ(m(0, 0), 0); EXPECT_DOUBLE_EQ(m(0, 1), 2); EXPECT_DOUBLE_EQ(m(0, 2), 0); EXPECT_DOUBLE_EQ(m(0, 3), 2); EXPECT_DOUBLE_EQ(m(1, 0), 1); EXPECT_DOUBLE_EQ(m(1, 1), 3); EXPECT_DOUBLE_EQ(m(1, 2), 1); EXPECT_DOUBLE_EQ(m(1, 3), 3); EXPECT_DOUBLE_EQ(m(2, 0), 0); EXPECT_DOUBLE_EQ(m(2, 1), 2); EXPECT_DOUBLE_EQ(m(2, 2), 0); EXPECT_DOUBLE_EQ(m(2, 3), 2); EXPECT_DOUBLE_EQ(m(3, 0), 1); EXPECT_DOUBLE_EQ(m(3, 1), 3); EXPECT_DOUBLE_EQ(m(3, 2), 1); EXPECT_DOUBLE_EQ(m(3, 3), 3);
}
{
Matrix<double, 4, 4, RowStorage> m;
m.setIdentity();
EXPECT_DOUBLE_EQ(m(0, 0), 1); EXPECT_DOUBLE_EQ(m(0, 1), 0); EXPECT_DOUBLE_EQ(m(0, 2), 0); EXPECT_DOUBLE_EQ(m(0, 3), 0); EXPECT_DOUBLE_EQ(m(1, 0), 0); EXPECT_DOUBLE_EQ(m(1, 1), 1); EXPECT_DOUBLE_EQ(m(1, 2), 0); EXPECT_DOUBLE_EQ(m(1, 3), 0); EXPECT_DOUBLE_EQ(m(2, 0), 0); EXPECT_DOUBLE_EQ(m(2, 1), 0); EXPECT_DOUBLE_EQ(m(2, 2), 1); EXPECT_DOUBLE_EQ(m(2, 3), 0); EXPECT_DOUBLE_EQ(m(3, 0), 0); EXPECT_DOUBLE_EQ(m(3, 1), 0); EXPECT_DOUBLE_EQ(m(3, 2), 0); EXPECT_DOUBLE_EQ(m(3, 3), 1);
Matrix<double, 4, 1, RowStorage> c;
c(0, 0) = 1.0; c(1, 0) = 2.0; c(2, 0) = 3.0; c(3, 0) = 4.0;
m.block(0, 0, 4, 1) = c;
m.block(0, 1, 4, 1) = c;
m.block(0, 2, 4, 1) = c;
m.block(0, 3, 4, 1) = c;
EXPECT_DOUBLE_EQ(m(0, 0), 1); EXPECT_DOUBLE_EQ(m(0, 1), 1); EXPECT_DOUBLE_EQ(m(0, 2), 1); EXPECT_DOUBLE_EQ(m(0, 3), 1); EXPECT_DOUBLE_EQ(m(1, 0), 2); EXPECT_DOUBLE_EQ(m(1, 1), 2); EXPECT_DOUBLE_EQ(m(1, 2), 2); EXPECT_DOUBLE_EQ(m(1, 3), 2); EXPECT_DOUBLE_EQ(m(2, 0), 3); EXPECT_DOUBLE_EQ(m(2, 1), 3); EXPECT_DOUBLE_EQ(m(2, 2), 3); EXPECT_DOUBLE_EQ(m(2, 3), 3); EXPECT_DOUBLE_EQ(m(3, 0), 4); EXPECT_DOUBLE_EQ(m(3, 1), 4); EXPECT_DOUBLE_EQ(m(3, 2), 4); EXPECT_DOUBLE_EQ(m(3, 3), 4);
Matrix<double, 2, 2, ColumnStorage> b;
b(0, 0) = 0.0; b(1, 0) = 1.0; b(0, 1) = 2.0; b(1, 1) = 3.0;
m.block(0, 0, 2, 2) = b;
m.block(2, 0, 2, 2) = b;
m.block(0, 2, 2, 2) = b;
m.block(2, 2, 2, 2) = b;
EXPECT_DOUBLE_EQ(m(0, 0), 0); EXPECT_DOUBLE_EQ(m(0, 1), 2); EXPECT_DOUBLE_EQ(m(0, 2), 0); EXPECT_DOUBLE_EQ(m(0, 3), 2); EXPECT_DOUBLE_EQ(m(1, 0), 1); EXPECT_DOUBLE_EQ(m(1, 1), 3); EXPECT_DOUBLE_EQ(m(1, 2), 1); EXPECT_DOUBLE_EQ(m(1, 3), 3); EXPECT_DOUBLE_EQ(m(2, 0), 0); EXPECT_DOUBLE_EQ(m(2, 1), 2); EXPECT_DOUBLE_EQ(m(2, 2), 0); EXPECT_DOUBLE_EQ(m(2, 3), 2); EXPECT_DOUBLE_EQ(m(3, 0), 1); EXPECT_DOUBLE_EQ(m(3, 1), 3); EXPECT_DOUBLE_EQ(m(3, 2), 1); EXPECT_DOUBLE_EQ(m(3, 3), 3);
}
}

801
labo01/tests/Tests1b.cpp Normal file
View File

@ -0,0 +1,801 @@
/**
* @file Tests1b.cpp
*
* @brief Tests unitaires de la partie 1b
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
#include "Math3D.h"
#include "Operators.h"
#include <gtest/gtest.h>
#include <chrono>
using namespace gti320;
namespace
{
/**
* Multiplication matrice * vecteur, utilisant une implémentation naive
*/
template<typename _Scalar>
static inline Vector<_Scalar, Dynamic> naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Vector<_Scalar, Dynamic>& v)
{
assert(A.cols() == v.rows());
Vector<_Scalar, Dynamic> b(A.rows());
assert(b.rows() == A.rows());
for (int i = 0; i < A.rows(); ++i) {
b(i) = 0.0;
for (int j = 0; j < A.cols(); ++j) {
b(i) += A(i, j) * v(j);
}
}
return b;
}
/**
* Addition matrice + matrice, utilisant une implémentation naive
*/
template<typename _Scalar>
static inline Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> naiveMatrixAddition(const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& A, const Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage>& B)
{
assert(A.cols() == B.cols() && A.rows() == B.rows());
Matrix<_Scalar, Dynamic, Dynamic, ColumnStorage> C(A.rows(), A.cols());
assert(C.rows() == A.rows() && C.cols() == A.cols());
for (int i = 0; i < C.rows(); ++i) {
for (int j = 0; j < C.cols(); ++j) {
C(i, j) = A(i, j) + B(i, j);
}
}
return C;
}
/**
* Multiplication matrice * matrice, utilisant une implémentation naive.
*/
template<typename _Scalar, int _Storage>
static inline Matrix<_Scalar, Dynamic, Dynamic, _Storage> naiveMatrixMult(const Matrix<_Scalar, Dynamic, Dynamic, _Storage>& A, const Matrix<_Scalar, Dynamic, Dynamic, _Storage>& B)
{
assert(A.cols() == B.rows());
Matrix<_Scalar, Dynamic, Dynamic> product(A.rows(), B.cols());
for (int i = 0; i < A.rows(); ++i)
{
for (int j = 0; j < B.cols(); ++j)
{
for (int k = 0; k < A.cols(); ++k)
{
product(i, j) += A(i, k) * B(k, j);
}
}
}
return product;
}
}
/**
* Test pour les opérateurs d'arithmétique matricielle.
*/
TEST(TestLabo1b, MatrixMatrixOperators)
{
// Opérations arithmétiques avec matrices à taille dynamique
{
// Test : matrice identité
Matrix<double> A(6, 6);
A.setIdentity();
EXPECT_DOUBLE_EQ(A(0, 0), 1.0);
EXPECT_DOUBLE_EQ(A(1, 1), 1.0);
EXPECT_DOUBLE_EQ(A(2, 2), 1.0);
EXPECT_DOUBLE_EQ(A(3, 3), 1.0);
EXPECT_DOUBLE_EQ(A(4, 4), 1.0);
EXPECT_DOUBLE_EQ(A(5, 5), 1.0);
EXPECT_DOUBLE_EQ(A(0, 1), 0.0);
EXPECT_DOUBLE_EQ(A(1, 0), 0.0);
// Test : produit scalaire * matrice
const double alpha = 2.5;
Matrix<double> B = alpha * A;
EXPECT_DOUBLE_EQ(B(0, 0), alpha);
EXPECT_DOUBLE_EQ(B(1, 1), alpha);
EXPECT_DOUBLE_EQ(B(2, 2), alpha);
EXPECT_DOUBLE_EQ(B(3, 3), alpha);
EXPECT_DOUBLE_EQ(B(4, 4), alpha);
EXPECT_DOUBLE_EQ(B(5, 5), alpha);
EXPECT_DOUBLE_EQ(B(0, 1), 0.0);
EXPECT_DOUBLE_EQ(B(1, 0), 0.0);
// Test : produit matrice * matrice
Matrix<double> C = A * B;
EXPECT_DOUBLE_EQ(C(0, 0), A(0, 0) * B(0, 0));
EXPECT_DOUBLE_EQ(C(1, 1), A(1, 1) * B(1, 1));
EXPECT_DOUBLE_EQ(C(2, 2), A(2, 2) * B(2, 2));
EXPECT_DOUBLE_EQ(C(3, 3), A(3, 3) * B(3, 3));
EXPECT_DOUBLE_EQ(C(4, 4), A(4, 4) * B(4, 4));
EXPECT_DOUBLE_EQ(C(5, 5), A(5, 5) * B(5, 5));
EXPECT_DOUBLE_EQ(C(0, 1), 0.0);
EXPECT_DOUBLE_EQ(C(2, 3), 0.0);
// Test : addition matrice * matrice
Matrix<double> A_plus_B = A + B;
EXPECT_DOUBLE_EQ(A_plus_B(0, 0), A(0, 0) + B(0, 0));
EXPECT_DOUBLE_EQ(A_plus_B(1, 1), A(1, 1) + B(1, 1));
EXPECT_DOUBLE_EQ(A_plus_B(2, 2), A(2, 2) + B(2, 2));
EXPECT_DOUBLE_EQ(A_plus_B(3, 3), A(3, 3) + B(3, 3));
EXPECT_DOUBLE_EQ(A_plus_B(4, 4), A(4, 4) + B(4, 4));
EXPECT_DOUBLE_EQ(A_plus_B(5, 5), A(5, 5) + B(5, 5));
EXPECT_DOUBLE_EQ(A_plus_B(0, 1), 0.0);
EXPECT_DOUBLE_EQ(A_plus_B(2, 3), 0.0);
}
// Opérations arithmétique avec matrices à stockage par lignes et par
// colonnes.
{
// Création d'un matrice à stockage par lignes
Matrix<double, Dynamic, Dynamic, RowStorage> A(5, 5);
A(0, 0) = 0.8147; A(0, 1) = 0.0975; A(0, 2) = 0.1576; A(0, 3) = 0.1419; A(0, 4) = 0.6557;
A(1, 0) = 0.9058; A(1, 1) = 0.2785; A(1, 2) = 0.9706; A(1, 3) = 0.4218; A(1, 4) = 0.0357;
A(2, 0) = 0.1270; A(2, 1) = 0.5469; A(2, 2) = 0.9572; A(2, 3) = 0.9157; A(2, 4) = 0.8491;
A(3, 0) = 0.9134; A(3, 1) = 0.9575; A(3, 2) = 0.4854; A(3, 3) = 0.7922; A(3, 4) = 0.9340;
A(4, 0) = 0.6324; A(4, 1) = 0.9649; A(4, 2) = 0.8003; A(4, 3) = 0.9595; A(4, 4) = 0.6787;
// Test : transposée (le résultat est une matrice à stockage par
// colonnes)
Matrix<double, Dynamic, Dynamic, ColumnStorage> B = A.transpose();
// Test : multiplication matrix(ligne) * matrice(colonne)
// Note : teste seulement la première et la dernière colonne
const auto C = A * B;
EXPECT_NEAR(C(0, 0), 1.14815820000000, 1e-3); EXPECT_NEAR(C(0, 4), 1.31659795000000, 1e-3);
EXPECT_NEAR(C(1, 0), 1.00133748000000, 1e-3); EXPECT_NEAR(C(1, 4), 2.04727044000000, 1e-3);
EXPECT_NEAR(C(2, 0), 0.99433707000000, 1e-3); EXPECT_NEAR(C(2, 4), 2.82896409000000, 1e-3);
EXPECT_NEAR(C(3, 0), 1.63883925000000, 1e-3); EXPECT_NEAR(C(3, 4), 3.28401323000000, 1e-3);
EXPECT_NEAR(C(4, 0), 1.31659795000000, 1e-3); EXPECT_NEAR(C(4, 4), 3.35271580000000, 1e-3);
// Test : multiplication matrice(colonne) * matrice(ligne)
// Note : teste seulement la première et la dernière colonne
const auto C2 = B * A;
EXPECT_NEAR(C2(0, 0), 2.73456805000000, 1e-3); EXPECT_NEAR(C2(0, 4), 1.95669703000000, 1e-3);
EXPECT_NEAR(C2(1, 0), 1.88593811000000, 1e-3); EXPECT_NEAR(C2(1, 4), 2.08742862000000, 1e-3);
EXPECT_NEAR(C2(2, 0), 2.07860468000000, 1e-3); EXPECT_NEAR(C2(2, 4), 1.94727447000000, 1e-3);
EXPECT_NEAR(C2(3, 0), 1.94434955000000, 1e-3); EXPECT_NEAR(C2(3, 4), 2.27675041000000, 1e-3);
EXPECT_NEAR(C2(4, 0), 1.95669703000000, 1e-3); EXPECT_NEAR(C2(4, 4), 2.48517748000000, 1e-3);
// Test : addition matrice(ligne) + matrice(ligne)
// Note : teste seulement la première et la dernière colonne
const auto A_plus_A = A + A;
EXPECT_DOUBLE_EQ(A_plus_A(0, 0), A(0, 0) + A(0, 0)); EXPECT_DOUBLE_EQ(A_plus_A(0, 4), A(0, 4) + A(0, 4));
EXPECT_DOUBLE_EQ(A_plus_A(1, 0), A(1, 0) + A(1, 0)); EXPECT_DOUBLE_EQ(A_plus_A(1, 4), A(1, 4) + A(1, 4));
EXPECT_DOUBLE_EQ(A_plus_A(2, 0), A(2, 0) + A(2, 0)); EXPECT_DOUBLE_EQ(A_plus_A(2, 4), A(2, 4) + A(2, 4));
EXPECT_DOUBLE_EQ(A_plus_A(3, 0), A(3, 0) + A(3, 0)); EXPECT_DOUBLE_EQ(A_plus_A(3, 4), A(3, 4) + A(3, 4));
EXPECT_DOUBLE_EQ(A_plus_A(4, 0), A(4, 0) + A(4, 0)); EXPECT_DOUBLE_EQ(A_plus_A(4, 4), A(4, 4) + A(4, 4));
// Test : addition matrice(colonne) + matrice(colonne)
// Note : teste seulement la première et la dernière colonne
const auto B_plus_B = B + B;
EXPECT_DOUBLE_EQ(B_plus_B(0, 0), B(0, 0) + B(0, 0)); EXPECT_DOUBLE_EQ(B_plus_B(0, 4), B(0, 4) + B(0, 4));
EXPECT_DOUBLE_EQ(B_plus_B(1, 0), B(1, 0) + B(1, 0)); EXPECT_DOUBLE_EQ(B_plus_B(1, 4), B(1, 4) + B(1, 4));
EXPECT_DOUBLE_EQ(B_plus_B(2, 0), B(2, 0) + B(2, 0)); EXPECT_DOUBLE_EQ(B_plus_B(2, 4), B(2, 4) + B(2, 4));
EXPECT_DOUBLE_EQ(B_plus_B(3, 0), B(3, 0) + B(3, 0)); EXPECT_DOUBLE_EQ(B_plus_B(3, 4), B(3, 4) + B(3, 4));
EXPECT_DOUBLE_EQ(B_plus_B(4, 0), B(4, 0) + B(4, 0)); EXPECT_DOUBLE_EQ(B_plus_B(4, 4), B(4, 4) + B(4, 4));
}
}
/**
* Test pour la multiplication matrice * vecteur
*/
TEST(TestLabo1b, MatrixVectorOperators)
{
// Vecteur à taille dynamique
Vector<double> v(5);
v(0) = 1.0;
v(1) = 2.0;
v(2) = 4.0;
v(3) = 8.0;
v(4) = 16.0;
// Test : multiplication par la matrice identité
{
Matrix<double> M(5, 5);
M.setIdentity();
const auto b = M * v;
EXPECT_DOUBLE_EQ(b(0), 1.0);
EXPECT_DOUBLE_EQ(b(1), 2.0);
EXPECT_DOUBLE_EQ(b(2), 4.0);
EXPECT_DOUBLE_EQ(b(3), 8.0);
EXPECT_DOUBLE_EQ(b(4), 16.0);
}
// Test : multiplication par une matrice à taille dynamique avec stockage par ligne.
{
Matrix<double, Dynamic, Dynamic, RowStorage> M(5, 5);
M.setIdentity();
M = 2.0 * M;
Vector<double> b2 = M * v;
EXPECT_DOUBLE_EQ(b2(0), 2.0);
EXPECT_DOUBLE_EQ(b2(1), 4.0);
EXPECT_DOUBLE_EQ(b2(2), 8.0);
EXPECT_DOUBLE_EQ(b2(3), 16.0);
EXPECT_DOUBLE_EQ(b2(4), 32.0);
}
// Test : autres
{
Vector<double, 3> u; u(0) = 89.63885274026418; u(1) = 33.98267508829284; u(2) = 58.18085749457168;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -37.761291293282206; m33(0, 1) = -58.77833883088219; m33(0, 2) = -80.31292383738193; m33(1, 0) = 82.42200051847567; m33(1, 1) = -21.616077216647483; m33(1, 2) = -81.41765352024458; m33(2, 0) = -1.826095889555674; m33(2, 1) = -72.65845670305742; m33(2, 2) = -68.43559055076148;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = -0.6810804556978075; m44(0, 1) = -0.3717461369121619; m44(0, 2) = -0.6308202775413543; m44(0, 3) = 65.25663257716863; m44(1, 0) = -0.7322085856273928; m44(1, 1) = 0.3457881173505905; m44(1, 2) = 0.5867718168356992; m44(1, 3) = -72.82746334890318; m44(2, 0) = 0.0; m44(2, 1) = 0.8615308396047228; m44(2, 2) = -0.5077052416609282; m44(2, 3) = -50.951590508183095; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), -10054.998796995307, 1e-3);
EXPECT_NEAR(v(1), 1916.692541293076, 1e-3);
EXPECT_NEAR(v(2), -6614.459208480986, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -45.129230953732865, 1e-3);
EXPECT_NEAR(w(1), -92.57210823264782, 1e-3);
EXPECT_NEAR(w(2), -51.21319422167322, 1e-3);
}
{
Vector<double, 3> u; u(0) = 21.26102327359945; u(1) = -63.632003812151375; u(2) = 21.749676556807557;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -72.46587508929531; m33(0, 1) = 26.74413795088728; m33(0, 2) = 66.40155978725781; m33(1, 0) = 30.554539344667063; m33(1, 1) = 34.707428553000256; m33(1, 2) = -78.73225643210714; m33(2, 0) = 29.88002386285075; m33(2, 1) = 84.40743250651678; m33(2, 2) = 89.33116036637102;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = -0.8156253236747055; m44(0, 1) = 0.0; m44(0, 2) = 0.5785804450381398; m44(0, 3) = -36.44550865077372; m44(1, 0) = -0.4886547783002762; m44(1, 1) = 0.5354367269787551; m44(1, 2) = -0.6888570381774334; m44(1, 3) = 26.24571159164249; m44(2, 0) = -0.3097932197851331; m44(2, 1) = -0.8445753438280554; m44(2, 2) = -0.43671575374937205; m44(2, 3) = 58.09911341828064; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), -1798.269296618443, 1e-3);
EXPECT_NEAR(v(1), -3271.2835658593685, 1e-3);
EXPECT_NEAR(v(2), -2792.8103398519333, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -41.2026000982854, 1e-3);
EXPECT_NEAR(w(1), -33.19691864907287, 1e-3);
EXPECT_NEAR(w(2), 95.75618766923024, 1e-3);
}
{
Vector<double, 3> u; u(0) = 60.89920397428068; u(1) = 41.99263628824008; u(2) = 2.8397811977043403;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = 37.756340946034896; m33(0, 1) = 34.695096214807364; m33(0, 2) = 71.82563147437446; m33(1, 0) = 74.91874228380777; m33(1, 1) = 11.371524438829937; m33(1, 2) = 22.662878030996694; m33(2, 0) = -33.183135422386044; m33(2, 1) = -48.81237469926953; m33(2, 2) = 77.28631280104915;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.09071108841607319; m44(0, 1) = 0.15875989354807826; m44(0, 2) = -0.9831412892555038; m44(0, 3) = -20.28034322391565; m44(1, 0) = 0.8682639927927074; m44(1, 1) = -0.49610244790735053; m44(1, 2) = 0.0; m44(1, 3) = -65.91591665493024; m44(2, 0) = -0.487738800238444; m44(2, 1) = -0.8536261812883538; m44(2, 2) = -0.18284749208295362; m44(2, 3) = -28.08938117342305; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), 3960.2387427031354, 1e-3);
EXPECT_NEAR(v(1), 5104.369672559086, 1e-3);
EXPECT_NEAR(v(2), -3851.1106117734685, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -10.881269828726621, 1e-3);
EXPECT_NEAR(w(1), -33.87198031100272, 1e-3);
EXPECT_NEAR(w(2), -94.15754648234068, 1e-3);
}
{
Vector<double, 3> u; u(0) = 5.188467455547638; u(1) = 18.80451508110363; u(2) = -21.656727889135595;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = 87.9457818703861; m33(0, 1) = 73.05874068692381; m33(0, 2) = 39.940251688233246; m33(1, 0) = 75.10237717805055; m33(1, 1) = 72.18489393198146; m33(1, 2) = 99.55423077694917; m33(2, 0) = -23.406607601688265; m33(2, 1) = 36.642344803461384; m33(2, 2) = 59.655570064408465;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.17067228277067772; m44(0, 1) = -0.6737686724718157; m44(0, 2) = -0.7189622715340582; m44(0, 3) = -18.335201879756795; m44(1, 0) = -0.9693828421385298; m44(1, 1) = -0.24555428191629253; m44(1, 2) = 0.0; m44(1, 3) = 29.845708309352148; m44(2, 0) = -0.1765442643114522; m44(2, 1) = 0.6969496901700587; m44(2, 2) = -0.6950491005034012; m44(2, 3) = -31.894796135192152; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), 965.1628555052321, 1e-3);
EXPECT_NEAR(v(1), -408.9507197542059, 1e-3);
EXPECT_NEAR(v(2), -724.347344278602, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -14.549197180540624, 1e-3);
EXPECT_NEAR(w(1), 20.198567783425617, 1e-3);
EXPECT_NEAR(w(2), -4.652500106271733, 1e-3);
}
{
Vector<double, 3> u; u(0) = 37.47349664672282; u(1) = -60.74514266373965; u(2) = -40.46471876369226;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -18.595918421354753; m33(0, 1) = -63.34971612297557; m33(0, 2) = 51.78315639657973; m33(1, 0) = 60.68830258069576; m33(1, 1) = -63.44130757420927; m33(1, 2) = 0.1227343978225548; m33(2, 0) = 69.14823593166864; m33(2, 1) = 49.45675635266332; m33(2, 2) = 81.76280170074256;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = -0.250151688477416; m44(0, 1) = -0.2581347286114716; m44(0, 2) = -0.9331616122819132; m44(0, 3) = 15.902624286413442; m44(1, 0) = -0.7181238873483027; m44(1, 1) = 0.6959152839389017; m44(1, 2) = 0.0; m44(1, 3) = -12.358184768214258; m44(2, 0) = 0.6494014283720508; m44(2, 1) = 0.6701256445360971; m44(2, 2) = -0.3594570980846258; m44(2, 3) = -95.12313555065694; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), 1055.9425967082998, 1e-3);
EXPECT_NEAR(v(1), 6122.987769732011, 1e-3);
EXPECT_NEAR(v(2), -3721.5403091682488, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), 59.969118945113756, 1e-3);
EXPECT_NEAR(w(1), -81.54227105743792, 1e-3);
EXPECT_NEAR(w(2), -96.94934080054827, 1e-3);
}
{
Vector<double, 3> u; u(0) = -18.242981648343232; u(1) = 56.21809486615007; u(2) = 68.42388784251386;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = 72.32617531563085; m33(0, 1) = 16.921578626504015; m33(0, 2) = -27.34080821434577; m33(1, 0) = 75.31926937290174; m33(1, 1) = -81.4337114340538; m33(1, 2) = 49.516021610363254; m33(2, 0) = 40.651084247784695; m33(2, 1) = -19.615350275581747; m33(2, 2) = 96.61096468197644;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.8628144975844739; m44(0, 1) = -0.39604187777629973; m44(0, 2) = 0.3141687027784186; m44(0, 3) = -15.592609294116613; m44(1, 0) = -0.5055206650158348; m44(1, 1) = -0.6759578736217757; m44(1, 2) = 0.5362180622943203; m44(1, 3) = 10.857285613068584; m44(2, 0) = 0.0; m44(2, 1) = -0.6214754895699024; m44(2, 2) = -0.7834336065448367; m44(2, 3) = -95.09026591014809; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), -2238.9105712501655, 1e-3);
EXPECT_NEAR(v(1), -2564.0174545638056, 1e-3);
EXPECT_NEAR(v(2), 4766.163205213616, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -32.1009941127323, 1e-3);
EXPECT_NEAR(w(1), 18.768550516577342, 1e-3);
EXPECT_NEAR(w(2), -183.634007166056, 1e-3);
}
{
Vector<double, 3> u; u(0) = -59.27939726364973; u(1) = 96.57872437189005; u(2) = -50.71931054476342;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -53.51188145034156; m33(0, 1) = -39.773533094835024; m33(0, 2) = -81.91533947789623; m33(1, 0) = -48.98656664587613; m33(1, 1) = -30.35481126192778; m33(1, 2) = -24.41812181189411; m33(2, 0) = 50.987332445586844; m33(2, 1) = -15.614790715203753; m33(2, 2) = 1.953005394977339;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = -0.9781360701985449; m44(0, 1) = 0.0; m44(0, 2) = -0.20796593032645286; m44(0, 3) = 31.079492180635583; m44(1, 0) = 0.17988007252338098; m44(1, 1) = 0.5018585333987298; m44(1, 2) = -0.846038516832336; m44(1, 3) = 89.95140758767585; m44(2, 0) = 0.10436947679053606; m44(2, 1) = -0.8649497167205016; m44(2, 2) = -0.4908859336542387; m44(2, 3) = -66.61272043333199; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), 3485.5645301169448, 1e-3);
EXPECT_NEAR(v(1), 1210.7354976539962, 1e-3);
EXPECT_NEAR(v(2), -4629.6099911875845, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), 99.6106974667983, 1e-3);
EXPECT_NEAR(w(1), 170.66757254759543, 1e-3);
EXPECT_NEAR(w(2), -131.4380242858257, 1e-3);
}
{
Vector<double, 3> u; u(0) = -33.64679408120854; u(1) = 57.74585710730173; u(2) = -38.78100535372986;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -48.76215910463826; m33(0, 1) = -17.88064937607517; m33(0, 2) = -10.666843689761578; m33(1, 0) = 54.129177432133815; m33(1, 1) = -50.3409670888066; m33(1, 2) = 67.92880818886752; m33(2, 0) = -36.14536761608753; m33(2, 1) = 92.53432169416001; m33(2, 2) = 68.69365938680332;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.4751126538005149; m44(0, 1) = 0.5241078534879362; m44(0, 2) = -0.7068089728568109; m44(0, 3) = 7.21266115268952; m44(1, 0) = -0.7408885299063788; m44(1, 1) = 0.671628011813954; m44(1, 2) = 0.0; m44(1, 3) = -99.71424185587028; m44(2, 0) = 0.4747127051720829; m44(2, 1) = 0.5236666608245202; m44(2, 2) = 0.7074044641427562; m44(2, 3) = -11.057694172067542; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), 1021.8278247323343, 1e-3);
EXPECT_NEAR(v(1), -7362.603053042772, 1e-3);
EXPECT_NEAR(v(2), 3895.640286793221, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), 48.90246330164035, 1e-3);
EXPECT_NEAR(w(1), -36.001982853511294, 1e-3);
EXPECT_NEAR(w(2), -24.22453095406812, 1e-3);
}
{
Vector<double, 3> u; u(0) = 15.282052878141911; u(1) = -2.259998336973041; u(2) = 91.08586195638563;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -88.39582865245377; m33(0, 1) = -64.58071224128338; m33(0, 2) = -4.675696828923634; m33(1, 0) = -86.1584474938788; m33(1, 1) = -34.543435043656444; m33(1, 2) = -62.495745585883625; m33(2, 0) = -59.183456388092395; m33(2, 1) = -57.14307430989349; m33(2, 2) = 44.41751022537625;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.44386855289227883; m44(0, 1) = 0.5950916475208161; m44(0, 2) = 0.6699601770286612; m44(0, 3) = -72.72024311218823; m44(1, 0) = -0.8960919080949868; m44(1, 1) = 0.29477162558569525; m44(1, 2) = 0.33185686823727545; m44(1, 3) = 15.476209050615353; m44(2, 0) = 0.0; m44(2, 1) = -0.7476467212531113; m44(2, 2) = 0.6640966648007446; m44(2, 3) = 99.8824543736142; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), -1630.8073013173835, 1e-3);
EXPECT_NEAR(v(1), -6931.088700045262, 1e-3);
EXPECT_NEAR(v(2), 3270.5057477108207, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -6.258026348579378, 1e-3);
EXPECT_NEAR(w(1), 31.343370633361097, 1e-3);
EXPECT_NEAR(w(2), 162.0619518560263, 1e-3);
}
{
Vector<double, 3> u; u(0) = -45.53190350588168; u(1) = 98.18869916536156; u(2) = 82.67567746788419;
Matrix<double, 3, 3, ColumnStorage> m33;
m33(0, 0) = -51.297389589304785; m33(0, 1) = -66.59459838930371; m33(0, 2) = 27.56805306675247; m33(1, 0) = 96.48690448918092; m33(1, 1) = 9.10079699500524; m33(1, 2) = 83.88487605323621; m33(2, 0) = 41.570057963368356; m33(2, 1) = 24.52264689542079; m33(2, 2) = -76.44940699537474;
Matrix<double, 4, 4, ColumnStorage> m44;
m44(0, 0) = 0.774965095070908; m44(0, 1) = 0.0; m44(0, 2) = 0.6320040359220331; m44(0, 3) = -94.78257020280037; m44(1, 0) = -0.5539089538751407; m44(1, 1) = 0.4815245168438749; m44(1, 2) = 0.6792046896887713; m44(1, 3) = 63.87247725975902; m44(2, 0) = -0.3043254380407359; m44(2, 1) = -0.8764326212997054; m44(2, 2) = 0.3731646929748866; m44(2, 3) = -95.85244653326883; m44(3, 0) = 0.0; m44(3, 1) = 0.0; m44(3, 2) = 0.0; m44(3, 3) = 1.0;
Vector<double, 3> v = m33 * u;
EXPECT_NEAR(v(0), -1923.9617306372265, 1e-3);
EXPECT_NEAR(v(1), 3435.6019505357876, 1e-3);
EXPECT_NEAR(v(2), -5805.423584529748, 1e-3);
Vector<double, 3> w = m44 * u;
EXPECT_NEAR(w(0), -77.81684429970429, 1e-3);
EXPECT_NEAR(w(1), 192.52698008315832, 1e-3);
EXPECT_NEAR(w(2), -137.20006524672516, 1e-3);
}
}
/**
* Opérateurs d'arithmétique vectorielle
*/
TEST(TestLabo1b, VectorOperators)
{
Vector<double> v(5);
v(0) = 0.1;
v(1) = 0.2;
v(2) = 0.4;
v(3) = 0.8;
v(4) = 1.6;
// Test : multiplication scalaire * vecteur
const double alpha = 4.0;
const auto v2 = alpha * v;
EXPECT_DOUBLE_EQ(v2(0), alpha * v(0));
EXPECT_DOUBLE_EQ(v2(1), alpha * v(1));
EXPECT_DOUBLE_EQ(v2(2), alpha * v(2));
EXPECT_DOUBLE_EQ(v2(3), alpha * v(3));
EXPECT_DOUBLE_EQ(v2(4), alpha * v(4));
// Test : addition vecteur + vecteur
const auto v3 = v + v2;
EXPECT_DOUBLE_EQ(v3(0), v(0) + v2(0));
EXPECT_DOUBLE_EQ(v3(1), v(1) + v2(1));
EXPECT_DOUBLE_EQ(v3(2), v(2) + v2(2));
EXPECT_DOUBLE_EQ(v3(3), v(3) + v2(3));
EXPECT_DOUBLE_EQ(v3(4), v(4) + v2(4));
}
/**
* Mathématiques 3D
*/
TEST(TestLabo1b, Math3D)
{
// Test : norme d'un vecteur de dimension 3
Vector3d v;
v.setZero();
v(1) = 2.0;
EXPECT_EQ(v.rows(), 3);
EXPECT_EQ(v.cols(), 1);
EXPECT_DOUBLE_EQ(v(0), 0.0);
EXPECT_DOUBLE_EQ(v(1), 2.0);
EXPECT_DOUBLE_EQ(v(2), 0.0);
EXPECT_DOUBLE_EQ(v.norm(), 2.0);
// Test : calcul de la norme d'un deuxième vecteur 3D
Vector3d v2;
v2(0) = 4.0;
v2(1) = 2.0;
v2(2) = 5.0;
EXPECT_EQ(v2.rows(), 3);
EXPECT_EQ(v2.cols(), 1);
EXPECT_DOUBLE_EQ(v2(0), 4.0);
EXPECT_DOUBLE_EQ(v2(1), 2.0);
EXPECT_DOUBLE_EQ(v2(2), 5.0);
EXPECT_DOUBLE_EQ(v2.norm(), 6.7082039324993690892275210061938);
// Test : produit scalaire
EXPECT_DOUBLE_EQ(v.dot(v2), 4.0);
// Test : matrice identité 4x4
Matrix4d M;
M.setIdentity();
EXPECT_DOUBLE_EQ(M(0, 0), 1.0);
EXPECT_DOUBLE_EQ(M(0, 1), 0.0);
EXPECT_DOUBLE_EQ(M(0, 2), 0.0);
EXPECT_DOUBLE_EQ(M(1, 1), 1.0);
EXPECT_DOUBLE_EQ(M(1, 0), 0.0);
EXPECT_DOUBLE_EQ(M(1, 2), 0.0);
EXPECT_DOUBLE_EQ(M(2, 0), 0.0);
EXPECT_DOUBLE_EQ(M(2, 1), 0.0);
EXPECT_DOUBLE_EQ(M(2, 2), 1.0);
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des x
const auto Rx = makeRotation<double>(M_PI / 4.0, 0, 0);
EXPECT_NEAR(Rx(0, 0), 1, 1e-3); EXPECT_NEAR(Rx(0, 1), 0, 1e-3); EXPECT_NEAR(Rx(0, 2), 0, 1e-3);
EXPECT_NEAR(Rx(1, 0), 0, 1e-3); EXPECT_NEAR(Rx(1, 1), 0.7071, 1e-3); EXPECT_NEAR(Rx(1, 2), -0.7071, 1e-3);
EXPECT_NEAR(Rx(2, 0), 0, 1e-3); EXPECT_NEAR(Rx(2, 1), 0.7071, 1e-3); EXPECT_NEAR(Rx(2, 2), 0.7071, 1e-3);
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des y
const auto Ry = makeRotation<double>(0, M_PI / 4.0, 0);
EXPECT_NEAR(Ry(0, 0), 0.7071, 1e-3); EXPECT_NEAR(Ry(0, 1), 0, 1e-3); EXPECT_NEAR(Ry(0, 2), 0.7071, 1e-3);
EXPECT_NEAR(Ry(1, 0), 0, 1e-3); EXPECT_NEAR(Ry(1, 1), 1, 1e-3); EXPECT_NEAR(Ry(1, 2), 0, 1e-3);
EXPECT_NEAR(Ry(2, 0), -0.7071, 1e-3); EXPECT_NEAR(Ry(2, 1), 0, 1e-3); EXPECT_NEAR(Ry(2, 2), 0.7071, 1e-3);
// Test : création d'une matrice de rotation de 45 degrés autour de l'axe des z
const auto Rz = makeRotation<double>(0, 0, M_PI / 4.0);
EXPECT_NEAR(Rz(0, 0), 0.7071, 1e-3); EXPECT_NEAR(Rz(0, 1), -0.7071, 1e-3); EXPECT_NEAR(Rz(0, 2), 0, 1e-3);
EXPECT_NEAR(Rz(1, 0), 0.7071, 1e-3); EXPECT_NEAR(Rz(1, 1), 0.7071, 1e-3); EXPECT_NEAR(Rz(1, 2), 0, 1e-3);
EXPECT_NEAR(Rz(2, 0), 0, 1e-3); EXPECT_NEAR(Rz(2, 1), 0, 1e-3); EXPECT_NEAR(Rz(2, 2), 1, 1e-3);
// Test : création d'une matrice de rotation quelconque.
const auto Rxyz = makeRotation<double>(M_PI / 3.0, -M_PI / 6.0, M_PI / 4.0);
EXPECT_NEAR(Rxyz(0, 0), 0.6124, 1e-3); EXPECT_NEAR(Rxyz(0, 1), -0.6597, 1e-3); EXPECT_NEAR(Rxyz(0, 2), 0.4356, 1e-3);
EXPECT_NEAR(Rxyz(1, 0), 0.6124, 1e-3); EXPECT_NEAR(Rxyz(1, 1), 0.0474, 1e-3); EXPECT_NEAR(Rxyz(1, 2), -0.7891, 1e-3);
EXPECT_NEAR(Rxyz(2, 0), 0.5, 1e-3); EXPECT_NEAR(Rxyz(2, 1), 0.75, 1e-3); EXPECT_NEAR(Rxyz(2, 2), 0.4330, 1e-3);
// Test : création d'une transformation homogène via la sous-matrice 3x3 en
// utilisant la fonction `block`
M.block(0, 0, 3, 3) = Rxyz;
M(0, 3) = -0.1;
M(1, 3) = 1.0;
M(2, 3) = 2.1;
// Test : calcule l'inverse de la matrice M et vérifie que M^(-1) * M * v = v
const Matrix4d Minv = M.inverse();
const Vector3d v3 = Minv * (M * v2);
EXPECT_DOUBLE_EQ(v3(0), v2(0));
EXPECT_DOUBLE_EQ(v3(1), v2(1));
EXPECT_DOUBLE_EQ(v3(2), v2(2));
// Test : translation d'un vecteur 3D effectuée avec une matrice 4x4 en coordonnées homogènes
Matrix4d T;
T.setIdentity();
T(0, 3) = 1.2;
T(1, 3) = 2.5;
T(2, 3) = -4.0;
const Vector3d t = T * v3;
EXPECT_DOUBLE_EQ(t(0), v3(0) + 1.2);
EXPECT_DOUBLE_EQ(t(1), v3(1) + 2.5);
EXPECT_DOUBLE_EQ(t(2), v3(2) - 4.0);
// Test : inverse d'un matrice de rotation
const Matrix3d Rinv = Rxyz.inverse();
const Matrix3d RT = Rxyz.transpose<double, 3, 3, ColumnStorage>();
EXPECT_DOUBLE_EQ(Rinv(0, 0), RT(0, 0));
EXPECT_DOUBLE_EQ(Rinv(1, 1), RT(1, 1));
EXPECT_DOUBLE_EQ(Rinv(0, 2), RT(0, 2));
}
TEST(TestLabo1b, RigidTransformInverse) {
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.7899179842354741; m(0, 1) = 0.5558875173441905; m(0, 2) = 0.2588795979645317; m(0, 3) = 63.53646575655944; m(1, 0) = -0.6132125065435026; m(1, 1) = -0.7160740240561843; m(1, 2) = -0.3334792555626483; m(1, 3) = 46.40415080808421; m(2, 0) = 0.0; m(2, 1) = -0.4221694684991984; m(2, 2) = 0.9065169275127214; m(2, 3) = 37.91755407005138; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.7899179842354742, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.6132125065435026, 1e-3);
EXPECT_NEAR(mi(0, 2), 5.0321798256853854e-17, 1e-3);
EXPECT_NEAR(mi(0, 3), 78.64420258691568, 1e-3);
EXPECT_NEAR(mi(1, 0), 0.5558875173441905, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.7160740240561843, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.42216946849919834, 1e-3);
EXPECT_NEAR(mi(1, 3), 13.917312440360101, 1e-3);
EXPECT_NEAR(mi(2, 0), 0.25887959796453164, 1e-3);
EXPECT_NEAR(mi(2, 1), -0.33347925556264824, 1e-3);
EXPECT_NEAR(mi(2, 2), 0.9065169275127212, 1e-3);
EXPECT_NEAR(mi(2, 3), -35.34637765902901, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = 0.6589112893907124; m(0, 1) = -0.32720889915223983; m(0, 2) = 0.6773258071482646; m(0, 3) = -47.579657162583366; m(1, 0) = -0.7522206542720485; m(1, 1) = -0.2866202043457096; m(1, 2) = 0.5933067888937011; m(1, 3) = 30.029355381737275; m(2, 0) = 0.0; m(2, 1) = -0.9004350030826229; m(2, 2) = -0.4349905806147956; m(2, 3) = 80.66547089822612; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), 0.6589112893907124, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.7522206542720486, 1e-3);
EXPECT_NEAR(mi(0, 2), 5.551115123125783e-17, 1e-3);
EXPECT_NEAR(mi(0, 3), 53.939474602384124, 1e-3);
EXPECT_NEAR(mi(1, 0), -0.3272088991522398, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.28662020434570956, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.9004350030826228, 1e-3);
EXPECT_NEAR(mi(1, 3), 65.67254627057903, 1e-3);
EXPECT_NEAR(mi(2, 0), 0.6773258071482645, 1e-3);
EXPECT_NEAR(mi(2, 1), 0.5933067888937011, 1e-3);
EXPECT_NEAR(mi(2, 2), -0.43499058061479545, 1e-3);
EXPECT_NEAR(mi(2, 3), 49.49902929898342, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.12921677297502387; m(0, 1) = 0.0; m(0, 2) = -0.9916163701663668; m(0, 3) = -85.67624239250289; m(1, 0) = 0.5536164401240612; m(1, 1) = -0.8296411663169171; m(1, 2) = -0.07214133611645579; m(1, 3) = -80.61895745285052; m(2, 0) = -0.8226857618837724; m(2, 1) = -0.5582969954713223; m(2, 2) = 0.1072035542387071; m(2, 3) = -59.16846832515195; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.12921677297502387, 1e-3);
EXPECT_NEAR(mi(0, 1), 0.5536164401240611, 1e-3);
EXPECT_NEAR(mi(0, 2), -0.8226857618837723, 1e-3);
EXPECT_NEAR(mi(0, 3), -15.115883774598393, 1e-3);
EXPECT_NEAR(mi(1, 0), -0.0, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.829641166316917, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.5582969954713222, 1e-3);
EXPECT_NEAR(mi(1, 3), -99.91838398100924, 1e-3);
EXPECT_NEAR(mi(2, 0), -0.991616370166367, 1e-3);
EXPECT_NEAR(mi(2, 1), -0.07214133611645579, 1e-3);
EXPECT_NEAR(mi(2, 2), 0.10720355423870709, 1e-3);
EXPECT_NEAR(mi(2, 3), -84.43085369439521, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = 0.6178488588834932; m(0, 1) = 0.656563779114608; m(0, 2) = 0.4326508887441579; m(0, 3) = -3.573389719025613; m(1, 0) = -0.7862968825935693; m(1, 1) = 0.5159084191865928; m(1, 2) = 0.33996428553015007; m(1, 3) = 9.98340634305292; m(2, 0) = 0.0; m(2, 1) = -0.5502385909468138; m(2, 2) = 0.8350074808244923; m(2, 3) = 57.03623931292819; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), 0.6178488588834931, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.7862968825935694, 1e-3);
EXPECT_NEAR(mi(0, 2), -0.0, 1e-3);
EXPECT_NEAR(mi(0, 3), 10.057736045453359, 1e-3);
EXPECT_NEAR(mi(1, 0), 0.656563779114608, 1e-3);
EXPECT_NEAR(mi(1, 1), 0.5159084191865929, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.5502385909468138, 1e-3);
EXPECT_NEAR(mi(1, 3), 28.57917482608178, 1e-3);
EXPECT_NEAR(mi(2, 0), 0.4326508887441579, 1e-3);
EXPECT_NEAR(mi(2, 1), 0.33996428553015007, 1e-3);
EXPECT_NEAR(mi(2, 2), 0.8350074808244924, 1e-3);
EXPECT_NEAR(mi(2, 3), -49.473657871198526, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.30532791727234754; m(0, 1) = 0.42993321248800975; m(0, 2) = -0.8496659906892062; m(0, 3) = -4.900019523419871; m(1, 0) = -0.9522472698486095; m(1, 1) = -0.13785349298616878; m(1, 2) = 0.272436325656807; m(1, 3) = 61.254180348498465; m(2, 0) = 0.0; m(2, 1) = 0.8922745358191345; m(2, 2) = 0.45149324771113447; m(2, 3) = 41.98722428204593; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.3053279172723474, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.9522472698486094, 1e-3);
EXPECT_NEAR(mi(0, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(0, 3), 56.83301324799237, 1e-3);
EXPECT_NEAR(mi(1, 0), 0.42993321248800975, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.13785349298616878, 1e-3);
EXPECT_NEAR(mi(1, 2), 0.8922745358191346, 1e-3);
EXPECT_NEAR(mi(1, 3), -26.91334720059331, 1e-3);
EXPECT_NEAR(mi(2, 0), -0.8496659906892063, 1e-3);
EXPECT_NEAR(mi(2, 1), 0.27243632565680703, 1e-3);
EXPECT_NEAR(mi(2, 2), 0.4514932477111345, 1e-3);
EXPECT_NEAR(mi(2, 3), -39.80819202150404, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.8426665329724763; m(0, 1) = -0.13329457405486883; m(0, 2) = 0.5216758291656588; m(0, 3) = 54.75958681320196; m(1, 0) = -0.5384358032376251; m(1, 1) = 0.20860959822407732; m(1, 2) = -0.81643672217793; m(1, 3) = 91.96965943681118; m(2, 0) = 0.0; m(2, 1) = -0.9688728461755547; m(2, 2) = -0.24755889792871488; m(2, 3) = 99.00199341303772; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.8426665329724762, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.5384358032376252, 1e-3);
EXPECT_NEAR(mi(0, 2), -2.7755575615628914e-17, 1e-3);
EXPECT_NEAR(mi(0, 3), 95.66382861923651, 1e-3);
EXPECT_NEAR(mi(1, 0), -0.13329457405486886, 1e-3);
EXPECT_NEAR(mi(1, 1), 0.20860959822407737, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.9688728461755547, 1e-3);
EXPECT_NEAR(mi(1, 3), 84.03374523091135, 1e-3);
EXPECT_NEAR(mi(2, 0), 0.5216758291656588, 1e-3);
EXPECT_NEAR(mi(2, 1), -0.8164367221779302, 1e-3);
EXPECT_NEAR(mi(2, 2), -0.2475588979287149, 1e-3);
EXPECT_NEAR(mi(2, 3), 71.02947881694217, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.7135830808790772; m(0, 1) = 0.0; m(0, 2) = -0.7005706150582712; m(0, 3) = 46.98894352455778; m(1, 0) = 0.5815768734392242; m(1, 1) = 0.5575439866421585; m(1, 2) = -0.5923791380862643; m(1, 3) = -32.31315706340996; m(2, 0) = 0.39059893364393755; m(2, 1) = -0.8301473983330724; m(2, 2) = -0.39785395571371457; m(2, 3) = 7.702713412699751; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.7135830808790772, 1e-3);
EXPECT_NEAR(mi(0, 1), 0.581576873439224, 1e-3);
EXPECT_NEAR(mi(0, 2), 0.39059893364393744, 1e-3);
EXPECT_NEAR(mi(0, 3), 49.314428298230084, 1e-3);
EXPECT_NEAR(mi(1, 0), -5.551115123125783e-17, 1e-3);
EXPECT_NEAR(mi(1, 1), 0.5575439866421584, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.8301473983330723, 1e-3);
EXPECT_NEAR(mi(1, 3), 24.41039390978577, 1e-3);
EXPECT_NEAR(mi(2, 0), -0.7005706150582711, 1e-3);
EXPECT_NEAR(mi(2, 1), -0.5923791380862641, 1e-3);
EXPECT_NEAR(mi(2, 2), -0.3978539557137144, 1e-3);
EXPECT_NEAR(mi(2, 3), 16.841987936840617, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = 0.655199358191179; m(0, 1) = -0.7015590749923188; m(0, 2) = 0.2802296653136303; m(0, 3) = -24.378695814492147; m(1, 0) = -0.7554560219005917; m(1, 1) = -0.6084550818878111; m(1, 2) = 0.24304035117451178; m(1, 3) = 36.943036725120294; m(2, 0) = 0.0; m(2, 1) = -0.3709410702804682; m(2, 2) = -0.928656407063011; m(2, 3) = -32.64950630748629; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), 0.6551993581911789, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.7554560219005915, 1e-3);
EXPECT_NEAR(mi(0, 2), 5.1550786254351327e-17, 1e-3);
EXPECT_NEAR(mi(0, 3), 43.88174541248007, 1e-3);
EXPECT_NEAR(mi(1, 0), -0.7015590749923187, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.6084550818878111, 1e-3);
EXPECT_NEAR(mi(1, 2), -0.37094107028046824, 1e-3);
EXPECT_NEAR(mi(1, 3), -6.735959663194609, 1e-3);
EXPECT_NEAR(mi(2, 0), 0.28022966531363025, 1e-3);
EXPECT_NEAR(mi(2, 1), 0.24304035117451175, 1e-3);
EXPECT_NEAR(mi(2, 2), -0.9286564070630109, 1e-3);
EXPECT_NEAR(mi(2, 3), -32.467188070139514, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.12316316348384228; m(0, 1) = 0.47851608243384774; m(0, 2) = -0.8693981792095123; m(0, 3) = -97.68194268706864; m(1, 0) = -0.9684362001788849; m(1, 1) = -0.24926156178416892; m(1, 2) = 0.0; m(1, 3) = -93.30408292578261; m(2, 0) = -0.2167075479620758; m(2, 1) = 0.8419566691161013; m(2, 2) = 0.4941121390809829; m(2, 3) = -48.17348057895294; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.12316316348384225, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.9684362001788847, 1e-3);
EXPECT_NEAR(mi(0, 2), -0.2167075479620758, 1e-3);
EXPECT_NEAR(mi(0, 3), -112.82942545947078, 1e-3);
EXPECT_NEAR(mi(1, 0), 0.47851608243384763, 1e-3);
EXPECT_NEAR(mi(1, 1), -0.2492615617841688, 1e-3);
EXPECT_NEAR(mi(1, 2), 0.8419566691161011, 1e-3);
EXPECT_NEAR(mi(1, 3), 64.04524235620795, 1e-3);
EXPECT_NEAR(mi(2, 0), -0.8693981792095123, 1e-3);
EXPECT_NEAR(mi(2, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(2, 2), 0.49411213908098284, 1e-3);
EXPECT_NEAR(mi(2, 3), -61.12140157794279, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
{
Matrix<double, 4, 4, ColumnStorage> m;
m(0, 0) = -0.8820615884895145; m(0, 1) = -0.11346792289034995; m(0, 2) = -0.45726620757093356; m(0, 3) = -48.8253349197771; m(1, 0) = -0.12758811649101956; m(1, 1) = 0.9918272392560481; m(1, 2) = 0.0; m(1, 3) = 50.033634014775515; m(2, 0) = 0.4535290802601621; m(2, 1) = 0.058341734158967; m(2, 2) = -0.8893298687290875; m(2, 3) = 92.22065810812657; m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
Matrix<double, 4, 4, ColumnStorage> mi = m.inverse();
EXPECT_NEAR(mi(0, 0), -0.8820615884895142, 1e-3);
EXPECT_NEAR(mi(0, 1), -0.12758811649101953, 1e-3);
EXPECT_NEAR(mi(0, 2), 0.453529080260162, 1e-3);
EXPECT_NEAR(mi(0, 3), -78.50800560549041, 1e-3);
EXPECT_NEAR(mi(1, 0), -0.11346792289034992, 1e-3);
EXPECT_NEAR(mi(1, 1), 0.991827239256048, 1e-3);
EXPECT_NEAR(mi(1, 2), 0.05834173415896697, 1e-3);
EXPECT_NEAR(mi(1, 3), -60.545143551904374, 1e-3);
EXPECT_NEAR(mi(2, 0), -0.45726620757093345, 1e-3);
EXPECT_NEAR(mi(2, 1), 6.120531779240618e-18, 1e-3);
EXPECT_NEAR(mi(2, 2), -0.8893298687290875, 1e-3);
EXPECT_NEAR(mi(2, 3), 59.68841003726312, 1e-3);
EXPECT_NEAR(mi(3, 0), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 1), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 2), 0.0, 1e-3);
EXPECT_NEAR(mi(3, 3), 1.0, 1e-3);
}
}
/**
* Test des performance de la multiplication matrice * vecteur
* pour de grandes dimensions.
*/
TEST(TestLabo1b, PerformanceMatrixVector)
{
Matrix<double> A(16384, 16384); // grande matrice avec stockage colonne
Vector<double> v(16384); // grand vecteur
using namespace std::chrono;
// Test : multiplication avec l'algorithme naif.
high_resolution_clock::time_point t = high_resolution_clock::now();
naiveMatrixMult(A, v);
const duration<double> naive_t = duration_cast<duration<double>>(high_resolution_clock::now() - t);
// Test : multiplication avec l'implémentation spécifique pour les matrices avec
// stockage par colonnes.
t = high_resolution_clock::now();
A* v;
const duration<double> optimal_t = duration_cast<duration<double>>(high_resolution_clock::now() - t);
EXPECT_TRUE(optimal_t < 0.4 * naive_t)
<< "Naive time: " << duration_cast<std::chrono::milliseconds>(naive_t).count() << " ms, "
<< "optimized time: " << duration_cast<std::chrono::milliseconds>(optimal_t).count() << " ms";
}
/**
* Test des performances de l'addition matrice + matrice
* pour de grandes dimensions.
*/
TEST(TestLabo1b, PerformanceLargeMatrixMatrix)
{
// deux grandes matrices à stockage par colonnes
Matrix<double> A(16384, 16384);
Matrix<double> B(16384, 16384);
using namespace std::chrono;
high_resolution_clock::time_point t = high_resolution_clock::now();
// Test : addition avec l'algorithme naif
naiveMatrixAddition(A, B);
const duration<double> naive_t = duration_cast<duration<double>>(high_resolution_clock::now() - t);
// Test : addition avec l'implémentation spécifique pour les matrices à
// stockage par colonnes.
t = high_resolution_clock::now();
A + B;
const duration<double> optimal_t = duration_cast<duration<double>>(high_resolution_clock::now() - t);
EXPECT_TRUE(optimal_t < 0.4 * naive_t);
}
/**
* Test pour la matrice à taille fixe 4D
*/
TEST(TestLabo1b, Matrix4x4SizeTest)
{
Matrix4d M;
EXPECT_EQ(M.cols(), 4);
EXPECT_EQ(M.rows(), 4);
}
/**
* Test pour la matrice à taille fixe 3D
*/
TEST(TestLabo1b, Matrix3x3SizeTest)
{
Matrix3d M;
EXPECT_EQ(M.cols(), 3);
EXPECT_EQ(M.rows(), 3);
}

View File

@ -0,0 +1,54 @@
/**
* @file TestsSupplementaire1a.cpp
*
* @brief Tests unitaires supplémentaires de la partie 1a
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
#include <gtest/gtest.h>
using namespace gti320;
TEST(TestLabo1a, Supplementaires)
{
// TODO vos propres tests ici
// TEST 1
//
// TEST 2
//
// TEST 3
//
// TEST 4
//
// TEST 5
//
// TEST 6
//
// TEST 7
//
// TEST 8
//
// TEST 9
//
// TEST 10
//
EXPECT_TRUE(false);
}

View File

@ -0,0 +1,56 @@
/**
* @file TestsSupplementaire1b.cpp
*
* @brief Tests unitaires supplémentaires de la partie 1b
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Matrix.h"
#include "Vector.h"
#include "Math3D.h"
#include "Operators.h"
#include <gtest/gtest.h>
using namespace gti320;
TEST(TestLabo1b, Supplementaires)
{
// TODO vos propres tests ici
// TEST 1
//
// TEST 2
//
// TEST 3
//
// TEST 4
//
// TEST 5
//
// TEST 6
//
// TEST 7
//
// TEST 8
//
// TEST 9
//
// TEST 10
//
EXPECT_TRUE(false);
}

View File

@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.15)
project(labo_physique)
# Setup language requirements
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OpenMP REQUIRED)
#--------------------------------------------------
# Sous MAC, OpenGL est Deprecated, mais toujours
# fonctionnel, on veut éviter tous les warnings
# durant la compilation.
#--------------------------------------------------
if (APPLE)
add_definitions( -DGL_SILENCE_DEPRECATION )
endif()
add_definitions(${NANOGUI_EXTRA_DEFS})
include_directories(${nanogui_SRC_DIR}/include)
include_directories(${NANOGUI_EXTRA_INCS})
include_directories(${PROJECT_SOURCE_DIR}/../labo01/src ${COMMON_INCLUDES})
find_package(OpenGL REQUIRED)
# Add .cpp and .h files
set(HEADERS ParticleSimGLCanvas.h ParticleSimApplication.h ParticleSystem.h Vector2d.h Solvers.h GraphColoring.h)
set(SOURCE main.cpp ParticleSimApplication.cpp ParticleSimGLCanvas.cpp ParticleSystem.cpp GraphColoring.cpp)
add_executable(labo_physique ${SOURCE} ${HEADERS})
target_link_libraries(labo_physique nanogui ${NANOGUI_EXTRA_LIBS} OpenMP::OpenMP_CXX)
if(MSVC)
set_property(TARGET labo_physique PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/labo_physique)
endif()

View File

@ -0,0 +1,53 @@
#include "GraphColoring.h"
#include "ParticleSystem.h"
using namespace gti320;
void GraphColoring::color(ParticleSystem& particleSystem)
{
// La palette de couleurs
ColorList C;
std::vector<Particle>& particles = particleSystem.getParticles();
// Initialiser toutes les particules avec color = -1
for (Particle& p : particles)
{
p.color = -1;
}
// TODO Calculer les couleurs de toutes les particules du système.
// Boucler sur chaque particule et appeler la fonction findColor.
// TODO Construire les partitions qui correspond à chaque couleur.
// Les partitions sont représentées par un tableau d'indices de particules, un pour chaque couleur.
// Stocker les partitions dans m_partitions.
}
int GraphColoring::findColor(const Particle& p, const std::vector<Particle>& particles, const std::vector<Spring>& springs, ColorList& C) const
{
// TODO Trouver la première couleur de la palette C qui n'est pas attribuée à une particule voisine.
// Si une couleur est introuvable, ajouter une nouvelle couleur à la palette et retournez la couleur.
// Utiliser la fonction findNeighbors pour assembler une liste de particules qui sont directement connectées à la particule p par un ressort (les voisines).
return -1;
}
NeighborList GraphColoring::findNeighbors(const Particle& p, const std::vector<Particle>& particles, const std::vector<Spring>& springs) const
{
NeighborList N;
for (const Spring& s : springs)
{
if (&particles[s.index0] == &p)
{
N.push_back(&particles[s.index1]);
}
else if (&particles[s.index1] == &p)
{
N.push_back(&particles[s.index0]);
}
}
return N;
}

View File

@ -0,0 +1,50 @@
#pragma once
/**
* @file GraphColoring.h
*
* @brief Algorithme glouton pour la coloration de graphe.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "ParticleSystem.h"
#include <vector>
#include <list>
namespace gti320
{
typedef std::list<const Particle*> NeighborList; // Type pour les particules voisines
typedef std::list<int> ColorList; // Type pour stocker la palette de couleurs
typedef std::vector<std::vector<int>> Partitions; // Type pour stocker les indices des particules dans chaque partition.
// Classe pour la coloration de graphe.
//
class GraphColoring
{
public:
// Attribuer des couleurs à toutes les particules du système.
//
void color(ParticleSystem& particleSystem);
// Retourner l'index des particules de chaque partition. (voir le type @a Partitions)
//
const Partitions& getPartitions() const { return m_partitions; }
private:
// Trouver une couleur qui n'est pas partagée par un voisine pour la particule @a p.
//
int findColor(const Particle& p, const std::vector<Particle>& particles, const std::vector<Spring>& springs, ColorList& C) const;
// Retourner toutes les particules qui sont directement raccordées à la particule @a p par un ressort..
//
NeighborList findNeighbors(const Particle& p, const std::vector<Particle>& particles, const std::vector<Spring>& springs) const;
Partitions m_partitions; // Les partitions.
};
}

View File

@ -0,0 +1,584 @@
#include "ParticleSimApplication.h"
#include "ParticleSimGLCanvas.h"
#include <nanogui/window.h>
#include <nanogui/formhelper.h>
#include <nanogui/layout.h>
#include <nanogui/label.h>
#include <nanogui/checkbox.h>
#include <nanogui/button.h>
#include <nanogui/toolbutton.h>
#include <nanogui/popupbutton.h>
#include <nanogui/combobox.h>
#include <nanogui/progressbar.h>
#include <nanogui/messagedialog.h>
#include <nanogui/textbox.h>
#include <nanogui/slider.h>
#include <nanogui/imagepanel.h>
#include <nanogui/imageview.h>
#include <nanogui/vscrollpanel.h>
#include <nanogui/colorwheel.h>
#include <nanogui/graph.h>
#include <nanogui/tabwidget.h>
#include <nanogui/opengl.h>
#include <fstream>
#include <random>
using namespace gti320;
namespace
{
static const float deltaT = 0.01667f;
/**
* Crée un système masse-ressort qui simule un tissu suspendu
*/
static inline void createHangingCloth(ParticleSystem& particleSystem, float k)
{
particleSystem.clear();
const int N = 16;
const int x_start = 240;
const int y_start = 80;
const int dx = 32;
const int dy = 32;
int index = 0;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
const int x = x_start + j * dx;
const int y = y_start + i * dy;
Particle particle(Vector2f(x, y), Vector2f(0, 0), Vector2f(0, 0), 1.0);
if (j == 0 && i == (N - 1)) particle.fixed = true;
if (j == (N - 1) && i == (N - 1)) particle.fixed = true;
particleSystem.addParticle(particle);
if (i > 0)
{
Spring s(index - N, index, k, (float)dy);
particleSystem.addSpring(s);
}
if (j > 0)
{
Spring s(index - 1, index, k, (float)dx);
particleSystem.addSpring(s);
}
if (i > 0 && j > 0)
{
Spring s(index - N - 1, index, k, std::sqrt((float)dx * dx + (float)dy * dy));
particleSystem.addSpring(s);
}
++index;
}
}
}
/**
* Crée un système masse-ressort qui simule un grand tissu suspendu
*/
static inline void createLargeHangingCloth(ParticleSystem& particleSystem, float k)
{
particleSystem.clear();
const int N = 32;
const int x_start = 240;
const int y_start = 80;
const int dx = 16;
const int dy = 16;
int index = 0;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
const int x = x_start + j * dx;
const int y = y_start + i * dy;
Particle particle(Vector2f(x, y), Vector2f(0, 0), Vector2f(0, 0), 1.0);
if (j == 0 && i == (N - 1)) particle.fixed = true;
if (j == (N - 1) && i == (N - 1)) particle.fixed = true;
particleSystem.addParticle(particle);
if (i > 0)
{
Spring s(index - N, index, k, (float)dy);
particleSystem.addSpring(s);
}
if (j > 0)
{
Spring s(index - 1, index, k, (float)dx);
particleSystem.addSpring(s);
}
if (i > 0 && j > 0)
{
Spring s(index - N - 1, index, k, std::sqrt((float)dx * dx + (float)dy * dy));
particleSystem.addSpring(s);
}
++index;
}
}
}
/**
* Crée un système masse-ressort qui simule une corde suspendu par ses
* extrémités.
*/
static inline void createHangingRope(ParticleSystem& particleSystem, float k)
{
particleSystem.clear();
const int N = 20;
const int x_start = 200;
const int dx = 32;
int index = 0;
for (int j = 0; j < N; ++j)
{
const int x = x_start + j * dx;
const int y = 480;
Particle particle(Vector2f(x, y), Vector2f(0, 0), Vector2f(0, 0), 1.0);
particle.fixed = (index == 0) || (index == N - 1);
particleSystem.addParticle(particle);
if (j > 0)
{
Spring s(index - 1, index, k, (float)dx);
particleSystem.addSpring(s);
}
++index;
}
}
/**
* Crée un système masse-ressort qui simule une poutre flexible
*/
static inline void createBeam(ParticleSystem& particleSystem, float k)
{
particleSystem.clear();
const int N = 20;
const int x_start = 200;
const int y_start = 400;
const int dx = 32;
const int dy = 32;
int index = 0;
for (int j = 0; j < N; ++j)
{
const int x = x_start + j * dx;
// Bottom particle
{
Particle particle(Vector2f(x, y_start), Vector2f(0, 0), Vector2f(0, 0), 1.0);
particle.fixed = (j == 0);
particleSystem.addParticle(particle);
if (j > 0)
{
Spring s(index - 1, index, k, (float)sqrt((float)dx * dx + (float)dy * dy));
particleSystem.addSpring(s);
Spring s2(index - 2, index, k, (float)dx);
particleSystem.addSpring(s2);
}
++index;
}
// Top particle
{
Particle particle(Vector2f(x, y_start + dy), Vector2f(0, 0), Vector2f(0, 0), 1.0);
particle.fixed = (j == 0);
particleSystem.addParticle(particle);
Spring s(index - 1, index, k, (float)dy);
particleSystem.addSpring(s);
if (j > 0)
{
Spring s2(index - 2, index, k, (float)dx);
particleSystem.addSpring(s2);
Spring s3(index - 3, index, k, (float)sqrt((float)dx * dx + (float)dy * dy));
particleSystem.addSpring(s3);
}
++index;
}
}
}
/**
* TODO Créez votre propre exemple
*/
static inline void createVotreExemple(ParticleSystem& particleSystem, float k)
{
particleSystem.clear();
// TODO Amusez-vous. Rendu ici, vous le méritez.
}
}
ParticleSimApplication::ParticleSimApplication() : nanogui::Screen(nanogui::Vector2i(1280, 720), "GTI320 Labo Physique lineaire", true, false, true, true, false, 4, 1)
, m_particleSystem(), m_stepping(false), m_fpsCounter(0), m_fpsTime(0.0), m_maxIter(10), m_solverType(kGaussSeidel)
{
initGui();
createBeam(m_particleSystem, m_stiffness); // le modèle "poutre" est sélectionné à l'initialisation
m_particleSystem.pack(m_p0, m_v0, m_f0);
perform_layout();
reset();
}
void ParticleSimApplication::initGui()
{
// Initialisation de la fenêtre
m_window = new nanogui::Window(this, "Particle sim");
m_window->set_position(nanogui::Vector2i(8, 8));
m_window->set_layout(new nanogui::GroupLayout());
// initialisation du canvas où est affiché le système de particules
m_canvas = new ParticleSimGLCanvas(this);
m_canvas->set_background_color({ 255, 255, 255, 255 });
m_canvas->set_size({ 1000, 600 });
m_canvas->set_draw_border(false);
// Initialisation de la fenêtre de contrôle
nanogui::Window* controls = new nanogui::Window(this, "Controls");
controls->set_position(nanogui::Vector2i(960, 10));
controls->set_layout(new nanogui::GroupLayout());
Widget* tools = new Widget(controls);
tools->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Vertical, nanogui::Alignment::Middle, 0, 20));
// Intervalles des curseur
const auto stiffnessMinMax = std::make_pair<float, float>(0.0f, logf(5000.f));
const auto iterMinMax = std::make_pair<float, float>(1.f, 100.f);
// Affichage du FPS
m_panelFPS = new Widget(tools);
m_panelFPS->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal, nanogui::Alignment::Middle, 0, 5));
m_labelFPS = new nanogui::Label(m_panelFPS, "FPS :");
m_textboxFPS = new nanogui::TextBox(m_panelFPS);
m_textboxFPS->set_fixed_width(60);
m_textboxFPS->set_value("0");
// Affichage du numéro de frame
m_panelFrames = new Widget(tools);
m_panelFrames->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal, nanogui::Alignment::Middle, 0, 5));
m_labelFrames = new nanogui::Label(m_panelFrames, "Frame :");
m_textboxFrames = new nanogui::TextBox(m_panelFrames);
m_textboxFrames->set_fixed_width(60);
m_textboxFrames->set_value("0");
// Boutons pour le choix du solveur
m_panelSolver = new Widget(tools);
m_panelSolver->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Vertical, nanogui::Alignment::Middle, 0, 5));
new nanogui::Label(m_panelSolver, "Solver : ");
nanogui::Button* b = new nanogui::Button(m_panelSolver, "Gauss-Seidel");
b->set_flags(nanogui::Button::RadioButton);
b->set_pushed(true);
b->set_callback([this] { m_solverType = kGaussSeidel; });
b = new nanogui::Button(m_panelSolver, "Gauss-Seidel (coloration)");
b->set_callback([this] { m_solverType = kColorGaussSeidel; });
b->set_flags(nanogui::Button::RadioButton);
b = new nanogui::Button(m_panelSolver, "Cholesky");
b->set_callback([this] { m_solverType = kCholesky; });
b->set_flags(nanogui::Button::RadioButton);
b = new nanogui::Button(m_panelSolver, "None");
b->set_callback([this] { m_solverType = kNone; });
b->set_flags(nanogui::Button::RadioButton);
// Curseur de rigidité
Widget* panelSimControl = new Widget(tools);
panelSimControl->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Vertical, nanogui::Alignment::Middle, 0, 5));
m_panelStiffness = new Widget(panelSimControl);
m_panelStiffness->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal, nanogui::Alignment::Middle, 0, 5));
m_labelStiffness = new nanogui::Label(m_panelStiffness, "Stiffness : ");
m_sliderStiffness = new nanogui::Slider(m_panelStiffness);
m_sliderStiffness->set_range(stiffnessMinMax);
m_textboxStiffness = new nanogui::TextBox(m_panelStiffness);
m_sliderStiffness->set_callback([this](float value)
{
m_stiffness = std::exp(value);
onStiffnessSliderChanged();
});
m_sliderStiffness->set_value(std::logf(300.f));
// Curseur du nombre maximum d'itération pour Jacobi et Gauss-Seidel
Widget* panelMaxIter = new Widget(panelSimControl);
panelMaxIter->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal, nanogui::Alignment::Middle, 0, 5));
new nanogui::Label(panelMaxIter, "Max iterations : ");
nanogui::Slider* sliderMaxIter = new nanogui::Slider(panelMaxIter);
sliderMaxIter->set_range(iterMinMax);
nanogui::TextBox* textboxMaxIter = new nanogui::TextBox(panelMaxIter);
textboxMaxIter->set_value(std::to_string(m_maxIter));
sliderMaxIter->set_value(m_maxIter);
sliderMaxIter->set_callback([this, textboxMaxIter](float value)
{
m_maxIter = (int)value;
textboxMaxIter->set_value(std::to_string(m_maxIter));
});
// Bouton «Simulate»
nanogui::Button* startStopButton = new nanogui::Button(panelSimControl, "Simulate");
startStopButton->set_flags(nanogui::Button::ToggleButton);
startStopButton->set_change_callback([this](bool val)
{
m_stepping = val;
if (val)
{
m_prevTime = glfwGetTime();
draw_all();
}
});
// Bouton «Step»
nanogui::Button* stepButton = new nanogui::Button(panelSimControl, "Step");
stepButton->set_callback([this]
{
if (!m_stepping)
step(deltaT);
});
// Bouton «Reset»
nanogui::Button* resetButton = new nanogui::Button(panelSimControl, "Reset");
resetButton->set_callback([this]
{
reset();
});
// Boutons pour le choix du modèle
Widget* panelExamples = new Widget(tools);
panelExamples->set_layout(new nanogui::BoxLayout(nanogui::Orientation::Vertical, nanogui::Alignment::Middle, 0, 5));
new nanogui::Label(panelExamples, "Examples : ");
nanogui::Button* loadClothButton = new nanogui::Button(panelExamples, "Cloth");
loadClothButton->set_callback([this]
{
createHangingCloth(m_particleSystem, m_stiffness);
m_particleSystem.pack(m_p0, m_v0, m_f0);
reset();
});
nanogui::Button* loadLargeClothButton = new nanogui::Button(panelExamples, "Large cloth");
loadLargeClothButton->set_callback([this]
{
createLargeHangingCloth(m_particleSystem, m_sliderStiffness->value());
m_particleSystem.pack(m_p0, m_v0, m_f0);
reset();
});
nanogui::Button* loadBeamButton = new nanogui::Button(panelExamples, "Beam");
loadBeamButton->set_callback([this]
{
createBeam(m_particleSystem, m_stiffness);
m_particleSystem.pack(m_p0, m_v0, m_f0);
reset();
});
nanogui::Button* loadRopeButton = new nanogui::Button(panelExamples, "Rope");
loadRopeButton->set_callback([this]
{
createHangingRope(m_particleSystem, m_stiffness);
m_particleSystem.pack(m_p0, m_v0, m_f0);
reset();
});
nanogui::Button* loadVotreExemple = new nanogui::Button(panelExamples, "Le vôtre");
loadVotreExemple->set_callback([this]
{
createVotreExemple(m_particleSystem, m_stiffness);
m_particleSystem.pack(m_p0, m_v0, m_f0);
reset();
});
}
/**
* Réaction aux événements déclenchés par le clavier
*/
bool ParticleSimApplication::keyboard_event(int key, int scancode, int action, int modifiers)
{
if (Screen::keyboard_event(key, scancode, action, modifiers))
return true;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
set_visible(false);
return true;
}
return false;
}
/**
* Boucle principale
*
* Cette fonction est appelée périodiquement lorsque le programme est actif.
* C'est ici que tout se passe. Si la simulation est en cours, la fonction
* `step` est appelée pour faire avancer le système d'un intervalle de temps
* DELTA_T. Ensuite, l'affichage est mis à jour.
*/
void ParticleSimApplication::draw_contents()
{
Screen::draw_contents();
if (m_stepping)
{
auto now = glfwGetTime();
float dt = now - m_prevTime;
step(deltaT);
// Update frames per second
//
m_fpsTime += dt;
++m_fpsCounter;
if (m_fpsCounter > 30)
{
const float fps = (float)m_fpsCounter / m_fpsTime;
char buf[64];
snprintf(buf, sizeof(buf), "%3.1f", fps);
m_fpsCounter = 0;
m_fpsTime = 0.0;
m_textboxFPS->set_value(buf);
}
m_prevTime = now;
updateFrameCounter();
}
redraw();
}
/**
* Appelée lorsque le curseur de rigidité est modifié. La nouvelle rigidité est
* affectée à tous les ressorts
*/
void ParticleSimApplication::onStiffnessSliderChanged()
{
// Update all springs with the slider value
for (Spring& s : getParticleSystem().getSprings())
{
s.k = m_stiffness;
}
char buf[16];
snprintf(buf, sizeof(buf), "%4.0f", m_stiffness);
m_textboxStiffness->set_value(buf);
}
/**
* Effectue un pas de simulation de taille dt.
*/
void ParticleSimApplication::step(float dt)
{
// Construction des matrices de masse et de rigidité
//
m_particleSystem.buildMassMatrix(m_M);
m_particleSystem.buildDfDx(m_dfdx);
// Calcul des forces actuelles sur chacune de sparticules
//
m_particleSystem.computeForces();
m_canvas->applyMouseSpring();
// Assemblage des vecteurs d'états.
//
m_particleSystem.pack(m_x, m_v, m_f);
//////////////////////////////////////////////////////////////////////////////////
// TODO Construire le système d'équation linéaire sous la forme `A*v_plus = b`.
// la construction de A et b est donnée dans les diapos du Cours 8.
//
// Note : lors du calcul de b, NE PAS calculer `Mg + Kx` ce
// calcul est inutilement coûteux. Pour être plus efficace, on utilise
// directement le vecteur d'état m_f.
//
//////////////////////////////////////////////////////////////////////////////////
// Remarque : A et b sont déclarés `const` et ce n'est pas une erreur. C'est
// pour vous forcer à optimiser votre code.
//
// Considérez les exemples suivants :
//
// # Version 1
// Matrix A; // constructeur par défaut
// A = B + C; // operator+ on construit une matrice et elle est suite copiée avec operator=
//
// # Version 2
// Matrix A = B + C; // la matrice construite dans operator+ est la matrice A.
//
// Bilan : Version 1 utilise 2 constructeurs et 1 opérateur de copie
// Version 2 utilise un seul constructeur et aucune copie
//////////////////////////////////////////////////////////////////////////////////
//
const Matrix<float, Dynamic, Dynamic> A;
const Vector<float, Dynamic> b;
// Résolution du système d'équations `A*v_plus = b`.
//
Vector<float, Dynamic> v_plus;
Vector<float, Dynamic> acc; // vecteur d'accélérations
switch (m_solverType)
{
case kGaussSeidel:
gaussSeidel(A, b, v_plus, m_maxIter);
break;
case kColorGaussSeidel:
gaussSeidelColor(A, b, v_plus, m_graphColor.getPartitions(), m_maxIter);
break;
case kCholesky:
cholesky(A, b, v_plus);
break;
default:
case kNone:
// N'utilise pas de solveur, il s'agit de l'implémentation naive de
// l'intégration d'Euler.
acc.resize(m_M.rows()); // vecteur d'accélérations
for (int i = 0; i < m_M.rows(); ++i)
acc(i) = (1.0 / m_M(i, i)) * m_f(i);
v_plus = m_v + dt * acc;
break;
}
// TODO Mise à jour du vecteur d'état de position via l'intégration d'Euler
// implicite. Les nouvelles position sont calculées à partir des position
// actuelles m_x et des nouvelles vitesses v_plus. Les nouvelles positions
// sont stockées directement dans le vecteur m_x.
// Affecte les valeurs calculées dans le vecteurs d'états aux particules du
// système
m_particleSystem.unpack(m_x, v_plus);
}
/**
* Réinitialisation du système de particules
*/
void ParticleSimApplication::reset()
{
m_frameCounter = 0;
m_particleSystem.unpack(m_p0, m_v0);
m_graphColor.color(m_particleSystem);
onStiffnessSliderChanged();
}
/**
* Mise à jour du compteur de frames
*/
void ParticleSimApplication::updateFrameCounter()
{
++m_frameCounter;
char buf[16];
snprintf(buf, sizeof(buf), "%d", m_frameCounter);
m_textboxFrames->set_value(buf);
}

View File

@ -0,0 +1,133 @@
#pragma once
/**
* @file ParticleSimApplication.h
*
* @brief Partir applicative du laboratoire 3 : contrôle de la simulaiton via
* l'interface graphique.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <nanogui/window.h>
#include <nanogui/textbox.h>
#include <nanogui/label.h>
#include <nanogui/slider.h>
#include <nanogui/screen.h>
#include "ParticleSystem.h"
#include "GraphColoring.h"
#include "Solvers.h"
#include <chrono>
class ParticleSimGLCanvas;
/**
* Interface graphique
*/
class ParticleSimApplication : public nanogui::Screen
{
public:
ParticleSimApplication();
virtual bool keyboard_event(int key, int scancode, int action, int modifiers) override;
virtual void draw_contents() override;
nanogui::Window* getWindow() const { return m_window; }
const gti320::ParticleSystem& getParticleSystem() const { return m_particleSystem; }
gti320::ParticleSystem& getParticleSystem() { return m_particleSystem; }
private:
void initGui();
/**
* Effectue un pas de simulation d'un intervale de temps dt.
*/
void step(float dt);
/**
* Fonction appelée lorsque le glisseur de rigidité est modifié
*/
void onStiffnessSliderChanged();
/**
* Réinitialise le système de particules
*/
void reset();
void updateFrameCounter();
ParticleSimGLCanvas* m_canvas;
nanogui::Window* m_window;
// Panels
Widget* m_panelFPS;
Widget* m_panelFrames;
Widget* m_panelStiffness;
Widget* m_panelSolver;
Widget* m_panelRayleigh;
// Textboxes
nanogui::TextBox* m_textboxFPS;
nanogui::TextBox* m_textboxFrames;
nanogui::TextBox* m_textboxStiffness;
nanogui::TextBox* m_textboxRayleighAlpha;
nanogui::TextBox* m_textboxRayleighBeta;
// Labels
nanogui::Label* m_labelFPS;
nanogui::Label* m_labelFrames;
nanogui::Label* m_labelStiffness;
nanogui::Label* m_labelRayleighAlpha;
nanogui::Label* m_labelRayleighBeta;
// Sliders
nanogui::Slider* m_sliderStiffness;
nanogui::Slider* m_sliderRayleighAlpha;
nanogui::Slider* m_sliderRayleighBeta;
// Le système de particules
gti320::ParticleSystem m_particleSystem;
// Coloration de graphe
gti320::GraphColoring m_graphColor;
// The variable m_stepping is true if the simulation is running,
// and step() will be called automatically
bool m_stepping; // true lorsque la simulation est cours, false lorsqu'elles à l'arrêt
float m_stiffness; // la rigidité des ressorts
int m_maxIter; // nombre max d'itération pour les solveurs itératifs
gti320::eSolverType m_solverType; // indique le choix du solveur
// Variables pour le calcul du fps et le compteur de frames
int m_fpsCounter;
float m_fpsTime;
int m_frameCounter;
float m_prevTime;
// Matrices du système
gti320::Matrix<float, gti320::Dynamic, gti320::Dynamic> m_M; // matrice de masses
gti320::Matrix<float, gti320::Dynamic, gti320::Dynamic> m_dfdx; // matrice de rigidité
// Vecteurs d'état
gti320::Vector<float, gti320::Dynamic> m_x; // positions des particules
gti320::Vector<float, gti320::Dynamic> m_v; // vélocités des particules
gti320::Vector<float, gti320::Dynamic> m_f; // forces des particules
// État initial (utilisé pour réinitialiser le système)
gti320::Vector<float, gti320::Dynamic> m_p0; // positions des particules
gti320::Vector<float, gti320::Dynamic> m_v0; // vélocités des particules
gti320::Vector<float, gti320::Dynamic> m_f0; // forces des particules
// Paramètre pour l'amortissement de Rayleigh
float m_alpha, m_beta;
};

View File

@ -0,0 +1,230 @@
#include "ParticleSimGLCanvas.h"
#include "ParticleSystem.h"
#include <nanogui/opengl.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace nanogui;
namespace
{
static const float r = 6.0f;
static inline gti320::Particle* pickParticle(std::vector<gti320::Particle>& particles, const gti320::Vector2f& mousePos)
{
for (gti320::Particle& particle : particles)
{
const float dist = (particle.x - mousePos).norm();
if (dist <= r)
{
return &particle;
}
}
return nullptr;
}
static const float fixedColor[3] = { 1.0f, 1.0f, 0.68f };
static const float defaultColor[3] = { 1.0f, 0.0f, 0.0f };
static const int maxColors = 8;
static const int colorMap[maxColors * 3] = {
228,26,28,
55,126,184,
77,175,74,
152,78,163,
255,127,0,
255,255,51,
166,86,40,
247,129,191
};
}
ParticleSimGLCanvas::ParticleSimGLCanvas(ParticleSimApplication* _app) : nanogui::Canvas(_app->getWindow()), m_app(_app), m_selectedParticle(nullptr)
{
// Un shader minimaliste pour afficher les particules
m_particleShader = new Shader(render_pass(),
// Nom du shader
"particle_shader",
// Nuanceur de particules
R"(#version 410
uniform mat4 modelViewProj;
uniform vec4 color;
in vec2 position;
void main() {
gl_Position = modelViewProj * vec4(position.xy, -1, 1);
})",
// Fragment shader
R"(#version 410
uniform vec4 color;
out vec4 frag_color;
void main() {
frag_color = color;
})"
);
// Initialise la géométrie pour un cercle
static const int numTris = 8;
m_circle.resize(4 * (numTris+1));
m_circle.setZero();
for (int i = 0; i <= numTris; ++i)
{
const float angle = 2.0f * M_PI * ((float)i / numTris);
const float x = r * sin(angle);
const float y = r * cos(angle);
m_circle(4 * i) = x;
m_circle(4 * i + 1) = y;
m_circle(4 * i + 2) = 0;
m_circle(4 * i + 3) = 0;
}
const Matrix4f mvp = Matrix4f::scale(Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
m_particleShader->set_uniform("modelViewProj", mvp);
m_particleShader->set_uniform("color", Vector4f(0.0f, 0.0f, 1.0f, 10.0f));
m_particleShader->set_buffer("position", VariableType::Float32, { (uint32_t)m_circle.rows(), (int)2 }, m_circle.storage().data());
}
ParticleSimGLCanvas::~ParticleSimGLCanvas() {
}
void ParticleSimGLCanvas::draw_contents()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_DEPTH_TEST);
m_particleShader->set_uniform("color", Vector4f(1.0f, 0.0f, 0.0f, 1.0f));
// Matrice de projection orthographique
const Matrix4f projMat = Matrix4f::ortho(0, width() - 1, 0, height() - 1, 0.1f, 1.0f);
const gti320::ParticleSystem& particleSystem = m_app->getParticleSystem();
const auto particles = particleSystem.getParticles();
const int numParticles = particles.size();
// Affichage des ressorts
const auto springs = particleSystem.getSprings();
const int numSprings = springs.size();
std::vector<float> points(4 * numSprings);
for (int i = 0; i < numSprings; ++i)
{
const int index0 = springs[i].index0;
const int index1 = springs[i].index1;
const int k = 4 * i;
memcpy(&points[k], particles[index0].x.data(), sizeof(float) * 2);
memcpy(&points[k+2], particles[index1].x.data(), sizeof(float) * 2);
}
m_particleShader->set_uniform("modelViewProj", projMat);
m_particleShader->set_uniform("color", Vector4f(0.0f, 0.0f, 1.0f, 1.0f));
m_particleShader->set_buffer("position", VariableType::Float32, { (uint32_t)points.size() / 2, 2 }, points.data());
m_particleShader->begin();
m_particleShader->draw_array(Shader::PrimitiveType::Line, 0, points.size() / 2);
m_particleShader->end();
// Affichage des particules
m_particleShader->set_buffer("position", VariableType::Float32, { (uint32_t)m_circle.rows() / 2, 2 }, m_circle.data());
for (int i = 0; i < numParticles; ++i)
{
Matrix4f modelMat = Matrix4f::scale(Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
modelMat.m[3][0] = particles[i].x(0);
modelMat.m[3][1] = particles[i].x(1);
const Matrix4f mvp = projMat * modelMat;
m_particleShader->set_uniform("modelViewProj", mvp);
if (particles[i].color > -1)
{
const int c = std::min(particles[i].color, maxColors - 1);
const int i = 3 * c;
m_particleShader->set_uniform("color", Vector4f((float)colorMap[i]/255.0f, (float)colorMap[i+1] / 255.0f, (float)colorMap[i+2] / 255.0f, 1.0f));
}
else
{
m_particleShader->set_uniform("color", Vector4f(defaultColor[0], defaultColor[1], defaultColor[2], 1.0f));
}
m_particleShader->begin();
m_particleShader->draw_array(Shader::PrimitiveType::TriangleStrip, 0, m_circle.rows() / 2);
m_particleShader->end();
}
// Affichage du ressort déféni par la souris
if (m_selectedParticle)
{
const gti320::Vector2f p = m_selectedParticle->x;
const float coords[4] = { m_mousePos(0), m_mousePos(1), p(0), p(1) };
m_particleShader->set_uniform("modelViewProj", projMat);
m_particleShader->set_uniform("color", Vector4f(0.0f, 1.0f, 0.0f, 1.0f));
m_particleShader->set_buffer("position", VariableType::Float32, { 2, 2 }, coords);
m_particleShader->begin();
m_particleShader->draw_array(Shader::PrimitiveType::Line, 0, 2);
m_particleShader->end();
}
}
bool ParticleSimGLCanvas::mouse_button_event(const Vector2i& p, int button, bool down, int modifiers)
{
if (modifiers == GLFW_MOD_SHIFT)
{
if (button == GLFW_MOUSE_BUTTON_1 && down)
{
convertAndStoreMousePos(p);
m_selectedParticle = pickParticle(m_app->getParticleSystem().getParticles(), m_mousePos);
if (m_selectedParticle != nullptr)
{
m_selectedParticle->fixed = !(m_selectedParticle->fixed);
m_selectedParticle = nullptr;
}
return true;
}
}
else
{
if (button == GLFW_MOUSE_BUTTON_1 && down)
{
convertAndStoreMousePos(p);
m_selectedParticle = pickParticle(m_app->getParticleSystem().getParticles(), m_mousePos);
return true;
}
else if (button == 0)
{
m_selectedParticle = nullptr;
return true;
}
}
return false;
}
bool ParticleSimGLCanvas::mouse_drag_event(const Vector2i& p, const Vector2i& rel, int button, int modifiers)
{
if (button == GLFW_MOUSE_BUTTON_2 && modifiers == 0 && m_selectedParticle != nullptr)
{
convertAndStoreMousePos(p);
return true;
}
return false;
}
void ParticleSimGLCanvas::convertAndStoreMousePos(const Vector2i& mousePos)
{
const Vector2i& pos = position();
const int y = height() - (mousePos.y() - pos.y()) - 1;
m_mousePos(0) = (float)(mousePos.x() - pos.x());
m_mousePos(1) = (float)y;
}
void ParticleSimGLCanvas::applyMouseSpring()
{
if (m_selectedParticle != nullptr)
{
const float k = 20.0f * m_selectedParticle->m;
const gti320::Vector2f f = k * (m_mousePos - m_selectedParticle->x);
m_selectedParticle->f = m_selectedParticle->f + f;
}
}

View File

@ -0,0 +1,51 @@
#pragma once
/**
* @file ParticleSimGLCanvas.h
*
* @brief Classe Canvas pour l'affichage via OpenGL.
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <nanogui/canvas.h>
#include <nanogui/shader.h>
#include "ParticleSimApplication.h"
/**
* Classe canvas OpenGL pour afficher des points et des segments.
*/
class ParticleSimGLCanvas : public nanogui::Canvas
{
public:
ParticleSimGLCanvas(ParticleSimApplication* _app);
~ParticleSimGLCanvas();
virtual void draw_contents() override;
virtual bool mouse_button_event(const nanogui::Vector2i &p, int button, bool down, int modifiers) override;
virtual bool mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int modifiers) override;
void applyMouseSpring();
private:
void convertAndStoreMousePos(const nanogui::Vector2i& mousePos);
nanogui::ref<nanogui::Shader> m_particleShader;
ParticleSimApplication* m_app;
gti320::Particle* m_selectedParticle;
double m_mouseStiffness;
gti320::Vector2f m_mousePos;
gti320::Vector<float> m_circle;
};

View File

@ -0,0 +1,108 @@
#include "ParticleSystem.h"
using namespace gti320;
/**
* Calcule des forces qui affectent chacune des particules.
*
* É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()
{
// TODO
//
// Calcul de la force gravitationnelle sur chacune des particules
for (Particle& p : m_particles)
{
}
// TODO
//
// Pour chaque ressort, ajouter la force exercée à chacune des exptré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)
{
}
}
/**
* Assemble les données du système dans les vecteurs trois vecteurs d'état _pos,
* _vel et _force.
*/
void ParticleSystem::pack(Vector<float, Dynamic>& _pos,
Vector<float, Dynamic>& _vel,
Vector<float, Dynamic>& _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.
}
/**
* Copie les données des vecteurs d'états dans le particules du système.
*/
void ParticleSystem::unpack(const Vector<float, Dynamic>& _pos,
const Vector<float, Dynamic>& _vel)
{
// TODO
//
// Mise à jour des propriétés de chacune des particules à partir des valeurs
// contenues dans le vecteur d'état.
}
/**
* Construction de la matirce de masses.
*/
void ParticleSystem::buildMassMatrix(Matrix<float, Dynamic, Dynamic>& M)
{
const int numParticles = m_particles.size();
const int dim = 2 * numParticles;
M.resize(dim, dim);
M.setZero();
// TODO
//
// Inscrire la masse de chacune des particules dans la matrice de masses M
//
for (int i = 0; i < numParticles; ++i)
{
}
}
/**
* Construction de la matrice de rigidité.
*/
void ParticleSystem::buildDfDx(Matrix<float, Dynamic, Dynamic>& dfdx)
{
const int numParticles = m_particles.size();
const int numSprings = m_springs.size();
const int dim = 2 * numParticles;
dfdx.resize(dim, dim);
dfdx.setZero();
// Pour chaque ressort...
for (const Spring& spring : m_springs)
{
// TODO
//
// Calculer le coefficients alpha et le produit dyadique tel que décrit au cours.
// Utiliser les indices spring.index0 et spring.index1 pour calculer les coordonnées des endroits affectés.
//
// Astuce: créer une matrice de taille fixe 2 par 2 puis utiliser la classe SubMatrix pour accumuler
// les modifications sur la diagonale (2 endroits) et pour mettre à jour les blocs non diagonale (2 endroits).
}
}

View File

@ -0,0 +1,152 @@
#pragma once
/**
* @file ParticleSystem.h
*
* @brief Système de particules de type masse-ressort
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Math3D.h"
#include "Vector2d.h"
#include <vector>
namespace gti320
{
/**
* Classe particule 2D.
*/
class Particle
{
public:
bool fixed; // indique si la particule est stationnaire (impossible à bouger)
float m; // masse
Vector2f x; // position
Vector2f v; // vélocité
Vector2f f; // force
int color; // coleur de particule
Particle(const Vector2f& _x, const Vector2f& _v, const Vector2f& _f, float _m)
: fixed(false), m(_m), x(_x), v(_v), f(_f), color(-1)
{ }
Particle() : fixed(false), m(1.0), x(0, 0), v(0, 0), f(0, 0), color(-1)
{ }
};
/**
* Classe ressort 2d.
*
* Un ressort relie toujours deux particules appelées particule 0 et particule
* 1. Ces particules sont identifiées par leur indice dans le tableau de
* particules du système de particule.
*/
class Spring
{
public:
int index0; // indice de la particule 0
int index1; // indice de la particule 1
float k; // rigidité
float l0; // longueur au repos
Spring(int _index0, int _index1, float _k, float _l0)
: index0(_index0), index1(_index1), k(_k), l0(_l0)
{ }
};
/**
* Classe représentant un système de particule masse-ressort 2D.
*/
class ParticleSystem
{
private:
std::vector<Particle> m_particles; // les particules
std::vector<Spring> m_springs; // les ressorts
public:
ParticleSystem() : m_particles(), m_springs() { }
/**
* Supprime toutes les particules et tous les ressorts
*/
void clear()
{
m_particles.clear();
m_springs.clear();
}
/**
* Ajoute une particule au système. La particule est copiée dans le tableau
* m_particles.
*/
void addParticle(const Particle& _particle)
{
m_particles.push_back(_particle);
}
/**
* Ajoute un ressort au système. Le ressort est copié dans le tableau
* m_springs.
*/
void addSpring(const Spring& _spring) { m_springs.push_back(_spring); }
/**
* Calcul des forces exercées sur chacune des particules.
*/
void computeForces();
/**
* Accesseurs pour les particules et les ressorts
*/
const std::vector<Particle>& getParticles() const
{
return m_particles;
}
std::vector<Particle>& getParticles()
{
return m_particles;
}
const std::vector<Spring>& getSprings() const
{
return m_springs;
}
std::vector<Spring>& getSprings()
{
return m_springs;
}
/**
* Assemble les vecteurs d'états.
*/
void pack(Vector<float, Dynamic>& _pos,
Vector<float, Dynamic>& _vel,
Vector<float, Dynamic>& _force);
/**
* Mise à jour de la position et de la vitesse de chacune des particules à
* partir des vecteurs d'état.
*/
void unpack(const Vector<float, Dynamic>& _pos,
const Vector<float, Dynamic>& _vel);
/**
* Contruit la matrice de masse.
*/
void buildMassMatrix(Matrix<float, Dynamic, Dynamic>& M);
/**
* Construit la matrice df/dx
*/
void buildDfDx(Matrix<float, Dynamic, Dynamic>& dfdx);
};
}

81
labo_physique/Solvers.h Normal file
View File

@ -0,0 +1,81 @@
#pragma once
/**
* @file Solvers.hpp
*
* @brief Implémentation de plusieurs algorihtmes de solveurs pour un système
* d'équations linéaires
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Math3D.h"
namespace gti320
{
// Identification des solveurs
enum eSolverType { kNone, kGaussSeidel, kColorGaussSeidel, kCholesky };
// Paramètres de convergences pour les algorithmes itératifs
static const float eps = 1e-4f;
static const float tau = 1e-5f;
/**
* Résout Ax = b avec la méthode Gauss-Seidel
*/
static void gaussSeidel(const Matrix<float, Dynamic, Dynamic>& A,
const Vector<float, Dynamic>& b,
Vector<float, Dynamic>& x, int k_max)
{
// TODO
//
// Implémenter la méthode de Gauss-Seidel
}
/**
* Résout Ax = b avec la méthode Gauss-Seidel (coloration de graphe)
*/
static void gaussSeidelColor(const Matrix<float, Dynamic, Dynamic>& A, const Vector<float, Dynamic>& b, Vector<float, Dynamic>& x, const Partitions& P, const int maxIter)
{
// TODO
//
// Implémenter la méthode de Gauss-Seidel avec coloration de graphe.
// Les partitions avec l'index de chaque particule sont stockées dans la table des tables, P.
}
/**
* Résout Ax = b avec la méthode de Cholesky
*/
static void cholesky(const Matrix<float, Dynamic, Dynamic>& A,
const Vector<float, Dynamic>& b,
Vector<float, Dynamic>& x)
{
// TODO
//
// Calculer la matrice L de la factorisation de Cholesky
// TODO
//
// Résoudre Ly = b
// TODO
//
// Résoudre L^t x = y
//
// Remarque : ne pas caculer la transposer de L, c'est inutilement
// coûteux.
}
}

109
labo_physique/Vector2d.h Normal file
View File

@ -0,0 +1,109 @@
#pragma once
/**
* @file Vector2d.h
*
* @brief Vecteur 2D
*
* Nom:
* Code permanent :
* Email :
*
*/
#include "Math3D.h"
namespace gti320
{
/**
* Spécialisation de template pour le les vecteurs 2D
*/
template<typename _Scalar>
class Vector<_Scalar, 2> : public MatrixBase<_Scalar, 2, 1>
{
public:
/**
* Constructeur par défaut
*/
Vector() : MatrixBase<_Scalar, 2, 1>() { }
/**
* Constructeur à partir de la dimension. Afin d'assurer la compatibilité
* avec la version non-spécialisée de la classe.
*
* @param _size doit être 2
*/
Vector(int _size) : MatrixBase<_Scalar, 2, 1>()
{
assert(_size == 2);
}
/**
* Constructeur à partir des coordonnées x et y
*/
Vector(_Scalar x, _Scalar y) : MatrixBase<_Scalar, 2, 1>()
{
(*this)(0) = x;
(*this)(1) = y;
}
/**
* Constructeur de copie
*/
Vector(const Vector& other) : MatrixBase<_Scalar, 2, 1>(other) { }
/**
* Destructeur
*/
~Vector() { }
/**
* Opérateur de copie
*/
Vector& operator=(const Vector& other)
{
this->m_storage = other.m_storage;
return *this;
}
/**
* Accesseur à une entrée du vecteur (lecture seule)
*/
_Scalar operator()(int i) const
{
assert(0 <= i && i < 2);
return *(this->m_storage.data() + i);
}
/**
* Accesseur à une entrée du vecteur (lecture et écriture)
*/
_Scalar& operator()(int i)
{
assert(0 <= i && i < 2);
return *(this->m_storage.data() + i);
}
/**
* Produit scalaire de *this et other
*/
inline _Scalar dot(const Vector& other) const
{
return (*this)(0) * other(0) + (*this)(1) * other(1);
}
/**
* Norme eulidienne du vecteur
*/
inline _Scalar norm() const
{
return sqrt(dot(*this));
}
};
typedef Vector<float, 2> Vector2f;
}

74
labo_physique/main.cpp Normal file
View File

@ -0,0 +1,74 @@
/**
* @file main.cpp
*
* @brief GTI320 Simulation d'un système masse-ressort
*
* Nom:
* Code permanent :
* Email :
*
*/
#include <nanogui/opengl.h>
#include <nanogui/screen.h>
#include <nanogui/window.h>
#include <nanogui/formhelper.h>
#include <nanogui/layout.h>
#include <nanogui/label.h>
#include <nanogui/checkbox.h>
#include <nanogui/button.h>
#include <nanogui/toolbutton.h>
#include <nanogui/popupbutton.h>
#include <nanogui/combobox.h>
#include <nanogui/progressbar.h>
#include <nanogui/messagedialog.h>
#include <nanogui/textbox.h>
#include <nanogui/slider.h>
#include <nanogui/imagepanel.h>
#include <nanogui/imageview.h>
#include <nanogui/vscrollpanel.h>
#include <nanogui/colorwheel.h>
#include <nanogui/graph.h>
#include <nanogui/tabwidget.h>
#include <iostream>
#include <fstream>
#include <string>
#include "ParticleSimApplication.h"
// Includes for the GLTexture class.
#include <cstdint>
#include <memory>
#include <utility>
#include <ctime>
#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
#if defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4457 4456 4005 4312)
#endif
#if defined(_WIN32)
# pragma warning(pop)
#endif
#if defined(_WIN32)
# if defined(APIENTRY)
# undef APIENTRY
# endif
# include <windows.h>
#endif
int main(int argc, char** argv)
{
nanogui::init();
nanogui::ref<ParticleSimApplication> app = new ParticleSimApplication();
app->draw_all();
app->set_visible(true);
nanogui::mainloop(10);
nanogui::shutdown();
return 0;
}