BowlerKernel
TransformFactory.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.physics;
2 
3 import javax.vecmath.Matrix4d;
4 import javax.vecmath.Quat4d;
5 import javax.vecmath.Quat4f;
6 import javax.vecmath.Vector3d;
7 
8 import com.neuronrobotics.sdk.addons.kinematics.math.RotationNR;
9 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
10 
11 import javafx.application.Platform;
12 import javafx.scene.transform.Affine;
13 
14 // TODO: Auto-generated Javadoc
15 
19 @SuppressWarnings("restriction")
20 public class TransformFactory {
21 
30  public static Affine newAffine(double x, double y, double z){
31  return nrToAffine(new TransformNR(x, y, z, new RotationNR()));
32  }
33 
40  public static Affine nrToAffine(TransformNR input){
41  Affine rotations =new Affine();
42  return nrToAffine( input , rotations);
43  }
44 
51  public static TransformNR affineToNr(Affine input){
52  TransformNR rotations =new TransformNR();
53  return affineToNr( rotations,input );
54  }
62  public static TransformNR affineToNr(TransformNR outputValue ,Affine rotations){
63  double[][] poseRot = outputValue
65 
66  poseRot[0][0]=rotations.getMxx();
67  poseRot[0][1]=rotations.getMxy();
68  poseRot[0][2]=rotations.getMxz();
69  poseRot[1][0]=rotations.getMyx();
70  poseRot[1][1]=rotations.getMyy();
71  poseRot[1][2]=rotations.getMyz();
72  poseRot[2][0]=rotations.getMzx();
73  poseRot[2][1]=rotations.getMzy();
74  poseRot[2][2]=rotations.getMzz();
75 
76  outputValue.set(rotations.getTx(),rotations.getTy(),rotations.getTz(),poseRot);
77  return outputValue;
78  }
79 
87  public static Affine nrToAffine(TransformNR input ,Affine rotations){
88  if (!Platform.isFxApplicationThread()) {
89  new RuntimeException("This method must be in UI thread!").printStackTrace();
90  }
91  if(input==null )
92  return rotations;
93  if( rotations==null)
94  rotations=new Affine();
95  double[][] poseRot = input
97  try {
98  rotations.setMxx(poseRot[0][0]);
99  rotations.setMxy(poseRot[0][1]);
100  rotations.setMxz(poseRot[0][2]);
101  rotations.setMyx(poseRot[1][0]);
102  rotations.setMyy(poseRot[1][1]);
103  rotations.setMyz(poseRot[1][2]);
104  rotations.setMzx(poseRot[2][0]);
105  rotations.setMzy(poseRot[2][1]);
106  rotations.setMzz(poseRot[2][2]);
107  rotations.setTx(input.getX());
108  rotations.setTy(input.getY());
109  rotations.setTz(input.getZ());
110  }catch(Exception e) {
111  e.printStackTrace();
112  }
113  return rotations;
114  }
115  public static void nrToBullet(TransformNR nr, com.bulletphysics.linearmath.Transform bullet) {
116  bullet.origin.set((float) nr.getX(), (float) nr.getY(), (float) nr.getZ());
117  bullet.setRotation(new Quat4f((float) nr.getRotation().getRotationMatrix2QuaturnionX(),
121  }
122 
123  public static TransformNR bulletToNr(com.bulletphysics.linearmath.Transform bullet) {
124  Quat4f out = new Quat4f();
125  bullet.getRotation(out);
126  return new TransformNR(bullet.origin.x, bullet.origin.y, bullet.origin.z, out.w, out.x, out.y, out.z);
127  }
128 
129  public static void bulletToAffine(Affine affine, com.bulletphysics.linearmath.Transform bullet) {
130  if (!Platform.isFxApplicationThread()) {
131  new RuntimeException("This method must be in UI thread!").printStackTrace();
132  }
133  // synchronized(out){
134  double[][] vals = bulletToNr(bullet).getRotationMatrix().getRotationMatrix();
135  // we explicitly test norm against one here, saving a division
136  // at the cost of a test and branch. Is it worth it?
137  // compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs
138  // will be used 2-4 times each.
139  affine.setMxx(vals[0][0]);
140  affine.setMxy(vals[0][1]);
141  affine.setMxz(vals[0][2]);
142  affine.setMyx(vals[1][0]);
143  affine.setMyy(vals[1][1]);
144  affine.setMyz(vals[1][2]);
145  affine.setMzx(vals[2][0]);
146  affine.setMzy(vals[2][1]);
147  affine.setMzz(vals[2][2]);
148  // }
149  affine.setTx(bullet.origin.x);
150  affine.setTy(bullet.origin.y);
151  affine.setTz(bullet.origin.z);
152  }
153 
154  public static void affineToBullet(Affine affine, com.bulletphysics.linearmath.Transform bullet) {
155  TransformNR nr = affineToNr(affine);
156  nrToBullet(nr, bullet);
157  }
158 
159  public static eu.mihosoft.vrl.v3d.Transform nrToCSG(TransformNR nr) {
160  Quat4d q1 = new Quat4d();
165  Vector3d t1 = new Vector3d();
166  t1.x = nr.getX();
167  t1.y = nr.getY();
168  t1.z = nr.getZ();
169  double s = 1.0;
170 
171  Matrix4d rotation = new Matrix4d(q1, t1, s);
172  return new eu.mihosoft.vrl.v3d.Transform(rotation);
173  }
174 
175  public static TransformNR scale(TransformNR incoming, double scale) {
176  return new TransformNR(incoming.getX() * scale, incoming.getY() * scale, incoming.getZ() * scale,
177  new RotationNR(Math.toDegrees(incoming.getRotation().getRotationTilt()) * scale,
178  Math.toDegrees(incoming.getRotation().getRotationAzimuth()) * scale,
179  Math.toDegrees(incoming.getRotation().getRotationElevation()) * scale));
180  }
181 
182  public static TransformNR csgToNR(eu.mihosoft.vrl.v3d.Transform csg) {
183  Matrix4d rotation = csg.getInternalMatrix();
184  Quat4d q1 = new Quat4d();
185  rotation.get(q1);
186  Vector3d t1 = new Vector3d();
187  rotation.get(t1);
188 
189  return new TransformNR(t1.x, t1.y, t1.z, new RotationNR(q1.w, q1.x, q1.y, q1.z));
190  }
191 
192 }
static eu.mihosoft.vrl.v3d.Transform nrToCSG(TransformNR nr)
static Affine newAffine(double x, double y, double z)
static TransformNR affineToNr(TransformNR outputValue, Affine rotations)
static TransformNR csgToNR(eu.mihosoft.vrl.v3d.Transform csg)
static void affineToBullet(Affine affine, com.bulletphysics.linearmath.Transform bullet)
static TransformNR scale(TransformNR incoming, double scale)
static void bulletToAffine(Affine affine, com.bulletphysics.linearmath.Transform bullet)
static void nrToBullet(TransformNR nr, com.bulletphysics.linearmath.Transform bullet)
static TransformNR bulletToNr(com.bulletphysics.linearmath.Transform bullet)
static Affine nrToAffine(TransformNR input, Affine rotations)
TransformNR set(double tx, double ty, double tz, double[][] poseRot)