1 package com.neuronrobotics.sdk.addons.kinematics.math;
3 import com.neuronrobotics.sdk.common.Log;
19 double[][] rotationMatrix =
new double[][] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
39 if (Double.isNaN(tilt))
40 throw new NumberFormatException(
"Value can not be NaN");
41 if (Double.isNaN(azumeth))
42 throw new NumberFormatException(
"Value can not be NaN");
43 if (Double.isNaN(elevation))
44 throw new NumberFormatException(
"Value can not be NaN");
45 if (elevation >= 90 || elevation <= -90)
46 throw new NumberFormatException(
"Elevation must be between 90 and -90");
51 loadFromAngles(tilt + Math.random() * .02 + .001, azumeth + Math.random() * .02 + .001,
52 elevation + Math.random() * .02 + .001);
58 double attitude = Math.toRadians(elevation);
59 double heading = Math.toRadians(azumeth);
60 double bank = Math.toRadians(tilt);
63 double c1 = Math.cos(heading / 2);
66 double s1 = Math.sin(heading / 2);
67 double c2 = Math.cos(attitude / 2);
68 double s2 = Math.sin(attitude / 2);
69 double c3 = Math.cos(bank / 2);
70 double s3 = Math.sin(bank / 2);
71 double c1c2 = c1 * c2;
72 double s1s2 = s1 * s2;
75 w = c1c2 * c3 - s1s2 * s3;
76 x = c1c2 * s3 + s1s2 * c3;
77 y = s1 * c2 * c3 + c1 * s2 * s3;
78 z = c1 * s2 * c3 - s1 * c2 * s3;
100 this(values[0], values[1], values[2], values[3]);
111 double[][] rotation =
new double[3][3];
112 double rotationAngleRadians = Math.PI / 180 * rotationAngleDegrees;
120 rotation[1][1] = Math.cos(rotationAngleRadians);
121 rotation[2][1] = Math.sin(rotationAngleRadians);
124 rotation[1][2] = -Math.sin(rotationAngleRadians);
125 rotation[2][2] = Math.cos(rotationAngleRadians);
138 double[][] rotation =
new double[3][3];
139 double rotationAngleRadians = Math.PI / 180 * rotationAngleDegrees;
142 rotation[0][0] = Math.cos(rotationAngleRadians);
144 rotation[2][0] = -Math.sin(rotationAngleRadians);
150 rotation[0][2] = Math.sin(rotationAngleRadians);
152 rotation[2][2] = Math.cos(rotationAngleRadians);
165 double[][] rotation =
new double[3][3];
166 double rotationAngleRadians = Math.PI / 180 * rotationAngleDegrees;
169 rotation[0][0] = Math.cos(rotationAngleRadians);
170 rotation[1][0] = Math.sin(rotationAngleRadians);
173 rotation[0][1] = -Math.sin(rotationAngleRadians);
174 rotation[1][1] = Math.cos(rotationAngleRadians);
208 double[][] rotation =
new double[3][3];
209 for (
int i = 0; i < 3; i++) {
210 for (
int j = 0; j < 3; j++) {
211 rotation[i][j] = m.get(i, j);
224 if (rotM.length != 3)
225 throw new RuntimeException(
"Must be 3x3 rotation matrix");
226 for (
int i = 0; i < 3; i++) {
227 if (rotM[i].length != 3) {
228 throw new RuntimeException(
"Must be 3x3 rotation matrix");
231 for (
int i = 0; i < 3; i++) {
232 for (
int j = 0; j < 3; j++) {
236 rotationMatrix[i][j] = rotM[i][j];
247 double[][] b =
new double[3][3];
248 for (
int i = 0; i < 3; i++) {
249 for (
int j = 0; j < 3; j++) {
250 b[i][j] = rotationMatrix[i][j];
265 for (
int i = 0; i < 3; i++) {
267 for (
int j = 0; j < 3; j++) {
268 s += m[i][j] +
"\t\t";
289 for (
int i = 0; i < 3; i++) {
291 for (
int j = 0; j < 3; j++) {
292 s += array[i][j] +
"\t\t";
297 return "Matrix = " + s;
314 throw new NumberFormatException(
"Value can not be NaN");
316 throw new NumberFormatException(
"Value can not be NaN");
318 throw new NumberFormatException(
"Value can not be NaN");
320 throw new NumberFormatException(
"Value can not be NaN");
321 double norm = Math.sqrt(w * w + x * x + y * y + z * z);
324 double s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0;
341 rotationMatrix[0][0] = 1 - (yy + zz);
342 rotationMatrix[0][1] = (xy - zw);
343 rotationMatrix[0][2] = (xz + yw);
345 rotationMatrix[1][0] = (xy + zw);
346 rotationMatrix[1][1] = 1 - (xx + zz);
347 rotationMatrix[1][2] = (yz - xw);
349 rotationMatrix[2][0] = (xz - yw);
350 rotationMatrix[2][1] = (yz + xw);
351 rotationMatrix[2][2] = 1 - (xx + yy);
472 public static boolean bound(
double low,
double high,
double n) {
473 return n >= low && n <= high;
484 double w, x, y, z, tilt, elev, azumeth;
493 double unit = sqx + sqy + sqz + sqw;
495 double test = x * y + z * w;
496 double testingValue = (0.5 - Double.MIN_VALUE) * unit;
503 if (test > testingValue) {
505 elev = 2 * Math.atan2(x, w);
506 azumeth = Math.PI / 2;
509 }
else if (test < -testingValue) {
511 elev = -2 * Math.atan2(x, w);
512 azumeth = -Math.PI / 2;
516 elev = Math.atan2(2 * y * w - 2 * x * z, sqx - sqy - sqz + sqw);
517 azumeth = Math.asin(2 * test / unit);
518 tilt = Math.atan2(2 * x * w - 2 * y * z, -sqx + sqy - sqz + sqw);
621 double temp = 0.5 * Math.sqrt(1 + rotationMatrix[0][0] + rotationMatrix[1][1] + rotationMatrix[2][2]);
623 throw new RuntimeException(
"Matrix needs normalization");
633 double temp = 0.5 * Math.sqrt(1 + rotationMatrix[0][0] + rotationMatrix[1][1] + rotationMatrix[2][2]);
634 return (rotationMatrix[2][1] - rotationMatrix[1][2]) * 0.25 / temp;
643 double temp = 0.5 * Math.sqrt(1 + rotationMatrix[0][0] + rotationMatrix[1][1] + rotationMatrix[2][2]);
644 return (rotationMatrix[0][2] - rotationMatrix[2][0]) * 0.25 / temp;
653 double temp = 0.5 * Math.sqrt(1 + rotationMatrix[0][0] + rotationMatrix[1][1] + rotationMatrix[2][2]);
654 return (rotationMatrix[1][0] - rotationMatrix[0][1]) * 0.25 / temp;
String toString(double[][] array)
RotationNRLegacy(double[][] rotationMatrix)
RotationNRLegacy(double w, double x, double y, double z)
void loadRotations(double[][] rotM)
static RotationNRLegacy getRotationX(double rotationAngleDegrees)
void quaternion2RotationMatrix(double w, double x, double y, double z)
double getRotationMatrix2QuaturnionW()
double[][] getRotationMatrix()
double getRotationMatrix2QuaturnionZ()
double getRotationAzimuth()
void loadFromAngles(double tilt, double azumeth, double elevation)
double getRotAngle(int index)
RotationNRLegacy(Matrix m)
double getRotationMatrix2QuaturnionY()
static boolean bound(double low, double high, double n)
static RotationNRLegacy getRotationY(double rotationAngleDegrees)
RotationNRLegacy(double[] values)
static RotationNRLegacy getRotationZ(double rotationAngleDegrees)
RotationNRLegacy(double tilt, double elevation, double azumeth)
double getRotationMatrix2QuaturnionX()
double getRotationElevation()
static void warning(String message)