]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java
639d894ee44adbac2eebd8afc79c89b687d8ce9c
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / utils / vtkUtil.java
1 package org.simantics.g3d.vtk.utils;\r
2 \r
3 import java.util.Collection;\r
4 \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
12 \r
13 import org.simantics.g3d.math.MathTools;\r
14 import org.simantics.g3d.math.Ray;\r
15 \r
16 import vtk.vtkMatrix4x4;\r
17 import vtk.vtkProp3D;\r
18 import vtk.vtkRenderer;\r
19 \r
20 public class vtkUtil {\r
21 \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
29         }\r
30         \r
31         public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {\r
32                                 \r
33                  ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);\r
34                  ren1.DisplayToWorld();\r
35                  double world[] = ren1.GetWorldPoint();  \r
36 \r
37                 return new Point3d(world);\r
38                  \r
39         }\r
40         \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
45                  \r
46                  return new Point2d(screen);\r
47                  \r
48         }\r
49 \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
55                         }\r
56                 }\r
57 \r
58                 return mat;\r
59         }\r
60         \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
66                         }\r
67                 }\r
68                 return mat;\r
69         }\r
70         \r
71         public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {\r
72                 AxisAngle4d aa = new AxisAngle4d();\r
73                 aa.set(q);\r
74                 updateTransform(props, pos, aa);\r
75         }\r
76         \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
81         }\r
82         \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
86         }\r
87         \r
88         \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
94                 }\r
95         }\r
96         \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
103                 }\r
104         }\r
105         \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
111         }\r
112         \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
118         }\r
119 }\r