rendertvtk.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 Common functions for *2tvtk.py
4 '''
5 import math
6 import numpy
7 from collections import defaultdict
8 from tvtk.api import tvtk, write_data
9 from tvtk.common import configure_input
10 
11 
12 
13 def extrude(pts, dx):
14  '''
15  make a 3d set of cells based on ring of pts extruded along X axis by dx
16 
17  Return points and "relative cells"
18 
19 
20  '''
21  pts2 = [ [pt[0]+dx,pt[1],pt[2]] for pt in pts] # the other face
22  all_pts = pts + pts2
23 
24  n = len(pts)
25  top_cell = range(n)
26  bot_cell = range(n, 2*n)
27  cells = [top_cell, bot_cell]
28 
29  # enumerate the sides
30  for ind in range(n):
31  ind2 = (ind+1)%n
32  cell = [top_cell[ind], top_cell[ind2], bot_cell[ind2], bot_cell[ind]]
33  cells.append(cell)
34 
35  return all_pts, cells
36 
37 
38 
39 def orderpoints(pointset):
40  c = [0.0,0.0,0.0]
41  for p in pointset:
42  for i in range(3):
43  c[i] += p[i]
44  n = len(pointset)
45  for i in range(3):
46  c[i] /= n
47 
48  byang = list()
49  for p in pointset:
50  ang = math.atan2(p[2]-c[2], p[1]-c[1]);
51  byang.append((ang, p))
52  byang.sort()
53  return [p for a,p in byang]
54 
55 def blobs(blobs):
56  '''
57  Given a data structure which is a list of blobs, each blob is a dict:
58 
59  {
60  points=[[x1,y1,z1], [x2,y2,z2],...],
61  values=dict(name1=val1, name2=val2),
62  thickness=1.0, # optional
63  }
64 
65  return an unstructured grid
66  '''
67  datasetnames = set()
68  all_points = list()
69  blob_cells = []
70  for blob in blobs:
71  myvalnames = list(blob['values'].keys())
72  datasetnames = datasetnames.union(myvalnames)
73  pts = orderpoints(blob['points'])
74  pts,cells = extrude(pts, blob.get('thickness', 1.0))
75  all_points += pts
76  blob_cells.append((len(pts),cells))
77 
78  ugrid = tvtk.UnstructuredGrid(points=all_points)
79 
80  ptype = tvtk.Polyhedron().cell_type
81 
82  offset = 0
83  for npts,cells in blob_cells:
84  cell_ids = [len(cells)]
85  for cell in cells:
86  cell_ids.append(len(cell))
87  cell_ids += [offset+cid for cid in cell]
88  ugrid.insert_next_cell(ptype, cell_ids)
89  offset += npts
90 
91  ugrid.cell_data.scalars = list(range(len(blobs)))
92  ugrid.cell_data.scalars.name = "indices"
93 
94  print (datasetnames)
95  narrays = 1
96  for datasetname in sorted(datasetnames):
97  arr = [b["values"].get(datasetname, 0.0) for b in blobs]
98  ugrid.cell_data.add_array(arr)
99  ugrid.cell_data.get_array(narrays).name = datasetname
100  narrays += 1
101 
102  return ugrid
103 
104 
105 def points(jdat):
106  '''
107  Given a data strucutre which lists point data as list of objects like:
108  {
109  point= [x,y,z],
110  values=dict(name1=val1, name2=val2, ...)
111  }
112  '''
113  datasetnames = set()
114  for one in jdat:
115  for n in one['values'].keys():
116  datasetnames.add(n)
117 
118  values = defaultdict(list)
119  points = list()
120  indices = list()
121 
122  for count,one in enumerate(jdat):
123  for name in datasetnames:
124  values[name].append(one['values'].get(name, 0.0))
125  pt = one['point']
126  #print (count,pt)
127  points.append(pt)
128  indices.append(count)
129  points = numpy.array(points, 'f')
130  npoints = len(points)
131  #print (points.shape)
132  ret = tvtk.PolyData(points=points)
133  verts = numpy.arange(0, npoints, 1)
134  verts.shape = (npoints,1)
135  ret.verts = verts
136  ret.point_data.scalars = indices[:npoints]
137  ret.point_data.scalars.name = 'indices'
138  for count, datasetname in enumerate(sorted(datasetnames)):
139  count += 1
140  arr = [p["values"].get(datasetname, 0.0) for p in jdat]
141  ret.point_data.add_array(numpy.array(arr, 'f'))
142  ret.point_data.get_array(count).name = datasetname
143 
144  return ret
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def extrude(pts, dx)
Definition: rendertvtk.py:13
def orderpoints(pointset)
Definition: rendertvtk.py:39
def blobs(blobs)
Definition: rendertvtk.py:55
Configuration append(Configuration &a, Configuration &b)
Return an array which is composed of the array b appended to the array a.
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
def points(jdat)
Definition: rendertvtk.py:105