]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java
6a9b6b9132f8d20d6f772fdffa55777d57c08f4f
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / utils / vtkUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.vtk.utils;\r
13 \r
14 import java.util.Collection;\r
15 \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
23 \r
24 import org.simantics.g3d.math.MathTools;\r
25 import org.simantics.g3d.math.Ray;\r
26 \r
27 import vtk.vtkMatrix4x4;\r
28 import vtk.vtkProp3D;\r
29 import vtk.vtkRenderer;\r
30 \r
31 public class vtkUtil {\r
32 \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
40         }\r
41         \r
42         public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {\r
43                                 \r
44                  ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);\r
45                  ren1.DisplayToWorld();\r
46                  double world[] = ren1.GetWorldPoint();  \r
47 \r
48                 return new Point3d(world);\r
49                  \r
50         }\r
51         \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
56                  \r
57                  return new Point2d(screen);\r
58                  \r
59         }\r
60 \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
66                         }\r
67                 }\r
68 \r
69                 return mat;\r
70         }\r
71         \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
77                         }\r
78                 }\r
79                 return mat;\r
80         }\r
81         \r
82         public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {\r
83                 AxisAngle4d aa = new AxisAngle4d();\r
84                 aa.set(q);\r
85                 updateTransform(props, pos, aa);\r
86         }\r
87         \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
92         }\r
93         \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
97         }\r
98         \r
99         \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
105                 }\r
106         }\r
107         \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
114                 }\r
115         }\r
116         \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
122         }\r
123         \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
129         }\r
130 }\r