def __init__(self, parent=None):
        '''
        Initialise the ZincViewGraphics first calling the QMainWindow __init__ function.
        '''
        QtWidgets.QMainWindow.__init__(self, parent)

        # create instance of Zinc Context from which all other objects are obtained
        self._context = ZincContext("ZincViewGraphics")

        # set up standard materials for colouring graphics
        self._context.getMaterialmodule().defineStandardMaterials()
        # set up standard glyphs, graphics to show at points e.g. spheres, arrows
        self._context.getGlyphmodule().defineStandardGlyphs()

        # Load the user interface controls
        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._makeConnections()
        # Must pass the Context to the Zinc SceneviewerWidget
        self._ui.sceneviewerWidget.setContext(self._context)
        # must set other sceneviewer defaults once graphics are initialised
        self._ui.sceneviewerWidget.graphicsInitialized.connect(
            self._graphicsInitialized)

        # read the model and create some graphics to see it:
        self.setupModel()
    def __init__(self, parent=None):
        '''
        Initialise the ZincViewGraphics first calling the QMainWindow __init__ function.
        '''
        QtGui.QMainWindow.__init__(self, parent)

        # create instance of Zinc Context from which all other objects are obtained
        self._context = ZincContext("ZincViewGraphics");

        # set up standard materials for colouring graphics
        self._context.getMaterialmodule().defineStandardMaterials()
        # set up standard glyphs, graphics to show at points e.g. spheres, arrows
        self._context.getGlyphmodule().defineStandardGlyphs()

        # Load the user interface controls
        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._makeConnections()
        # Must pass the Context to the Zinc SceneviewerWidget
        self._ui.sceneviewerWidget.setContext(self._context)
        # must set other sceneviewer defaults once graphics are initialised
        self._ui.sceneviewerWidget.graphicsInitialized.connect(self._graphicsInitialized)

        # read the model and create some graphics to see it:
        self.setupModel()
    def __init__(self, filename, parent=None):
        super(ZincViewImage, self).__init__(parent)

        self._context = ZincContext("ZincViewGraphics")
        self._material_module = self._context.getMaterialmodule()
        self._region = self._context.getDefaultRegion()

        self._fieldmodule = self._region.getFieldmodule()
        self._scaled_coordinate_field = None
        self._image_based_material = None
        self._filename = filename

        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._make_connections()
        self._ui.sceneviewerWidget.setContext(self._context)
        self._ui.sceneviewerWidget.graphicsInitialized.connect(self._graphics_initialized)

        self._create_model()
        self._load_image()
class ZincViewGraphics(QtGui.QMainWindow):
    '''
    Create a subclass of QWidget for our application.  We could also have derived this 
    application from QMainWindow to give us a menu bar among other things, but a
    QWidget is sufficient for our purposes.
    '''

    # ZincViewGraphics__init__ start
    def __init__(self, parent=None):
        '''
        Initialise the ZincViewGraphics first calling the QMainWindow __init__ function.
        '''
        QtGui.QMainWindow.__init__(self, parent)

        # create instance of Zinc Context from which all other objects are obtained
        self._context = ZincContext("ZincViewGraphics");

        # set up standard materials for colouring graphics
        self._context.getMaterialmodule().defineStandardMaterials()
        # set up standard glyphs, graphics to show at points e.g. spheres, arrows
        self._context.getGlyphmodule().defineStandardGlyphs()

        # Load the user interface controls
        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._makeConnections()
        # Must pass the Context to the Zinc SceneviewerWidget
        self._ui.sceneviewerWidget.setContext(self._context)
        # must set other sceneviewer defaults once graphics are initialised
        self._ui.sceneviewerWidget.graphicsInitialized.connect(self._graphicsInitialized)

        # read the model and create some graphics to see it:
        self.setupModel()
        # ZincViewGraphics__init__ end

    def _graphicsInitialized(self):
        '''
        Callback for when SceneviewerWidget is initialised
        Needed since Sceneviewer is not fully constructed in __init__
        '''
        # use a white background instead of the default black
        sceneviewer = self._ui.sceneviewerWidget.getSceneviewer()
        sceneviewer.setBackgroundColourRGB([1.0, 1.0, 1.0])

    # _makeConnections start
    def _makeConnections(self):
        self._ui.customButton.clicked.connect(self.customButtonClicked)
        self._ui.viewAllButton.clicked.connect(self.viewAllButtonClicked)
        # _makeConnections end
        
    # setupModel start
    def setupModel(self):
        region = self._context.getDefaultRegion()

        # read the model file
        region.readFile('trilinear_cube.exfile')
        
        # define fields calculated from the existing fields to use for visualisation
        # starting with a scalar field giving the magnitude
        fieldmodule = region.getFieldmodule()
        fieldmodule.beginChange()
        coordinates = fieldmodule.findFieldByName('coordinates')
        mag = fieldmodule.createFieldMagnitude(coordinates);
        mag.setName('mag') # name it so it can be found by name later
        mag.setManaged(True) # manage its lifetime so it is kept even if not being used
        fieldmodule.endChange()
        
        scene = region.getScene()
        scene.beginChange()
        materialmodule = self._context.getMaterialmodule()

        # view the 1-D line elements on the edges of the cube
        lines = scene.createGraphicsLines()
        lines.setCoordinateField(coordinates)
        # default material is white, so choose black to view on white background
        black = materialmodule.findMaterialByName('black')
        lines.setMaterial(black)

        # view coordinate axes at the origin
        axes = scene.createGraphicsPoints()
        axes.setFieldDomainType(Field.DOMAIN_TYPE_POINT)
        pointAttr = axes.getGraphicspointattributes()
        pointAttr.setGlyphShapeType(Glyph.SHAPE_TYPE_AXES_XYZ)
        pointAttr.setBaseSize([1.2])
        blue = materialmodule.findMaterialByName('blue')
        axes.setMaterial(blue)

        # view the nodes at the corners of the cube as green spheres
        nodepoints = scene.createGraphicsPoints()
        nodepoints.setFieldDomainType(Field.DOMAIN_TYPE_NODES)
        nodepoints.setCoordinateField(coordinates)
        pointAttr = nodepoints.getGraphicspointattributes()
        pointAttr.setGlyphShapeType(Glyph.SHAPE_TYPE_SPHERE)
        pointAttr.setBaseSize([0.05])
        green = materialmodule.findMaterialByName('green')
        nodepoints.setMaterial(green)

        # view 'isosurfaces' where the magnitude of coordinates equals 1.0
        isosurfaces = scene.createGraphicsContours()
        isosurfaces.setCoordinateField(coordinates)
        isosurfaces.setIsoscalarField(mag)
        isosurfaces.setListIsovalues(1.0);
        gold = materialmodule.findMaterialByName('gold')
        isosurfaces.setMaterial(gold)
        # use finer tessellation to see the curvature of the isosurfaces
        tessellationmodule = self._context.getTessellationmodule()
        fineTessellation = tessellationmodule.createTessellation()
        fineTessellation.setName('fine') # name it so it can be found by name later
        fineTessellation.setManaged(True) # manage its lifetime so it is kept even if not being used
        fineTessellation.setMinimumDivisions(8) # divide element edges into 8 line segments
        isosurfaces.setTessellation(fineTessellation)

        scene.endChange()
        # setupModel end

    # customButtonClicked start
    def customButtonClicked(self):
        tessellationmodule = self._context.getTessellationmodule()
        fineTessellation = tessellationmodule.findTessellationByName('fine')
        result, divisions = fineTessellation.getMinimumDivisions(1)
        if result == ZINC_OK:
            divisions = divisions*2
            if divisions > 100:
                divisions = 1
            fineTessellation.setMinimumDivisions(divisions)
        # customButtonClicked end

    def viewAllButtonClicked(self):
        self._ui.sceneviewerWidget.getSceneviewer().viewAll()
class ZincViewImage(QtGui.QMainWindow):

    def __init__(self, filename, parent=None):
        super(ZincViewImage, self).__init__(parent)

        self._context = ZincContext("ZincViewGraphics")
        self._material_module = self._context.getMaterialmodule()
        self._region = self._context.getDefaultRegion()

        self._fieldmodule = self._region.getFieldmodule()
        self._scaled_coordinate_field = None
        self._image_based_material = None
        self._filename = filename

        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._make_connections()
        self._ui.sceneviewerWidget.setContext(self._context)
        self._ui.sceneviewerWidget.graphicsInitialized.connect(self._graphics_initialized)

        self._create_model()
        self._load_image()

    def _graphics_initialized(self):
        sceneviewer = self._ui.sceneviewerWidget.getSceneviewer()
        sceneviewer.setBackgroundColourRGB([0.0, 0.0, 0.0])

    def _make_connections(self):
        self._ui.viewAllButton.clicked.connect(self._view_all_clicked)

    def _view_all_clicked(self):
        self._ui.sceneviewerWidget.getSceneviewer().viewAll()

    def _create_square_2d_finite_element(self, coordinate_field, node_coordinate_set):
        nodeset = self._fieldmodule.findNodesetByName('nodes')
        node_template = nodeset.createNodetemplate()
        node_template.defineField(coordinate_field)
        field_cache = self._fieldmodule.createFieldcache()

        node_identifiers = []
        # Create eight nodes to define a cube finite element
        for node_coordinate in node_coordinate_set:
            node = nodeset.createNode(-1, node_template)
            node_identifiers.append(node.getIdentifier())
            # Set the node coordinates, first set the field cache to use the current node
            field_cache.setNode(node)
            # Pass in floats as an array
            result = coordinate_field.assignReal(field_cache, node_coordinate)
            if result != ZINC_OK:
                raise ValueError('Could not create nodes for box.')

        # Use a 3D mesh to to create the 2D finite element.
        mesh = self._fieldmodule.findMeshByDimension(2)
        element_template = mesh.createElementtemplate()
        element_template.setElementShapeType(Element.SHAPE_TYPE_SQUARE)
        element_node_count = 4
        element_template.setNumberOfNodes(element_node_count)
        # Specify the dimension and the interpolation function for the element basis function
        linear_basis = self._fieldmodule.createElementbasis(2, Elementbasis.FUNCTION_TYPE_LINEAR_LAGRANGE)
        # the indecies of the nodes in the node template we want to use.
        node_indexes = [1, 2, 3, 4]

        # Define a nodally interpolated element field or field component in the
        # element_template
        element_template.defineFieldSimpleNodal(coordinate_field, -1, linear_basis, node_indexes)

        for i, node_identifier in enumerate(node_identifiers):
            node = nodeset.findNodeByIdentifier(node_identifier)
            result = element_template.setNode(i + 1, node)
            if result != ZINC_OK:
                raise ValueError('Could not create elements for box.')

        mesh.defineElement(-1, element_template)
        self._fieldmodule.defineAllFaces()

    def _create_model(self):
        coordinate_field = self._create_finite_element_field()
        scale_field = self._fieldmodule.createFieldConstant([2, 3, 1])
        scale_field.setName('scale')
        offset_field = self._fieldmodule.createFieldConstant([+0.5, +0.5, 0.0])
        self._scaled_coordinate_field = self._fieldmodule.createFieldMultiply(scale_field, coordinate_field)
        self._scaled_coordinate_field = self._fieldmodule.createFieldAdd(self._scaled_coordinate_field, offset_field)
        self._scaled_coordinate_field.setManaged(True)
        self._scaled_coordinate_field.setName('scaled_coordinates')
        self._create_square_2d_finite_element(coordinate_field, [[0.0, 0.0, 0.0],
                                                                 [1.0, 0.0, 0.0],
                                                                 [0.0, 1.0, 0.0],
                                                                 [1.0, 1.0, 0.0]])

    def _create_finite_element_field(self, dimension=3, field_name='coordinates', managed=True, type_coordinate=True):
        fieldmodule = self._region.getFieldmodule()
        fieldmodule.beginChange()

        # Create a finite element field with 3 components to represent 3 dimensions
        finite_element_field = fieldmodule.createFieldFiniteElement(dimension)

        # Set the name of the field
        finite_element_field.setName(field_name)
        # Set the attribute is managed to 1 so the field module will manage the field for us

        finite_element_field.setManaged(managed)
        finite_element_field.setTypeCoordinate(type_coordinate)
        fieldmodule.endChange()

        return finite_element_field

    def _create_image_field(self, image_filename, field_name='image'):
        image_field = self._fieldmodule.createFieldImage()
        image_field.setName(field_name)
        image_field.setFilterMode(image_field.FILTER_MODE_LINEAR)

        stream_information = image_field.createStreaminformationImage()
        stream_information.createStreamresourceFile(image_filename)
        result = image_field.read(stream_information)
        if result != ZINC_OK:
            raise ValueError('Could not read image.')

        return image_field

    def _create_material_using_image_field(self, image_field, colour_mapping_type=None, image_range=None):
        # create a graphics material from the graphics module, assign it a name
        # and set flag to true
        scene = self._region.getScene()
        material_module = scene.getMaterialmodule()
        spectrum_module = scene.getSpectrummodule()
        material = material_module.createMaterial()
        spectrum = spectrum_module.createSpectrum()
        component = spectrum.createSpectrumcomponent()
        if colour_mapping_type is None:
            colour_mapping_type = component.COLOUR_MAPPING_TYPE_RAINBOW
        component.setColourMappingType(colour_mapping_type)
        if image_range is not None:
            component.setRangeMinimum(image_range[0])
            component.setRangeMaximum(image_range[1])
        material.setTextureField(1, image_field)
        return material

    def _load_image(self):
        image_dimensions = [0, 0]
        image_based_material = None
        width, height = imagesize.get(self._filename)
        if width != -1 or height != -1:
            cache = self._fieldmodule.createFieldcache()
            # self._scaled_coordinate_field = self._fieldmodule.findFieldByName('scale')
            self._scaled_coordinate_field.assignReal(cache, [width, height, 1.0])
            image_dimensions = [width, height]
            image_field = self._create_image_field(self._filename)
            self._image_based_material = self._create_material_using_image_field(image_field)
            self._image_based_material.setName('images')
            self._image_based_material.setManaged(True)

        return image_dimensions, image_based_material

    def create_graphics(self):
        scene = self._region.getScene()
        # coordinate_field = self._scaled_coordinate_field
        scene.beginChange()
        scene.removeAllGraphics()
        xi = self._fieldmodule.findFieldByName('xi')
        lines = scene.createGraphicsLines()
        lines.setExterior(True)
        lines.setName('plane-lines')
        lines.setCoordinateField(self._scaled_coordinate_field)
        surfaces = scene.createGraphicsSurfaces()
        surfaces.setName('plane-surfaces')
        surfaces.setCoordinateField(self._scaled_coordinate_field)
        temp1 = self._fieldmodule.createFieldComponent(xi, [1, 2])
        texture_field = self._fieldmodule.createFieldConcatenate([temp1])
        result = surfaces.setTextureCoordinateField(texture_field)
        if result != ZINC_OK:
            raise ValueError('Texture coordinate was not successful.')
        surfaces.setMaterial(self._image_based_material)
        scene.endChange()
class ZincViewGraphics(QtWidgets.QMainWindow):
    '''
    Create a subclass of QWidget for our application.  We could also have derived this 
    application from QMainWindow to give us a menu bar among other things, but a
    QWidget is sufficient for our purposes.
    '''

    # ZincViewGraphics__init__ start
    def __init__(self, parent=None):
        '''
        Initialise the ZincViewGraphics first calling the QMainWindow __init__ function.
        '''
        QtWidgets.QMainWindow.__init__(self, parent)

        # create instance of Zinc Context from which all other objects are obtained
        self._context = ZincContext("ZincViewGraphics")

        # set up standard materials for colouring graphics
        self._context.getMaterialmodule().defineStandardMaterials()
        # set up standard glyphs, graphics to show at points e.g. spheres, arrows
        self._context.getGlyphmodule().defineStandardGlyphs()

        # Load the user interface controls
        self._ui = Ui_ZincViewGraphics()
        self._ui.setupUi(self)
        self._makeConnections()
        # Must pass the Context to the Zinc SceneviewerWidget
        self._ui.sceneviewerWidget.setContext(self._context)
        # must set other sceneviewer defaults once graphics are initialised
        self._ui.sceneviewerWidget.graphicsInitialized.connect(
            self._graphicsInitialized)

        # read the model and create some graphics to see it:
        self.setupModel()
        # ZincViewGraphics__init__ end

    def _graphicsInitialized(self):
        '''
        Callback for when SceneviewerWidget is initialised
        Needed since Sceneviewer is not fully constructed in __init__
        '''
        # use a white background instead of the default black
        sceneviewer = self._ui.sceneviewerWidget.getSceneviewer()
        sceneviewer.setBackgroundColourRGB([1.0, 1.0, 1.0])

    # _makeConnections start
    def _makeConnections(self):
        self._ui.customButton.clicked.connect(self.customButtonClicked)
        self._ui.viewAllButton.clicked.connect(self.viewAllButtonClicked)
        # _makeConnections end

    # setupModel start
    def setupModel(self):
        region = self._context.getDefaultRegion()

        # read the model file
        region.readFile('trilinear_cube.exfile')

        # define fields calculated from the existing fields to use for visualisation
        # starting with a scalar field giving the magnitude
        fieldmodule = region.getFieldmodule()
        fieldmodule.beginChange()
        coordinates = fieldmodule.findFieldByName('coordinates')
        mag = fieldmodule.createFieldMagnitude(coordinates)
        mag.setName('mag')  # name it so it can be found by name later
        mag.setManaged(
            True)  # manage its lifetime so it is kept even if not being used
        fieldmodule.endChange()

        scene = region.getScene()
        scene.beginChange()
        materialmodule = self._context.getMaterialmodule()

        # view the 1-D line elements on the edges of the cube
        lines = scene.createGraphicsLines()
        lines.setCoordinateField(coordinates)
        # default material is white, so choose black to view on white background
        black = materialmodule.findMaterialByName('black')
        lines.setMaterial(black)

        # view coordinate axes at the origin
        axes = scene.createGraphicsPoints()
        axes.setFieldDomainType(Field.DOMAIN_TYPE_POINT)
        pointAttr = axes.getGraphicspointattributes()
        pointAttr.setGlyphShapeType(Glyph.SHAPE_TYPE_AXES_XYZ)
        pointAttr.setBaseSize([1.2])
        blue = materialmodule.findMaterialByName('blue')
        axes.setMaterial(blue)

        # view the nodes at the corners of the cube as green spheres
        nodepoints = scene.createGraphicsPoints()
        nodepoints.setFieldDomainType(Field.DOMAIN_TYPE_NODES)
        nodepoints.setCoordinateField(coordinates)
        pointAttr = nodepoints.getGraphicspointattributes()
        pointAttr.setGlyphShapeType(Glyph.SHAPE_TYPE_SPHERE)
        pointAttr.setBaseSize([0.05])
        green = materialmodule.findMaterialByName('green')
        nodepoints.setMaterial(green)

        # view 'isosurfaces' where the magnitude of coordinates equals 1.0
        isosurfaces = scene.createGraphicsContours()
        isosurfaces.setCoordinateField(coordinates)
        isosurfaces.setIsoscalarField(mag)
        isosurfaces.setListIsovalues(1.0)
        gold = materialmodule.findMaterialByName('gold')
        isosurfaces.setMaterial(gold)
        # use finer tessellation to see the curvature of the isosurfaces
        tessellationmodule = self._context.getTessellationmodule()
        fineTessellation = tessellationmodule.createTessellation()
        fineTessellation.setName(
            'fine')  # name it so it can be found by name later
        fineTessellation.setManaged(
            True)  # manage its lifetime so it is kept even if not being used
        fineTessellation.setMinimumDivisions(
            8)  # divide element edges into 8 line segments
        isosurfaces.setTessellation(fineTessellation)

        scene.endChange()
        # setupModel end

    # customButtonClicked start
    def customButtonClicked(self):
        tessellationmodule = self._context.getTessellationmodule()
        fineTessellation = tessellationmodule.findTessellationByName('fine')
        result, divisions = fineTessellation.getMinimumDivisions(1)
        if result == ZINC_OK:
            divisions = divisions * 2
            if divisions > 100:
                divisions = 1
            fineTessellation.setMinimumDivisions(divisions)
        # customButtonClicked end

    def viewAllButtonClicked(self):
        self._ui.sceneviewerWidget.getSceneviewer().viewAll()