~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/math/geometry/Vector3DTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
8
 
 *
9
 
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.commons.math.geometry;
19
 
 
20
 
import org.apache.commons.math.geometry.Vector3D;
21
 
 
22
 
import junit.framework.*;
23
 
 
24
 
public class Vector3DTest
25
 
  extends TestCase {
26
 
 
27
 
  public Vector3DTest(String name) {
28
 
    super(name);
29
 
  }
30
 
 
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,
36
 
                              -3, Vector3D.minusK),
37
 
                  2, 0, 3);
38
 
      checkVector(new Vector3D(2, Vector3D.plusI,
39
 
                               5, Vector3D.plusJ,
40
 
                              -3, Vector3D.minusK),
41
 
                  2, 5, 3);
42
 
      checkVector(new Vector3D(2, Vector3D.plusI,
43
 
                               5, Vector3D.plusJ,
44
 
                               5, Vector3D.minusJ,
45
 
                               -3, Vector3D.minusK),
46
 
                  2, 0, 3);
47
 
  }
48
 
 
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);
54
 
  }
55
 
  
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))
59
 
               < 1.0e-12);
60
 
  }
61
 
 
62
 
  public void testSubtract() {
63
 
 
64
 
    Vector3D v1 = new Vector3D(1, 2, 3);
65
 
    Vector3D v2 = new Vector3D(-3, -2, -1);
66
 
    v1 = v1.subtract(v2);
67
 
    checkVector(v1, 4, 4, 4);
68
 
 
69
 
    checkVector(v2.subtract(v1), -7, -6, -5);
70
 
    checkVector(v2.subtract(3, v1), -15, -14, -13);
71
 
 
72
 
  }
73
 
 
74
 
  public void testAdd() {
75
 
    Vector3D v1 = new Vector3D(1, 2, 3);
76
 
    Vector3D v2 = new Vector3D(-3, -2, -1);
77
 
    v1 = v1.add(v2);
78
 
    checkVector(v1, -2, 0, 2);
79
 
 
80
 
    checkVector(v2.add(v1), -5, -2, 1);
81
 
    checkVector(v2.add(3, v1), -9, -2, 5);
82
 
 
83
 
  }
84
 
 
85
 
  public void testScalarProduct() {
86
 
    Vector3D v = new Vector3D(1, 2, 3);
87
 
    v = v.scalarMultiply(3);
88
 
    checkVector(v, 3, 6, 9);
89
 
 
90
 
    checkVector(v.scalarMultiply(0.5), 1.5, 3, 4.5);
91
 
 
92
 
  }
93
 
 
94
 
  public void testVectorialProducts() {
95
 
    Vector3D v1 = new Vector3D(2, 1, -4);
96
 
    Vector3D v2 = new Vector3D(3, 1, -1);
97
 
 
98
 
    assertTrue(Math.abs(Vector3D.dotProduct(v1, v2) - 11) < 1.0e-12);
99
 
 
100
 
    Vector3D v3 = Vector3D.crossProduct(v1, v2);
101
 
    checkVector(v3, 3, -10, -1);
102
 
 
103
 
    assertTrue(Math.abs(Vector3D.dotProduct(v1, v3)) < 1.0e-12);
104
 
    assertTrue(Math.abs(Vector3D.dotProduct(v2, v3)) < 1.0e-12);
105
 
 
106
 
  }
107
 
 
108
 
  public void testAngular() {
109
 
 
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);
116
 
 
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);
120
 
 
121
 
  }
122
 
 
123
 
  public void testAngularSeparation() {
124
 
    Vector3D v1 = new Vector3D(2, -1, 4);
125
 
 
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)));
129
 
 
130
 
    assertTrue(Math.abs(Vector3D.angle(v1, v2) - 1.2) < 1.0e-12);
131
 
 
132
 
  }
133
 
 
134
 
  public void testNormalize() {
135
 
    assertEquals(1.0, new Vector3D(5, -4, 2).normalize().getNorm(), 1.0e-12);
136
 
    try {
137
 
        new Vector3D().normalize();
138
 
        fail("an exception should have been thrown");
139
 
    } catch (ArithmeticException ae) {
140
 
        // expected behavior
141
 
    } catch (Exception e) {
142
 
        fail("wrong exception caught: " + e.getMessage());
143
 
    }
144
 
  }
145
 
 
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);
153
 
      try {
154
 
          new Vector3D(0, 0, 0).orthogonal();
155
 
          fail("an exception should have been thrown");
156
 
      } catch (ArithmeticException ae) {
157
 
          // expected behavior
158
 
      } catch (Exception e) {
159
 
          fail("wrong exception caught: " + e.getMessage());
160
 
      }
161
 
  }
162
 
 
163
 
  public void testAngle() {
164
 
     assertEquals(0.22572612855273393616, 
165
 
                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(4, 5, 6)),
166
 
                  1.0e-12);
167
 
     assertEquals(7.98595620686106654517199e-8, 
168
 
                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(2, 4, 6.000001)),
169
 
                  1.0e-12);
170
 
     assertEquals(3.14159257373023116985197793156, 
171
 
                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(-2, -4, -6.000001)),
172
 
                  1.0e-12);
173
 
     try {
174
 
         Vector3D.angle(new Vector3D(), Vector3D.plusI);
175
 
         fail("an exception should have been thrown");
176
 
     } catch (ArithmeticException ae) {
177
 
         // expected behavior
178
 
     } catch (Exception e) {
179
 
         fail("wrong exception caught: " + e.getMessage());
180
 
     }
181
 
  }
182
 
 
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);
187
 
  }
188
 
  
189
 
  public static Test suite() {
190
 
    return new TestSuite(Vector3DTest.class);
191
 
  }
192
 
 
193
 
}