KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > c3d > prim > Sphere


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.prim;
32
33 import org.objectweb.proactive.examples.c3d.geom.Ray;
34 import org.objectweb.proactive.examples.c3d.geom.Vec;
35
36 public class Sphere extends Primitive implements java.io.Serializable JavaDoc {
37
38   Vec c;
39   double r, r2;
40   Vec v,b; // temporary vecs used to minimize the memory load
41

42   public Sphere(Vec center, double radius) {
43     c = center;
44     r = radius;
45     r2 = r * r;
46     v = new Vec();
47     b = new Vec();
48   }
49
50
51   /**
52    * Modified intersection method - creates _much_ less Vecs
53    * @author Doyon Florian
54    * @author Wilfried Klauser
55    */

56   public Isect intersect(Ray ry) {
57     double b, disc, t;
58     Isect ip;
59     v.sub2(c, ry.P);
60     b = Vec.dot(v, ry.D);
61     disc = b * b - Vec.dot(v, v) + r2;
62     if (disc < 0.0) {
63       return null;
64     }
65     disc = Math.sqrt(disc);
66     t = (b - disc < 1e-6) ? b + disc : b - disc;
67     if (t < 1e-6) {
68       return null;
69     }
70     ip = new Isect();
71     ip.t = t;
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
87   public String JavaDoc toString() {
88     return "Sphere {" + c.toString() + "," + r + "}";
89   }
90
91
92   public Vec getCenter() {
93     return c;
94   }
95
96
97   public void setCenter(Vec c) {
98     this.c = c;
99   }
100 }
101
Popular Tags