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