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