]> gerrit.simantics Code Review - simantics/3d.git/blob - 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
1 package org.simantics.g3d.vtk.gizmo;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.List;\r
6 \r
7 import vtk.vtkActor;\r
8 import vtk.vtkDataSetMapper;\r
9 import vtk.vtkPoints;\r
10 import vtk.vtkPolyData;\r
11 import vtk.vtkProp;\r
12 import vtk.vtkTriangle;\r
13 \r
14 public class TranslateGizmo extends vtkGizmo {\r
15         \r
16         \r
17         \r
18         private List<vtkProp> parts;\r
19         \r
20         @Override\r
21         public Collection<vtkProp> getGizmo() {\r
22                 if (parts == null) {\r
23                         parts = new ArrayList<vtkProp>();\r
24                 \r
25                 \r
26                         float size = 2.f;               \r
27                 float sizeD2 = 1.f;\r
28                 float offset = 0.2f;\r
29                         \r
30                 double[] colorX = new double[]{0.5,0.0,0.0};\r
31                 double[] colorY = new double[]{0.0,0.5,0.0};\r
32                 double[] colorZ = new double[]{0.0,0.0,0.5};\r
33                 \r
34                 double[] colorXY = add(colorX, colorY);\r
35                 double[] colorXZ = add(colorX, colorZ);\r
36                 double[] colorYZ = add(colorY, colorZ);\r
37                 double[] colorP =  add(colorX,colorY,colorZ);\r
38                 \r
39                         vtkActor actorX = new vtkActor();\r
40                         vtkActor actorY = new vtkActor();\r
41                         vtkActor actorZ = new vtkActor();\r
42                         vtkActor actorXY = new vtkActor();\r
43                         vtkActor actorXZ = new vtkActor();\r
44                         vtkActor actorYZ = new vtkActor();\r
45                         vtkActor actorP = new vtkActor();\r
46                         \r
47                         actorX.GetProperty().SetColor(colorX);\r
48                         actorY.GetProperty().SetColor(colorY);\r
49                         actorZ.GetProperty().SetColor(colorZ);\r
50                         actorXY.GetProperty().SetColor(colorXY);\r
51                         actorXZ.GetProperty().SetColor(colorXZ);\r
52                         actorYZ.GetProperty().SetColor(colorYZ);\r
53                         actorP.GetProperty().SetColor(colorP);\r
54                         \r
55                         actorX.GetProperty().SetOpacity(0.5);\r
56                         actorY.GetProperty().SetOpacity(0.5);\r
57                         actorZ.GetProperty().SetOpacity(0.5);\r
58                         actorXY.GetProperty().SetOpacity(0.5);\r
59                         actorXZ.GetProperty().SetOpacity(0.5);\r
60                         actorYZ.GetProperty().SetOpacity(0.5);\r
61                         actorP.GetProperty().SetOpacity(0.5);\r
62                         \r
63                         actorX.GetProperty().BackfaceCullingOff();\r
64                         actorY.GetProperty().BackfaceCullingOff();\r
65                         actorZ.GetProperty().BackfaceCullingOff();\r
66                         actorXY.GetProperty().BackfaceCullingOff();\r
67                         actorXZ.GetProperty().BackfaceCullingOff();\r
68                         actorYZ.GetProperty().BackfaceCullingOff();\r
69                         actorP.GetProperty().BackfaceCullingOff();\r
70                         \r
71                         actorX.SetPickable(1);\r
72                         actorY.SetPickable(1);\r
73                         actorZ.SetPickable(1);\r
74                         actorXY.SetPickable(1);\r
75                         actorXZ.SetPickable(1);\r
76                         actorYZ.SetPickable(1);\r
77                         actorP.SetPickable(1);\r
78                         \r
79                         \r
80                         actorX.GetProperty().LightingOff();\r
81                         actorY.GetProperty().LightingOff();\r
82                         actorZ.GetProperty().LightingOff();\r
83                         actorXY.GetProperty().LightingOff();\r
84                         actorXZ.GetProperty().LightingOff();\r
85                         actorYZ.GetProperty().LightingOff();\r
86                         actorP.GetProperty().LightingOff();\r
87                         \r
88 \r
89                         vtkTriangle triangle = new vtkTriangle();\r
90                         \r
91                         // X\r
92                         vtkPolyData actorXData = new vtkPolyData();\r
93                         actorXData.Allocate(6, 6);\r
94                         \r
95                         triangle.GetPointIds().SetId(0, 0);\r
96                         triangle.GetPointIds().SetId(1, 1);\r
97                         triangle.GetPointIds().SetId(2, 3);\r
98                         actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
99                         triangle.GetPointIds().SetId(0, 1);\r
100                         triangle.GetPointIds().SetId(1, 2);\r
101                         triangle.GetPointIds().SetId(2, 3);\r
102                         actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
103                         triangle.GetPointIds().SetId(0, 0);\r
104                         triangle.GetPointIds().SetId(1, 3);\r
105                         triangle.GetPointIds().SetId(2, 5);\r
106                         actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
107                         triangle.GetPointIds().SetId(0, 3);\r
108                         triangle.GetPointIds().SetId(1, 4);\r
109                         triangle.GetPointIds().SetId(2, 5);\r
110                         actorXData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
111                         \r
112                         vtkPoints partPoints = new vtkPoints();\r
113                         partPoints.InsertPoint(0, new double[]{size,0.,0.});\r
114                         partPoints.InsertPoint(1, new double[]{size-offset,offset,0.});\r
115                         partPoints.InsertPoint(2, new double[]{sizeD2-offset,offset,0.});\r
116                         partPoints.InsertPoint(3, new double[]{sizeD2,0.,0.});\r
117                         partPoints.InsertPoint(4, new double[]{sizeD2-offset,0.,offset});\r
118                         partPoints.InsertPoint(5, new double[]{size-offset,0.,offset});\r
119                         actorXData.SetPoints(partPoints);\r
120                         partPoints.Delete();\r
121                         \r
122                         // Y\r
123                         vtkPolyData actorYData = new vtkPolyData();\r
124                         actorYData.Allocate(6, 6);\r
125                         \r
126                         triangle.GetPointIds().SetId(0, 0);\r
127                         triangle.GetPointIds().SetId(1, 1);\r
128                         triangle.GetPointIds().SetId(2, 3);\r
129                         actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
130                         triangle.GetPointIds().SetId(0, 1);\r
131                         triangle.GetPointIds().SetId(1, 2);\r
132                         triangle.GetPointIds().SetId(2, 3);\r
133                         actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
134                         triangle.GetPointIds().SetId(0, 0);\r
135                         triangle.GetPointIds().SetId(1, 3);\r
136                         triangle.GetPointIds().SetId(2, 5);\r
137                         actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
138                         triangle.GetPointIds().SetId(0, 3);\r
139                         triangle.GetPointIds().SetId(1, 4);\r
140                         triangle.GetPointIds().SetId(2, 5);\r
141                         actorYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
142                         \r
143                         partPoints = new vtkPoints();\r
144                         partPoints.InsertPoint(0, new double[]{0., size, 0.});\r
145                         partPoints.InsertPoint(1, new double[]{offset, size - offset, 0.});\r
146                         partPoints.InsertPoint(2, new double[]{offset, sizeD2 - offset, 0.});\r
147                         partPoints.InsertPoint(3, new double[]{0., sizeD2, 0.});\r
148                         partPoints.InsertPoint(4, new double[]{0., sizeD2 - offset, offset});\r
149                         partPoints.InsertPoint(5, new double[]{0., size - offset, offset});\r
150                         actorYData.SetPoints(partPoints);\r
151                         partPoints.Delete();\r
152                         \r
153                         // Z\r
154                         vtkPolyData actorZData = new vtkPolyData();\r
155                         actorZData.Allocate(6, 6);\r
156                         \r
157                         triangle.GetPointIds().SetId(0, 0);\r
158                         triangle.GetPointIds().SetId(1, 1);\r
159                         triangle.GetPointIds().SetId(2, 3);\r
160                         actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
161                         triangle.GetPointIds().SetId(0, 1);\r
162                         triangle.GetPointIds().SetId(1, 2);\r
163                         triangle.GetPointIds().SetId(2, 3);\r
164                         actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
165                         triangle.GetPointIds().SetId(0, 0);\r
166                         triangle.GetPointIds().SetId(1, 3);\r
167                         triangle.GetPointIds().SetId(2, 5);\r
168                         actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
169                         triangle.GetPointIds().SetId(0, 3);\r
170                         triangle.GetPointIds().SetId(1, 4);\r
171                         triangle.GetPointIds().SetId(2, 5);\r
172                         actorZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
173                         \r
174                         partPoints = new vtkPoints();\r
175                         partPoints.InsertPoint(0, new double[]{0., 0.,size});\r
176                         partPoints.InsertPoint(1, new double[]{offset, 0., size - offset});\r
177                         partPoints.InsertPoint(2, new double[]{offset, 0., sizeD2 - offset});\r
178                         partPoints.InsertPoint(3, new double[]{0., 0., sizeD2});\r
179                         partPoints.InsertPoint(4, new double[]{0., offset, sizeD2 - offset});\r
180                         partPoints.InsertPoint(5, new double[]{0., offset, size - offset});\r
181                         actorZData.SetPoints(partPoints);\r
182                         partPoints.Delete();\r
183                         \r
184                         // XY\r
185                         vtkPolyData actorXYData = new vtkPolyData();\r
186                         actorXYData.Allocate(4, 4);\r
187                         \r
188                         triangle.GetPointIds().SetId(0, 2);\r
189                         triangle.GetPointIds().SetId(1, 1);\r
190                         triangle.GetPointIds().SetId(2, 3);\r
191                         actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
192                         triangle.GetPointIds().SetId(0, 0);\r
193                         triangle.GetPointIds().SetId(1, 1);\r
194                         triangle.GetPointIds().SetId(2, 3);\r
195                         actorXYData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
196                         \r
197                         partPoints = new vtkPoints();\r
198                         partPoints.InsertPoint(0, new double[]{offset, size-offset, 0.f});\r
199                         partPoints.InsertPoint(1, new double[]{offset, sizeD2 - offset, 0.f});\r
200                         partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f});\r
201                         partPoints.InsertPoint(3, new double[]{size-offset, offset, 0.f});\r
202                         actorXYData.SetPoints(partPoints);\r
203                         partPoints.Delete();\r
204                         \r
205                         // XZ\r
206                         vtkPolyData actorXZData = new vtkPolyData();\r
207                         actorXZData.Allocate(4, 4);\r
208                         \r
209                         triangle.GetPointIds().SetId(0, 2);\r
210                         triangle.GetPointIds().SetId(1, 1);\r
211                         triangle.GetPointIds().SetId(2, 3);\r
212                         actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
213                         triangle.GetPointIds().SetId(0, 0);\r
214                         triangle.GetPointIds().SetId(1, 1);\r
215                         triangle.GetPointIds().SetId(2, 3);\r
216                         actorXZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
217                         \r
218                         partPoints = new vtkPoints();\r
219                         partPoints.InsertPoint(0, new double[]{offset, 0.f, size-offset});\r
220                         partPoints.InsertPoint(1, new double[]{offset, 0.f, sizeD2 - offset});\r
221                         partPoints.InsertPoint(2, new double[]{sizeD2 - offset, 0.f, offset});\r
222                         partPoints.InsertPoint(3, new double[]{size-offset, 0.f, offset});\r
223                         actorXZData.SetPoints(partPoints);\r
224                         partPoints.Delete();\r
225                         \r
226                         // YZ\r
227                         vtkPolyData actorYZData = new vtkPolyData();\r
228                         actorYZData.Allocate(4, 4);\r
229                         \r
230                         triangle.GetPointIds().SetId(0, 2);\r
231                         triangle.GetPointIds().SetId(1, 1);\r
232                         triangle.GetPointIds().SetId(2, 3);\r
233                         actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
234                         triangle.GetPointIds().SetId(0, 0);\r
235                         triangle.GetPointIds().SetId(1, 1);\r
236                         triangle.GetPointIds().SetId(2, 3);\r
237                         actorYZData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
238                         \r
239                         partPoints = new vtkPoints();\r
240                         partPoints.InsertPoint(0, new double[]{0.f,offset, size-offset});\r
241                         partPoints.InsertPoint(1, new double[]{0.f,offset, sizeD2 - offset});\r
242                         partPoints.InsertPoint(2, new double[]{0.f,sizeD2 - offset, offset});\r
243                         partPoints.InsertPoint(3, new double[]{0.f,size-offset, offset});\r
244                         \r
245                         actorYZData.SetPoints(partPoints);\r
246                         partPoints.Delete();\r
247                         \r
248                         vtkPolyData actorPData = new vtkPolyData();\r
249                         actorPData.Allocate(10, 10);\r
250                         \r
251                         triangle.GetPointIds().SetId(0, 0);\r
252                         triangle.GetPointIds().SetId(1, 1);\r
253                         triangle.GetPointIds().SetId(2, 2);\r
254                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
255                         triangle.GetPointIds().SetId(0, 0);\r
256                         triangle.GetPointIds().SetId(1, 2);\r
257                         triangle.GetPointIds().SetId(2, 3);\r
258                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
259                         triangle.GetPointIds().SetId(0, 0);\r
260                         triangle.GetPointIds().SetId(1, 3);\r
261                         triangle.GetPointIds().SetId(2, 4);\r
262                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
263                         triangle.GetPointIds().SetId(0, 0);\r
264                         triangle.GetPointIds().SetId(1, 4);\r
265                         triangle.GetPointIds().SetId(2, 5);\r
266                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
267                         triangle.GetPointIds().SetId(0, 0);\r
268                         triangle.GetPointIds().SetId(1, 5);\r
269                         triangle.GetPointIds().SetId(2, 6);\r
270                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
271                         triangle.GetPointIds().SetId(0, 0);\r
272                         triangle.GetPointIds().SetId(1, 6);\r
273                         triangle.GetPointIds().SetId(2, 7);\r
274                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
275                         triangle.GetPointIds().SetId(0, 0);\r
276                         triangle.GetPointIds().SetId(1, 7);\r
277                         triangle.GetPointIds().SetId(2, 8);\r
278                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
279                         triangle.GetPointIds().SetId(0, 0);\r
280                         triangle.GetPointIds().SetId(1, 8);\r
281                         triangle.GetPointIds().SetId(2, 9);\r
282                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
283                         triangle.GetPointIds().SetId(0, 0);\r
284                         triangle.GetPointIds().SetId(1, 9);\r
285                         triangle.GetPointIds().SetId(2, 1);\r
286                         actorPData.InsertNextCell(triangle.GetCellType(), triangle.GetPointIds());\r
287                         \r
288                         partPoints = new vtkPoints();\r
289                         partPoints.InsertPoint(0, new double[]{0.f, 0.f, 0.f});\r
290                         partPoints.InsertPoint(1, new double[]{sizeD2, 0.f, 0.f});\r
291                         partPoints.InsertPoint(2, new double[]{sizeD2 - offset, offset, 0.f});\r
292                         partPoints.InsertPoint(3, new double[]{offset, sizeD2 - offset, 0.f});\r
293                         partPoints.InsertPoint(4, new double[]{0.f, sizeD2, 0.f});\r
294                         partPoints.InsertPoint(5, new double[]{0.f, sizeD2 - offset, offset});\r
295                         partPoints.InsertPoint(6, new double[]{0.f, offset, sizeD2-offset});\r
296                         partPoints.InsertPoint(7, new double[]{0.f, 0.f, sizeD2});\r
297                         partPoints.InsertPoint(8, new double[]{offset, 0.f, sizeD2-offset});\r
298                         partPoints.InsertPoint(9, new double[]{sizeD2-offset, 0.f, offset});\r
299                         actorPData.SetPoints(partPoints);\r
300                         partPoints.Delete();\r
301                         \r
302                         \r
303                         vtkDataSetMapper partMapper = new vtkDataSetMapper();\r
304                         partMapper.SetInput(actorXData);\r
305                         partMapper.ScalarVisibilityOn();\r
306                         actorX.SetMapper(partMapper);\r
307                         partMapper.Delete();\r
308                         \r
309                         partMapper = new vtkDataSetMapper();\r
310                         partMapper.SetInput(actorYData);\r
311                         partMapper.ScalarVisibilityOn();\r
312                         actorY.SetMapper(partMapper);\r
313                         partMapper.Delete();\r
314                         \r
315                         partMapper = new vtkDataSetMapper();\r
316                         partMapper.SetInput(actorZData);\r
317                         partMapper.ScalarVisibilityOn();\r
318                         actorZ.SetMapper(partMapper);\r
319                         partMapper.Delete();\r
320                         \r
321                         partMapper = new vtkDataSetMapper();\r
322                         partMapper.SetInput(actorXYData);\r
323                         partMapper.ScalarVisibilityOn();\r
324                         actorXY.SetMapper(partMapper);\r
325                         partMapper.Delete();\r
326                         \r
327                         partMapper = new vtkDataSetMapper();\r
328                         partMapper.SetInput(actorXZData);\r
329                         partMapper.ScalarVisibilityOn();\r
330                         actorXZ.SetMapper(partMapper);\r
331                         partMapper.Delete();\r
332                         \r
333                         partMapper = new vtkDataSetMapper();\r
334                         partMapper.SetInput(actorYZData);\r
335                         partMapper.ScalarVisibilityOn();\r
336                         actorYZ.SetMapper(partMapper);\r
337                         partMapper.Delete();\r
338                         \r
339                         partMapper = new vtkDataSetMapper();\r
340                         partMapper.SetInput(actorPData);\r
341                         partMapper.ScalarVisibilityOn();\r
342                         actorP.SetMapper(partMapper);\r
343                         partMapper.Delete();\r
344                         \r
345                         \r
346                         actorXData.Delete();\r
347                         actorYData.Delete();\r
348                         actorZData.Delete();\r
349                         actorXYData.Delete();\r
350                         actorXZData.Delete();\r
351                         actorYZData.Delete();\r
352                         actorPData.Delete();\r
353                         \r
354                         parts.add(actorX);\r
355                         parts.add(actorY);\r
356                         parts.add(actorZ);\r
357                         parts.add(actorXY);\r
358                         parts.add(actorXZ);\r
359                         parts.add(actorYZ);\r
360                         parts.add(actorP);\r
361                 }\r
362                 return parts;\r
363         }\r
364         \r
365         public int getTranslateAxis(vtkActor actor) {\r
366                 if (actor == null)\r
367                         return -1;\r
368                 return parts.indexOf(actor);\r
369         }\r
370         \r
371 \r
372 }\r