1 package org.simantics.g3d.vtk.shape;
\r
3 import javax.vecmath.Matrix4d;
\r
4 import javax.vecmath.Point3d;
\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
10 import vtk.vtkActor;
\r
11 import vtk.vtkAssembly;
\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
23 public class axesSphereActor extends vtkAssembly implements IvtkVisualObject{
\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
35 public axesSphereActor(vtkRenderer _ren) {
\r
41 public axesSphereActor(vtkRenderer _ren, double axisLength) {
\r
44 this.axisLength = axisLength;
\r
48 public void createAxes() {
\r
49 vtkAxes axes = new vtkAxes();
\r
50 axes.SetOrigin(0, 0, 0);
\r
51 axes.SetScaleFactor(axisLength);
\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
60 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
\r
61 tubeMapper.SetInput(tube.GetOutput());
\r
63 tubeActor = new vtkActor();
\r
64 tubeActor.SetMapper(tubeMapper);
\r
65 tubeActor.PickableOff();
\r
68 double coneScale = 0.3 * axisLength;
\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
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
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
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
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
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
138 tubeMapper.Delete();
\r
139 xconeMapper.Delete();
\r
140 yconeMapper.Delete();
\r
141 zconeMapper.Delete();
\r
142 ballMapper.Delete();
\r
144 xconeActor.GetProperty().Delete();
\r
145 yconeActor.GetProperty().Delete();
\r
146 zconeActor.GetProperty().Delete();
\r
147 oBallActor.GetProperty().Delete();
\r
150 xactor = new vtkTextActor();
\r
151 yactor = new vtkTextActor();
\r
152 zactor = new vtkTextActor();
\r
154 xactor.SetInput("X");
\r
155 yactor.SetInput("Y");
\r
156 zactor.SetInput("Z");
\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
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
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
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
180 xactor.GetTextProperty().Delete();
\r
181 yactor.GetTextProperty().Delete();
\r
182 zactor.GetTextProperty().Delete();
\r
184 xactor.SetMaximumLineHeight(0.25);
\r
185 yactor.SetMaximumLineHeight(0.25);
\r
186 zactor.SetMaximumLineHeight(0.25);
\r
188 xactor.SetPickable(0);
\r
189 yactor.SetPickable(0);
\r
190 zactor.SetPickable(0);
\r
193 public void addToRenderer() {
\r
198 ren.AddActor2D(xactor);
\r
199 ren.AddActor2D(yactor);
\r
200 ren.AddActor2D(zactor);
\r
203 ren.AddActor(this);
\r
206 public void removeFromRenderer() {
\r
210 ren.RemoveActor2D(xactor);
\r
211 ren.RemoveActor2D(yactor);
\r
212 ren.RemoveActor2D(zactor);
\r
213 ren.RemoveActor(this);
\r
216 public boolean isRendered() {
\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
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
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
243 private void updateTextLoc() {
\r
246 xactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
247 yactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
248 zactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\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
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
263 xactor.GetPositionCoordinate().Delete();
\r
264 yactor.GetPositionCoordinate().Delete();
\r
265 zactor.GetPositionCoordinate().Delete();
\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
280 public void SetOrientation(double id0, double id1, double id2) {
\r
281 super.SetOrientation(id0, id1, id2);
\r
286 public void RotateWXYZ(double id0, double id1, double id2, double id3) {
\r
287 super.RotateWXYZ(id0, id1, id2, id3);
\r
292 public void SetPosition(double[] id0) {
\r
293 super.SetPosition(id0);
\r
298 public void SetPosition(double id0, double id1, double id2) {
\r
299 super.SetPosition(id0, id1, id2);
\r
304 public void SetOrientation(double[] id0) {
\r
305 super.SetOrientation(id0);
\r
310 public void SetScale(double id0) {
\r
311 super.SetScale(id0);
\r
316 public void SetScale(double id0, double id1, double id2) {
\r
317 super.SetScale(id0, id1, id2);
\r
322 public void SetScale(double[] id0) {
\r
323 super.SetScale(id0);
\r
328 public void SetUserMatrix(vtkMatrix4x4 id0) {
\r
329 super.SetUserMatrix(id0);
\r
334 public void SetUserTransform(vtkLinearTransform id0) {
\r
335 super.SetUserTransform(id0);
\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
351 tubeActor.Delete();
\r
352 xconeActor.Delete();
\r
353 yconeActor.Delete();
\r
354 zconeActor.Delete();
\r
358 public void dispose() {
\r
359 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
\r
362 public void run() {
\r
363 removeFromRenderer();
\r
370 public vtkProp3D getVtkProp() {
\r