Esempio n. 1
0
    def RequestData(self, request: str, inInfo: vtkInformation,
                    outInfo: vtkInformation) -> int:
        input_src = vtkImageData.GetData(inInfo[0])
        output = vtkImageData.GetData(outInfo)
        ext = input_src.GetExtent()
        output.ShallowCopy(input_src)

        error_map = DitheringEnum.FloydSteinberg.value

        for j in range(ext[2], ext[3] + 1):
            for i in range(ext[0], ext[1] + 1):
                pix_val = output.GetScalarComponentAsDouble(i, j, ext[4], 0)
                new_val = self.closest_color(pix_val)
                error = pix_val - new_val
                if error != 0:
                    output.SetScalarComponentFromDouble(
                        i, j, ext[4], 0, new_val)
                    for m in error_map:
                        di = i + m[0]
                        dj = j + m[1]
                        derror = m[2]

                        if ext[0] <= di and di <= ext[1] and ext[
                                2] <= dj and dj <= ext[3]:
                            old_val = output.GetScalarComponentAsDouble(
                                di, dj, ext[4], 0)
                            quantization = old_val + error * derror
                            output.SetScalarComponentFromDouble(
                                di, dj, ext[4], 0, quantization)

        return 1
Esempio n. 2
0
 def RequestInformation(self, request: str, inInfo: vtkInformation,
                        outInfo: vtkInformation) -> int:
     input_src = vtkImageData.GetData(inInfo[0])
     ext = input_src.GetExtent()
     o_info = outInfo.GetInformationObject(0)
     o_info.Set(vtkStreamingDemandDrivenPipeline.WHOLE_EXTENT(), ext, 6)
     return 1
    def RequestData(self, request, inInfo, outInfo):
        output = vtkImageData.GetData(outInfo, 0)
        if output is None:
            return 0

        # create 3D coordinate grid
        xs = np.linspace(0.0, 2.0, self.Dimensions[0])
        ys = np.linspace(0.0, 1.0, self.Dimensions[1])
        ts = np.linspace(0.0, 10.0, self.Dimensions[2])
        x, y, t = np.meshgrid(xs, ys, ts, indexing='ij')

        # allocate image data output
        output.SetDimensions(self.Dimensions)
        output.SetSpacing(abs(xs[1] - xs[0]), abs(ys[1] - ys[0]),
                          abs(ts[1] - ts[0]))
        output.SetOrigin(xs[0], ys[0], ts[0])
        output = dsa.WrapDataObject(output)

        # double gyre (https://shaddenlab.berkeley.edu/uploads/LCS-tutorial/examples.html)
        a = self.eps * np.sin(2.0 * np.pi / self.T * t)
        b = 1.0 - 2.0 * self.eps * np.sin(2.0 * np.pi / self.T * t)
        f = a * x**2 + b * x
        df = 2.0 * a * x + b
        dx = -np.pi * self.A * np.sin(np.pi * f) * np.cos(np.pi * y)
        dy = np.pi * self.A * np.cos(np.pi * f) * np.sin(np.pi * y) * df
        dt = np.full_like(t, 1.0)

        # stack and flatten such that layout matches VTK
        v = np.stack([dx, dy, dt], axis=-1)
        v = v.reshape((-1, 3), order='F')
        output.PointData.append(v, 'v')

        # compute and output velocity magnitude
        v_mag = np.linalg.norm(v[..., :2], axis=-1)
        output.PointData.append(v_mag, 'v_mag')

        return 1
    def RequestData(self, request, inInfoVec, outInfoVec):
        import sys, os
        import vtk
        from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkImageData, vtkDataSet
        from vtk.numpy_interface import dataset_adapter as dsa
        import ctypes as C
        import numpy as np

        if 'HOME' in os.environ:
            ccode_so = os.environ['HOME'] + '/python/_sample.so'
        elif 'HOMEPATH' in os.environ:
            ccode_so = os.environ['HOMEPATH'] + '/python/_sample.so'
        else:
            code_so = '_sample.so'

        if not os.path.isfile(ccode_so):
            print('can\'t find so:', ccode_so)
            return

        ccode = C.CDLL(ccode_so)
        if not ccode:
            print('failed to load ', ccode.so)

        ccode.SampleTetrahedra.argtypes = [
            C.c_int, C.c_int, C.c_int, C.c_int,
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS")
        ]

        ccode.SampleRectilinear.argtypes = [
            C.c_int, C.c_int,
            np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS")
        ]

        ccode.SampleVTI.argtypes = [
            C.c_int, C.c_int,
            np.ctypeslib.ndpointer(C.c_int, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS"),
            np.ctypeslib.ndpointer(C.c_float, flags="C_CONTIGUOUS")
        ]

        ccode.GetNumberOfSamples.restype = C.c_int

        ccode.GetSamples.restype = C.c_void_p

        obj = dsa.WrapDataObject(vtkDataSet.GetData(inInfoVec[0], 0))

        pdf = self.inputArray
        nSamples = self.nSamples

        if obj.GetClassName() == 'vtkImageData':

            a = vtkImageData.GetData(inInfoVec[0], 0)
            vti = dsa.WrapDataObject(vtkImageData.GetData(inInfoVec[0], 0))
            e = vti.VTKObject.GetExtent()
            o = vti.VTKObject.GetOrigin()
            s = vti.VTKObject.GetSpacing()
            dimensions = np.array([(e[2 * i + 1] - e[2 * i]) + 1
                                   for i in range(3)]).astype('i4')
            origin = np.array([o[i] + e[2 * i] * s[i]
                               for i in range(3)]).astype('f4')
            spacing = np.array(s).astype('f4')
            pdf = np.ascontiguousarray(vti.CellData[pdf]).astype('f4')
            ccode.SampleVTI(0, nSamples, dimensions, origin, spacing, pdf)

        elif obj.GetClassName() == 'vtkUnstructuredGrid':

            vtu = dsa.WrapDataObject(
                vtkUnstructuredGrid.GetData(inInfoVec[0], 0))
            points = np.ascontiguousarray(vtu.Points).astype('f4')
            pdf = np.ascontiguousarray(vtu.CellData[pdf]).astype('f4')
            tets = np.ascontiguousarray(vtu.Cells).astype('i4')
            ccode.SampleTetrahedra(0, nSamples, vtu.GetNumberOfPoints(),
                                   vtu.GetNumberOfCells(), points, pdf, tets)

        elif obj.GetClassName() == 'vtkRectilinearGrid':

            vtr = vtkRectilinearGrid.GetData(inInfoVec[0], 0)
            x_array = dsa.numpy_support.vtk_to_numpy(
                vtr.GetXCoordinates()).astype('f4')
            y_array = dsa.numpy_support.vtk_to_numpy(
                vtr.GetYCoordinates()).astype('f4')
            z_array = dsa.numpy_support.vtk_to_numpy(
                vtr.GetZCoordinates()).astype('f4')
            pdf = dsa.numpy_support.vtk_to_numpy(
                vtr.GetCellData().GetArray(pdf)).astype('f4')
            dim = (len(x_array), len(y_array), len(z_array))
            ccode.SampleRectilinear(0, dim, x_array, y_array, z_array, pdf)

        else:
            print(
                "SampleCellDensity requires vtkImageData or vtkUnstructuredGrid"
            )
            return 0

        n = ccode.GetNumberOfSamples()
        s = np.ctypeslib.as_array(C.cast(ccode.GetSamples(),
                                         C.POINTER(C.c_float)),
                                  shape=(n, 3))
        samples = dsa.WrapDataObject(vtk.vtkUnstructuredGrid())
        co = dsa.numpy_support.numpy_to_vtkIdTypeArray(
            np.arange(n).astype('i8') * 2)
        ca = vtk.vtkCellArray()
        ca.SetCells(
            n,
            dsa.numpy_support.numpy_to_vtkIdTypeArray(
                np.column_stack(([1] * n, range(n))).reshape(
                    (2 * n, )).astype('i8')))
        ct = dsa.numpyTovtkDataArray(
            np.array([vtk.VTK_VERTEX] * n).astype('u1'))
        samples.VTKObject.SetCells(ct, co, ca)
        samples.Points = dsa.numpy_support.numpy_to_vtk(s, deep=1)

        outpt = vtkUnstructuredGrid.GetData(outInfoVec, 0)
        outpt.ShallowCopy(samples.VTKObject)

        ccode.Cleanup()
        return 1
Esempio n. 5
0
 def RequestData(self, request: str, inInfo: vtkInformation,
                 outInfo: vtkInformation) -> int:
     input_src = vtkPolyData.GetData(inInfo[0])
     output = vtkImageData.GetData(outInfo)
     output.ShallowCopy(self.slicer.slice_object(input_src))
     return 1