|
| 1 | +import math |
| 2 | +import csv |
| 3 | +import itertools |
| 4 | + |
| 5 | +def ecrire_points(gm, fichier): |
| 6 | + points = gm.getVertices() |
| 7 | + |
| 8 | + with open(fichier + "_points.csv", "w", encoding="utf-8") as f: |
| 9 | + # Écriture d'un en-tête |
| 10 | + f.write("Nom_Point;X;Y;Z\n") |
| 11 | + |
| 12 | + # Parcours de tous les points |
| 13 | + for nom_point in points: |
| 14 | + coord = gm.getCoord(nom_point) |
| 15 | + x = coord.getX() |
| 16 | + y = coord.getY() |
| 17 | + z = coord.getZ() |
| 18 | + |
| 19 | + # Écriture de la ligne correspondante |
| 20 | + f.write(f"{nom_point};{x};{y};{z}\n") |
| 21 | + |
| 22 | + |
| 23 | +def distance(p1, p2): |
| 24 | + """Calcule la distance euclidienne entre deux points 3D.""" |
| 25 | + return math.sqrt((p1[0] - p2[0])**2 + |
| 26 | + (p1[1] - p2[1])**2 + |
| 27 | + (p1[2] - p2[2])**2) |
| 28 | + |
| 29 | + |
| 30 | +def lire_points(fichier): |
| 31 | + """Lit un fichier de points et retourne un dict {nom: (x, y, z)}""" |
| 32 | + points = {} |
| 33 | + with open(fichier + "_points.csv", newline='', encoding='utf-8') as f: |
| 34 | + lecteur = csv.DictReader(f, delimiter=';') |
| 35 | + for ligne in lecteur: |
| 36 | + nom = ligne['Nom_Point'] |
| 37 | + x = float(ligne['X']) |
| 38 | + y = float(ligne['Y']) |
| 39 | + z = float(ligne['Z']) |
| 40 | + points[nom] = (x, y, z) |
| 41 | + return points |
| 42 | + |
| 43 | + |
| 44 | +def ecrire_courbes(gm, fichier): |
| 45 | + with open(fichier + "_courbes.csv", "w", encoding="utf-8", newline='') as f: |
| 46 | + writer = csv.writer(f, delimiter=';') |
| 47 | + writer.writerow(["Nom_Courbe", "Points"]) |
| 48 | + |
| 49 | + for curve_name in gm.getCurves(): |
| 50 | + # Récupération de la liste des points de la courbe |
| 51 | + pts = gm.getInfos(curve_name, 1).vertices() |
| 52 | + writer.writerow([curve_name] + pts) |
| 53 | + |
| 54 | + |
| 55 | +def ecrire_surfaces(gm, fichier): |
| 56 | + """ |
| 57 | + Exporte les surfaces d'un objet gm dans un fichier texte. |
| 58 | + Chaque ligne contient : Nom_Surface;Point1;Point2;Point3;... |
| 59 | + """ |
| 60 | + |
| 61 | + ecrire_points(gm, fichier) |
| 62 | + ecrire_courbes(gm, fichier) |
| 63 | + |
| 64 | + with open(fichier + "_surfaces.csv", "w", encoding="utf-8", newline='') as f: |
| 65 | + writer = csv.writer(f, delimiter=';') |
| 66 | + writer.writerow(["Nom_Surface", "Points"]) |
| 67 | + |
| 68 | + for surf_name in gm.getSurfaces(): |
| 69 | + # Récupération de la liste des points de la surface |
| 70 | + pts = gm.getInfos(surf_name, 2).vertices() |
| 71 | + writer.writerow([surf_name] + pts) |
| 72 | + |
| 73 | + |
| 74 | +def lire_courbes(fichier): |
| 75 | + courbes = {} |
| 76 | + with open(fichier + "_courbes.csv", newline='', encoding='utf-8') as f: |
| 77 | + lecteur = csv.reader(f, delimiter=';') |
| 78 | + next(lecteur) # ignorer l'en-tête |
| 79 | + for ligne in lecteur: |
| 80 | + nom_courbe = ligne[0] |
| 81 | + points = ligne[1:] |
| 82 | + courbes[nom_courbe] = points |
| 83 | + return courbes |
| 84 | + |
| 85 | + |
| 86 | +def lire_surfaces(fichier): |
| 87 | + """ |
| 88 | + Lit un fichier de surfaces et retourne un dict : |
| 89 | + {nom_surface: [liste_points]} |
| 90 | + """ |
| 91 | + surfaces = {} |
| 92 | + with open(fichier + "_surfaces.csv", newline='', encoding='utf-8') as f: |
| 93 | + lecteur = csv.reader(f, delimiter=';') |
| 94 | + next(lecteur) # ignorer l'en-tête |
| 95 | + for ligne in lecteur: |
| 96 | + nom_surface = ligne[0] |
| 97 | + points = ligne[1:] |
| 98 | + surfaces[nom_surface] = points |
| 99 | + return surfaces |
| 100 | + |
| 101 | + |
| 102 | +def ecrire_correspondances(fichier1, fichier2, epsilon=1e-8): |
| 103 | + # Trouve les correspondances entre deux fichiers de points. |
| 104 | + pts1 = lire_points(fichier1) |
| 105 | + pts2 = lire_points(fichier2) |
| 106 | + corr_pts = {} |
| 107 | + for nom1, coord1 in pts1.items(): |
| 108 | + candidats = [] |
| 109 | + for nom2, coord2 in pts2.items(): |
| 110 | + if math.dist(coord1, coord2) <= epsilon: |
| 111 | + candidats.append(nom2) |
| 112 | + corr_pts[nom1] = candidats |
| 113 | + print(corr_pts) |
| 114 | + |
| 115 | + # Trouve les correspondances entre les courbes. |
| 116 | + cvr1 = lire_courbes(fichier1) |
| 117 | + cvr2 = lire_courbes(fichier2) |
| 118 | + |
| 119 | + corr_crvs = [] |
| 120 | + for nom1, pts1 in cvr1.items(): |
| 121 | + # Liste de candidats pour chaque point |
| 122 | + candidats_par_point = [corr_pts[p] for p in pts1] |
| 123 | + # Test de toutes les combinaisons possibles |
| 124 | + for combinaison in itertools.product(*candidats_par_point): |
| 125 | + if set(combinaison) in [set(s) for s in cvr2.values()]: |
| 126 | + # Récupérer le nom de la courbe correspondante dans cvr2 |
| 127 | + for nom2, pts2 in cvr2.items(): |
| 128 | + if set(combinaison) == set(pts2): |
| 129 | + corr_crvs.append((nom1, nom2)) |
| 130 | + break |
| 131 | + break |
| 132 | + print(corr_crvs) |
| 133 | + |
| 134 | + # Trouve les correspondances entre les surfaces |
| 135 | + surf1 = lire_surfaces(fichier1) |
| 136 | + surf2 = lire_surfaces(fichier2) |
| 137 | + |
| 138 | + corr_surfs = [] |
| 139 | + for nom1, pts1 in surf1.items(): |
| 140 | + # Liste de candidats pour chaque point |
| 141 | + candidats_par_point = [corr_pts[p] for p in pts1] |
| 142 | + # Test de toutes les combinaisons possibles |
| 143 | + for combinaison in itertools.product(*candidats_par_point): |
| 144 | + if set(combinaison) in [set(s) for s in surf2.values()]: |
| 145 | + # Récupérer le nom de la surface correspondante dans surf2 |
| 146 | + for nom2, pts2 in surf2.items(): |
| 147 | + if set(combinaison) == set(pts2): |
| 148 | + corr_surfs.append((nom1, nom2)) |
| 149 | + break |
| 150 | + break |
| 151 | + print(corr_surfs) |
0 commit comments