Rupesh Chandgude
Rupesh Chandgude

Reputation: 63

How to edit points data in vtk file

I am trying to project some points of mesh using some function. I have access the point and cell data from polydata. From that I get projected point. Now I want to replace those points with projected points. Is there any way to update points in mayavi or vtk?

To get polydata from .ply file

from plyfile import PlyData,PlyElement
import numpy as np 
import time
from mayavi import mlab
from tvtk.api import tvtk

plydata = PlyData.read(ply_file)
points = plydata.elements[0].data

# Get X,Y,Z coordinates from .ply file
x,y,z = [],[],[]                                                                                                                                                                                    

for i in points: 
    x.append(i[0]) 
    y.append(i[1]) 
    z.append(i[2]) 

s = [0.1]*len(x)

mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

actor = surf.actor.actors[0]
polydata = tvtk.to_vtk(actor.mapper.input)

projecting points:

for i in range(polydata.GetNumberOfCells()):
    pts = polydata.GetCell(i).GetPoints()    
    np_pts = np.array([pts.GetPoint(i) for i in range(pts.GetNumberOfPoints())])
    projected_point1,projected_point2,projected_point3 = project_points(point1,point2,point3)

Now for saving I tried following line in above for loop:

polydata_return.GetCell(i).GetPoints() = np.array([projected_point1,projected_point2,projected_point3])

but got error as follows: SyntaxError: can't assign to function call

Is there any way to replace/edit present points with projected points. Thanks in advance.

UPDATE : To generate mesh :

from mayavi import mlab
from tvtk.api import tvtk
import numpy as np
from plyfile import PlyData,PlyElement

x = [random.randint(0,250) for i in range(250)]
y = [random.randint(0,250) for i in range(250)]
z = [random.randint(0,5) for i in range(250)]
s = [0.1]*len(x)

pts = mlab.points3d(x, y, z,s)
mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

After generating above mesh I want to map a texture to mesh using following image and then I have to flattened the mesh using some transformation, so mesh will get flattened along with a texture. I doing this for dewrapping image to remove distortion in image. texture image

For projecting image I use following code:

image_file = texture_image_path
if image_file.split('.')[-1] == 'png':
    img = tvtk.PNGReader()
elif image_file.split('.')[-1] == 'jpg':
    img = tvtk.JPEGReader()

img.file_name=image_file
texture = tvtk.Texture(input_connection=img.output_port, interpolate=0)

surf.actor.enable_texture = True  
surf.actor.tcoord_generator_mode = 'plane'  
surf.actor.actor.texture = texture
mlab.show()

Upvotes: 0

Views: 1202

Answers (1)

Joe
Joe

Reputation: 7121

This should get you started, you just have to use the correct data at the correct position. There are several attributes to mesh.mlab_source. I added some in the comments.

from mayavi import mlab
from tvtk.api import tvtk
import numpy as np
#from plyfile import PlyData,PlyElement
import random

x = [random.randint(0,250) for i in range(250)]
y = [random.randint(0,250) for i in range(250)]
z = [random.randint(0,5) for i in range(250)]
s = [0.1]*len(x)

pts = mlab.points3d(x, y, z,s)
mesh = mlab.pipeline.delaunay2d(pts)
surf = mlab.pipeline.surface(mesh,color=(1,1,1))

#After generating above mesh I want to map a texture to mesh using following image and then I have to flattened the mesh using some transformation, so mesh will get flattened along with a texture. I doing this for dewrapping image to remove distortion in image. texture image

#For projecting image I use following code:

image_file = 'texture.jpg'
if image_file.split('.')[-1] == 'png':
    img = tvtk.PNGReader()
elif image_file.split('.')[-1] == 'jpg':
    img = tvtk.JPEGReader()


img.file_name=image_file
texture = tvtk.Texture(input_connection=img.output_port, interpolate=1)
#texture = tvtk.Texture(interpolate=1)
#texture.input = img

surf.actor.enable_texture = True  
surf.actor.tcoord_generator_mode = 'plane'  
surf.actor.actor.texture = texture

#src = mlab.pipeline.scalar_scatter(x, y, z, s)
#src.mlab_source.dataset.lines = connections src.update(
#src.mlab_source.reset()

@mlab.animate
def anim():
    for i in range(10):
        print(i)

        #plt.mlab_source.set(x=x, y=y, z=z)
        print(surf.mlab_source.dataset)
        x = [random.randint(0,250) for i in range(250)]
        y = [random.randint(0,250) for i in range(250)]
        z = [random.randint(0,5) for i in range(250)]
        s = [0.1]*len(x)

        pts = mlab.points3d(x, y, z,s)
        mesh = mlab.pipeline.delaunay2d(pts)


        surf.mlab_source.x = mesh.mlab_source.x
        surf.mlab_source.y = mesh.mlab_source.y
        surf.mlab_source.z = mesh.mlab_source.z


        #s.mlab_source.scalars = np.asarray(x*0.1*(i+1), 'd')
        yield

anim()

mlab.show()

Upvotes: 1

Related Questions