diff --git a/square.msh b/square.msh index 3aab3e9d699c60f444fe845a75ab61bef7664452..76ae375e17dcc0ec16b97473ae2e155af64606e2 100644 --- a/square.msh +++ b/square.msh @@ -12,7 +12,7 @@ $Nodes 7 0.2999999999992664 0 0 8 0.3999999999989749 0 0 9 0.4999999999986943 0 0 -10 0.5999999999989468 0 0 +10 0.5999999999989468 0 0build 11 0.69999999999921 0 0 12 0.7999999999994734 0 0 13 0.8999999999997368 0 0 diff --git a/src/main.cpp b/src/main.cpp index 9e8851aac5991d764ccf7647e774f991c9af2332..c1429206ff6fa86586c82d63ec2e519a2323537a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,5 +6,17 @@ 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; + } + return 0; -} \ No newline at end of file +} + + + \ No newline at end of file diff --git a/src/msh.cpp b/src/msh.cpp index 5ddb7b1bf13177199e0ea0bf2d85d300d35148d0..80c464734ead0ab6d35badedc443c8d2e78d19a5 100644 --- a/src/msh.cpp +++ b/src/msh.cpp @@ -1,7 +1,26 @@ #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]]; -// classe maillage 2d avec liste de noeuds et liste de triangles + // 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 + } + } + 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); diff --git a/src/msh.h b/src/msh.h index 4ee327cce3fc6108f6556cafb6a1f8546547110f..7c0a820f576733ca8e87f8e698731724a73fbfc7 100644 --- a/src/msh.h +++ b/src/msh.h @@ -19,6 +19,9 @@ public: friend ostream &operator<<(ostream &os, const Mesh &m); // affichage dans un fichier lisible par gnuplot void writeGnuplot(const string &filename) const; + int findContainingTriangle(const array<double, 2> &p) const; + }; + #endif