0

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()
  • https://stackoverflow.com/questions/39840638/update-mayavi-plot-in-loop – Joe Jul 16 '19 at 08:13
  • `# Connect them src.mlab_source.dataset.lines = connections src.update()` in https://docs.enthought.com/mayavi/mayavi/auto/example_plotting_many_lines.html – Joe Jul 16 '19 at 08:14
  • https://docs.enthought.com/mayavi/mayavi/mlab_animating.html – Joe Jul 16 '19 at 08:15
  • Thanks @Joe. src.mlab_source, that is what I am looking for. – Rupesh Chandgude Jul 18 '19 at 03:56
  • Hi @Joe I am able to make changes using mlab_souce. But it is not working if we use it after mapping texture to mesh. I use **surf.mlab_source.points** to update the points. – Rupesh Chandgude Jul 18 '19 at 09:28
  • could you add a small, complete working example to show what you mean. Please add random datapoints instead of showing where you load them from. – Joe Jul 18 '19 at 16:48
  • @Joe I have updated question with sample code and what I actually want to achieve. – Rupesh Chandgude Jul 19 '19 at 06:14
  • Does this fit your case? https://github.com/enthought/mayavi/issues/211 – Joe Jul 19 '19 at 11:38
  • Ok, now I see the texture mapped onto the random surface. Now, what are you trying to do? – Joe Jul 19 '19 at 11:48
  • @Joe I managed to map a texture to mesh. Now I want to flattened the mesh using some transformation, so mesh will get flattened along with a texture.But I am not able to flattened the mesh after mapping texture. I am doing this for dewrapping image to remove distortion in image. – Rupesh Chandgude Jul 22 '19 at 02:59

1 Answers1

1

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()
Joe
  • 6,758
  • 2
  • 26
  • 47
  • Thank you. New problem I am facing is , when I map image as a texture to a mesh, alignment of image and mesh is mismatch. Image is getting larger than mesh. Is there any setting to align mesh with texture image. – Rupesh Chandgude Jul 23 '19 at 09:48
  • https://discourse.vtk.org/t/python-vtk-can-we-get-the-mesh-rendered-from-the-colorized-point-cloud/200 – Joe Jul 23 '19 at 10:11
  • This related to the plane mapping. Do you know how texture mapping works? – Joe Jul 23 '19 at 10:18
  • https://stackoverflow.com/questions/24107084/mayavi-texture-to-span-over-full-surface – Joe Jul 23 '19 at 10:19
  • Thank you. I will check the shared links. I just know that texture mapping is uses one-2-one type mapping. – Rupesh Chandgude Jul 24 '19 at 12:31