--- /dev/null
+package org.simantics.g3d.vtk.shape;\r
+\r
+import javax.vecmath.Tuple3d;\r
+\r
+import vtk.vtkActor;\r
+import vtk.vtkDataSetMapper;\r
+import vtk.vtkLine;\r
+import vtk.vtkPoints;\r
+import vtk.vtkPolyLine;\r
+import vtk.vtkUnstructuredGrid;\r
+\r
+public class vtkShape {\r
+ \r
+ /**\r
+ * Creates a grid shaped actor.\r
+ * \r
+ * @param size number of grid lines\r
+ * @param space distance between grid lines\r
+ * @param axes bitmask of axes: 1:x, 2:y, 4:z\r
+ * @return vtkActor representing a grid.\r
+ */\r
+ public static vtkActor createGridActor(int size, double space, int axes) {\r
+ int gridCount = 0;\r
+ if ((axes & 0x1) > 0) {\r
+ gridCount++;\r
+ }\r
+ if ((axes & 0x2) > 0) {\r
+ gridCount++;\r
+ }\r
+ if ((axes & 0x4) > 0) {\r
+ gridCount++;\r
+ }\r
+ int pointCount = (size+1) * 2 * 2 * gridCount;\r
+ vtkPoints linePoints = new vtkPoints();\r
+ linePoints.SetNumberOfPoints(pointCount);\r
+ \r
+ \r
+ double max = space * (double)size * 0.5;\r
+ double min = -max;\r
+ int base = 0;\r
+ if ((axes & 0x1) > 0) {\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,0.0, s, min);\r
+ linePoints.InsertPoint(base + i*2+1,0.0, s, max);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,0.0, s, max);\r
+ linePoints.InsertPoint(base + i*2+1,0.0, s, min);\r
+ }\r
+ base += (size+1)*2;\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 , 0.0, max, s);\r
+ linePoints.InsertPoint(base + i*2+1, 0.0, min, s);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 , 0.0, min, s);\r
+ linePoints.InsertPoint(base + i*2+1, 0.0, max, s);\r
+ }\r
+ base += (size+1)*2;\r
+ } \r
+ if ((axes & 0x4) > 0) {\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,s, min, 0.0);\r
+ linePoints.InsertPoint(base + i*2+1,s, max, 0.0);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,s, max, 0.0);\r
+ linePoints.InsertPoint(base + i*2+1,s, min, 0.0);\r
+ }\r
+ base += (size+1)*2;\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,max, s, 0.0);\r
+ linePoints.InsertPoint(base + i*2+1,min, s, 0.0);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,min, s, 0.0);\r
+ linePoints.InsertPoint(base + i*2+1,max, s, 0.0);\r
+ }\r
+ base += (size+1)*2;\r
+ } \r
+ if ((axes & 0x2) > 0) {\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,s, 0.0, min);\r
+ linePoints.InsertPoint(base + i*2+1,s, 0.0, max);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,s, 0.0, max);\r
+ linePoints.InsertPoint(base + i*2+1,s, 0.0, min);\r
+ }\r
+ base += (size+1)*2;\r
+ for (int i = 0; i <= size; i++) {\r
+ double s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,max, 0.0, s);\r
+ linePoints.InsertPoint(base + i*2+1,min, 0.0, s);\r
+ i++;\r
+ if (i > size)\r
+ break;\r
+ s = min + ((double)i) * space;\r
+ linePoints.InsertPoint(base + i*2 ,min, 0.0, s);\r
+ linePoints.InsertPoint(base + i*2+1,max, 0.0, s);\r
+ }\r
+ base += (size+1)*2;\r
+ }\r
+ \r
+ \r
+ //vtkLine aLine = new vtkLine();\r
+ vtkPolyLine aLine = new vtkPolyLine();\r
+ aLine.GetPointIds().SetNumberOfIds(pointCount);\r
+ for (int i = 0; i < pointCount; i++) {\r
+ aLine.GetPointIds().SetId(i, i);\r
+ }\r
+ \r
+ \r
+ vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();\r
+ aLineGrid.Allocate(pointCount, pointCount);\r
+ aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());\r
+ aLineGrid.SetPoints(linePoints);\r
+ vtkDataSetMapper aLineMapper = new vtkDataSetMapper();\r
+ aLineMapper.SetInput(aLineGrid);\r
+ vtkActor aLineActor = new vtkActor();\r
+ aLineActor.SetMapper(aLineMapper);\r
+ \r
+ linePoints.Delete();\r
+ aLine.GetPointIds().Delete();\r
+ aLine.Delete();\r
+ aLineGrid.Delete();\r
+ aLineMapper.Delete();\r
+ \r
+ aLineActor.GetProperty().SetColor(0, 0, 0);\r
+ aLineActor.GetProperty().Delete();\r
+ \r
+ return aLineActor;\r
+ }\r
+ \r
+ /**\r
+ * Creates a line.\r
+ * \r
+ * @param p1\r
+ * @param p2\r
+ * @return\r
+ */\r
+ public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {\r
+ vtkPoints linePoints = new vtkPoints();\r
+ linePoints.SetNumberOfPoints(2);\r
+ linePoints.InsertPoint(0,p1.x, p1.y, p1.z);\r
+ linePoints.InsertPoint(1,p2.x, p2.y, p2.z);\r
+ vtkLine aLine = new vtkLine();\r
+ aLine.GetPointIds().SetId(0, 0);\r
+ aLine.GetPointIds().SetId(1, 1);\r
+ vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();\r
+ aLineGrid.Allocate(1, 1);\r
+ aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());\r
+ aLineGrid.SetPoints(linePoints);\r
+ vtkDataSetMapper aLineMapper = new vtkDataSetMapper();\r
+ aLineMapper.SetInput(aLineGrid);\r
+ vtkActor aLineActor = new vtkActor();\r
+ aLineActor.SetMapper(aLineMapper);\r
+ //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);\r
+ \r
+ linePoints.Delete();\r
+ aLine.GetPointIds().Delete();\r
+ aLine.Delete();\r
+ aLineGrid.Delete();\r
+ aLineMapper.Delete();\r
+ \r
+ return aLineActor;\r
+ }\r
+\r
+}\r