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.utils;
14 import java.util.Collection;
16 import javax.vecmath.AxisAngle4d;
17 import javax.vecmath.Matrix4d;
18 import javax.vecmath.Point2d;
19 import javax.vecmath.Point3d;
20 import javax.vecmath.Quat4d;
21 import javax.vecmath.Tuple3d;
22 import javax.vecmath.Vector3d;
24 import org.simantics.g3d.math.MathTools;
25 import org.simantics.g3d.math.Ray;
27 import vtk.vtkMatrix4x4;
29 import vtk.vtkRenderer;
31 public class vtkUtil {
33 public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {
34 Point2d screenPos = new Point2d(x,y);
35 Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);
36 Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);
37 Vector3d dir = new Vector3d(worldCoords2);
39 return new Ray(worldCoords, dir);
42 public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {
44 ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);
45 ren1.DisplayToWorld();
46 double world[] = ren1.GetWorldPoint();
48 return new Point3d(world);
52 public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {
53 ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);
54 ren1.WorldToDisplay();
55 double screen[] = ren1.GetDisplayPoint();
57 return new Point2d(screen);
61 public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {
62 Matrix4d mat = new Matrix4d();
63 for (int i = 0; i < 4; i++) {
64 for (int j = 0; j < 4; j++) {
65 mat.setElement(i, j, ptm.GetElement(i, j));
72 public static vtkMatrix4x4 getMatrix(Matrix4d m) {
73 vtkMatrix4x4 mat= new vtkMatrix4x4();
74 for (int i = 0; i < 4; i++) {
75 for (int j = 0; j < 4; j++) {
76 mat.SetElement(i, j, m.getElement(i, j));
82 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {
83 AxisAngle4d aa = new AxisAngle4d();
85 updateTransform(props, pos, aa);
88 public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {
89 actor.SetOrientation(0, 0, 0);
90 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
91 actor.SetPosition(pos);
94 public static void updateTransform(vtkProp3D actor, Tuple3d pos, AxisAngle4d aa) {
95 actor.SetOrientation(0, 0, 0);
96 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
97 actor.SetPosition(new double[] {pos.x,pos.y,pos.z});
100 public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {
101 actor.SetOrientation(0, 0, 0);
102 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
106 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {
107 for (vtkProp3D actor : props) {
108 actor.SetOrientation(0, 0, 0);
109 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
110 actor.SetPosition(pos.x, pos.y, pos.z);
114 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {
115 for (vtkProp3D actor : props) {
116 actor.SetOrientation(0, 0, 0);
117 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
118 actor.SetScale(scale);
119 actor.SetPosition(pos.x,pos.y,pos.z);
123 public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {
124 actor.SetOrientation(0, 0, 0);
125 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
126 actor.SetScale(scale);
127 actor.SetPosition(pos.x,pos.y,pos.z);
130 public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {
131 actor.SetOrientation(0, 0, 0);
132 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
133 actor.SetScale(scalex,scaley, scalez);
134 actor.SetPosition(pos.x,pos.y,pos.z);