예제 #1
0
def sample(dataDir, outputDir):
    convert(os.path.join(dataDir, 'Data/bot2.wrl'), outputDir, True, True)
    convert(os.path.join(dataDir, 'Data/can.ex2'), outputDir)
    convert(os.path.join(dataDir, 'Data/can.ex2'), outputDir, True, True,
            'can_MS.ex2')
    convert(os.path.join(dataDir, 'Data/can.ex2'), outputDir, True, False,
            'can_M.ex2')
    convert(os.path.join(dataDir, 'Data/can.ex2'), outputDir, False, True,
            'can_S.ex2')
    convert(os.path.join(dataDir, 'Data/disk_out_ref.ex2'), outputDir, True,
            False, 'disk_out_ref_M.ex2')
    convert(os.path.join(dataDir, 'Data/disk_out_ref.ex2'), outputDir)
    convert(os.path.join(dataDir, 'Data/RectGrid2.vtk'), outputDir)

    # Create image data based on the Wavelet source
    wavelet = simple.Wavelet()
    wavelet.UpdatePipeline()
    imageData = wavelet.GetClientSideObject().GetOutputDataObject(0)
    writeDataSet('Wavelet.vti', imageData, outputDir)

    # Create a table based on the disk_out_ref
    diskout = simple.ExtractSurface(
        simple.MergeBlocks(
            simple.OpenDataFile(os.path.join(dataDir,
                                             'Data/disk_out_ref.ex2'))))
    diskout.UpdatePipeline()
    unstructuredGrid = diskout.GetClientSideObject().GetOutputDataObject(0)
    table = vtkTable()
    _nbFields = unstructuredGrid.GetPointData().GetNumberOfArrays()
    for i in range(_nbFields):
        table.AddColumn(unstructuredGrid.GetPointData().GetArray(i))
    writeDataSet('table', table, outputDir)
예제 #2
0
def convert(inputFile, outputDir, merge=False, extract=False, newName=None):
    print(inputFile, outputDir)
    reader = simple.OpenDataFile(inputFile)
    activeSource = reader

    if merge:
        activeSource = simple.MergeBlocks(activeSource)

    if extract:
        activeSource = simple.ExtractSurface(activeSource)

    activeSource.UpdatePipeline()
    dataObject = activeSource.GetClientSideObject().GetOutputDataObject(0)

    if 'TimestepValues' in reader.ListProperties():
        if len(reader.TimestepValues) == 0:
            writeDataSet(inputFile, dataObject, outputDir, newName)
        else:
            writeTimeDataSource(inputFile, reader, activeSource, outputDir,
                                newName)
    else:
        writeDataSet(inputFile, dataObject, outputDir, newName)
예제 #3
0
    def writeData(self, time=0):
        if not self.dataHandler.can_write:
            return

        currentScene = []
        for data in self.config['scene']:
            currentData = {'name': data['name'], 'fields': {}}
            currentScene.append(currentData)
            if self.surfaceExtract:
                self.merge.Input = data['source']
            else:
                self.merge = simple.MergeBlocks(Input=data['source'],
                                                MergePoints=0)
                self.surfaceExtract = simple.ExtractSurface(Input=self.merge)

            # Extract surface
            self.surfaceExtract.UpdatePipeline(time)
            ds = self.surfaceExtract.SMProxy.GetClientSideObject(
            ).GetOutputDataObject(0)
            originalDS = data['source'].SMProxy.GetClientSideObject(
            ).GetOutputDataObject(0)

            originalPoints = ds.GetPoints()

            # Points
            points = vtkFloatArray()
            nbPoints = originalPoints.GetNumberOfPoints()
            points.SetNumberOfComponents(3)
            points.SetNumberOfTuples(nbPoints)
            for idx in range(nbPoints):
                coord = originalPoints.GetPoint(idx)
                points.SetTuple3(idx, coord[0], coord[1], coord[2])

            pBuffer = buffer(points)
            pMd5 = hashlib.md5(pBuffer).hexdigest()
            pPath = os.path.join(self.dataHandler.getBasePath(), 'points',
                                 "%s.Float32Array" % pMd5)
            currentData['points'] = 'points/%s.Float32Array' % pMd5
            with open(pPath, 'wb') as f:
                f.write(pBuffer)

            # Polys
            poly = ds.GetPolys()
            nbCells = poly.GetNumberOfCells()
            cellLocation = 0
            idList = vtkIdList()
            topo = vtkTypeUInt32Array()
            topo.Allocate(poly.GetData().GetNumberOfTuples())

            for cellIdx in range(nbCells):
                poly.GetCell(cellLocation, idList)
                cellSize = idList.GetNumberOfIds()
                cellLocation += cellSize + 1
                if cellSize == 3:
                    topo.InsertNextValue(idList.GetId(0))
                    topo.InsertNextValue(idList.GetId(1))
                    topo.InsertNextValue(idList.GetId(2))
                elif cellSize == 4:
                    topo.InsertNextValue(idList.GetId(0))
                    topo.InsertNextValue(idList.GetId(1))
                    topo.InsertNextValue(idList.GetId(3))
                    topo.InsertNextValue(idList.GetId(1))
                    topo.InsertNextValue(idList.GetId(2))
                    topo.InsertNextValue(idList.GetId(3))
                else:
                    print "Cell size of", cellSize, "not supported"

            iBuffer = buffer(topo)
            iMd5 = hashlib.md5(iBuffer).hexdigest()
            iPath = os.path.join(self.dataHandler.getBasePath(), 'index',
                                 "%s.Uint32Array" % iMd5)
            currentData['index'] = 'index/%s.Uint32Array' % iMd5
            with open(iPath, 'wb') as f:
                f.write(iBuffer)

            # Grow object side
            self.objSize[data['name']]['points'] = max(
                self.objSize[data['name']]['points'], nbPoints)
            self.objSize[data['name']]['index'] = max(
                self.objSize[data['name']]['index'], topo.GetNumberOfTuples())

            # Colors / FIXME
            for fieldName, fieldInfo in data['colors'].iteritems():
                array = ds.GetPointData().GetArray(fieldName)
                tupleSize = array.GetNumberOfComponents()
                arraySize = array.GetNumberOfTuples()
                outputField = vtkFloatArray()
                outputField.SetNumberOfTuples(arraySize)
                if tupleSize == 1:
                    for i in range(arraySize):
                        outputField.SetValue(i, array.GetValue(i))
                else:
                    # compute magnitude
                    tupleIdxs = range(tupleSize)
                    for i in range(arraySize):
                        magnitude = 0
                        for j in tupleIdxs:
                            magnitude += math.pow(
                                array.GetValue(i * tupleSize + j), 2)

                        outputField.SetValue(i, math.sqrt(magnitude))

                fBuffer = buffer(outputField)
                fMd5 = hashlib.md5(fBuffer).hexdigest()
                fPath = os.path.join(self.dataHandler.getBasePath(), 'fields',
                                     "%s_%s.Float32Array" % (fieldName, fMd5))
                with open(fPath, 'wb') as f:
                    f.write(fBuffer)

                currentData['fields'][
                    fieldName] = 'fields/%s_%s.Float32Array' % (fieldName,
                                                                fMd5)

        # Write scene
        with open(self.dataHandler.getDataAbsoluteFilePath('scene'), 'w') as f:
            f.write(json.dumps(currentScene, indent=4))