From 0ed637ce31ed30df2eea689e5b4783cec18dd1e4 Mon Sep 17 00:00:00 2001 From: User01 <user01@uds-507831.ad.unistra.fr> Date: Fri, 4 Apr 2025 15:41:51 +0200 Subject: [PATCH] Maillage --- src/main.cpp | 22 ++++-------- src/msh.cpp | 98 ++++++++++++++++++++-------------------------------- 2 files changed, 45 insertions(+), 75 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c142920..a3097c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,13 @@ -#include <msh.h> +# include <msh.h> - -int main() -{ - Mesh m("../square.msh"); - //cout << m << endl; - m.writeGnuplot("gnu.plt"); - array<double, 2> p = {0.3, 0.4}; // Point à tester - int triIndex = m.findContainingTriangle(p); - - if (triIndex >= 0) { - cout << "Le point est dans le triangle " << triIndex << endl; - } else { - cout << "Le point n'est dans aucun triangle" << endl; - } + int main () + { + Mesh m ( " ../ square . msh " ) ; +// cout << m << endl ; + m . writeGnuplot ( " gnu . plt " ) ; return 0; + } diff --git a/src/msh.cpp b/src/msh.cpp index 80c4647..4be79c8 100644 --- a/src/msh.cpp +++ b/src/msh.cpp @@ -1,79 +1,57 @@ #include "msh.h" -#include <cassert> -int Mesh::findContainingTriangle(const array<double, 2> &p) const { - for (int i = 0; i < triangle.size(); ++i) { - const auto &tri = triangle[i]; - const auto &a = node[tri[0]]; - const auto &b = node[tri[1]]; - const auto &c = node[tri[2]]; +# include <cassert> - // Calcul des vecteurs - double detT = (b[0]-a[0])*(c[1]-a[1]) - (c[0]-a[0])*(b[1]-a[1]); - double alpha = ((b[0]-p[0])*(c[1]-p[1]) - (c[0]-p[0])*(b[1]-p[1])) / detT; - double beta = ((c[0]-p[0])*(a[1]-p[1]) - (a[0]-p[0])*(c[1]-p[1])) / detT; - double gamma = 1.0 - alpha - beta; - - if (alpha >= 0 && beta >= 0 && gamma >= 0) { - return i; // p est dans le triangle i - } +// classe maillage 2 d avec liste de noeuds et liste de triangles +Mesh :: Mesh ( string gmsh2_filename ){ + ifstream file = ifstream ( gmsh2_filename ) ; + if (! file . is_open () ) { + throw runtime_error ( " file not found " ) ; } - return -1; // Aucun triangle ne contient le point -} - -//classe maillage 2d avec liste de noeuds et liste de triangles -Mesh::Mesh(string gmsh2_filename) -{ - ifstream file = ifstream(gmsh2_filename); - if (!file.is_open()) { - throw runtime_error("file not found"); - } - string line; - while (getline(file, line)) + string line ; + while ( getline ( file , line ) ) { - if (line == "$Nodes") + if ( line == " $Nodes " ) { - int n; - file >> n; - node.resize(n); - for (int i = 0; i < n; i++) + int n ; + file >> n ; + node . resize ( n ) ; + for ( int i = 0; i < n ; i ++) { - int id; - double z; - file >> id; - file >> node[id - 1][0] >> node[id - 1][1] >> z; + int id ; + double z ; + file >> id ; + file >> node [ id - 1][0] >> node [ id - 1][1] >> z ; } } - if (line == "$Elements") + if ( line == " $Elements " ) { - int n; - file >> n; - for (int i = 0; i < n; i++) - { - int id, type, ntags; - file >> id >> type >> ntags; - for (int j = 0; j < ntags; j++) + int n ; + file >> n ; + for ( int i = 0; i < n ; i ++){ + int id , type , ntags ; + file >> id >> type >> ntags ; + for ( int j = 0; j < ntags ; j ++) { - int tag; - file >> tag; + int tag ; + file >> tag ; } - if (type == 2) + if ( type == 2) { - array<int, 3> t; - file >> t[0] >> t[1] >> t[2]; - t[0]--; - t[1]--; - t[2]--; - triangle.push_back(t); - } // sinon sauter la ligne (on ne connait pas sa taille) - else - { - string line; - getline(file, line); + array < int , 3 > t ; + file >> t [0] >> t [1] >> t [2]; + t [0] --; + t [1] --; + t [2] --; + triangle . push_back ( t ) ; + }// sinon sauter la ligne ( on ne connait pas sa taille ) + else{ + string line ; + getline ( file , line ) ; } } } } -}; +} // affichage dans un fichier lisible par gnuplot void Mesh::writeGnuplot(const string &filename) const { -- GitLab