1 package org.simantics.g3d.vtk.utils;
\r
3 import java.util.Collection;
\r
5 import javax.vecmath.AxisAngle4d;
\r
6 import javax.vecmath.Matrix4d;
\r
7 import javax.vecmath.Point2d;
\r
8 import javax.vecmath.Point3d;
\r
9 import javax.vecmath.Quat4d;
\r
10 import javax.vecmath.Tuple3d;
\r
11 import javax.vecmath.Vector3d;
\r
13 import org.simantics.g3d.math.MathTools;
\r
14 import org.simantics.g3d.math.Ray;
\r
16 import vtk.vtkMatrix4x4;
\r
17 import vtk.vtkProp3D;
\r
18 import vtk.vtkRenderer;
\r
20 public class vtkUtil {
\r
22 public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {
\r
23 Point2d screenPos = new Point2d(x,y);
\r
24 Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);
\r
25 Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);
\r
26 Vector3d dir = new Vector3d(worldCoords2);
\r
27 dir.sub(worldCoords);
\r
28 return new Ray(worldCoords, dir);
\r
31 public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {
\r
33 ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);
\r
34 ren1.DisplayToWorld();
\r
35 double world[] = ren1.GetWorldPoint();
\r
37 return new Point3d(world);
\r
41 public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {
\r
42 ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);
\r
43 ren1.WorldToDisplay();
\r
44 double screen[] = ren1.GetDisplayPoint();
\r
46 return new Point2d(screen);
\r
50 public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {
\r
51 Matrix4d mat = new Matrix4d();
\r
52 for (int i = 0; i < 4; i++) {
\r
53 for (int j = 0; j < 4; j++) {
\r
54 mat.setElement(i, j, ptm.GetElement(i, j));
\r
61 public static vtkMatrix4x4 getMatrix(Matrix4d m) {
\r
62 vtkMatrix4x4 mat= new vtkMatrix4x4();
\r
63 for (int i = 0; i < 4; i++) {
\r
64 for (int j = 0; j < 4; j++) {
\r
65 mat.SetElement(i, j, m.getElement(i, j));
\r
71 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {
\r
72 AxisAngle4d aa = new AxisAngle4d();
\r
74 updateTransform(props, pos, aa);
\r
77 public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {
\r
78 actor.SetOrientation(0, 0, 0);
\r
79 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
80 actor.SetPosition(pos);
\r
83 public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {
\r
84 actor.SetOrientation(0, 0, 0);
\r
85 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
89 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {
\r
90 for (vtkProp3D actor : props) {
\r
91 actor.SetOrientation(0, 0, 0);
\r
92 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
93 actor.SetPosition(pos.x, pos.y, pos.z);
\r
97 public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {
\r
98 for (vtkProp3D actor : props) {
\r
99 actor.SetOrientation(0, 0, 0);
\r
100 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
101 actor.SetScale(scale);
\r
102 actor.SetPosition(pos.x,pos.y,pos.z);
\r
106 public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {
\r
107 actor.SetOrientation(0, 0, 0);
\r
108 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
109 actor.SetScale(scale);
\r
110 actor.SetPosition(pos.x,pos.y,pos.z);
\r
113 public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {
\r
114 actor.SetOrientation(0, 0, 0);
\r
115 actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
\r
116 actor.SetScale(scalex,scaley, scalez);
\r
117 actor.SetPosition(pos.x,pos.y,pos.z);
\r