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