2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.geometry;
20
import org.apache.commons.math.geometry.Vector3D;
22
import junit.framework.*;
24
public class Vector3DTest
27
public Vector3DTest(String name) {
31
public void testConstructors() {
32
double r = Math.sqrt(2) /2;
33
checkVector(new Vector3D(2, new Vector3D(Math.PI / 3, -Math.PI / 4)),
34
r, r * Math.sqrt(3), -2 * r);
35
checkVector(new Vector3D(2, Vector3D.plusI,
38
checkVector(new Vector3D(2, Vector3D.plusI,
42
checkVector(new Vector3D(2, Vector3D.plusI,
49
public void testCoordinates() {
50
Vector3D v = new Vector3D(1, 2, 3);
51
assertTrue(Math.abs(v.getX() - 1) < 1.0e-12);
52
assertTrue(Math.abs(v.getY() - 2) < 1.0e-12);
53
assertTrue(Math.abs(v.getZ() - 3) < 1.0e-12);
56
public void testNorm() {
57
assertTrue(Math.abs(new Vector3D().getNorm()) < 1.0e-12);
58
assertTrue(Math.abs(new Vector3D(1, 2, 3).getNorm() - Math.sqrt(14))
62
public void testSubtract() {
64
Vector3D v1 = new Vector3D(1, 2, 3);
65
Vector3D v2 = new Vector3D(-3, -2, -1);
67
checkVector(v1, 4, 4, 4);
69
checkVector(v2.subtract(v1), -7, -6, -5);
70
checkVector(v2.subtract(3, v1), -15, -14, -13);
74
public void testAdd() {
75
Vector3D v1 = new Vector3D(1, 2, 3);
76
Vector3D v2 = new Vector3D(-3, -2, -1);
78
checkVector(v1, -2, 0, 2);
80
checkVector(v2.add(v1), -5, -2, 1);
81
checkVector(v2.add(3, v1), -9, -2, 5);
85
public void testScalarProduct() {
86
Vector3D v = new Vector3D(1, 2, 3);
87
v = v.scalarMultiply(3);
88
checkVector(v, 3, 6, 9);
90
checkVector(v.scalarMultiply(0.5), 1.5, 3, 4.5);
94
public void testVectorialProducts() {
95
Vector3D v1 = new Vector3D(2, 1, -4);
96
Vector3D v2 = new Vector3D(3, 1, -1);
98
assertTrue(Math.abs(Vector3D.dotProduct(v1, v2) - 11) < 1.0e-12);
100
Vector3D v3 = Vector3D.crossProduct(v1, v2);
101
checkVector(v3, 3, -10, -1);
103
assertTrue(Math.abs(Vector3D.dotProduct(v1, v3)) < 1.0e-12);
104
assertTrue(Math.abs(Vector3D.dotProduct(v2, v3)) < 1.0e-12);
108
public void testAngular() {
110
assertEquals(0, Vector3D.plusI.getAlpha(), 1.0e-10);
111
assertEquals(0, Vector3D.plusI.getDelta(), 1.0e-10);
112
assertEquals(Math.PI / 2, Vector3D.plusJ.getAlpha(), 1.0e-10);
113
assertEquals(0, Vector3D.plusJ.getDelta(), 1.0e-10);
114
assertEquals(0, Vector3D.plusK.getAlpha(), 1.0e-10);
115
assertEquals(Math.PI / 2, Vector3D.plusK.getDelta(), 1.0e-10);
117
Vector3D u = new Vector3D(-1, 1, -1);
118
assertEquals(3 * Math.PI /4, u.getAlpha(), 1.0e-10);
119
assertEquals(-1.0 / Math.sqrt(3), Math.sin(u.getDelta()), 1.0e-10);
123
public void testAngularSeparation() {
124
Vector3D v1 = new Vector3D(2, -1, 4);
126
Vector3D k = v1.normalize();
127
Vector3D i = k.orthogonal();
128
Vector3D v2 = k.scalarMultiply(Math.cos(1.2)).add(i.scalarMultiply(Math.sin(1.2)));
130
assertTrue(Math.abs(Vector3D.angle(v1, v2) - 1.2) < 1.0e-12);
134
public void testNormalize() {
135
assertEquals(1.0, new Vector3D(5, -4, 2).normalize().getNorm(), 1.0e-12);
137
new Vector3D().normalize();
138
fail("an exception should have been thrown");
139
} catch (ArithmeticException ae) {
141
} catch (Exception e) {
142
fail("wrong exception caught: " + e.getMessage());
146
public void testOrthogonal() {
147
Vector3D v1 = new Vector3D(0.1, 2.5, 1.3);
148
assertEquals(0.0, Vector3D.dotProduct(v1, v1.orthogonal()), 1.0e-12);
149
Vector3D v2 = new Vector3D(2.3, -0.003, 7.6);
150
assertEquals(0.0, Vector3D.dotProduct(v2, v2.orthogonal()), 1.0e-12);
151
Vector3D v3 = new Vector3D(-1.7, 1.4, 0.2);
152
assertEquals(0.0, Vector3D.dotProduct(v3, v3.orthogonal()), 1.0e-12);
154
new Vector3D(0, 0, 0).orthogonal();
155
fail("an exception should have been thrown");
156
} catch (ArithmeticException ae) {
158
} catch (Exception e) {
159
fail("wrong exception caught: " + e.getMessage());
163
public void testAngle() {
164
assertEquals(0.22572612855273393616,
165
Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(4, 5, 6)),
167
assertEquals(7.98595620686106654517199e-8,
168
Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(2, 4, 6.000001)),
170
assertEquals(3.14159257373023116985197793156,
171
Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(-2, -4, -6.000001)),
174
Vector3D.angle(new Vector3D(), Vector3D.plusI);
175
fail("an exception should have been thrown");
176
} catch (ArithmeticException ae) {
178
} catch (Exception e) {
179
fail("wrong exception caught: " + e.getMessage());
183
private void checkVector(Vector3D v, double x, double y, double z) {
184
assertEquals(x, v.getX(), 1.0e-12);
185
assertEquals(y, v.getY(), 1.0e-12);
186
assertEquals(z, v.getZ(), 1.0e-12);
189
public static Test suite() {
190
return new TestSuite(Vector3DTest.class);