init
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: anaconda安装
|
||||
tags: [python, 开发环境]
|
||||
categories: 开发
|
||||
toc: true
|
||||
date:
|
||||
cover: /img/cov5.JPG
|
||||
top_img: /img/dev.jpg
|
||||
---
|
||||
<!--  -->
|
||||
|
||||
### 为什么需要 anaconda
|
||||
|
||||
### 下载和安装
|
||||
|
||||
### 基本使用
|
||||
@@ -0,0 +1,237 @@
|
||||
---
|
||||
title: pyvista初试
|
||||
tags: [python, 数据可视化]
|
||||
categories: 开发
|
||||
toc: true
|
||||
date: 2024-11-27 15:27:30
|
||||
cover: /img/cov5.JPG
|
||||
top_img: /img/dev.jpg
|
||||
---
|
||||
|
||||
|
||||
跳槽后工作没那么忙了, 遂开始写博客打法时间.
|
||||
|
||||
因为现在的项目需要加载三维模型并显示应力, 所以需要vtk, 为了开发简单, 选择了`python`
|
||||
|
||||
### 开发环境
|
||||
|
||||
需要 `python > 3.8`
|
||||
|
||||
使用 **`pip`** 进行安装
|
||||
|
||||
```bash
|
||||
pip install pyvista
|
||||
```
|
||||
|
||||
使用 **`conda`** 进行安装
|
||||
|
||||
需要有[*`Anaconda`*](https://aniakea.github.io/2024/11/29/anaconda%E5%AE%89%E8%A3%85/ "安装教程")环境
|
||||
|
||||
```bash
|
||||
conda install -c conda-forge pyvista
|
||||
```
|
||||
|
||||
详情参考 *[官方文档](https://docs.pyvista.org/getting-started/installation.html)*.
|
||||
|
||||
### 尝试
|
||||
|
||||
尝试官方示例
|
||||
|
||||
```python
|
||||
import pyvista
|
||||
mesh = pyvista.Sphere()
|
||||
mesh.plot()
|
||||
```
|
||||
|
||||
可以看到
|
||||
|
||||
```python
|
||||
from pyvista import examples
|
||||
mesh = examples.download_dragon()
|
||||
mesh['scalars'] = mesh.points[:, 1]
|
||||
mesh.plot(cpos='xy', cmap='plasma')
|
||||
```
|
||||
|
||||
可以看到
|
||||
|
||||
更多请查看 *[官方文档](https://docs.pyvista.org/user-guide/)*
|
||||
|
||||
### 尝试通过excel或csv提供数据
|
||||
|
||||
为了方便,这里我们需要使用`pandas`.`pyvistaqt`.`pyqt`库
|
||||
|
||||
```shell
|
||||
pip install pandas pyvistaqt PyQt5
|
||||
```
|
||||
|
||||
分为两个数据文件, 一个为提供点的xyz轴数据, 一个提供单元所用的点
|
||||
|
||||
*point.xlsx*文件结构为
|
||||
|
||||
| 编号 | x | y | z |
|
||||
| --- | --- | --- | --- |
|
||||
|
||||
*node.xlsx*文件结构为
|
||||
|
||||
| 编号 | unit | p1 | p2 | ... |
|
||||
| --- | --- | --- | --- | --- |
|
||||
|
||||
考虑到单元类型不唯一, 这里只能选择 `UnStructedGrid`
|
||||
|
||||
代码如下
|
||||
|
||||
```python
|
||||
from PyQt5.QtWidgets import (
|
||||
QFrame,
|
||||
QVBoxLayout,
|
||||
QApplication,
|
||||
QHBoxLayout,
|
||||
QButtonGroup,
|
||||
QRadioButton,
|
||||
)
|
||||
import pyvista as pv
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from pyvistaqt import QtInteractor
|
||||
import os
|
||||
|
||||
class Widget(QFrame):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
processor.change(0)
|
||||
self.vmin = 0
|
||||
self.vmax = 0.1319
|
||||
layout = QVBoxLayout()
|
||||
|
||||
self.plotter = QtInteractor(self, auto_update=1)
|
||||
layout.addWidget(self.plotter.interactor)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
self.read_excel()
|
||||
|
||||
def read_excel(self):
|
||||
point_data = pd.read_excel("./point.xlsx", header=None)
|
||||
point_pos = []
|
||||
point_keys = []
|
||||
for row in point_data.values:
|
||||
point_pos.append(row[1:4])
|
||||
point_keys.append(row[1])
|
||||
|
||||
cell_data = pd.read_excel("./node.xlsx", header=None)
|
||||
cell = []
|
||||
cell_shapes = []
|
||||
|
||||
def covert(code):
|
||||
return point_keys.index(code)
|
||||
|
||||
for row in cell_data.values:
|
||||
cell_shapes.append(pv.CellType.QUAD)
|
||||
cell.append([4].append(map(covert, row[2:])))
|
||||
|
||||
self.grid = pv.UnstructuredGrid(cell, cell_shapes, point_pos)
|
||||
|
||||
self.actor = self.plotter.add_mesh(
|
||||
self.grid, cmap="viridis", smooth_shading=True, copy_mesh=True
|
||||
)
|
||||
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
|
||||
widget = Widget()
|
||||
|
||||
|
||||
widget.show()
|
||||
```
|
||||
|
||||
需要注意的一点为cell中的值对应的是point数组的下标, 这里这么写主要是应对point编号非从0开始递增的情况
|
||||
|
||||
目前的`unit_type`没有考虑非四边形的情况, 若需要考虑多类型组合的方式, 请参考[官方文档](https://docs.pyvista.org/examples/00-load/create-polyhedron#sphx-glr-examples-00-load-create-polyhedron-py)
|
||||
|
||||
从excel中加载数据通常很大, 建议完成读取后存储为模型文件
|
||||
|
||||
```python
|
||||
self.grid.save("./mesh.vtk")
|
||||
```
|
||||
|
||||
这种方式不能储存stl文件, 若是确定只需要表面的点需要存储为stl文件, 需要提取表面
|
||||
|
||||
```python
|
||||
self.grid.extract_surface().save("./mesh.stl")
|
||||
```
|
||||
|
||||
### 尝试添加数据到模型
|
||||
|
||||
注意数据的数量必须要和当前显示的模型的点的数量一致
|
||||
|
||||
```python
|
||||
self.grid["range"] = data
|
||||
self.actor = self.plotter.add_mesh(
|
||||
self.grid,
|
||||
cmap="viridis",
|
||||
scalars="range",
|
||||
smooth_shading=True,
|
||||
copy_mesh=True
|
||||
)
|
||||
```
|
||||
|
||||
### 尝试自定义色彩映射
|
||||
|
||||
```python
|
||||
from matplotlib.colors import LinearSegmentedColormap
|
||||
|
||||
def make_cmap(vmin, vmax, n=256):
|
||||
"""
|
||||
创建一个从蓝黑色到黄色再到红色的自定义颜色映射
|
||||
|
||||
参数:
|
||||
vmin : float
|
||||
颜色映射的最小值
|
||||
vmax : float
|
||||
颜色映射的最大值
|
||||
n : int, 可选
|
||||
颜色映射中的颜色数量,默认为256
|
||||
|
||||
返回:
|
||||
pyvista.LookupTable
|
||||
PyVista 查找表对象,可用于设置 mesh 的标量条
|
||||
"""
|
||||
# 定义颜色映射的颜色
|
||||
colors = ['#000033', '#0000FF', '#00FFFF', '#FFFF00', '#FF0000']
|
||||
|
||||
# 创建 matplotlib 颜色映射
|
||||
cmap = LinearSegmentedColormap.from_list("custom_blue_black_to_yellow_red", colors, N=n)
|
||||
|
||||
# 创建 PyVista 查找表
|
||||
lut = pv.LookupTable()
|
||||
|
||||
# 设置颜色映射的范围
|
||||
lut.SetRange(vmin, vmax)
|
||||
|
||||
# 填充查找表
|
||||
for i in range(n):
|
||||
r, g, b, _ = cmap(i / (n - 1))
|
||||
lut.SetTableValue(i, r, g, b)
|
||||
|
||||
return lut
|
||||
```
|
||||
|
||||
函数 `make_cmap` 前两个参数决定了映射的上下限, 第三个参数决定映射的色阶数量
|
||||
|
||||
colors至少需要两个颜色, 此时色阶在rgb上线性变化, 这里定义了中间色号, 所以色阶呈贝塞尔曲线变化
|
||||
|
||||
在 add_mesh 函数中的 `cmap` 参数设置为函数 `make_cmap` 的返回值
|
||||
|
||||
```python
|
||||
self.grid["range"] = data
|
||||
actor = self.plotter.add_mesh(
|
||||
self.grid,
|
||||
scalars="range",
|
||||
cmap = make_cmap(self.vmin,self.vmax),
|
||||
# cmap="viridis",
|
||||
smooth_shading=True,
|
||||
copy_mesh=True,
|
||||
)
|
||||
```
|
||||
@@ -0,0 +1,64 @@
|
||||
---
|
||||
title: 在c++中嵌入python解释器
|
||||
tags: [c++, python]
|
||||
categories: 开发
|
||||
toc: true
|
||||
date:
|
||||
cover: /img/cov5.JPG
|
||||
top_img: /img/dev.jpg
|
||||
---
|
||||
|
||||
还是哪个展示应力模型的项目, 由于是基于之前的项目的, 而之前的项目的gui是qt做的, 之前又没有告诉我需要嵌入这个程序, 而我python的已经写完了
|
||||
|
||||
所以开始研究嵌入方案
|
||||
|
||||
首先需要确定python是否独立运行于老的gui程序
|
||||
|
||||
如果独立运行的情况, 我需要在python端额外写一个网络模块来和老gui程序进行交互,两边都得改, 怪麻烦的, 不考虑
|
||||
|
||||
非独立运行的话, 就需要嵌入python解释器来执行我写的python代码
|
||||
|
||||
### 准备
|
||||
|
||||
首先我们先安装 pybind11 库, 这是个 head-only 的库, 所以只需要下载源码, 在项目中添加头文件搜索的路径即可
|
||||
|
||||
先下载, ~~最简单的方案就是去github下载zip(he~~
|
||||
|
||||
为了不出现违法收入, 我们使用`gitee`的镜像, [链接](https://gitee.com/mirrors/pybind11)
|
||||
|
||||
直接一键下载或者使用git
|
||||
|
||||
```bash
|
||||
# if has ssh
|
||||
git clone [email protected]:mirrors/pybind11.git
|
||||
|
||||
# or use https
|
||||
git clone https://gitee.com/mirrors/pybind11.git
|
||||
```
|
||||
|
||||
如果是项目是cmake的话可以直接用 `FetchContent`
|
||||
|
||||
```cmake
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
pybind11
|
||||
GIT_REPOSITORY https://gitee.com/mirrors/pybind11.git
|
||||
GIT_TAG v2.13
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(pybind11)
|
||||
```
|
||||
|
||||
如果环境变量中包含python的路径会自动寻找到python和libpython
|
||||
|
||||
可惜的是, 这~~傻逼~~项目用的qmake, 所以我们只能在pro文件里手动加include路径和link路径了
|
||||
|
||||
### 使用
|
||||
|
||||
这里建议去逛[官方文档](https://pybind11.readthedocs.io/en/stable/index.html), 比我这啰啰嗦嗦好多了, 在这我只讲一些我遇见的坑
|
||||
|
||||
1. 解释器需要自己设置`pythonhome`, 不设置的话啥都import不了
|
||||
但是pybind11库没有提供接口, 只能使用libpython提供的c接口, libpython提供的接口命名又怪的要死
|
||||
|
||||
1. 暂留, 之后想起来了再更新
|
||||
Reference in New Issue
Block a user