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.AxisAngle4d;
15 import javax.vecmath.Matrix4d;
16 import javax.vecmath.Point3d;
17 import javax.vecmath.Vector3d;
19 import org.simantics.g3d.math.MathTools;
20 import org.simantics.utils.threads.AWTThread;
21 import org.simantics.utils.threads.ThreadUtils;
24 import vtk.vtkAssembly;
25 import vtk.vtkConeSource;
26 import vtk.vtkLineSource;
27 import vtk.vtkLinearTransform;
28 import vtk.vtkMatrix4x4;
29 import vtk.vtkPolyDataMapper;
31 import vtk.vtkRenderer;
32 import vtk.vtkTextActor;
33 import vtk.vtkTubeFilter;
35 public class axisActor extends vtkAssembly implements IvtkVisualObject{
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;
44 public axisActor(vtkRenderer _ren, String label) {
50 public axisActor(vtkRenderer _ren, String label, Vector3d dir) {
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);
62 tactor = new vtkTextActor();
64 tactor.SetInput(label);
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();
72 tactor.GetTextProperty().Delete();
74 tactor.SetMaximumLineHeight(0.25);
76 tactor.SetPickable(0);
78 vtkTubeFilter tube = new vtkTubeFilter();
79 tube.SetInputData(line.GetOutput());
80 tube.SetRadius(0.05 * axisDir.length());
81 tube.SetNumberOfSides(8);
83 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
84 tubeMapper.SetInputData(tube.GetOutput());
86 tubeActor = new vtkActor();
87 tubeActor.SetMapper(tubeMapper);
88 tubeActor.PickableOff();
91 double coneScale = 0.3 * axisDir.length();
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);
105 AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), new Vector3d(axisDir));
107 coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
109 this.AddPart(tubeActor);
110 this.AddPart(coneActor);
112 tube.GetOutput().Delete();
113 cone.GetOutput().Delete();
114 line.GetOutput().Delete();
123 coneActor.GetProperty().Delete();
126 public void addToRenderer() {
131 ren.AddActor2D(tactor);
136 public void removeFromRenderer() {
140 ren.RemoveActor2D(tactor);
141 ren.RemoveActor(this);
144 public boolean isRendered() {
148 public void setAxesVisibility(boolean ison) {
149 this.SetVisibility(ison ? 1 : 0);
150 tactor.SetVisibility(ison ? 1 : 0);
153 public void setLabelVisibility(boolean ison) {
154 tactor.SetVisibility(ison ? 1 : 0);
157 double mat[] = new double[16];
158 Matrix4d m = new Matrix4d();
159 Point3d p = new Point3d();
160 private void updateTextLoc() {
161 tactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
165 MathTools.set(m, mat);
166 p.set(axisDir.x, axisDir.y,axisDir.z);
168 tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z);
170 tactor.GetPositionCoordinate().Delete();
174 public void SetPickable(int id0) {
175 super.SetPickable(id0);
176 tubeActor.SetPickable(id0);
177 coneActor.SetPickable(id0);
181 public void SetOrientation(double id0, double id1, double id2) {
182 super.SetOrientation(id0, id1, id2);
187 public void RotateWXYZ(double id0, double id1, double id2, double id3) {
188 super.RotateWXYZ(id0, id1, id2, id3);
193 public void SetPosition(double[] id0) {
194 super.SetPosition(id0);
199 public void SetPosition(double id0, double id1, double id2) {
200 super.SetPosition(id0, id1, id2);
205 public void SetOrientation(double[] id0) {
206 super.SetOrientation(id0);
211 public void SetScale(double id0) {
217 public void SetScale(double id0, double id1, double id2) {
218 super.SetScale(id0, id1, id2);
223 public void SetScale(double[] id0) {
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();
235 public void SetTextColor(double r, double g, double b) {
236 tactor.GetTextProperty().SetColor(r, g, b);
237 tactor.GetTextProperty().Delete();
241 public void SetUserMatrix(vtkMatrix4x4 id0) {
242 super.SetUserMatrix(id0);
247 public void SetUserTransform(vtkLinearTransform id0) {
248 super.SetUserTransform(id0);
253 public void Delete() {
254 ren.RemoveActor(tactor);
255 ren.RemoveActor(tubeActor);
256 ren.RemoveActor(coneActor);
263 public void dispose() {
264 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
268 removeFromRenderer();
275 public vtkProp3D getVtkProp() {