KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > c3d > geom > Vec


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library 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 GNU
18 * 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 this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.c3d.geom;
32
33 /**
34  * This class reflects the 3d vectors used in 3d computations
35  */

36 public class Vec implements java.io.Serializable JavaDoc {
37
38   /**
39    * The x coordinate
40    */

41   public double x;
42   /**
43    * The y coordinate
44    */

45   public double y;
46   /**
47    * The z coordinate
48    */

49   public double z;
50
51
52   /**
53    * Constructor
54    * @param a the x coordinate
55    * @param b the y coordinate
56    * @param c the z coordinate
57    */

58   public Vec(double a, double b, double c) {
59     x = a;
60     y = b;
61     z = c;
62   }
63
64
65   /**
66    * Copy constructor
67    */

68   public Vec(Vec a) {
69     x = a.x;
70     y = a.y;
71     z = a.z;
72   }
73
74
75   /**
76    * Default (0,0,0) constructor
77    */

78   public Vec() {
79     x = 0.0;
80     y = 0.0;
81     z = 0.0;
82   }
83
84
85   /**
86    * Add a vector to the current vector
87    * @param: a The vector to be added
88    */

89   public final void add(Vec a) {
90     x += a.x;
91     y += a.y;
92     z += a.z;
93   }
94
95
96   /**
97    * adds: Returns a new vector such as
98    * new = sA + B
99    */

100   public static Vec adds(double s, Vec a, Vec b) {
101     return new Vec(s * a.x + b.x, s * a.y + b.y, s * a.z + b.z);
102   }
103
104
105   /**
106    * Adds vector such as:
107    * this+=sB
108    * @param: s The multiplier
109    * @param: b The vector to be added
110    */

111   public final void adds(double s, Vec b) {
112     x += s * b.x;
113     y += s * b.y;
114     z += s * b.z;
115   }
116
117
118   /**
119    * Substracs two vectors
120    */

121   public static Vec sub(Vec a, Vec b) {
122     return new Vec(a.x - b.x, a.y - b.y, a.z - b.z);
123   }
124
125
126   /**
127    * Substracts two vects and places the results in the current vector
128    * Used for speedup with local variables -there were too much Vec to be gc'ed
129    * Consumes about 10 units, whether sub consumes nearly 999 units!!
130    * cf thinking in java p. 831,832
131    */

132   public final void sub2(Vec a, Vec b) {
133     this.x = a.x - b.x;
134     this.y = a.y - b.y;
135     this.z = a.z - b.z;
136   }
137
138
139   public static Vec mult(Vec a, Vec b) {
140     return new Vec(a.x * b.x, a.y * b.y, a.z * b.z);
141   }
142
143
144   public static Vec cross(Vec a, Vec b) {
145     return new Vec(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
146   }
147
148
149   public static double dot(Vec a, Vec b) {
150     return a.x * b.x + a.y * b.y + a.z * b.z;
151   }
152
153
154   public static Vec comb(double a, Vec A, double b, Vec B) {
155     return new Vec(a * A.x + b * B.x, a * A.y + b * B.y, a * A.z + b * B.z);
156   }
157
158
159   public final void comb2(double a, Vec A, double b, Vec B) {
160     x = a * A.x + b * B.x;
161     y = a * A.y + b * B.y;
162     z = a * A.z + b * B.z;
163   }
164
165
166   public final void scale(double t) {
167     x *= t;
168     y *= t;
169     z *= t;
170   }
171
172
173   public final void negate() {
174     x = -x;
175     y = -y;
176     z = -z;
177   }
178
179
180   public final double normalize() {
181     double len;
182     len = Math.sqrt(x * x + y * y + z * z);
183     if (len > 0.0) {
184       x /= len;
185       y /= len;
186       z /= len;
187     }
188     return len;
189   }
190
191
192   public final String JavaDoc toString() {
193     return "<" + x + "," + y + "," + z + ">";
194   }
195 }
196
Popular Tags