-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
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.shape;
+
+import javax.vecmath.Point3d;
+import javax.vecmath.Tuple3d;
+
+import vtk.vtkActor;
+import vtk.vtkDataSetMapper;
+import vtk.vtkLine;
+import vtk.vtkPoints;
+import vtk.vtkPolyLine;
+import vtk.vtkUnstructuredGrid;
+
+public class vtkShape {
+
+ /**
+ * Creates a grid shaped actor.
+ *
+ * @param size number of grid lines
+ * @param space distance between grid lines
+ * @param axes bitmask of axes: 1:x, 2:y, 4:z
+ * @return vtkActor representing a grid.
+ */
+ public static vtkActor createGridActor(int size, double space, int axes) {
+ int gridCount = 0;
+ if ((axes & 0x1) > 0) {
+ gridCount++;
+ }
+ if ((axes & 0x2) > 0) {
+ gridCount++;
+ }
+ if ((axes & 0x4) > 0) {
+ gridCount++;
+ }
+ int pointCount = (size+1) * 2 * 2 * gridCount;
+ vtkPoints linePoints = new vtkPoints();
+ linePoints.SetNumberOfPoints(pointCount);
+
+
+ double max = space * (double)size * 0.5;
+ double min = -max;
+ int base = 0;
+ if ((axes & 0x1) > 0) {
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,0.0, s, min);
+ linePoints.InsertPoint(base + i*2+1,0.0, s, max);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,0.0, s, max);
+ linePoints.InsertPoint(base + i*2+1,0.0, s, min);
+ }
+ base += (size+1)*2;
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 , 0.0, max, s);
+ linePoints.InsertPoint(base + i*2+1, 0.0, min, s);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 , 0.0, min, s);
+ linePoints.InsertPoint(base + i*2+1, 0.0, max, s);
+ }
+ base += (size+1)*2;
+ }
+ if ((axes & 0x4) > 0) {
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,s, min, 0.0);
+ linePoints.InsertPoint(base + i*2+1,s, max, 0.0);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,s, max, 0.0);
+ linePoints.InsertPoint(base + i*2+1,s, min, 0.0);
+ }
+ base += (size+1)*2;
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,max, s, 0.0);
+ linePoints.InsertPoint(base + i*2+1,min, s, 0.0);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,min, s, 0.0);
+ linePoints.InsertPoint(base + i*2+1,max, s, 0.0);
+ }
+ base += (size+1)*2;
+ }
+ if ((axes & 0x2) > 0) {
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,s, 0.0, min);
+ linePoints.InsertPoint(base + i*2+1,s, 0.0, max);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,s, 0.0, max);
+ linePoints.InsertPoint(base + i*2+1,s, 0.0, min);
+ }
+ base += (size+1)*2;
+ for (int i = 0; i <= size; i++) {
+ double s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,max, 0.0, s);
+ linePoints.InsertPoint(base + i*2+1,min, 0.0, s);
+ i++;
+ if (i > size)
+ break;
+ s = min + ((double)i) * space;
+ linePoints.InsertPoint(base + i*2 ,min, 0.0, s);
+ linePoints.InsertPoint(base + i*2+1,max, 0.0, s);
+ }
+ base += (size+1)*2;
+ }
+
+
+ //vtkLine aLine = new vtkLine();
+ vtkPolyLine aLine = new vtkPolyLine();
+ aLine.GetPointIds().SetNumberOfIds(pointCount);
+ for (int i = 0; i < pointCount; i++) {
+ aLine.GetPointIds().SetId(i, i);
+ }
+
+
+ vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
+ aLineGrid.Allocate(pointCount, pointCount);
+ aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
+ aLineGrid.SetPoints(linePoints);
+ vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
+ aLineMapper.SetInputData(aLineGrid);
+ vtkActor aLineActor = new vtkActor();
+ aLineActor.SetMapper(aLineMapper);
+
+ linePoints.Delete();
+ aLine.GetPointIds().Delete();
+ aLine.Delete();
+ aLineGrid.Delete();
+ aLineMapper.Delete();
+
+ aLineActor.GetProperty().SetColor(0, 0, 0);
+ aLineActor.GetProperty().Delete();
+
+ return aLineActor;
+ }
+
+ /**
+ * Creates a line.
+ *
+ * @param p1
+ * @param p2
+ * @return
+ */
+ public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {
+ vtkPoints linePoints = new vtkPoints();
+ linePoints.SetNumberOfPoints(2);
+ linePoints.InsertPoint(0,p1.x, p1.y, p1.z);
+ linePoints.InsertPoint(1,p2.x, p2.y, p2.z);
+ vtkLine aLine = new vtkLine();
+ aLine.GetPointIds().SetId(0, 0);
+ aLine.GetPointIds().SetId(1, 1);
+ vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
+ aLineGrid.Allocate(1, 1);
+ aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
+ aLineGrid.SetPoints(linePoints);
+ vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
+ aLineMapper.SetInputData(aLineGrid);
+ vtkActor aLineActor = new vtkActor();
+ aLineActor.SetMapper(aLineMapper);
+ //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
+
+ linePoints.Delete();
+ aLine.GetPointIds().Delete();
+ aLine.Delete();
+ aLineGrid.Delete();
+ aLineMapper.Delete();
+
+ return aLineActor;
+ }
+
+ public static vtkActor createLineActor(Tuple3d... p) {
+ vtkPoints linePoints = new vtkPoints();
+ linePoints.SetNumberOfPoints(p.length);
+
+ for (int i = 0; i < p.length; i++) {
+ Tuple3d p1 = p[i];
+ linePoints.InsertPoint(i,p1.x, p1.y, p1.z);
+
+ }
+ vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
+ //aLineGrid.Allocate(1, 1);
+ for (int i = 0; i< p.length -1; i++) {
+ vtkLine aLine = new vtkLine();
+ aLine.GetPointIds().SetId(0, i);
+ aLine.GetPointIds().SetId(1, i+1);
+ aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
+ aLine.GetPointIds().Delete();
+ aLine.Delete();
+ }
+
+ aLineGrid.SetPoints(linePoints);
+ vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
+ aLineMapper.SetInputData(aLineGrid);
+ vtkActor aLineActor = new vtkActor();
+ aLineActor.SetMapper(aLineMapper);
+ //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
+
+ linePoints.Delete();
+
+ aLineGrid.Delete();
+ aLineMapper.Delete();
+
+ return aLineActor;
+ }
+
+}
+