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