blob2tvtk.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 A tvtk based converter for JSON file dumped from tests
4 
5 Convert a bunch of individual blobs JSON files into one:
6 
7 jq -s . test-pdsp-*.json|jq '{blobs:[.[]|.blobs|.[0]]}' > test-pdsp.json
8 
9 '''
10 import os
11 import json
12 import numpy
13 from tvtk.api import tvtk, write_data
14 import rendertvtk
15 
16 
17 
18 def main(outname=None, *infiles):
19  if outname is None or outname == "-":
20  outname = os.path.splitext(infile)[0]
21 
22  jblobs = list()
23  jpoints = list()
24  for infile in infiles:
25  jdat = json.loads(open(infile).read())
26  jblobs += jdat["blobs"]
27  jpoints += jdat.get("points", [])
28 
29  blobdata = rendertvtk.blobs(jblobs)
30  #w = tvtk.XMLUnstructuredGridWriter(file_name=outfile)
31  #w = tvtk.UnstructuredGridWriter(file_name=outfile)
32  #configure_input(w, ugrid)
33  #w.write()
34  ofile = outname + '-blobs.vtu'
35  print (ofile)
36  write_data(blobdata, ofile)
37 
38  ### see depo2tvtk.py for rendering depos from NumpyDepoSaver
39  if not jpoints:
40  print ("no points to convert")
41  return
42  pointdata = rendertvtk.points(jpoints)
43  ofile = outname + "-points.vtp"
44  print(ofile)
45  write_data(pointdata, ofile)
46  #visualize_blobs(blobdata)
47 
48 def visualize_blobs(blobdata):
49  '''
50  Make a mayavi pipeline to produce an immediate display.
51  '''
52  print ("visualize")
53  from mayavi import mlab
54  #s = mlab.points3d(x,y,z)
55  mlab.pipeline.surface(blobdata)
56  mlab.show()
57 
58 
59 def test(filename = "blob2tvtk_test.vtk"):
60  points = numpy.asarray([
61  [0.0,0.0,0.0],
62  [0.0,0.0,1.0],
63  [0.0,1.0,1.0],
64  [0.0,1.0,0.0],
65 
66  [1.0,0.0,0.0],
67  [1.0,0.0,1.0],
68  [1.0,1.0,1.0],
69  [1.0,1.0,0.0],
70 
71  ])
72 
73  ugrid = tvtk.UnstructuredGrid(points=points)
74 
75  # #faces, #pts, pt1, pt2, ... #pts, pt1, pt2, ..., ...
76  pt_ids = [6]
77  pt_ids.append(4)
78  pt_ids += list(range(4))
79  pt_ids.append(4)
80  pt_ids += list(range(4,8))
81  for ind in range(4):
82  ind2 = (ind+1)%4
83  pt_ids.append(4)
84  pt_ids += [ind, ind2, 4+ind2, 4+ind]
85  print (pt_ids)
86 
87  ptype = tvtk.Polyhedron().cell_type
88  ugrid.insert_next_cell(ptype, pt_ids)
89 
90  ugrid.cell_data.scalars = [1.0]
91  ugrid.cell_data.scalars.name = "unity"
92  # exercise multiple values
93  ugrid.cell_data.add_array([2.0])
94  ugrid.cell_data.get_array(1).name = "duality"
95  ugrid.cell_data.add_array([3.0])
96  ugrid.cell_data.get_array(2).name = "trinity"
97  #w = tvtk.XMLUnstructuredGridWriter(file_name=filename)
98  w = tvtk.UnstructuredGridWriter(file_name=filename)
99  configure_input(w, ugrid)
100  w.write()
101 
102 if '__main__' == __name__:
103  import sys
104  main(*sys.argv[1:])
105 
106 
def test(filename="blob2tvtk_test.vtk")
Definition: blob2tvtk.py:59
int open(const char *, int)
Opens a file descriptor.
def visualize_blobs(blobdata)
Definition: blob2tvtk.py:48
std::enable_if< internal::is_string< String >::value >::type print(std::FILE *f, const text_style &ts, const String &format_str, const Args &...args)
Definition: color.h:549
def blobs(blobs)
Definition: rendertvtk.py:55
int read(int, char *, size_t)
Read bytes from a file descriptor.
def points(jdat)
Definition: rendertvtk.py:105
def main(outname=None, infiles)
Definition: blob2tvtk.py:18