1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.shape;
14 import javax.vecmath.Matrix4d;
15 import javax.vecmath.Point3d;
17 import org.simantics.g3d.math.MathTools;
18 import org.simantics.utils.threads.AWTThread;
19 import org.simantics.utils.threads.ThreadUtils;
22 import vtk.vtkAssembly;
24 import vtk.vtkConeSource;
25 import vtk.vtkLinearTransform;
26 import vtk.vtkMatrix4x4;
27 import vtk.vtkPolyDataMapper;
29 import vtk.vtkRenderer;
30 import vtk.vtkSphereSource;
31 import vtk.vtkTextActor;
32 import vtk.vtkTubeFilter;
34 public class axesSphereActor extends vtkAssembly implements IvtkVisualObject{
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;
46 public axesSphereActor(vtkRenderer _ren) {
52 public axesSphereActor(vtkRenderer _ren, double axisLength) {
55 this.axisLength = axisLength;
59 public void createAxes() {
60 vtkAxes axes = new vtkAxes();
61 axes.SetOrigin(0, 0, 0);
62 axes.SetScaleFactor(axisLength);
66 vtkTubeFilter tube = new vtkTubeFilter();
67 tube.SetInputData(axes.GetOutput());
68 tube.SetRadius(0.05 * axisLength);
69 tube.SetNumberOfSides(8);
71 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
72 tubeMapper.SetInputData(tube.GetOutput());
74 tubeActor = new vtkActor();
75 tubeActor.SetMapper(tubeMapper);
76 tubeActor.PickableOff();
79 double coneScale = 0.3 * axisLength;
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);
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);
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);
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);
131 this.AddPart(tubeActor);
132 this.AddPart(xconeActor);
133 this.AddPart(yconeActor);
134 this.AddPart(zconeActor);
135 this.AddPart(oBallActor);
137 tube.GetOutput().Delete();
138 xcone.GetOutput().Delete();
139 ycone.GetOutput().Delete();
140 zcone.GetOutput().Delete();
141 axes.GetOutput().Delete();
142 ball.GetOutput().Delete();
150 xconeMapper.Delete();
151 yconeMapper.Delete();
152 zconeMapper.Delete();
155 xconeActor.GetProperty().Delete();
156 yconeActor.GetProperty().Delete();
157 zconeActor.GetProperty().Delete();
158 oBallActor.GetProperty().Delete();
161 xactor = new vtkTextActor();
162 yactor = new vtkTextActor();
163 zactor = new vtkTextActor();
165 xactor.SetInput("X");
166 yactor.SetInput("Y");
167 zactor.SetInput("Z");
169 // xactor.SetTextScaleModeToViewport();
170 // yactor.SetTextScaleModeToViewport();
171 // zactor.SetTextScaleModeToViewport();
172 xactor.SetTextScaleModeToNone();
173 yactor.SetTextScaleModeToNone();
174 zactor.SetTextScaleModeToNone();
176 xactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
177 xactor.GetTextProperty().ShadowOff();
178 xactor.GetTextProperty().ItalicOff();
179 xactor.GetTextProperty().BoldOff();
181 yactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
182 yactor.GetTextProperty().ShadowOff();
183 yactor.GetTextProperty().ItalicOff();
184 yactor.GetTextProperty().BoldOff();
186 zactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
187 zactor.GetTextProperty().ShadowOff();
188 zactor.GetTextProperty().ItalicOff();
189 zactor.GetTextProperty().BoldOff();
191 xactor.GetTextProperty().Delete();
192 yactor.GetTextProperty().Delete();
193 zactor.GetTextProperty().Delete();
195 xactor.SetMaximumLineHeight(0.25);
196 yactor.SetMaximumLineHeight(0.25);
197 zactor.SetMaximumLineHeight(0.25);
199 xactor.SetPickable(0);
200 yactor.SetPickable(0);
201 zactor.SetPickable(0);
204 public void addToRenderer() {
209 ren.AddActor2D(xactor);
210 ren.AddActor2D(yactor);
211 ren.AddActor2D(zactor);
217 public void removeFromRenderer() {
221 ren.RemoveActor2D(xactor);
222 ren.RemoveActor2D(yactor);
223 ren.RemoveActor2D(zactor);
224 ren.RemoveActor(this);
227 public boolean isRendered() {
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);
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);
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();
254 private void updateTextLoc() {
257 xactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
258 yactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
259 zactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
262 MathTools.set(m, mat);
263 x.set(axisLength, 0,0);
264 y.set(0,axisLength, 0);
265 z.set(0,0,axisLength);
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);
274 xactor.GetPositionCoordinate().Delete();
275 yactor.GetPositionCoordinate().Delete();
276 zactor.GetPositionCoordinate().Delete();
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);
291 public void SetOrientation(double id0, double id1, double id2) {
292 super.SetOrientation(id0, id1, id2);
297 public void RotateWXYZ(double id0, double id1, double id2, double id3) {
298 super.RotateWXYZ(id0, id1, id2, id3);
303 public void SetPosition(double[] id0) {
304 super.SetPosition(id0);
309 public void SetPosition(double id0, double id1, double id2) {
310 super.SetPosition(id0, id1, id2);
315 public void SetOrientation(double[] id0) {
316 super.SetOrientation(id0);
321 public void SetScale(double id0) {
327 public void SetScale(double id0, double id1, double id2) {
328 super.SetScale(id0, id1, id2);
333 public void SetScale(double[] id0) {
339 public void SetUserMatrix(vtkMatrix4x4 id0) {
340 super.SetUserMatrix(id0);
345 public void SetUserTransform(vtkLinearTransform id0) {
346 super.SetUserTransform(id0);
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);
369 public void dispose() {
370 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
374 removeFromRenderer();
381 public vtkProp3D getVtkProp() {