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.AxisAngle4d;
\r
15 import javax.vecmath.Matrix4d;
\r
16 import javax.vecmath.Point3d;
\r
17 import javax.vecmath.Vector3d;
\r
19 import org.simantics.g3d.math.MathTools;
\r
20 import org.simantics.utils.threads.AWTThread;
\r
21 import org.simantics.utils.threads.ThreadUtils;
\r
23 import vtk.vtkActor;
\r
24 import vtk.vtkAssembly;
\r
25 import vtk.vtkConeSource;
\r
26 import vtk.vtkLineSource;
\r
27 import vtk.vtkLinearTransform;
\r
28 import vtk.vtkMatrix4x4;
\r
29 import vtk.vtkPolyDataMapper;
\r
30 import vtk.vtkProp3D;
\r
31 import vtk.vtkRenderer;
\r
32 import vtk.vtkTextActor;
\r
33 import vtk.vtkTubeFilter;
\r
35 public class DualHeadArrowActor extends vtkAssembly implements IvtkVisualObject{
\r
37 private vtkRenderer ren;
\r
38 private Vector3d axisDir = new Vector3d(1,0,0);
\r
39 private vtkTextActor tactor;
\r
40 private vtkActor tubeActor;
\r
41 private vtkActor coneActor;
\r
42 private vtkActor coneActor2;
\r
43 private boolean rendered = false;
\r
45 public DualHeadArrowActor(vtkRenderer _ren, String label) {
\r
51 public DualHeadArrowActor(vtkRenderer _ren, String label, Vector3d dir) {
\r
55 if (dir.lengthSquared() < MathTools.NEAR_ZERO)
\r
56 throw new IllegalArgumentException("Direction vector length must mer larger than zero");
\r
60 public void createAxis(String label) {
\r
61 double coneScale = 0.3 * axisDir.length();
\r
62 Vector3d coneOffset = new Vector3d(axisDir);
\r
63 coneOffset.normalize();
\r
64 coneOffset.scale(coneScale*0.5);
\r
66 vtkLineSource line = new vtkLineSource();
\r
67 //line.SetPoint1(0.0,0.0,0.0);
\r
68 line.SetPoint1(coneOffset.x,coneOffset.y,coneOffset.z);
\r
69 line.SetPoint2(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z);
\r
71 tactor = new vtkTextActor();
\r
73 tactor.SetInput(label);
\r
75 tactor.SetTextScaleModeToNone();
\r
76 tactor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
\r
77 tactor.GetTextProperty().ShadowOff();
\r
78 tactor.GetTextProperty().ItalicOff();
\r
79 tactor.GetTextProperty().BoldOff();
\r
81 tactor.GetTextProperty().Delete();
\r
83 tactor.SetMaximumLineHeight(0.25);
\r
85 tactor.SetPickable(0);
\r
87 vtkTubeFilter tube = new vtkTubeFilter();
\r
88 tube.SetInput(line.GetOutput());
\r
89 tube.SetRadius(0.05 * axisDir.length());
\r
90 tube.SetNumberOfSides(8);
\r
92 vtkPolyDataMapper tubeMapper = new vtkPolyDataMapper();
\r
93 tubeMapper.SetInput(tube.GetOutput());
\r
95 tubeActor = new vtkActor();
\r
96 tubeActor.SetMapper(tubeMapper);
\r
97 tubeActor.PickableOff();
\r
105 vtkConeSource cone = new vtkConeSource();
\r
106 cone.SetResolution(coneRes);
\r
107 vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
\r
108 coneMapper.SetInput(cone.GetOutput());
\r
109 coneActor = new vtkActor();
\r
110 coneActor.SetMapper(coneMapper);
\r
111 coneActor.GetProperty().SetColor(1, 0, 0);
\r
112 coneActor.SetScale(coneScale, coneScale, coneScale);
\r
113 coneActor.SetPosition(axisDir.x-coneOffset.x,axisDir.y-coneOffset.y,axisDir.z-coneOffset.z);
\r
114 coneActor.SetPickable(0);
\r
116 AxisAngle4d aa = MathTools.createRotation(new Vector3d(1,0,0), axisDir);
\r
118 coneActor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
121 coneActor2 = new vtkActor();
\r
122 coneActor2.SetMapper(coneMapper);
\r
123 coneActor2.GetProperty().SetColor(1, 0, 0);
\r
124 coneActor2.SetScale(coneScale, coneScale, coneScale);
\r
125 coneActor2.SetPosition(coneOffset.x,coneOffset.y,coneOffset.z);
\r
126 coneActor2.SetPickable(0);
\r
128 aa = MathTools.createRotation(new Vector3d(-1,0,0), axisDir);
\r
130 coneActor2.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
132 this.AddPart(tubeActor);
\r
133 this.AddPart(coneActor);
\r
134 this.AddPart(coneActor2);
\r
136 tube.GetOutput().Delete();
\r
137 cone.GetOutput().Delete();
\r
138 line.GetOutput().Delete();
\r
140 tubeMapper.Delete();
\r
145 coneMapper.Delete();
\r
147 coneActor.GetProperty().Delete();
\r
148 coneActor2.GetProperty().Delete();
\r
151 public void addToRenderer() {
\r
156 ren.AddActor2D(tactor);
\r
158 ren.AddActor(this);
\r
161 public void removeFromRenderer() {
\r
165 ren.RemoveActor2D(tactor);
\r
166 ren.RemoveActor(this);
\r
169 public boolean isRendered() {
\r
173 public void setAxesVisibility(boolean ison) {
\r
174 this.SetVisibility(ison ? 1 : 0);
\r
175 tactor.SetVisibility(ison ? 1 : 0);
\r
178 public void setLabelVisibility(boolean ison) {
\r
179 tactor.SetVisibility(ison ? 1 : 0);
\r
182 double mat[] = new double[16];
\r
183 Matrix4d m = new Matrix4d();
\r
184 private void updateTextLoc() {
\r
185 tactor.GetPositionCoordinate().SetCoordinateSystemToWorld();
\r
189 MathTools.set(m,mat);
\r
190 Point3d p = new Point3d(axisDir.x*0.5, axisDir.y*0.5,axisDir.z*0.5);
\r
192 tactor.GetPositionCoordinate().SetValue(p.x, p.y, p.z);
\r
194 tactor.GetPositionCoordinate().Delete();
\r
198 public void SetPickable(int id0) {
\r
199 super.SetPickable(id0);
\r
200 tubeActor.SetPickable(id0);
\r
201 coneActor.SetPickable(id0);
\r
202 coneActor2.SetPickable(id0);
\r
206 public void SetOrientation(double id0, double id1, double id2) {
\r
207 super.SetOrientation(id0, id1, id2);
\r
212 public void RotateWXYZ(double id0, double id1, double id2, double id3) {
\r
213 super.RotateWXYZ(id0, id1, id2, id3);
\r
218 public void SetPosition(double[] id0) {
\r
219 super.SetPosition(id0);
\r
224 public void SetPosition(double id0, double id1, double id2) {
\r
225 super.SetPosition(id0, id1, id2);
\r
230 public void SetOrientation(double[] id0) {
\r
231 super.SetOrientation(id0);
\r
236 public void SetScale(double id0) {
\r
237 super.SetScale(id0);
\r
242 public void SetScale(double id0, double id1, double id2) {
\r
243 super.SetScale(id0, id1, id2);
\r
248 public void SetScale(double[] id0) {
\r
249 super.SetScale(id0);
\r
253 public void SetColor(double r, double g, double b) {
\r
254 coneActor.GetProperty().SetColor(r, g, b);
\r
255 coneActor2.GetProperty().SetColor(r, g, b);
\r
256 tubeActor.GetProperty().SetColor(r, g, b);
\r
257 coneActor.GetProperty().Delete();
\r
258 coneActor2.GetProperty().Delete();
\r
259 tubeActor.GetProperty().Delete();
\r
262 public void SetTextColor(double r, double g, double b) {
\r
263 tactor.GetTextProperty().SetColor(r, g, b);
\r
264 tactor.GetTextProperty().Delete();
\r
268 public void SetUserMatrix(vtkMatrix4x4 id0) {
\r
269 super.SetUserMatrix(id0);
\r
274 public void SetUserTransform(vtkLinearTransform id0) {
\r
275 super.SetUserTransform(id0);
\r
280 public void Delete() {
\r
281 ren.RemoveActor(tactor);
\r
282 ren.RemoveActor(tubeActor);
\r
283 ren.RemoveActor(coneActor);
\r
284 ren.RemoveActor(coneActor2);
\r
286 tubeActor.Delete();
\r
287 coneActor.Delete();
\r
288 coneActor2.Delete();
\r
292 public void dispose() {
\r
293 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
\r
296 public void run() {
\r
297 removeFromRenderer();
\r
304 public vtkProp3D getVtkProp() {
\r