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