def stuff_vtu(self, outpt): outpt.SetPoints(dsa.VTKArray(np.array(self.points).astype('f4'))) outpt.PointData.append(dsa.VTKArray(np.array(self.V).astype('f4')), 'V') outpt.PointData.append(dsa.VTKArray(np.array(self.PV).astype('f4')), 'PV') outpt.PointData.append(dsa.VTKArray(np.array(self.I).astype('f4')), 'I') ct = dsa.numpyTovtkDataArray(np.array([vtk.VTK_VERTEX]*outpt.GetNumberOfPoints()).astype('u1')) co = dsa.numpy_support.numpy_to_vtkIdTypeArray(np.array(range(0, 2*outpt.GetNumberOfPoints(), 2))) ca = vtk.vtkCellArray() for i in range(outpt.GetNumberOfPoints()): ca.InsertNextCell(1, [i]) outpt.VTKObject.SetCells(ct, co, ca) for v in self.vars: outpt.PointData.append(dsa.VTKArray(np.array(v[2]).astype('f4')), v[0])
def RequestData(): import vtk import numpy as np from vtk.numpy_interface import dataset_adapter as dsa inpt = inputs[0] cl = inpt.GetCellLocations() ct = inpt.GetCellTypes() cells = inpt.GetCells() ncl = [] ncells = [] remap = [] while len(cells) > 0: onids = cells[0] oids = cells[1:onids+1] nids = [oids[0]] + list(oids[factor:-1:factor]) + [oids[-1]] nnids = len(nids) ncell = [nnids] + list(range(len(remap), len(remap)+len(nids))) remap = remap + nids ncells = ncells + ncell cells = cells[onids+1:] new_points = inpt.GetPoints()[remap] ncells = dsa.VTKArray(ncells).astype('i8') ncl = dsa.VTKArray(np.array(ncl)).astype('i8') o = dsa.WrapDataObject(vtk.vtkUnstructuredGrid()) o.SetPoints(dsa.VTKArray(np.array(new_points).astype('f4'))) o.SetCells(ct, ncl, ncells) ipd = inpt.GetPointData() opd = o.GetPointData() for i in ipd.keys(): opd.append(ipd[i][remap], i) icd = inpt.GetCellData() ocd = o.GetCellData() for i in icd.keys(): ocd.append(icd[i], i) if 0 == 1: wrtr = vtk.vtkXMLUnstructuredGridWriter() wrtr.SetFileName('reduced.vtu') wrtr.SetInputData(o.VTKObject) wrtr.Write() else: output.VTKObject.ShallowCopy(o.VTKObject)
def _create_id_array(dataobject, attributeType): """Returns a VTKArray or VTKCompositeDataArray for the ids""" if not dataobject: raise RuntimeError ("dataobject cannot be None") if dataobject.IsA("vtkCompositeDataSet"): ids = [] for ds in dataobject: ids.append(_create_id_array(ds, attributeType)) return dsa.VTKCompositeDataArray(ids) else: return dsa.VTKArray(\ np.arange(dataobject.GetNumberOfElements(attributeType)))
def RequestData(): import random from vtk.numpy_interface import dataset_adapter as dsa from numpy import cross, ascontiguousarray, array, column_stack, arange from numpy.linalg import norm inp = inputs[0] tf = vtk.vtkTriangleFilter() tf.SetInputData(inp.VTKObject) tf.Update() dobj = dsa.WrapDataObject(tf.GetOutput()) nCells = dobj.GetNumberOfCells() tris = dobj.GetPolygons().reshape((-1, 4))[:,1:] points = dobj.GetPoints() p0 = points[tris[:,0]] p1 = points[tris[:,1]] p2 = points[tris[:,2]] areas = norm(cross(p1-p0, p2-p0), axis=1)/2.0 area_per_sample = sum(areas) / nsamples samples_per_triangle = [(a / area_per_sample) for a in areas] samples_per_triangle = [(int(i) + 1) if (random.random() < (i - int(i))) else int(i) for i in samples_per_triangle] sampled_triangles = [[i]*j for i,j in enumerate(samples_per_triangle)] sampled_triangles = [j for i in sampled_triangles for j in i] selected_triangles = tris[sampled_triangles,:] p = points[selected_triangles[:,0]] q = points[selected_triangles[:,1]] r = points[selected_triangles[:,2]] qp = q - p rp = r - p unit = [[random.random(), random.random()] for i in range(len(selected_triangles))] unit = [u if (u[0]+u[1]) < 1 else [1-u[0], 1-u[1]] for u in unit] v = [u[0]*q + u[1]*r for u,q,r in zip(unit, qp, rp)] samples = p + v output.SetPoints(dsa.VTKArray(samples))
def RequestData(): import numpy as np from vtk.numpy_interface import dataset_adapter as dsa from vtk.util import numpy_support as ns ipoints = inputs[0] number_of_glyphs = ipoints.GetNumberOfPoints() glyph = inputs[1] glyph_points = [scale * xscale, scale * yscale, scale * zscale ] * glyph.Points points_per_glyph = glyph_points.shape[0] cells_per_glyph = len(glyph.CellTypes) if forward not in ipoints.PointData.keys(): print 'can\'t find forward array' return U = ipoints.PointData[forward] if up not in ipoints.PointData.keys(): print 'can\'t find up array' return V = ipoints.PointData[up] W = dsa.VTKArray(np.cross(U, V)) l = np.linalg.norm(U, axis=1) U = U / np.where(l == 0, 1.0, l) l = np.linalg.norm(U, axis=1) V = V / np.where(l == 0, 1.0, l) l = np.linalg.norm(W, axis=1) W = W / np.where(l == 0, 1.0, l) P = ipoints.Points p = P[0] u = U[0] v = V[0] w = W[0] opoints = [] for i, p, u, v, w in zip(range(len(P)), P, U, V, W): opoints.append(p + glyph_points[:, 0][:, np.newaxis] * u + glyph_points[:, 1][:, np.newaxis] * v + glyph_points[:, 2][:, np.newaxis] * w) opolys = [glyph.Cells] for i in range(1, len(P)): o = np.zeros(len(glyph.Cells)) k = 0 for j in range(len(glyph.Cells)): if k == 0: k = glyph.Cells[j] o[j] = k else: k = k - 1 o[j] = glyph.Cells[j] + i * points_per_glyph opolys.append(o) opoints = dsa.numpyTovtkDataArray(np.vstack(opoints)) ids = [np.array([i] * points_per_glyph) for i in range(len(P))] ids = dsa.numpyTovtkDataArray(np.vstack(ids).flatten(), name='ID') oug = vtk.vtkUnstructuredGrid() pts = vtk.vtkPoints() pts.SetData(opoints) oug.SetPoints(pts) ct = np.hstack([glyph.CellTypes for i in range(number_of_glyphs)]) co = np.hstack([ glyph.CellLocations + i * len(glyph.Cells) for i in range(number_of_glyphs) ]) opolys = np.hstack(opolys).astype('i8') # print '11111111' # if dbg == 1: # return ct = dsa.numpyTovtkDataArray(ct) co = dsa.numpy_support.numpy_to_vtkIdTypeArray(co) opolys = ns.numpy_to_vtkIdTypeArray(opolys) # print 'XYXYXYXY' # if dbg == 2: # return ca = vtk.vtkCellArray() ca.SetCells(number_of_glyphs * cells_per_glyph, opolys) # print 'BBBBBBBB' # if dbg == 3: # return oug.SetCells(ct, co, ca) oug.GetPointData().AddArray(ids) # print 'CCCCCCC' # if dbg == 4: # return oug.GetPointData().AddArray( dsa.numpyTovtkDataArray(np.vstack( [glyph.PointData['Normals'] for i in range(number_of_glyphs)]), name='Normals')) for n in ipoints.PointData.keys(): if n != 'Normals': a = [[ipoints.PointData[n][i]] * points_per_glyph for i in range(number_of_glyphs)] oug.GetPointData().AddArray( dsa.numpyTovtkDataArray(np.concatenate(a), name=n)) # print 'DDDDDDDDDDDDDDDDDDDDDDDDDDD' # print dir(self) # print 'DDDDDDDDDDDDDDDDDDDDDDDDDDD' # print self.GetUnstructuredGridOutput() # print 'DDDDDDDDDDDDDDDDDDDDDDDDDDD' self.GetUnstructuredGridOutput().Initialize() # print self.GetUnstructuredGridOutput() # print 'DDDDDDDDDDDDDDDDDDDDDDDDDDD' # if dbg == 5: # return self.GetUnstructuredGridOutput().ShallowCopy(oug) # print self.GetUnstructuredGridOutput() return
def RequestData(): import numpy as np from time import time from vtk.numpy_interface import dataset_adapter as dsa from vtk.util import numpy_support as ns ipoints = inputs[0] number_of_glyphs = ipoints.GetNumberOfPoints() glyph = inputs[1] glyph_points = [scale * xscale, scale * yscale, scale * zscale ] * glyph.Points points_per_glyph = glyph_points.shape[0] cells_per_glyph = len(glyph.CellTypes) if forward not in ipoints.PointData.keys(): print('can\'t find forward array') return U = ipoints.PointData[forward] if up not in ipoints.PointData.keys(): print('can\'t find up array') return V = ipoints.PointData[up] W = dsa.VTKArray(np.cross(U, V)) l = np.linalg.norm(U, axis=1) U = U / np.where(l == 0, 1.0, l) l = np.linalg.norm(U, axis=1) V = V / np.where(l == 0, 1.0, l) l = np.linalg.norm(W, axis=1) W = W / np.where(l == 0, 1.0, l) P = ipoints.Points p = P[0] u = U[0] v = V[0] w = W[0] xpts = glyph_points[:, 0][:, np.newaxis] ypts = glyph_points[:, 1][:, np.newaxis] zpts = glyph_points[:, 2][:, np.newaxis] opoints = [] for i, p, u, v, w in zip(range(len(P)), P, U, V, W): opoints.append(p + xpts * u + ypts * v + zpts * w) opolys = [glyph.Cells.reshape(-1, 4)] ijk = glyph.Cells.reshape((-1, 4))[:, 1:4] for i in range(1, len(P)): nijk = np.column_stack( ([3] * ijk.shape[0], ijk + i * points_per_glyph)) opolys.append(nijk) opoints = dsa.numpyTovtkDataArray(np.vstack(opoints)) ids = [np.array([i] * points_per_glyph) for i in range(len(P))] ids = dsa.numpyTovtkDataArray(np.vstack(ids).flatten(), name='ID') oug = vtk.vtkUnstructuredGrid() pts = vtk.vtkPoints() pts.SetData(opoints) oug.SetPoints(pts) ct = np.hstack([glyph.CellTypes for i in range(number_of_glyphs)]) co = np.hstack([ glyph.CellLocations + i * len(glyph.Cells) for i in range(number_of_glyphs) ]) opolys = np.hstack(opolys).astype('i8') ct = dsa.numpyTovtkDataArray(ct) co = dsa.numpy_support.numpy_to_vtkIdTypeArray(co) opolys = ns.numpy_to_vtkIdTypeArray(opolys) ca = vtk.vtkCellArray() ca.SetCells(number_of_glyphs * cells_per_glyph, opolys) oug.SetCells(ct, co, ca) oug.GetPointData().AddArray(ids) oug.GetPointData().AddArray( dsa.numpyTovtkDataArray(np.vstack( [glyph.PointData['Normals'] for i in range(number_of_glyphs)]), name='Normals')) if 'Texture Coordinates' in glyph.PointData.keys(): a = np.vstack([ glyph.PointData['Texture Coordinates'] for i in range(len(ipoints.Points)) ]) oug.GetPointData().SetTCoords(dsa.numpyTovtkDataArray(a)) for n in ipoints.PointData.keys(): if n != 'Normals': a = [[ipoints.PointData[n][i]] * points_per_glyph for i in range(number_of_glyphs)] oug.GetPointData().AddArray( dsa.numpyTovtkDataArray(np.concatenate(a), name=n)) self.GetUnstructuredGridOutput().Initialize() self.GetUnstructuredGridOutput().ShallowCopy(oug) return
s_yz = inputs[0].PointData['stress_yz'] s_xz = inputs[0].PointData['stress_xz'] direction = [] sigma_d = [] for i in range(len(s_xx)): stress = np.array ( [[s_xx[i], s_xy[i], \ s_xz[i]], [s_xy[i], s_yy[i], s_yz[i] ], [s_xz[i], \ s_yz[i], s_zz[i] ]] ) u, v = np.linalg.eig(stress) indx = np.argsort(u) sigma_d.append(u[indx[2]] - u[indx[0]]) direction.append(v[indx[2]].tolist()) vtk_arr = da.VTKArray(direction) output.PointData.append(vtk_arr, "direction_1") ####### routine for stress rotation and coloumb stress calculation for j in range(0, 90, 10): theta = np.radians(j) coloumb = [] phi = np.radians(40) # along z # rotation matrix for rotation about z R = [[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]]
stress = np.array ( [[s_xx[i], s_xy[i], \ s_xz[i]], [s_xy[i], s_yy[i], s_yz[i] ], [s_xz[i], \ s_yz[i], s_zz[i] ]] ) u, v = np.linalg.eig(stress) indx = np.argsort(u) sigma_d.append(u[indx[2]] - u[indx[0]]) sigma_3.append(v[indx[2]].tolist()) sigma_1.append(v[indx[0]].tolist()) sigma_2.append(v[indx[1]].tolist()) mag_3.append(u[indx[2]].tolist()) mag_1.append(u[indx[0]].tolist()) mag_2.append(u[indx[1]].tolist()) vtk_arr = da.VTKArray(sigma_3) vtk_arr2 = da.VTKArray(sigma_2) vtk_arr3 = da.VTKArray(sigma_1) output.PointData.append(vtk_arr, "sigma_1") output.PointData.append(vtk_arr2, "sigma_2") output.PointData.append(vtk_arr3, "sigma_3") output.PointData.append(np.array(mag_3), "mag_3") output.PointData.append(np.array(mag_2), "mag_2") output.PointData.append(np.array(mag_1), "mag_1") output.PointData.append(np.array(sigma_d), "sigma_d") ####### routine for stress rotation and coloumb stress calculation