Ported over bamboo benchmarks for use as non-Bamboo java benchmarks.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / RayTracer / Sphere.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 public class Sphere extends Primitive 
27 //implements java.io.Serializable 
28 {
29   Vec      c;
30   float   r, r2;
31 //Vec      v,b; // temporary vecs used to minimize the memory load
32
33
34   public Sphere(Vec center, float radius) {
35     super();
36     c = center;
37     r = radius;
38     r2 = r*r;
39 //  v=new Vec();
40 //  b=new Vec();
41   }
42
43   public float dot(float x1, float y1, float z1, float x2, float y2, float z2){
44
45     return x1*x2 + y1*y2 + z1*z2; 
46
47   }
48
49   public Isect intersect(Ray ry) {
50
51
52     float b, disc, t;
53     Isect ip;
54
55     float x=c.x-ry.P.x;
56     float y=c.y-ry.P.y;
57     float z=c.z-ry.P.z;
58
59     b=dot( x, y, z, ry.D.x, ry.D.y, ry.D.z);
60     disc = (float) (b*b -dot(x,y,z,x,y,z) + r2);
61     if (disc < 0.0) {
62       return null;
63     }
64     disc = (float) Math.sqrtf((float)disc);
65     t = (b - disc < 1e-6) ? b + disc : b - disc;
66     if (t < 1e-6) {
67       return null;
68     }
69     ip = new Isect();
70     ip.t = t;
71     ip.enter = dot(x,y,z,x,y,z) > r2 + 1e-6 ? 1 : 0;
72 //  ip.enter = Vec.dot(v, v) > r2 + 1e-6 ? 1 : 0;
73     ip.prim = this;
74     ip.surf = surf;
75     return ip;
76
77   }
78
79   public Vec normal(Vec p) {
80     Vec r;
81     r = Vec.sub(p, c);
82     r.normalize();
83     return r;
84   }
85
86   public String toString() {
87     return "Sphere {" + c.toString() + "," + r + "}";
88   }
89
90   public Vec getCenter() {
91     return c;
92   }
93   public void setCenter(Vec c) {
94     this.c = c;
95   }
96 }
97