1 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 { 37 38 Vec c; 39 double r, r2; 40 Vec v,b; 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 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 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 |