1 package org.simantics.g3d.vtk.shape;
\r
3 import javax.vecmath.Tuple3d;
\r
6 import vtk.vtkDataSetMapper;
\r
8 import vtk.vtkPoints;
\r
9 import vtk.vtkPolyLine;
\r
10 import vtk.vtkUnstructuredGrid;
\r
12 public class vtkShape {
\r
15 * Creates a grid shaped actor.
\r
17 * @param size number of grid lines
\r
18 * @param space distance between grid lines
\r
19 * @param axes bitmask of axes: 1:x, 2:y, 4:z
\r
20 * @return vtkActor representing a grid.
\r
22 public static vtkActor createGridActor(int size, double space, int axes) {
\r
24 if ((axes & 0x1) > 0) {
\r
27 if ((axes & 0x2) > 0) {
\r
30 if ((axes & 0x4) > 0) {
\r
33 int pointCount = (size+1) * 2 * 2 * gridCount;
\r
34 vtkPoints linePoints = new vtkPoints();
\r
35 linePoints.SetNumberOfPoints(pointCount);
\r
38 double max = space * (double)size * 0.5;
\r
41 if ((axes & 0x1) > 0) {
\r
42 for (int i = 0; i <= size; i++) {
\r
43 double s = min + ((double)i) * space;
\r
44 linePoints.InsertPoint(base + i*2 ,0.0, s, min);
\r
45 linePoints.InsertPoint(base + i*2+1,0.0, s, max);
\r
49 s = min + ((double)i) * space;
\r
50 linePoints.InsertPoint(base + i*2 ,0.0, s, max);
\r
51 linePoints.InsertPoint(base + i*2+1,0.0, s, min);
\r
54 for (int i = 0; i <= size; i++) {
\r
55 double s = min + ((double)i) * space;
\r
56 linePoints.InsertPoint(base + i*2 , 0.0, max, s);
\r
57 linePoints.InsertPoint(base + i*2+1, 0.0, min, s);
\r
61 s = min + ((double)i) * space;
\r
62 linePoints.InsertPoint(base + i*2 , 0.0, min, s);
\r
63 linePoints.InsertPoint(base + i*2+1, 0.0, max, s);
\r
67 if ((axes & 0x4) > 0) {
\r
68 for (int i = 0; i <= size; i++) {
\r
69 double s = min + ((double)i) * space;
\r
70 linePoints.InsertPoint(base + i*2 ,s, min, 0.0);
\r
71 linePoints.InsertPoint(base + i*2+1,s, max, 0.0);
\r
75 s = min + ((double)i) * space;
\r
76 linePoints.InsertPoint(base + i*2 ,s, max, 0.0);
\r
77 linePoints.InsertPoint(base + i*2+1,s, min, 0.0);
\r
80 for (int i = 0; i <= size; i++) {
\r
81 double s = min + ((double)i) * space;
\r
82 linePoints.InsertPoint(base + i*2 ,max, s, 0.0);
\r
83 linePoints.InsertPoint(base + i*2+1,min, s, 0.0);
\r
87 s = min + ((double)i) * space;
\r
88 linePoints.InsertPoint(base + i*2 ,min, s, 0.0);
\r
89 linePoints.InsertPoint(base + i*2+1,max, s, 0.0);
\r
93 if ((axes & 0x2) > 0) {
\r
94 for (int i = 0; i <= size; i++) {
\r
95 double s = min + ((double)i) * space;
\r
96 linePoints.InsertPoint(base + i*2 ,s, 0.0, min);
\r
97 linePoints.InsertPoint(base + i*2+1,s, 0.0, max);
\r
101 s = min + ((double)i) * space;
\r
102 linePoints.InsertPoint(base + i*2 ,s, 0.0, max);
\r
103 linePoints.InsertPoint(base + i*2+1,s, 0.0, min);
\r
105 base += (size+1)*2;
\r
106 for (int i = 0; i <= size; i++) {
\r
107 double s = min + ((double)i) * space;
\r
108 linePoints.InsertPoint(base + i*2 ,max, 0.0, s);
\r
109 linePoints.InsertPoint(base + i*2+1,min, 0.0, s);
\r
113 s = min + ((double)i) * space;
\r
114 linePoints.InsertPoint(base + i*2 ,min, 0.0, s);
\r
115 linePoints.InsertPoint(base + i*2+1,max, 0.0, s);
\r
117 base += (size+1)*2;
\r
121 //vtkLine aLine = new vtkLine();
\r
122 vtkPolyLine aLine = new vtkPolyLine();
\r
123 aLine.GetPointIds().SetNumberOfIds(pointCount);
\r
124 for (int i = 0; i < pointCount; i++) {
\r
125 aLine.GetPointIds().SetId(i, i);
\r
129 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
\r
130 aLineGrid.Allocate(pointCount, pointCount);
\r
131 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
\r
132 aLineGrid.SetPoints(linePoints);
\r
133 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
\r
134 aLineMapper.SetInput(aLineGrid);
\r
135 vtkActor aLineActor = new vtkActor();
\r
136 aLineActor.SetMapper(aLineMapper);
\r
138 linePoints.Delete();
\r
139 aLine.GetPointIds().Delete();
\r
141 aLineGrid.Delete();
\r
142 aLineMapper.Delete();
\r
144 aLineActor.GetProperty().SetColor(0, 0, 0);
\r
145 aLineActor.GetProperty().Delete();
\r
157 public static vtkActor createLineActor(Tuple3d p1, Tuple3d p2) {
\r
158 vtkPoints linePoints = new vtkPoints();
\r
159 linePoints.SetNumberOfPoints(2);
\r
160 linePoints.InsertPoint(0,p1.x, p1.y, p1.z);
\r
161 linePoints.InsertPoint(1,p2.x, p2.y, p2.z);
\r
162 vtkLine aLine = new vtkLine();
\r
163 aLine.GetPointIds().SetId(0, 0);
\r
164 aLine.GetPointIds().SetId(1, 1);
\r
165 vtkUnstructuredGrid aLineGrid = new vtkUnstructuredGrid();
\r
166 aLineGrid.Allocate(1, 1);
\r
167 aLineGrid.InsertNextCell(aLine.GetCellType(), aLine.GetPointIds());
\r
168 aLineGrid.SetPoints(linePoints);
\r
169 vtkDataSetMapper aLineMapper = new vtkDataSetMapper();
\r
170 aLineMapper.SetInput(aLineGrid);
\r
171 vtkActor aLineActor = new vtkActor();
\r
172 aLineActor.SetMapper(aLineMapper);
\r
173 //aLineActor.GetProperty().SetDiffuseColor(.2, 1, 1);
\r
175 linePoints.Delete();
\r
176 aLine.GetPointIds().Delete();
\r
178 aLineGrid.Delete();
\r
179 aLineMapper.Delete();
\r