Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1415:projekte_im_wintersemester_2014_15:nuetzliche_programmschnipsel [2014/11/27 10:20] stefanborn angelegt |
ws1415:projekte_im_wintersemester_2014_15:nuetzliche_programmschnipsel [2016/05/10 14:46] (aktuell) |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
===== Nützliche Programmschnipsel ===== | ===== Nützliche Programmschnipsel ===== | ||
- | ==== Dreiecke zeichnen ==== | + | ==== Test für Objektorientierte Dreiecke ==== |
+ | <code Python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | from mpl_toolkits.mplot3d import Axes3D | ||
+ | import matplotlib.tri as mtri | ||
+ | from scipy.spatial import Delaunay | ||
+ | |||
+ | |||
+ | |||
+ | xinput=[1,2,3,4] | ||
+ | yinput=[1,2,3,4] | ||
+ | |||
+ | z=[] | ||
+ | x=[] | ||
+ | y=[] | ||
+ | |||
+ | |||
+ | def do(xinput,yinput): | ||
+ | xlange=len(xinput) | ||
+ | ylange=len(yinput) | ||
+ | for i in range((xlange*ylange)): | ||
+ | z.append(np.random.uniform(1,3,size=None)) | ||
+ | |||
+ | for i in range(xlange): | ||
+ | for ii in range(xlange): | ||
+ | x.append(i) | ||
+ | |||
+ | for i in range(ylange): | ||
+ | for ii in range(ylange): | ||
+ | y.append(ii) | ||
+ | do(xinput,yinput) | ||
+ | |||
+ | Array=np.array([x,y]).T | ||
+ | def triang(x,y,z): | ||
+ | global tri | ||
+ | |||
+ | tri = Delaunay(Array) | ||
+ | fig = plt.figure() | ||
+ | ax = fig.add_subplot(1, 1, 1, projection='3d') | ||
+ | ax.plot_trisurf(x, y, z, triangles=tri.simplices, cmap=plt.cm.Spectral) | ||
+ | |||
+ | triang(x,y,z) | ||
+ | dreiecke=[] | ||
+ | def eckpunkte(simplices): | ||
+ | for bla in range(len(simplices)): | ||
+ | dreieck=[] | ||
+ | sxyz=tri.simplices[bla] | ||
+ | #x | ||
+ | sx=sxyz[0] | ||
+ | ssx=Array[sx] | ||
+ | dreieck.append(ssx) | ||
+ | |||
+ | #y | ||
+ | sy=sxyz[1] | ||
+ | ssy=Array[sy] | ||
+ | dreieck.append(ssy) | ||
+ | |||
+ | #z | ||
+ | sz=sxyz[2] | ||
+ | ssz=Array[sz] | ||
+ | dreieck.append(ssz) | ||
+ | dreiecke.append(dreieck) | ||
+ | eckpunkte(tri.simplices) | ||
+ | print dreiecke | ||
+ | |||
+ | |||
+ | plt.show() | ||
+ | </code> | ||
+ | ==== Funktionierende Triangulierung ==== | ||
+ | |||
+ | <code Python> | ||
+ | def Show(NAME): | ||
+ | |||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | from mpl_toolkits.mplot3d import Axes3D | ||
+ | import matplotlib.tri as mtri | ||
+ | from scipy.spatial import Delaunay | ||
+ | |||
+ | rangey=[] | ||
+ | rangex=[] | ||
+ | z=[] | ||
+ | myArray = np.loadtxt(NAME) | ||
+ | zeilen,spalten=myArray.shape | ||
+ | |||
+ | for i in range(zeilen): | ||
+ | for ii in range(zeilen): | ||
+ | rangex.append(i) | ||
+ | |||
+ | for i in range(spalten): | ||
+ | for i in range(spalten): | ||
+ | rangey.append(i) | ||
+ | reader=open(NAME,'r') | ||
+ | z1=reader.read() | ||
+ | reader.close() | ||
+ | z1=z1.replace("\n","") | ||
+ | z1=(z1.split()) | ||
+ | |||
+ | for word in z1: | ||
+ | z.append(word) | ||
+ | z=map(int,z) | ||
+ | |||
+ | tri = Delaunay(np.array([rangex,rangey]).T) | ||
+ | fig = plt.figure() | ||
+ | ax = fig.add_subplot(1, 1, 1, projection='3d') | ||
+ | ax.plot_trisurf(rangex, rangey, z, triangles=tri.simplices, cmap=plt.cm.BrBG) | ||
+ | plt.show() | ||
+ | print len(tri.simplices) | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | Show("montblanc1.asc") | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
+ | ==== 3d-Punkte zeichnen ==== | ||
+ | |||
+ | |||
+ | <code Python> | ||
+ | import matplotlib.pyplot as plt | ||
+ | from mpl_toolkits.mplot3d import Axes3D | ||
+ | import numpy as np | ||
+ | |||
+ | myArray = np.loadtxt("etopo1_bedrock.asc", skiprows=6) | ||
+ | z,s=myArray.shape | ||
+ | |||
+ | x=np.zeros((z,s)) | ||
+ | for i in range(z): | ||
+ | x[i]=i | ||
+ | |||
+ | y=np.zeros((z,s)) | ||
+ | for i in range(s): | ||
+ | y[:,i]=i | ||
+ | z=myArray | ||
- | ==== Noch eine Variante ==== | + | fig = plt.figure() |
+ | ax = fig.add_subplot(111, projection='3d') | ||
+ | ax.scatter(x, y, z, zdir='z', c= 'red') | ||
+ | plt.show() | ||
+ | </code> | ||
+ | ==== 3d-Dreiecke zeichnen ==== | ||
<code python> | <code python> | ||
- | #!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | # | + | |
- | # delaunay.py | + | |
- | # | + | |
- | # Copyright 2014 Stefan Born <born@math.tu-berlin.de> | + | |
- | # | + | |
- | # This program is free software; you can redistribute it and/or modify | + | |
- | # it under the terms of the GNU General Public License as published by | + | |
- | # the Free Software Foundation; either version 2 of the License, or | + | |
- | # (at your option) any later version. | + | |
- | # | + | |
- | # This program is distributed in the hope that it will be useful, | + | |
- | # but WITHOUT ANY WARRANTY; without even the implied warranty of | + | |
- | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | + | |
- | # GNU General Public License for more details. | + | |
- | # | + | |
- | # You should have received a copy of the GNU General Public License | + | |
- | # along with this program; if not, write to the Free Software | + | |
- | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | + | |
- | # MA 02110-1301, USA. | + | |
- | # | + | |
- | # | + | |
import numpy as np | import numpy as np | ||
+ | from scipy.spatial import Delaunay | ||
+ | import mpl_toolkits.mplot3d as mplot3d | ||
+ | import matplotlib.colors as colors | ||
+ | import pylab as pl | ||
### erzeuge ein Gitter von x,y-Werten mit zugehörigen z-Werten | ### erzeuge ein Gitter von x,y-Werten mit zugehörigen z-Werten | ||
Zeile 47: | Zeile 169: | ||
### Berechne Delaunay-Triangulierung des Gitters | ### Berechne Delaunay-Triangulierung des Gitters | ||
- | from scipy.spatial import Delaunay | + | |
tri = Delaunay(points2d) | tri = Delaunay(points2d) | ||
Zeile 59: | Zeile 181: | ||
### mplot3d.art3d.Poly3DCollection(...) werden | ### mplot3d.art3d.Poly3DCollection(...) werden | ||
- | import mpl_toolkits.mplot3d as mplot3d | + | |
- | import matplotlib.colors as colors | + | |
- | import pylab as pl | + | |
- | import numpy as np | + | |
ax = mplot3d.Axes3D(pl.figure()) | ax = mplot3d.Axes3D(pl.figure()) |