Merge pull request #2385 from alicevision/Viewer3DBoundingBox

Viewer3D: Adjust bounding-box by moving faces
This commit is contained in:
Fabien Castan 2024-05-12 23:35:09 +02:00 committed by GitHub
commit bfea635eb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -57,6 +57,11 @@ Entity {
Z
}
enum Direction {
Forward,
Backward
}
enum Type {
TRANSLATION,
ROTATION,
@ -72,6 +77,13 @@ Entity {
}
}
function convertDirectionEnum(direction) {
switch (direction) {
case TransformGizmo.Direction.Forward: return 1
case TransformGizmo.Direction.Backward: return -1
}
}
function convertTypeEnum(type) {
switch (type) {
case TransformGizmo.Type.TRANSLATION: return "TRANSLATION"
@ -215,11 +227,12 @@ Entity {
// Get the selected axis
const pickedAxis = convertAxisEnum(objectPicker.gizmoAxis)
// TRANSLATION or SCALE transformation
if (objectPicker.gizmoType === TransformGizmo.Type.TRANSLATION || objectPicker.gizmoType === TransformGizmo.Type.SCALE) {
// TRANSLATION, SCALE or SURFACE MOVE transformation = SURFACE MOVE is a combination of TRANSLATION AND SCALE
if (objectPicker.gizmoType === TransformGizmo.Type.TRANSLATION || objectPicker.gizmoType === TransformGizmo.Type.SCALE || objectPicker.gizmoType === TransformGizmo.Type.ALL) {
// Compute the vector PickedPosition -> CurrentMousePoint
const pickedPosition = objectPicker.screenPoint
const mouseVector = Qt.vector2d(mouse.x - pickedPosition.x, -(mouse.y - pickedPosition.y))
const mouseVector = Qt.vector2d((mouse.x - pickedPosition.x), -(mouse.y - pickedPosition.y))
// Transform the positive picked axis vector from World Coord to Screen Coord
const gizmoLocalPointOnAxis = gizmoDisplayTransform.matrix.times(Qt.vector4d(pickedAxis.x, pickedAxis.y, pickedAxis.z, 1))
@ -243,6 +256,11 @@ Entity {
doRelativeScale(objectPicker.modelMatrix, pickedAxis.times(offset)) // Do a scale on one axis from the initial Object Model Matrix when we picked the gizmo
}
else if (objectPicker.gizmoType === TransformGizmo.Type.ALL && offset !== 0) {
const sign = convertDirectionEnum(objectPicker.gizmoDirection)
doRelativeScale(objectPicker.modelMatrix, pickedAxis.times(sign * offset/2))
doRelativeTranslation(objectPicker.modelMatrix, pickedAxis.times(offset/2))
}
return
}
// ROTATION transformation
@ -416,7 +434,6 @@ Entity {
}
}
}
Entity {
id: axisScaleBox
components: [cubeScaleMesh, cubeScaleTransform, scaleMaterial, scalePicker, frontLayerComponent]
@ -541,6 +558,86 @@ Entity {
}
}
// MOVE SURFACE ENTITY INSTANTIATOR => Forward/Backward axis directions
// The bounding box has 6 surfaces. Each of the three axes is pointing to a surface.
// These three surfaces have "forward direction". The three othersurfaces have "backward direction".
NodeInstantiator {
model: 2
active: !root.uniformScale // shouldn't be active for SfmTransform Gizmo node for example
Entity {
property int direction : {
switch (index) {
case 0: return TransformGizmo.Direction.Forward
case 1: return TransformGizmo.Direction.Backward
}
}
// MOVE SURFACE ENTITY
Entity {
id: surfaceMoveEntity
components: [surfaceMesh, surfaceTransform, surfaceMaterial, frontLayerComponent, surfacePicker]
SphereMesh {
id: surfaceMesh
radius: 0.04
rings: 8
slices: 8
}
Transform {
id: surfaceTransform
matrix: {
const m = Qt.matrix4x4()
const sign = convertDirectionEnum(direction)
const offset = 0.3
switch (axis) {
case TransformGizmo.Axis.X: {
m.translate(Qt.vector3d(sign * (objectTransform.scale3D.x + offset) / gizmoDisplayTransform.scale3D.x, 0, 0))
m.rotate(-90, Qt.vector3d(0, 0, 1))
break
}
case TransformGizmo.Axis.Y: {
m.translate(Qt.vector3d(0, sign * (objectTransform.scale3D.y + offset) / gizmoDisplayTransform.scale3D.y, 0))
break
}
case TransformGizmo.Axis.Z: {
m.translate(Qt.vector3d(0, 0, sign * (objectTransform.scale3D.z + offset) / gizmoDisplayTransform.scale3D.z))
m.rotate(90, Qt.vector3d(1, 0, 0))
break
}
}
return m
}
}
PhongMaterial {
id: surfaceMaterial
ambient: baseColor
}
TransformGizmoPicker {
id: surfacePicker
mouseController: mouseHandler
gizmoMaterial: surfaceMaterial
gizmoBaseColor: baseColor
gizmoAxis: axis
gizmoType: TransformGizmo.Type.ALL
property var gizmoDirection: direction
onPickedChanged: {
// Save the current transformations of the OBJECT
this.modelMatrix = Transformations3DHelper.modelMatrixToMatrices(objectTransform.matrix)
// Compute a scale unit at picking time
this.scaleUnit = Transformations3DHelper.computeScaleUnitFromModelMatrix(convertAxisEnum(gizmoAxis), gizmoDisplayTransform.matrix, camera, root.windowSize)
// Prevent camera transformations
root.pickedChanged(picker.isPressed)
}
}
}
}
}
// ROTATION ENTITY
Entity {
id: rotationEntity