File indexing completed on 2026-04-09 07:48:47
0001
0002 """
0003 See cubeplt.py for 3d plotting of the cubes
0004 """
0005 import numpy as np
0006
0007 def make_pyvista_indices(indices):
0008 """
0009 :param indices: (nface,3) triangles OR (nface,4) quads
0010 :return ii: vista type list
0011 """
0012 sh = list(indices.shape)
0013 last = sh[-1]
0014 assert last in (3,4)
0015 sh[-1] = last+1
0016 ii = np.zeros(sh, dtype=np.int32)
0017 ii[:,1:] = indices
0018 ii[:,0] = last
0019 return ii
0020
0021
0022 def make_cube_oxyz(oxyz):
0023 """
0024 :param oxyz: (4,3) : origin and orthogonal surrounding points nominally
0025 assumed in +X,+Y,+Z directions relative to first point
0026
0027
0028 (YZ) (XYZ)
0029 6.........7
0030 /. /|
0031 / . / |
0032 / . / |
0033 3.........5 |
0034 | . | |
0035 | 2.....|...4
0036 Z | / | /
0037 | / Y | /
0038 |/ |/
0039 0---------1
0040 X
0041
0042 """
0043 o = oxyz[0]
0044 v = oxyz[1:] - o
0045
0046 verts = np.zeros([8,3], dtype=np.float32)
0047 verts[:4] = oxyz
0048 verts[4] = o + v[0] + v[1]
0049 verts[5] = o + v[0] + v[2]
0050 verts[6] = o + v[1] + v[2]
0051 verts[7] = o + v[0] + v[1] + v[2]
0052
0053 indices = np.array([
0054 [0,1,5,3],
0055 [1,4,7,5],
0056 [4,2,6,7],
0057 [0,3,6,2],
0058 [0,2,4,1],
0059 [5,7,6,3]],
0060 dtype=np.int32)
0061
0062 pv_indices = make_pyvista_indices(indices)
0063 faces = verts[indices]
0064 assert verts.shape == (8,3)
0065 assert indices.shape == (6,4)
0066 assert pv_indices.shape == (6,5)
0067 assert faces.shape == (6,4,3)
0068 return verts, faces, pv_indices
0069
0070
0071 def make_cube_bbox(bbox):
0072 """
0073 :param bbox: (2,3) mi,mx
0074
0075 mx
0076 (YZ) (XYZ)
0077 6.........7
0078 /. /|
0079 / . / |
0080 / . / |
0081 3.........5 |
0082 | . | |
0083 | 2.....|...4
0084 Z | / | /
0085 | / Y | /
0086 |/ |/
0087 0---------1
0088 mi X
0089
0090 """
0091 assert bbox.shape == (2,3)
0092 mi, mx = bbox
0093 verts = np.zeros([8,3], dtype=np.float32)
0094 verts[0] = [mi[0], mi[1], mi[2]]
0095 verts[1] = [mx[0], mi[1], mi[2]]
0096 verts[2] = [mi[0], mx[1], mi[2]]
0097 verts[3] = [mi[0], mi[1], mx[2]]
0098 verts[4] = [mx[0], mx[1], mi[2]]
0099 verts[5] = [mx[0], mi[1], mx[2]]
0100 verts[6] = [mi[0], mx[1], mx[2]]
0101 verts[7] = [mx[0], mx[1], mx[2]]
0102
0103 indices = np.array([
0104 [0,1,5,3],
0105 [1,4,7,5],
0106 [4,2,6,7],
0107 [0,3,6,2],
0108 [0,2,4,1],
0109 [5,7,6,3]],
0110 dtype=np.int32)
0111 pv_indices = make_pyvista_indices(indices)
0112
0113 faces = verts[indices]
0114 assert verts.shape == (8,3)
0115 assert indices.shape == (6,4)
0116 assert pv_indices.shape == (6,5)
0117 assert faces.shape == (6,4,3)
0118
0119 return verts, faces, pv_indices
0120
0121
0122 def make_cube(cube):
0123 """
0124 :param cube: shape of either (4,3) or (2,3)
0125 """
0126 if cube.shape == (4,3):
0127 return make_cube_oxyz(cube)
0128 elif cube.shape == (2,3):
0129 return make_cube_bbox(cube)
0130 else:
0131 assert 0, ("cube specification array not handled", cube.shape)
0132 pass
0133
0134
0135 if __name__ == '__main__':
0136
0137 oxyz = np.array([(0,0,0), (100,0,0), (0,100,0), (0,0,100)], dtype=np.float32)
0138 bbox = np.array([(0,0,0),(100,100,100)], dtype=np.float32)
0139
0140 points0, faces0, indices0 = make_cube(oxyz)
0141 points1, faces1, indices1 = make_cube(bbox)
0142
0143 assert np.all( points0 == points1 )
0144 assert np.all( faces0 == faces1 )
0145
0146
0147
0148