Debian Codegraph @

src/dr/geo/math/Vector3D.java

raw symbols SCIP
1 /*
2 * Vector3D.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo.math;
27
28 public final class Vector3D {
29
30 /** Component X */
31 protected double x;
32 /** Component Y */
33 protected double y;
34 /** Component Z */
35 protected double z;
36
37 /** Origin Vector3D */
38 public static final Vector3D ORIGIN = new Vector3D(0.0, 0.0, 0.0);
39
40 /**
41 * Create a Vector3D by components.
42 * @param x component X
43 * @param y component Y
44 * @param z component Z
45 */
46 public Vector3D(double x, double y, double z) {
47 this.x = x;
48 this.y = y;
49 this.z = z;
50 }
51
52 /**
53 * Create a Vector3D from another Vector3D.
54 */
55 public Vector3D(Vector3D a) {
56 this.x = a.x;
57 this.y = a.y;
58 this.z = a.z;
59 }
60
61 /**
62 * add two Vector3Ds together.
63 * @param a Vector3D to add to this one
64 * @return a new <code>Vector3D</code>
65 */
66 public Vector3D add(Vector3D a) {
67 return (new Vector3D(x + a.x, y + a.y, z + a.z));
68 }
69
70 /**
71 * Add two Vector3Ds (modifies this Vector3D).
72 * @param a Vector3D to add
73 * @return this <code>Vector3D</code>
74 */
75 public Vector3D addU(Vector3D a) {
76 x += a.x;
77 y += a.y;
78 z += a.z;
79 return (this);
80 }
81
82 /**
83 * Cross product of two Vector3Ds
84 */
85 public Vector3D cross(Vector3D a) {
86 return (new Vector3D(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x));
87 }
88
89 /**
90 * Dot product of two Vector3Ds
91 */
92 public double dot(Vector3D a) {
93 return (x * a.x + y * a.y + z * a.z);
94 }
95
96 public void negate() {
97 x = -x;
98 y = -y;
99 z = -z;
100 }
101
102 /**
103 * @return <code>true</code> if the given Vector3D is equivalent to this Vector3D.
104 */
105 public boolean equals(Object a) {
106 if (!(a instanceof Vector3D))
107 return (false);
108 return ((x == ((Vector3D) a).x) && (x == ((Vector3D) a).y) && (x == ((Vector3D) a).z));
109 }
110
111 public Vector3D mirror(Vector3D a) {
112 return (this.add(a.mul(-2.0 * a.dot(this))));
113 }
114
115 public double getX() { return x; }
116 public double getY() { return y; }
117 public double getZ() { return z; }
118
119 public double getCoordinate(int index) {
120
121 switch (index) {
122 case 0: return x;
123 case 1: return y;
124 case 2: return z;
125 default: throw new IllegalArgumentException("illegal coordinates:" + index);
126 }
127 }
128
129 /**
130 *The modulus of this Vector3D
131 * @return modulus
132 */
133 public double modulus() {
134 return (Math.sqrt(x * x + y * y + z * z));
135 }
136
137 /**
138 * The modulus squared. <br>
139 * @return modulus^2
140 */
141 public double mod2() {
142 return (x * x + y * y + z * z);
143 }
144
145 /**
146 * Returns a new Vector3D that is this Vector3D multiplied by a scalar.
147 */
148 public Vector3D mul(double a) {
149 return (new Vector3D(x * a, y * a, z * a));
150 }
151
152 /**
153 * Multiplies this Vector3D by a scalar.
154 * @param a scalare da usare
155 * @return questo stesso oggetto <code>Vector3D</code>
156 */
157 public Vector3D mulU(double a) {
158 x *= a;
159 y *= a;
160 z *= a;
161 return (this);
162 }
163
164 /**
165 * Subtracts the given Vector3D from this and returns the result.
166 */
167 public Vector3D sub(Vector3D a) {
168 return (new Vector3D(x - a.x, y - a.y, z - a.z));
169 }
170
171 /**
172 * Subtracts the given Vector3D from this one.
173 */
174 public Vector3D subU(Vector3D a) {
175 x -= a.x;
176 y -= a.y;
177 z -= a.z;
178 return (this);
179 }
180
181 /**
182 * Returns this Vector3D with unit length.
183 */
184 public Vector3D normalized() {
185 return (this.mul(1.0 / this.modulus()));
186 }
187
188 /**
189 * Transforms this Vector3D to be unit length.
190 */
191 public Vector3D normalize() {
192 double im = 1.0 / this.modulus();
193 this.x *= im;
194 this.y *= im;
195 this.z *= im;
196 return (this);
197 }
198
199 public String toString() {
200 return ("Vector3D[" + x + "," + y + "," + z + "]");
201 }
202
203 }