Accessing the triangle data of a mayavi isosurface

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing the triangle data of a mayavi isosurface

Amit Aides
Hello,

I am using mayavi to visualize scalar data over a RectilinearGrid:

     import scipy.io as sio
     import mayavi.mlab as mlab
     from tvtk.api import tvtk

     data = sio.loadmat(r'data.mat')
     X = data['X']
     Y = data['Y']
     Z = data['Z']
     V = data['V']

     r = tvtk.RectilinearGrid()
     r.point_data.scalars = np.swapaxes(V, 0, 2).ravel()
     r.point_data.scalars.name = 'scalars'

     r.x_coordinates = X[0, :, 0]
     r.y_coordinates = Y[:, 0, 0]
     r.z_coordinates = Z[0, 0, :]

     fig = mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
     iso = mlab.pipeline.iso_surface(r)

     iso.contour.number_of_contours = 10
     iso.actor.property.opacity = 0.2

  I would like to access the underlying data, i.e. the vertices, faces,
colors and alpha of the different surfaces. How can I do this?

Thanks in advance,
Amit
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the triangle data of a mayavi isosurface

Jan Kurzawski
Hi,

You might want to look at this example. http://docs.enthought.com/mayavi/mayavi/auto/example_mayavi_traits_ui.html
Using traitsui you can divide the window into 2 parts scene and properties


Best,
Jan

Wiadomość napisana przez Amit Aides <[hidden email]> w dniu 5 gru 2012, o godz. 17:28:

> Hello,
>
> I am using mayavi to visualize scalar data over a RectilinearGrid:
>
>     import scipy.io as sio
>     import mayavi.mlab as mlab
>     from tvtk.api import tvtk
>
>     data = sio.loadmat(r'data.mat')
>     X = data['X']
>     Y = data['Y']
>     Z = data['Z']
>     V = data['V']
>
>     r = tvtk.RectilinearGrid()
>     r.point_data.scalars = np.swapaxes(V, 0, 2).ravel()
>     r.point_data.scalars.name = 'scalars'
>
>     r.x_coordinates = X[0, :, 0]
>     r.y_coordinates = Y[:, 0, 0]
>     r.z_coordinates = Z[0, 0, :]
>
>     fig = mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
>     iso = mlab.pipeline.iso_surface(r)
>
>     iso.contour.number_of_contours = 10
>     iso.actor.property.opacity = 0.2
>
>  I would like to access the underlying data, i.e. the vertices, faces,
> colors and alpha of the different surfaces. How can I do this?
>
> Thanks in advance,
> Amit
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the triangle data of a mayavi isosurface

Amit Aides
Hi,

Thanks for the reply. But I am looking for the underlying vtk data,
something more similar to:
http://docs.enthought.com/mayavi/mayavi/auto/example_polydata.html

My intention is to translate the isosurface to Asymptote format and then
embed it in a PDF as a 3DPDF object. I am looking to do in python what
the following does in R:
http://homepage.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf

Thanks,
Amit

On 06/12/2012 01:30, Jan Kurzawski wrote:

> Hi,
>
> You might want to look at this example. http://docs.enthought.com/mayavi/mayavi/auto/example_mayavi_traits_ui.html
> Using traitsui you can divide the window into 2 parts scene and properties
>
>
> Best,
> Jan
>
> Wiadomość napisana przez Amit Aides <[hidden email]> w dniu 5 gru 2012, o godz. 17:28:
>
>> Hello,
>>
>> I am using mayavi to visualize scalar data over a RectilinearGrid:
>>
>>      import scipy.io as sio
>>      import mayavi.mlab as mlab
>>      from tvtk.api import tvtk
>>
>>      data = sio.loadmat(r'data.mat')
>>      X = data['X']
>>      Y = data['Y']
>>      Z = data['Z']
>>      V = data['V']
>>
>>      r = tvtk.RectilinearGrid()
>>      r.point_data.scalars = np.swapaxes(V, 0, 2).ravel()
>>      r.point_data.scalars.name = 'scalars'
>>
>>      r.x_coordinates = X[0, :, 0]
>>      r.y_coordinates = Y[:, 0, 0]
>>      r.z_coordinates = Z[0, 0, :]
>>
>>      fig = mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
>>      iso = mlab.pipeline.iso_surface(r)
>>
>>      iso.contour.number_of_contours = 10
>>      iso.actor.property.opacity = 0.2
>>
>>   I would like to access the underlying data, i.e. the vertices, faces,
>> colors and alpha of the different surfaces. How can I do this?
>>
>> Thanks in advance,
>> Amit
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the triangle data of a mayavi isosurface

Eric Carlson
Hello,
I don't know if you figured it out or got your solution, but the
following works for me:

#mod of test_contour3d():
import numpy
from enthought.mayavi import mlab as p3d
dims = [64, 64, 64]
xmin, xmax, ymin, ymax, zmin, zmax = [-5,5,-5,5,-5,5]
x, y, z = numpy.ogrid[xmin:xmax:dims[0]*1j,
                       ymin:ymax:dims[1]*1j,
                       zmin:zmax:dims[2]*1j]
x = x.astype('f')
y = y.astype('f')
z = z.astype('f')


scalars = x*x*0.5 + y*y + z*z*2.0

#create one isosurface data set
my_obj = p3d.contour3d(scalars, contours=[20.0])
my_actor=my_obj.actor.actors[0]
poly_data_object=my_actor.mapper.input

#array with all vertices
the_points = array(poly_data_object.points)#(number_of_points by 3)

#poly_data_object.polys.data.to_array() gives a 1D array for cell data
#  of the form [n_vertices_in_face0,
#             vert0,vert1,vert2,...,
#             n_vertices_in_face1,vert0,vert1,...]
# If all faces are triangles, then the following works
#The first column gives number of vertices for the poly
the_cells = reshape(poly_data_object.polys.data.to_array(), [-1,4])
#Since all faces are triangles, get rid of first column
the_cells = the_cells[:,1:]


#Do interesting things with your points and triangles!

On 12/5/2012 9:07 PM, Amit Aides wrote:

> Hi,
>
> Thanks for the reply. But I am looking for the underlying vtk data,
> something more similar to:
> http://docs.enthought.com/mayavi/mayavi/auto/example_polydata.html
>
> My intention is to translate the isosurface to Asymptote format and then
> embed it in a PDF as a 3DPDF object. I am looking to do in python what
> the following does in R:
> http://homepage.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf
>
> Thanks,
> Amit
>
> On 06/12/2012 01:30, Jan Kurzawski wrote:


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the triangle data of a mayavi isosurface

Amit Aides
Hi,

Thank you for your help. It works.
Is it possible to get the color information of each vertex/polygon? I
would like to use it for an isosurface object which uses different color
for each surface.
Is there a place that explains the tvtk/vtk classes (actor, mapper etc)?

Thanks,
Amit

On 12/10/2012 5:49 PM, Eric Carlson wrote:

> Hello,
> I don't know if you figured it out or got your solution, but the
> following works for me:
>
> #mod of test_contour3d():
> import numpy
> from enthought.mayavi import mlab as p3d
> dims = [64, 64, 64]
> xmin, xmax, ymin, ymax, zmin, zmax = [-5,5,-5,5,-5,5]
> x, y, z = numpy.ogrid[xmin:xmax:dims[0]*1j,
>                         ymin:ymax:dims[1]*1j,
>                         zmin:zmax:dims[2]*1j]
> x = x.astype('f')
> y = y.astype('f')
> z = z.astype('f')
>
>
> scalars = x*x*0.5 + y*y + z*z*2.0
>
> #create one isosurface data set
> my_obj = p3d.contour3d(scalars, contours=[20.0])
> my_actor=my_obj.actor.actors[0]
> poly_data_object=my_actor.mapper.input
>
> #array with all vertices
> the_points = array(poly_data_object.points)#(number_of_points by 3)
>
> #poly_data_object.polys.data.to_array() gives a 1D array for cell data
> #  of the form [n_vertices_in_face0,
> #             vert0,vert1,vert2,...,
> #             n_vertices_in_face1,vert0,vert1,...]
> # If all faces are triangles, then the following works
> #The first column gives number of vertices for the poly
> the_cells = reshape(poly_data_object.polys.data.to_array(), [-1,4])
> #Since all faces are triangles, get rid of first column
> the_cells = the_cells[:,1:]
>
>
> #Do interesting things with your points and triangles!
>
> On 12/5/2012 9:07 PM, Amit Aides wrote:
>> Hi,
>>
>> Thanks for the reply. But I am looking for the underlying vtk data,
>> something more similar to:
>> http://docs.enthought.com/mayavi/mayavi/auto/example_polydata.html
>>
>> My intention is to translate the isosurface to Asymptote format and then
>> embed it in a PDF as a 3DPDF object. I am looking to do in python what
>> the following does in R:
>> http://homepage.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf
>>
>> Thanks,
>> Amit
>>
>> On 06/12/2012 01:30, Jan Kurzawski wrote:
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the triangle data of a mayavi isosurface

Eric Carlson
For the isosurfaces (any PolyData object), the color is set by the
"scalars" attribute in conjunction with a color table. Using the
previous example, the data is located at
poly_data_object.point_data.scalars, and for an isosurface the scalars
attribute is an array of the same length as the number of vertices, and
values=isosurface function value.

I usually create and analyze one isosurface at a time after specifiying
a function value. For multiple values, I create an array and loop
through one value at a time. You can create multiple surfaces at one
time and then identify surface associations looking at the scalar
properties, but then you must identify the unique function values and
then create a loop that identifies vertices and faces for each surface.
I think the isosurfaces are created using contiguous blocks of arrays,
but it still seems to be more work than extracting one isosurface at a time.


Regarding documentation, I just create objects, then examine the
properties/objects that pop up during autocompletion and the doc strings
from object call tips (and cross reference with vtk documentation). It
is often like walking through a jungle, but is usually sufficient (even
if inefficient).


You can make a colorful isosurface doing something like (extended from
previous example)
simple_tm=p3d.triangular_mesh(the_points[:,0], the_points[:,1],
                               the_points[:,2],
                               the_cells,
scalars=numpy.linspace(-pi,pi,len(poly_data_object.point_data.scalars)))

This is a handy trick if you have more than one scalar field and want to
brute-force map one scalar field on a precise isosurface of another
field after interpolation at the isosurface vertices.

Cheers,
EC

On 12/18/2012 2:50 AM, Amit Aides wrote:

> Hi,
>
> Thank you for your help. It works.
> Is it possible to get the color information of each vertex/polygon? I
> would like to use it for an isosurface object which uses different color
> for each surface.
> Is there a place that explains the tvtk/vtk classes (actor, mapper etc)?
>
> Thanks,
> Amit
>
> On 12/10/2012 5:49 PM, Eric Carlson wrote:
>> Hello,
>> I don't know if you figured it out or got your solution, but the
>> following works for me:
>>
>> #mod of test_contour3d():
>> import numpy
>> from enthought.mayavi import mlab as p3d
>> dims = [64, 64, 64]
>> xmin, xmax, ymin, ymax, zmin, zmax = [-5,5,-5,5,-5,5]
>> x, y, z = numpy.ogrid[xmin:xmax:dims[0]*1j,
>>                          ymin:ymax:dims[1]*1j,
>>                          zmin:zmax:dims[2]*1j]
>> x = x.astype('f')
>> y = y.astype('f')
>> z = z.astype('f')
>>
>>
>> scalars = x*x*0.5 + y*y + z*z*2.0
>>
>> #create one isosurface data set
>> my_obj = p3d.contour3d(scalars, contours=[20.0])
>> my_actor=my_obj.actor.actors[0]
>> poly_data_object=my_actor.mapper.input
>>
>> #array with all vertices
>> the_points = array(poly_data_object.points)#(number_of_points by 3)
>>
>> #poly_data_object.polys.data.to_array() gives a 1D array for cell data
>> #  of the form [n_vertices_in_face0,
>> #             vert0,vert1,vert2,...,
>> #             n_vertices_in_face1,vert0,vert1,...]
>> # If all faces are triangles, then the following works
>> #The first column gives number of vertices for the poly
>> the_cells = reshape(poly_data_object.polys.data.to_array(), [-1,4])
>> #Since all faces are triangles, get rid of first column
>> the_cells = the_cells[:,1:]
>>
>>
>> #Do interesting things with your points and triangles!
>>
>> On 12/5/2012 9:07 PM, Amit Aides wrote:
>>> Hi,
>>>
>>> Thanks for the reply. But I am looking for the underlying vtk data,
>>> something more similar to:
>>> http://docs.enthought.com/mayavi/mayavi/auto/example_polydata.html
>>>
>>> My intention is to translate the isosurface to Asymptote format and then
>>> embed it in a PDF as a 3DPDF object. I am looking to do in python what
>>> the following does in R:
>>> http://homepage.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf
>>>
>>> Thanks,
>>> Amit
>>>
>>> On 06/12/2012 01:30, Jan Kurzawski wrote:
>>
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev