QuaternionTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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 
00021 import org.junit.Test;
00022 
00026 public class QuaternionTest {
00027 
00028   @Test
00029   public void testAxisAngleToQuaternion() {
00030     Quaternion quaternion;
00031 
00032     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), 0);
00033     assertEquals(0, quaternion.getX(), 1e-9);
00034     assertEquals(0, quaternion.getY(), 1e-9);
00035     assertEquals(0, quaternion.getZ(), 1e-9);
00036     assertEquals(1, quaternion.getW(), 1e-9);
00037 
00038     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI);
00039     assertEquals(0, quaternion.getX(), 1e-9);
00040     assertEquals(0, quaternion.getY(), 1e-9);
00041     assertEquals(1, quaternion.getZ(), 1e-9);
00042     assertEquals(0, quaternion.getW(), 1e-9);
00043 
00044     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2);
00045     assertEquals(0, quaternion.getX(), 1e-9);
00046     assertEquals(0, quaternion.getY(), 1e-9);
00047     assertEquals(0.7071067811865475, quaternion.getZ(), 1e-9);
00048     assertEquals(0.7071067811865475, quaternion.getW(), 1e-9);
00049 
00050     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), -Math.PI / 2);
00051     assertEquals(0, quaternion.getX(), 1e-9);
00052     assertEquals(0, quaternion.getY(), 1e-9);
00053     assertEquals(-0.7071067811865475, quaternion.getZ(), 1e-9);
00054     assertEquals(0.7071067811865475, quaternion.getW(), 1e-9);
00055 
00056     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), 0.75 * Math.PI);
00057     assertEquals(0, quaternion.getX(), 1e-9);
00058     assertEquals(0, quaternion.getY(), 1e-9);
00059     assertEquals(0.9238795325112867, quaternion.getZ(), 1e-9);
00060     assertEquals(0.38268343236508984, quaternion.getW(), 1e-9);
00061 
00062     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), -0.75 * Math.PI);
00063     assertEquals(0, quaternion.getX(), 1e-9);
00064     assertEquals(0, quaternion.getY(), 1e-9);
00065     assertEquals(-0.9238795325112867, quaternion.getZ(), 1e-9);
00066     assertEquals(0.38268343236508984, quaternion.getW(), 1e-9);
00067 
00068     quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), 1.5 * Math.PI);
00069     assertEquals(0, quaternion.getX(), 1e-9);
00070     assertEquals(0, quaternion.getY(), 1e-9);
00071     assertEquals(0.7071067811865475, quaternion.getZ(), 1e-9);
00072     assertEquals(-0.7071067811865475, quaternion.getW(), 1e-9);
00073   }
00074 
00075   @Test
00076   public void testInvert() {
00077     Quaternion inverse = Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2).invert();
00078     assertEquals(0, inverse.getX(), 1e-9);
00079     assertEquals(0, inverse.getY(), 1e-9);
00080     assertEquals(-0.7071067811865475, inverse.getZ(), 1e-9);
00081     assertEquals(0.7071067811865475, inverse.getW(), 1e-9);
00082   }
00083 
00084   @Test
00085   public void testMultiply() {
00086     Quaternion quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2);
00087     Quaternion inverse = quaternion.invert();
00088     Quaternion rotated = quaternion.multiply(inverse);
00089     assertEquals(1, rotated.getW(), 1e-9);
00090   }
00091 
00092   @Test
00093   public void testRotateVector() {
00094     Quaternion quaternion = Quaternion.fromAxisAngle(Vector3.zAxis(), Math.PI / 2);
00095     Vector3 vector = new Vector3(1, 0, 0);
00096     Vector3 rotated = quaternion.rotateAndScaleVector(vector);
00097     assertEquals(0, rotated.getX(), 1e-9);
00098     assertEquals(1, rotated.getY(), 1e-9);
00099     assertEquals(0, rotated.getZ(), 1e-9);
00100   }
00101 }


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