| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from PyQt5.QtWidgets import (QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, |
| 25 |
QGroupBox) |
| 26 |
|
| 27 |
from mpl_toolkits.mplot3d import Axes3D |
| 28 |
from matplotlib import cm |
| 29 |
from matplotlib.axes import Axes |
| 30 |
from matplotlib.colors import LinearSegmentedColormap |
| 31 |
from numpy import max as nmax, min as nmin, meshgrid |
| 32 |
|
| 33 |
from ...GUI.qtlib import BusyCursor |
| 34 |
from ...GUI.menu import MenuBar |
| 35 |
from ...GUI.panel import Panel_API_graphique |
| 36 |
from ...pylib import fullrange, eval_safe |
| 37 |
from ...pylib.securite import dictionnaire_builtins |
| 38 |
from ...mathlib import end_user_functions |
| 39 |
from ...mathlib.parsers import traduire_formule |
| 40 |
|
| 41 |
|
| 42 |
class SurfacesMenuBar(MenuBar): |
| 43 |
def __init__(self, panel): |
| 44 |
MenuBar.__init__(self, panel) |
| 45 |
|
| 46 |
self.ajouter("Fichier", ["exporter"], ['session'], ["presse-papier"], ["quitter"]) |
| 47 |
self.ajouter("Affichage", ["onglet"], ["plein_ecran"]) |
| 48 |
self.ajouter("Outils", ["debug"], ["options"]) |
| 49 |
self.ajouter("?") |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class MyAxes3D(Axes3D): |
| 54 |
def draw(self, renderer): |
| 55 |
|
| 56 |
self.axesPatch.draw(renderer) |
| 57 |
self._frameon = False |
| 58 |
|
| 59 |
|
| 60 |
self.M = self.get_proj() |
| 61 |
renderer.M = self.M |
| 62 |
renderer.vvec = self.vvec |
| 63 |
renderer.eye = self.eye |
| 64 |
renderer.get_axis_position = self.get_axis_position |
| 65 |
|
| 66 |
|
| 67 |
zlist = [(col.do_3d_projection(renderer), col) \ |
| 68 |
for col in self.collections] |
| 69 |
zlist.sort(reverse=True) |
| 70 |
for i, (z, col) in enumerate(zlist): |
| 71 |
col.zorder = getattr(col, '_force_zorder', i) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
zlist = [(patch.do_3d_projection(renderer), patch) \ |
| 76 |
for patch in self.patches] |
| 77 |
zlist.sort(reverse=True) |
| 78 |
for i, (z, patch) in enumerate(zlist): |
| 79 |
patch.zorder = i |
| 80 |
|
| 81 |
self.w_xaxis.draw(renderer) |
| 82 |
self.w_yaxis.draw(renderer) |
| 83 |
self.w_zaxis.draw(renderer) |
| 84 |
Axes.draw(self, renderer) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
class Surfaces(Panel_API_graphique): |
| 89 |
|
| 90 |
titre = "Surfaces" |
| 91 |
|
| 92 |
def __init__(self, *args, **kw): |
| 93 |
Panel_API_graphique.__init__(self, *args, **kw) |
| 94 |
|
| 95 |
self.canvas.fixe = True |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
self.couleurs = [ |
| 101 |
[0.0, 0.0, 0.5], |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
[0.0, 0.33333333333333326, 1.0], |
| 107 |
|
| 108 |
|
| 109 |
[0.0, 0.83333333333333326, 1.0], |
| 110 |
[0.080645161290322731, 1.0, 0.88709677419354827], |
| 111 |
|
| 112 |
|
| 113 |
[0.4838709677419355, 1.0, 0.48387096774193528], |
| 114 |
|
| 115 |
|
| 116 |
[0.88709677419354827, 1.0, 0.080645161290322509], |
| 117 |
|
| 118 |
[1.0, 0.82, 0.0], |
| 119 |
|
| 120 |
[1.0, 0.59259259259259256, 0.0], |
| 121 |
|
| 122 |
[1.0, 0.28395061728395099, 0.0], |
| 123 |
|
| 124 |
[0.8787878787878789, 0.0, 0.0], |
| 125 |
[0.68939393939393945, 0.0, 0.0], |
| 126 |
[0.5, 0, 0], |
| 127 |
] |
| 128 |
self._Z = None |
| 129 |
|
| 130 |
self.entrees = QVBoxLayout() |
| 131 |
|
| 132 |
|
| 133 |
ligne = QHBoxLayout() |
| 134 |
box = QGroupBox("Equation") |
| 135 |
box.setLayout(ligne) |
| 136 |
ligne.addWidget(QLabel("Z = ")) |
| 137 |
self.equation = QLineEdit(self) |
| 138 |
self.equation.setMinimumWidth(160) |
| 139 |
self.equation.returnPressed.connect(self.affiche) |
| 140 |
ligne.addWidget(self.equation) |
| 141 |
self.entrees.addWidget(box) |
| 142 |
|
| 143 |
box = QGroupBox("Abscisse") |
| 144 |
liste = QVBoxLayout() |
| 145 |
box.setLayout(liste) |
| 146 |
|
| 147 |
ligne = QHBoxLayout() |
| 148 |
ligne.addWidget(QLabel("Xmin")) |
| 149 |
self.xmin = QLineEdit(self) |
| 150 |
self.xmin.setMinimumWidth(160) |
| 151 |
self.xmin.setText("-5") |
| 152 |
self.xmin.returnPressed.connect(self.affiche) |
| 153 |
ligne.addWidget(self.xmin) |
| 154 |
liste.addLayout(ligne) |
| 155 |
|
| 156 |
ligne = QHBoxLayout() |
| 157 |
ligne.addWidget(QLabel("Xmax")) |
| 158 |
self.xmax = QLineEdit(self) |
| 159 |
self.xmax.setMinimumWidth(160) |
| 160 |
self.xmax.setText("5") |
| 161 |
self.xmax.returnPressed.connect(self.affiche) |
| 162 |
ligne.addWidget(self.xmax) |
| 163 |
liste.addLayout(ligne) |
| 164 |
|
| 165 |
ligne = QHBoxLayout() |
| 166 |
ligne.addWidget(QLabel("Pas")) |
| 167 |
self.pasX = QLineEdit(self) |
| 168 |
self.pasX.setMinimumWidth(160) |
| 169 |
self.pasX.returnPressed.connect(self.affiche) |
| 170 |
ligne.addWidget(self.pasX) |
| 171 |
liste.addLayout(ligne) |
| 172 |
|
| 173 |
self.entrees.addWidget(box) |
| 174 |
|
| 175 |
box = QGroupBox("Ordonnée") |
| 176 |
liste = QVBoxLayout() |
| 177 |
box.setLayout(liste) |
| 178 |
|
| 179 |
ligne = QHBoxLayout() |
| 180 |
ligne.addWidget(QLabel("Ymin")) |
| 181 |
self.ymin = QLineEdit(self) |
| 182 |
self.ymin.setMinimumWidth(160) |
| 183 |
self.ymin.setText("-5") |
| 184 |
self.ymin.returnPressed.connect(self.affiche) |
| 185 |
ligne.addWidget(self.ymin) |
| 186 |
liste.addLayout(ligne) |
| 187 |
|
| 188 |
ligne = QHBoxLayout() |
| 189 |
ligne.addWidget(QLabel("Ymax")) |
| 190 |
self.ymax = QLineEdit(self) |
| 191 |
self.ymax.setMinimumWidth(160) |
| 192 |
self.ymax.setText("5") |
| 193 |
self.ymax.returnPressed.connect(self.affiche) |
| 194 |
ligne.addWidget(self.ymax) |
| 195 |
liste.addLayout(ligne) |
| 196 |
|
| 197 |
ligne = QHBoxLayout() |
| 198 |
ligne.addWidget(QLabel("Pas")) |
| 199 |
self.pasY = QLineEdit(self) |
| 200 |
self.pasY.setMinimumWidth(160) |
| 201 |
self.pasY.returnPressed.connect(self.affiche) |
| 202 |
ligne.addWidget(self.pasY) |
| 203 |
liste.addLayout(ligne) |
| 204 |
|
| 205 |
self.entrees.addWidget(box) |
| 206 |
|
| 207 |
box = QGroupBox("Seuils") |
| 208 |
liste = QVBoxLayout() |
| 209 |
box.setLayout(liste) |
| 210 |
|
| 211 |
ligne = QHBoxLayout() |
| 212 |
|
| 213 |
self.seuils = QLineEdit(self) |
| 214 |
self.seuils.setMinimumWidth(160) |
| 215 |
self.seuils.returnPressed.connect(self.affiche) |
| 216 |
ligne.addWidget(self.seuils) |
| 217 |
liste.addLayout(ligne) |
| 218 |
) |
| 219 |
|
| 220 |
self.entrees.addWidget(box) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
self.sizer = QHBoxLayout() |
| 226 |
self.sizer.addWidget(self.canvas, 1) |
| 227 |
self.sizer.addLayout(self.entrees) |
| 228 |
self.setLayout(self.sizer) |
| 229 |
self.adjustSize() |
| 230 |
self.ax3d = MyAxes3D(self.canvas.figure) |
| 231 |
self.plt = self.canvas.figure.axes.append(self.ax3d) |
| 232 |
self.initialisation_terminee = True |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
def _sauvegarder(self, fgeo, feuille=None): |
| 241 |
Panel_API_graphique._sauvegarder(self, fgeo, feuille) |
| 242 |
e |
| 243 |
return |
| 244 |
fgeo.contenu["Courbe"] = [{"Y" : [self.equations[i].text()], "intervalle" : [self.intervalles[i].text()], "active" : [str(self.boites[i].text())]} for i in range(self.nombre_courbes)] |
| 245 |
|
| 246 |
|
| 247 |
def _ouvrir(self, fgeo): |
| 248 |
Panel_API_graphique._ouvrir(self, fgeo) |
| 249 |
e |
| 250 |
return |
| 251 |
if "Courbe" in fgeo.contenu: |
| 252 |
for i in range(min(len(fgeo.contenu["Courbe"]), self.nombre_courbes)): |
| 253 |
self.equations[i].SetValue(fgeo.contenu["Courbe"][i]["Y"][0]) |
| 254 |
self.intervalles[i].SetValue(fgeo.contenu["Courbe"][i]["intervalle"][0]) |
| 255 |
self.boites[i].SetValue(fgeo.contenu["Courbe"][i]["active"][0] == "True") |
| 256 |
self.affiche() |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
def _affiche(self): |
| 261 |
|
| 262 |
|
| 263 |
if not self.equation.text().strip(): |
| 264 |
return |
| 265 |
xmin = eval_safe(self.xmin.text().strip()) |
| 266 |
xmax = eval_safe(self.xmax.text().strip()) |
| 267 |
ymin = eval_safe(self.ymin.text().strip()) |
| 268 |
ymax = eval_safe(self.ymax.text().strip()) |
| 269 |
|
| 270 |
pasX = self.pasX.text().strip() |
| 271 |
if not pasX: |
| 272 |
pasX = self._param_.resolution*(xmax - xmin) |
| 273 |
else: |
| 274 |
pasX = eval_safe(pasX) |
| 275 |
if pasX < self._param_.resolution_minimale*max(xmax - xmin, ymax - ymin): |
| 276 |
pasX = self._param_.resolution_minimale*max(xmax - xmin, ymax - ymin) |
| 277 |
self.canvas.message("Attention, le pas est trop petit !") |
| 278 |
pasY = self.pasY.text().strip() |
| 279 |
if not pasY: |
| 280 |
pasY = self._param_.resolution*(xmax - xmin) |
| 281 |
else: |
| 282 |
pasY = eval_safe(pasY) |
| 283 |
if pasY < self._param_.resolution_minimale*max(xmax - xmin, ymax - ymin): |
| 284 |
pasY = self._param_.resolution_minimale*max(xmax - xmin, ymax - ymin) |
| 285 |
self.canvas.message("Attention, le pas est trop petit !") |
| 286 |
with BusyCursor(): |
| 287 |
X = fullrange(xmin, xmax, pasX) |
| 288 |
Y = fullrange(ymin, ymax, pasY) |
| 289 |
X, Y = meshgrid(X, Y) |
| 290 |
dico = vars(end_user_functions).copy() |
| 291 |
dico.update({'x': X, 'X': X, 'Y': Y, 'y': Y}) |
| 292 |
dico.update(dictionnaire_builtins) |
| 293 |
formule = traduire_formule(self.equation.text(), dico) |
| 294 |
self._Z = Z = eval(formule, dico) + 0*X |
| 295 |
|
| 296 |
seuils_txt = self.seuils.text().strip() |
| 297 |
if seuils_txt: |
| 298 |
rs |
| 299 |
seuils = sorted(float(seuil) for seuil in seuils_txt.split(' ')) |
| 300 |
cmap = self._creer_cmap(seuils) |
| 301 |
else: |
| 302 |
cmap = cm.jet |
| 303 |
|
| 304 |
self.ax3d.clear() |
| 305 |
self.polyc = self.ax3d.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = cmap) |
| 306 |
self.polyc.set_linewidth(self._param_.epaisseur_grillage) |
| 307 |
return |
| 308 |
if seuils_txt: |
| 309 |
|
| 310 |
self.cs = self.ax3d.contour(X, Y, Z, cmap = cmap, levels = seuils, linewidths = 2*self._param_.epaisseur_grillage + 1) |
| 311 |
for collection in self.cs.collections: |
| 312 |
collection._force_zorder = 100 |
| 313 |
|
| 314 |
|
| 315 |
def _creer_cmap(self, seuils): |
| 316 |
zmax = nmax(self._Z) |
| 317 |
zmin = nmin(self._Z) |
| 318 |
delta = zmax - zmin |
| 319 |
e |
| 320 |
if delta: |
| 321 |
a = 1/delta |
| 322 |
b = -zmin/delta |
| 323 |
seuils = [0] + [a*z + b for z in seuils if zmin < z < zmax] + [1] |
| 324 |
print(seuils) |
| 325 |
cdict = {'red': [], 'green': [], 'blue': []} |
| 326 |
def add_col(val, color1, color2): |
| 327 |
cdict['red'].append((val, color1[0], color2[0])) |
| 328 |
cdict['green'].append((val, color1[1], color2[1])) |
| 329 |
cdict['blue'].append((val, color1[2], color2[2])) |
| 330 |
|
| 331 |
n = len(self.couleurs) |
| 332 |
for i, seuil in enumerate(seuils): |
| 333 |
add_col(seuil, self.couleurs[(i - 1)%n], self.couleurs[i%n]) |
| 334 |
return LinearSegmentedColormap('seuils', cdict, 256) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
def _colors_from_cmap(cmap, n): |
| 341 |
"Retourne 'n' couleurs régulièrement espacées de cmap." |
| 342 |
cdict = cmap._segmentdata |
| 343 |
vals = fullrange(0, 1, 1/(n - 1)) |
| 344 |
l = [] |
| 345 |
for val in vals: |
| 346 |
l.append([]) |
| 347 |
for c in ('red', 'green', 'blue'): |
| 348 |
gradient = cdict[c] |
| 349 |
for i, triplet in enumerate(gradient): |
| 350 |
if triplet[0] > val: |
| 351 |
val0, tmp, x0 = gradient[i - 1] |
| 352 |
val1, x1, tmp = triplet |
| 353 |
a = (x1 - x0)/(val1 - val0) |
| 354 |
b = x0 - a*val0 |
| 355 |
x = a*val + b |
| 356 |
l[-1].append(x) |
| 357 |
break |
| 358 |
else: |
| 359 |
l[-1].append(gradient[-1][1]) |
| 360 |
return l |
| 361 |
|
| 362 |
t__ |
| 363 |
|