Ported over bamboo benchmarks for use as non-Bamboo java benchmarks.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / RayTracer / Vec.java
1 /**************************************************************************
2  *                                                                         *
3  *             Java Grande Forum Benchmark Suite - Version 2.0             *
4  *                                                                         *
5  *                            produced by                                  *
6  *                                                                         *
7  *                  Java Grande Benchmarking Project                       *
8  *                                                                         *
9  *                                at                                       *
10  *                                                                         *
11  *                Edinburgh Parallel Computing Centre                      *
12  *                                                                         *
13  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
14  *                                                                         *
15  *                 Original version of this code by                        *
16  *            Florian Doyon (Florian.Doyon@sophia.inria.fr)                *
17  *              and  Wilfried Klauser (wklauser@acm.org)                   *
18  *                                                                         *
19  *      This version copyright (c) The University of Edinburgh, 1999.      *
20  *                         All rights reserved.                            *
21  *                                                                         *
22  **************************************************************************/
23
24
25
26
27 /**
28  * This class reflects the 3d vectors used in 3d computations
29  */
30 public class Vec
31 //implements java.io.Serializable 
32 {
33
34   /**
35    * The x coordinate
36    */
37   public float x; 
38
39   /**
40    * The y coordinate
41    */
42   public float y;
43
44   /**
45    * The z coordinate
46    */
47   public float z;
48
49   /**
50    * Constructor
51    * @param a the x coordinate
52    * @param b the y coordinate
53    * @param c the z coordinate
54    */
55   public Vec(float a, float b, float c) {
56     x = a;
57     y = b;
58     z = c;
59   }
60
61   /**
62    * Copy constructor
63    */
64   public Vec(Vec a) {
65     x = a.x;
66     y = a.y;
67     z = a.z;
68   }
69   /**
70    * Default (0,0,0) constructor
71    */
72   public Vec() {
73     x = (float) 0.0;
74     y = (float) 0.0; 
75     z = (float) 0.0;
76   }
77
78   /**
79    * Add a vector to the current vector
80    * @param: a The vector to be added
81    */
82   public final void add(Vec a) {
83     x+=a.x;
84     y+=a.y;
85     z+=a.z;
86   }  
87
88   /**
89    * adds: Returns a new vector such as
90    * new = sA + B
91    */
92   public static Vec adds(float s, Vec a, Vec b) {
93     return new Vec(s * a.x + b.x, s * a.y + b.y, s * a.z + b.z);
94   }
95
96   /**
97    * Adds vector such as:
98    * this+=sB
99    * @param: s The multiplier
100    * @param: b The vector to be added
101    */
102   public final void adds(float s,Vec b){
103     x+=s*b.x;
104     y+=s*b.y;
105     z+=s*b.z;
106   }
107
108   /**
109    * Substracs two vectors
110    */
111   public static Vec sub(Vec a, Vec b) {
112     return new Vec(a.x - b.x, a.y - b.y, a.z - b.z);
113   }
114
115   /**
116    * Substracts two vects and places the results in the current vector
117    * Used for speedup with local variables -there were too much Vec to be gc'ed
118    * Consumes about 10 units, whether sub consumes nearly 999 units!! 
119    * cf thinking in java p. 831,832
120    */
121   public final void sub2(Vec a,Vec b) {
122     this.x=a.x-b.x;
123     this.y=a.y-b.y;
124     this.z=a.z-b.z;
125   }
126
127   public static Vec mult(Vec a, Vec b) {
128     return new Vec(a.x * b.x, a.y * b.y, a.z * b.z);
129   }
130
131   public static Vec cross(Vec a, Vec b) {
132     return
133     new Vec(a.y*b.z - a.z*b.y,
134         a.z*b.x - a.x*b.z,
135         a.x*b.y - a.y*b.x);
136   }
137
138   public static float dot(Vec a, Vec b) {
139     return a.x*b.x + a.y*b.y + a.z*b.z;
140   }
141
142   public static Vec comb(float a, Vec A, float b, Vec B) {
143     return
144     new Vec(a * A.x + b * B.x,
145         a * A.y + b * B.y,
146         a * A.z + b * B.z);
147   }
148
149   public final void comb2(float a,Vec A,float b,Vec B) {
150     x=a * A.x + b * B.x;
151     y=a * A.y + b * B.y;
152     z=a * A.z + b * B.z;      
153   }
154
155   public final void scale(float t) {
156     x *= t;
157     y *= t;
158     z *= t;
159   }
160
161   public final void negate() {
162     x = -x;
163     y = -y;
164     z = -z;
165   }
166
167   public final float normalize() {
168     float len;
169     len =(float)  Math.sqrt(x*x + y*y + z*z);
170     if (len > 0.0) {
171       x /= len;
172       y /= len;
173       z /= len;
174     }
175     return len;
176   }
177
178   public final String toString() {
179     return "<" + x + "," + y + "," + z + ">";
180   }
181 }