dreme
dreme

Reputation: 4961

How are x,y,z coordinates defined for Mayavi's mlab.mesh?

I'm trying to create a 3D box which would appear in a Mayavi mlab.points3d plot.

I figure this should be possible using the mlab.mesh function, something like this:

points = mlab.points3d(xp,yp,zp,sp)
3Dbox = mlab.mesh(xbox,ybox,zbox)
mlab.show()

I've looked at the mlab manual entry for mesh, but for the life of me can't understand how the x, y, z data points for the mesh are defined.

Could anyone enlighten me please? An example of the x,y,z data points for a 3D box would be particularly appreciated.

Upvotes: 3

Views: 2588

Answers (1)

Geodor
Geodor

Reputation: 316

You can see that like 3D array split in 3 matrix for x, y and z axes. Each matrix index will depict a coordinates and each points of this coordinates will be connected with this neighbors. So you can start by define 6 planes to make your box :

import numpy
import mayavi.mlab


#             pt1_ _ _ _ _ _ _ _ _pt2
#              /|                 /|
#             / |                / |
#         pt3/_ | _ _ _ _ _ _pt4/  |
#           |   |              |   |
#           |   |              |   |
#           |  pt5_ _ _ _ _ _ _|_ _|pt6
#           |  /               |  /
#           | /                | /
#        pt7|/_ _ _ _ _ _ _ _ _|/pt8

# Where :
x1, y1, z1 = (0, 1, 1)  # | => pt1
x2, y2, z2 = (1, 1, 1)  # | => pt2
x3, y3, z3 = (0, 0, 1)  # | => pt3
x4, y4, z4 = (1, 0, 1)  # | => pt4
x5, y5, z5 = (0, 1, 0)  # | => pt5
x6, y6, z6 = (1, 1, 0)  # | => pt6
x7, y7, z7 = (0, 0, 0)  # | => pt7
x8, y8, z8 = (1, 0, 0)  # | => pt8


box_points = numpy.array([[x1, y1, z1], [x2, y2, z2], [x3, y3, z3],
                          [x4, y4, z4], [x5, y5, z5], [x6, y6, z6],
                          [x7, y7, z7], [x8, y8, z8]])

mayavi.mlab.points3d(box_points[:, 0], box_points[:, 1], box_points[:, 2],
                     mode="axes", color=(1, 0, 0))


mayavi.mlab.mesh([[x1, x2],
                  [x3, x4]],  # | => x coordinate

                 [[y1, y2],
                  [y3, y4]],  # | => y coordinate

                 [[z1, z2],
                  [z3, z4]],  # | => z coordinate

                 color=(0, 0, 0))  # black

# Where each point will be connected with this neighbors :
# (link = -)
#
# x1 - x2     y1 - y2     z1 - z2 | =>  pt1 - pt2
# -    -  and  -   -  and -    -  | =>   -     -
# x3 - x4     y3 - y4     z3 - z4 | =>  pt3 - pt4


mayavi.mlab.mesh([[x5, x6], [x7, x8]],
                 [[y5, y6], [y7, y8]],
                 [[z5, z6], [z7, z8]],
                 color=(1, 0, 0))  # red

mayavi.mlab.mesh([[x1, x3], [x5, x7]],
                 [[y1, y3], [y5, y7]],
                 [[z1, z3], [z5, z7]],
                 color=(0, 0, 1))  # blue

mayavi.mlab.mesh([[x1, x2], [x5, x6]],
                 [[y1, y2], [y5, y6]],
                 [[z1, z2], [z5, z6]],
                 color=(1, 1, 0))  # yellow

mayavi.mlab.mesh([[x2, x4], [x6, x8]],
                 [[y2, y4], [y6, y8]],
                 [[z2, z4], [z6, z8]],
                 color=(1, 1, 1))  # white

mayavi.mlab.mesh([[x3, x4], [x7, x8]],
                 [[y3, y4], [y7, y8]],
                 [[z3, z4], [z7, z8]],
                 color=(1, 0, 1))  # pink

mayavi.mlab.show()

enter image description here

Otherwise, you can also make mesh who show in on go more faces with one call. With two call to mayavi.mlab.mesh :

# Define 2 mesh objects with 3 planes connected among themselves :
#
# pt1 - pt2 - pt6        pt8 - pt6 - pt2
#  -     -     -          -     -     -
# pt3 - pt4 - pt8   AND  pt7   pt5 - pt1
#  -     -     -          -     -     -
# pt7 - pt8 - pt8        pt3 - pt1 - pt1

mayavi.mlab.figure()
mayavi.mlab.mesh([[x1, x2, x6], [x3, x4, x8], [x7, x8, x8]],
                 [[y1, y2, y6], [y3, y4, y8], [y7, y8, y8]],
                 [[z1, z2, z6], [z3, z4, z8], [z7, z8, z8]],
                 color=(1, 0, 0))  # red

mayavi.mlab.mesh([[x8, x6, x2], [x7, x5, x1], [x3, x1, x1]],
                 [[y8, y6, y2], [y7, y5, y1], [y3, y1, y1]],
                 [[z8, z6, z2], [z7, z5, z1], [z3, z1, z1]],
                 color=(0, 0, 1))  # blue

mayavi.mlab.show()

enter image description here

With one call to mayavi.mlab.mesh:

# Merge previous mesh objects in single one:
#
# pt1 - pt2 - pt6             | => pt1 - pt2 - pt6 - pt6 - pt6
#  -     -     -              | =>  -     -     -     -     -
# pt3 - pt4 - pt8             | => pt3 - pt4 - pt8 - pt6 - pt2
#  -     -     -              | =>  -     -     -     -     -
# pt7 - pt8 - pt8 - pt6 - pt2 | => pt7 - pt8 - pt8 - pt6 - pt2
#              -     -    -   | =>  -     -      -     -    -
#             pt7   pt5 - pt1 | => pt7 - pt7 - pt7   pt5 - pt1
#              -     -     -  | =>  -     -     -     -     -
#             pt3 - pt1 - pt1 | => pt7 - pt3 - pt3 - pt1 - pt1


mayavi.mlab.figure()
mayavi.mlab.mesh([[x1, x2, x6, x6, x6],
                  [x3, x4, x8, x6, x2],
                  [x7, x8, x8, x6, x2],
                  [x7, x7, x7, x5, x1],
                  [x7, x3, x3, x1, x1]],

                 [[y1, y2, y6, y6, y6],
                  [y3, y4, y8, y6, y2],
                  [y7, y8, y8, y6, y2],
                  [y7, y7, y7, y5, y1],
                  [y7, y3, y3, y1, y1]],

                 [[z1, z2, z6, z6, z6],
                  [z3, z4, z8, z6, z2],
                  [z7, z8, z8, z6, z2],
                  [z7, z7, z7, z5, z1],
                  [z7, z3, z3, z1, z1]])

mayavi.mlab.show()

enter image description here

Upvotes: 9

Related Questions