File mart3d.py added (mode: 100644) (index 0000000..f8020af) |
|
1 |
|
#!/usr/bin/env python |
|
2 |
|
# coding: utf-8 |
|
3 |
|
|
|
4 |
|
|
|
5 |
|
#č nazvy proměnných jsou v angličtině |
|
6 |
|
#č Ale komenty teda ne) |
|
7 |
|
|
|
8 |
|
import numpy as np |
|
9 |
|
from matplotlib import colors |
|
10 |
|
import matplotlib.tri as mtri |
|
11 |
|
|
|
12 |
|
def get_g_model_wireframe_data(shape_share, limits, space='R', ngrid=50): |
|
13 |
|
xmin, xmax, ymin, ymax = limits |
|
14 |
|
|
|
15 |
|
# endpoint=True by default |
|
16 |
|
# we'll get len(.) == ngrid |
|
17 |
|
# ngrid 51 is an default maximum for MPL |
|
18 |
|
# (otherwise MPL will downsample) |
|
19 |
|
x = np.linspace(xmin, xmax, ngrid) |
|
20 |
|
y = np.linspace(ymin, ymax, ngrid) |
|
21 |
|
X, Y = np.meshgrid(x, y) |
|
22 |
|
XY = np.vstack((X.flatten(), Y.flatten())).T |
|
23 |
|
|
|
24 |
|
if space == 'R': |
|
25 |
|
g_values = shape_share.gm(XY).g_values |
|
26 |
|
else: |
|
27 |
|
XY_sample = shape_share.f_model.new_sample(XY, space) |
|
28 |
|
g_values = shape_share.gm(XY_sample).g_values |
|
29 |
|
|
|
30 |
|
Z = g_values.reshape(ngrid, ngrid) |
|
31 |
|
return X, Y, Z |
|
32 |
|
|
|
33 |
|
|
|
34 |
|
#č napadlo mě, že bych mohl matplotlibovskému Axes |
|
35 |
|
#č přiřazovat (připsavat, zadávat) atribut 'space' |
|
36 |
|
#č Daválo by to smysl, ne? U všeho ostatního, u sample boksů |
|
37 |
|
#č nejsem jist, ale mám pocit, že na jedném sablotu někdo potřebuje |
|
38 |
|
#č kreslit z různejch prostoru. |
|
39 |
|
#č Zkrátka, funkce v tomto modulu požadujou aby |
|
40 |
|
#č ax.space a ax.sample_box byl nastaven! |
|
41 |
|
|
|
42 |
|
# ax.space and ax.sample_box attributes should (expected to) be set up! |
|
43 |
|
|
|
44 |
|
|
|
45 |
|
def limit_state_surface(ax3d, **kwargs): |
|
46 |
|
xmin, xmax = ax3d.get_xlim() |
|
47 |
|
ymin, ymax = ax3d.get_ylim() |
|
48 |
|
limits = (xmin, xmax, ymin, ymax) |
|
49 |
|
|
|
50 |
|
if 'rcount' in kwargs: |
|
51 |
|
ngrid = kwargs['rcount'] |
|
52 |
|
else: |
|
53 |
|
ngrid = 50 |
|
54 |
|
|
|
55 |
|
X, Y, Z = get_g_model_wireframe_data(ax3d.sample_box,\ |
|
56 |
|
limits, space=ax3d.space, ngrid=ngrid) |
|
57 |
|
|
|
58 |
|
# make a color map of fixed colors |
|
59 |
|
if 'cmap' not in kwargs: |
|
60 |
|
kwargs['cmap'] = colors.ListedColormap(['red', 'green']) |
|
61 |
|
if 'norm' not in kwargs: |
|
62 |
|
kwargs['norm'] = colors.BoundaryNorm([0], kwargs['cmap'].N, extend='both') |
|
63 |
|
|
|
64 |
|
return ax3d.plot_surface(X, Y, Z, **kwargs) |
|
65 |
|
|
|
66 |
|
|
|
67 |
|
def density_surface(ax3d, **kwargs): |
|
68 |
|
xmin, xmax = ax3d.get_xlim() |
|
69 |
|
ymin, ymax = ax3d.get_ylim() |
|
70 |
|
|
|
71 |
|
if 'rcount' in kwargs: |
|
72 |
|
ngrid = kwargs['rcount'] |
|
73 |
|
else: |
|
74 |
|
ngrid = 50 |
|
75 |
|
|
|
76 |
|
x = np.linspace(xmin, xmax, ngrid) |
|
77 |
|
y = np.linspace(ymin, ymax, ngrid) |
|
78 |
|
X, Y = np.meshgrid(x, y) |
|
79 |
|
XY = np.vstack((X.flatten(), Y.flatten())).T |
|
80 |
|
z = ax3d.sample_box.sample_pdf(XY, ax3d.space) |
|
81 |
|
Z = z.reshape(ngrid, ngrid) |
|
82 |
|
|
|
83 |
|
return ax3d.plot_surface(X, Y, Z, **kwargs) |
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
def tri_surface(ax3d, **kwargs): |
|
89 |
|
xy = getattr(ax3d.sample_box, ax3d.space) |
|
90 |
|
x, y = xy.T |
|
91 |
|
z = ax3d.sample_box.g_values |
|
92 |
|
|
|
93 |
|
return ax3d.plot_trisurf(x, y, z, **kwargs) |
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
# from Matplotlib sources: |
|
99 |
|
# "TODO: Support custom face colours" |
|
100 |
|
|
|
101 |
|
#def tri_colored_surface(ax3d, **kwargs): |
|
102 |
|
# xy = getattr(ax3d.sample_box, ax3d.space) |
|
103 |
|
# x, y = xy.T |
|
104 |
|
# z = ax3d.sample_box.g_values |
|
105 |
|
# tri = mtri.Triangulation(x, y) |
|
106 |
|
# colors = get_simplices_colors(ax3d.sample_box, tri.get_masked_triangles()) |
|
107 |
|
# return ax3d.plot_trisurf(tri, z, color=colors, **kwargs) |