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