00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.rosjava_geometry;
00018
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertTrue;
00021
00022 import org.junit.Test;
00023
00024 import java.util.Random;
00025
00029 public class TransformTest {
00030
00031 @Test
00032 public void testMultiply() {
00033 Transform transform1 = new Transform(Vector3.xAxis(), Quaternion.identity());
00034 Transform transform2 =
00035 new Transform(Vector3.yAxis(), Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2));
00036
00037 Transform result1 = transform1.multiply(transform2);
00038 assertEquals(1.0, result1.getTranslation().getX(), 1e-9);
00039 assertEquals(1.0, result1.getTranslation().getY(), 1e-9);
00040 assertEquals(0.0, result1.getTranslation().getZ(), 1e-9);
00041 assertEquals(0.0, result1.getRotationAndScale().getX(), 1e-9);
00042 assertEquals(0.0, result1.getRotationAndScale().getY(), 1e-9);
00043 assertEquals(0.7071067811865475, result1.getRotationAndScale().getZ(), 1e-9);
00044 assertEquals(0.7071067811865475, result1.getRotationAndScale().getW(), 1e-9);
00045
00046 Transform result2 = transform2.multiply(transform1);
00047 assertEquals(0.0, result2.getTranslation().getX(), 1e-9);
00048 assertEquals(2.0, result2.getTranslation().getY(), 1e-9);
00049 assertEquals(0.0, result2.getTranslation().getZ(), 1e-9);
00050 assertEquals(0.0, result2.getRotationAndScale().getX(), 1e-9);
00051 assertEquals(0.0, result2.getRotationAndScale().getY(), 1e-9);
00052 assertEquals(0.7071067811865475, result2.getRotationAndScale().getZ(), 1e-9);
00053 assertEquals(0.7071067811865475, result2.getRotationAndScale().getW(), 1e-9);
00054 }
00055
00056 @Test
00057 public void testInvert() {
00058 Transform transform =
00059 new Transform(Vector3.yAxis(), Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2));
00060 Transform inverse = transform.invert();
00061
00062 assertEquals(-1.0, inverse.getTranslation().getX(), 1e-9);
00063 assertEquals(0.0, inverse.getTranslation().getY(), 1e-9);
00064 assertEquals(0.0, inverse.getTranslation().getZ(), 1e-9);
00065 assertEquals(0.0, inverse.getRotationAndScale().getX(), 1e-9);
00066 assertEquals(0.0, inverse.getRotationAndScale().getY(), 1e-9);
00067 assertEquals(-0.7071067811865475, inverse.getRotationAndScale().getZ(), 1e-9);
00068 assertEquals(0.7071067811865475, inverse.getRotationAndScale().getW(), 1e-9);
00069
00070 Transform neutral = transform.multiply(inverse);
00071 assertTrue(neutral.almostEquals(Transform.identity(), 1e-9));
00072 }
00073
00074 @Test
00075 public void testInvertRandom() {
00076 Random random = new Random();
00077 random.setSeed(42);
00078 for (int i = 0; i < 10000; i++) {
00079 Vector3 vector = randomVector(random);
00080 Quaternion quaternion = randomQuaternion(random);
00081 Transform transform = new Transform(vector, quaternion);
00082 Transform inverse = transform.invert();
00083 Transform neutral = transform.multiply(inverse);
00084 assertTrue(neutral.almostEquals(Transform.identity(), 1e-9));
00085 }
00086 }
00087
00088 @Test
00089 public void testMultiplyRandom() {
00090 Random random = new Random();
00091 random.setSeed(42);
00092 for (int i = 0; i < 10000; i++) {
00093 Vector3 data = randomVector(random);
00094 Vector3 vector1 = randomVector(random);
00095 Vector3 vector2 = randomVector(random);
00096 Quaternion quaternion1 = randomQuaternion(random);
00097 Quaternion quaternion2 = randomQuaternion(random);
00098 Transform transform1 = new Transform(vector1, quaternion1);
00099 Transform transform2 = new Transform(vector2, quaternion2);
00100 Vector3 result1 = transform1.apply(transform2.apply(data));
00101 Vector3 result2 = transform1.multiply(transform2).apply(data);
00102 assertTrue(result1.almostEquals(result2, 1e-9));
00103 }
00104 }
00105
00106 @Test
00107 public void testScale() {
00108 assertTrue(Vector3.xAxis().scale(2)
00109 .almostEquals(Transform.identity().scale(2).apply(Vector3.xAxis()), 1e-9));
00110 }
00111
00112 private Quaternion randomQuaternion(Random random) {
00113 return new Quaternion(random.nextDouble(), random.nextDouble(), random.nextDouble(),
00114 random.nextDouble());
00115 }
00116
00117 private Vector3 randomVector(Random random) {
00118 return new Vector3(random.nextDouble(), random.nextDouble(), random.nextDouble());
00119 }
00120 }