Beispiel #1
0
 def mouseMoveEvent(self, event):
     if self._model.isStateAlign() and self._alignKeyPressed and (
             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)
 def mouse_move_event(self, event):
     if self._lastMousePos is not None:
         pixel_scale = self._scene_viewer.get_pixel_scale()
         pos = [event.x() * pixel_scale, event.y() * pixel_scale]
         delta = [pos[0] - self._lastMousePos[0], pos[1] - self._lastMousePos[1]]
         mag = vectorops.magnitude(delta)
         if mag <= 0.0:
             return
         result, eye = self._zinc_sceneviewer.getEyePosition()
         result, lookat = self._zinc_sceneviewer.getLookatPosition()
         result, up = self._zinc_sceneviewer.getUpVector()
         lookatToEye = vectorops.sub(eye, lookat)
         eyeDistance = vectorops.magnitude(lookatToEye)
         front = vectorops.div(lookatToEye, eyeDistance)
         right = vectorops.cross(up, front)
         viewportWidth = self._scene_viewer.width()
         viewportHeight = self._scene_viewer.height()
         if self._active_button == QtCore.Qt.LeftButton:
             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._zinc_sceneviewer.getViewingVolume()
             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
    def _doAutoAlign(self):
        """
        Perform auto alignment to groups and/or markers.
        """
        modelCoordinates = self._fitter.getModelCoordinatesField()
        componentsCount = modelCoordinates.getNumberOfComponents()
        pointMap = {
        }  # dict group/marker name -> (modelCoordinates, dataCoordinates)

        if self._alignGroups:
            fieldmodule = self._fitter._fieldmodule
            datapoints = fieldmodule.findNodesetByFieldDomainType(
                Field.DOMAIN_TYPE_DATAPOINTS)
            dataCoordinates = self._fitter.getDataCoordinatesField()
            groups = get_group_list(fieldmodule)
            with ChangeManager(fieldmodule):
                one = fieldmodule.createFieldConstant(1.0)
                for group in groups:
                    dataGroup = self._fitter.getGroupDataProjectionNodesetGroup(
                        group)
                    if not dataGroup:
                        continue
                    meshGroup = self._fitter.getGroupDataProjectionMeshGroup(
                        group)
                    if not meshGroup:
                        continue
                    groupName = group.getName()
                    meanDataCoordinates = evaluate_field_nodeset_mean(
                        dataCoordinates, dataGroup)
                    coordinates_integral = evaluate_field_mesh_integral(
                        modelCoordinates, modelCoordinates, meshGroup)
                    mass = evaluate_field_mesh_integral(
                        one, modelCoordinates, meshGroup)
                    meanModelCoordinates = div(coordinates_integral, mass)
                    pointMap[groupName] = (meanModelCoordinates,
                                           meanDataCoordinates)
                del one

        if self._alignMarkers:
            markerGroup = self._fitter.getMarkerGroup()
            assert markerGroup, "Align:  No marker group to align with"
            markerPrefix = markerGroup.getName()

            markerNodeGroup, markerLocation, markerCoordinates, markerName = self._fitter.getMarkerModelFields(
            )
            assert markerNodeGroup and markerCoordinates and markerName, "Align:  No marker group, coordinates or name fields"
            modelMarkers = getNodeNameCentres(markerNodeGroup,
                                              markerCoordinates, markerName)

            markerDataGroup, markerDataCoordinates, markerDataName = self._fitter.getMarkerDataFields(
            )
            assert markerDataGroup and markerDataCoordinates and markerDataName, "Align:  No marker data group, coordinates or name fields"
            dataMarkers = getNodeNameCentres(markerDataGroup,
                                             markerDataCoordinates,
                                             markerDataName)

            # match model and data markers, warn of unmatched markers
            writeDiagnostics = self.getDiagnosticLevel() > 0
            for modelName in modelMarkers:
                # name match allows case and whitespace differences
                matchName = modelName.strip().casefold()
                for dataName in dataMarkers:
                    if dataName.strip().casefold() == matchName:
                        pointMap[modelName] = (modelMarkers[modelName],
                                               dataMarkers[dataName])
                        if writeDiagnostics:
                            print("Align:  Model marker '" + modelName +
                                  "' found in data" +
                                  (" as '" + dataName +
                                   "'" if (dataName != modelName) else ""))
                            dataMarkers.pop(dataName)
                        break
                else:
                    if writeDiagnostics:
                        print("Align:  Model marker '" + modelName +
                              "' not found in data")
            if writeDiagnostics:
                for dataName in dataMarkers:
                    print("Align:  Data marker '" + dataName +
                          "' not found in model")

        self._optimiseAlignment(pointMap)
 def mouseMoveEvent(self, event):
     if self._editNode:
         mousePos = [event.x(), event.y()]
         nodeset = self._editNode.getNodeset()
         fieldmodule = nodeset.getFieldmodule()
         with ChangeManager(fieldmodule):
             meshEditsNodeset = self._model.getOrCreateMeshEditsNodesetGroup(
                 nodeset)
             meshEditsNodeset.addNode(self._editNode)
             editCoordinateField = coordinateField = self._editGraphics.getCoordinateField(
             )
             localScene = self._editGraphics.getScene(
             )  # need set local scene to get correct transformation
             if coordinateField.getCoordinateSystemType(
             ) != Field.COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN:
                 editCoordinateField = fieldmodule.createFieldCoordinateTransformation(
                     coordinateField)
                 editCoordinateField.setCoordinateSystemType(
                     Field.COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN)
             fieldcache = fieldmodule.createFieldcache()
             fieldcache.setNode(self._editNode)
             componentsCount = coordinateField.getNumberOfComponents()
             result, initialCoordinates = editCoordinateField.evaluateReal(
                 fieldcache, componentsCount)
             if result == RESULT_OK:
                 for c in range(componentsCount, 3):
                     initialCoordinates.append(0.0)
                 pointattr = self._editGraphics.getGraphicspointattributes()
                 editVectorField = vectorField = pointattr.getOrientationScaleField(
                 )
                 pointBaseSize = pointattr.getBaseSize(3)[1][0]
                 pointScaleFactor = pointattr.getScaleFactors(3)[1][0]
                 if editVectorField.isValid() and (vectorField.getNumberOfComponents() == componentsCount) \
                         and (pointBaseSize == 0.0) and (pointScaleFactor != 0.0):
                     if vectorField.getCoordinateSystemType(
                     ) != Field.COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN:
                         editVectorField = fieldmodule.createFieldCoordinateTransformation(
                             vectorField, coordinateField)
                         editVectorField.setCoordinateSystemType(
                             Field.
                             COORDINATE_SYSTEM_TYPE_RECTANGULAR_CARTESIAN)
                     result, initialVector = editVectorField.evaluateReal(
                         fieldcache, componentsCount)
                     for c in range(componentsCount, 3):
                         initialVector.append(0.0)
                     initialTipCoordinates = [
                         (initialCoordinates[c] +
                          initialVector[c] * pointScaleFactor)
                         for c in range(3)
                     ]
                     windowCoordinates = self.projectLocal(
                         initialTipCoordinates[0], initialTipCoordinates[1],
                         initialTipCoordinates[2], localScene)
                     finalTipCoordinates = self.unprojectLocal(
                         mousePos[0], -mousePos[1], windowCoordinates[2],
                         localScene)
                     finalVector = [
                         (finalTipCoordinates[c] - initialCoordinates[c]) /
                         pointScaleFactor for c in range(3)
                     ]
                     result = editVectorField.assignReal(
                         fieldcache, finalVector)
                 else:
                     windowCoordinates = self.projectLocal(
                         initialCoordinates[0], initialCoordinates[1],
                         initialCoordinates[2], localScene)
                     xa = self.unprojectLocal(self._lastMousePos[0],
                                              -self._lastMousePos[1],
                                              windowCoordinates[2],
                                              localScene)
                     xb = self.unprojectLocal(mousePos[0], -mousePos[1],
                                              windowCoordinates[2],
                                              localScene)
                     finalCoordinates = [
                         (initialCoordinates[c] + xb[c] - xa[c])
                         for c in range(3)
                     ]
                     result = editCoordinateField.assignReal(
                         fieldcache, finalCoordinates)
                 del editVectorField
             del editCoordinateField
             del fieldcache
         self._lastMousePos = mousePos
         event.accept()
         return
     if self._alignMode != self.AlignMode.NONE:
         mousePos = [event.x(), event.y()]
         delta = [
             mousePos[0] - self._lastMousePos[0],
             mousePos[1] - self._lastMousePos[1]
         ]
         result, eye = self._sceneviewer.getEyePosition()
         result, lookat = self._sceneviewer.getLookatPosition()
         result, up = self._sceneviewer.getUpVector()
         lookatToEye = sub(eye, lookat)
         eyeDistance = magnitude(lookatToEye)
         front = div(lookatToEye, eyeDistance)
         right = cross(up, front)
         if self._alignMode == self.AlignMode.ROTATION:
             mag = magnitude(delta)
             prop = div(delta, mag)
             axis = add(mult(up, prop[0]), mult(right, prop[1]))
             angle = mag * 0.002
             # print('delta', delta, 'axis', axis, 'angle', angle)
             self._model.interactionRotate(axis, angle)
         elif self._alignMode == self.AlignMode.SCALE:
             factor = 1.0 + delta[1] * 0.0005
             if factor < 0.9:
                 factor = 0.9
             self._model.interactionScale(factor)
         elif self._alignMode == self.AlignMode.TRANSLATION:
             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 = add(mult(right, eyeScale * delta[0]),
                          mult(up, -eyeScale * delta[1]))
             self._model.interactionTranslate(offset)
         self._lastMousePos = mousePos
         event.accept()
         return
     else:
         super(NodeEditorSceneviewerWidget, self).mouseMoveEvent(event)