]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - 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
index 6a9b6b9132f8d20d6f772fdffa55777d57c08f4f..71710def6d3df76170582933db615050f6589c04 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.vtk.utils;\r
-\r
-import java.util.Collection;\r
-\r
-import javax.vecmath.AxisAngle4d;\r
-import javax.vecmath.Matrix4d;\r
-import javax.vecmath.Point2d;\r
-import javax.vecmath.Point3d;\r
-import javax.vecmath.Quat4d;\r
-import javax.vecmath.Tuple3d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-import org.simantics.g3d.math.Ray;\r
-\r
-import vtk.vtkMatrix4x4;\r
-import vtk.vtkProp3D;\r
-import vtk.vtkRenderer;\r
-\r
-public class vtkUtil {\r
-\r
-       public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {\r
-               Point2d screenPos = new Point2d(x,y);\r
-               Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);\r
-               Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);\r
-               Vector3d dir = new Vector3d(worldCoords2);\r
-               dir.sub(worldCoords);\r
-               return new Ray(worldCoords, dir);\r
-       }\r
-       \r
-       public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {\r
-                               \r
-                ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);\r
-                ren1.DisplayToWorld();\r
-                double world[] = ren1.GetWorldPoint();  \r
-\r
-               return new Point3d(world);\r
-                \r
-       }\r
-       \r
-       public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {\r
-                ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);\r
-                ren1.WorldToDisplay();\r
-                double screen[] = ren1.GetDisplayPoint();\r
-                \r
-                return new Point2d(screen);\r
-                \r
-       }\r
-\r
-       public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {\r
-               Matrix4d mat = new Matrix4d();\r
-               for (int i = 0; i < 4; i++) {\r
-                       for (int j = 0; j < 4; j++) {\r
-                               mat.setElement(i, j, ptm.GetElement(i, j));\r
-                       }\r
-               }\r
-\r
-               return mat;\r
-       }\r
-       \r
-       public static vtkMatrix4x4 getMatrix(Matrix4d m) {\r
-               vtkMatrix4x4 mat= new vtkMatrix4x4();\r
-               for (int i = 0; i < 4; i++) {\r
-                       for (int j = 0; j < 4; j++) {\r
-                               mat.SetElement(i, j, m.getElement(i, j));\r
-                       }\r
-               }\r
-               return mat;\r
-       }\r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {\r
-               AxisAngle4d aa = new AxisAngle4d();\r
-               aa.set(q);\r
-               updateTransform(props, pos, aa);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetPosition(pos);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-       }\r
-       \r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {\r
-               for (vtkProp3D actor : props) {\r
-                       actor.SetOrientation(0, 0, 0);\r
-                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-                       actor.SetPosition(pos.x, pos.y, pos.z);\r
-               }\r
-       }\r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {\r
-               for (vtkProp3D actor : props) {\r
-                       actor.SetOrientation(0, 0, 0);\r
-                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-                       actor.SetScale(scale);\r
-                       actor.SetPosition(pos.x,pos.y,pos.z);\r
-               }\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetScale(scale);\r
-               actor.SetPosition(pos.x,pos.y,pos.z);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetScale(scalex,scaley, scalez);\r
-               actor.SetPosition(pos.x,pos.y,pos.z);\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.utils;
+
+import java.util.Collection;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Matrix4d;
+import javax.vecmath.Point2d;
+import javax.vecmath.Point3d;
+import javax.vecmath.Quat4d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.math.Ray;
+
+import vtk.vtkMatrix4x4;
+import vtk.vtkProp3D;
+import vtk.vtkRenderer;
+
+public class vtkUtil {
+
+       public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {
+               Point2d screenPos = new Point2d(x,y);
+               Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);
+               Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);
+               Vector3d dir = new Vector3d(worldCoords2);
+               dir.sub(worldCoords);
+               return new Ray(worldCoords, dir);
+       }
+       
+       public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {
+                               
+                ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);
+                ren1.DisplayToWorld();
+                double world[] = ren1.GetWorldPoint();  
+
+               return new Point3d(world);
+                
+       }
+       
+       public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {
+                ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);
+                ren1.WorldToDisplay();
+                double screen[] = ren1.GetDisplayPoint();
+                
+                return new Point2d(screen);
+                
+       }
+
+       public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {
+               Matrix4d mat = new Matrix4d();
+               for (int i = 0; i < 4; i++) {
+                       for (int j = 0; j < 4; j++) {
+                               mat.setElement(i, j, ptm.GetElement(i, j));
+                       }
+               }
+
+               return mat;
+       }
+       
+       public static vtkMatrix4x4 getMatrix(Matrix4d m) {
+               vtkMatrix4x4 mat= new vtkMatrix4x4();
+               for (int i = 0; i < 4; i++) {
+                       for (int j = 0; j < 4; j++) {
+                               mat.SetElement(i, j, m.getElement(i, j));
+                       }
+               }
+               return mat;
+       }
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {
+               AxisAngle4d aa = new AxisAngle4d();
+               aa.set(q);
+               updateTransform(props, pos, aa);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetPosition(pos);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Tuple3d pos, AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetPosition(new double[] {pos.x,pos.y,pos.z});
+       }
+       
+       public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+       }
+       
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {
+               for (vtkProp3D actor : props) {
+                       actor.SetOrientation(0, 0, 0);
+                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+                       actor.SetPosition(pos.x, pos.y, pos.z);
+               }
+       }
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {
+               for (vtkProp3D actor : props) {
+                       actor.SetOrientation(0, 0, 0);
+                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+                       actor.SetScale(scale);
+                       actor.SetPosition(pos.x,pos.y,pos.z);
+               }
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetScale(scale);
+               actor.SetPosition(pos.x,pos.y,pos.z);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetScale(scalex,scaley, scalez);
+               actor.SetPosition(pos.x,pos.y,pos.z);
+       }
+}