]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axesSphereActor.java
Minor improvement for Plant3DEditor extensibility
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / axesSphereActor.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.shape;
13
14 import javax.vecmath.Matrix4d;
15 import javax.vecmath.Point3d;
16
17 import org.simantics.g3d.math.MathTools;
18 import org.simantics.utils.threads.AWTThread;
19 import org.simantics.utils.threads.ThreadUtils;
20
21 import vtk.vtkActor;
22 import vtk.vtkAssembly;
23 import vtk.vtkAxes;
24 import vtk.vtkConeSource;
25 import vtk.vtkLinearTransform;
26 import vtk.vtkMatrix4x4;
27 import vtk.vtkPolyDataMapper;
28 import vtk.vtkProp3D;
29 import vtk.vtkRenderer;
30 import vtk.vtkSphereSource;
31 import vtk.vtkTextActor;
32 import vtk.vtkTubeFilter;
33
34 public class axesSphereActor extends vtkAssembly implements IvtkVisualObject{
35
36         private vtkRenderer ren;
37         private double axisLength = 0.8;
38         private vtkTextActor xactor, yactor, zactor;
39         private vtkActor tubeActor;
40         private vtkActor xconeActor;
41         private vtkActor yconeActor;
42         private vtkActor zconeActor;
43         private vtkActor oBallActor;
44         private boolean rendered = false;
45
46         public axesSphereActor(vtkRenderer _ren) {
47                 super();
48                 ren = _ren;
49                 createAxes();
50         }
51         
52         public axesSphereActor(vtkRenderer _ren, double axisLength) {
53                 super();
54                 ren = _ren;
55                 this.axisLength = axisLength;
56                 createAxes();
57         }
58
59         public void createAxes() {
60                 vtkAxes axes = new vtkAxes();
61                 axes.SetOrigin(0, 0, 0);
62                 axes.SetScaleFactor(axisLength);
63
64
65
66                 vtkTubeFilter tube = new vtkTubeFilter();
67                 tube.SetInputData(axes.GetOutput());
68                 tube.SetRadius(0.05 * axisLength);
69                 tube.SetNumberOfSides(8);
70
71                 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
72                 tubeMapper.SetInputData(tube.GetOutput());
73
74                 tubeActor = new vtkActor();
75                 tubeActor.SetMapper(tubeMapper);
76                 tubeActor.PickableOff();
77
78                 int coneRes = 12;
79                 double coneScale = 0.3 * axisLength;
80
81                 // --- x-Cone
82                 vtkConeSource xcone = new vtkConeSource();
83                 xcone.SetResolution(coneRes);
84                 vtkPolyDataMapper xconeMapper = new vtkPolyDataMapper();
85                 xconeMapper.SetInputData(xcone.GetOutput());
86                 xconeActor = new vtkActor();
87                 xconeActor.SetMapper(xconeMapper);
88                 xconeActor.GetProperty().SetColor(1, 0, 0);
89                 xconeActor.SetScale(coneScale, coneScale, coneScale);
90                 xconeActor.SetPosition(axisLength, 0.0, 0.0);
91                 xconeActor.SetPickable(0);
92
93                 // --- y-Cone
94                 vtkConeSource ycone = new vtkConeSource();
95                 ycone.SetResolution(coneRes);
96                 vtkPolyDataMapper yconeMapper = new vtkPolyDataMapper();
97                 yconeMapper.SetInputData(ycone.GetOutput());
98                 yconeActor = new vtkActor();
99                 yconeActor.SetMapper(yconeMapper);
100                 yconeActor.GetProperty().SetColor(1, 1, 0);
101                 yconeActor.RotateZ(90);
102                 yconeActor.SetScale(coneScale, coneScale, coneScale);
103                 yconeActor.SetPosition(0.0, axisLength, 0.0);
104                 yconeActor.SetPickable(0);
105
106                 // --- z-Cone
107                 vtkConeSource zcone = new vtkConeSource();
108                 zcone.SetResolution(coneRes);
109                 vtkPolyDataMapper zconeMapper = new vtkPolyDataMapper();
110                 zconeMapper.SetInputData(zcone.GetOutput());
111                 zconeActor = new vtkActor();
112                 zconeActor.SetMapper(zconeMapper);
113                 zconeActor.GetProperty().SetColor(0, 1, 0);
114                 zconeActor.RotateY(-90);
115                 zconeActor.SetScale(coneScale, coneScale, coneScale);
116                 zconeActor.SetPosition(0.0, 0.0, axisLength);
117                 zconeActor.SetPickable(0);
118
119                 vtkSphereSource ball = new vtkSphereSource();
120                 ball.SetRadius(axisLength * 0.3);
121                 ball.SetPhiResolution(6);
122                 ball.SetThetaResolution(8);
123                 vtkPolyDataMapper ballMapper = new vtkPolyDataMapper();
124                 ballMapper.SetInputData(ball.GetOutput());
125                 oBallActor = new vtkActor();
126                 oBallActor.SetMapper(ballMapper);
127                 oBallActor.GetProperty().SetColor(0, 0, 1);
128                 oBallActor.SetPickable(0);
129                 
130                 
131                 this.AddPart(tubeActor);
132                 this.AddPart(xconeActor);
133                 this.AddPart(yconeActor);
134                 this.AddPart(zconeActor);
135                 this.AddPart(oBallActor);
136                 
137                 tube.GetOutput().Delete();
138                 xcone.GetOutput().Delete();
139                 ycone.GetOutput().Delete();
140                 zcone.GetOutput().Delete();
141                 axes.GetOutput().Delete();
142                 ball.GetOutput().Delete();
143                 axes.Delete();
144                 tube.Delete();
145                 xcone.Delete();
146                 ycone.Delete();
147                 zcone.Delete();
148                 ball.Delete();
149                 tubeMapper.Delete();
150                 xconeMapper.Delete();
151                 yconeMapper.Delete();
152                 zconeMapper.Delete();
153                 ballMapper.Delete();
154                 
155                 xconeActor.GetProperty().Delete();
156                 yconeActor.GetProperty().Delete();
157                 zconeActor.GetProperty().Delete();
158                 oBallActor.GetProperty().Delete();
159                 
160                 
161                 xactor = new vtkTextActor();
162                 yactor = new vtkTextActor();
163                 zactor = new vtkTextActor();
164
165                 xactor.SetInput("X");
166                 yactor.SetInput("Y");
167                 zactor.SetInput("Z");
168
169 //              xactor.SetTextScaleModeToViewport();
170 //              yactor.SetTextScaleModeToViewport();
171 //              zactor.SetTextScaleModeToViewport();
172                 xactor.SetTextScaleModeToNone();
173                 yactor.SetTextScaleModeToNone();
174                 zactor.SetTextScaleModeToNone();
175                 
176                 xactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
177                 xactor.GetTextProperty().ShadowOff();
178                 xactor.GetTextProperty().ItalicOff();
179                 xactor.GetTextProperty().BoldOff();
180
181                 yactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
182                 yactor.GetTextProperty().ShadowOff();
183                 yactor.GetTextProperty().ItalicOff();
184                 yactor.GetTextProperty().BoldOff();
185
186                 zactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
187                 zactor.GetTextProperty().ShadowOff();
188                 zactor.GetTextProperty().ItalicOff();
189                 zactor.GetTextProperty().BoldOff();
190                 
191                 xactor.GetTextProperty().Delete();
192                 yactor.GetTextProperty().Delete();
193                 zactor.GetTextProperty().Delete();
194
195                 xactor.SetMaximumLineHeight(0.25);
196                 yactor.SetMaximumLineHeight(0.25);
197                 zactor.SetMaximumLineHeight(0.25);
198                 
199                 xactor.SetPickable(0);
200                 yactor.SetPickable(0);
201                 zactor.SetPickable(0);
202         }
203         
204         public void addToRenderer() {
205                 if (rendered)
206                         return;
207                 rendered = true;
208                 
209                 ren.AddActor2D(xactor);
210                 ren.AddActor2D(yactor);
211                 ren.AddActor2D(zactor);
212                 
213                 
214                 ren.AddActor(this);
215         }
216         
217         public void removeFromRenderer() {
218                 if (!rendered)
219                         return;
220                 rendered = false;
221                 ren.RemoveActor2D(xactor);
222                 ren.RemoveActor2D(yactor);
223                 ren.RemoveActor2D(zactor);
224                 ren.RemoveActor(this);
225         }
226         
227         public boolean isRendered() {
228                 return rendered;
229         }
230
231         public void setAxesVisibility(boolean ison) {
232                 this.SetVisibility(ison ? 1 : 0);
233                 xactor.SetVisibility(ison ? 1 : 0);
234                 yactor.SetVisibility(ison ? 1 : 0);
235                 zactor.SetVisibility(ison ? 1 : 0);
236         }
237         
238         private boolean labelVisible = true;
239         public void setLabelVisibility(boolean ison) {
240                 xactor.SetVisibility(ison ? 1 : 0);
241                 yactor.SetVisibility(ison ? 1 : 0);
242                 zactor.SetVisibility(ison ? 1 : 0);
243                 labelVisible = ison;
244                 if (labelVisible)
245                         updateTextLoc();
246         }
247         
248         Matrix4d m = new Matrix4d();
249         double mat[] = new double[16];
250         Point3d x = new Point3d();
251         Point3d y = new Point3d();
252         Point3d z = new Point3d();
253         
254         private void updateTextLoc() {
255                 if (!labelVisible)
256                         return;
257                 xactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
258                 yactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
259                 zactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
260
261                 GetMatrix(mat);
262                 MathTools.set(m, mat);
263                 x.set(axisLength, 0,0);
264                 y.set(0,axisLength, 0);
265                 z.set(0,0,axisLength);
266                 
267                 m.transform(x);
268                 m.transform(y);
269                 m.transform(z);
270                 xactor.GetPositionCoordinate().SetValue(x.x, x.y, x.z);
271                 yactor.GetPositionCoordinate().SetValue(y.x, y.y, y.z);
272                 zactor.GetPositionCoordinate().SetValue(z.x, z.y, z.z);
273                 
274                 xactor.GetPositionCoordinate().Delete();
275                 yactor.GetPositionCoordinate().Delete();
276                 zactor.GetPositionCoordinate().Delete();
277         }
278         
279         @Override
280         public void SetPickable(int id0) {
281                 super.SetPickable(id0);
282                 tubeActor.SetPickable(id0);
283                 xconeActor.SetPickable(id0);
284                 yconeActor.SetPickable(id0);
285                 zconeActor.SetPickable(id0);
286                 oBallActor.SetPickable(id0);
287
288         }
289         
290         @Override
291         public void SetOrientation(double id0, double id1, double id2) {
292                 super.SetOrientation(id0, id1, id2);
293                 updateTextLoc();
294         }
295         
296         @Override
297         public void RotateWXYZ(double id0, double id1, double id2, double id3) {
298                 super.RotateWXYZ(id0, id1, id2, id3);
299                 updateTextLoc();
300         }
301         
302         @Override
303         public void SetPosition(double[] id0) {
304                 super.SetPosition(id0);
305                 updateTextLoc();
306         }
307         
308         @Override
309         public void SetPosition(double id0, double id1, double id2) {
310                 super.SetPosition(id0, id1, id2);
311                 updateTextLoc();
312         }
313         
314         @Override
315         public void SetOrientation(double[] id0) {
316                 super.SetOrientation(id0);
317                 updateTextLoc();
318         }
319         
320         @Override
321         public void SetScale(double id0) {
322                 super.SetScale(id0);
323                 updateTextLoc();
324         }
325         
326         @Override
327         public void SetScale(double id0, double id1, double id2) {
328                 super.SetScale(id0, id1, id2);
329                 updateTextLoc();
330         }
331         
332         @Override
333         public void SetScale(double[] id0) {
334                 super.SetScale(id0);
335                 updateTextLoc();
336         }
337         
338         @Override
339         public void SetUserMatrix(vtkMatrix4x4 id0) {
340                 super.SetUserMatrix(id0);
341                 updateTextLoc();
342         }
343         
344         @Override
345         public void SetUserTransform(vtkLinearTransform id0) {
346                 super.SetUserTransform(id0);
347                 updateTextLoc();
348         }
349         
350         @Override
351         public void Delete() {
352                 ren.RemoveActor(xactor);
353                 ren.RemoveActor(yactor);
354                 ren.RemoveActor(zactor);
355                 ren.RemoveActor(tubeActor);
356                 ren.RemoveActor(xconeActor);
357                 ren.RemoveActor(yconeActor);
358                 ren.RemoveActor(xconeActor);
359                 xactor.Delete();
360                 yactor.Delete();
361                 zactor.Delete();
362                 tubeActor.Delete();
363                 xconeActor.Delete();
364                 yconeActor.Delete();
365                 zconeActor.Delete();
366                 super.Delete();
367         }
368
369         public void dispose() {
370                 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
371                         
372                         @Override
373                         public void run() {
374                                 removeFromRenderer();
375                                 Delete();
376                         }
377                 });
378         }
379         
380         @Override
381         public vtkProp3D getVtkProp() {
382                 return this;
383         }
384         
385 }