The author introduces himself: Da Shuangge, The small UP owner of station b, Live programming + red Police three, python 1 to 1 tutor.
This blog is a lesson plan for one-to-one tutoring of students on Python code. It is made public with the permission of the students.
The current specific tutoring content is PyGeM
This article is an explanation and analysis ofPyGeM tutorial-1-ffd
Tutorial 1 shows how to apply the free-form deformation to mesh nodes.
0 preparation
Prepare software
Must be installed
- python 3.8 or above
https://www.python.org/downloads/ - conda
conda is divided into Anaconda and Miniconda
If the computer performance is good enough, it is recommended to install Anaconda (this is a big software)https://docs.conda.io/en/latest/miniconda.html Miniconda: https://www.anaconda .com/products/individual Anaconda:
If the performance is average or you don’t want to install too many big things, you can try to install Miniconda
Recommended installation
3. Pycharm
https://blog.csdn.net/python1639er/article/details/122629870
Install third-party library pygem
PyGeM cannot be installed directly using
pip
. You need to follow the steps below. (To be precise, the pip installation does not meet the requirements and the version is too old)
It is recommended to use Pycharm to create the project first,
and then install it in Pycharm's Terminal. (This is also equivalent to the command line)
When creating a Pycharm projectPython Interpreter
it is recommended to choose the following
1. Choose Conda< a i=5> 2Choose according to the location of Conda on your computer
Install dependent libraries first
- The ones that can be installed directly through pip are
numpy, scipy, matplotlib, sphinx, pytest
The installation command is as follows (run in the command line)
pip install numpy
pip install scipy
pip install matplotlib
pip install sphinx
pip install pytest
If you cannot install it, you may need to use a mirror.
Specific recommended reading:
Dashuang Python Introduction Tutorial 8-2 Python Library (Library) and Packages (Package), module (Module)’s third-party library part.
- Manual installation
pythonocc-core
To use the submodulesubmodule
you need to install this.
You can use theconda
command to install
. The installation command is as follows (run in the command line)
conda install -c conda-forge pythonocc-core=7.4.0
- Install PyGeM manually
The installation command is as follows (run in the command line)
git clone https://github.com/mathLab/PyGeM
Then switch the current path of the console to the folder of this project
If you run the above code in the Terminal of the Pycharm project,
At this time, the command to switch paths in Terminal is
cd PyGeM
Then enter the following command to install PyGeM
python setup.py install
It’s basically installed here.
1 beginning
first paragraph
Original text and translation
Free Form Deformation on a sphere
Free Form Deformation on a sphere
In this tutorial we will show the typical workflow to perform a deformation on a generic geometry using the free-form deformation method implemented within PyGeM.
In this tutorial, we will show a typical deformation of generic geometry using the free-form deformation (FFD) method implemented within PyGeM. work process.
A brief theoretical overview of the method is introduced in thepygem.ffd
module,
pygem.ffd
,
while in the README you can find several references that focus on FFD.
And inREADME
you can find some references focusing on FFD.
First of all, we import the required PyGeM class and we set matplotlib for 3D plots.
First of all, we import the required PyGeM class and we set matplotlib for 3D plots.
The version of PyGeM we are using in this tutorial is the 2.0.0.
The version of PyGeM we are using in this tutorial is the 2.0.0.
import
Original code
%matplotlib inline
import numpy as np
import mpl_toolkits.mplot3d
import matplotlib.pyplot as plt
import pygem
print(pygem.__version__)
from pygem import FFD
The first line%matplotlib inline
is for Ipython compiler (can be used for embedded drawing in web pages),
is usually Jupyter or colab< /span>ipynb
Notebook will be used.
Generally, .py files do not need this command. It is best to comment or delete it.
Dashuang Python introductory tutorial 8-1 Importimport
import math
print(math.pi)
from math import pi
print(pi)
import math as m
print(m.pi)
2 mesh_points
Then, the other ingredient is the original geometry we want to deform.
Then, the other ingredient is the original geometry we want to deform.
In this simple tutorial we just span some points around a sphere and morph their coordinates using the FFD class.
In this simple tutorial, we just span a Some points around the sphere and change their coordinates using the FFD class.
def mesh_points(num_pts = 2000):
indices = np.arange(0, num_pts, dtype=float) + 0.5
phi = np.arccos(1 - 2*indices/num_pts)
theta = np.pi * (1 + 5**0.5) * indices
return np.array([np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)]).T
mesh = mesh_points()
plt.figure(figsize=(8,8)).add_subplot(111, projection='3d').scatter(*mesh.T);
plt.show()
phi
numpy.arange(start, stop, dtype = None)
Return evenly spaced values within a given interval.
Return evenly spaced values within a given interval.
>>> import numpy as np
>>> np.arange(0, 5, dtype=float)
array([0., 1., 2., 3., 4.])
np.arange(0, 5, dtype=float) + 0.5
array([0.5, 1.5, 2.5, 3.5, 4.5])
>>> np.arange(0, 2000, dtype=float) + 0.5
array([5.0000e-01, 1.5000e+00, 2.5000e+00, ..., 1.9975e+03, 1.9985e+03,
1.9995e+03])
Aboute
: Da Shuang Python Introduction Tutorial 1-2 Numbers and Strings
num_pts
Take the default value2000
indices = np.arange(0, 2000, dtype=float) + 0.5
and get an array of 0.5, 1.5, 2.5 until 1999.5
indices/num_pts
gets an array of 1 divided into num_pts parts
1 - 2* indices/num_pts
gets an array of [-1, 1]
divided into num_pts parts
np.arccos(1 - 2*indices/num_pts)
obtainable is [ 0 , π ] [0, \pi] [0,An array composed of numbers between π] (not equally separated at this time)
is the abscissa of points on the curve corresponding to equally spaced points on the y-axis.
phi corresponds to the spherical coordinate system ϕ \phi ϕ
For details, see the ellipsoid formula below
theta
np.pi * (1 + 5**0.5) * indices
This is where you are π ∗ ( 1 + 5 ) \pi * (1+ \sqrt 5) Pi∗(1+5)
This is used to get the coordinates,
It is probably the golden spiral method
I don’t understand the detailed principle, a>
I won’t go into detail here, as long as it works.
If you are interested, you can check this out:
evenly-distributing-n-points-on-a-sphere
return
np.array([np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)])
will get a 3 rowsnum_pts
column
[[ 0.05111927 -0.21801231 0.29980505 -0.19082906 ...... -0.17335079 0.14097948]
[-0.13147935 0.10756687 0.08727502 -0.31414053 ...... 0.17043915 -0.0049786 ]
[ 0.99 0.97 0.95 0.93 ...... -0.97 -0.99 ]]
additional .T
obtained num_pts
row 3 column
[[ 0.05111927 -0.13147935 0.99 ]
[-0.21801231 0.10756687 0.97 ]
[ 0.29980505 0.08727502 0.95 ]
[-0.19082906 -0.31414053 0.93 ]
......
[-0.17335079 0.17043915 -0.97 ]
[ 0.14097948 -0.0049786 -0.99 ]]
Official official
Spherical coordinate system, where φ \varphi φhere is the sky, θ \theta θObserve the equation, which is the equivalent of the equation:
x = a sin φ cos θ ; y = b sin ϕ sin θ ; z = c cos φ ; 0 ≤ φ ≤ 18 0 ∘ ; 0 ≤ θ ≤ 36 0 ∘ ; x = a \sin \varphi \cos \theta; \\ y = b \sin \height \sin \theta;\\ z = c \cos \height;\\ 0 \leq \height \leq 180 ^\circ ; 0 \leq \theta \leq 360 ^\circ ; \\ x=asinPhicosθ;and=bsinPhisinθ;With=ccosφ;0≤Phi≤180∘;0≤i≤360∘;
3 first picture
Original code
import numpy as np
import matplotlib.pyplot as plt
def mesh_points(num_pts = 2000):
indices = np.arange(0, num_pts, dtype=float) + 0.5
phi = np.arccos(1 - 2*indices/num_pts)
theta = np.pi * (1 + 5**0.5) * indices
return np.array([np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)]).T
mesh = mesh_points()
plt.figure(figsize=(8,8)).add_subplot(111, projection='3d').scatter(*mesh.T);
plt.show()
The operation effect is as shown in the figure
Let’s take a detailed look at the drawing part of the code here.
plt.figure(figsize=(8,8)).add_subplot(111, projection='3d').scatter(*mesh.T);
plt.show()
figure
plt.figure(figsize=(8,8))
Create a new figure, or activate an existing figure.
figsize
指定 Width, height in inches.
figure
It can be thought of as a drawing window.
Sample code
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8))
plt.show()
The operation effect is as follows
an empty drawing window
subplot
This method is used to add or plan the axis in the window
figure
axes
plt.subplot(*args, **kwargs)
Add an Axes to the current figure or retrieve an existing Axes.
Add an Axes to the current figure or retrieve an existing Axes.
This is a wrapper of Figure.add_subplot which provides additional behavior when working with the implicit API (see the notes section).
This is< a A wrapper for i=4> that provides additional behavior when using the implicit API (see comments section). Figure.add_subplot
Call signatures:
subplot(nrows, ncols, index, **kwargs) # 位置
subplot(pos, **kwargs) # 位置
subplot(**kwargs)
subplot(ax)
Example: tutorial_101d.py
Like below
import matplotlib.pyplot as plt
# 按 三行x二列 的布局分割窗口,
# 得到总共六块区域
# 在第1个区域,放下第1个轴线 ax1
# plt.subplot(321)
# 下面的和上面的效果相同,下面的写法更正式
ax1 = plt.subplot(3, 2, 1)
# 在第2个区域,放下第2个轴线 ax2
# 该轴线无边框
ax2 = plt.subplot(322, frameon=False)
# 在第3个区域,放下第3个轴线 ax3
# 该轴线为极坐标轴线
ax3 = plt.subplot(323, projection='polar')
# 在第4个区域,放下第4个轴线 ax4
# 该轴线背景色为红色
ax4 = plt.subplot(325, facecolor='red')
plt.show()
The running effect is as shown below
3d
fig.add_subplot(111, projection='3d')
111
:Lay out the window by one row and one column, and place the axis in the first area.projection='3d'
:A 3D graph axis
Example code
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
plt.show()
The running effect is as shown below
The three axes of the 3D diagram are as follows:
scatter
scatter(xs, ys, zs)
Draw a scatter plot
based on the specified x, y, z coordinates
Exampletutorial_101f.py
As below
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
xs = [i for i in range(10)]
ys = [0 for i in range(10)]
zs = [0 for i in range(10)]
ax.scatter(xs, ys, zs)
plt.show()
The operation effect is as follows
Drawing code disassembly
plt.figure(figsize=(8,8)).add_subplot(111, projection='3d').scatter(*mesh.T);
This long code, broken down into step-by-step words,
is equivalent to the following code
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
xs, ys, zs = mesh.T
ax.scatter(xs, ys, zs)
It can also be written in the following form.
plt.figure(figsize=(8,8))
plt.add_subplot(111, projection='3d')
xs, ys, zs = mesh.T
plt.scatter(xs, ys, zs)