]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/axisActor.java
vtk 8.2.0 API changes
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / shape / axisActor.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.AxisAngle4d;
15 import javax.vecmath.Matrix4d;
16 import javax.vecmath.Point3d;
17 import javax.vecmath.Vector3d;
18
19 import org.simantics.g3d.math.MathTools;
20 import org.simantics.utils.threads.AWTThread;
21 import org.simantics.utils.threads.ThreadUtils;
22
23 import vtk.vtkActor;
24 import vtk.vtkAssembly;
25 import vtk.vtkConeSource;
26 import vtk.vtkLineSource;
27 import vtk.vtkLinearTransform;
28 import vtk.vtkMatrix4x4;
29 import vtk.vtkPolyDataMapper;
30 import vtk.vtkProp3D;
31 import vtk.vtkRenderer;
32 import vtk.vtkTextActor;
33 import vtk.vtkTubeFilter;
34
35 public class axisActor extends vtkAssembly implements IvtkVisualObject{
36
37         private vtkRenderer ren;
38         private Vector3d axisDir = new Vector3d(1,0,0);
39         private vtkTextActor tactor;
40         private vtkActor tubeActor;
41         private vtkActor coneActor;
42         private boolean rendered = false;
43
44         public axisActor(vtkRenderer _ren, String label) {
45                 super();
46                 ren = _ren;
47                 createAxis(label);
48         }
49         
50         public axisActor(vtkRenderer _ren, String label, Vector3d dir) {
51                 super();
52                 ren = _ren;
53                 this.axisDir = dir;
54                 createAxis(label);
55         }
56
57         public void createAxis(String label) {
58                 vtkLineSource line = new vtkLineSource();
59                 line.SetPoint1(0.0,0.0,0.0);
60                 line.SetPoint2(axisDir.x,axisDir.y,axisDir.z);
61                 
62                 tactor = new vtkTextActor();
63                 
64                 tactor.SetInput(label);
65                 
66                 tactor.SetTextScaleModeToNone();
67                 tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
68                 tactor.GetTextProperty().ShadowOff();
69                 tactor.GetTextProperty().ItalicOff();
70                 tactor.GetTextProperty().BoldOff();
71                 
72                 tactor.GetTextProperty().Delete();
73                 
74                 tactor.SetMaximumLineHeight(0.25);
75                 
76                 tactor.SetPickable(0);
77                 
78                 vtkTubeFilter tube = new vtkTubeFilter();
79                 tube.SetInputData(line.GetOutput());
80                 tube.SetRadius(0.05 * axisDir.length());
81                 tube.SetNumberOfSides(8);
82
83                 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
84                 tubeMapper.SetInputData(tube.GetOutput());
85
86                 tubeActor = new vtkActor();
87                 tubeActor.SetMapper(tubeMapper);
88                 tubeActor.PickableOff();
89
90                 int coneRes = 12;
91                 double coneScale = 0.3 * axisDir.length();
92
93                 // --- x-Cone
94                 vtkConeSource cone = new vtkConeSource();
95                 cone.SetResolution(coneRes);
96                 vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
97                 coneMapper.SetInputData(cone.GetOutput());
98                 coneActor = new vtkActor();
99                 coneActor.SetMapper(coneMapper);
100                 coneActor.GetProperty().SetColor(1, 0, 0);
101                 coneActor.SetScale(coneScale, coneScale, coneScale);
102                 coneActor.SetPosition(axisDir.x,axisDir.y,axisDir.z);
103                 coneActor.SetPickable(0);
104                 
105                 AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), new Vector3d(axisDir));
106                 if (aa != null)
107                         coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
108                 
109                 this.AddPart(tubeActor);
110                 this.AddPart(coneActor);
111                 
112                 tube.GetOutput().Delete();
113                 cone.GetOutput().Delete();
114                 line.GetOutput().Delete();
115                 
116                 tubeMapper.Delete();
117                 tube.Delete();
118                 cone.Delete();
119                 line.Delete();
120
121                 coneMapper.Delete();
122                 
123                 coneActor.GetProperty().Delete();
124         }
125         
126         public void addToRenderer() {
127                 if (rendered)
128                         return;
129                 rendered = true;
130                 
131                 ren.AddActor2D(tactor);
132                 
133                 ren.AddActor(this);
134         }
135         
136         public void removeFromRenderer() {
137                 if (!rendered)
138                         return;
139                 rendered = false;
140                 ren.RemoveActor2D(tactor);
141                 ren.RemoveActor(this);
142         }
143         
144         public boolean isRendered() {
145                 return rendered;
146         }
147
148         public void setAxesVisibility(boolean ison) {
149                 this.SetVisibility(ison ? 1 : 0);
150                 tactor.SetVisibility(ison ? 1 : 0);
151         }
152         
153         public void setLabelVisibility(boolean ison) {
154                 tactor.SetVisibility(ison ? 1 : 0);
155         }
156         
157         double mat[] = new double[16];
158         Matrix4d m = new Matrix4d();
159         Point3d p = new Point3d();
160         private void updateTextLoc() {
161                 tactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
162                 
163                 
164                 GetMatrix(mat);
165                 MathTools.set(m, mat);
166                 p.set(axisDir.x, axisDir.y,axisDir.z);
167                 m.transform(p);
168                 tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z);
169                 
170                 tactor.GetPositionCoordinate().Delete();
171         }
172         
173         @Override
174         public void SetPickable(int id0) {
175                 super.SetPickable(id0);
176                 tubeActor.SetPickable(id0);
177                 coneActor.SetPickable(id0);
178         }
179         
180         @Override
181         public void SetOrientation(double id0, double id1, double id2) {
182                 super.SetOrientation(id0, id1, id2);
183                 updateTextLoc();
184         }
185         
186         @Override
187         public void RotateWXYZ(double id0, double id1, double id2, double id3) {
188                 super.RotateWXYZ(id0, id1, id2, id3);
189                 updateTextLoc();
190         }
191         
192         @Override
193         public void SetPosition(double[] id0) {
194                 super.SetPosition(id0);
195                 updateTextLoc();
196         }
197         
198         @Override
199         public void SetPosition(double id0, double id1, double id2) {
200                 super.SetPosition(id0, id1, id2);
201                 updateTextLoc();
202         }
203         
204         @Override
205         public void SetOrientation(double[] id0) {
206                 super.SetOrientation(id0);
207                 updateTextLoc();
208         }
209         
210         @Override
211         public void SetScale(double id0) {
212                 super.SetScale(id0);
213                 updateTextLoc();
214         }
215         
216         @Override
217         public void SetScale(double id0, double id1, double id2) {
218                 super.SetScale(id0, id1, id2);
219                 updateTextLoc();
220         }
221         
222         @Override
223         public void SetScale(double[] id0) {
224                 super.SetScale(id0);
225                 updateTextLoc();
226         }
227         
228         public void SetColor(double r, double g, double b) {
229                 coneActor.GetProperty().SetColor(r, g, b);
230                 tubeActor.GetProperty().SetColor(r, g, b);
231                 coneActor.GetProperty().Delete();
232                 tubeActor.GetProperty().Delete();
233         }
234         
235         public void SetTextColor(double r, double g, double b) {
236                 tactor.GetTextProperty().SetColor(r, g, b);
237                 tactor.GetTextProperty().Delete();
238         }
239         
240         @Override
241         public void SetUserMatrix(vtkMatrix4x4 id0) {
242                 super.SetUserMatrix(id0);
243                 updateTextLoc();
244         }
245         
246         @Override
247         public void SetUserTransform(vtkLinearTransform id0) {
248                 super.SetUserTransform(id0);
249                 updateTextLoc();
250         }
251         
252         @Override
253         public void Delete() {
254                 ren.RemoveActor(tactor);
255                 ren.RemoveActor(tubeActor);
256                 ren.RemoveActor(coneActor);
257                 tactor.Delete();
258                 tubeActor.Delete();
259                 coneActor.Delete();
260                 super.Delete();
261         }
262
263         public void dispose() {
264                 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
265                         
266                         @Override
267                         public void run() {
268                                 removeFromRenderer();
269                                 Delete();
270                         }
271                 });
272         }
273         
274         @Override
275         public vtkProp3D getVtkProp() {
276                 return this;
277         }
278         
279 }