FrameTransformTreeTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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.Before;
00023 import org.junit.Test;
00024 import org.ros.internal.message.DefaultMessageFactory;
00025 import org.ros.internal.message.definition.MessageDefinitionReflectionProvider;
00026 import org.ros.message.MessageDefinitionProvider;
00027 import org.ros.message.MessageFactory;
00028 import org.ros.message.Time;
00029 import org.ros.namespace.NameResolver;
00030 
00034 public class FrameTransformTreeTest {
00035 
00036   private NameResolver nameResolver;
00037   private FrameTransformTree frameTransformTree;
00038   private MessageDefinitionProvider messageDefinitionProvider;
00039   private MessageFactory messageFactory;
00040 
00041   @Before
00042   public void before() {
00043     nameResolver = NameResolver.newRoot();
00044     frameTransformTree = new FrameTransformTree(nameResolver);
00045     messageDefinitionProvider = new MessageDefinitionReflectionProvider();
00046     messageFactory = new DefaultMessageFactory(messageDefinitionProvider);
00047   }
00048 
00049   @Test
00050   public void testUpdateAndGet() {
00051     FrameTransform frameTransform =
00052         new FrameTransform(Transform.identity(), nameResolver.resolve("foo"),
00053             nameResolver.resolve("bar"), new Time());
00054     frameTransformTree.update(frameTransform);
00055     assertEquals(frameTransform, frameTransformTree.get("foo"));
00056   }
00057 
00058   @Test
00059   public void testUpdateAndGetWithTransformStampedMessage() {
00060     FrameTransform frameTransform =
00061         new FrameTransform(Transform.identity(), nameResolver.resolve("foo"),
00062             nameResolver.resolve("bar"), new Time());
00063     frameTransformTree.update(newTransformStampedMessage(Transform.identity(), "foo", "bar",
00064         new Time()));
00065     assertEquals(frameTransform, frameTransformTree.get("foo"));
00066   }
00067 
00068   private geometry_msgs.TransformStamped newTransformStampedMessage(Transform transform,
00069       String source, String target, Time time) {
00070     geometry_msgs.TransformStamped message =
00071         messageFactory.newFromType(geometry_msgs.TransformStamped._TYPE);
00072     FrameTransform frameTransform =
00073         new FrameTransform(transform, nameResolver.resolve(source), nameResolver.resolve(target),
00074             time);
00075     frameTransform.toTransformStampedMessage(message);
00076     return message;
00077   }
00078 
00079   @Test
00080   public void testIdentityTransforms() {
00081     frameTransformTree.update(newTransformStampedMessage(Transform.identity(), "baz", "bar",
00082         new Time()));
00083     frameTransformTree.update(newTransformStampedMessage(Transform.identity(), "bar", "foo",
00084         new Time()));
00085 
00086     // Full tree transform.
00087     {
00088       FrameTransform frameTransform = frameTransformTree.transform("baz", "foo");
00089       assertTrue(frameTransform != null);
00090       assertEquals(nameResolver.resolve("baz"), frameTransform.getSourceFrame());
00091       assertEquals(nameResolver.resolve("foo"), frameTransform.getTargetFrame());
00092       assertEquals(Transform.identity(), frameTransform.getTransform());
00093     }
00094 
00095     // Same node transform.
00096     {
00097       FrameTransform frameTransform = frameTransformTree.transform("baz", "baz");
00098       assertTrue(frameTransform != null);
00099       assertEquals(nameResolver.resolve("baz"), frameTransform.getSourceFrame());
00100       assertEquals(nameResolver.resolve("baz"), frameTransform.getTargetFrame());
00101       assertEquals(Transform.identity(), frameTransform.getTransform());
00102     }
00103 
00104     // Same node transform.
00105     {
00106       FrameTransform frameTransform = frameTransformTree.transform("bar", "bar");
00107       assertTrue(frameTransform != null);
00108       assertEquals(nameResolver.resolve("bar"), frameTransform.getSourceFrame());
00109       assertEquals(nameResolver.resolve("bar"), frameTransform.getTargetFrame());
00110       assertEquals(Transform.identity(), frameTransform.getTransform());
00111     }
00112 
00113     // Root-to-root transform.
00114     {
00115       FrameTransform frameTransform = frameTransformTree.transform("foo", "foo");
00116       assertTrue(frameTransform != null);
00117       assertEquals(nameResolver.resolve("foo"), frameTransform.getSourceFrame());
00118       assertEquals(nameResolver.resolve("foo"), frameTransform.getTargetFrame());
00119       assertEquals(Transform.identity(), frameTransform.getTransform());
00120     }
00121 
00122     // Root-to-leaf transform.
00123     {
00124       FrameTransform frameTransform = frameTransformTree.transform("foo", "baz");
00125       assertTrue(frameTransform != null);
00126       assertEquals(nameResolver.resolve("foo"), frameTransform.getSourceFrame());
00127       assertEquals(nameResolver.resolve("baz"), frameTransform.getTargetFrame());
00128       assertEquals(Transform.identity(), frameTransform.getTransform());
00129     }
00130   }
00131 
00141   private void updateFrameTransformTree() {
00142     {
00143       Transform transform = Transform.translation(0, 1, 0);
00144       frameTransformTree.update(newTransformStampedMessage(transform, "bar", "foo", new Time()));
00145     }
00146     {
00147       Transform transform = Transform.xRotation(Math.PI / 2);
00148       frameTransformTree.update(newTransformStampedMessage(transform, "baz", "bar", new Time()));
00149     }
00150     {
00151       Transform transform = Transform.translation(1, 0, 0);
00152       frameTransformTree.update(newTransformStampedMessage(transform, "bop", "foo", new Time()));
00153     }
00154     {
00155       Transform transform = Transform.yRotation(Math.PI / 2);
00156       frameTransformTree.update(newTransformStampedMessage(transform, "fuz", "bop", new Time()));
00157     }
00158   }
00159 
00160   private void checkBazToFooTransform(FrameTransform frameTransform) {
00161     // If we were to reverse the order of the transforms in our implementation,
00162     // we would expect the translation vector to be <0, 0, 1> instead.
00163     Transform transform = Transform.translation(0, 1, 0).multiply(Transform.xRotation(Math.PI / 2));
00164     Quaternion rotationAndScale = transform.getRotationAndScale();
00165     assertTrue(String.format("%s is not neutral.", rotationAndScale),
00166         rotationAndScale.isAlmostNeutral(1e-9));
00167     assertEquals(nameResolver.resolve("baz"), frameTransform.getSourceFrame());
00168     assertEquals(nameResolver.resolve("foo"), frameTransform.getTargetFrame());
00169     assertTrue(transform.almostEquals(frameTransform.getTransform(), 1e-9));
00170   }
00171 
00172   @Test
00173   public void testTransformBazToRoot() {
00174     updateFrameTransformTree();
00175     checkBazToFooTransform(frameTransformTree.transformToRoot(nameResolver.resolve("baz")));
00176   }
00177 
00178   @Test
00179   public void testTransformBazToFoo() {
00180     updateFrameTransformTree();
00181     checkBazToFooTransform(frameTransformTree.transform("baz", "foo"));
00182     checkBazToFooTransform(frameTransformTree.transform("foo", "baz").invert());
00183   }
00184 
00185   private void checkFuzToFooTransform(FrameTransform frameTransform) {
00186     // If we were to reverse the order of the transforms in our implementation,
00187     // we would expect the translation vector to be <0, 0, 1> instead.
00188     Transform transform = Transform.translation(1, 0, 0).multiply(Transform.yRotation(Math.PI / 2));
00189     Quaternion rotationAndScale = transform.getRotationAndScale();
00190     assertTrue(String.format("%s is not neutral.", rotationAndScale),
00191         rotationAndScale.isAlmostNeutral(1e-9));
00192     assertEquals(nameResolver.resolve("fuz"), frameTransform.getSourceFrame());
00193     assertEquals(nameResolver.resolve("foo"), frameTransform.getTargetFrame());
00194     assertTrue(String.format("Expected %s != %s", transform, frameTransform.getTransform()),
00195         transform.almostEquals(frameTransform.getTransform(), 1e-9));
00196   }
00197 
00198   @Test
00199   public void testTransformFuzToRoot() {
00200     updateFrameTransformTree();
00201     checkFuzToFooTransform(frameTransformTree.transformToRoot(nameResolver.resolve("fuz")));
00202   }
00203 
00204   @Test
00205   public void testTransformFuzToFoo() {
00206     updateFrameTransformTree();
00207     checkFuzToFooTransform(frameTransformTree.transform("fuz", "foo"));
00208     checkFuzToFooTransform(frameTransformTree.transform("foo", "fuz").invert());
00209   }
00210 
00211   @Test
00212   public void testTransformBazToFuz() {
00213     updateFrameTransformTree();
00214     FrameTransform frameTransform = frameTransformTree.transform("baz", "fuz");
00215     Transform transform =
00216         Transform.yRotation(Math.PI / 2).invert().multiply(Transform.translation(1, 0, 0).invert())
00217             .multiply(Transform.translation(0, 1, 0)).multiply(Transform.xRotation(Math.PI / 2));
00218     assertTrue(transform.getRotationAndScale().isAlmostNeutral(1e-9));
00219     assertEquals(nameResolver.resolve("baz"), frameTransform.getSourceFrame());
00220     assertEquals(nameResolver.resolve("fuz"), frameTransform.getTargetFrame());
00221     assertTrue(String.format("Expected %s != %s", transform, frameTransform.getTransform()),
00222         transform.almostEquals(frameTransform.getTransform(), 1e-9));
00223   }
00224 
00225   @Test
00226   public void testTimeTravel() {
00227     FrameTransform frameTransform1 =
00228         new FrameTransform(Transform.identity(), nameResolver.resolve("foo"),
00229             nameResolver.resolve("bar"), new Time());
00230     FrameTransform frameTransform2 =
00231         new FrameTransform(Transform.identity(), nameResolver.resolve("foo"),
00232             nameResolver.resolve("bar"), new Time(2));
00233     FrameTransform frameTransform3 =
00234         new FrameTransform(Transform.identity(), nameResolver.resolve("foo"),
00235             nameResolver.resolve("bar"), new Time(4));
00236     frameTransformTree.update(frameTransform1);
00237     frameTransformTree.update(frameTransform2);
00238     frameTransformTree.update(frameTransform3);
00239     assertEquals(frameTransform3, frameTransformTree.get("foo"));
00240     assertEquals(frameTransform1, frameTransformTree.get("foo", new Time()));
00241     assertEquals(frameTransform1, frameTransformTree.get("foo", new Time(0.5)));
00242     assertEquals(frameTransform2, frameTransformTree.get("foo", new Time(1.5)));
00243     assertEquals(frameTransform2, frameTransformTree.get("foo", new Time(2)));
00244     assertEquals(frameTransform3, frameTransformTree.get("foo", new Time(10)));
00245   }
00246 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49