读取列格式的三维模型并显示pyvista

数据格式如下

前四行为基本信息,读取时跨过去,输出时按k,j,i的循环顺序

#coding=utf-8

import numpy as np
import pyvista as pv
import matplotlib.pyplot as plt
import matplotlib as mpl

ni=129
nj=129
nk=100
class MyDataSet:
	def __init__(self,  fileNameList):
		self._fileNameList=fileNameList
		self.create_colorMap()
		self.p=pv.Plotter()		
		self.p.set_scale(zscale=10)
		#self.p.add_slider_widget(self.set_zscale(),rng=np.range(1,100))
		self.p.add_axes()
		
	def readData(self, fileName):
		dat=np.loadtxt(fileName,skiprows=4)
		dat=dat.transpose()
		self.x = dat[3].reshape(nk,nj,ni)
		self.y = dat[4].reshape(nk,nj,ni)
		self.z = dat[5].reshape(nk,nj,ni)
		self.facies = dat[6].reshape(nk,nj,ni)		
		self.facies[self.facies>7]=7
		
	def create_colorMap(self):
		#colors= ['blue','cyan','green','pink','magenta','purple','gold','red']
		colors= ['blue','green','pink','magenta','orange','yellow','red']
		self.discmap = mpl.colors.ListedColormap(colors)
 
	def set_zscale(self, _zscale):
		self.p.set_scale(zscale=_zscale)
 
	def showModel(self):	
		grid = pv.StructuredGrid(self.x, self.y, self.z)
		#grid.point_arrays["values"]=self.facies.flatten(order="F")
		grid.cell_arrays["values"]=self.facies[0:nk-1, 0:nj-1, 0:ni-1].flatten(order="F")
		self.p.add_mesh(grid, cmap=self.discmap,  show_scalar_bar=True)
		self.p.show()

if __name__=="__main__":
	fileNameList=[]
	myDataSet = MyDataSet(fileNameList)
	myDataSet.readData('./case5.dat')
	myDataSet.showModel()

效果如下

 

 

 

posted @ 2022-08-21 10:12  Oliver2022  阅读(73)  评论(0编辑  收藏  举报