]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/vtkShape.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / vtkShape.java
1 package org.simantics.g3d.vtk.shape;\r
2 \r
3 import javax.vecmath.Tuple3d;\r
4 \r
5 import vtk.vtkActor;\r
6 import vtk.vtkDataSetMapper;\r
7 import vtk.vtkLine;\r
8 import vtk.vtkPoints;\r
9 import vtk.vtkPolyLine;\r
10 import vtk.vtkUnstructuredGrid;\r
11 \r
12 public class vtkShape {\r
13         \r
14         /**\r
15          * Creates a grid shaped actor.\r
16          * \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
21          */\r
22         public static vtkActor createGridActor(int size, double space, int axes) {\r
23                 int gridCount = 0;\r
24                 if ((axes & 0x1) > 0) {\r
25                         gridCount++;\r
26                 }\r
27                 if ((axes & 0x2) > 0) {\r
28                         gridCount++;\r
29                 }\r
30                 if ((axes & 0x4) > 0) {\r
31                         gridCount++;\r
32                 }\r
33                 int pointCount = (size+1) * 2 * 2 * gridCount;\r
34                 vtkPoints linePoints = new vtkPoints();\r
35                 linePoints.SetNumberOfPoints(pointCount);\r
36                 \r
37                 \r
38                 double max = space * (double)size * 0.5;\r
39                 double min = -max;\r
40                 int base = 0;\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
46                                 i++;\r
47                                 if (i > size)\r
48                                         break;\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
52                         }\r
53                         base += (size+1)*2;\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
58                                 i++;\r
59                                 if (i > size)\r
60                                         break;\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
64                         }\r
65                         base += (size+1)*2;\r
66                 } \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
72                                 i++;\r
73                                 if (i > size)\r
74                                         break;\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
78                         }\r
79                         base += (size+1)*2;\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
84                                 i++;\r
85                                 if (i > size)\r
86                                         break;\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
90                         }\r
91                         base += (size+1)*2;\r
92                 } \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
98                                 i++;\r
99                                 if (i > size)\r
100                                         break;\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
104                         }\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
110                                 i++;\r
111                                 if (i > size)\r
112                                         break;\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
116                         }\r
117                         base += (size+1)*2;\r
118                 }\r
119                         \r
120                 \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
126                 }\r
127                 \r
128                 \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
137                 \r
138                 linePoints.Delete();\r
139                 aLine.GetPointIds().Delete();\r
140             aLine.Delete();\r
141             aLineGrid.Delete();\r
142             aLineMapper.Delete();\r
143             \r
144             aLineActor.GetProperty().SetColor(0, 0, 0);\r
145             aLineActor.GetProperty().Delete();\r
146             \r
147             return aLineActor;\r
148         }\r
149         \r
150         /**\r
151          * Creates a line.\r
152          * \r
153          * @param p1\r
154          * @param p2\r
155          * @return\r
156          */\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
174             \r
175             linePoints.Delete();\r
176             aLine.GetPointIds().Delete();\r
177             aLine.Delete();\r
178             aLineGrid.Delete();\r
179             aLineMapper.Delete();\r
180             \r
181             return aLineActor;\r
182         }\r
183 \r
184 }\r