def mouseMoveEvent(self, event):
     if self._lastMousePos is not None:
         pos = [ event.x(), event.y() ]
         delta = [ pos[0] - self._lastMousePos[0], pos[1] - self._lastMousePos[1] ]
         result, eye = self._sceneviewer.getEyePosition()
         result, lookat = self._sceneviewer.getLookatPosition()
         result, up = self._sceneviewer.getUpVector()
         lookatToEye = vectorops.sub(eye, lookat)
         eyeDistance = vectorops.magnitude(lookatToEye)
         front = vectorops.div(lookatToEye, eyeDistance)
         right = vectorops.cross(up, front)
         if self._active_button == QtCore.Qt.LeftButton:
             mag = vectorops.magnitude(delta)
             prop = vectorops.div(delta, mag)
             axis = vectorops.add(vectorops.mult(up, prop[0]), vectorops.mult(right, prop[1]))
             angle = mag*0.002
             self._model.rotateModel(axis, angle)
         elif self._active_button == QtCore.Qt.MiddleButton:
             result, l, r, b, t, near, far = self._sceneviewer.getViewingVolume()
             viewportWidth = self.width()
             viewportHeight = self.height()
             if viewportWidth > viewportHeight:
                 eyeScale = (t - b) / viewportHeight
             else:
                 eyeScale = (r - l) / viewportWidth
             offset = vectorops.add(vectorops.mult(right, eyeScale*delta[0]), vectorops.mult(up, -eyeScale*delta[1]))
             self._model.offsetModel(offset)
         elif self._active_button == QtCore.Qt.RightButton:
             factor = 1.0 + delta[1]*0.0005
             if factor < 0.9:
                 factor = 0.9
             self._model.scaleModel(factor)
         self._lastMousePos = pos
     else:
         super(AlignmentSceneviewerWidget, self).mouseMoveEvent(event)
Exemple #2
0
 def mouseMoveEvent(self, event):
     if self._lastMousePos is not None:
         pos = [ event.x(), event.y() ]
         delta = [ pos[0] - self._lastMousePos[0], pos[1] - self._lastMousePos[1] ]
         result, eye = self._sceneviewer.getEyePosition()
         result, lookat = self._sceneviewer.getLookatPosition()
         result, up = self._sceneviewer.getUpVector()
         lookatToEye = vectorops.sub(eye, lookat)
         eyeDistance = vectorops.magnitude(lookatToEye)
         front = vectorops.div(lookatToEye, eyeDistance)
         right = vectorops.cross(up, front)
         if self._active_button == QtCore.Qt.LeftButton:
             mag = vectorops.magnitude(delta)
             prop = vectorops.div(delta, mag)
             axis = vectorops.add(vectorops.mult(up, prop[0]), vectorops.mult(right, prop[1]))
             angle = mag*0.002
             self._model.rotateModel(axis, angle)
         elif self._active_button == QtCore.Qt.MiddleButton:
             result, l, r, b, t, near, far = self._sceneviewer.getViewingVolume()
             viewportWidth = self.width()
             viewportHeight = self.height()
             if viewportWidth > viewportHeight:
                 eyeScale = (t - b) / viewportHeight
             else:
                 eyeScale = (r - l) / viewportWidth
             offset = vectorops.add(vectorops.mult(right, eyeScale*delta[0]), vectorops.mult(up, -eyeScale*delta[1]))
             self._model.translateModel(offset)
         elif self._active_button == QtCore.Qt.RightButton:
             factor = 1.0 + delta[1]*0.0005
             if factor < 0.9:
                 factor = 0.9
             self._model.scaleModel(factor)
         self._lastMousePos = pos
     else:
         super(AlignmentSceneviewerWidget, self).mouseMoveEvent(event)
def transformCoordinates(field, rotationScale, offset, time = 0.0):
    '''
    Transform finite element field coordinates by matrix and offset, handling nodal derivatives and versions.
    Limited to nodal parameters, rectangular cartesian coordinates
    :param field: the coordinate field to transform
    :param rotationScale: square transformation matrix 2-D array with as many rows and columns as field components.
    :param offset: coordinates offset
    :return: True on success, otherwise false
    '''
    ncomp = field.getNumberOfComponents()
    if ((ncomp != 2) and (ncomp != 3)):
        print('zinc.transformCoordinates: field has invalid number of components')
        return False
    if (len(rotationScale) != ncomp) or (len(offset) != ncomp):
        print('zinc.transformCoordinates: invalid matrix number of columns or offset size')
        return False
    for matRow in rotationScale:
        if len(matRow) != ncomp:
            print('zinc.transformCoordinates: invalid matrix number of columns')
            return False
    if (field.getCoordinateSystemType() != Field.COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN):
        print('zinc.transformCoordinates: field is not rectangular cartesian')
        return False
    feField = field.castFiniteElement()
    if not feField.isValid():
        print('zinc.transformCoordinates: field is not finite element field type')
        return False
    success = True
    fm = field.getFieldmodule()
    fm.beginChange()
    cache = fm.createFieldcache()
    cache.setTime(time)
    nodes = fm.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
    nodetemplate = nodes.createNodetemplate()
    nodeIter = nodes.createNodeiterator()
    node = nodeIter.next()
    while node.isValid():
        nodetemplate.defineFieldFromNode(feField, node)
        cache.setNode(node)
        for derivative in [Node.VALUE_LABEL_VALUE, Node.VALUE_LABEL_D_DS1, Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D2_DS1DS2,
                           Node.VALUE_LABEL_D_DS3, Node.VALUE_LABEL_D2_DS1DS3, Node.VALUE_LABEL_D2_DS2DS3, Node.VALUE_LABEL_D3_DS1DS2DS3]:
            versions = nodetemplate.getValueNumberOfVersions(feField, -1, derivative)
            for v in range(1, versions + 1):
                result, values = feField.getNodeParameters(cache, -1, derivative, v, ncomp)
                if result != ZINC_OK:
                    success = False
                else:
                    newValues = vectorops.matrixvectormult(rotationScale, values)
                    if derivative == Node.VALUE_LABEL_VALUE:
                        newValues = vectorops.add(newValues, offset)
                    result = feField.setNodeParameters(cache, -1, derivative, v, newValues)
                    if result != ZINC_OK:
                        success = False
        node = nodeIter.next()
    fm.endChange()
    if not success:
        print('zinc.transformCoordinates: failed to get/set some values')
    return success
    def load(self):
        if self._modelReferenceCoordinateField is None:
            # read and rename coordinates to reference_coordinates, for calculating strains
            result = self._region.readFile(self._zincModelFile)
            if result != ZINC_OK:
                raise ValueError('Failed to read reference model')
            self._modelReferenceCoordinateField = self._getModelCoordinateField(
            )
            name = self._modelReferenceCoordinateField.getName()
            number = 0
            numberString = ''
            while True:
                result = self._modelReferenceCoordinateField.setName(
                    'reference_' + name + numberString)
                if result == ZINC_OK:
                    #print('Renamed field', name, ' to', 'reference_' + name + numberString)
                    break
                number = number + 1
                numberString = str(number)
            # read data cloud
            if self._zincPointCloudFile:
                sir = self._region.createStreaminformationRegion()
                pointCloudResource = sir.createStreamresourceFile(
                    self._zincPointCloudFile)
                sir.setResourceDomainTypes(pointCloudResource,
                                           Field.DOMAIN_TYPE_DATAPOINTS)
                result = self._region.read(sir)
                if result != ZINC_OK:
                    raise ValueError('Failed to read point cloud')
                self._dataCoordinateField = self._getDataCoordinateField()
            elif self._pointCloudData:
                self._dataCoordinateField = createFiniteElementField(
                    self._region, field_name='data_coordinates')
                self._createDataPoints(self._pointCloudData)

        result = self._region.readFile(self._zincModelFile)
        if result != ZINC_OK:
            raise ValueError('Failed to read model')
        self._mesh = self._getMesh()
        self._modelCoordinateField = self._getModelCoordinateField()
        minimums, maximums = self._getModelRange()
        self._modelCentre = vectorops.mult(vectorops.add(minimums, maximums),
                                           0.5)
        self._projectSurfaceGroup, self._projectSurfaceElementGroup = self._getProjectSurfaceGroup(
        )
        fm = self._region.getFieldmodule()
        datapoints = fm.findNodesetByFieldDomainType(
            Field.DOMAIN_TYPE_DATAPOINTS)
        self._activeDataPointGroupField = fm.createFieldNodeGroup(datapoints)
        tmpTrue = fm.createFieldConstant([1])
        activeDatapointsGroup = self._activeDataPointGroupField.getNodesetGroup(
        )
        activeDatapointsGroup.addNodesConditional(tmpTrue)
        self._applyAlignSettings()
        self._showModelGraphics()
    def _load(self):
        if self._modelReferenceCoordinateField is None:
            # read and rename coordinates to reference_coordinates, for calculating strains
            result = self._region.readFile(self._zincModelFile)
            if result != ZINC_OK:
                raise ValueError('Failed to read reference model')
            self._modelReferenceCoordinateField = self._getModelCoordinateField()
            name = self._modelReferenceCoordinateField.getName()
            number = 0
            numberString = ''
            while True:
                result = self._modelReferenceCoordinateField.setName('reference_' + name + numberString)
                if result == ZINC_OK:
                    #print('Renamed field', name, ' to', 'reference_' + name + numberString)
                    break
                number = number + 1
                numberString = str(number)
            # read data cloud
            if self._zincPointCloudFile:
                sir = self._region.createStreaminformationRegion()
                pointCloudResource = sir.createStreamresourceFile(self._zincPointCloudFile)
                sir.setResourceDomainTypes(pointCloudResource, Field.DOMAIN_TYPE_DATAPOINTS)
                result = self._region.read(sir)
                if result != ZINC_OK:
                    raise ValueError('Failed to read point cloud')
                self._dataCoordinateField = self._getDataCoordinateField()
            elif self._pointCloudData:
                self._dataCoordinateField = createFiniteElementField(self._region, field_name='data_coordinates')
                self._createDataPoints(self._pointCloudData)

        result = self._region.readFile(self._zincModelFile)
        if result != ZINC_OK:
            raise ValueError('Failed to read model')
        self._mesh = self._getMesh()
        self._modelCoordinateField = self._getModelCoordinateField()
        minimums, maximums = self._getModelRange()
        self._modelCentre = vectorops.mult(vectorops.add(minimums, maximums), 0.5)
        self._projectSurfaceGroup, self._projectSurfaceElementGroup = self._getProjectSurfaceGroup()
        fm = self._region.getFieldmodule()
        datapoints = fm.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_DATAPOINTS)
        self._activeDataPointGroupField = fm.createFieldNodeGroup(datapoints)
        tmpTrue = fm.createFieldConstant([1])
        activeDatapointsGroup = self._activeDataPointGroupField.getNodesetGroup()
        activeDatapointsGroup.addNodesConditional(tmpTrue)
        self._applyAlignSettings()
        self._showModelGraphics()
 def autoCentreModelOnData(self):
     minimums, maximums = self._getDataRange()
     dataCentre = vectorops.mult(vectorops.add(minimums, maximums), 0.5)
     self.setAlignOffset(vectorops.sub(dataCentre, self._modelCentre))
 def offsetModel(self, relativeOffset):
     self._alignSettings['offset'] = vectorops.add(
         self._alignSettings['offset'], relativeOffset)
     self._applyAlignSettings()
 def autoCentreModelOnData(self):
     minimums, maximums = self._getDataRange()
     dataCentre = vectorops.mult(vectorops.add(minimums, maximums), 0.5)
     self.setAlignOffset(vectorops.sub(dataCentre, self._modelCentre))
 def offsetModel(self, relativeOffset):
     self._alignSettings['offset'] = vectorops.add(self._alignSettings['offset'], relativeOffset)
     self._applyAlignSettings()
Exemple #10
0
def transformCoordinates(field, rotationScale, offset, time=0.0):
    '''
    Transform finite element field coordinates by matrix and offset, handling nodal derivatives and versions.
    Limited to nodal parameters, rectangular cartesian coordinates
    :param field: the coordinate field to transform
    :param rotationScale: square transformation matrix 2-D array with as many rows and columns as field components.
    :param offset: coordinates offset
    :return: True on success, otherwise false
    '''
    ncomp = field.getNumberOfComponents()
    if ((ncomp != 2) and (ncomp != 3)):
        print(
            'zinc.transformCoordinates: field has invalid number of components'
        )
        return False
    if (len(rotationScale) != ncomp) or (len(offset) != ncomp):
        print(
            'zinc.transformCoordinates: invalid matrix number of columns or offset size'
        )
        return False
    for matRow in rotationScale:
        if len(matRow) != ncomp:
            print(
                'zinc.transformCoordinates: invalid matrix number of columns')
            return False
    if (field.getCoordinateSystemType() !=
            Field.COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN):
        print('zinc.transformCoordinates: field is not rectangular cartesian')
        return False
    feField = field.castFiniteElement()
    if not feField.isValid():
        print(
            'zinc.transformCoordinates: field is not finite element field type'
        )
        return False
    success = True
    fm = field.getFieldmodule()
    fm.beginChange()
    cache = fm.createFieldcache()
    cache.setTime(time)
    nodes = fm.findNodesetByFieldDomainType(Field.DOMAIN_TYPE_NODES)
    nodetemplate = nodes.createNodetemplate()
    nodeIter = nodes.createNodeiterator()
    node = nodeIter.next()
    while node.isValid():
        nodetemplate.defineFieldFromNode(feField, node)
        cache.setNode(node)
        for derivative in [
                Node.VALUE_LABEL_VALUE, Node.VALUE_LABEL_D_DS1,
                Node.VALUE_LABEL_D_DS2, Node.VALUE_LABEL_D2_DS1DS2,
                Node.VALUE_LABEL_D_DS3, Node.VALUE_LABEL_D2_DS1DS3,
                Node.VALUE_LABEL_D2_DS2DS3, Node.VALUE_LABEL_D3_DS1DS2DS3
        ]:
            versions = nodetemplate.getValueNumberOfVersions(
                feField, -1, derivative)
            for v in range(1, versions + 1):
                result, values = feField.getNodeParameters(
                    cache, -1, derivative, v, ncomp)
                if result != ZINC_OK:
                    success = False
                else:
                    newValues = vectorops.matrixvectormult(
                        rotationScale, values)
                    if derivative == Node.VALUE_LABEL_VALUE:
                        newValues = vectorops.add(newValues, offset)
                    result = feField.setNodeParameters(cache, -1, derivative,
                                                       v, newValues)
                    if result != ZINC_OK:
                        success = False
        node = nodeIter.next()
    fm.endChange()
    if not success:
        print('zinc.transformCoordinates: failed to get/set some values')
    return success