1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.vtk.shape;
\r
14 import javax.vecmath.Matrix4d;
\r
15 import javax.vecmath.Point3d;
\r
17 import org.simantics.g3d.math.MathTools;
\r
18 import org.simantics.utils.threads.AWTThread;
\r
19 import org.simantics.utils.threads.ThreadUtils;
\r
21 import vtk.vtkActor;
\r
22 import vtk.vtkAssembly;
\r
24 import vtk.vtkConeSource;
\r
25 import vtk.vtkLinearTransform;
\r
26 import vtk.vtkMatrix4x4;
\r
27 import vtk.vtkPolyDataMapper;
\r
28 import vtk.vtkProp3D;
\r
29 import vtk.vtkRenderer;
\r
30 import vtk.vtkSphereSource;
\r
31 import vtk.vtkTextActor;
\r
32 import vtk.vtkTubeFilter;
\r
34 public class axesSphereActor extends vtkAssembly implements IvtkVisualObject{
\r
36 private vtkRenderer ren;
\r
37 private double axisLength = 0.8;
\r
38 private vtkTextActor xactor, yactor, zactor;
\r
39 private vtkActor tubeActor;
\r
40 private vtkActor xconeActor;
\r
41 private vtkActor yconeActor;
\r
42 private vtkActor zconeActor;
\r
43 private vtkActor oBallActor;
\r
44 private boolean rendered = false;
\r
46 public axesSphereActor(vtkRenderer _ren) {
\r
52 public axesSphereActor(vtkRenderer _ren, double axisLength) {
\r
55 this.axisLength = axisLength;
\r
59 public void createAxes() {
\r
60 vtkAxes axes = new vtkAxes();
\r
61 axes.SetOrigin(0, 0, 0);
\r
62 axes.SetScaleFactor(axisLength);
\r
66 vtkTubeFilter tube = new vtkTubeFilter();
\r
67 tube.SetInput(axes.GetOutput());
\r
68 tube.SetRadius(0.05 * axisLength);
\r
69 tube.SetNumberOfSides(8);
\r
71 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
\r
72 tubeMapper.SetInput(tube.GetOutput());
\r
74 tubeActor = new vtkActor();
\r
75 tubeActor.SetMapper(tubeMapper);
\r
76 tubeActor.PickableOff();
\r
79 double coneScale = 0.3 * axisLength;
\r
82 vtkConeSource xcone = new vtkConeSource();
\r
83 xcone.SetResolution(coneRes);
\r
84 vtkPolyDataMapper xconeMapper = new vtkPolyDataMapper();
\r
85 xconeMapper.SetInput(xcone.GetOutput());
\r
86 xconeActor = new vtkActor();
\r
87 xconeActor.SetMapper(xconeMapper);
\r
88 xconeActor.GetProperty().SetColor(1, 0, 0);
\r
89 xconeActor.SetScale(coneScale, coneScale, coneScale);
\r
90 xconeActor.SetPosition(axisLength, 0.0, 0.0);
\r
91 xconeActor.SetPickable(0);
\r
94 vtkConeSource ycone = new vtkConeSource();
\r
95 ycone.SetResolution(coneRes);
\r
96 vtkPolyDataMapper yconeMapper = new vtkPolyDataMapper();
\r
97 yconeMapper.SetInput(ycone.GetOutput());
\r
98 yconeActor = new vtkActor();
\r
99 yconeActor.SetMapper(yconeMapper);
\r
100 yconeActor.GetProperty().SetColor(1, 1, 0);
\r
101 yconeActor.RotateZ(90);
\r
102 yconeActor.SetScale(coneScale, coneScale, coneScale);
\r
103 yconeActor.SetPosition(0.0, axisLength, 0.0);
\r
104 yconeActor.SetPickable(0);
\r
107 vtkConeSource zcone = new vtkConeSource();
\r
108 zcone.SetResolution(coneRes);
\r
109 vtkPolyDataMapper zconeMapper = new vtkPolyDataMapper();
\r
110 zconeMapper.SetInput(zcone.GetOutput());
\r
111 zconeActor = new vtkActor();
\r
112 zconeActor.SetMapper(zconeMapper);
\r
113 zconeActor.GetProperty().SetColor(0, 1, 0);
\r
114 zconeActor.RotateY(-90);
\r
115 zconeActor.SetScale(coneScale, coneScale, coneScale);
\r
116 zconeActor.SetPosition(0.0, 0.0, axisLength);
\r
117 zconeActor.SetPickable(0);
\r
119 vtkSphereSource ball = new vtkSphereSource();
\r
120 ball.SetRadius(axisLength * 0.3);
\r
121 ball.SetPhiResolution(6);
\r
122 ball.SetThetaResolution(8);
\r
123 vtkPolyDataMapper ballMapper = new vtkPolyDataMapper();
\r
124 ballMapper.SetInput(ball.GetOutput());
\r
125 oBallActor = new vtkActor();
\r
126 oBallActor.SetMapper(ballMapper);
\r
127 oBallActor.GetProperty().SetColor(0, 0, 1);
\r
128 oBallActor.SetPickable(0);
\r
131 this.AddPart(tubeActor);
\r
132 this.AddPart(xconeActor);
\r
133 this.AddPart(yconeActor);
\r
134 this.AddPart(zconeActor);
\r
135 this.AddPart(oBallActor);
\r
137 tube.GetOutput().Delete();
\r
138 xcone.GetOutput().Delete();
\r
139 ycone.GetOutput().Delete();
\r
140 zcone.GetOutput().Delete();
\r
141 axes.GetOutput().Delete();
\r
142 ball.GetOutput().Delete();
\r
149 tubeMapper.Delete();
\r
150 xconeMapper.Delete();
\r
151 yconeMapper.Delete();
\r
152 zconeMapper.Delete();
\r
153 ballMapper.Delete();
\r
155 xconeActor.GetProperty().Delete();
\r
156 yconeActor.GetProperty().Delete();
\r
157 zconeActor.GetProperty().Delete();
\r
158 oBallActor.GetProperty().Delete();
\r
161 xactor = new vtkTextActor();
\r
162 yactor = new vtkTextActor();
\r
163 zactor = new vtkTextActor();
\r
165 xactor.SetInput("X");
\r
166 yactor.SetInput("Y");
\r
167 zactor.SetInput("Z");
\r
169 // xactor.SetTextScaleModeToViewport();
\r
170 // yactor.SetTextScaleModeToViewport();
\r
171 // zactor.SetTextScaleModeToViewport();
\r
172 xactor.SetTextScaleModeToNone();
\r
173 yactor.SetTextScaleModeToNone();
\r
174 zactor.SetTextScaleModeToNone();
\r
176 xactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
\r
177 xactor.GetTextProperty().ShadowOff();
\r
178 xactor.GetTextProperty().ItalicOff();
\r
179 xactor.GetTextProperty().BoldOff();
\r
181 yactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
\r
182 yactor.GetTextProperty().ShadowOff();
\r
183 yactor.GetTextProperty().ItalicOff();
\r
184 yactor.GetTextProperty().BoldOff();
\r
186 zactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
\r
187 zactor.GetTextProperty().ShadowOff();
\r
188 zactor.GetTextProperty().ItalicOff();
\r
189 zactor.GetTextProperty().BoldOff();
\r
191 xactor.GetTextProperty().Delete();
\r
192 yactor.GetTextProperty().Delete();
\r
193 zactor.GetTextProperty().Delete();
\r
195 xactor.SetMaximumLineHeight(0.25);
\r
196 yactor.SetMaximumLineHeight(0.25);
\r
197 zactor.SetMaximumLineHeight(0.25);
\r
199 xactor.SetPickable(0);
\r
200 yactor.SetPickable(0);
\r
201 zactor.SetPickable(0);
\r
204 public void addToRenderer() {
\r
209 ren.AddActor2D(xactor);
\r
210 ren.AddActor2D(yactor);
\r
211 ren.AddActor2D(zactor);
\r
214 ren.AddActor(this);
\r
217 public void removeFromRenderer() {
\r
221 ren.RemoveActor2D(xactor);
\r
222 ren.RemoveActor2D(yactor);
\r
223 ren.RemoveActor2D(zactor);
\r
224 ren.RemoveActor(this);
\r
227 public boolean isRendered() {
\r
231 public void setAxesVisibility(boolean ison) {
\r
232 this.SetVisibility(ison ? 1 : 0);
\r
233 xactor.SetVisibility(ison ? 1 : 0);
\r
234 yactor.SetVisibility(ison ? 1 : 0);
\r
235 zactor.SetVisibility(ison ? 1 : 0);
\r
238 private boolean labelVisible = true;
\r
239 public void setLabelVisibility(boolean ison) {
\r
240 xactor.SetVisibility(ison ? 1 : 0);
\r
241 yactor.SetVisibility(ison ? 1 : 0);
\r
242 zactor.SetVisibility(ison ? 1 : 0);
\r
243 labelVisible = ison;
\r
248 Matrix4d m = new Matrix4d();
\r
249 double mat[] = new double[16];
\r
250 Point3d x = new Point3d();
\r
251 Point3d y = new Point3d();
\r
252 Point3d z = new Point3d();
\r
254 private void updateTextLoc() {
\r
257 xactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
258 yactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
259 zactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
262 MathTools.set(m, mat);
\r
263 x.set(axisLength, 0,0);
\r
264 y.set(0,axisLength, 0);
\r
265 z.set(0,0,axisLength);
\r
270 xactor.GetPositionCoordinate().SetValue(x.x, x.y, x.z);
\r
271 yactor.GetPositionCoordinate().SetValue(y.x, y.y, y.z);
\r
272 zactor.GetPositionCoordinate().SetValue(z.x, z.y, z.z);
\r
274 xactor.GetPositionCoordinate().Delete();
\r
275 yactor.GetPositionCoordinate().Delete();
\r
276 zactor.GetPositionCoordinate().Delete();
\r
280 public void SetPickable(int id0) {
\r
281 super.SetPickable(id0);
\r
282 tubeActor.SetPickable(id0);
\r
283 xconeActor.SetPickable(id0);
\r
284 yconeActor.SetPickable(id0);
\r
285 zconeActor.SetPickable(id0);
\r
286 oBallActor.SetPickable(id0);
\r
291 public void SetOrientation(double id0, double id1, double id2) {
\r
292 super.SetOrientation(id0, id1, id2);
\r
297 public void RotateWXYZ(double id0, double id1, double id2, double id3) {
\r
298 super.RotateWXYZ(id0, id1, id2, id3);
\r
303 public void SetPosition(double[] id0) {
\r
304 super.SetPosition(id0);
\r
309 public void SetPosition(double id0, double id1, double id2) {
\r
310 super.SetPosition(id0, id1, id2);
\r
315 public void SetOrientation(double[] id0) {
\r
316 super.SetOrientation(id0);
\r
321 public void SetScale(double id0) {
\r
322 super.SetScale(id0);
\r
327 public void SetScale(double id0, double id1, double id2) {
\r
328 super.SetScale(id0, id1, id2);
\r
333 public void SetScale(double[] id0) {
\r
334 super.SetScale(id0);
\r
339 public void SetUserMatrix(vtkMatrix4x4 id0) {
\r
340 super.SetUserMatrix(id0);
\r
345 public void SetUserTransform(vtkLinearTransform id0) {
\r
346 super.SetUserTransform(id0);
\r
351 public void Delete() {
\r
352 ren.RemoveActor(xactor);
\r
353 ren.RemoveActor(yactor);
\r
354 ren.RemoveActor(zactor);
\r
355 ren.RemoveActor(tubeActor);
\r
356 ren.RemoveActor(xconeActor);
\r
357 ren.RemoveActor(yconeActor);
\r
358 ren.RemoveActor(xconeActor);
\r
362 tubeActor.Delete();
\r
363 xconeActor.Delete();
\r
364 yconeActor.Delete();
\r
365 zconeActor.Delete();
\r
369 public void dispose() {
\r
370 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
\r
373 public void run() {
\r
374 removeFromRenderer();
\r
381 public vtkProp3D getVtkProp() {
\r