package org.simantics.g3d.vtk.gizmo; import java.util.ArrayList; import java.util.Collection; import java.util.List; import vtk.vtkActor; import vtk.vtkDataSetMapper; import vtk.vtkPoints; import vtk.vtkPolyData; import vtk.vtkProp; import vtk.vtkTriangle; public class TranslateGizmo extends vtkGizmo { private List parts; @Override public Collection getGizmo() { if (parts == null) { parts = new ArrayList(); float size = 2.f; float sizeD2 = 1.f; float offset = 0.2f; double[] colorX = new double[]{0.5,0.0,0.0}; double[] colorY = new double[]{0.0,0.5,0.0}; double[] colorZ = new double[]{0.0,0.0,0.5}; double[] colorXY = add(colorX, colorY); double[] colorXZ = add(colorX, colorZ); double[] colorYZ = add(colorY, colorZ); double[] colorP = add(colorX,colorY,colorZ); vtkActor actorX = new vtkActor(); vtkActor actorY = new vtkActor(); vtkActor actorZ = new vtkActor(); vtkActor actorXY = new vtkActor(); vtkActor actorXZ = new vtkActor(); vtkActor actorYZ = new vtkActor(); vtkActor actorP = new vtkActor(); actorX.GetProperty().SetColor(colorX); actorY.GetProperty().SetColor(colorY); actorZ.GetProperty().SetColor(colorZ); actorXY.GetProperty().SetColor(colorXY); actorXZ.GetProperty().SetColor(colorXZ); actorYZ.GetProperty().SetColor(colorYZ); actorP.GetProperty().SetColor(colorP); actorX.GetProperty().SetOpacity(0.5); actorY.GetProperty().SetOpacity(0.5); actorZ.GetProperty().SetOpacity(0.5); actorXY.GetProperty().SetOpacity(0.5); actorXZ.GetProperty().SetOpacity(0.5); actorYZ.GetProperty().SetOpacity(0.5); actorP.GetProperty().SetOpacity(0.5); actorX.GetProperty().BackfaceCullingOff(); actorY.GetProperty().BackfaceCullingOff(); actorZ.GetProperty().BackfaceCullingOff(); actorXY.GetProperty().BackfaceCullingOff(); actorXZ.GetProperty().BackfaceCullingOff(); actorYZ.GetProperty().BackfaceCullingOff(); actorP.GetProperty().BackfaceCullingOff(); actorX.SetPickable(1); actorY.SetPickable(1); actorZ.SetPickable(1); actorXY.SetPickable(1); actorXZ.SetPickable(1); actorYZ.SetPickable(1); actorP.SetPickable(1); actorX.GetProperty().LightingOff(); actorY.GetProperty().LightingOff(); actorZ.GetProperty().LightingOff(); actorXY.GetProperty().LightingOff(); actorXZ.GetProperty().LightingOff(); actorYZ.GetProperty().LightingOff(); actorP.GetProperty().LightingOff(); vtkTriangle triangle = new vtkTriangle(); // X vtkPolyData actorXData = new vtkPolyData(); actorXData.Allocate(6, 6); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 1); triangle.GetPointIds().SetId(1, 2); triangle.GetPointIds().SetId(2, 3); actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 3); triangle.GetPointIds().SetId(2, 5); actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 3); triangle.GetPointIds().SetId(1, 4); triangle.GetPointIds().SetId(2, 5); actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); vtkPoints partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{size,0.,0.}); partPoints.InsertPoint(1, new double[]{size-offset,offset,0.}); partPoints.InsertPoint(2, new double[]{sizeD2-offset,offset,0.}); partPoints.InsertPoint(3, new double[]{sizeD2,0.,0.}); partPoints.InsertPoint(4, new double[]{sizeD2-offset,0.,offset}); partPoints.InsertPoint(5, new double[]{size-offset,0.,offset}); actorXData.SetPoints(partPoints); partPoints.Delete(); // Y vtkPolyData actorYData = new vtkPolyData(); actorYData.Allocate(6, 6); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 1); triangle.GetPointIds().SetId(1, 2); triangle.GetPointIds().SetId(2, 3); actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 3); triangle.GetPointIds().SetId(2, 5); actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 3); triangle.GetPointIds().SetId(1, 4); triangle.GetPointIds().SetId(2, 5); actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{0., size, 0.}); partPoints.InsertPoint(1, new double[]{offset, size - offset, 0.}); partPoints.InsertPoint(2, new double[]{offset, sizeD2 - offset, 0.}); partPoints.InsertPoint(3, new double[]{0., sizeD2, 0.}); partPoints.InsertPoint(4, new double[]{0., sizeD2 - offset, offset}); partPoints.InsertPoint(5, new double[]{0., size - offset, offset}); actorYData.SetPoints(partPoints); partPoints.Delete(); // Z vtkPolyData actorZData = new vtkPolyData(); actorZData.Allocate(6, 6); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 1); triangle.GetPointIds().SetId(1, 2); triangle.GetPointIds().SetId(2, 3); actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 3); triangle.GetPointIds().SetId(2, 5); actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 3); triangle.GetPointIds().SetId(1, 4); triangle.GetPointIds().SetId(2, 5); actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{0., 0.,size}); partPoints.InsertPoint(1, new double[]{offset, 0., size - offset}); partPoints.InsertPoint(2, new double[]{offset, 0., sizeD2 - offset}); partPoints.InsertPoint(3, new double[]{0., 0., sizeD2}); partPoints.InsertPoint(4, new double[]{0., offset, sizeD2 - offset}); partPoints.InsertPoint(5, new double[]{0., offset, size - offset}); actorZData.SetPoints(partPoints); partPoints.Delete(); // XY vtkPolyData actorXYData = new vtkPolyData(); actorXYData.Allocate(4, 4); triangle.GetPointIds().SetId(0, 2); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{offset, size-offset, 0.f}); partPoints.InsertPoint(1, new double[]{offset, sizeD2 - offset, 0.f}); partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); partPoints.InsertPoint(3, new double[]{size-offset, offset, 0.f}); actorXYData.SetPoints(partPoints); partPoints.Delete(); // XZ vtkPolyData actorXZData = new vtkPolyData(); actorXZData.Allocate(4, 4); triangle.GetPointIds().SetId(0, 2); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{offset, 0.f, size-offset}); partPoints.InsertPoint(1, new double[]{offset, 0.f, sizeD2 - offset}); partPoints.InsertPoint(2, new double[]{sizeD2 - offset, 0.f, offset}); partPoints.InsertPoint(3, new double[]{size-offset, 0.f, offset}); actorXZData.SetPoints(partPoints); partPoints.Delete(); // YZ vtkPolyData actorYZData = new vtkPolyData(); actorYZData.Allocate(4, 4); triangle.GetPointIds().SetId(0, 2); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 3); actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{0.f,offset, size-offset}); partPoints.InsertPoint(1, new double[]{0.f,offset, sizeD2 - offset}); partPoints.InsertPoint(2, new double[]{0.f,sizeD2 - offset, offset}); partPoints.InsertPoint(3, new double[]{0.f,size-offset, offset}); actorYZData.SetPoints(partPoints); partPoints.Delete(); vtkPolyData actorPData = new vtkPolyData(); actorPData.Allocate(10, 10); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 1); triangle.GetPointIds().SetId(2, 2); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 2); triangle.GetPointIds().SetId(2, 3); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 3); triangle.GetPointIds().SetId(2, 4); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 4); triangle.GetPointIds().SetId(2, 5); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 5); triangle.GetPointIds().SetId(2, 6); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 6); triangle.GetPointIds().SetId(2, 7); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 7); triangle.GetPointIds().SetId(2, 8); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 8); triangle.GetPointIds().SetId(2, 9); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); triangle.GetPointIds().SetId(0, 0); triangle.GetPointIds().SetId(1, 9); triangle.GetPointIds().SetId(2, 1); actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds()); partPoints = new vtkPoints(); partPoints.InsertPoint(0, new double[]{0.f, 0.f, 0.f}); partPoints.InsertPoint(1, new double[]{sizeD2, 0.f, 0.f}); partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f}); partPoints.InsertPoint(3, new double[]{offset, sizeD2 - offset, 0.f}); partPoints.InsertPoint(4, new double[]{0.f, sizeD2, 0.f}); partPoints.InsertPoint(5, new double[]{0.f, sizeD2 - offset, offset}); partPoints.InsertPoint(6, new double[]{0.f, offset, sizeD2-offset}); partPoints.InsertPoint(7, new double[]{0.f, 0.f, sizeD2}); partPoints.InsertPoint(8, new double[]{offset, 0.f, sizeD2-offset}); partPoints.InsertPoint(9, new double[]{sizeD2-offset, 0.f, offset}); actorPData.SetPoints(partPoints); partPoints.Delete(); vtkDataSetMapper partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorXData); partMapper.ScalarVisibilityOn(); actorX.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorYData); partMapper.ScalarVisibilityOn(); actorY.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorZData); partMapper.ScalarVisibilityOn(); actorZ.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorXYData); partMapper.ScalarVisibilityOn(); actorXY.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorXZData); partMapper.ScalarVisibilityOn(); actorXZ.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorYZData); partMapper.ScalarVisibilityOn(); actorYZ.SetMapper(partMapper); partMapper.Delete(); partMapper = new vtkDataSetMapper(); partMapper.SetInput(actorPData); partMapper.ScalarVisibilityOn(); actorP.SetMapper(partMapper); partMapper.Delete(); actorXData.Delete(); actorYData.Delete(); actorZData.Delete(); actorXYData.Delete(); actorXZData.Delete(); actorYZData.Delete(); actorPData.Delete(); parts.add(actorX); parts.add(actorY); parts.add(actorZ); parts.add(actorXY); parts.add(actorXZ); parts.add(actorYZ); parts.add(actorP); } return parts; } public int getTranslateAxis(vtkActor actor) { if (actor == null) return -1; return parts.indexOf(actor); } }