コード例 #1
0
    def _export_surface(self, filename, filetype):
        if filetype in (const.FILETYPE_STL, const.FILETYPE_VTP,
                        const.FILETYPE_PLY, const.FILETYPE_STL_ASCII):
            # First we identify all surfaces that are selected
            # (if any)
            proj = prj.Project()
            polydata_list = []

            for index in proj.surface_dict:
                surface = proj.surface_dict[index]
                if surface.is_shown:
                    polydata_list.append(surface.polydata)

            if len(polydata_list) == 0:
                utl.debug("oops - no polydata")
                return
            elif len(polydata_list) == 1:
                polydata = polydata_list[0]
            else:
                polydata = pu.Merge(polydata_list)

            if polydata.GetNumberOfPoints() == 0:
                raise ValueError

            # Having a polydata that represents all surfaces
            # selected, we write it, according to filetype
            if filetype == const.FILETYPE_STL:
                writer = vtkSTLWriter()
                writer.SetFileTypeToBinary()
            elif filetype == const.FILETYPE_STL_ASCII:
                writer = vtkSTLWriter()
                writer.SetFileTypeToASCII()
            elif filetype == const.FILETYPE_VTP:
                writer = vtkXMLPolyDataWriter()
            #elif filetype == const.FILETYPE_IV:
            #    writer = vtkIVWriter()
            elif filetype == const.FILETYPE_PLY:
                writer = vtkPLYWriter()
                writer.SetFileTypeToASCII()
                writer.SetColorModeToOff()
                #writer.SetDataByteOrderToLittleEndian()
                #writer.SetColorModeToUniformCellColor()
                #writer.SetColor(255, 0, 0)

            if filetype in (const.FILETYPE_STL, const.FILETYPE_STL_ASCII,
                            const.FILETYPE_PLY):
                # Invert normals
                normals = vtkPolyDataNormals()
                normals.SetInputData(polydata)
                normals.SetFeatureAngle(80)
                normals.AutoOrientNormalsOn()
                #  normals.GetOutput().ReleaseDataFlagOn()
                normals.UpdateInformation()
                normals.Update()
                polydata = normals.GetOutput()

            filename = filename.encode(const.FS_ENCODE)
            writer.SetFileName(filename)
            writer.SetInputData(polydata)
            writer.Write()
コード例 #2
0
def main():
    reader = vtkXMLImageDataReader()
    source = MyImageSource()
    algorithm = MyAlgorithm()
    writer = vtkXMLPolyDataWriter()

    if len(sys.argv) == 5:
        reader.SetFileName(sys.argv[1])
        algorithm.SetInputConnection(reader.GetOutputPort())
        algorithm.SetInputArrayToProcess(
            0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_POINTS, sys.argv[2])
        algorithm.SetMultiplier(float(sys.argv[3]))
        writer.SetFileName(sys.argv[4])
    elif len(sys.argv) == 3:
        algorithm.SetInputConnection(source.GetOutputPort())
        algorithm.SetInputArrayToProcess(
            0, 0, 0, vtkDataObject.FIELD_ASSOCIATION_POINTS, 'v_mag')
        algorithm.SetMultiplier(float(sys.argv[1]))
        writer.SetFileName(sys.argv[2])
    else:
        print('Usage: {} [IN_FILE IN_ARRAY] MULTIPLIER OUT_FILE'.format(
            sys.argv[0]))
        sys.exit(1)

    writer.SetInputConnection(algorithm.GetOutputPort())
    writer.Update()
コード例 #3
0
ファイル: vtk.py プロジェクト: manymachines/cadquery
def exportVTP(shape: Shape,
              fname: str,
              tolerance: float = 0.1,
              angularTolerance: float = 0.1):

    writer = vtkXMLPolyDataWriter()
    writer.SetFileName(fname)
    writer.SetInputData(shape.toVtkPolyData(tolerance, angularTolerance))
    writer.Write()
コード例 #4
0
 def save_image(self):
     today = datetime.now()
     today_str = today.strftime("%Y-%m-%d_%H:%M:%S.%f")
     writer = vtkXMLPolyDataWriter()
     writer.SetFileName(self.__prefix + "animal_" +
                        self.__txt_box_idx.text() + "_img_" +
                        str(self.__counter) + "_3D_" + today_str + ".vtp")
     writer.SetInputData(self.pd_collection[0])
     writer.Write()
     self.__counter += 1
コード例 #5
0
ファイル: vtk.py プロジェクト: manymachines/cadquery
def toString(shape: Shape,
             tolerance: float = 1e-3,
             angularTolerance: float = 0.1) -> str:

    writer = vtkXMLPolyDataWriter()
    writer.SetWriteToOutputString(True)
    writer.SetInputData(shape.toVtkPolyData(tolerance, angularTolerance))
    writer.Write()

    return writer.GetOutputString()
コード例 #6
0
def Export(polydata, filename, bin=False):
    writer = vtkXMLPolyDataWriter()
    if _has_win32api:
        touch(filename)
        filename = win32api.GetShortPathName(filename)
    writer.SetFileName(filename.encode(const.FS_ENCODE))
    if bin:
        writer.SetDataModeToBinary()
    else:
        writer.SetDataModeToAscii()
    writer.SetInputData(polydata)
    writer.Write()
コード例 #7
0
    def save_3d_image(self):
        for i in range(nr_devices):
            writer = vtkXMLPolyDataWriter()
            path_cam = self.__path + serial_numbers[i] + "/3D/"
            path_exists = os.path.isdir(path_cam)
            today_str = datetime.now().strftime("%H:%M:%S.%f")

            if not path_exists:
                os.makedirs(path_cam)

            f_name = path_cam + self.__txt_box_3d_idx.text() + "_img_" + str(self.__counter) + "_" + today_str + ".vtp"

            writer.SetFileName(f_name)
            writer.SetInputData(self.pd_collection[i])
            writer.Write()

        self.__counter += 1
コード例 #8
0
ファイル: postprocess.py プロジェクト: knappa/nlisim
def generate_vtk(state: State, postprocess_step_dir: Path):
    volume, molecules, cells = generate_vtk_objects(state)

    grid_writer = vtkXMLImageDataWriter()
    grid_writer.SetDataModeToBinary()
    grid_writer.SetFileName(str(postprocess_step_dir / 'geometry_001.vti'))
    grid_writer.SetInputData(volume)
    grid_writer.Write()

    grid_writer.SetFileName(str(postprocess_step_dir / 'molecules_001.vti'))
    grid_writer.SetInputData(molecules)
    grid_writer.Write()

    cell_writer = vtkXMLPolyDataWriter()
    cell_writer.SetDataModeToBinary()
    for module, data in cells.items():
        cell_writer.SetFileName(str(postprocess_step_dir / f'{module}_001.vtp'))
        cell_writer.SetInputData(data)
        cell_writer.Write()
コード例 #9
0
def create_surface_piece(filename, shape, dtype, mask_filename, mask_shape,
                         mask_dtype, roi, spacing, mode, min_value, max_value,
                         decimate_reduction, smooth_relaxation_factor,
                         smooth_iterations, language, flip_image, from_binary,
                         algorithm, imagedata_resolution, fill_border_holes):

    log_path = tempfile.mktemp('vtkoutput.txt')
    fow = vtkFileOutputWindow()
    fow.SetFileName(log_path)
    ow = vtkOutputWindow()
    ow.SetInstance(fow)

    pad_bottom = (roi.start == 0)
    pad_top = (roi.stop >= shape[0])

    if fill_border_holes:
        padding = (1, 1, pad_bottom)
    else:
        padding = (0, 0, 0)

    if from_binary:
        mask = numpy.memmap(mask_filename,
                            mode='r',
                            dtype=mask_dtype,
                            shape=mask_shape)
        if fill_border_holes:
            a_mask = pad_image(mask[roi.start + 1:roi.stop + 1, 1:, 1:], 0,
                               pad_bottom, pad_top)
        else:
            a_mask = numpy.array(mask[roi.start + 1:roi.stop + 1, 1:, 1:])
        image = converters.to_vtk(a_mask,
                                  spacing,
                                  roi.start,
                                  "AXIAL",
                                  padding=padding)
        del a_mask
    else:
        image = numpy.memmap(filename, mode='r', dtype=dtype, shape=shape)
        mask = numpy.memmap(mask_filename,
                            mode='r',
                            dtype=mask_dtype,
                            shape=mask_shape)
        if fill_border_holes:
            a_image = pad_image(image[roi],
                                numpy.iinfo(image.dtype).min, pad_bottom,
                                pad_top)
        else:
            a_image = numpy.array(image[roi])
        #  if z_iadd:
        #  a_image[0, 1:-1, 1:-1] = image[0]
        #  if z_eadd:
        #  a_image[-1, 1:-1, 1:-1] = image[-1]

        if algorithm == u'InVesalius 3.b2':
            a_mask = numpy.array(mask[roi.start + 1:roi.stop + 1, 1:, 1:])
            a_image[a_mask == 1] = a_image.min() - 1
            a_image[a_mask == 254] = (min_value + max_value) / 2.0

            image = converters.to_vtk(a_image,
                                      spacing,
                                      roi.start,
                                      "AXIAL",
                                      padding=padding)

            gauss = vtkImageGaussianSmooth()
            gauss.SetInputData(image)
            gauss.SetRadiusFactor(0.3)
            gauss.ReleaseDataFlagOn()
            gauss.Update()

            del image
            image = gauss.GetOutput()
            del gauss
            del a_mask
        else:
            #  if z_iadd:
            #  origin = -spacing[0], -spacing[1], -spacing[2]
            #  else:
            #  origin = 0, -spacing[1], -spacing[2]
            image = converters.to_vtk(a_image,
                                      spacing,
                                      roi.start,
                                      "AXIAL",
                                      padding=padding)
        del a_image

    #  if imagedata_resolution:
    #  image = ResampleImage3D(image, imagedata_resolution)

    flip = vtkImageFlip()
    flip.SetInputData(image)
    flip.SetFilteredAxis(1)
    flip.FlipAboutOriginOn()
    flip.ReleaseDataFlagOn()
    flip.Update()

    #  writer = vtkXMLImageDataWriter()
    #  writer.SetFileName('/tmp/camboja.vti')
    #  writer.SetInputData(flip.GetOutput())
    #  writer.Write()

    del image
    image = flip.GetOutput()
    del flip

    contour = vtkContourFilter()
    contour.SetInputData(image)
    if from_binary:
        contour.SetValue(0, 127)  # initial threshold
    else:
        contour.SetValue(0, min_value)  # initial threshold
        contour.SetValue(1, max_value)  # final threshold
    #  contour.ComputeScalarsOn()
    #  contour.ComputeGradientsOn()
    #  contour.ComputeNormalsOn()
    contour.ReleaseDataFlagOn()
    contour.Update()

    polydata = contour.GetOutput()
    del image
    del contour

    filename = tempfile.mktemp(suffix='_%d_%d.vtp' % (roi.start, roi.stop))
    writer = vtkXMLPolyDataWriter()
    writer.SetInputData(polydata)
    writer.SetFileName(filename)
    writer.Write()

    print("Writing piece", roi, "to", filename)
    print("MY PID MC", os.getpid())
    return filename
コード例 #10
0
def join_process_surface(filenames, algorithm, smooth_iterations,
                         smooth_relaxation_factor, decimate_reduction,
                         keep_largest, fill_holes, options, msg_queue):
    def send_message(msg):
        try:
            msg_queue.put_nowait(msg)
        except queue.Full as e:
            print(e)

    log_path = tempfile.mktemp('vtkoutput.txt')
    fow = vtkFileOutputWindow()
    fow.SetFileName(log_path)
    ow = vtkOutputWindow()
    ow.SetInstance(fow)

    send_message('Joining surfaces ...')
    polydata_append = vtkAppendPolyData()
    for f in filenames:
        reader = vtkXMLPolyDataReader()
        reader.SetFileName(f)
        reader.Update()

        polydata = reader.GetOutput()

        polydata_append.AddInputData(polydata)
        del reader
        del polydata

    polydata_append.Update()
    #  polydata_append.GetOutput().ReleaseDataFlagOn()
    polydata = polydata_append.GetOutput()
    #polydata.Register(None)
    #  polydata.SetSource(None)
    del polydata_append

    send_message('Cleaning surface ...')
    clean = vtkCleanPolyData()
    #  clean.ReleaseDataFlagOn()
    #  clean.GetOutput().ReleaseDataFlagOn()
    clean_ref = weakref.ref(clean)
    #  clean_ref().AddObserver("ProgressEvent", lambda obj,evt:
    #  UpdateProgress(clean_ref(), _("Creating 3D surface...")))
    clean.SetInputData(polydata)
    clean.PointMergingOn()
    clean.Update()

    del polydata
    polydata = clean.GetOutput()
    #  polydata.SetSource(None)
    del clean

    if algorithm == 'ca_smoothing':
        send_message('Calculating normals ...')
        normals = vtkPolyDataNormals()
        normals_ref = weakref.ref(normals)
        #  normals_ref().AddObserver("ProgressEvent", lambda obj,evt:
        #  UpdateProgress(normals_ref(), _("Creating 3D surface...")))
        normals.SetInputData(polydata)
        #  normals.ReleaseDataFlagOn()
        #normals.SetFeatureAngle(80)
        #normals.AutoOrientNormalsOn()
        normals.ComputeCellNormalsOn()
        #  normals.GetOutput().ReleaseDataFlagOn()
        normals.Update()
        del polydata
        polydata = normals.GetOutput()
        #  polydata.SetSource(None)
        del normals

        clean = vtkCleanPolyData()
        #  clean.ReleaseDataFlagOn()
        #  clean.GetOutput().ReleaseDataFlagOn()
        clean_ref = weakref.ref(clean)
        #  clean_ref().AddObserver("ProgressEvent", lambda obj,evt:
        #  UpdateProgress(clean_ref(), _("Creating 3D surface...")))
        clean.SetInputData(polydata)
        clean.PointMergingOn()
        clean.Update()

        del polydata
        polydata = clean.GetOutput()
        #  polydata.SetSource(None)
        del clean

        #  try:
        #  polydata.BuildLinks()
        #  except TypeError:
        #  polydata.BuildLinks(0)
        #  polydata = ca_smoothing.ca_smoothing(polydata, options['angle'],
        #  options['max distance'],
        #  options['min weight'],
        #  options['steps'])

        send_message('Context Aware smoothing ...')
        mesh = cy_mesh.Mesh(polydata)
        cy_mesh.ca_smoothing(mesh, options['angle'], options['max distance'],
                             options['min weight'], options['steps'])
        #  polydata = mesh.to_vtk()

        #  polydata.SetSource(None)
        #  polydata.DebugOn()
    #  else:
    #  #smoother = vtkWindowedSincPolyDataFilter()
    #  send_message('Smoothing ...')
    #  smoother = vtkSmoothPolyDataFilter()
    #  smoother_ref = weakref.ref(smoother)
    #  #  smoother_ref().AddObserver("ProgressEvent", lambda obj,evt:
    #  #  UpdateProgress(smoother_ref(), _("Creating 3D surface...")))
    #  smoother.SetInputData(polydata)
    #  smoother.SetNumberOfIterations(smooth_iterations)
    #  smoother.SetRelaxationFactor(smooth_relaxation_factor)
    #  smoother.SetFeatureAngle(80)
    #  #smoother.SetEdgeAngle(90.0)
    #  #smoother.SetPassBand(0.1)
    #  smoother.BoundarySmoothingOn()
    #  smoother.FeatureEdgeSmoothingOn()
    #  #smoother.NormalizeCoordinatesOn()
    #  #smoother.NonManifoldSmoothingOn()
    #  #  smoother.ReleaseDataFlagOn()
    #  #  smoother.GetOutput().ReleaseDataFlagOn()
    #  smoother.Update()
    #  del polydata
    #  polydata = smoother.GetOutput()
    #  #polydata.Register(None)
    #  #  polydata.SetSource(None)
    #  del smoother

    if not decimate_reduction:
        print("Decimating", decimate_reduction)
        send_message('Decimating ...')
        decimation = vtkQuadricDecimation()
        #  decimation.ReleaseDataFlagOn()
        decimation.SetInputData(polydata)
        decimation.SetTargetReduction(decimate_reduction)
        decimation_ref = weakref.ref(decimation)
        #  decimation_ref().AddObserver("ProgressEvent", lambda obj,evt:
        #  UpdateProgress(decimation_ref(), _("Creating 3D surface...")))
        #decimation.PreserveTopologyOn()
        #decimation.SplittingOff()
        #decimation.BoundaryVertexDeletionOff()
        #  decimation.GetOutput().ReleaseDataFlagOn()
        decimation.Update()
        del polydata
        polydata = decimation.GetOutput()
        #polydata.Register(None)
        #  polydata.SetSource(None)
        del decimation

    #to_measure.Register(None)
    #  to_measure.SetSource(None)

    if keep_largest:
        send_message('Finding the largest ...')
        conn = vtkPolyDataConnectivityFilter()
        conn.SetInputData(polydata)
        conn.SetExtractionModeToLargestRegion()
        conn_ref = weakref.ref(conn)
        #  conn_ref().AddObserver("ProgressEvent", lambda obj,evt:
        #  UpdateProgress(conn_ref(), _("Creating 3D surface...")))
        conn.Update()
        #  conn.GetOutput().ReleaseDataFlagOn()
        del polydata
        polydata = conn.GetOutput()
        #polydata.Register(None)
        #  polydata.SetSource(None)
        del conn

    #Filter used to detect and fill holes. Only fill boundary edges holes.
    #TODO: Hey! This piece of code is the same from
    #polydata_utils.FillSurfaceHole, we need to review this.
    if fill_holes:
        send_message('Filling holes ...')
        filled_polydata = vtkFillHolesFilter()
        #  filled_polydata.ReleaseDataFlagOn()
        filled_polydata.SetInputData(polydata)
        filled_polydata.SetHoleSize(300)
        filled_polydata_ref = weakref.ref(filled_polydata)
        #  filled_polydata_ref().AddObserver("ProgressEvent", lambda obj,evt:
        #  UpdateProgress(filled_polydata_ref(), _("Creating 3D surface...")))
        filled_polydata.Update()
        #  filled_polydata.GetOutput().ReleaseDataFlagOn()
        del polydata
        polydata = filled_polydata.GetOutput()
        #polydata.Register(None)
        #  polydata.SetSource(None)
        #  polydata.DebugOn()
        del filled_polydata

    to_measure = polydata

    normals = vtkPolyDataNormals()
    #  normals.ReleaseDataFlagOn()
    #  normals_ref = weakref.ref(normals)
    #  normals_ref().AddObserver("ProgressEvent", lambda obj,evt:
    #  UpdateProgress(normals_ref(), _("Creating 3D surface...")))
    normals.SetInputData(polydata)
    normals.SetFeatureAngle(80)
    normals.SplittingOn()
    normals.AutoOrientNormalsOn()
    normals.NonManifoldTraversalOn()
    normals.ComputeCellNormalsOn()
    #  normals.GetOutput().ReleaseDataFlagOn()
    normals.Update()
    del polydata
    polydata = normals.GetOutput()
    #polydata.Register(None)
    #  polydata.SetSource(None)
    del normals

    #  # Improve performance
    #  stripper = vtkStripper()
    #  #  stripper.ReleaseDataFlagOn()
    #  #  stripper_ref = weakref.ref(stripper)
    #  #  stripper_ref().AddObserver("ProgressEvent", lambda obj,evt:
    #  #  UpdateProgress(stripper_ref(), _("Creating 3D surface...")))
    #  stripper.SetInputData(polydata)
    #  stripper.PassThroughCellIdsOn()
    #  stripper.PassThroughPointIdsOn()
    #  #  stripper.GetOutput().ReleaseDataFlagOn()
    #  stripper.Update()
    #  del polydata
    #  polydata = stripper.GetOutput()
    #  #polydata.Register(None)
    #  #  polydata.SetSource(None)
    #  del stripper

    send_message('Calculating area and volume ...')
    measured_polydata = vtkMassProperties()
    measured_polydata.SetInputData(to_measure)
    measured_polydata.Update()
    volume = float(measured_polydata.GetVolume())
    area = float(measured_polydata.GetSurfaceArea())
    del measured_polydata

    filename = tempfile.mktemp(suffix='_full.vtp')
    writer = vtkXMLPolyDataWriter()
    writer.SetInputData(polydata)
    writer.SetFileName(filename)
    writer.Write()
    del writer

    print("MY PID", os.getpid())
    return filename, {'volume': volume, 'area': area}