BowlerKernel
TransformNR.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.kinematics.math;
2 
3 import java.math.BigDecimal;
4 import java.text.DecimalFormat;
5 import java.util.ArrayList;
6 import com.neuronrobotics.sdk.common.Log;
7 import Jama.Matrix;
8 
9 // TODO: Auto-generated Javadoc
13 public class TransformNR {
14  private ArrayList<ITransformNRChangeListener> listeners=null;
16  private double x;
17 
19  private double y;
20 
22  private double z;
23 
26 
27 
28 
34  public TransformNR(Matrix m) {
35  this.setX(m.get(0, 3));
36  this.setY(m.get(1, 3));
37  this.setZ(m.get(2, 3));
38  this.setRotation(new RotationNR(m));
39  }
40 
52  public TransformNR(double x, double y, double z, double w, double rotx, double roty,
53  double rotz) {
54  this.setX(x);
55  this.setY(y);
56  this.setZ(z);
57  this.setRotation(new RotationNR(new double[] {w, rotx, roty, rotz}));
58  }
59 
66  public TransformNR(double[] cartesianSpaceVector, double[][] rotationMatrix) {
67  this.setX(cartesianSpaceVector[0]);
68  this.setY(cartesianSpaceVector[1]);
69  this.setZ(cartesianSpaceVector[2]);
70  this.setRotation(new RotationNR(rotationMatrix));
71  }
72 
79  public TransformNR(double[] cartesianSpaceVector, double[] quaternionVector) {
80  this.setX(cartesianSpaceVector[0]);
81  this.setY(cartesianSpaceVector[1]);
82  this.setZ(cartesianSpaceVector[2]);
83  this.setRotation(new RotationNR(quaternionVector));
84  }
85 
94  public TransformNR(double x, double y, double z, RotationNR q) {
95  this.setX(x);
96  this.setY(y);
97  this.setZ(z);
98  this.setRotation(q);
99  }
100 
107  public TransformNR(double[] cartesianSpaceVector, RotationNR q) {
108  this.setX(cartesianSpaceVector[0]);
109  this.setY(cartesianSpaceVector[1]);
110  this.setZ(cartesianSpaceVector[2]);
111  this.setRotation(q);
112  }
113 
117  public TransformNR() {
118  this.setX(0);
119  this.setY(0);
120  this.setZ(0);
121  this.setRotation(new RotationNR());
122  }
123 
129  public double getX() {
130  return x;
131  }
132 
138  public double getY() {
139  return y;
140  }
141 
147  public double getZ() {
148  return z;
149  }
150 
156  public double[][] getRotationMatrixArray() {
157  return getRotation().getRotationMatrix();
158  }
159 
166  return getRotation();
167  }
168 
176  public double getRotationValue(int i, int j) {
177  return getRotation().getRotationMatrix()[i][j];
178  }
179 
186 
187  return rotation;
188  }
189 
198  }
199 
200  /*
201  * (non-Javadoc)
202  *
203  * @see java.lang.Object#toString()
204  */
205  @Override
206  public String toString() {
207  try {
209  } catch (Exception ex) {
210  return "Transform error" + ex.getLocalizedMessage();
211  }
212  }
213 
220  public static String getMatrixString(Matrix matrix) {
221 
222  String s = "{\n";
223  double[][] m = matrix.getArray();
224 
225  DecimalFormat decimalFormat = new DecimalFormat("000.00");
226  int across = m.length;
227  int down = m[0].length;
228 
229  for (int i = 0; i < across; i++) {
230  s += "{ ";
231  for (int j = 0; j < down; j++) {
232  if (m[i][j] < 0) {
233  s += decimalFormat.format(m[i][j]);
234  } else
235  s += decimalFormat.format(m[i][j]);
236  if (j < down - 1)
237  s += ",";
238  s += "\t";
239  }
240  s += " }";
241  if (i < across - 1)
242  s += ",";
243  s += "\n";
244  }
245  return s + "}\n";
246  }
247 
253  public double[] getPositionArray() {
254  return new double[] {getX(), getY(), getZ()};
255  }
256 
262  public Matrix getMatrixTransform() {
263  double[][] transform = new double[4][4];
264  double[][] rotation = getRotationMatrixArray();
265 
266 
267  for (int i = 0; i < 3; i++) {
268  for (int j = 0; j < 3; j++) {
269  transform[i][j] = rotation[i][j];
270  }
271  }
272  for (int i = 0; i < 3; i++) {
273  transform[3][i] = 0;
274  }
275  transform[3][3] = 1;
276  transform[0][3] = getX();
277  transform[1][3] = getY();
278  transform[2][3] = getZ();
279 
280 
281  return new Matrix(transform);
282  }
283 
297  double r = Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)));
298  return r;
299  }
300 
308  double x = getX() - t.getX();
309  double y = getY() - t.getY();
310  double z = getZ() - t.getZ();
311  double r = Math.sqrt((Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)));
312  return r;
313  }
314 
320  public TransformNR inverse() {
321  return new TransformNR(getMatrixTransform().inverse());
322  }
323 
330  public TransformNR scale(BigDecimal scale) {
331  return scale(scale.doubleValue());
332  }
333 
340  public TransformNR scale(double t) {
341  if (t > 1)
342  t = 1;
343  if (t <= 0)
344  return new TransformNR();
345 
346  double tilt = Math.toDegrees(getRotation().getRotationTilt() * t);
347  double az = Math.toDegrees(getRotation().getRotationAzimuth() * t);
348  double ele = Math.toDegrees(getRotation().getRotationElevation() * t);
349  return new TransformNR(getX() * t, getY() * t, getZ() * t, new RotationNR(tilt, az, ele));
350  }
351 
357  public TransformNR copy() {
358  return new TransformNR(getMatrixTransform());
359  }
360 
367  public TransformNR translateX(double translation) {
368  setX(getX() + translation);
369  return this;
370  }
371 
377  public TransformNR translateY(double translation) {
378  setY(getY() + translation);
379  return this;
380 
381  }
382 
388  public TransformNR translateZ(double translation) {
389 
390  setZ(getZ() + translation);
391  return this;
392  }
393 
394  public TransformNR set(double tx, double ty, double tz, double[][] poseRot) {
395  if (Double.isNaN(tx))
396  throw new RuntimeException("Value can not be NaN");
397  x = tx;
398  if (Double.isNaN(ty))
399  throw new RuntimeException("Value can not be NaN");
400  y = ty;
401  if (Double.isNaN(tz))
402  throw new RuntimeException("Value can not be NaN");
403  z = tz;
404  getRotation().set(poseRot);
405  fireChangeEvent();
406  return this;
407  }
408 
414  public TransformNR setX(double tx) {
415  if (Double.isNaN(tx))
416  throw new RuntimeException("Value can not be NaN");
417  x = tx;
418  fireChangeEvent();
419  return this;
420  }
421 
427  public TransformNR setY(double ty) {
428  if (Double.isNaN(ty))
429  throw new RuntimeException("Value can not be NaN");
430  y = ty;
431  fireChangeEvent();
432  return this;
433  }
434 
440  public TransformNR setZ(double tz) {
441  if (Double.isNaN(tz))
442  throw new RuntimeException("Value can not be NaN");
443  z = tz;
444  fireChangeEvent();
445  return this;
446  }
447 
453  /*
454  *
455  * Generate the xml configuration to generate an XML of this robot.
456  */
457  public String getXml() {
458  String xml =
459  "\t<x>" + getX() + "</x>\n" + "\t<y>" + getY() + "</y>\n" + "\t<z>" + getZ() + "</z>\n";
460  if (Double.isNaN(getRotation().getRotationMatrix2QuaturnionW())
461  || Double.isNaN(getRotation().getRotationMatrix2QuaturnionX())
462  || Double.isNaN(getRotation().getRotationMatrix2QuaturnionY())
463  || Double.isNaN(getRotation().getRotationMatrix2QuaturnionZ())) {
464  xml += "\n\t<!-- ERROR a NaN was detected and replaced with a valid rotation -->\n";
465  setRotation(new RotationNR());
466  }
467  xml += "\t<rotw>" + getRotation().getRotationMatrix2QuaturnionW() + "</rotw>\n" + "\t<rotx>"
468  + getRotation().getRotationMatrix2QuaturnionX() + "</rotx>\n" + "\t<roty>"
469  + getRotation().getRotationMatrix2QuaturnionY() + "</roty>\n" + "\t<rotz>"
470  + getRotation().getRotationMatrix2QuaturnionZ() + "</rotz>";
471 
472  return xml;
473  }
474 
481  this.rotation = rotation;
482  fireChangeEvent();
483  return this;
484  }
485 
486 
488  if(!getListeners().contains(l))
489  getListeners().add(l);
490  }
492  if(getListeners().contains(l))
493  getListeners().remove(l);
494  }
495  public void clearChangeListener() {
496  getListeners().clear();
497  listeners=null;
498  }
499  public ArrayList<ITransformNRChangeListener> getListeners() {
500  if(listeners==null)
501  listeners=new ArrayList<ITransformNRChangeListener>();
502  return listeners;
503  }
504 
505  void fireChangeEvent() {
506  if(listeners!=null) {
507  for(int i=0;i<listeners.size();i++) {
508  try {
509  listeners.get(i).event(this);
510  }catch (Throwable t) {
511  t.printStackTrace();
512  }
513  }
514  }
515  }
516 
517  public void setTiltDegrees(double newAngleDegrees) {
518  double e=0;
519  try{
520  e=Math.toDegrees(getRotation().getRotationElevation());
521  }catch(Exception ex){
522  ex.printStackTrace();
523  }
524  double a=0;
525  try{
526  a=Math.toDegrees(getRotation().getRotationAzimuth());
527  }catch(Exception ex){
528  ex.printStackTrace();
529  }
530 
531  setRotation(new RotationNR(newAngleDegrees, a, e));
532 
533 
534  }
535 
536  public void setElevationDegrees(double newAngleDegrees) {
537  double t=0;
538  try{
539  t=Math.toDegrees(getRotation().getRotationTilt());
540  }catch(Exception ex){
541  ex.printStackTrace();
542  }
543 
544  double a=0;
545  try{
546  a=Math.toDegrees(getRotation().getRotationAzimuth());
547  }catch(Exception ex){
548  ex.printStackTrace();
549  }
550  setRotation(new RotationNR(t, a, newAngleDegrees));
551  }
552 
553  public void setAzimuthDegrees(double newAngleDegrees) {
554  double t=0;
555  try{
556  t=Math.toDegrees(getRotation().getRotationTilt());
557  }catch(Exception ex){
558  ex.printStackTrace();
559  }
560 
561  double e=0;
562  try{
563  e=Math.toDegrees(getRotation().getRotationElevation());
564  }catch(Exception ex){
565  ex.printStackTrace();
566  }
567  setRotation(new RotationNR(t, newAngleDegrees, e));
568  }
569 }
TransformNR(double[] cartesianSpaceVector, double[] quaternionVector)
ArrayList< ITransformNRChangeListener > listeners
void addChangeListener(ITransformNRChangeListener l)
ArrayList< ITransformNRChangeListener > getListeners()
TransformNR(double x, double y, double z, RotationNR q)
TransformNR(double[] cartesianSpaceVector, double[][] rotationMatrix)
TransformNR(double x, double y, double z, double w, double rotx, double roty, double rotz)
void removeChangeListener(ITransformNRChangeListener l)
TransformNR(double[] cartesianSpaceVector, RotationNR q)