Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How did you create the vtk files in python? #5

Closed
ricky2208 opened this issue Oct 6, 2018 · 3 comments
Closed

How did you create the vtk files in python? #5

ricky2208 opened this issue Oct 6, 2018 · 3 comments

Comments

@ricky2208
Copy link

I am trying to create a 3D dataset (I do not want to use existing) using python.
The output will be a vtk structured data format.
The size of the dataset is user input (currently I am trying with 216*216) over a domain from -50.0 to 50.0 in a. I am calculating the scalar value at each grid location by the circle formula sqrt(xx + yy + zz)
I have written the following code but it seems to be incorrect as no file is being generated.
import vtk
import math
x=0
y=0
z=0
print("enter size of the dataset ")
limit=int(input())
for i in range(0,limit):
for j in range(0,limit):
for k in range(0,limit):
x=i
y=j
z=k
vol=math.sqrt(pow(x,2)+pow(y,2)+pow(z,2))
w=vtk.vtkDataSetWriter()
w.SetFileName('C:/Users/prakhyati/datafile1.vtk')
w.Update()
w.Write()

@marcomusy
Copy link
Owner

marcomusy commented Oct 6, 2018

Your code is not quite right.
try this:

pip install --upgrade vtkplotter

from vtkplotter import Plotter, sqrt
from vtkplotter.utils import pointScalars


print("enter size of the dataset ")
#limit=int(input())
limit=20

vp = Plotter()

grid=[]
scalars=[]
for i in range(0,limit):
    for j in range(0,limit):
        for k in range(0,limit):
            vol = sqrt(pow(i,2)+pow(j,2)+pow(k,2))
            scalars.append(vol)
            grid.append([i,j,k])

act = vp.points(grid)

pointScalars(act, scalars, 'myscalars')

vp.write(act, 'myfile.vtk')

vp.show()

#to visualize, at command line:
# > vtkplotter myfile.vtk  # (then press p and k)

@ricky2208
Copy link
Author

can we do it without using vtkplotter
I tried yesterday and wrote this

import vtk
import math
fname='C:/Users/prakhyati/datafile1.vtk'
print("enter size of the dataset ")
limit=int(input())
xAxis = -50.0
yAxis = -50.0
zAxis = -50.0
domain=50
width=0
scalar = "volume"
Points = vtk.vtkPoints()
for i in range(0,limit):
xAxis=xAxis+width
for j in range(0,limit):
yAxis=yAxis+width
for k in range(0,limit):
zAxis=zAxis+width
width=((2*domain)/limit)
vol = math.sqrt(pow(xAxis, 2) + pow(yAxis, 2) + pow(zAxis, 2))
Points.InsertNextPoint(xAxis,yAxis,zAxis)
polydata = vtk.vtkPolyData()
polydata.SetPoints(Points)

        if vtk.VTK_MAJOR_VERSION <= 5:
            polydata.Update()
        writer = vtk.vtkPolyDataWriter()
        writer.SetFileName(fname)
        writer.SetFileFormat(1,2)
        if vtk.VTK_MAJOR_VERSION <= 5:
            writer.SetInput(polydata)
        else:
            writer.SetInputData(polydata)
        writer.Write()

It does create the file, but I am unable to add scalars (can't find set scalar function for polydata )
Also, I have doubt if I am calculating the coordinates of the grid correctly (I did this on pen and paper and implemented, domain is from -50.0 to 50.0)
Any information on the same will be a help.

@marcomusy
Copy link
Owner

I really wonder why you would not use the package.

Anyway you can find hints in vtkplotter/utils.py (pointScalars() method).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants